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 trace_data_union (*trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
52 char tracefile[TRACEFILE_NAME_SIZE];
53 long long tracefile_size = TRACEFILE_SIZE;
54 static struct tracefiled_ctl trace_tctl;
55 struct semaphore trace_thread_sem;
56 static int thread_running = 0;
58 atomic_t tage_allocated = ATOMIC_INIT(0);
60 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
61 struct trace_cpu_data *tcd);
63 static inline struct trace_page *tage_from_list(struct list_head *list)
65 return list_entry(list, struct trace_page, linkage);
68 static struct trace_page *tage_alloc(int gfp)
71 struct trace_page *tage;
74 * Don't spam console with allocation failures: they will be reported
75 * by upper layer anyway.
77 gfp |= CFS_ALLOC_NOWARN;
78 page = cfs_alloc_page(gfp);
82 tage = cfs_alloc(sizeof(*tage), gfp);
89 atomic_inc(&tage_allocated);
93 static void tage_free(struct trace_page *tage)
95 __LASSERT(tage != NULL);
96 __LASSERT(tage->page != NULL);
98 cfs_free_page(tage->page);
100 atomic_dec(&tage_allocated);
103 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
105 __LASSERT(tage != NULL);
106 __LASSERT(queue != NULL);
108 list_move_tail(&tage->linkage, queue);
111 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
112 struct list_head *stock)
117 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
118 * from here: this will lead to infinite recursion.
121 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
122 struct trace_page *tage;
124 tage = tage_alloc(gfp);
127 list_add_tail(&tage->linkage, stock);
132 /* return a page that has 'len' bytes left at the end */
133 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
136 struct trace_page *tage;
138 if (tcd->tcd_cur_pages > 0) {
139 __LASSERT(!list_empty(&tcd->tcd_pages));
140 tage = tage_from_list(tcd->tcd_pages.prev);
141 if (tage->used + len <= CFS_PAGE_SIZE)
145 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
146 if (tcd->tcd_cur_stock_pages > 0) {
147 tage = tage_from_list(tcd->tcd_stock_pages.prev);
148 -- tcd->tcd_cur_stock_pages;
149 list_del_init(&tage->linkage);
151 tage = tage_alloc(CFS_ALLOC_ATOMIC);
153 if (printk_ratelimit())
155 "cannot allocate a tage (%ld)\n",
162 tage->cpu = smp_processor_id();
163 tage->type = tcd->tcd_type;
164 list_add_tail(&tage->linkage, &tcd->tcd_pages);
165 tcd->tcd_cur_pages++;
167 if (tcd->tcd_cur_pages > 8 && thread_running) {
168 struct tracefiled_ctl *tctl = &trace_tctl;
170 * wake up tracefiled to process some pages.
172 cfs_waitq_signal(&tctl->tctl_waitq);
179 static void tcd_shrink(struct trace_cpu_data *tcd)
181 int pgcount = tcd->tcd_cur_pages / 10;
182 struct page_collection pc;
183 struct trace_page *tage;
184 struct trace_page *tmp;
187 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
188 * from here: this will lead to infinite recursion.
191 if (printk_ratelimit())
192 printk(KERN_WARNING "debug daemon buffer overflowed; "
193 "discarding 10%% of pages (%d of %ld)\n",
194 pgcount + 1, tcd->tcd_cur_pages);
196 CFS_INIT_LIST_HEAD(&pc.pc_pages);
197 spin_lock_init(&pc.pc_lock);
199 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
200 struct trace_page, linkage) {
204 list_move_tail(&tage->linkage, &pc.pc_pages);
205 tcd->tcd_cur_pages--;
207 put_pages_on_tcd_daemon_list(&pc, tcd);
210 /* return a page that has 'len' bytes left at the end */
211 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
214 struct trace_page *tage;
217 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
218 * from here: this will lead to infinite recursion.
221 if (len > CFS_PAGE_SIZE) {
223 "cowardly refusing to write %lu bytes in a page\n", len);
227 tage = trace_get_tage_try(tcd, len);
232 if (tcd->tcd_cur_pages > 0) {
233 tage = tage_from_list(tcd->tcd_pages.next);
235 tage_to_tail(tage, &tcd->tcd_pages);
240 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
241 const char *file, const char *fn, const int line,
242 const char *format1, va_list args,
243 const char *format2, ...)
245 struct trace_cpu_data *tcd = NULL;
246 struct ptldebug_header header;
247 struct trace_page *tage;
248 /* string_buf is used only if tcd != NULL, and is always set then */
249 char *string_buf = NULL;
252 int needed = 85; /* average message length */
259 if (strchr(file, '/'))
260 file = strrchr(file, '/') + 1;
263 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
265 tcd = trace_get_tcd();
266 if (tcd == NULL) /* arch may not log in IRQ context */
269 if (tcd->tcd_shutting_down) {
275 depth = __current_nesting_level();
276 known_size = strlen(file) + 1 + depth;
278 known_size += strlen(fn) + 1;
280 if (libcfs_debug_binary)
281 known_size += sizeof(header);
284 * '2' used because vsnprintf return real size required for output
285 * _without_ terminating NULL.
286 * if needed is to small for this format.
288 for (i = 0; i < 2; i++) {
289 tage = trace_get_tage(tcd, needed + known_size + 1);
291 if (needed + known_size > CFS_PAGE_SIZE)
299 string_buf = (char *)cfs_page_address(tage->page) +
300 tage->used + known_size;
302 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
304 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
314 needed = vsnprintf(string_buf, max_nob, format1, ap);
319 remain = max_nob - needed;
323 va_start(ap, format2);
324 needed += vsnprintf(string_buf + needed, remain,
329 if (needed < max_nob) /* well. printing ok.. */
333 if (*(string_buf+needed-1) != '\n')
334 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
337 header.ph_len = known_size + needed;
338 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
340 if (libcfs_debug_binary) {
341 memcpy(debug_buf, &header, sizeof(header));
342 tage->used += sizeof(header);
343 debug_buf += sizeof(header);
346 /* indent message according to the nesting level */
347 while (depth-- > 0) {
348 *(debug_buf++) = '.';
352 strcpy(debug_buf, file);
353 tage->used += strlen(file) + 1;
354 debug_buf += strlen(file) + 1;
357 strcpy(debug_buf, fn);
358 tage->used += strlen(fn) + 1;
359 debug_buf += strlen(fn) + 1;
362 __LASSERT(debug_buf == string_buf);
364 tage->used += needed;
365 __LASSERT (tage->used <= CFS_PAGE_SIZE);
368 if ((mask & libcfs_printk) == 0) {
369 /* no console output requested */
376 if (libcfs_console_ratelimit &&
377 cdls->cdls_next != 0 && /* not first time ever */
378 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
379 /* skipping a console message */
386 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
387 libcfs_console_max_delay
388 + cfs_time_seconds(10))) {
389 /* last timeout was a long time ago */
390 cdls->cdls_delay /= libcfs_console_backoff * 4;
392 cdls->cdls_delay *= libcfs_console_backoff;
394 if (cdls->cdls_delay < libcfs_console_min_delay)
395 cdls->cdls_delay = libcfs_console_min_delay;
396 else if (cdls->cdls_delay > libcfs_console_max_delay)
397 cdls->cdls_delay = libcfs_console_max_delay;
400 /* ensure cdls_next is never zero after it's been seen */
401 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
405 print_to_console(&header, mask, string_buf, needed, file, fn);
408 string_buf = trace_get_console_buffer();
411 if (format1 != NULL) {
413 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
416 if (format2 != NULL) {
417 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
419 va_start(ap, format2);
420 needed += vsnprintf(string_buf+needed, remain, format2, ap);
424 print_to_console(&header, mask,
425 string_buf, needed, file, fn);
427 trace_put_console_buffer(string_buf);
430 if (cdls != NULL && cdls->cdls_count != 0) {
431 string_buf = trace_get_console_buffer();
433 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
434 "Skipped %d previous similar message%s\n",
435 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
437 print_to_console(&header, mask,
438 string_buf, needed, file, fn);
440 trace_put_console_buffer(string_buf);
441 cdls->cdls_count = 0;
446 EXPORT_SYMBOL(libcfs_debug_vmsg2);
449 libcfs_assertion_failed(const char *expr, const char *file,
450 const char *func, const int line)
452 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
453 "ASSERTION(%s) failed\n", expr);
454 lbug_with_loc(file, func, line);
456 EXPORT_SYMBOL(libcfs_assertion_failed);
459 trace_assertion_failed(const char *str,
460 const char *fn, const char *file, int line)
462 struct ptldebug_header hdr;
464 libcfs_panic_in_progress = 1;
465 libcfs_catastrophe = 1;
468 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
471 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
473 LIBCFS_PANIC("Lustre debug assertion failure\n");
479 panic_collect_pages(struct page_collection *pc)
481 /* Do the collect_pages job on a single CPU: assumes that all other
482 * CPUs have been stopped during a panic. If this isn't true for some
483 * arch, this will have to be implemented separately in each arch. */
486 struct trace_cpu_data *tcd;
488 CFS_INIT_LIST_HEAD(&pc->pc_pages);
490 tcd_for_each(tcd, i, j) {
491 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
492 tcd->tcd_cur_pages = 0;
494 if (pc->pc_want_daemon_pages) {
495 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
496 tcd->tcd_cur_daemon_pages = 0;
501 static void collect_pages_on_cpu(void *info)
503 struct trace_cpu_data *tcd;
504 struct page_collection *pc = info;
507 spin_lock(&pc->pc_lock);
508 tcd_for_each_type_lock(tcd, i) {
509 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
510 tcd->tcd_cur_pages = 0;
511 if (pc->pc_want_daemon_pages) {
512 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
513 tcd->tcd_cur_daemon_pages = 0;
516 spin_unlock(&pc->pc_lock);
519 static void collect_pages(struct page_collection *pc)
521 CFS_INIT_LIST_HEAD(&pc->pc_pages);
523 if (libcfs_panic_in_progress)
524 panic_collect_pages(pc);
526 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
529 static void put_pages_back_on_cpu(void *info)
531 struct page_collection *pc = info;
532 struct trace_cpu_data *tcd;
533 struct list_head *cur_head;
534 struct trace_page *tage;
535 struct trace_page *tmp;
538 spin_lock(&pc->pc_lock);
539 tcd_for_each_type_lock(tcd, i) {
540 cur_head = tcd->tcd_pages.next;
542 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
543 struct trace_page, linkage) {
545 __LASSERT_TAGE_INVARIANT(tage);
547 if (tage->cpu != smp_processor_id() || tage->type != i)
550 tage_to_tail(tage, cur_head);
551 tcd->tcd_cur_pages++;
554 spin_unlock(&pc->pc_lock);
557 static void put_pages_back(struct page_collection *pc)
559 if (!libcfs_panic_in_progress)
560 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
563 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
564 * we have a good amount of data at all times for dumping during an LBUG, even
565 * if we have been steadily writing (and otherwise discarding) pages via the
567 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
568 struct trace_cpu_data *tcd)
570 struct trace_page *tage;
571 struct trace_page *tmp;
573 spin_lock(&pc->pc_lock);
574 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
575 struct trace_page, linkage) {
577 __LASSERT_TAGE_INVARIANT(tage);
579 if (tage->cpu != smp_processor_id() ||
580 tage->type != tcd->tcd_type)
583 tage_to_tail(tage, &tcd->tcd_daemon_pages);
584 tcd->tcd_cur_daemon_pages++;
586 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
587 struct trace_page *victim;
589 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
590 victim = tage_from_list(tcd->tcd_daemon_pages.next);
592 __LASSERT_TAGE_INVARIANT(victim);
594 list_del(&victim->linkage);
596 tcd->tcd_cur_daemon_pages--;
599 spin_unlock(&pc->pc_lock);
602 static void put_pages_on_daemon_list_on_cpu(void *info)
604 struct trace_cpu_data *tcd;
607 tcd_for_each_type_lock(tcd, i)
608 put_pages_on_tcd_daemon_list(info, tcd);
611 static void put_pages_on_daemon_list(struct page_collection *pc)
613 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
616 void trace_debug_print(void)
618 struct page_collection pc;
619 struct trace_page *tage;
620 struct trace_page *tmp;
622 spin_lock_init(&pc.pc_lock);
624 pc.pc_want_daemon_pages = 1;
626 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
627 struct trace_page, linkage) {
631 __LASSERT_TAGE_INVARIANT(tage);
634 p = cfs_page_address(page);
635 while (p < ((char *)cfs_page_address(page) + tage->used)) {
636 struct ptldebug_header *hdr;
641 p += strlen(file) + 1;
644 len = hdr->ph_len - (int)(p - (char *)hdr);
646 print_to_console(hdr, D_EMERG, p, len, file, fn);
651 list_del(&tage->linkage);
656 int tracefile_dump_all_pages(char *filename)
658 struct page_collection pc;
660 struct trace_page *tage;
661 struct trace_page *tmp;
666 tracefile_write_lock();
668 filp = cfs_filp_open(filename,
669 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
672 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
677 spin_lock_init(&pc.pc_lock);
678 pc.pc_want_daemon_pages = 1;
680 if (list_empty(&pc.pc_pages)) {
685 /* ok, for now, just write the pages. in the future we'll be building
686 * iobufs with the pages and calling generic_direct_IO */
688 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
689 struct trace_page, linkage) {
691 __LASSERT_TAGE_INVARIANT(tage);
693 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
694 tage->used, cfs_filp_poff(filp));
695 if (rc != (int)tage->used) {
696 printk(KERN_WARNING "wanted to write %u but wrote "
697 "%d\n", tage->used, rc);
699 __LASSERT(list_empty(&pc.pc_pages));
702 list_del(&tage->linkage);
706 rc = cfs_filp_fsync(filp);
708 printk(KERN_ERR "sync returns %d\n", rc);
710 cfs_filp_close(filp);
712 tracefile_write_unlock();
716 void trace_flush_pages(void)
718 struct page_collection pc;
719 struct trace_page *tage;
720 struct trace_page *tmp;
722 spin_lock_init(&pc.pc_lock);
724 pc.pc_want_daemon_pages = 1;
726 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
727 struct trace_page, linkage) {
729 __LASSERT_TAGE_INVARIANT(tage);
731 list_del(&tage->linkage);
736 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
737 const char *usr_buffer, int usr_buffer_nob)
741 if (usr_buffer_nob > knl_buffer_nob)
744 if (copy_from_user((void *)knl_buffer,
745 (void *)usr_buffer, usr_buffer_nob))
748 nob = strnlen(knl_buffer, usr_buffer_nob);
749 while (nob-- >= 0) /* strip trailing whitespace */
750 if (!isspace(knl_buffer[nob]))
753 if (nob < 0) /* empty string */
756 if (nob == knl_buffer_nob) /* no space to terminate */
759 knl_buffer[nob + 1] = 0; /* terminate */
763 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
764 const char *knl_buffer, char *append)
766 /* NB if 'append' != NULL, it's a single character to append to the
767 * copied out string - usually "\n", for /proc entries and "" (i.e. a
768 * terminating zero byte) for sysctl entries */
769 int nob = strlen(knl_buffer);
771 if (nob > usr_buffer_nob)
772 nob = usr_buffer_nob;
774 if (copy_to_user(usr_buffer, knl_buffer, nob))
777 if (append != NULL && nob < usr_buffer_nob) {
778 if (copy_to_user(usr_buffer + nob, append, 1))
787 int trace_allocate_string_buffer(char **str, int nob)
789 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
792 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
799 void trace_free_string_buffer(char *str, int nob)
804 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
809 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
813 rc = trace_copyin_string(str, usr_str_nob + 1,
814 usr_str, usr_str_nob);
818 #if !defined(__WINNT__)
824 rc = tracefile_dump_all_pages(str);
826 trace_free_string_buffer(str, usr_str_nob + 1);
830 int trace_daemon_command(char *str)
834 tracefile_write_lock();
836 if (strcmp(str, "stop") == 0) {
838 memset(tracefile, 0, sizeof(tracefile));
840 } else if (strncmp(str, "size=", 5) == 0) {
841 tracefile_size = simple_strtoul(str + 5, NULL, 0);
842 if (tracefile_size < 10 || tracefile_size > 20480)
843 tracefile_size = TRACEFILE_SIZE;
845 tracefile_size <<= 20;
847 } else if (strlen(str) >= sizeof(tracefile)) {
850 } else if (str[0] != '/') {
854 strcpy(tracefile, str);
856 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
857 "to %s (%lukB max)\n", tracefile,
858 (long)(tracefile_size >> 10));
860 trace_start_thread();
863 tracefile_write_unlock();
867 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
872 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
876 rc = trace_copyin_string(str, usr_str_nob + 1,
877 usr_str, usr_str_nob);
879 rc = trace_daemon_command(str);
881 trace_free_string_buffer(str, usr_str_nob + 1);
885 int trace_set_debug_mb(int mb)
890 int limit = trace_max_debug_mb();
891 struct trace_cpu_data *tcd;
893 if (mb < num_possible_cpus())
897 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
898 "%dMB - limit is %d\n", mb, limit);
902 mb /= num_possible_cpus();
903 pages = mb << (20 - CFS_PAGE_SHIFT);
905 tracefile_write_lock();
907 tcd_for_each(tcd, i, j)
908 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
910 tracefile_write_unlock();
915 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
920 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
924 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
927 int trace_get_debug_mb(void)
931 struct trace_cpu_data *tcd;
934 tracefile_read_lock();
936 tcd_for_each(tcd, i, j)
937 total_pages += tcd->tcd_max_pages;
939 tracefile_read_unlock();
941 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
944 static int tracefiled(void *arg)
946 struct page_collection pc;
947 struct tracefiled_ctl *tctl = arg;
948 struct trace_page *tage;
949 struct trace_page *tmp;
950 struct ptldebug_header *hdr;
956 /* we're started late enough that we pick up init's fs context */
957 /* this is so broken in uml? what on earth is going on? */
958 cfs_daemonize("ktracefiled");
960 spin_lock_init(&pc.pc_lock);
961 complete(&tctl->tctl_start);
964 cfs_waitlink_t __wait;
966 cfs_waitlink_init(&__wait);
967 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
968 set_current_state(TASK_INTERRUPTIBLE);
969 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
970 cfs_time_seconds(1));
971 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
973 if (atomic_read(&tctl->tctl_shutdown))
976 pc.pc_want_daemon_pages = 0;
978 if (list_empty(&pc.pc_pages))
982 tracefile_read_lock();
983 if (tracefile[0] != 0) {
984 filp = cfs_filp_open(tracefile,
985 O_CREAT | O_RDWR | O_LARGEFILE,
988 printk(KERN_WARNING "couldn't open %s: %d\n",
991 tracefile_read_unlock();
993 put_pages_on_daemon_list(&pc);
994 __LASSERT(list_empty(&pc.pc_pages));
1000 /* mark the first header, so we can sort in chunks */
1001 tage = tage_from_list(pc.pc_pages.next);
1002 __LASSERT_TAGE_INVARIANT(tage);
1004 hdr = cfs_page_address(tage->page);
1005 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
1007 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1008 struct trace_page, linkage) {
1009 static loff_t f_pos;
1011 __LASSERT_TAGE_INVARIANT(tage);
1013 if (f_pos >= (off_t)tracefile_size)
1015 else if (f_pos > (off_t)cfs_filp_size(filp))
1016 f_pos = cfs_filp_size(filp);
1018 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1019 tage->used, &f_pos);
1020 if (rc != (int)tage->used) {
1021 printk(KERN_WARNING "wanted to write %u but "
1022 "wrote %d\n", tage->used, rc);
1023 put_pages_back(&pc);
1024 __LASSERT(list_empty(&pc.pc_pages));
1029 cfs_filp_close(filp);
1030 put_pages_on_daemon_list(&pc);
1031 __LASSERT(list_empty(&pc.pc_pages));
1033 complete(&tctl->tctl_stop);
1037 int trace_start_thread(void)
1039 struct tracefiled_ctl *tctl = &trace_tctl;
1042 mutex_down(&trace_thread_sem);
1046 init_completion(&tctl->tctl_start);
1047 init_completion(&tctl->tctl_stop);
1048 cfs_waitq_init(&tctl->tctl_waitq);
1049 atomic_set(&tctl->tctl_shutdown, 0);
1051 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1056 wait_for_completion(&tctl->tctl_start);
1059 mutex_up(&trace_thread_sem);
1063 void trace_stop_thread(void)
1065 struct tracefiled_ctl *tctl = &trace_tctl;
1067 mutex_down(&trace_thread_sem);
1068 if (thread_running) {
1069 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1070 atomic_set(&tctl->tctl_shutdown, 1);
1071 wait_for_completion(&tctl->tctl_stop);
1074 mutex_up(&trace_thread_sem);
1077 int tracefile_init(int max_pages)
1079 struct trace_cpu_data *tcd;
1085 rc = tracefile_init_arch();
1089 tcd_for_each(tcd, i, j) {
1090 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1091 factor = tcd->tcd_pages_factor;
1092 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1093 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1094 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1095 tcd->tcd_cur_pages = 0;
1096 tcd->tcd_cur_stock_pages = 0;
1097 tcd->tcd_cur_daemon_pages = 0;
1098 tcd->tcd_max_pages = (max_pages * factor) / 100;
1099 LASSERT(tcd->tcd_max_pages > 0);
1100 tcd->tcd_shutting_down = 0;
1106 static void trace_cleanup_on_cpu(void *info)
1108 struct trace_cpu_data *tcd;
1109 struct trace_page *tage;
1110 struct trace_page *tmp;
1113 tcd_for_each_type_lock(tcd, i) {
1114 tcd->tcd_shutting_down = 1;
1116 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
1117 struct trace_page, linkage) {
1118 __LASSERT_TAGE_INVARIANT(tage);
1120 list_del(&tage->linkage);
1123 tcd->tcd_cur_pages = 0;
1127 static void trace_cleanup(void)
1129 struct page_collection pc;
1131 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1132 spin_lock_init(&pc.pc_lock);
1134 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1136 tracefile_fini_arch();
1139 void tracefile_exit(void)
1141 trace_stop_thread();