1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/tracefile.c
38 * Author: Zach Brown <zab@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_LNET
44 #define LUSTRE_TRACEFILE_PRIVATE
45 #include "tracefile.h"
47 #include <libcfs/libcfs.h>
49 /* XXX move things up to the top, comment */
50 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[CFS_NR_CPUS] __cacheline_aligned;
52 char cfs_tracefile[TRACEFILE_NAME_SIZE];
53 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
54 static struct tracefiled_ctl trace_tctl;
55 cfs_semaphore_t cfs_trace_thread_sem;
56 static int thread_running = 0;
58 cfs_atomic_t cfs_tage_allocated = CFS_ATOMIC_INIT(0);
60 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
61 struct cfs_trace_cpu_data *tcd);
63 static inline struct cfs_trace_page *
64 cfs_tage_from_list(cfs_list_t *list)
66 return cfs_list_entry(list, struct cfs_trace_page, linkage);
69 static struct cfs_trace_page *cfs_tage_alloc(int gfp)
72 struct cfs_trace_page *tage;
75 * Don't spam console with allocation failures: they will be reported
76 * by upper layer anyway.
78 gfp |= CFS_ALLOC_NOWARN;
79 page = cfs_alloc_page(gfp);
83 tage = cfs_alloc(sizeof(*tage), gfp);
90 cfs_atomic_inc(&cfs_tage_allocated);
94 static void cfs_tage_free(struct cfs_trace_page *tage)
96 __LASSERT(tage != NULL);
97 __LASSERT(tage->page != NULL);
99 cfs_free_page(tage->page);
101 cfs_atomic_dec(&cfs_tage_allocated);
104 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
107 __LASSERT(tage != NULL);
108 __LASSERT(queue != NULL);
110 cfs_list_move_tail(&tage->linkage, queue);
113 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, int gfp,
119 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
120 * from here: this will lead to infinite recursion.
123 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
124 struct cfs_trace_page *tage;
126 tage = cfs_tage_alloc(gfp);
129 cfs_list_add_tail(&tage->linkage, stock);
134 /* return a page that has 'len' bytes left at the end */
135 static struct cfs_trace_page *
136 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
138 struct cfs_trace_page *tage;
140 if (tcd->tcd_cur_pages > 0) {
141 __LASSERT(!cfs_list_empty(&tcd->tcd_pages));
142 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
143 if (tage->used + len <= CFS_PAGE_SIZE)
147 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
148 if (tcd->tcd_cur_stock_pages > 0) {
149 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
150 -- tcd->tcd_cur_stock_pages;
151 cfs_list_del_init(&tage->linkage);
153 tage = cfs_tage_alloc(CFS_ALLOC_ATOMIC);
155 if (printk_ratelimit())
156 printk(CFS_KERN_WARNING
157 "cannot allocate a tage (%ld)\n",
164 tage->cpu = cfs_smp_processor_id();
165 tage->type = tcd->tcd_type;
166 cfs_list_add_tail(&tage->linkage, &tcd->tcd_pages);
167 tcd->tcd_cur_pages++;
169 if (tcd->tcd_cur_pages > 8 && thread_running) {
170 struct tracefiled_ctl *tctl = &trace_tctl;
172 * wake up tracefiled to process some pages.
174 cfs_waitq_signal(&tctl->tctl_waitq);
181 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
183 int pgcount = tcd->tcd_cur_pages / 10;
184 struct page_collection pc;
185 struct cfs_trace_page *tage;
186 struct cfs_trace_page *tmp;
189 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
190 * from here: this will lead to infinite recursion.
193 if (printk_ratelimit())
194 printk(CFS_KERN_WARNING "debug daemon buffer overflowed; "
195 "discarding 10%% of pages (%d of %ld)\n",
196 pgcount + 1, tcd->tcd_cur_pages);
198 CFS_INIT_LIST_HEAD(&pc.pc_pages);
199 cfs_spin_lock_init(&pc.pc_lock);
201 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
202 struct cfs_trace_page, linkage) {
206 cfs_list_move_tail(&tage->linkage, &pc.pc_pages);
207 tcd->tcd_cur_pages--;
209 put_pages_on_tcd_daemon_list(&pc, tcd);
212 /* return a page that has 'len' bytes left at the end */
213 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
216 struct cfs_trace_page *tage;
219 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
220 * from here: this will lead to infinite recursion.
223 if (len > CFS_PAGE_SIZE) {
225 "cowardly refusing to write %lu bytes in a page\n", len);
229 tage = cfs_trace_get_tage_try(tcd, len);
234 if (tcd->tcd_cur_pages > 0) {
235 tage = cfs_tage_from_list(tcd->tcd_pages.next);
237 cfs_tage_to_tail(tage, &tcd->tcd_pages);
242 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
243 const char *file, const char *fn, const int line,
244 const char *format1, va_list args,
245 const char *format2, ...)
247 struct cfs_trace_cpu_data *tcd = NULL;
248 struct ptldebug_header header = {0};
249 struct cfs_trace_page *tage;
250 /* string_buf is used only if tcd != NULL, and is always set then */
251 char *string_buf = NULL;
254 int needed = 85; /* average message length */
261 if (strchr(file, '/'))
262 file = strrchr(file, '/') + 1;
265 cfs_set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
267 tcd = cfs_trace_get_tcd();
268 if (tcd == NULL) /* arch may not log in IRQ context */
271 if (tcd->tcd_cur_pages == 0)
272 header.ph_flags |= PH_FLAG_FIRST_RECORD;
274 if (tcd->tcd_shutting_down) {
275 cfs_trace_put_tcd(tcd);
280 depth = __current_nesting_level();
281 known_size = strlen(file) + 1 + depth;
283 known_size += strlen(fn) + 1;
285 if (libcfs_debug_binary)
286 known_size += sizeof(header);
289 * '2' used because vsnprintf return real size required for output
290 * _without_ terminating NULL.
291 * if needed is to small for this format.
293 for (i = 0; i < 2; i++) {
294 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
296 if (needed + known_size > CFS_PAGE_SIZE)
299 cfs_trace_put_tcd(tcd);
304 string_buf = (char *)cfs_page_address(tage->page) +
305 tage->used + known_size;
307 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
309 printk(CFS_KERN_EMERG "negative max_nob: %i\n",
312 cfs_trace_put_tcd(tcd);
320 needed = vsnprintf(string_buf, max_nob, format1, ap);
325 remain = max_nob - needed;
329 va_start(ap, format2);
330 needed += vsnprintf(string_buf + needed, remain,
335 if (needed < max_nob) /* well. printing ok.. */
339 if (*(string_buf+needed-1) != '\n')
340 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
343 header.ph_len = known_size + needed;
344 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
346 if (libcfs_debug_binary) {
347 memcpy(debug_buf, &header, sizeof(header));
348 tage->used += sizeof(header);
349 debug_buf += sizeof(header);
352 /* indent message according to the nesting level */
353 while (depth-- > 0) {
354 *(debug_buf++) = '.';
358 strcpy(debug_buf, file);
359 tage->used += strlen(file) + 1;
360 debug_buf += strlen(file) + 1;
363 strcpy(debug_buf, fn);
364 tage->used += strlen(fn) + 1;
365 debug_buf += strlen(fn) + 1;
368 __LASSERT(debug_buf == string_buf);
370 tage->used += needed;
371 __LASSERT (tage->used <= CFS_PAGE_SIZE);
374 if ((mask & libcfs_printk) == 0) {
375 /* no console output requested */
377 cfs_trace_put_tcd(tcd);
382 if (libcfs_console_ratelimit &&
383 cdls->cdls_next != 0 && /* not first time ever */
384 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
385 /* skipping a console message */
388 cfs_trace_put_tcd(tcd);
392 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
393 libcfs_console_max_delay
394 + cfs_time_seconds(10))) {
395 /* last timeout was a long time ago */
396 cdls->cdls_delay /= libcfs_console_backoff * 4;
398 cdls->cdls_delay *= libcfs_console_backoff;
400 if (cdls->cdls_delay < libcfs_console_min_delay)
401 cdls->cdls_delay = libcfs_console_min_delay;
402 else if (cdls->cdls_delay > libcfs_console_max_delay)
403 cdls->cdls_delay = libcfs_console_max_delay;
406 /* ensure cdls_next is never zero after it's been seen */
407 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
411 cfs_print_to_console(&header, mask, string_buf, needed, file,
413 cfs_trace_put_tcd(tcd);
415 string_buf = cfs_trace_get_console_buffer();
418 if (format1 != NULL) {
420 needed = vsnprintf(string_buf,
421 CFS_TRACE_CONSOLE_BUFFER_SIZE,
425 if (format2 != NULL) {
426 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
428 va_start(ap, format2);
429 needed += vsnprintf(string_buf+needed, remain, format2, ap);
433 cfs_print_to_console(&header, mask,
434 string_buf, needed, file, fn);
436 cfs_trace_put_console_buffer(string_buf);
439 if (cdls != NULL && cdls->cdls_count != 0) {
440 string_buf = cfs_trace_get_console_buffer();
442 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
443 "Skipped %d previous similar message%s\n",
444 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
446 cfs_print_to_console(&header, mask,
447 string_buf, needed, file, fn);
449 cfs_trace_put_console_buffer(string_buf);
450 cdls->cdls_count = 0;
455 EXPORT_SYMBOL(libcfs_debug_vmsg2);
458 libcfs_assertion_failed(const char *expr, const char *file,
459 const char *func, const int line)
461 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
462 "ASSERTION(%s) failed\n", expr);
463 /* cfs_enter_debugger(); */
464 lbug_with_loc(file, func, line);
466 EXPORT_SYMBOL(libcfs_assertion_failed);
469 cfs_trace_assertion_failed(const char *str,
470 const char *fn, const char *file, int line)
472 struct ptldebug_header hdr;
474 libcfs_panic_in_progress = 1;
475 libcfs_catastrophe = 1;
478 cfs_set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
481 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
483 LIBCFS_PANIC("Lustre debug assertion failure\n");
489 panic_collect_pages(struct page_collection *pc)
491 /* Do the collect_pages job on a single CPU: assumes that all other
492 * CPUs have been stopped during a panic. If this isn't true for some
493 * arch, this will have to be implemented separately in each arch. */
496 struct cfs_trace_cpu_data *tcd;
498 CFS_INIT_LIST_HEAD(&pc->pc_pages);
500 cfs_tcd_for_each(tcd, i, j) {
501 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
502 tcd->tcd_cur_pages = 0;
504 if (pc->pc_want_daemon_pages) {
505 cfs_list_splice_init(&tcd->tcd_daemon_pages,
507 tcd->tcd_cur_daemon_pages = 0;
512 static void collect_pages_on_all_cpus(struct page_collection *pc)
514 struct cfs_trace_cpu_data *tcd;
517 cfs_spin_lock(&pc->pc_lock);
518 cfs_for_each_possible_cpu(cpu) {
519 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
520 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
521 tcd->tcd_cur_pages = 0;
522 if (pc->pc_want_daemon_pages) {
523 cfs_list_splice_init(&tcd->tcd_daemon_pages,
525 tcd->tcd_cur_daemon_pages = 0;
529 cfs_spin_unlock(&pc->pc_lock);
532 static void collect_pages(struct page_collection *pc)
534 CFS_INIT_LIST_HEAD(&pc->pc_pages);
536 if (libcfs_panic_in_progress)
537 panic_collect_pages(pc);
539 collect_pages_on_all_cpus(pc);
542 static void put_pages_back_on_all_cpus(struct page_collection *pc)
544 struct cfs_trace_cpu_data *tcd;
545 cfs_list_t *cur_head;
546 struct cfs_trace_page *tage;
547 struct cfs_trace_page *tmp;
550 cfs_spin_lock(&pc->pc_lock);
551 cfs_for_each_possible_cpu(cpu) {
552 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
553 cur_head = tcd->tcd_pages.next;
555 cfs_list_for_each_entry_safe_typed(tage, tmp,
557 struct cfs_trace_page,
560 __LASSERT_TAGE_INVARIANT(tage);
562 if (tage->cpu != cpu || tage->type != i)
565 cfs_tage_to_tail(tage, cur_head);
566 tcd->tcd_cur_pages++;
570 cfs_spin_unlock(&pc->pc_lock);
573 static void put_pages_back(struct page_collection *pc)
575 if (!libcfs_panic_in_progress)
576 put_pages_back_on_all_cpus(pc);
579 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
580 * we have a good amount of data at all times for dumping during an LBUG, even
581 * if we have been steadily writing (and otherwise discarding) pages via the
583 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
584 struct cfs_trace_cpu_data *tcd)
586 struct cfs_trace_page *tage;
587 struct cfs_trace_page *tmp;
589 cfs_spin_lock(&pc->pc_lock);
590 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
591 struct cfs_trace_page, linkage) {
593 __LASSERT_TAGE_INVARIANT(tage);
595 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
598 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
599 tcd->tcd_cur_daemon_pages++;
601 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
602 struct cfs_trace_page *victim;
604 __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
605 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
607 __LASSERT_TAGE_INVARIANT(victim);
609 cfs_list_del(&victim->linkage);
610 cfs_tage_free(victim);
611 tcd->tcd_cur_daemon_pages--;
614 cfs_spin_unlock(&pc->pc_lock);
617 static void put_pages_on_daemon_list(struct page_collection *pc)
619 struct cfs_trace_cpu_data *tcd;
622 cfs_for_each_possible_cpu(cpu) {
623 cfs_tcd_for_each_type_lock(tcd, i, cpu)
624 put_pages_on_tcd_daemon_list(pc, tcd);
628 void cfs_trace_debug_print(void)
630 struct page_collection pc;
631 struct cfs_trace_page *tage;
632 struct cfs_trace_page *tmp;
634 cfs_spin_lock_init(&pc.pc_lock);
636 pc.pc_want_daemon_pages = 1;
638 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
639 struct cfs_trace_page, linkage) {
643 __LASSERT_TAGE_INVARIANT(tage);
646 p = cfs_page_address(page);
647 while (p < ((char *)cfs_page_address(page) + tage->used)) {
648 struct ptldebug_header *hdr;
653 p += strlen(file) + 1;
656 len = hdr->ph_len - (int)(p - (char *)hdr);
658 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
663 cfs_list_del(&tage->linkage);
668 int cfs_tracefile_dump_all_pages(char *filename)
670 struct page_collection pc;
672 struct cfs_trace_page *tage;
673 struct cfs_trace_page *tmp;
678 cfs_tracefile_write_lock();
680 filp = cfs_filp_open(filename,
681 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
685 "LustreError: can't open %s for dump: rc %d\n",
690 cfs_spin_lock_init(&pc.pc_lock);
691 pc.pc_want_daemon_pages = 1;
693 if (cfs_list_empty(&pc.pc_pages)) {
698 /* ok, for now, just write the pages. in the future we'll be building
699 * iobufs with the pages and calling generic_direct_IO */
701 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
702 struct cfs_trace_page, linkage) {
704 __LASSERT_TAGE_INVARIANT(tage);
706 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
707 tage->used, cfs_filp_poff(filp));
708 if (rc != (int)tage->used) {
709 printk(CFS_KERN_WARNING "wanted to write %u but wrote "
710 "%d\n", tage->used, rc);
712 __LASSERT(cfs_list_empty(&pc.pc_pages));
715 cfs_list_del(&tage->linkage);
719 rc = cfs_filp_fsync(filp);
721 printk(CFS_KERN_ERR "sync returns %d\n", rc);
723 cfs_filp_close(filp);
725 cfs_tracefile_write_unlock();
729 void cfs_trace_flush_pages(void)
731 struct page_collection pc;
732 struct cfs_trace_page *tage;
733 struct cfs_trace_page *tmp;
735 cfs_spin_lock_init(&pc.pc_lock);
737 pc.pc_want_daemon_pages = 1;
739 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
740 struct cfs_trace_page, linkage) {
742 __LASSERT_TAGE_INVARIANT(tage);
744 cfs_list_del(&tage->linkage);
749 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
750 const char *usr_buffer, int usr_buffer_nob)
754 if (usr_buffer_nob > knl_buffer_nob)
757 if (cfs_copy_from_user((void *)knl_buffer,
758 (void *)usr_buffer, usr_buffer_nob))
761 nob = strnlen(knl_buffer, usr_buffer_nob);
762 while (nob-- >= 0) /* strip trailing whitespace */
763 if (!isspace(knl_buffer[nob]))
766 if (nob < 0) /* empty string */
769 if (nob == knl_buffer_nob) /* no space to terminate */
772 knl_buffer[nob + 1] = 0; /* terminate */
776 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
777 const char *knl_buffer, char *append)
779 /* NB if 'append' != NULL, it's a single character to append to the
780 * copied out string - usually "\n", for /proc entries and "" (i.e. a
781 * terminating zero byte) for sysctl entries */
782 int nob = strlen(knl_buffer);
784 if (nob > usr_buffer_nob)
785 nob = usr_buffer_nob;
787 if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
790 if (append != NULL && nob < usr_buffer_nob) {
791 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
799 EXPORT_SYMBOL(cfs_trace_copyout_string);
801 int cfs_trace_allocate_string_buffer(char **str, int nob)
803 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
806 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
813 void cfs_trace_free_string_buffer(char *str, int nob)
818 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
823 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
827 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
828 usr_str, usr_str_nob);
832 #if !defined(__WINNT__)
838 rc = cfs_tracefile_dump_all_pages(str);
840 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
844 int cfs_trace_daemon_command(char *str)
848 cfs_tracefile_write_lock();
850 if (strcmp(str, "stop") == 0) {
851 cfs_tracefile_write_unlock();
852 cfs_trace_stop_thread();
853 cfs_tracefile_write_lock();
854 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
856 } else if (strncmp(str, "size=", 5) == 0) {
857 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
858 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
859 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
861 cfs_tracefile_size <<= 20;
863 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
866 } else if (str[0] != '/') {
870 strcpy(cfs_tracefile, str);
873 "Lustre: debug daemon will attempt to start writing "
874 "to %s (%lukB max)\n", cfs_tracefile,
875 (long)(cfs_tracefile_size >> 10));
877 cfs_trace_start_thread();
880 cfs_tracefile_write_unlock();
884 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
889 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
893 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
894 usr_str, usr_str_nob);
896 rc = cfs_trace_daemon_command(str);
898 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
902 int cfs_trace_set_debug_mb(int mb)
907 int limit = cfs_trace_max_debug_mb();
908 struct cfs_trace_cpu_data *tcd;
910 if (mb < cfs_num_possible_cpus()) {
911 printk(KERN_ERR "Cannot set debug_mb to %d, the value should be >= %d\n",
912 mb, num_possible_cpus());
917 printk(CFS_KERN_ERR "Lustre: Refusing to set debug buffer size "
918 "to %dMB - limit is %d\n", mb, limit);
922 mb /= cfs_num_possible_cpus();
923 pages = mb << (20 - CFS_PAGE_SHIFT);
925 cfs_tracefile_write_lock();
927 cfs_tcd_for_each(tcd, i, j)
928 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
930 cfs_tracefile_write_unlock();
935 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
940 rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
944 return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
947 int cfs_trace_get_debug_mb(void)
951 struct cfs_trace_cpu_data *tcd;
954 cfs_tracefile_read_lock();
956 cfs_tcd_for_each(tcd, i, j)
957 total_pages += tcd->tcd_max_pages;
959 cfs_tracefile_read_unlock();
961 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
964 static int tracefiled(void *arg)
966 struct page_collection pc;
967 struct tracefiled_ctl *tctl = arg;
968 struct cfs_trace_page *tage;
969 struct cfs_trace_page *tmp;
976 /* we're started late enough that we pick up init's fs context */
977 /* this is so broken in uml? what on earth is going on? */
978 cfs_daemonize("ktracefiled");
980 cfs_spin_lock_init(&pc.pc_lock);
981 cfs_complete(&tctl->tctl_start);
984 cfs_waitlink_t __wait;
986 pc.pc_want_daemon_pages = 0;
988 if (cfs_list_empty(&pc.pc_pages))
992 cfs_tracefile_read_lock();
993 if (cfs_tracefile[0] != 0) {
994 filp = cfs_filp_open(cfs_tracefile,
995 O_CREAT | O_RDWR | O_LARGEFILE,
998 printk(CFS_KERN_WARNING "couldn't open %s: "
999 "%d\n", cfs_tracefile, rc);
1001 cfs_tracefile_read_unlock();
1003 put_pages_on_daemon_list(&pc);
1004 __LASSERT(cfs_list_empty(&pc.pc_pages));
1010 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1011 struct cfs_trace_page,
1013 static loff_t f_pos;
1015 __LASSERT_TAGE_INVARIANT(tage);
1017 if (f_pos >= (off_t)cfs_tracefile_size)
1019 else if (f_pos > (off_t)cfs_filp_size(filp))
1020 f_pos = cfs_filp_size(filp);
1022 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1023 tage->used, &f_pos);
1024 if (rc != (int)tage->used) {
1025 printk(CFS_KERN_WARNING "wanted to write %u "
1026 "but wrote %d\n", tage->used, rc);
1027 put_pages_back(&pc);
1028 __LASSERT(cfs_list_empty(&pc.pc_pages));
1033 cfs_filp_close(filp);
1034 put_pages_on_daemon_list(&pc);
1035 if (!cfs_list_empty(&pc.pc_pages)) {
1038 printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1040 printk(CFS_KERN_ERR "total cpus(%d): ",
1041 cfs_num_possible_cpus());
1042 for (i = 0; i < cfs_num_possible_cpus(); i++)
1043 if (cfs_cpu_online(i))
1044 printk(CFS_KERN_ERR "%d(on) ", i);
1046 printk(CFS_KERN_ERR "%d(off) ", i);
1047 printk(CFS_KERN_ERR "\n");
1050 cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1052 printk(CFS_KERN_ERR "page %d belongs to cpu "
1053 "%d\n", ++i, tage->cpu);
1054 printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1057 __LASSERT(cfs_list_empty(&pc.pc_pages));
1059 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1060 if (last_loop == 0) {
1067 cfs_waitlink_init(&__wait);
1068 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1069 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1070 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1071 cfs_time_seconds(1));
1072 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1074 cfs_complete(&tctl->tctl_stop);
1078 int cfs_trace_start_thread(void)
1080 struct tracefiled_ctl *tctl = &trace_tctl;
1083 cfs_mutex_down(&cfs_trace_thread_sem);
1087 cfs_init_completion(&tctl->tctl_start);
1088 cfs_init_completion(&tctl->tctl_stop);
1089 cfs_waitq_init(&tctl->tctl_waitq);
1090 cfs_atomic_set(&tctl->tctl_shutdown, 0);
1092 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1097 cfs_wait_for_completion(&tctl->tctl_start);
1100 cfs_mutex_up(&cfs_trace_thread_sem);
1104 void cfs_trace_stop_thread(void)
1106 struct tracefiled_ctl *tctl = &trace_tctl;
1108 cfs_mutex_down(&cfs_trace_thread_sem);
1109 if (thread_running) {
1110 printk(CFS_KERN_INFO
1111 "Lustre: shutting down debug daemon thread...\n");
1112 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1113 cfs_wait_for_completion(&tctl->tctl_stop);
1116 cfs_mutex_up(&cfs_trace_thread_sem);
1119 int cfs_tracefile_init(int max_pages)
1121 struct cfs_trace_cpu_data *tcd;
1127 rc = cfs_tracefile_init_arch();
1131 cfs_tcd_for_each(tcd, i, j) {
1132 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1133 factor = tcd->tcd_pages_factor;
1134 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1135 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1136 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1137 tcd->tcd_cur_pages = 0;
1138 tcd->tcd_cur_stock_pages = 0;
1139 tcd->tcd_cur_daemon_pages = 0;
1140 tcd->tcd_max_pages = (max_pages * factor) / 100;
1141 LASSERT(tcd->tcd_max_pages > 0);
1142 tcd->tcd_shutting_down = 0;
1148 static void trace_cleanup_on_all_cpus(void)
1150 struct cfs_trace_cpu_data *tcd;
1151 struct cfs_trace_page *tage;
1152 struct cfs_trace_page *tmp;
1155 cfs_for_each_possible_cpu(cpu) {
1156 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1157 tcd->tcd_shutting_down = 1;
1159 cfs_list_for_each_entry_safe_typed(tage, tmp,
1161 struct cfs_trace_page,
1163 __LASSERT_TAGE_INVARIANT(tage);
1165 cfs_list_del(&tage->linkage);
1166 cfs_tage_free(tage);
1169 tcd->tcd_cur_pages = 0;
1174 static void cfs_trace_cleanup(void)
1176 struct page_collection pc;
1178 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1179 cfs_spin_lock_init(&pc.pc_lock);
1181 trace_cleanup_on_all_cpus();
1183 cfs_tracefile_fini_arch();
1186 void cfs_tracefile_exit(void)
1188 cfs_trace_stop_thread();
1189 cfs_trace_cleanup();