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 (c) 2004, 2010, Oracle and/or its affiliates. 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 * lnet/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/kp30.h>
48 #include <libcfs/libcfs.h>
50 /* XXX move things up to the top, comment */
51 union trace_data_union (*trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
53 char tracefile[TRACEFILE_NAME_SIZE];
54 long long tracefile_size = TRACEFILE_SIZE;
55 static struct tracefiled_ctl trace_tctl;
56 struct semaphore trace_thread_sem;
57 static int thread_running = 0;
59 atomic_t tage_allocated = ATOMIC_INIT(0);
61 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
62 struct trace_cpu_data *tcd);
64 static inline struct trace_page *tage_from_list(struct list_head *list)
66 return list_entry(list, struct trace_page, linkage);
69 static struct trace_page *tage_alloc(int gfp)
72 struct 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 atomic_inc(&tage_allocated);
94 static void tage_free(struct trace_page *tage)
96 __LASSERT(tage != NULL);
97 __LASSERT(tage->page != NULL);
99 cfs_free_page(tage->page);
101 atomic_dec(&tage_allocated);
104 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
106 __LASSERT(tage != NULL);
107 __LASSERT(queue != NULL);
109 list_move_tail(&tage->linkage, queue);
112 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
113 struct list_head *stock)
118 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
119 * from here: this will lead to infinite recursion.
122 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
123 struct trace_page *tage;
125 tage = tage_alloc(gfp);
128 list_add_tail(&tage->linkage, stock);
133 /* return a page that has 'len' bytes left at the end */
134 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
137 struct trace_page *tage;
139 if (tcd->tcd_cur_pages > 0) {
140 __LASSERT(!list_empty(&tcd->tcd_pages));
141 tage = tage_from_list(tcd->tcd_pages.prev);
142 if (tage->used + len <= CFS_PAGE_SIZE)
146 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
147 if (tcd->tcd_cur_stock_pages > 0) {
148 tage = tage_from_list(tcd->tcd_stock_pages.prev);
149 -- tcd->tcd_cur_stock_pages;
150 list_del_init(&tage->linkage);
152 tage = tage_alloc(CFS_ALLOC_ATOMIC);
155 "failure to 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 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, 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;
261 tcd = trace_get_tcd();
263 /* fs_trace_get_tcd() grabs a lock, which disables preemption and
264 * pins us to a particular CPU. This avoids an smp_processor_id()
265 * warning on Linux when debugging is enabled. */
266 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
268 if (tcd == NULL) /* arch may not log in IRQ context */
271 if (tcd->tcd_shutting_down) {
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.
291 tage = trace_get_tage(tcd, needed + known_size + 1);
293 if (needed + known_size > CFS_PAGE_SIZE)
301 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
303 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
305 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
315 needed = vsnprintf(string_buf, max_nob, format1, ap);
320 remain = max_nob - needed;
324 va_start(ap, format2);
325 needed += vsnprintf(string_buf + needed, remain,
330 if (needed < max_nob) /* well. printing ok.. */
334 if (*(string_buf+needed-1) != '\n')
335 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
338 header.ph_len = known_size + needed;
339 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
341 if (libcfs_debug_binary) {
342 memcpy(debug_buf, &header, sizeof(header));
343 tage->used += sizeof(header);
344 debug_buf += sizeof(header);
347 /* indent message according to the nesting level */
348 while (depth-- > 0) {
349 *(debug_buf++) = '.';
353 strcpy(debug_buf, file);
354 tage->used += strlen(file) + 1;
355 debug_buf += strlen(file) + 1;
358 strcpy(debug_buf, fn);
359 tage->used += strlen(fn) + 1;
360 debug_buf += strlen(fn) + 1;
363 __LASSERT(debug_buf == string_buf);
365 tage->used += needed;
366 __LASSERT (tage->used <= CFS_PAGE_SIZE);
369 if ((mask & libcfs_printk) == 0) {
370 /* no console output requested */
377 if (libcfs_console_ratelimit &&
378 cdls->cdls_next != 0 && /* not first time ever */
379 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
380 /* skipping a console message */
387 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
388 libcfs_console_max_delay
389 + cfs_time_seconds(10))) {
390 /* last timeout was a long time ago */
391 cdls->cdls_delay /= libcfs_console_backoff * 4;
393 cdls->cdls_delay *= libcfs_console_backoff;
395 if (cdls->cdls_delay < libcfs_console_min_delay)
396 cdls->cdls_delay = libcfs_console_min_delay;
397 else if (cdls->cdls_delay > libcfs_console_max_delay)
398 cdls->cdls_delay = libcfs_console_max_delay;
401 /* ensure cdls_next is never zero after it's been seen */
402 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
406 print_to_console(&header, mask, string_buf, needed, file, fn);
409 string_buf = trace_get_console_buffer();
412 if (format1 != NULL) {
414 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
417 if (format2 != NULL) {
418 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
420 va_start(ap, format2);
421 needed += vsnprintf(string_buf+needed, remain, format2, ap);
425 print_to_console(&header, mask,
426 string_buf, needed, file, fn);
428 trace_put_console_buffer(string_buf);
431 if (cdls != NULL && cdls->cdls_count != 0) {
432 string_buf = trace_get_console_buffer();
434 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
435 "Skipped %d previous similar message%s\n",
436 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
438 print_to_console(&header, mask,
439 string_buf, needed, file, fn);
441 trace_put_console_buffer(string_buf);
442 cdls->cdls_count = 0;
447 EXPORT_SYMBOL(libcfs_debug_vmsg2);
450 libcfs_assertion_failed(const char *expr, const char *file,
451 const char *func, const int line)
453 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
454 "ASSERTION(%s) failed\n", expr);
455 lbug_with_loc(file, func, line);
457 EXPORT_SYMBOL(libcfs_assertion_failed);
460 trace_assertion_failed(const char *str,
461 const char *fn, const char *file, int line)
463 struct ptldebug_header hdr;
465 libcfs_panic_in_progress = 1;
466 libcfs_catastrophe = 1;
469 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
472 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
474 LIBCFS_PANIC("Lustre debug assertion failure\n");
480 panic_collect_pages(struct page_collection *pc)
482 /* Do the collect_pages job on a single CPU: assumes that all other
483 * CPUs have been stopped during a panic. If this isn't true for some
484 * arch, this will have to be implemented separately in each arch. */
487 struct trace_cpu_data *tcd;
489 CFS_INIT_LIST_HEAD(&pc->pc_pages);
491 tcd_for_each(tcd, i, j) {
492 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
493 tcd->tcd_cur_pages = 0;
495 if (pc->pc_want_daemon_pages) {
496 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
497 tcd->tcd_cur_daemon_pages = 0;
502 static void collect_pages_on_all_cpus(struct page_collection *pc)
504 struct trace_cpu_data *tcd;
507 spin_lock(&pc->pc_lock);
508 for_each_possible_cpu(cpu) {
509 tcd_for_each_type_lock(tcd, i, cpu) {
510 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
511 tcd->tcd_cur_pages = 0;
512 if (pc->pc_want_daemon_pages) {
513 list_splice_init(&tcd->tcd_daemon_pages,
515 tcd->tcd_cur_daemon_pages = 0;
519 spin_unlock(&pc->pc_lock);
522 static void collect_pages(struct page_collection *pc)
524 CFS_INIT_LIST_HEAD(&pc->pc_pages);
526 if (libcfs_panic_in_progress)
527 panic_collect_pages(pc);
529 collect_pages_on_all_cpus(pc);
532 static void put_pages_back_on_all_cpus(struct page_collection *pc)
534 struct trace_cpu_data *tcd;
535 struct list_head *cur_head;
536 struct trace_page *tage;
537 struct trace_page *tmp;
540 spin_lock(&pc->pc_lock);
541 for_each_possible_cpu(cpu) {
542 tcd_for_each_type_lock(tcd, i, cpu) {
543 cur_head = tcd->tcd_pages.next;
545 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
548 __LASSERT_TAGE_INVARIANT(tage);
550 if (tage->cpu != cpu || tage->type != i)
553 tage_to_tail(tage, cur_head);
554 tcd->tcd_cur_pages++;
558 spin_unlock(&pc->pc_lock);
561 static void put_pages_back(struct page_collection *pc)
563 if (!libcfs_panic_in_progress)
564 put_pages_back_on_all_cpus(pc);
567 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
568 * we have a good amount of data at all times for dumping during an LBUG, even
569 * if we have been steadily writing (and otherwise discarding) pages via the
571 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
572 struct trace_cpu_data *tcd)
574 struct trace_page *tage;
575 struct trace_page *tmp;
577 spin_lock(&pc->pc_lock);
578 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
580 __LASSERT_TAGE_INVARIANT(tage);
582 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
585 tage_to_tail(tage, &tcd->tcd_daemon_pages);
586 tcd->tcd_cur_daemon_pages++;
588 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
589 struct trace_page *victim;
591 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
592 victim = tage_from_list(tcd->tcd_daemon_pages.next);
594 __LASSERT_TAGE_INVARIANT(victim);
596 list_del(&victim->linkage);
598 tcd->tcd_cur_daemon_pages--;
601 spin_unlock(&pc->pc_lock);
604 static void put_pages_on_daemon_list(struct page_collection *pc)
606 struct trace_cpu_data *tcd;
609 for_each_possible_cpu(cpu) {
610 tcd_for_each_type_lock(tcd, i, cpu)
611 put_pages_on_tcd_daemon_list(pc, tcd);
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 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
629 __LASSERT_TAGE_INVARIANT(tage);
632 p = cfs_page_address(page);
633 while (p < ((char *)cfs_page_address(page) + tage->used)) {
634 struct ptldebug_header *hdr;
639 p += strlen(file) + 1;
642 len = hdr->ph_len - (p - (char *)hdr);
644 print_to_console(hdr, D_EMERG, p, len, file, fn);
649 list_del(&tage->linkage);
654 int tracefile_dump_all_pages(char *filename)
656 struct page_collection pc;
658 struct trace_page *tage;
659 struct trace_page *tmp;
664 tracefile_write_lock();
666 filp = cfs_filp_open(filename,
667 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
670 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
675 spin_lock_init(&pc.pc_lock);
676 pc.pc_want_daemon_pages = 1;
678 if (list_empty(&pc.pc_pages)) {
683 /* ok, for now, just write the pages. in the future we'll be building
684 * iobufs with the pages and calling generic_direct_IO */
686 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
688 __LASSERT_TAGE_INVARIANT(tage);
690 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
691 tage->used, cfs_filp_poff(filp));
692 if (rc != (int)tage->used) {
693 printk(KERN_WARNING "wanted to write %u but wrote "
694 "%d\n", tage->used, rc);
696 __LASSERT(list_empty(&pc.pc_pages));
699 list_del(&tage->linkage);
703 rc = cfs_filp_fsync(filp);
705 printk(KERN_ERR "sync returns %d\n", rc);
707 cfs_filp_close(filp);
709 tracefile_write_unlock();
713 void trace_flush_pages(void)
715 struct page_collection pc;
716 struct trace_page *tage;
717 struct trace_page *tmp;
719 spin_lock_init(&pc.pc_lock);
721 pc.pc_want_daemon_pages = 1;
723 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
725 __LASSERT_TAGE_INVARIANT(tage);
727 list_del(&tage->linkage);
732 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
733 const char *usr_buffer, int usr_buffer_nob)
737 if (usr_buffer_nob > knl_buffer_nob)
740 if (copy_from_user((void *)knl_buffer,
741 (void *)usr_buffer, usr_buffer_nob))
744 nob = strnlen(knl_buffer, usr_buffer_nob);
745 while (nob-- >= 0) /* strip trailing whitespace */
746 if (!isspace(knl_buffer[nob]))
749 if (nob < 0) /* empty string */
752 if (nob == knl_buffer_nob) /* no space to terminate */
755 knl_buffer[nob + 1] = 0; /* terminate */
759 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
760 const char *knl_buffer, char *append)
762 /* NB if 'append' != NULL, it's a single character to append to the
763 * copied out string - usually "\n", for /proc entries and "" (i.e. a
764 * terminating zero byte) for sysctl entries */
765 int nob = strlen(knl_buffer);
767 if (nob > usr_buffer_nob)
768 nob = usr_buffer_nob;
770 if (copy_to_user(usr_buffer, knl_buffer, nob))
773 if (append != NULL && nob < usr_buffer_nob) {
774 if (copy_to_user(usr_buffer + nob, append, 1))
782 EXPORT_SYMBOL(trace_copyout_string);
784 int trace_allocate_string_buffer(char **str, int nob)
786 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
789 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
796 void trace_free_string_buffer(char *str, int nob)
801 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
806 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
810 rc = trace_copyin_string(str, usr_str_nob + 1,
811 usr_str, usr_str_nob);
815 #if !defined(__WINNT__)
821 rc = tracefile_dump_all_pages(str);
823 trace_free_string_buffer(str, usr_str_nob + 1);
827 int trace_daemon_command(char *str)
831 tracefile_write_lock();
833 if (strcmp(str, "stop") == 0) {
834 tracefile_write_unlock();
836 tracefile_write_lock();
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);
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;
957 /* we're started late enough that we pick up init's fs context */
958 /* this is so broken in uml? what on earth is going on? */
959 cfs_daemonize("ktracefiled");
961 spin_lock_init(&pc.pc_lock);
962 complete(&tctl->tctl_start);
965 cfs_waitlink_t __wait;
967 pc.pc_want_daemon_pages = 0;
969 if (list_empty(&pc.pc_pages))
973 tracefile_read_lock();
974 if (tracefile[0] != 0) {
975 filp = cfs_filp_open(tracefile,
976 O_CREAT | O_RDWR | O_LARGEFILE,
979 printk(KERN_WARNING "couldn't open %s: %d\n",
982 tracefile_read_unlock();
984 put_pages_on_daemon_list(&pc);
985 __LASSERT(list_empty(&pc.pc_pages));
991 /* mark the first header, so we can sort in chunks */
992 tage = tage_from_list(pc.pc_pages.next);
993 __LASSERT_TAGE_INVARIANT(tage);
995 hdr = cfs_page_address(tage->page);
996 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
998 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1001 __LASSERT_TAGE_INVARIANT(tage);
1003 if (f_pos >= (off_t)tracefile_size)
1005 else if (f_pos > cfs_filp_size(filp))
1006 f_pos = cfs_filp_size(filp);
1008 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1009 tage->used, &f_pos);
1010 if (rc != (int)tage->used) {
1011 printk(KERN_WARNING "wanted to write %u but "
1012 "wrote %d\n", tage->used, rc);
1013 put_pages_back(&pc);
1014 __LASSERT(list_empty(&pc.pc_pages));
1019 cfs_filp_close(filp);
1020 put_pages_on_daemon_list(&pc);
1021 if (!list_empty(&pc.pc_pages)) {
1024 printk(KERN_ALERT "Lustre: trace pages aren't empty\n");
1025 printk(KERN_ALERT "total cpus(%d): ", num_possible_cpus());
1026 for (i = 0; i < num_possible_cpus(); i++)
1028 printk(KERN_ALERT "%d(on) ", i);
1030 printk(KERN_ALERT "%d(off) ", i);
1031 printk(KERN_ALERT "\n");
1034 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1036 printk(KERN_ALERT "page %d belongs to cpu %d\n",
1038 printk(KERN_ALERT "There are %d pages unwritten\n", i);
1040 __LASSERT(list_empty(&pc.pc_pages));
1042 if (atomic_read(&tctl->tctl_shutdown)) {
1043 if (last_loop == 0) {
1050 cfs_waitlink_init(&__wait);
1051 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1052 set_current_state(TASK_INTERRUPTIBLE);
1053 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1054 cfs_time_seconds(1));
1055 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1057 complete(&tctl->tctl_stop);
1061 int trace_start_thread(void)
1063 struct tracefiled_ctl *tctl = &trace_tctl;
1066 mutex_down(&trace_thread_sem);
1070 init_completion(&tctl->tctl_start);
1071 init_completion(&tctl->tctl_stop);
1072 cfs_waitq_init(&tctl->tctl_waitq);
1073 atomic_set(&tctl->tctl_shutdown, 0);
1075 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1080 wait_for_completion(&tctl->tctl_start);
1083 mutex_up(&trace_thread_sem);
1087 void trace_stop_thread(void)
1089 struct tracefiled_ctl *tctl = &trace_tctl;
1091 mutex_down(&trace_thread_sem);
1092 if (thread_running) {
1093 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1094 atomic_set(&tctl->tctl_shutdown, 1);
1095 wait_for_completion(&tctl->tctl_stop);
1098 mutex_up(&trace_thread_sem);
1101 int tracefile_init(int max_pages)
1103 struct trace_cpu_data *tcd;
1109 rc = tracefile_init_arch();
1113 tcd_for_each(tcd, i, j) {
1114 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1115 factor = tcd->tcd_pages_factor;
1116 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1117 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1118 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1119 tcd->tcd_cur_pages = 0;
1120 tcd->tcd_cur_stock_pages = 0;
1121 tcd->tcd_cur_daemon_pages = 0;
1122 tcd->tcd_max_pages = (max_pages * factor) / 100;
1123 LASSERT(tcd->tcd_max_pages > 0);
1124 tcd->tcd_shutting_down = 0;
1130 static void trace_cleanup_on_all_cpus(void)
1132 struct trace_cpu_data *tcd;
1133 struct trace_page *tage;
1134 struct trace_page *tmp;
1137 for_each_possible_cpu(cpu) {
1138 tcd_for_each_type_lock(tcd, i, cpu) {
1139 tcd->tcd_shutting_down = 1;
1141 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1143 __LASSERT_TAGE_INVARIANT(tage);
1145 list_del(&tage->linkage);
1149 tcd->tcd_cur_pages = 0;
1154 static void trace_cleanup(void)
1156 struct page_collection pc;
1158 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1159 spin_lock_init(&pc.pc_lock);
1161 trace_cleanup_on_all_cpus();
1163 tracefile_fini_arch();
1166 void tracefile_exit(void)
1168 trace_stop_thread();