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;
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_shutting_down) {
272 cfs_trace_put_tcd(tcd);
277 depth = __current_nesting_level();
278 known_size = strlen(file) + 1 + depth;
280 known_size += strlen(fn) + 1;
282 if (libcfs_debug_binary)
283 known_size += sizeof(header);
286 * '2' used because vsnprintf return real size required for output
287 * _without_ terminating NULL.
288 * if needed is to small for this format.
290 for (i = 0; i < 2; i++) {
291 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
293 if (needed + known_size > CFS_PAGE_SIZE)
296 cfs_trace_put_tcd(tcd);
301 string_buf = (char *)cfs_page_address(tage->page) +
302 tage->used + known_size;
304 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
306 printk(CFS_KERN_EMERG "negative max_nob: %i\n",
309 cfs_trace_put_tcd(tcd);
317 needed = vsnprintf(string_buf, max_nob, format1, ap);
322 remain = max_nob - needed;
326 va_start(ap, format2);
327 needed += vsnprintf(string_buf + needed, remain,
332 if (needed < max_nob) /* well. printing ok.. */
336 if (*(string_buf+needed-1) != '\n')
337 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
340 header.ph_len = known_size + needed;
341 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
343 if (libcfs_debug_binary) {
344 memcpy(debug_buf, &header, sizeof(header));
345 tage->used += sizeof(header);
346 debug_buf += sizeof(header);
349 /* indent message according to the nesting level */
350 while (depth-- > 0) {
351 *(debug_buf++) = '.';
355 strcpy(debug_buf, file);
356 tage->used += strlen(file) + 1;
357 debug_buf += strlen(file) + 1;
360 strcpy(debug_buf, fn);
361 tage->used += strlen(fn) + 1;
362 debug_buf += strlen(fn) + 1;
365 __LASSERT(debug_buf == string_buf);
367 tage->used += needed;
368 __LASSERT (tage->used <= CFS_PAGE_SIZE);
371 if ((mask & libcfs_printk) == 0) {
372 /* no console output requested */
374 cfs_trace_put_tcd(tcd);
379 if (libcfs_console_ratelimit &&
380 cdls->cdls_next != 0 && /* not first time ever */
381 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
382 /* skipping a console message */
385 cfs_trace_put_tcd(tcd);
389 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
390 libcfs_console_max_delay
391 + cfs_time_seconds(10))) {
392 /* last timeout was a long time ago */
393 cdls->cdls_delay /= libcfs_console_backoff * 4;
395 cdls->cdls_delay *= libcfs_console_backoff;
397 if (cdls->cdls_delay < libcfs_console_min_delay)
398 cdls->cdls_delay = libcfs_console_min_delay;
399 else if (cdls->cdls_delay > libcfs_console_max_delay)
400 cdls->cdls_delay = libcfs_console_max_delay;
403 /* ensure cdls_next is never zero after it's been seen */
404 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
408 cfs_print_to_console(&header, mask, string_buf, needed, file,
410 cfs_trace_put_tcd(tcd);
412 string_buf = cfs_trace_get_console_buffer();
415 if (format1 != NULL) {
417 needed = vsnprintf(string_buf,
418 CFS_TRACE_CONSOLE_BUFFER_SIZE,
422 if (format2 != NULL) {
423 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
425 va_start(ap, format2);
426 needed += vsnprintf(string_buf+needed, remain, format2, ap);
430 cfs_print_to_console(&header, mask,
431 string_buf, needed, file, fn);
433 cfs_trace_put_console_buffer(string_buf);
436 if (cdls != NULL && cdls->cdls_count != 0) {
437 string_buf = cfs_trace_get_console_buffer();
439 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
440 "Skipped %d previous similar message%s\n",
441 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
443 cfs_print_to_console(&header, mask,
444 string_buf, needed, file, fn);
446 cfs_trace_put_console_buffer(string_buf);
447 cdls->cdls_count = 0;
452 EXPORT_SYMBOL(libcfs_debug_vmsg2);
455 libcfs_assertion_failed(const char *expr, const char *file,
456 const char *func, const int line)
458 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
459 "ASSERTION(%s) failed\n", expr);
460 /* cfs_enter_debugger(); */
461 lbug_with_loc(file, func, line);
463 EXPORT_SYMBOL(libcfs_assertion_failed);
466 cfs_trace_assertion_failed(const char *str,
467 const char *fn, const char *file, int line)
469 struct ptldebug_header hdr;
471 libcfs_panic_in_progress = 1;
472 libcfs_catastrophe = 1;
475 cfs_set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
478 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
480 LIBCFS_PANIC("Lustre debug assertion failure\n");
486 panic_collect_pages(struct page_collection *pc)
488 /* Do the collect_pages job on a single CPU: assumes that all other
489 * CPUs have been stopped during a panic. If this isn't true for some
490 * arch, this will have to be implemented separately in each arch. */
493 struct cfs_trace_cpu_data *tcd;
495 CFS_INIT_LIST_HEAD(&pc->pc_pages);
497 cfs_tcd_for_each(tcd, i, j) {
498 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
499 tcd->tcd_cur_pages = 0;
501 if (pc->pc_want_daemon_pages) {
502 cfs_list_splice_init(&tcd->tcd_daemon_pages,
504 tcd->tcd_cur_daemon_pages = 0;
509 static void collect_pages_on_all_cpus(struct page_collection *pc)
511 struct cfs_trace_cpu_data *tcd;
514 cfs_spin_lock(&pc->pc_lock);
515 cfs_for_each_possible_cpu(cpu) {
516 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
517 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
518 tcd->tcd_cur_pages = 0;
519 if (pc->pc_want_daemon_pages) {
520 cfs_list_splice_init(&tcd->tcd_daemon_pages,
522 tcd->tcd_cur_daemon_pages = 0;
526 cfs_spin_unlock(&pc->pc_lock);
529 static void collect_pages(struct page_collection *pc)
531 CFS_INIT_LIST_HEAD(&pc->pc_pages);
533 if (libcfs_panic_in_progress)
534 panic_collect_pages(pc);
536 collect_pages_on_all_cpus(pc);
539 static void put_pages_back_on_all_cpus(struct page_collection *pc)
541 struct cfs_trace_cpu_data *tcd;
542 cfs_list_t *cur_head;
543 struct cfs_trace_page *tage;
544 struct cfs_trace_page *tmp;
547 cfs_spin_lock(&pc->pc_lock);
548 cfs_for_each_possible_cpu(cpu) {
549 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
550 cur_head = tcd->tcd_pages.next;
552 cfs_list_for_each_entry_safe_typed(tage, tmp,
554 struct cfs_trace_page,
557 __LASSERT_TAGE_INVARIANT(tage);
559 if (tage->cpu != cpu || tage->type != i)
562 cfs_tage_to_tail(tage, cur_head);
563 tcd->tcd_cur_pages++;
567 cfs_spin_unlock(&pc->pc_lock);
570 static void put_pages_back(struct page_collection *pc)
572 if (!libcfs_panic_in_progress)
573 put_pages_back_on_all_cpus(pc);
576 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
577 * we have a good amount of data at all times for dumping during an LBUG, even
578 * if we have been steadily writing (and otherwise discarding) pages via the
580 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
581 struct cfs_trace_cpu_data *tcd)
583 struct cfs_trace_page *tage;
584 struct cfs_trace_page *tmp;
586 cfs_spin_lock(&pc->pc_lock);
587 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
588 struct cfs_trace_page, linkage) {
590 __LASSERT_TAGE_INVARIANT(tage);
592 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
595 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
596 tcd->tcd_cur_daemon_pages++;
598 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
599 struct cfs_trace_page *victim;
601 __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
602 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
604 __LASSERT_TAGE_INVARIANT(victim);
606 cfs_list_del(&victim->linkage);
607 cfs_tage_free(victim);
608 tcd->tcd_cur_daemon_pages--;
611 cfs_spin_unlock(&pc->pc_lock);
614 static void put_pages_on_daemon_list(struct page_collection *pc)
616 struct cfs_trace_cpu_data *tcd;
619 cfs_for_each_possible_cpu(cpu) {
620 cfs_tcd_for_each_type_lock(tcd, i, cpu)
621 put_pages_on_tcd_daemon_list(pc, tcd);
625 void cfs_trace_debug_print(void)
627 struct page_collection pc;
628 struct cfs_trace_page *tage;
629 struct cfs_trace_page *tmp;
631 cfs_spin_lock_init(&pc.pc_lock);
633 pc.pc_want_daemon_pages = 1;
635 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
636 struct cfs_trace_page, linkage) {
640 __LASSERT_TAGE_INVARIANT(tage);
643 p = cfs_page_address(page);
644 while (p < ((char *)cfs_page_address(page) + tage->used)) {
645 struct ptldebug_header *hdr;
650 p += strlen(file) + 1;
653 len = hdr->ph_len - (int)(p - (char *)hdr);
655 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
660 cfs_list_del(&tage->linkage);
665 int cfs_tracefile_dump_all_pages(char *filename)
667 struct page_collection pc;
669 struct cfs_trace_page *tage;
670 struct cfs_trace_page *tmp;
675 cfs_tracefile_write_lock();
677 filp = cfs_filp_open(filename,
678 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
682 "LustreError: can't open %s for dump: rc %d\n",
687 cfs_spin_lock_init(&pc.pc_lock);
688 pc.pc_want_daemon_pages = 1;
690 if (cfs_list_empty(&pc.pc_pages)) {
695 /* ok, for now, just write the pages. in the future we'll be building
696 * iobufs with the pages and calling generic_direct_IO */
698 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
699 struct cfs_trace_page, linkage) {
701 __LASSERT_TAGE_INVARIANT(tage);
703 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
704 tage->used, cfs_filp_poff(filp));
705 if (rc != (int)tage->used) {
706 printk(CFS_KERN_WARNING "wanted to write %u but wrote "
707 "%d\n", tage->used, rc);
709 __LASSERT(cfs_list_empty(&pc.pc_pages));
712 cfs_list_del(&tage->linkage);
716 rc = cfs_filp_fsync(filp);
718 printk(CFS_KERN_ERR "sync returns %d\n", rc);
720 cfs_filp_close(filp);
722 cfs_tracefile_write_unlock();
726 void cfs_trace_flush_pages(void)
728 struct page_collection pc;
729 struct cfs_trace_page *tage;
730 struct cfs_trace_page *tmp;
732 cfs_spin_lock_init(&pc.pc_lock);
734 pc.pc_want_daemon_pages = 1;
736 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
737 struct cfs_trace_page, linkage) {
739 __LASSERT_TAGE_INVARIANT(tage);
741 cfs_list_del(&tage->linkage);
746 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
747 const char *usr_buffer, int usr_buffer_nob)
751 if (usr_buffer_nob > knl_buffer_nob)
754 if (cfs_copy_from_user((void *)knl_buffer,
755 (void *)usr_buffer, usr_buffer_nob))
758 nob = strnlen(knl_buffer, usr_buffer_nob);
759 while (nob-- >= 0) /* strip trailing whitespace */
760 if (!isspace(knl_buffer[nob]))
763 if (nob < 0) /* empty string */
766 if (nob == knl_buffer_nob) /* no space to terminate */
769 knl_buffer[nob + 1] = 0; /* terminate */
773 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
774 const char *knl_buffer, char *append)
776 /* NB if 'append' != NULL, it's a single character to append to the
777 * copied out string - usually "\n", for /proc entries and "" (i.e. a
778 * terminating zero byte) for sysctl entries */
779 int nob = strlen(knl_buffer);
781 if (nob > usr_buffer_nob)
782 nob = usr_buffer_nob;
784 if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
787 if (append != NULL && nob < usr_buffer_nob) {
788 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
796 EXPORT_SYMBOL(cfs_trace_copyout_string);
798 int cfs_trace_allocate_string_buffer(char **str, int nob)
800 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
803 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
810 void cfs_trace_free_string_buffer(char *str, int nob)
815 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
820 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
824 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
825 usr_str, usr_str_nob);
829 #if !defined(__WINNT__)
835 rc = cfs_tracefile_dump_all_pages(str);
837 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
841 int cfs_trace_daemon_command(char *str)
845 cfs_tracefile_write_lock();
847 if (strcmp(str, "stop") == 0) {
848 cfs_tracefile_write_unlock();
849 cfs_trace_stop_thread();
850 cfs_tracefile_write_lock();
851 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
853 } else if (strncmp(str, "size=", 5) == 0) {
854 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
855 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
856 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
858 cfs_tracefile_size <<= 20;
860 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
863 } else if (str[0] != '/') {
867 strcpy(cfs_tracefile, str);
870 "Lustre: debug daemon will attempt to start writing "
871 "to %s (%lukB max)\n", cfs_tracefile,
872 (long)(cfs_tracefile_size >> 10));
874 cfs_trace_start_thread();
877 cfs_tracefile_write_unlock();
881 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
886 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
890 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
891 usr_str, usr_str_nob);
893 rc = cfs_trace_daemon_command(str);
895 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
899 int cfs_trace_set_debug_mb(int mb)
904 int limit = cfs_trace_max_debug_mb();
905 struct cfs_trace_cpu_data *tcd;
907 if (mb < cfs_num_possible_cpus())
911 printk(CFS_KERN_ERR "Lustre: Refusing to set debug buffer size "
912 "to %dMB - limit is %d\n", mb, limit);
916 mb /= cfs_num_possible_cpus();
917 pages = mb << (20 - CFS_PAGE_SHIFT);
919 cfs_tracefile_write_lock();
921 cfs_tcd_for_each(tcd, i, j)
922 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
924 cfs_tracefile_write_unlock();
929 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
934 rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
938 return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
941 int cfs_trace_get_debug_mb(void)
945 struct cfs_trace_cpu_data *tcd;
948 cfs_tracefile_read_lock();
950 cfs_tcd_for_each(tcd, i, j)
951 total_pages += tcd->tcd_max_pages;
953 cfs_tracefile_read_unlock();
955 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
958 static int tracefiled(void *arg)
960 struct page_collection pc;
961 struct tracefiled_ctl *tctl = arg;
962 struct cfs_trace_page *tage;
963 struct cfs_trace_page *tmp;
964 struct ptldebug_header *hdr;
971 /* we're started late enough that we pick up init's fs context */
972 /* this is so broken in uml? what on earth is going on? */
973 cfs_daemonize("ktracefiled");
975 cfs_spin_lock_init(&pc.pc_lock);
976 cfs_complete(&tctl->tctl_start);
979 cfs_waitlink_t __wait;
981 pc.pc_want_daemon_pages = 0;
983 if (cfs_list_empty(&pc.pc_pages))
987 cfs_tracefile_read_lock();
988 if (cfs_tracefile[0] != 0) {
989 filp = cfs_filp_open(cfs_tracefile,
990 O_CREAT | O_RDWR | O_LARGEFILE,
993 printk(CFS_KERN_WARNING "couldn't open %s: "
994 "%d\n", cfs_tracefile, rc);
996 cfs_tracefile_read_unlock();
998 put_pages_on_daemon_list(&pc);
999 __LASSERT(cfs_list_empty(&pc.pc_pages));
1005 /* mark the first header, so we can sort in chunks */
1006 tage = cfs_tage_from_list(pc.pc_pages.next);
1007 __LASSERT_TAGE_INVARIANT(tage);
1009 hdr = cfs_page_address(tage->page);
1010 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
1012 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1013 struct cfs_trace_page,
1015 static loff_t f_pos;
1017 __LASSERT_TAGE_INVARIANT(tage);
1019 if (f_pos >= (off_t)cfs_tracefile_size)
1021 else if (f_pos > (off_t)cfs_filp_size(filp))
1022 f_pos = cfs_filp_size(filp);
1024 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1025 tage->used, &f_pos);
1026 if (rc != (int)tage->used) {
1027 printk(CFS_KERN_WARNING "wanted to write %u "
1028 "but wrote %d\n", tage->used, rc);
1029 put_pages_back(&pc);
1030 __LASSERT(cfs_list_empty(&pc.pc_pages));
1035 cfs_filp_close(filp);
1036 put_pages_on_daemon_list(&pc);
1037 if (!cfs_list_empty(&pc.pc_pages)) {
1040 printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1042 printk(CFS_KERN_ERR "total cpus(%d): ",
1043 cfs_num_possible_cpus());
1044 for (i = 0; i < cfs_num_possible_cpus(); i++)
1045 if (cfs_cpu_online(i))
1046 printk(CFS_KERN_ERR "%d(on) ", i);
1048 printk(CFS_KERN_ERR "%d(off) ", i);
1049 printk(CFS_KERN_ERR "\n");
1052 cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1054 printk(CFS_KERN_ERR "page %d belongs to cpu "
1055 "%d\n", ++i, tage->cpu);
1056 printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1059 __LASSERT(cfs_list_empty(&pc.pc_pages));
1061 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1062 if (last_loop == 0) {
1069 cfs_waitlink_init(&__wait);
1070 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1071 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1072 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1073 cfs_time_seconds(1));
1074 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1076 cfs_complete(&tctl->tctl_stop);
1080 int cfs_trace_start_thread(void)
1082 struct tracefiled_ctl *tctl = &trace_tctl;
1085 cfs_mutex_down(&cfs_trace_thread_sem);
1089 cfs_init_completion(&tctl->tctl_start);
1090 cfs_init_completion(&tctl->tctl_stop);
1091 cfs_waitq_init(&tctl->tctl_waitq);
1092 cfs_atomic_set(&tctl->tctl_shutdown, 0);
1094 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1099 cfs_wait_for_completion(&tctl->tctl_start);
1102 cfs_mutex_up(&cfs_trace_thread_sem);
1106 void cfs_trace_stop_thread(void)
1108 struct tracefiled_ctl *tctl = &trace_tctl;
1110 cfs_mutex_down(&cfs_trace_thread_sem);
1111 if (thread_running) {
1112 printk(CFS_KERN_INFO
1113 "Lustre: shutting down debug daemon thread...\n");
1114 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1115 cfs_wait_for_completion(&tctl->tctl_stop);
1118 cfs_mutex_up(&cfs_trace_thread_sem);
1121 int cfs_tracefile_init(int max_pages)
1123 struct cfs_trace_cpu_data *tcd;
1129 rc = cfs_tracefile_init_arch();
1133 cfs_tcd_for_each(tcd, i, j) {
1134 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1135 factor = tcd->tcd_pages_factor;
1136 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1137 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1138 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1139 tcd->tcd_cur_pages = 0;
1140 tcd->tcd_cur_stock_pages = 0;
1141 tcd->tcd_cur_daemon_pages = 0;
1142 tcd->tcd_max_pages = (max_pages * factor) / 100;
1143 LASSERT(tcd->tcd_max_pages > 0);
1144 tcd->tcd_shutting_down = 0;
1150 static void trace_cleanup_on_all_cpus(void)
1152 struct cfs_trace_cpu_data *tcd;
1153 struct cfs_trace_page *tage;
1154 struct cfs_trace_page *tmp;
1157 cfs_for_each_possible_cpu(cpu) {
1158 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1159 tcd->tcd_shutting_down = 1;
1161 cfs_list_for_each_entry_safe_typed(tage, tmp,
1163 struct cfs_trace_page,
1165 __LASSERT_TAGE_INVARIANT(tage);
1167 cfs_list_del(&tage->linkage);
1168 cfs_tage_free(tage);
1171 tcd->tcd_cur_pages = 0;
1176 static void cfs_trace_cleanup(void)
1178 struct page_collection pc;
1180 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1181 cfs_spin_lock_init(&pc.pc_lock);
1183 trace_cleanup_on_all_cpus();
1185 cfs_tracefile_fini_arch();
1188 void cfs_tracefile_exit(void)
1190 cfs_trace_stop_thread();
1191 cfs_trace_cleanup();