4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, 2014, Intel Corporation.
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 <linux/kthread.h>
48 #include <libcfs/libcfs.h>
50 /* XXX move things up to the top, comment */
51 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
53 char cfs_tracefile[TRACEFILE_NAME_SIZE];
54 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
55 static struct tracefiled_ctl trace_tctl;
56 static DEFINE_MUTEX(cfs_trace_thread_mutex);
57 static int thread_running = 0;
59 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
61 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
62 struct cfs_trace_cpu_data *tcd);
64 static inline struct cfs_trace_page *
65 cfs_tage_from_list(struct list_head *list)
67 return list_entry(list, struct cfs_trace_page, linkage);
70 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
73 struct cfs_trace_page *tage;
75 /* My caller is trying to free memory */
76 if (!in_interrupt() && memory_pressure_get())
80 * Don't spam console with allocation failures: they will be reported
81 * by upper layer anyway.
84 page = alloc_page(gfp);
88 tage = kmalloc(sizeof(*tage), gfp);
95 atomic_inc(&cfs_tage_allocated);
99 static void cfs_tage_free(struct cfs_trace_page *tage)
101 __LASSERT(tage != NULL);
102 __LASSERT(tage->page != NULL);
104 __free_page(tage->page);
106 atomic_dec(&cfs_tage_allocated);
109 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
110 struct list_head *queue)
112 __LASSERT(tage != NULL);
113 __LASSERT(queue != NULL);
115 list_move_tail(&tage->linkage, queue);
118 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
119 struct list_head *stock)
124 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
125 * from here: this will lead to infinite recursion.
128 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
129 struct cfs_trace_page *tage;
131 tage = cfs_tage_alloc(gfp);
134 list_add_tail(&tage->linkage, stock);
139 /* return a page that has 'len' bytes left at the end */
140 static struct cfs_trace_page *
141 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
143 struct cfs_trace_page *tage;
145 if (tcd->tcd_cur_pages > 0) {
146 __LASSERT(!list_empty(&tcd->tcd_pages));
147 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
148 if (tage->used + len <= PAGE_CACHE_SIZE)
152 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
153 if (tcd->tcd_cur_stock_pages > 0) {
154 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
155 --tcd->tcd_cur_stock_pages;
156 list_del_init(&tage->linkage);
158 tage = cfs_tage_alloc(GFP_ATOMIC);
159 if (unlikely(tage == NULL)) {
160 if ((!memory_pressure_get() ||
161 in_interrupt()) && printk_ratelimit())
163 "cannot allocate a tage (%ld)\n",
170 tage->cpu = smp_processor_id();
171 tage->type = tcd->tcd_type;
172 list_add_tail(&tage->linkage, &tcd->tcd_pages);
173 tcd->tcd_cur_pages++;
175 if (tcd->tcd_cur_pages > 8 && thread_running) {
176 struct tracefiled_ctl *tctl = &trace_tctl;
178 * wake up tracefiled to process some pages.
180 wake_up(&tctl->tctl_waitq);
187 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
189 int pgcount = tcd->tcd_cur_pages / 10;
190 struct page_collection pc;
191 struct cfs_trace_page *tage;
192 struct cfs_trace_page *tmp;
195 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
196 * from here: this will lead to infinite recursion.
199 if (printk_ratelimit())
200 printk(KERN_WARNING "debug daemon buffer overflowed; "
201 "discarding 10%% of pages (%d of %ld)\n",
202 pgcount + 1, tcd->tcd_cur_pages);
204 INIT_LIST_HEAD(&pc.pc_pages);
206 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
210 list_move_tail(&tage->linkage, &pc.pc_pages);
211 tcd->tcd_cur_pages--;
213 put_pages_on_tcd_daemon_list(&pc, tcd);
216 /* return a page that has 'len' bytes left at the end */
217 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
220 struct cfs_trace_page *tage;
223 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
224 * from here: this will lead to infinite recursion.
227 if (len > PAGE_CACHE_SIZE) {
229 "cowardly refusing to write %lu bytes in a page\n", len);
233 tage = cfs_trace_get_tage_try(tcd, len);
238 if (tcd->tcd_cur_pages > 0) {
239 tage = cfs_tage_from_list(tcd->tcd_pages.next);
241 cfs_tage_to_tail(tage, &tcd->tcd_pages);
246 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
247 const char *format, ...)
252 va_start(args, format);
253 rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
258 EXPORT_SYMBOL(libcfs_debug_msg);
260 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
261 const char *format1, va_list args,
262 const char *format2, ...)
264 struct cfs_trace_cpu_data *tcd = NULL;
265 struct ptldebug_header header = {0};
266 struct cfs_trace_page *tage;
267 /* string_buf is used only if tcd != NULL, and is always set then */
268 char *string_buf = NULL;
271 int needed = 85; /* average message length */
276 int mask = msgdata->msg_mask;
277 char *file = (char *)msgdata->msg_file;
278 cfs_debug_limit_state_t *cdls = msgdata->msg_cdls;
280 if (strchr(file, '/'))
281 file = strrchr(file, '/') + 1;
283 tcd = cfs_trace_get_tcd();
285 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
286 * pins us to a particular CPU. This avoids an smp_processor_id()
287 * warning on Linux when debugging is enabled. */
288 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
290 if (tcd == NULL) /* arch may not log in IRQ context */
293 if (tcd->tcd_cur_pages == 0)
294 header.ph_flags |= PH_FLAG_FIRST_RECORD;
296 if (tcd->tcd_shutting_down) {
297 cfs_trace_put_tcd(tcd);
302 known_size = strlen(file) + 1;
304 known_size += strlen(msgdata->msg_fn) + 1;
306 if (libcfs_debug_binary)
307 known_size += sizeof(header);
310 * '2' used because vsnprintf return real size required for output
311 * _without_ terminating NULL.
312 * if needed is to small for this format.
314 for (i = 0; i < 2; i++) {
315 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
317 if (needed + known_size > PAGE_CACHE_SIZE)
320 cfs_trace_put_tcd(tcd);
325 string_buf = (char *)page_address(tage->page) +
326 tage->used + known_size;
328 max_nob = PAGE_CACHE_SIZE - tage->used - known_size;
330 printk(KERN_EMERG "negative max_nob: %d\n",
333 cfs_trace_put_tcd(tcd);
341 needed = vsnprintf(string_buf, max_nob, format1, ap);
346 remain = max_nob - needed;
350 va_start(ap, format2);
351 needed += vsnprintf(string_buf + needed, remain,
356 if (needed < max_nob) /* well. printing ok.. */
360 if (*(string_buf+needed-1) != '\n')
361 printk(KERN_INFO "format at %s:%d:%s doesn't end in "
362 "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
364 header.ph_len = known_size + needed;
365 debug_buf = (char *)page_address(tage->page) + tage->used;
367 if (libcfs_debug_binary) {
368 memcpy(debug_buf, &header, sizeof(header));
369 tage->used += sizeof(header);
370 debug_buf += sizeof(header);
373 strcpy(debug_buf, file);
374 tage->used += strlen(file) + 1;
375 debug_buf += strlen(file) + 1;
377 if (msgdata->msg_fn) {
378 strcpy(debug_buf, msgdata->msg_fn);
379 tage->used += strlen(msgdata->msg_fn) + 1;
380 debug_buf += strlen(msgdata->msg_fn) + 1;
383 __LASSERT(debug_buf == string_buf);
385 tage->used += needed;
386 __LASSERT(tage->used <= PAGE_CACHE_SIZE);
389 if ((mask & libcfs_printk) == 0) {
390 /* no console output requested */
392 cfs_trace_put_tcd(tcd);
397 if (libcfs_console_ratelimit &&
398 cdls->cdls_next != 0 && /* not first time ever */
399 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
400 /* skipping a console message */
403 cfs_trace_put_tcd(tcd);
407 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
408 libcfs_console_max_delay
409 + cfs_time_seconds(10))) {
410 /* last timeout was a long time ago */
411 cdls->cdls_delay /= libcfs_console_backoff * 4;
413 cdls->cdls_delay *= libcfs_console_backoff;
416 if (cdls->cdls_delay < libcfs_console_min_delay)
417 cdls->cdls_delay = libcfs_console_min_delay;
418 else if (cdls->cdls_delay > libcfs_console_max_delay)
419 cdls->cdls_delay = libcfs_console_max_delay;
421 /* ensure cdls_next is never zero after it's been seen */
422 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
426 cfs_print_to_console(&header, mask, string_buf, needed, file,
428 cfs_trace_put_tcd(tcd);
430 string_buf = cfs_trace_get_console_buffer();
433 if (format1 != NULL) {
435 needed = vsnprintf(string_buf,
436 CFS_TRACE_CONSOLE_BUFFER_SIZE,
440 if (format2 != NULL) {
441 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
443 va_start(ap, format2);
444 needed += vsnprintf(string_buf+needed, remain,
449 cfs_print_to_console(&header, mask,
450 string_buf, needed, file, msgdata->msg_fn);
452 cfs_trace_put_console_buffer(string_buf);
455 if (cdls != NULL && cdls->cdls_count != 0) {
456 string_buf = cfs_trace_get_console_buffer();
458 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
459 "Skipped %d previous similar message%s\n",
461 (cdls->cdls_count > 1) ? "s" : "");
463 cfs_print_to_console(&header, mask,
464 string_buf, needed, file, msgdata->msg_fn);
466 cfs_trace_put_console_buffer(string_buf);
467 cdls->cdls_count = 0;
472 EXPORT_SYMBOL(libcfs_debug_vmsg2);
475 cfs_trace_assertion_failed(const char *str,
476 struct libcfs_debug_msg_data *msgdata)
478 struct ptldebug_header hdr;
480 libcfs_panic_in_progress = 1;
481 libcfs_catastrophe = 1;
484 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
486 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
487 msgdata->msg_file, msgdata->msg_fn);
489 panic("Lustre debug assertion failure\n");
495 panic_collect_pages(struct page_collection *pc)
497 /* Do the collect_pages job on a single CPU: assumes that all other
498 * CPUs have been stopped during a panic. If this isn't true for some
499 * arch, this will have to be implemented separately in each arch. */
502 struct cfs_trace_cpu_data *tcd;
504 INIT_LIST_HEAD(&pc->pc_pages);
506 cfs_tcd_for_each(tcd, i, j) {
507 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
508 tcd->tcd_cur_pages = 0;
510 if (pc->pc_want_daemon_pages) {
511 list_splice_init(&tcd->tcd_daemon_pages,
513 tcd->tcd_cur_daemon_pages = 0;
518 static void collect_pages_on_all_cpus(struct page_collection *pc)
520 struct cfs_trace_cpu_data *tcd;
523 for_each_possible_cpu(cpu) {
524 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
525 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
526 tcd->tcd_cur_pages = 0;
527 if (pc->pc_want_daemon_pages) {
528 list_splice_init(&tcd->tcd_daemon_pages,
530 tcd->tcd_cur_daemon_pages = 0;
536 static void collect_pages(struct page_collection *pc)
538 INIT_LIST_HEAD(&pc->pc_pages);
540 if (libcfs_panic_in_progress)
541 panic_collect_pages(pc);
543 collect_pages_on_all_cpus(pc);
546 static void put_pages_back_on_all_cpus(struct page_collection *pc)
548 struct cfs_trace_cpu_data *tcd;
549 struct list_head *cur_head;
550 struct cfs_trace_page *tage;
551 struct cfs_trace_page *tmp;
554 for_each_possible_cpu(cpu) {
555 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
556 cur_head = tcd->tcd_pages.next;
558 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
561 __LASSERT_TAGE_INVARIANT(tage);
563 if (tage->cpu != cpu || tage->type != i)
566 cfs_tage_to_tail(tage, cur_head);
567 tcd->tcd_cur_pages++;
573 static void put_pages_back(struct page_collection *pc)
575 if (!libcfs_panic_in_progress)
576 put_pages_back_on_all_cpus(pc);
579 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
580 * we have a good amount of data at all times for dumping during an LBUG, even
581 * if we have been steadily writing (and otherwise discarding) pages via the
583 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
584 struct cfs_trace_cpu_data *tcd)
586 struct cfs_trace_page *tage;
587 struct cfs_trace_page *tmp;
589 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, 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(!list_empty(&tcd->tcd_daemon_pages));
602 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
604 __LASSERT_TAGE_INVARIANT(victim);
606 list_del(&victim->linkage);
607 cfs_tage_free(victim);
608 tcd->tcd_cur_daemon_pages--;
613 static void put_pages_on_daemon_list(struct page_collection *pc)
615 struct cfs_trace_cpu_data *tcd;
618 for_each_possible_cpu(cpu) {
619 cfs_tcd_for_each_type_lock(tcd, i, cpu)
620 put_pages_on_tcd_daemon_list(pc, tcd);
624 void cfs_trace_debug_print(void)
626 struct page_collection pc;
627 struct cfs_trace_page *tage;
628 struct cfs_trace_page *tmp;
630 pc.pc_want_daemon_pages = 1;
632 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
636 __LASSERT_TAGE_INVARIANT(tage);
639 p = page_address(page);
640 while (p < ((char *)page_address(page) + tage->used)) {
641 struct ptldebug_header *hdr;
646 p += strlen(file) + 1;
649 len = hdr->ph_len - (int)(p - (char *)hdr);
651 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
656 list_del(&tage->linkage);
661 int cfs_tracefile_dump_all_pages(char *filename)
663 struct page_collection pc;
665 struct cfs_trace_page *tage;
666 struct cfs_trace_page *tmp;
667 mm_segment_t __oldfs;
670 cfs_tracefile_write_lock();
672 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
676 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
681 pc.pc_want_daemon_pages = 1;
683 if (list_empty(&pc.pc_pages)) {
690 /* ok, for now, just write the pages. in the future we'll be building
691 * iobufs with the pages and calling generic_direct_IO */
692 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
694 __LASSERT_TAGE_INVARIANT(tage);
696 rc = vfs_write(filp, page_address(tage->page), tage->used,
698 if (rc != (int)tage->used) {
699 printk(KERN_WARNING "wanted to write %u but wrote "
700 "%d\n", tage->used, rc);
702 __LASSERT(list_empty(&pc.pc_pages));
705 list_del(&tage->linkage);
709 rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
711 printk(KERN_ERR "sync returns %d\n", rc);
713 filp_close(filp, NULL);
715 cfs_tracefile_write_unlock();
719 void cfs_trace_flush_pages(void)
721 struct page_collection pc;
722 struct cfs_trace_page *tage;
723 struct cfs_trace_page *tmp;
725 pc.pc_want_daemon_pages = 1;
727 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
729 __LASSERT_TAGE_INVARIANT(tage);
731 list_del(&tage->linkage);
736 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
737 const char __user *usr_buffer, int usr_buffer_nob)
741 if (usr_buffer_nob > knl_buffer_nob)
744 if (copy_from_user(knl_buffer, 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 */
761 EXPORT_SYMBOL(cfs_trace_copyin_string);
763 int cfs_trace_copyout_string(char __user *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))
786 EXPORT_SYMBOL(cfs_trace_copyout_string);
788 int cfs_trace_allocate_string_buffer(char **str, int nob)
790 if (nob > 2 * PAGE_CACHE_SIZE) /* string must be "sensible" */
793 *str = kmalloc(nob, GFP_IOFS | __GFP_ZERO);
800 void cfs_trace_free_string_buffer(char *str, int nob)
805 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
810 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
814 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
815 usr_str, usr_str_nob);
823 rc = cfs_tracefile_dump_all_pages(str);
825 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
829 int cfs_trace_daemon_command(char *str)
833 cfs_tracefile_write_lock();
835 if (strcmp(str, "stop") == 0) {
836 cfs_tracefile_write_unlock();
837 cfs_trace_stop_thread();
838 cfs_tracefile_write_lock();
839 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
841 } else if (strncmp(str, "size=", 5) == 0) {
842 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
843 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
844 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
846 cfs_tracefile_size <<= 20;
848 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
850 } else if (str[0] != '/') {
853 strcpy(cfs_tracefile, str);
856 "Lustre: debug daemon will attempt to start writing "
857 "to %s (%lukB max)\n", cfs_tracefile,
858 (long)(cfs_tracefile_size >> 10));
860 cfs_trace_start_thread();
863 cfs_tracefile_write_unlock();
867 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
872 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
876 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
877 usr_str, usr_str_nob);
879 rc = cfs_trace_daemon_command(str);
881 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
885 int cfs_trace_set_debug_mb(int mb)
890 int limit = cfs_trace_max_debug_mb();
891 struct cfs_trace_cpu_data *tcd;
893 if (mb < num_possible_cpus()) {
895 "Lustre: %d MB is too small for debug buffer size, "
896 "setting it to %d MB.\n", mb, num_possible_cpus());
897 mb = num_possible_cpus();
902 "Lustre: %d MB is too large for debug buffer size, "
903 "setting it to %d MB.\n", mb, limit);
907 mb /= num_possible_cpus();
908 pages = mb << (20 - PAGE_CACHE_SHIFT);
910 cfs_tracefile_write_lock();
912 cfs_tcd_for_each(tcd, i, j)
913 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
915 cfs_tracefile_write_unlock();
920 int cfs_trace_set_debug_mb_usrstr(void __user *usr_str, int usr_str_nob)
925 rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
929 return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
932 int cfs_trace_get_debug_mb(void)
936 struct cfs_trace_cpu_data *tcd;
939 cfs_tracefile_read_lock();
941 cfs_tcd_for_each(tcd, i, j)
942 total_pages += tcd->tcd_max_pages;
944 cfs_tracefile_read_unlock();
946 return (total_pages >> (20 - PAGE_CACHE_SHIFT)) + 1;
949 static int tracefiled(void *arg)
951 struct page_collection pc;
952 struct tracefiled_ctl *tctl = arg;
953 struct cfs_trace_page *tage;
954 struct cfs_trace_page *tmp;
955 mm_segment_t __oldfs;
960 /* we're started late enough that we pick up init's fs context */
961 /* this is so broken in uml? what on earth is going on? */
963 complete(&tctl->tctl_start);
968 pc.pc_want_daemon_pages = 0;
970 if (list_empty(&pc.pc_pages))
974 cfs_tracefile_read_lock();
975 if (cfs_tracefile[0] != 0) {
976 filp = filp_open(cfs_tracefile,
977 O_CREAT | O_RDWR | O_LARGEFILE,
982 printk(KERN_WARNING "couldn't open %s: "
983 "%d\n", cfs_tracefile, rc);
986 cfs_tracefile_read_unlock();
988 put_pages_on_daemon_list(&pc);
989 __LASSERT(list_empty(&pc.pc_pages));
995 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
996 struct dentry *de = filp->f_path.dentry;
999 __LASSERT_TAGE_INVARIANT(tage);
1001 if (f_pos >= (off_t)cfs_tracefile_size)
1003 else if (f_pos > i_size_read(de->d_inode))
1004 f_pos = i_size_read(de->d_inode);
1006 rc = vfs_write(filp, page_address(tage->page),
1007 tage->used, &f_pos);
1008 if (rc != (int)tage->used) {
1009 printk(KERN_WARNING "wanted to write %u "
1010 "but wrote %d\n", tage->used, rc);
1011 put_pages_back(&pc);
1012 __LASSERT(list_empty(&pc.pc_pages));
1018 filp_close(filp, NULL);
1019 put_pages_on_daemon_list(&pc);
1020 if (!list_empty(&pc.pc_pages)) {
1023 printk(KERN_ALERT "Lustre: trace pages aren't "
1025 printk(KERN_ERR "total cpus(%d): ",
1026 num_possible_cpus());
1027 for (i = 0; i < num_possible_cpus(); i++)
1029 printk(KERN_ERR "%d(on) ", i);
1031 printk(KERN_ERR "%d(off) ", i);
1032 printk(KERN_ERR "\n");
1035 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1037 printk(KERN_ERR "page %d belongs to cpu "
1038 "%d\n", ++i, tage->cpu);
1039 printk(KERN_ERR "There are %d pages unwritten\n",
1042 __LASSERT(list_empty(&pc.pc_pages));
1044 if (atomic_read(&tctl->tctl_shutdown)) {
1045 if (last_loop == 0) {
1052 init_waitqueue_entry(&__wait, current);
1053 add_wait_queue(&tctl->tctl_waitq, &__wait);
1054 set_current_state(TASK_INTERRUPTIBLE);
1055 schedule_timeout(cfs_time_seconds(1));
1056 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1058 complete(&tctl->tctl_stop);
1062 int cfs_trace_start_thread(void)
1064 struct tracefiled_ctl *tctl = &trace_tctl;
1067 mutex_lock(&cfs_trace_thread_mutex);
1071 init_completion(&tctl->tctl_start);
1072 init_completion(&tctl->tctl_stop);
1073 init_waitqueue_head(&tctl->tctl_waitq);
1074 atomic_set(&tctl->tctl_shutdown, 0);
1076 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1081 wait_for_completion(&tctl->tctl_start);
1084 mutex_unlock(&cfs_trace_thread_mutex);
1088 void cfs_trace_stop_thread(void)
1090 struct tracefiled_ctl *tctl = &trace_tctl;
1092 mutex_lock(&cfs_trace_thread_mutex);
1093 if (thread_running) {
1095 "Lustre: shutting down debug daemon thread...\n");
1096 atomic_set(&tctl->tctl_shutdown, 1);
1097 wait_for_completion(&tctl->tctl_stop);
1100 mutex_unlock(&cfs_trace_thread_mutex);
1103 int cfs_tracefile_init(int max_pages)
1105 struct cfs_trace_cpu_data *tcd;
1111 rc = cfs_tracefile_init_arch();
1115 cfs_tcd_for_each(tcd, i, j) {
1116 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1117 factor = tcd->tcd_pages_factor;
1118 INIT_LIST_HEAD(&tcd->tcd_pages);
1119 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1120 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1121 tcd->tcd_cur_pages = 0;
1122 tcd->tcd_cur_stock_pages = 0;
1123 tcd->tcd_cur_daemon_pages = 0;
1124 tcd->tcd_max_pages = (max_pages * factor) / 100;
1125 LASSERT(tcd->tcd_max_pages > 0);
1126 tcd->tcd_shutting_down = 0;
1131 static void trace_cleanup_on_all_cpus(void)
1133 struct cfs_trace_cpu_data *tcd;
1134 struct cfs_trace_page *tage;
1135 struct cfs_trace_page *tmp;
1138 for_each_possible_cpu(cpu) {
1139 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1140 tcd->tcd_shutting_down = 1;
1142 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1143 __LASSERT_TAGE_INVARIANT(tage);
1145 list_del(&tage->linkage);
1146 cfs_tage_free(tage);
1148 tcd->tcd_cur_pages = 0;
1153 static void cfs_trace_cleanup(void)
1155 struct page_collection pc;
1157 INIT_LIST_HEAD(&pc.pc_pages);
1159 trace_cleanup_on_all_cpus();
1161 cfs_tracefile_fini_arch();
1164 void cfs_tracefile_exit(void)
1166 cfs_trace_stop_thread();
1167 cfs_trace_cleanup();