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);
154 "failure to allocate a tage (%ld)\n",
161 tage->cpu = smp_processor_id();
162 tage->type = tcd->tcd_type;
163 list_add_tail(&tage->linkage, &tcd->tcd_pages);
164 tcd->tcd_cur_pages++;
166 if (tcd->tcd_cur_pages > 8 && thread_running) {
167 struct tracefiled_ctl *tctl = &trace_tctl;
169 * wake up tracefiled to process some pages.
171 cfs_waitq_signal(&tctl->tctl_waitq);
178 static void tcd_shrink(struct trace_cpu_data *tcd)
180 int pgcount = tcd->tcd_cur_pages / 10;
181 struct page_collection pc;
182 struct trace_page *tage;
183 struct trace_page *tmp;
186 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
187 * from here: this will lead to infinite recursion.
190 if (printk_ratelimit())
191 printk(KERN_WARNING "debug daemon buffer overflowed; "
192 "discarding 10%% of pages (%d of %ld)\n",
193 pgcount + 1, tcd->tcd_cur_pages);
195 CFS_INIT_LIST_HEAD(&pc.pc_pages);
196 spin_lock_init(&pc.pc_lock);
198 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
199 struct trace_page, linkage) {
203 list_move_tail(&tage->linkage, &pc.pc_pages);
204 tcd->tcd_cur_pages--;
206 put_pages_on_tcd_daemon_list(&pc, tcd);
209 /* return a page that has 'len' bytes left at the end */
210 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
213 struct trace_page *tage;
216 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
217 * from here: this will lead to infinite recursion.
220 if (len > CFS_PAGE_SIZE) {
222 "cowardly refusing to write %lu bytes in a page\n", len);
226 tage = trace_get_tage_try(tcd, len);
231 if (tcd->tcd_cur_pages > 0) {
232 tage = tage_from_list(tcd->tcd_pages.next);
234 tage_to_tail(tage, &tcd->tcd_pages);
239 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
240 const char *file, const char *fn, const int line,
241 const char *format1, va_list args,
242 const char *format2, ...)
244 struct trace_cpu_data *tcd = NULL;
245 struct ptldebug_header header;
246 struct trace_page *tage;
247 /* string_buf is used only if tcd != NULL, and is always set then */
248 char *string_buf = NULL;
251 int needed = 85; /* average message length */
258 if (strchr(file, '/'))
259 file = strrchr(file, '/') + 1;
262 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
264 tcd = trace_get_tcd();
265 if (tcd == NULL) /* arch may not log in IRQ context */
268 if (tcd->tcd_shutting_down) {
274 depth = __current_nesting_level();
275 known_size = strlen(file) + 1 + depth;
277 known_size += strlen(fn) + 1;
279 if (libcfs_debug_binary)
280 known_size += sizeof(header);
283 * '2' used because vsnprintf return real size required for output
284 * _without_ terminating NULL.
285 * if needed is to small for this format.
287 for (i = 0; i < 2; i++) {
288 tage = trace_get_tage(tcd, needed + known_size + 1);
290 if (needed + known_size > CFS_PAGE_SIZE)
298 string_buf = (char *)cfs_page_address(tage->page) +
299 tage->used + known_size;
301 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
303 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
313 needed = vsnprintf(string_buf, max_nob, format1, ap);
318 remain = max_nob - needed;
322 va_start(ap, format2);
323 needed += vsnprintf(string_buf + needed, remain,
328 if (needed < max_nob) /* well. printing ok.. */
332 if (*(string_buf+needed-1) != '\n')
333 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
336 header.ph_len = known_size + needed;
337 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
339 if (libcfs_debug_binary) {
340 memcpy(debug_buf, &header, sizeof(header));
341 tage->used += sizeof(header);
342 debug_buf += sizeof(header);
345 /* indent message according to the nesting level */
346 while (depth-- > 0) {
347 *(debug_buf++) = '.';
351 strcpy(debug_buf, file);
352 tage->used += strlen(file) + 1;
353 debug_buf += strlen(file) + 1;
356 strcpy(debug_buf, fn);
357 tage->used += strlen(fn) + 1;
358 debug_buf += strlen(fn) + 1;
361 __LASSERT(debug_buf == string_buf);
363 tage->used += needed;
364 __LASSERT (tage->used <= CFS_PAGE_SIZE);
367 if ((mask & libcfs_printk) == 0) {
368 /* no console output requested */
375 if (libcfs_console_ratelimit &&
376 cdls->cdls_next != 0 && /* not first time ever */
377 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
378 /* skipping a console message */
385 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
386 libcfs_console_max_delay
387 + cfs_time_seconds(10))) {
388 /* last timeout was a long time ago */
389 cdls->cdls_delay /= libcfs_console_backoff * 4;
391 cdls->cdls_delay *= libcfs_console_backoff;
393 if (cdls->cdls_delay < libcfs_console_min_delay)
394 cdls->cdls_delay = libcfs_console_min_delay;
395 else if (cdls->cdls_delay > libcfs_console_max_delay)
396 cdls->cdls_delay = libcfs_console_max_delay;
399 /* ensure cdls_next is never zero after it's been seen */
400 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
404 print_to_console(&header, mask, string_buf, needed, file, fn);
407 string_buf = trace_get_console_buffer();
410 if (format1 != NULL) {
412 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
415 if (format2 != NULL) {
416 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
418 va_start(ap, format2);
419 needed += vsnprintf(string_buf+needed, remain, format2, ap);
423 print_to_console(&header, mask,
424 string_buf, needed, file, fn);
426 trace_put_console_buffer(string_buf);
429 if (cdls != NULL && cdls->cdls_count != 0) {
430 string_buf = trace_get_console_buffer();
432 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
433 "Skipped %d previous similar message%s\n",
434 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
436 print_to_console(&header, mask,
437 string_buf, needed, file, fn);
439 trace_put_console_buffer(string_buf);
440 cdls->cdls_count = 0;
445 EXPORT_SYMBOL(libcfs_debug_vmsg2);
448 libcfs_assertion_failed(const char *expr, const char *file,
449 const char *func, const int line)
451 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
452 "ASSERTION(%s) failed\n", expr);
453 lbug_with_loc(file, func, line);
455 EXPORT_SYMBOL(libcfs_assertion_failed);
458 trace_assertion_failed(const char *str,
459 const char *fn, const char *file, int line)
461 struct ptldebug_header hdr;
463 libcfs_panic_in_progress = 1;
464 libcfs_catastrophe = 1;
467 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
470 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
472 LIBCFS_PANIC("Lustre debug assertion failure\n");
478 panic_collect_pages(struct page_collection *pc)
480 /* Do the collect_pages job on a single CPU: assumes that all other
481 * CPUs have been stopped during a panic. If this isn't true for some
482 * arch, this will have to be implemented separately in each arch. */
485 struct trace_cpu_data *tcd;
487 CFS_INIT_LIST_HEAD(&pc->pc_pages);
489 tcd_for_each(tcd, i, j) {
490 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
491 tcd->tcd_cur_pages = 0;
493 if (pc->pc_want_daemon_pages) {
494 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
495 tcd->tcd_cur_daemon_pages = 0;
500 static void collect_pages_on_cpu(void *info)
502 struct trace_cpu_data *tcd;
503 struct page_collection *pc = info;
506 spin_lock(&pc->pc_lock);
507 tcd_for_each_type_lock(tcd, i) {
508 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
509 tcd->tcd_cur_pages = 0;
510 if (pc->pc_want_daemon_pages) {
511 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
512 tcd->tcd_cur_daemon_pages = 0;
515 spin_unlock(&pc->pc_lock);
518 static void collect_pages(struct page_collection *pc)
520 CFS_INIT_LIST_HEAD(&pc->pc_pages);
522 if (libcfs_panic_in_progress)
523 panic_collect_pages(pc);
525 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
528 static void put_pages_back_on_cpu(void *info)
530 struct page_collection *pc = info;
531 struct trace_cpu_data *tcd;
532 struct list_head *cur_head;
533 struct trace_page *tage;
534 struct trace_page *tmp;
537 spin_lock(&pc->pc_lock);
538 tcd_for_each_type_lock(tcd, i) {
539 cur_head = tcd->tcd_pages.next;
541 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
542 struct trace_page, linkage) {
544 __LASSERT_TAGE_INVARIANT(tage);
546 if (tage->cpu != smp_processor_id() || tage->type != i)
549 tage_to_tail(tage, cur_head);
550 tcd->tcd_cur_pages++;
553 spin_unlock(&pc->pc_lock);
556 static void put_pages_back(struct page_collection *pc)
558 if (!libcfs_panic_in_progress)
559 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
562 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
563 * we have a good amount of data at all times for dumping during an LBUG, even
564 * if we have been steadily writing (and otherwise discarding) pages via the
566 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
567 struct trace_cpu_data *tcd)
569 struct trace_page *tage;
570 struct trace_page *tmp;
572 spin_lock(&pc->pc_lock);
573 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
574 struct trace_page, linkage) {
576 __LASSERT_TAGE_INVARIANT(tage);
578 if (tage->cpu != smp_processor_id() ||
579 tage->type != tcd->tcd_type)
582 tage_to_tail(tage, &tcd->tcd_daemon_pages);
583 tcd->tcd_cur_daemon_pages++;
585 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
586 struct trace_page *victim;
588 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
589 victim = tage_from_list(tcd->tcd_daemon_pages.next);
591 __LASSERT_TAGE_INVARIANT(victim);
593 list_del(&victim->linkage);
595 tcd->tcd_cur_daemon_pages--;
598 spin_unlock(&pc->pc_lock);
601 static void put_pages_on_daemon_list_on_cpu(void *info)
603 struct trace_cpu_data *tcd;
606 tcd_for_each_type_lock(tcd, i)
607 put_pages_on_tcd_daemon_list(info, tcd);
610 static void put_pages_on_daemon_list(struct page_collection *pc)
612 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
615 void trace_debug_print(void)
617 struct page_collection pc;
618 struct trace_page *tage;
619 struct trace_page *tmp;
621 spin_lock_init(&pc.pc_lock);
623 pc.pc_want_daemon_pages = 1;
625 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
626 struct trace_page, linkage) {
630 __LASSERT_TAGE_INVARIANT(tage);
633 p = cfs_page_address(page);
634 while (p < ((char *)cfs_page_address(page) + tage->used)) {
635 struct ptldebug_header *hdr;
640 p += strlen(file) + 1;
643 len = hdr->ph_len - (int)(p - (char *)hdr);
645 print_to_console(hdr, D_EMERG, p, len, file, fn);
650 list_del(&tage->linkage);
655 int tracefile_dump_all_pages(char *filename)
657 struct page_collection pc;
659 struct trace_page *tage;
660 struct trace_page *tmp;
665 tracefile_write_lock();
667 filp = cfs_filp_open(filename,
668 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
671 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
676 spin_lock_init(&pc.pc_lock);
677 pc.pc_want_daemon_pages = 1;
679 if (list_empty(&pc.pc_pages)) {
684 /* ok, for now, just write the pages. in the future we'll be building
685 * iobufs with the pages and calling generic_direct_IO */
687 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
688 struct trace_page, linkage) {
690 __LASSERT_TAGE_INVARIANT(tage);
692 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
693 tage->used, cfs_filp_poff(filp));
694 if (rc != (int)tage->used) {
695 printk(KERN_WARNING "wanted to write %u but wrote "
696 "%d\n", tage->used, rc);
698 __LASSERT(list_empty(&pc.pc_pages));
701 list_del(&tage->linkage);
705 rc = cfs_filp_fsync(filp);
707 printk(KERN_ERR "sync returns %d\n", rc);
709 cfs_filp_close(filp);
711 tracefile_write_unlock();
715 void trace_flush_pages(void)
717 struct page_collection pc;
718 struct trace_page *tage;
719 struct trace_page *tmp;
721 spin_lock_init(&pc.pc_lock);
723 pc.pc_want_daemon_pages = 1;
725 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
726 struct trace_page, linkage) {
728 __LASSERT_TAGE_INVARIANT(tage);
730 list_del(&tage->linkage);
735 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
736 const char *usr_buffer, int usr_buffer_nob)
740 if (usr_buffer_nob > knl_buffer_nob)
743 if (copy_from_user((void *)knl_buffer,
744 (void *)usr_buffer, usr_buffer_nob))
747 nob = strnlen(knl_buffer, usr_buffer_nob);
748 while (nob-- >= 0) /* strip trailing whitespace */
749 if (!isspace(knl_buffer[nob]))
752 if (nob < 0) /* empty string */
755 if (nob == knl_buffer_nob) /* no space to terminate */
758 knl_buffer[nob + 1] = 0; /* terminate */
762 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
763 const char *knl_buffer, char *append)
765 /* NB if 'append' != NULL, it's a single character to append to the
766 * copied out string - usually "\n", for /proc entries and "" (i.e. a
767 * terminating zero byte) for sysctl entries */
768 int nob = strlen(knl_buffer);
770 if (nob > usr_buffer_nob)
771 nob = usr_buffer_nob;
773 if (copy_to_user(usr_buffer, knl_buffer, nob))
776 if (append != NULL && nob < usr_buffer_nob) {
777 if (copy_to_user(usr_buffer + nob, append, 1))
786 int trace_allocate_string_buffer(char **str, int nob)
788 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
791 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
798 void trace_free_string_buffer(char *str, int nob)
803 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
808 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
812 rc = trace_copyin_string(str, usr_str_nob + 1,
813 usr_str, usr_str_nob);
817 #if !defined(__WINNT__)
823 rc = tracefile_dump_all_pages(str);
825 trace_free_string_buffer(str, usr_str_nob + 1);
829 int trace_daemon_command(char *str)
833 tracefile_write_lock();
835 if (strcmp(str, "stop") == 0) {
837 memset(tracefile, 0, sizeof(tracefile));
839 } else if (strncmp(str, "size=", 5) == 0) {
840 tracefile_size = simple_strtoul(str + 5, NULL, 0);
841 if (tracefile_size < 10 || tracefile_size > 20480)
842 tracefile_size = TRACEFILE_SIZE;
844 tracefile_size <<= 20;
846 } else if (strlen(str) >= sizeof(tracefile)) {
849 } else if (str[0] != '/') {
853 strcpy(tracefile, str);
855 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
856 "to %s (%lukB max)\n", tracefile,
857 (long)(tracefile_size >> 10));
859 trace_start_thread();
862 tracefile_write_unlock();
866 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
871 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
875 rc = trace_copyin_string(str, usr_str_nob + 1,
876 usr_str, usr_str_nob);
878 rc = trace_daemon_command(str);
880 trace_free_string_buffer(str, usr_str_nob + 1);
884 int trace_set_debug_mb(int mb)
889 int limit = trace_max_debug_mb();
890 struct trace_cpu_data *tcd;
892 if (mb < num_possible_cpus())
896 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
897 "%dMB - limit is %d\n", mb, limit);
901 mb /= num_possible_cpus();
902 pages = mb << (20 - CFS_PAGE_SHIFT);
904 tracefile_write_lock();
906 tcd_for_each(tcd, i, j)
907 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
909 tracefile_write_unlock();
914 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
919 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
923 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
926 int trace_get_debug_mb(void)
930 struct trace_cpu_data *tcd;
933 tracefile_read_lock();
935 tcd_for_each(tcd, i, j)
936 total_pages += tcd->tcd_max_pages;
938 tracefile_read_unlock();
940 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
943 static int tracefiled(void *arg)
945 struct page_collection pc;
946 struct tracefiled_ctl *tctl = arg;
947 struct trace_page *tage;
948 struct trace_page *tmp;
949 struct ptldebug_header *hdr;
955 /* we're started late enough that we pick up init's fs context */
956 /* this is so broken in uml? what on earth is going on? */
957 cfs_daemonize("ktracefiled");
959 spin_lock_init(&pc.pc_lock);
960 complete(&tctl->tctl_start);
963 cfs_waitlink_t __wait;
965 cfs_waitlink_init(&__wait);
966 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
967 set_current_state(TASK_INTERRUPTIBLE);
968 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
969 cfs_time_seconds(1));
970 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
972 if (atomic_read(&tctl->tctl_shutdown))
975 pc.pc_want_daemon_pages = 0;
977 if (list_empty(&pc.pc_pages))
981 tracefile_read_lock();
982 if (tracefile[0] != 0) {
983 filp = cfs_filp_open(tracefile,
984 O_CREAT | O_RDWR | O_LARGEFILE,
987 printk(KERN_WARNING "couldn't open %s: %d\n",
990 tracefile_read_unlock();
992 put_pages_on_daemon_list(&pc);
993 __LASSERT(list_empty(&pc.pc_pages));
999 /* mark the first header, so we can sort in chunks */
1000 tage = tage_from_list(pc.pc_pages.next);
1001 __LASSERT_TAGE_INVARIANT(tage);
1003 hdr = cfs_page_address(tage->page);
1004 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
1006 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1007 struct trace_page, linkage) {
1008 static loff_t f_pos;
1010 __LASSERT_TAGE_INVARIANT(tage);
1012 if (f_pos >= (off_t)tracefile_size)
1014 else if (f_pos > (off_t)cfs_filp_size(filp))
1015 f_pos = cfs_filp_size(filp);
1017 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1018 tage->used, &f_pos);
1019 if (rc != (int)tage->used) {
1020 printk(KERN_WARNING "wanted to write %u but "
1021 "wrote %d\n", tage->used, rc);
1022 put_pages_back(&pc);
1023 __LASSERT(list_empty(&pc.pc_pages));
1028 cfs_filp_close(filp);
1029 put_pages_on_daemon_list(&pc);
1030 __LASSERT(list_empty(&pc.pc_pages));
1032 complete(&tctl->tctl_stop);
1036 int trace_start_thread(void)
1038 struct tracefiled_ctl *tctl = &trace_tctl;
1041 mutex_down(&trace_thread_sem);
1045 init_completion(&tctl->tctl_start);
1046 init_completion(&tctl->tctl_stop);
1047 cfs_waitq_init(&tctl->tctl_waitq);
1048 atomic_set(&tctl->tctl_shutdown, 0);
1050 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1055 wait_for_completion(&tctl->tctl_start);
1058 mutex_up(&trace_thread_sem);
1062 void trace_stop_thread(void)
1064 struct tracefiled_ctl *tctl = &trace_tctl;
1066 mutex_down(&trace_thread_sem);
1067 if (thread_running) {
1068 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1069 atomic_set(&tctl->tctl_shutdown, 1);
1070 wait_for_completion(&tctl->tctl_stop);
1073 mutex_up(&trace_thread_sem);
1076 int tracefile_init(int max_pages)
1078 struct trace_cpu_data *tcd;
1084 rc = tracefile_init_arch();
1088 tcd_for_each(tcd, i, j) {
1089 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1090 factor = tcd->tcd_pages_factor;
1091 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1092 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1093 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1094 tcd->tcd_cur_pages = 0;
1095 tcd->tcd_cur_stock_pages = 0;
1096 tcd->tcd_cur_daemon_pages = 0;
1097 tcd->tcd_max_pages = (max_pages * factor) / 100;
1098 LASSERT(tcd->tcd_max_pages > 0);
1099 tcd->tcd_shutting_down = 0;
1105 static void trace_cleanup_on_cpu(void *info)
1107 struct trace_cpu_data *tcd;
1108 struct trace_page *tage;
1109 struct trace_page *tmp;
1112 tcd_for_each_type_lock(tcd, i) {
1113 tcd->tcd_shutting_down = 1;
1115 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
1116 struct trace_page, linkage) {
1117 __LASSERT_TAGE_INVARIANT(tage);
1119 list_del(&tage->linkage);
1122 tcd->tcd_cur_pages = 0;
1126 static void trace_cleanup(void)
1128 struct page_collection pc;
1130 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1131 spin_lock_init(&pc.pc_lock);
1133 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1135 tracefile_fini_arch();
1138 void tracefile_exit(void)
1140 trace_stop_thread();