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) 2008, 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 * 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;
74 /* My caller is trying to free memory */
75 if (!cfs_in_interrupt() && cfs_memory_pressure_get())
79 * Don't spam console with allocation failures: they will be reported
80 * by upper layer anyway.
82 gfp |= CFS_ALLOC_NOWARN;
83 page = cfs_alloc_page(gfp);
87 tage = cfs_alloc(sizeof(*tage), gfp);
94 cfs_atomic_inc(&cfs_tage_allocated);
98 static void cfs_tage_free(struct cfs_trace_page *tage)
100 __LASSERT(tage != NULL);
101 __LASSERT(tage->page != NULL);
103 cfs_free_page(tage->page);
105 cfs_atomic_dec(&cfs_tage_allocated);
108 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
111 __LASSERT(tage != NULL);
112 __LASSERT(queue != NULL);
114 cfs_list_move_tail(&tage->linkage, queue);
117 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, int gfp,
123 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
124 * from here: this will lead to infinite recursion.
127 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
128 struct cfs_trace_page *tage;
130 tage = cfs_tage_alloc(gfp);
133 cfs_list_add_tail(&tage->linkage, stock);
138 /* return a page that has 'len' bytes left at the end */
139 static struct cfs_trace_page *
140 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
142 struct cfs_trace_page *tage;
144 if (tcd->tcd_cur_pages > 0) {
145 __LASSERT(!cfs_list_empty(&tcd->tcd_pages));
146 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
147 if (tage->used + len <= CFS_PAGE_SIZE)
151 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
152 if (tcd->tcd_cur_stock_pages > 0) {
153 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
154 -- tcd->tcd_cur_stock_pages;
155 cfs_list_del_init(&tage->linkage);
157 tage = cfs_tage_alloc(CFS_ALLOC_ATOMIC);
159 if (printk_ratelimit())
160 printk(CFS_KERN_WARNING
161 "cannot allocate a tage (%ld)\n",
168 tage->cpu = cfs_smp_processor_id();
169 tage->type = tcd->tcd_type;
170 cfs_list_add_tail(&tage->linkage, &tcd->tcd_pages);
171 tcd->tcd_cur_pages++;
173 if (tcd->tcd_cur_pages > 8 && thread_running) {
174 struct tracefiled_ctl *tctl = &trace_tctl;
176 * wake up tracefiled to process some pages.
178 cfs_waitq_signal(&tctl->tctl_waitq);
185 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
187 int pgcount = tcd->tcd_cur_pages / 10;
188 struct page_collection pc;
189 struct cfs_trace_page *tage;
190 struct cfs_trace_page *tmp;
193 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
194 * from here: this will lead to infinite recursion.
197 if (printk_ratelimit())
198 printk(CFS_KERN_WARNING "debug daemon buffer overflowed; "
199 "discarding 10%% of pages (%d of %ld)\n",
200 pgcount + 1, tcd->tcd_cur_pages);
202 CFS_INIT_LIST_HEAD(&pc.pc_pages);
203 cfs_spin_lock_init(&pc.pc_lock);
205 cfs_list_for_each_entry_safe_typed(tage, tmp, &tcd->tcd_pages,
206 struct cfs_trace_page, linkage) {
210 cfs_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 > CFS_PAGE_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_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
247 const char *file, const char *fn, const int line,
248 const char *format1, va_list args,
249 const char *format2, ...)
251 struct cfs_trace_cpu_data *tcd = NULL;
252 struct ptldebug_header header = {0};
253 struct cfs_trace_page *tage;
254 /* string_buf is used only if tcd != NULL, and is always set then */
255 char *string_buf = NULL;
258 int needed = 85; /* average message length */
265 if (strchr(file, '/'))
266 file = strrchr(file, '/') + 1;
268 tcd = cfs_trace_get_tcd();
270 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
271 * pins us to a particular CPU. This avoids an smp_processor_id()
272 * warning on Linux when debugging is enabled. */
273 cfs_set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
275 if (tcd == NULL) /* arch may not log in IRQ context */
278 if (tcd->tcd_cur_pages == 0)
279 header.ph_flags |= PH_FLAG_FIRST_RECORD;
281 if (tcd->tcd_shutting_down) {
282 cfs_trace_put_tcd(tcd);
287 depth = __current_nesting_level();
288 known_size = strlen(file) + 1 + depth;
290 known_size += strlen(fn) + 1;
292 if (libcfs_debug_binary)
293 known_size += sizeof(header);
296 * '2' used because vsnprintf return real size required for output
297 * _without_ terminating NULL.
298 * if needed is to small for this format.
300 for (i = 0; i < 2; i++) {
301 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
303 if (needed + known_size > CFS_PAGE_SIZE)
306 cfs_trace_put_tcd(tcd);
311 string_buf = (char *)cfs_page_address(tage->page) +
312 tage->used + known_size;
314 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
316 printk(CFS_KERN_EMERG "negative max_nob: %d\n",
319 cfs_trace_put_tcd(tcd);
327 needed = vsnprintf(string_buf, max_nob, format1, ap);
332 remain = max_nob - needed;
336 va_start(ap, format2);
337 needed += vsnprintf(string_buf + needed, remain,
342 if (needed < max_nob) /* well. printing ok.. */
346 if (*(string_buf+needed-1) != '\n')
347 printk(CFS_KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
350 header.ph_len = known_size + needed;
351 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
353 if (libcfs_debug_binary) {
354 memcpy(debug_buf, &header, sizeof(header));
355 tage->used += sizeof(header);
356 debug_buf += sizeof(header);
359 /* indent message according to the nesting level */
360 while (depth-- > 0) {
361 *(debug_buf++) = '.';
365 strcpy(debug_buf, file);
366 tage->used += strlen(file) + 1;
367 debug_buf += strlen(file) + 1;
370 strcpy(debug_buf, fn);
371 tage->used += strlen(fn) + 1;
372 debug_buf += strlen(fn) + 1;
375 __LASSERT(debug_buf == string_buf);
377 tage->used += needed;
378 __LASSERT (tage->used <= CFS_PAGE_SIZE);
381 if ((mask & libcfs_printk) == 0) {
382 /* no console output requested */
384 cfs_trace_put_tcd(tcd);
389 if (libcfs_console_ratelimit &&
390 cdls->cdls_next != 0 && /* not first time ever */
391 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
392 /* skipping a console message */
395 cfs_trace_put_tcd(tcd);
399 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
400 libcfs_console_max_delay
401 + cfs_time_seconds(10))) {
402 /* last timeout was a long time ago */
403 cdls->cdls_delay /= libcfs_console_backoff * 4;
405 cdls->cdls_delay *= libcfs_console_backoff;
407 if (cdls->cdls_delay < libcfs_console_min_delay)
408 cdls->cdls_delay = libcfs_console_min_delay;
409 else if (cdls->cdls_delay > libcfs_console_max_delay)
410 cdls->cdls_delay = libcfs_console_max_delay;
413 /* ensure cdls_next is never zero after it's been seen */
414 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
418 cfs_print_to_console(&header, mask, string_buf, needed, file,
420 cfs_trace_put_tcd(tcd);
422 string_buf = cfs_trace_get_console_buffer();
425 if (format1 != NULL) {
427 needed = vsnprintf(string_buf,
428 CFS_TRACE_CONSOLE_BUFFER_SIZE,
432 if (format2 != NULL) {
433 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
435 va_start(ap, format2);
436 needed += vsnprintf(string_buf+needed, remain, format2, ap);
440 cfs_print_to_console(&header, mask,
441 string_buf, needed, file, fn);
443 cfs_trace_put_console_buffer(string_buf);
446 if (cdls != NULL && cdls->cdls_count != 0) {
447 string_buf = cfs_trace_get_console_buffer();
449 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
450 "Skipped %d previous similar message%s\n",
451 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
453 cfs_print_to_console(&header, mask,
454 string_buf, needed, file, fn);
456 cfs_trace_put_console_buffer(string_buf);
457 cdls->cdls_count = 0;
462 EXPORT_SYMBOL(libcfs_debug_vmsg2);
465 libcfs_assertion_failed(const char *expr, const char *file,
466 const char *func, const int line)
468 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
469 "ASSERTION(%s) failed\n", expr);
470 /* cfs_enter_debugger(); */
471 lbug_with_loc(file, func, line);
473 EXPORT_SYMBOL(libcfs_assertion_failed);
476 cfs_trace_assertion_failed(const char *str,
477 const char *fn, const char *file, int line)
479 struct ptldebug_header hdr;
481 libcfs_panic_in_progress = 1;
482 libcfs_catastrophe = 1;
485 cfs_set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
488 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
490 LIBCFS_PANIC("Lustre debug assertion failure\n");
496 panic_collect_pages(struct page_collection *pc)
498 /* Do the collect_pages job on a single CPU: assumes that all other
499 * CPUs have been stopped during a panic. If this isn't true for some
500 * arch, this will have to be implemented separately in each arch. */
503 struct cfs_trace_cpu_data *tcd;
505 CFS_INIT_LIST_HEAD(&pc->pc_pages);
507 cfs_tcd_for_each(tcd, i, j) {
508 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
509 tcd->tcd_cur_pages = 0;
511 if (pc->pc_want_daemon_pages) {
512 cfs_list_splice_init(&tcd->tcd_daemon_pages,
514 tcd->tcd_cur_daemon_pages = 0;
519 static void collect_pages_on_all_cpus(struct page_collection *pc)
521 struct cfs_trace_cpu_data *tcd;
524 cfs_spin_lock(&pc->pc_lock);
525 cfs_for_each_possible_cpu(cpu) {
526 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
527 cfs_list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
528 tcd->tcd_cur_pages = 0;
529 if (pc->pc_want_daemon_pages) {
530 cfs_list_splice_init(&tcd->tcd_daemon_pages,
532 tcd->tcd_cur_daemon_pages = 0;
536 cfs_spin_unlock(&pc->pc_lock);
539 static void collect_pages(struct page_collection *pc)
541 CFS_INIT_LIST_HEAD(&pc->pc_pages);
543 if (libcfs_panic_in_progress)
544 panic_collect_pages(pc);
546 collect_pages_on_all_cpus(pc);
549 static void put_pages_back_on_all_cpus(struct page_collection *pc)
551 struct cfs_trace_cpu_data *tcd;
552 cfs_list_t *cur_head;
553 struct cfs_trace_page *tage;
554 struct cfs_trace_page *tmp;
557 cfs_spin_lock(&pc->pc_lock);
558 cfs_for_each_possible_cpu(cpu) {
559 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
560 cur_head = tcd->tcd_pages.next;
562 cfs_list_for_each_entry_safe_typed(tage, tmp,
564 struct cfs_trace_page,
567 __LASSERT_TAGE_INVARIANT(tage);
569 if (tage->cpu != cpu || tage->type != i)
572 cfs_tage_to_tail(tage, cur_head);
573 tcd->tcd_cur_pages++;
577 cfs_spin_unlock(&pc->pc_lock);
580 static void put_pages_back(struct page_collection *pc)
582 if (!libcfs_panic_in_progress)
583 put_pages_back_on_all_cpus(pc);
586 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
587 * we have a good amount of data at all times for dumping during an LBUG, even
588 * if we have been steadily writing (and otherwise discarding) pages via the
590 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
591 struct cfs_trace_cpu_data *tcd)
593 struct cfs_trace_page *tage;
594 struct cfs_trace_page *tmp;
596 cfs_spin_lock(&pc->pc_lock);
597 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc->pc_pages,
598 struct cfs_trace_page, linkage) {
600 __LASSERT_TAGE_INVARIANT(tage);
602 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
605 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
606 tcd->tcd_cur_daemon_pages++;
608 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
609 struct cfs_trace_page *victim;
611 __LASSERT(!cfs_list_empty(&tcd->tcd_daemon_pages));
612 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
614 __LASSERT_TAGE_INVARIANT(victim);
616 cfs_list_del(&victim->linkage);
617 cfs_tage_free(victim);
618 tcd->tcd_cur_daemon_pages--;
621 cfs_spin_unlock(&pc->pc_lock);
624 static void put_pages_on_daemon_list(struct page_collection *pc)
626 struct cfs_trace_cpu_data *tcd;
629 cfs_for_each_possible_cpu(cpu) {
630 cfs_tcd_for_each_type_lock(tcd, i, cpu)
631 put_pages_on_tcd_daemon_list(pc, tcd);
635 void cfs_trace_debug_print(void)
637 struct page_collection pc;
638 struct cfs_trace_page *tage;
639 struct cfs_trace_page *tmp;
641 cfs_spin_lock_init(&pc.pc_lock);
643 pc.pc_want_daemon_pages = 1;
645 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
646 struct cfs_trace_page, linkage) {
650 __LASSERT_TAGE_INVARIANT(tage);
653 p = cfs_page_address(page);
654 while (p < ((char *)cfs_page_address(page) + tage->used)) {
655 struct ptldebug_header *hdr;
660 p += strlen(file) + 1;
663 len = hdr->ph_len - (int)(p - (char *)hdr);
665 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
670 cfs_list_del(&tage->linkage);
675 int cfs_tracefile_dump_all_pages(char *filename)
677 struct page_collection pc;
679 struct cfs_trace_page *tage;
680 struct cfs_trace_page *tmp;
685 cfs_tracefile_write_lock();
687 filp = cfs_filp_open(filename,
688 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
692 "LustreError: can't open %s for dump: rc %d\n",
697 cfs_spin_lock_init(&pc.pc_lock);
698 pc.pc_want_daemon_pages = 1;
700 if (cfs_list_empty(&pc.pc_pages)) {
705 /* ok, for now, just write the pages. in the future we'll be building
706 * iobufs with the pages and calling generic_direct_IO */
708 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
709 struct cfs_trace_page, linkage) {
711 __LASSERT_TAGE_INVARIANT(tage);
713 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
714 tage->used, cfs_filp_poff(filp));
715 if (rc != (int)tage->used) {
716 printk(CFS_KERN_WARNING "wanted to write %u but wrote "
717 "%d\n", tage->used, rc);
719 __LASSERT(cfs_list_empty(&pc.pc_pages));
722 cfs_list_del(&tage->linkage);
726 rc = cfs_filp_fsync(filp);
728 printk(CFS_KERN_ERR "sync returns %d\n", rc);
730 cfs_filp_close(filp);
732 cfs_tracefile_write_unlock();
736 void cfs_trace_flush_pages(void)
738 struct page_collection pc;
739 struct cfs_trace_page *tage;
740 struct cfs_trace_page *tmp;
742 cfs_spin_lock_init(&pc.pc_lock);
744 pc.pc_want_daemon_pages = 1;
746 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
747 struct cfs_trace_page, linkage) {
749 __LASSERT_TAGE_INVARIANT(tage);
751 cfs_list_del(&tage->linkage);
756 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
757 const char *usr_buffer, int usr_buffer_nob)
761 if (usr_buffer_nob > knl_buffer_nob)
764 if (cfs_copy_from_user((void *)knl_buffer,
765 (void *)usr_buffer, usr_buffer_nob))
768 nob = strnlen(knl_buffer, usr_buffer_nob);
769 while (nob-- >= 0) /* strip trailing whitespace */
770 if (!isspace(knl_buffer[nob]))
773 if (nob < 0) /* empty string */
776 if (nob == knl_buffer_nob) /* no space to terminate */
779 knl_buffer[nob + 1] = 0; /* terminate */
783 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
784 const char *knl_buffer, char *append)
786 /* NB if 'append' != NULL, it's a single character to append to the
787 * copied out string - usually "\n", for /proc entries and "" (i.e. a
788 * terminating zero byte) for sysctl entries */
789 int nob = strlen(knl_buffer);
791 if (nob > usr_buffer_nob)
792 nob = usr_buffer_nob;
794 if (cfs_copy_to_user(usr_buffer, knl_buffer, nob))
797 if (append != NULL && nob < usr_buffer_nob) {
798 if (cfs_copy_to_user(usr_buffer + nob, append, 1))
806 EXPORT_SYMBOL(cfs_trace_copyout_string);
808 int cfs_trace_allocate_string_buffer(char **str, int nob)
810 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
813 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
820 void cfs_trace_free_string_buffer(char *str, int nob)
825 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
830 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
834 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
835 usr_str, usr_str_nob);
839 #if !defined(__WINNT__)
845 rc = cfs_tracefile_dump_all_pages(str);
847 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
851 int cfs_trace_daemon_command(char *str)
855 cfs_tracefile_write_lock();
857 if (strcmp(str, "stop") == 0) {
858 cfs_tracefile_write_unlock();
859 cfs_trace_stop_thread();
860 cfs_tracefile_write_lock();
861 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
863 } else if (strncmp(str, "size=", 5) == 0) {
864 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
865 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
866 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
868 cfs_tracefile_size <<= 20;
870 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
873 } else if (str[0] != '/') {
877 strcpy(cfs_tracefile, str);
880 "Lustre: debug daemon will attempt to start writing "
881 "to %s (%lukB max)\n", cfs_tracefile,
882 (long)(cfs_tracefile_size >> 10));
884 cfs_trace_start_thread();
887 cfs_tracefile_write_unlock();
891 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
896 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
900 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
901 usr_str, usr_str_nob);
903 rc = cfs_trace_daemon_command(str);
905 cfs_trace_free_string_buffer(str, usr_str_nob + 1);
909 int cfs_trace_set_debug_mb(int mb)
914 int limit = cfs_trace_max_debug_mb();
915 struct cfs_trace_cpu_data *tcd;
917 if (mb < cfs_num_possible_cpus()) {
918 printk(KERN_ERR "Cannot set debug_mb to %d, the value should be >= %d\n",
919 mb, cfs_num_possible_cpus());
924 printk(CFS_KERN_ERR "Lustre: Refusing to set debug buffer size "
925 "to %dMB - limit is %d\n", mb, limit);
929 mb /= cfs_num_possible_cpus();
930 pages = mb << (20 - CFS_PAGE_SHIFT);
932 cfs_tracefile_write_lock();
934 cfs_tcd_for_each(tcd, i, j)
935 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
937 cfs_tracefile_write_unlock();
942 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
947 rc = cfs_trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
951 return cfs_trace_set_debug_mb(simple_strtoul(str, NULL, 0));
954 int cfs_trace_get_debug_mb(void)
958 struct cfs_trace_cpu_data *tcd;
961 cfs_tracefile_read_lock();
963 cfs_tcd_for_each(tcd, i, j)
964 total_pages += tcd->tcd_max_pages;
966 cfs_tracefile_read_unlock();
968 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
971 static int tracefiled(void *arg)
973 struct page_collection pc;
974 struct tracefiled_ctl *tctl = arg;
975 struct cfs_trace_page *tage;
976 struct cfs_trace_page *tmp;
983 /* we're started late enough that we pick up init's fs context */
984 /* this is so broken in uml? what on earth is going on? */
985 cfs_daemonize("ktracefiled");
987 cfs_spin_lock_init(&pc.pc_lock);
988 cfs_complete(&tctl->tctl_start);
991 cfs_waitlink_t __wait;
993 pc.pc_want_daemon_pages = 0;
995 if (cfs_list_empty(&pc.pc_pages))
999 cfs_tracefile_read_lock();
1000 if (cfs_tracefile[0] != 0) {
1001 filp = cfs_filp_open(cfs_tracefile,
1002 O_CREAT | O_RDWR | O_LARGEFILE,
1005 printk(CFS_KERN_WARNING "couldn't open %s: "
1006 "%d\n", cfs_tracefile, rc);
1008 cfs_tracefile_read_unlock();
1010 put_pages_on_daemon_list(&pc);
1011 __LASSERT(cfs_list_empty(&pc.pc_pages));
1017 cfs_list_for_each_entry_safe_typed(tage, tmp, &pc.pc_pages,
1018 struct cfs_trace_page,
1020 static loff_t f_pos;
1022 __LASSERT_TAGE_INVARIANT(tage);
1024 if (f_pos >= (off_t)cfs_tracefile_size)
1026 else if (f_pos > (off_t)cfs_filp_size(filp))
1027 f_pos = cfs_filp_size(filp);
1029 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1030 tage->used, &f_pos);
1031 if (rc != (int)tage->used) {
1032 printk(CFS_KERN_WARNING "wanted to write %u "
1033 "but wrote %d\n", tage->used, rc);
1034 put_pages_back(&pc);
1035 __LASSERT(cfs_list_empty(&pc.pc_pages));
1040 cfs_filp_close(filp);
1041 put_pages_on_daemon_list(&pc);
1042 if (!cfs_list_empty(&pc.pc_pages)) {
1045 printk(CFS_KERN_ALERT "Lustre: trace pages aren't "
1047 printk(CFS_KERN_ERR "total cpus(%d): ",
1048 cfs_num_possible_cpus());
1049 for (i = 0; i < cfs_num_possible_cpus(); i++)
1050 if (cfs_cpu_online(i))
1051 printk(CFS_KERN_ERR "%d(on) ", i);
1053 printk(CFS_KERN_ERR "%d(off) ", i);
1054 printk(CFS_KERN_ERR "\n");
1057 cfs_list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1059 printk(CFS_KERN_ERR "page %d belongs to cpu "
1060 "%d\n", ++i, tage->cpu);
1061 printk(CFS_KERN_ERR "There are %d pages unwritten\n",
1064 __LASSERT(cfs_list_empty(&pc.pc_pages));
1066 if (cfs_atomic_read(&tctl->tctl_shutdown)) {
1067 if (last_loop == 0) {
1074 cfs_waitlink_init(&__wait);
1075 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
1076 cfs_set_current_state(CFS_TASK_INTERRUPTIBLE);
1077 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
1078 cfs_time_seconds(1));
1079 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
1081 cfs_complete(&tctl->tctl_stop);
1085 int cfs_trace_start_thread(void)
1087 struct tracefiled_ctl *tctl = &trace_tctl;
1090 cfs_mutex_down(&cfs_trace_thread_sem);
1094 cfs_init_completion(&tctl->tctl_start);
1095 cfs_init_completion(&tctl->tctl_stop);
1096 cfs_waitq_init(&tctl->tctl_waitq);
1097 cfs_atomic_set(&tctl->tctl_shutdown, 0);
1099 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1104 cfs_wait_for_completion(&tctl->tctl_start);
1107 cfs_mutex_up(&cfs_trace_thread_sem);
1111 void cfs_trace_stop_thread(void)
1113 struct tracefiled_ctl *tctl = &trace_tctl;
1115 cfs_mutex_down(&cfs_trace_thread_sem);
1116 if (thread_running) {
1117 printk(CFS_KERN_INFO
1118 "Lustre: shutting down debug daemon thread...\n");
1119 cfs_atomic_set(&tctl->tctl_shutdown, 1);
1120 cfs_wait_for_completion(&tctl->tctl_stop);
1123 cfs_mutex_up(&cfs_trace_thread_sem);
1126 int cfs_tracefile_init(int max_pages)
1128 struct cfs_trace_cpu_data *tcd;
1134 rc = cfs_tracefile_init_arch();
1138 cfs_tcd_for_each(tcd, i, j) {
1139 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1140 factor = tcd->tcd_pages_factor;
1141 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1142 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1143 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1144 tcd->tcd_cur_pages = 0;
1145 tcd->tcd_cur_stock_pages = 0;
1146 tcd->tcd_cur_daemon_pages = 0;
1147 tcd->tcd_max_pages = (max_pages * factor) / 100;
1148 LASSERT(tcd->tcd_max_pages > 0);
1149 tcd->tcd_shutting_down = 0;
1155 static void trace_cleanup_on_all_cpus(void)
1157 struct cfs_trace_cpu_data *tcd;
1158 struct cfs_trace_page *tage;
1159 struct cfs_trace_page *tmp;
1162 cfs_for_each_possible_cpu(cpu) {
1163 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1164 tcd->tcd_shutting_down = 1;
1166 cfs_list_for_each_entry_safe_typed(tage, tmp,
1168 struct cfs_trace_page,
1170 __LASSERT_TAGE_INVARIANT(tage);
1172 cfs_list_del(&tage->linkage);
1173 cfs_tage_free(tage);
1176 tcd->tcd_cur_pages = 0;
1181 static void cfs_trace_cleanup(void)
1183 struct page_collection pc;
1185 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1186 cfs_spin_lock_init(&pc.pc_lock);
1188 trace_cleanup_on_all_cpus();
1190 cfs_tracefile_fini_arch();
1193 void cfs_tracefile_exit(void)
1195 cfs_trace_stop_thread();
1196 cfs_trace_cleanup();