1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * libcfs/libcfs/tracefile.c
38 * Author: Zach Brown <zab@clusterfs.com>
39 * Author: Phil Schwan <phil@clusterfs.com>
43 #define DEBUG_SUBSYSTEM S_LNET
44 #define LUSTRE_TRACEFILE_PRIVATE
45 #include "tracefile.h"
47 #include <libcfs/libcfs.h>
49 /* XXX move things up to the top, comment */
50 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[CFS_NR_CPUS] __cacheline_aligned;
52 char cfs_tracefile[TRACEFILE_NAME_SIZE];
53 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
54 static struct tracefiled_ctl trace_tctl;
55 cfs_semaphore_t cfs_trace_thread_sem;
56 static int thread_running = 0;
58 cfs_atomic_t cfs_tage_allocated = CFS_ATOMIC_INIT(0);
60 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
61 struct cfs_trace_cpu_data *tcd);
63 static inline struct cfs_trace_page *
64 cfs_tage_from_list(cfs_list_t *list)
66 return cfs_list_entry(list, struct cfs_trace_page, linkage);
69 static struct cfs_trace_page *cfs_tage_alloc(int gfp)
72 struct cfs_trace_page *tage;
75 * Don't spam console with allocation failures: they will be reported
76 * by upper layer anyway.
78 gfp |= CFS_ALLOC_NOWARN;
79 page = cfs_alloc_page(gfp);
83 tage = cfs_alloc(sizeof(*tage), gfp);
90 cfs_atomic_inc(&cfs_tage_allocated);
94 static void cfs_tage_free(struct cfs_trace_page *tage)
96 __LASSERT(tage != NULL);
97 __LASSERT(tage->page != NULL);
99 cfs_free_page(tage->page);
101 cfs_atomic_dec(&cfs_tage_allocated);
104 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
107 __LASSERT(tage != NULL);
108 __LASSERT(queue != NULL);
110 cfs_list_move_tail(&tage->linkage, queue);
113 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, int gfp,
119 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
120 * from here: this will lead to infinite recursion.
123 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
124 struct cfs_trace_page *tage;
126 tage = cfs_tage_alloc(gfp);
129 cfs_list_add_tail(&tage->linkage, stock);
134 /* return a page that has 'len' bytes left at the end */
135 static struct cfs_trace_page *
136 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
138 struct cfs_trace_page *tage;
140 if (tcd->tcd_cur_pages > 0) {
141 __LASSERT(!cfs_list_empty(&tcd->tcd_pages));
142 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
143 if (tage->used + len <= CFS_PAGE_SIZE)
147 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
148 if (tcd->tcd_cur_stock_pages > 0) {
149 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
150 -- tcd->tcd_cur_stock_pages;
151 cfs_list_del_init(&tage->linkage);
153 tage = cfs_tage_alloc(CFS_ALLOC_ATOMIC);
155 if (printk_ratelimit())
156 printk(CFS_KERN_WARNING
157 "cannot allocate a tage (%ld)\n",
164 tage->cpu = cfs_smp_processor_id();
165 tage->type = tcd->tcd_type;
166 cfs_list_add_tail(&tage->linkage, &tcd->tcd_pages);
167 tcd->tcd_cur_pages++;
169 if (tcd->tcd_cur_pages > 8 && thread_running) {
170 struct tracefiled_ctl *tctl = &trace_tctl;
172 * wake up tracefiled to process some pages.
174 cfs_waitq_signal(&tctl->tctl_waitq);
181 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
183 int pgcount = tcd->tcd_cur_pages / 10;
184 struct page_collection pc;
185 struct cfs_trace_page *tage;
186 struct cfs_trace_page *tmp;
189 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
190 * from here: this will lead to infinite recursion.
193 if (printk_ratelimit())
194 printk(CFS_KERN_WARNING "debug daemon buffer overflowed; "
195 "discarding 10%% of pages (%d of %ld)\n",
196 pgcount + 1, tcd->tcd_cur_pages);
198 CFS_INIT_LIST_HEAD(&pc.pc_pages);
199 cfs_spin_lock_init(&pc.pc_lock);
201 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
202 struct cfs_trace_page, linkage) {
206 cfs_list_move_tail(&tage->linkage, &pc.pc_pages);
207 tcd->tcd_cur_pages--;
209 put_pages_on_tcd_daemon_list(&pc, tcd);
212 /* return a page that has 'len' bytes left at the end */
213 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
216 struct cfs_trace_page *tage;
219 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
220 * from here: this will lead to infinite recursion.
223 if (len > CFS_PAGE_SIZE) {
225 "cowardly refusing to write %lu bytes in a page\n", len);
229 tage = cfs_trace_get_tage_try(tcd, len);
234 if (tcd->tcd_cur_pages > 0) {
235 tage = cfs_tage_from_list(tcd->tcd_pages.next);
237 cfs_tage_to_tail(tage, &tcd->tcd_pages);
242 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
243 const char *file, const char *fn, const int line,
244 const char *format1, va_list args,
245 const char *format2, ...)
247 struct cfs_trace_cpu_data *tcd = NULL;
248 struct ptldebug_header header = {0};
249 struct cfs_trace_page *tage;
250 /* string_buf is used only if tcd != NULL, and is always set then */
251 char *string_buf = NULL;
254 int needed = 85; /* average message length */
261 if (strchr(file, '/'))
262 file = strrchr(file, '/') + 1;
264 tcd = cfs_trace_get_tcd();
266 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
267 * pins us to a particular CPU. This avoids an smp_processor_id()
268 * warning on Linux when debugging is enabled. */
269 cfs_set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
271 if (tcd == NULL) /* arch may not log in IRQ context */
274 if (tcd->tcd_cur_pages == 0)
275 header.ph_flags |= PH_FLAG_FIRST_RECORD;
277 if (tcd->tcd_shutting_down) {
278 cfs_trace_put_tcd(tcd);
283 depth = __current_nesting_level();
284 known_size = strlen(file) + 1 + depth;
286 known_size += strlen(fn) + 1;
288 if (libcfs_debug_binary)
289 known_size += sizeof(header);
292 * '2' used because vsnprintf return real size required for output
293 * _without_ terminating NULL.
294 * if needed is to small for this format.
296 for (i = 0; i < 2; i++) {
297 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
299 if (needed + known_size > CFS_PAGE_SIZE)
302 cfs_trace_put_tcd(tcd);
307 string_buf = (char *)cfs_page_address(tage->page) +
308 tage->used + known_size;
310 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
312 printk(CFS_KERN_EMERG "negative max_nob: %i\n",
315 cfs_trace_put_tcd(tcd);
323 needed = vsnprintf(string_buf, max_nob, format1, ap);
328 remain = max_nob - needed;
332 va_start(ap, format2);
333 needed += vsnprintf(string_buf + needed, remain,
338 if (needed < max_nob) /* well. printing ok.. */
342 if (*(string_buf+needed-1) != '\n')
343 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
346 header.ph_len = known_size + needed;
347 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
349 if (libcfs_debug_binary) {
350 memcpy(debug_buf, &header, sizeof(header));
351 tage->used += sizeof(header);
352 debug_buf += sizeof(header);
355 /* indent message according to the nesting level */
356 while (depth-- > 0) {
357 *(debug_buf++) = '.';
361 strcpy(debug_buf, file);
362 tage->used += strlen(file) + 1;
363 debug_buf += strlen(file) + 1;
366 strcpy(debug_buf, fn);
367 tage->used += strlen(fn) + 1;
368 debug_buf += strlen(fn) + 1;
371 __LASSERT(debug_buf == string_buf);
373 tage->used += needed;
374 __LASSERT (tage->used <= CFS_PAGE_SIZE);
377 if ((mask & libcfs_printk) == 0) {
378 /* no console output requested */
380 cfs_trace_put_tcd(tcd);
385 if (libcfs_console_ratelimit &&
386 cdls->cdls_next != 0 && /* not first time ever */
387 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
388 /* skipping a console message */
391 cfs_trace_put_tcd(tcd);
395 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
396 libcfs_console_max_delay
397 + cfs_time_seconds(10))) {
398 /* last timeout was a long time ago */
399 cdls->cdls_delay /= libcfs_console_backoff * 4;
401 cdls->cdls_delay *= libcfs_console_backoff;
403 if (cdls->cdls_delay < libcfs_console_min_delay)
404 cdls->cdls_delay = libcfs_console_min_delay;
405 else if (cdls->cdls_delay > libcfs_console_max_delay)
406 cdls->cdls_delay = libcfs_console_max_delay;
409 /* ensure cdls_next is never zero after it's been seen */
410 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
414 cfs_print_to_console(&header, mask, string_buf, needed, file,
416 cfs_trace_put_tcd(tcd);
418 string_buf = cfs_trace_get_console_buffer();
421 if (format1 != NULL) {
423 needed = vsnprintf(string_buf,
424 CFS_TRACE_CONSOLE_BUFFER_SIZE,
428 if (format2 != NULL) {
429 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
431 va_start(ap, format2);
432 needed += vsnprintf(string_buf+needed, remain, format2, ap);
436 cfs_print_to_console(&header, mask,
437 string_buf, needed, file, fn);
439 cfs_trace_put_console_buffer(string_buf);
442 if (cdls != NULL && cdls->cdls_count != 0) {
443 string_buf = cfs_trace_get_console_buffer();
445 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
446 "Skipped %d previous similar message%s\n",
447 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
449 cfs_print_to_console(&header, mask,
450 string_buf, needed, file, fn);
452 cfs_trace_put_console_buffer(string_buf);
453 cdls->cdls_count = 0;
458 EXPORT_SYMBOL(libcfs_debug_vmsg2);
461 libcfs_assertion_failed(const char *expr, const char *file,
462 const char *func, const int line)
464 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
465 "ASSERTION(%s) failed\n", expr);
466 /* cfs_enter_debugger(); */
467 lbug_with_loc(file, func, line);
469 EXPORT_SYMBOL(libcfs_assertion_failed);
472 cfs_trace_assertion_failed(const char *str,
473 const char *fn, const char *file, int line)
475 struct ptldebug_header hdr;
477 libcfs_panic_in_progress = 1;
478 libcfs_catastrophe = 1;
481 cfs_set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
484 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
486 LIBCFS_PANIC("Lustre debug assertion failure\n");
492 panic_collect_pages(struct page_collection *pc)
494 /* Do the collect_pages job on a single CPU: assumes that all other
495 * CPUs have been stopped during a panic. If this isn't true for some
496 * arch, this will have to be implemented separately in each arch. */
499 struct cfs_trace_cpu_data *tcd;
501 CFS_INIT_LIST_HEAD(&pc->pc_pages);
503 cfs_tcd_for_each(tcd, i, j) {
504 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
505 tcd->tcd_cur_pages = 0;
507 if (pc->pc_want_daemon_pages) {
508 cfs_list_splice_init(&tcd->tcd_daemon_pages,
510 tcd->tcd_cur_daemon_pages = 0;
515 static void collect_pages_on_all_cpus(struct page_collection *pc)
517 struct cfs_trace_cpu_data *tcd;
520 cfs_spin_lock(&pc->pc_lock);
521 cfs_for_each_possible_cpu(cpu) {
522 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
523 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
524 tcd->tcd_cur_pages = 0;
525 if (pc->pc_want_daemon_pages) {
526 cfs_list_splice_init(&tcd->tcd_daemon_pages,
528 tcd->tcd_cur_daemon_pages = 0;
532 cfs_spin_unlock(&pc->pc_lock);
535 static void collect_pages(struct page_collection *pc)
537 CFS_INIT_LIST_HEAD(&pc->pc_pages);
539 if (libcfs_panic_in_progress)
540 panic_collect_pages(pc);
542 collect_pages_on_all_cpus(pc);
545 static void put_pages_back_on_all_cpus(struct page_collection *pc)
547 struct cfs_trace_cpu_data *tcd;
548 cfs_list_t *cur_head;
549 struct cfs_trace_page *tage;
550 struct cfs_trace_page *tmp;
553 cfs_spin_lock(&pc->pc_lock);
554 cfs_for_each_possible_cpu(cpu) {
555 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
556 cur_head = tcd->tcd_pages.next;
558 cfs_list_for_each_entry_safe_typed(tage, tmp,
560 struct cfs_trace_page,
563 __LASSERT_TAGE_INVARIANT(tage);
565 if (tage->cpu != cpu || tage->type != i)
568 cfs_tage_to_tail(tage, cur_head);
569 tcd->tcd_cur_pages++;
573 cfs_spin_unlock(&pc->pc_lock);
576 static void put_pages_back(struct page_collection *pc)
578 if (!libcfs_panic_in_progress)
579 put_pages_back_on_all_cpus(pc);
582 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
583 * we have a good amount of data at all times for dumping during an LBUG, even
584 * if we have been steadily writing (and otherwise discarding) pages via the
586 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
587 struct cfs_trace_cpu_data *tcd)
589 struct cfs_trace_page *tage;
590 struct cfs_trace_page *tmp;
592 cfs_spin_lock(&pc->pc_lock);
593 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
594 struct cfs_trace_page, linkage) {
596 __LASSERT_TAGE_INVARIANT(tage);
598 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
601 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
602 tcd->tcd_cur_daemon_pages++;
604 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
605 struct cfs_trace_page *victim;
607 __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
608 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
610 __LASSERT_TAGE_INVARIANT(victim);
612 cfs_list_del(&victim->linkage);
613 cfs_tage_free(victim);
614 tcd->tcd_cur_daemon_pages--;
617 cfs_spin_unlock(&pc->pc_lock);
620 static void put_pages_on_daemon_list(struct page_collection *pc)
622 struct cfs_trace_cpu_data *tcd;
625 cfs_for_each_possible_cpu(cpu) {
626 cfs_tcd_for_each_type_lock(tcd, i, cpu)
627 put_pages_on_tcd_daemon_list(pc, tcd);
631 void cfs_trace_debug_print(void)
633 struct page_collection pc;
634 struct cfs_trace_page *tage;
635 struct cfs_trace_page *tmp;
637 cfs_spin_lock_init(&pc.pc_lock);
639 pc.pc_want_daemon_pages = 1;
641 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
642 struct cfs_trace_page, linkage) {
646 __LASSERT_TAGE_INVARIANT(tage);
649 p = cfs_page_address(page);
650 while (p < ((char *)cfs_page_address(page) + tage->used)) {
651 struct ptldebug_header *hdr;
656 p += strlen(file) + 1;
659 len = hdr->ph_len - (int)(p - (char *)hdr);
661 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
666 cfs_list_del(&tage->linkage);
671 int cfs_tracefile_dump_all_pages(char *filename)
673 struct page_collection pc;
675 struct cfs_trace_page *tage;
676 struct cfs_trace_page *tmp;
681 cfs_tracefile_write_lock();
683 filp = cfs_filp_open(filename,
684 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
688 "LustreError: can't open %s for dump: rc %d\n",
693 cfs_spin_lock_init(&pc.pc_lock);
694 pc.pc_want_daemon_pages = 1;
696 if (cfs_list_empty(&pc.pc_pages)) {
701 /* ok, for now, just write the pages. in the future we'll be building
702 * iobufs with the pages and calling generic_direct_IO */
704 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
705 struct cfs_trace_page, linkage) {
707 __LASSERT_TAGE_INVARIANT(tage);
709 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
710 tage->used, cfs_filp_poff(filp));
711 if (rc != (int)tage->used) {
712 printk(CFS_KERN_WARNING "wanted to write %u but wrote "
713 "%d\n", tage->used, rc);
715 __LASSERT(cfs_list_empty(&pc.pc_pages));
718 cfs_list_del(&tage->linkage);
722 rc = cfs_filp_fsync(filp);
724 printk(CFS_KERN_ERR "sync returns %d\n", rc);
726 cfs_filp_close(filp);
728 cfs_tracefile_write_unlock();
732 void cfs_trace_flush_pages(void)
734 struct page_collection pc;
735 struct cfs_trace_page *tage;
736 struct cfs_trace_page *tmp;
738 cfs_spin_lock_init(&pc.pc_lock);
740 pc.pc_want_daemon_pages = 1;
742 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
743 struct cfs_trace_page, linkage) {
745 __LASSERT_TAGE_INVARIANT(tage);
747 cfs_list_del(&tage->linkage);
752 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
753 const char *usr_buffer, int usr_buffer_nob)
757 if (usr_buffer_nob > knl_buffer_nob)
760 if (cfs_copy_from_user((void *)knl_buffer,
761 (void *)usr_buffer, usr_buffer_nob))
764 nob = strnlen(knl_buffer, usr_buffer_nob);
765 while (nob-- >= 0) /* strip trailing whitespace */
766 if (!isspace(knl_buffer[nob]))
769 if (nob < 0) /* empty string */
772 if (nob == knl_buffer_nob) /* no space to terminate */
775 knl_buffer[nob + 1] = 0; /* terminate */
779 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
780 const char *knl_buffer, char *append)
782 /* NB if 'append' != NULL, it's a single character to append to the
783 * copied out string - usually "\n", for /proc entries and "" (i.e. a
784 * terminating zero byte) for sysctl entries */
785 int nob = strlen(knl_buffer);
787 if (nob > usr_buffer_nob)
788 nob = usr_buffer_nob;
790 if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
793 if (append != NULL && nob < usr_buffer_nob) {
794 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
802 EXPORT_SYMBOL(cfs_trace_copyout_string);
804 int cfs_trace_allocate_string_buffer(char **str, int nob)
806 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
809 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
816 void cfs_trace_free_string_buffer(char *str, int nob)
821 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
826 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
830 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
831 usr_str, usr_str_nob);
835 #if !defined(__WINNT__)
841 rc = cfs_tracefile_dump_all_pages(str);
843 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
847 int cfs_trace_daemon_command(char *str)
851 cfs_tracefile_write_lock();
853 if (strcmp(str, "stop") == 0) {
854 cfs_tracefile_write_unlock();
855 cfs_trace_stop_thread();
856 cfs_tracefile_write_lock();
857 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
859 } else if (strncmp(str, "size=", 5) == 0) {
860 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
861 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
862 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
864 cfs_tracefile_size <<= 20;
866 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
869 } else if (str[0] != '/') {
873 strcpy(cfs_tracefile, str);
876 "Lustre: debug daemon will attempt to start writing "
877 "to %s (%lukB max)\n", cfs_tracefile,
878 (long)(cfs_tracefile_size >> 10));
880 cfs_trace_start_thread();
883 cfs_tracefile_write_unlock();
887 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
892 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
896 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
897 usr_str, usr_str_nob);
899 rc = cfs_trace_daemon_command(str);
901 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
905 int cfs_trace_set_debug_mb(int mb)
910 int limit = cfs_trace_max_debug_mb();
911 struct cfs_trace_cpu_data *tcd;
913 if (mb < cfs_num_possible_cpus()) {
914 printk(KERN_ERR "Cannot set debug_mb to %d, the value should be >= %d\n",
915 mb, cfs_num_possible_cpus());
920 printk(CFS_KERN_ERR "Lustre: Refusing to set debug buffer size "
921 "to %dMB - limit is %d\n", mb, limit);
925 mb /= cfs_num_possible_cpus();
926 pages = mb << (20 - CFS_PAGE_SHIFT);
928 cfs_tracefile_write_lock();
930 cfs_tcd_for_each(tcd, i, j)
931 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
933 cfs_tracefile_write_unlock();
938 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
943 rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
947 return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
950 int cfs_trace_get_debug_mb(void)
954 struct cfs_trace_cpu_data *tcd;
957 cfs_tracefile_read_lock();
959 cfs_tcd_for_each(tcd, i, j)
960 total_pages += tcd->tcd_max_pages;
962 cfs_tracefile_read_unlock();
964 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
967 static int tracefiled(void *arg)
969 struct page_collection pc;
970 struct tracefiled_ctl *tctl = arg;
971 struct cfs_trace_page *tage;
972 struct cfs_trace_page *tmp;
979 /* we're started late enough that we pick up init's fs context */
980 /* this is so broken in uml? what on earth is going on? */
981 cfs_daemonize("ktracefiled");
983 cfs_spin_lock_init(&pc.pc_lock);
984 cfs_complete(&tctl->tctl_start);
987 cfs_waitlink_t __wait;
989 pc.pc_want_daemon_pages = 0;
991 if (cfs_list_empty(&pc.pc_pages))
995 cfs_tracefile_read_lock();
996 if (cfs_tracefile[0] != 0) {
997 filp = cfs_filp_open(cfs_tracefile,
998 O_CREAT | O_RDWR | O_LARGEFILE,
1001 printk(CFS_KERN_WARNING "couldn't open %s: "
1002 "%d\n", cfs_tracefile, rc);
1004 cfs_tracefile_read_unlock();
1006 put_pages_on_daemon_list(&pc);
1007 __LASSERT(cfs_list_empty(&pc.pc_pages));
1013 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1014 struct cfs_trace_page,
1016 static loff_t f_pos;
1018 __LASSERT_TAGE_INVARIANT(tage);
1020 if (f_pos >= (off_t)cfs_tracefile_size)
1022 else if (f_pos > (off_t)cfs_filp_size(filp))
1023 f_pos = cfs_filp_size(filp);
1025 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1026 tage->used, &f_pos);
1027 if (rc != (int)tage->used) {
1028 printk(CFS_KERN_WARNING "wanted to write %u "
1029 "but wrote %d\n", tage->used, rc);
1030 put_pages_back(&pc);
1031 __LASSERT(cfs_list_empty(&pc.pc_pages));
1036 cfs_filp_close(filp);
1037 put_pages_on_daemon_list(&pc);
1038 if (!cfs_list_empty(&pc.pc_pages)) {
1041 printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1043 printk(CFS_KERN_ERR "total cpus(%d): ",
1044 cfs_num_possible_cpus());
1045 for (i = 0; i < cfs_num_possible_cpus(); i++)
1046 if (cfs_cpu_online(i))
1047 printk(CFS_KERN_ERR "%d(on) ", i);
1049 printk(CFS_KERN_ERR "%d(off) ", i);
1050 printk(CFS_KERN_ERR "\n");
1053 cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1055 printk(CFS_KERN_ERR "page %d belongs to cpu "
1056 "%d\n", ++i, tage->cpu);
1057 printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1060 __LASSERT(cfs_list_empty(&pc.pc_pages));
1062 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1063 if (last_loop == 0) {
1070 cfs_waitlink_init(&__wait);
1071 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1072 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1073 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1074 cfs_time_seconds(1));
1075 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1077 cfs_complete(&tctl->tctl_stop);
1081 int cfs_trace_start_thread(void)
1083 struct tracefiled_ctl *tctl = &trace_tctl;
1086 cfs_mutex_down(&cfs_trace_thread_sem);
1090 cfs_init_completion(&tctl->tctl_start);
1091 cfs_init_completion(&tctl->tctl_stop);
1092 cfs_waitq_init(&tctl->tctl_waitq);
1093 cfs_atomic_set(&tctl->tctl_shutdown, 0);
1095 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1100 cfs_wait_for_completion(&tctl->tctl_start);
1103 cfs_mutex_up(&cfs_trace_thread_sem);
1107 void cfs_trace_stop_thread(void)
1109 struct tracefiled_ctl *tctl = &trace_tctl;
1111 cfs_mutex_down(&cfs_trace_thread_sem);
1112 if (thread_running) {
1113 printk(CFS_KERN_INFO
1114 "Lustre: shutting down debug daemon thread...\n");
1115 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1116 cfs_wait_for_completion(&tctl->tctl_stop);
1119 cfs_mutex_up(&cfs_trace_thread_sem);
1122 int cfs_tracefile_init(int max_pages)
1124 struct cfs_trace_cpu_data *tcd;
1130 rc = cfs_tracefile_init_arch();
1134 cfs_tcd_for_each(tcd, i, j) {
1135 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1136 factor = tcd->tcd_pages_factor;
1137 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1138 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1139 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1140 tcd->tcd_cur_pages = 0;
1141 tcd->tcd_cur_stock_pages = 0;
1142 tcd->tcd_cur_daemon_pages = 0;
1143 tcd->tcd_max_pages = (max_pages * factor) / 100;
1144 LASSERT(tcd->tcd_max_pages > 0);
1145 tcd->tcd_shutting_down = 0;
1151 static void trace_cleanup_on_all_cpus(void)
1153 struct cfs_trace_cpu_data *tcd;
1154 struct cfs_trace_page *tage;
1155 struct cfs_trace_page *tmp;
1158 cfs_for_each_possible_cpu(cpu) {
1159 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1160 tcd->tcd_shutting_down = 1;
1162 cfs_list_for_each_entry_safe_typed(tage, tmp,
1164 struct cfs_trace_page,
1166 __LASSERT_TAGE_INVARIANT(tage);
1168 cfs_list_del(&tage->linkage);
1169 cfs_tage_free(tage);
1172 tcd->tcd_cur_pages = 0;
1177 static void cfs_trace_cleanup(void)
1179 struct page_collection pc;
1181 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1182 cfs_spin_lock_init(&pc.pc_lock);
1184 trace_cleanup_on_all_cpus();
1186 cfs_tracefile_fini_arch();
1189 void cfs_tracefile_exit(void)
1191 cfs_trace_stop_thread();
1192 cfs_trace_cleanup();