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 * 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;
262 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
264 tcd = trace_get_tcd();
265 if (tcd == NULL) /* arch may not log in IRQ context */
268 if (tcd->tcd_shutting_down) {
274 depth = __current_nesting_level();
275 known_size = strlen(file) + 1 + depth;
277 known_size += strlen(fn) + 1;
279 if (libcfs_debug_binary)
280 known_size += sizeof(header);
283 * '2' used because vsnprintf return real size required for output
284 * _without_ terminating NULL.
285 * if needed is to small for this format.
288 tage = trace_get_tage(tcd, needed + known_size + 1);
290 if (needed + known_size > CFS_PAGE_SIZE)
298 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
300 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
302 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
312 needed = vsnprintf(string_buf, max_nob, format1, ap);
318 remain = max_nob - needed;
322 va_start(ap, format2);
323 needed += vsnprintf(string_buf+needed, remain, format2, ap);
327 if (needed < max_nob) /* well. printing ok.. */
331 if (*(string_buf+needed-1) != '\n')
332 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
335 header.ph_len = known_size + needed;
336 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
338 if (libcfs_debug_binary) {
339 memcpy(debug_buf, &header, sizeof(header));
340 tage->used += sizeof(header);
341 debug_buf += sizeof(header);
344 /* indent message according to the nesting level */
345 while (depth-- > 0) {
346 *(debug_buf++) = '.';
350 strcpy(debug_buf, file);
351 tage->used += strlen(file) + 1;
352 debug_buf += strlen(file) + 1;
355 strcpy(debug_buf, fn);
356 tage->used += strlen(fn) + 1;
357 debug_buf += strlen(fn) + 1;
360 __LASSERT(debug_buf == string_buf);
362 tage->used += needed;
363 __LASSERT (tage->used <= CFS_PAGE_SIZE);
366 if ((mask & libcfs_printk) == 0) {
367 /* no console output requested */
374 if (libcfs_console_ratelimit &&
375 cdls->cdls_next != 0 && /* not first time ever */
376 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
377 /* skipping a console message */
384 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
385 libcfs_console_max_delay
386 + cfs_time_seconds(10))) {
387 /* last timeout was a long time ago */
388 cdls->cdls_delay /= libcfs_console_backoff * 4;
390 cdls->cdls_delay *= libcfs_console_backoff;
392 if (cdls->cdls_delay < libcfs_console_min_delay)
393 cdls->cdls_delay = libcfs_console_min_delay;
394 else if (cdls->cdls_delay > libcfs_console_max_delay)
395 cdls->cdls_delay = libcfs_console_max_delay;
398 /* ensure cdls_next is never zero after it's been seen */
399 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
403 print_to_console(&header, mask, string_buf, needed, file, fn);
406 string_buf = trace_get_console_buffer();
409 if (format1 != NULL) {
411 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
414 if (format2 != NULL) {
415 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
417 va_start(ap, format2);
418 needed += vsnprintf(string_buf+needed, remain, format2, ap);
422 print_to_console(&header, mask,
423 string_buf, needed, file, fn);
425 trace_put_console_buffer(string_buf);
428 if (cdls != NULL && cdls->cdls_count != 0) {
429 string_buf = trace_get_console_buffer();
431 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
432 "Skipped %d previous similar message%s\n",
433 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
435 print_to_console(&header, mask,
436 string_buf, needed, file, fn);
438 trace_put_console_buffer(string_buf);
439 cdls->cdls_count = 0;
444 EXPORT_SYMBOL(libcfs_debug_vmsg2);
447 libcfs_assertion_failed(const char *expr, const char *file,
448 const char *func, const int line)
450 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
451 "ASSERTION(%s) failed\n", expr);
454 EXPORT_SYMBOL(libcfs_assertion_failed);
457 trace_assertion_failed(const char *str,
458 const char *fn, const char *file, int line)
460 struct ptldebug_header hdr;
462 libcfs_panic_in_progress = 1;
463 libcfs_catastrophe = 1;
466 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
469 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
471 LIBCFS_PANIC("Lustre debug assertion failure\n");
477 panic_collect_pages(struct page_collection *pc)
479 /* Do the collect_pages job on a single CPU: assumes that all other
480 * CPUs have been stopped during a panic. If this isn't true for some
481 * arch, this will have to be implemented separately in each arch. */
484 struct trace_cpu_data *tcd;
486 CFS_INIT_LIST_HEAD(&pc->pc_pages);
488 tcd_for_each(tcd, i, j) {
489 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
490 tcd->tcd_cur_pages = 0;
492 if (pc->pc_want_daemon_pages) {
493 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
494 tcd->tcd_cur_daemon_pages = 0;
499 static void collect_pages_on_cpu(void *info)
501 struct trace_cpu_data *tcd;
502 struct page_collection *pc = info;
505 spin_lock(&pc->pc_lock);
506 tcd_for_each_type_lock(tcd, i) {
507 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
508 tcd->tcd_cur_pages = 0;
509 if (pc->pc_want_daemon_pages) {
510 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
511 tcd->tcd_cur_daemon_pages = 0;
514 spin_unlock(&pc->pc_lock);
517 static void collect_pages(struct page_collection *pc)
519 CFS_INIT_LIST_HEAD(&pc->pc_pages);
521 if (libcfs_panic_in_progress)
522 panic_collect_pages(pc);
524 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
527 static void put_pages_back_on_cpu(void *info)
529 struct page_collection *pc = info;
530 struct trace_cpu_data *tcd;
531 struct list_head *cur_head;
532 struct trace_page *tage;
533 struct trace_page *tmp;
536 spin_lock(&pc->pc_lock);
537 tcd_for_each_type_lock(tcd, i) {
538 cur_head = tcd->tcd_pages.next;
540 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
542 __LASSERT_TAGE_INVARIANT(tage);
544 if (tage->cpu != smp_processor_id() || tage->type != i)
547 tage_to_tail(tage, cur_head);
548 tcd->tcd_cur_pages++;
551 spin_unlock(&pc->pc_lock);
554 static void put_pages_back(struct page_collection *pc)
556 if (!libcfs_panic_in_progress)
557 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
560 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
561 * we have a good amount of data at all times for dumping during an LBUG, even
562 * if we have been steadily writing (and otherwise discarding) pages via the
564 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
565 struct trace_cpu_data *tcd)
567 struct trace_page *tage;
568 struct trace_page *tmp;
570 spin_lock(&pc->pc_lock);
571 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
573 __LASSERT_TAGE_INVARIANT(tage);
575 if (tage->cpu != smp_processor_id() ||
576 tage->type != tcd->tcd_type)
579 tage_to_tail(tage, &tcd->tcd_daemon_pages);
580 tcd->tcd_cur_daemon_pages++;
582 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
583 struct trace_page *victim;
585 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
586 victim = tage_from_list(tcd->tcd_daemon_pages.next);
588 __LASSERT_TAGE_INVARIANT(victim);
590 list_del(&victim->linkage);
592 tcd->tcd_cur_daemon_pages--;
595 spin_unlock(&pc->pc_lock);
598 static void put_pages_on_daemon_list_on_cpu(void *info)
600 struct trace_cpu_data *tcd;
603 tcd_for_each_type_lock(tcd, i)
604 put_pages_on_tcd_daemon_list(info, tcd);
607 static void put_pages_on_daemon_list(struct page_collection *pc)
609 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
612 void trace_debug_print(void)
614 struct page_collection pc;
615 struct trace_page *tage;
616 struct trace_page *tmp;
618 spin_lock_init(&pc.pc_lock);
620 pc.pc_want_daemon_pages = 1;
622 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
626 __LASSERT_TAGE_INVARIANT(tage);
629 p = cfs_page_address(page);
630 while (p < ((char *)cfs_page_address(page) + tage->used)) {
631 struct ptldebug_header *hdr;
636 p += strlen(file) + 1;
639 len = hdr->ph_len - (p - (char *)hdr);
641 print_to_console(hdr, D_EMERG, p, len, file, fn);
646 list_del(&tage->linkage);
651 int tracefile_dump_all_pages(char *filename)
653 struct page_collection pc;
655 struct trace_page *tage;
656 struct trace_page *tmp;
661 tracefile_write_lock();
663 filp = cfs_filp_open(filename,
664 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
667 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
672 spin_lock_init(&pc.pc_lock);
673 pc.pc_want_daemon_pages = 1;
675 if (list_empty(&pc.pc_pages)) {
680 /* ok, for now, just write the pages. in the future we'll be building
681 * iobufs with the pages and calling generic_direct_IO */
683 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
685 __LASSERT_TAGE_INVARIANT(tage);
687 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
688 tage->used, cfs_filp_poff(filp));
689 if (rc != (int)tage->used) {
690 printk(KERN_WARNING "wanted to write %u but wrote "
691 "%d\n", tage->used, rc);
693 __LASSERT(list_empty(&pc.pc_pages));
696 list_del(&tage->linkage);
700 rc = cfs_filp_fsync(filp);
702 printk(KERN_ERR "sync returns %d\n", rc);
704 cfs_filp_close(filp);
706 tracefile_write_unlock();
710 void trace_flush_pages(void)
712 struct page_collection pc;
713 struct trace_page *tage;
714 struct trace_page *tmp;
716 spin_lock_init(&pc.pc_lock);
718 pc.pc_want_daemon_pages = 1;
720 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
722 __LASSERT_TAGE_INVARIANT(tage);
724 list_del(&tage->linkage);
729 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
730 const char *usr_buffer, int usr_buffer_nob)
734 if (usr_buffer_nob > knl_buffer_nob)
737 if (copy_from_user((void *)knl_buffer,
738 (void *)usr_buffer, usr_buffer_nob))
741 nob = strnlen(knl_buffer, usr_buffer_nob);
742 while (nob-- >= 0) /* strip trailing whitespace */
743 if (!isspace(knl_buffer[nob]))
746 if (nob < 0) /* empty string */
749 if (nob == knl_buffer_nob) /* no space to terminate */
752 knl_buffer[nob + 1] = 0; /* terminate */
756 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
757 const char *knl_buffer, char *append)
759 /* NB if 'append' != NULL, it's a single character to append to the
760 * copied out string - usually "\n", for /proc entries and "" (i.e. a
761 * terminating zero byte) for sysctl entries */
762 int nob = strlen(knl_buffer);
764 if (nob > usr_buffer_nob)
765 nob = usr_buffer_nob;
767 if (copy_to_user(usr_buffer, knl_buffer, nob))
770 if (append != NULL && nob < usr_buffer_nob) {
771 if (copy_to_user(usr_buffer + nob, append, 1))
780 int trace_allocate_string_buffer(char **str, int nob)
782 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
785 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
792 void trace_free_string_buffer(char *str, int nob)
797 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
802 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
806 rc = trace_copyin_string(str, usr_str_nob + 1,
807 usr_str, usr_str_nob);
811 #if !defined(__WINNT__)
817 rc = tracefile_dump_all_pages(str);
819 trace_free_string_buffer(str, usr_str_nob + 1);
823 int trace_daemon_command(char *str)
827 tracefile_write_lock();
829 if (strcmp(str, "stop") == 0) {
831 memset(tracefile, 0, sizeof(tracefile));
833 } else if (strncmp(str, "size=", 5) == 0) {
834 tracefile_size = simple_strtoul(str + 5, NULL, 0);
835 if (tracefile_size < 10 || tracefile_size > 20480)
836 tracefile_size = TRACEFILE_SIZE;
838 tracefile_size <<= 20;
840 } else if (strlen(str) >= sizeof(tracefile)) {
843 } else if (str[0] != '/') {
847 strcpy(tracefile, str);
849 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
850 "to %s (%lukB max)\n", tracefile,
851 (long)(tracefile_size >> 10));
853 trace_start_thread();
856 tracefile_write_unlock();
860 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
865 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
869 rc = trace_copyin_string(str, usr_str_nob + 1,
870 usr_str, usr_str_nob);
872 rc = trace_daemon_command(str);
874 trace_free_string_buffer(str, usr_str_nob + 1);
878 int trace_set_debug_mb(int mb)
883 int limit = trace_max_debug_mb();
884 struct trace_cpu_data *tcd;
886 if (mb < num_possible_cpus())
890 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
891 "%dMB - limit is %d\n", mb, limit);
895 mb /= num_possible_cpus();
896 pages = mb << (20 - CFS_PAGE_SHIFT);
898 tracefile_write_lock();
900 tcd_for_each(tcd, i, j)
901 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
903 tracefile_write_unlock();
908 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
913 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
917 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
920 int trace_get_debug_mb(void)
924 struct trace_cpu_data *tcd;
927 tracefile_read_lock();
929 tcd_for_each(tcd, i, j)
930 total_pages += tcd->tcd_max_pages;
932 tracefile_read_unlock();
934 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
937 static int tracefiled(void *arg)
939 struct page_collection pc;
940 struct tracefiled_ctl *tctl = arg;
941 struct trace_page *tage;
942 struct trace_page *tmp;
943 struct ptldebug_header *hdr;
949 /* we're started late enough that we pick up init's fs context */
950 /* this is so broken in uml? what on earth is going on? */
951 cfs_daemonize("ktracefiled");
953 spin_lock_init(&pc.pc_lock);
954 complete(&tctl->tctl_start);
957 cfs_waitlink_t __wait;
959 cfs_waitlink_init(&__wait);
960 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
961 set_current_state(TASK_INTERRUPTIBLE);
962 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
963 cfs_time_seconds(1));
964 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
966 if (atomic_read(&tctl->tctl_shutdown))
969 pc.pc_want_daemon_pages = 0;
971 if (list_empty(&pc.pc_pages))
975 tracefile_read_lock();
976 if (tracefile[0] != 0) {
977 filp = cfs_filp_open(tracefile,
978 O_CREAT | O_RDWR | O_LARGEFILE,
981 printk(KERN_WARNING "couldn't open %s: %d\n",
984 tracefile_read_unlock();
986 put_pages_on_daemon_list(&pc);
987 __LASSERT(list_empty(&pc.pc_pages));
993 /* mark the first header, so we can sort in chunks */
994 tage = tage_from_list(pc.pc_pages.next);
995 __LASSERT_TAGE_INVARIANT(tage);
997 hdr = cfs_page_address(tage->page);
998 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
1000 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1001 static loff_t f_pos;
1003 __LASSERT_TAGE_INVARIANT(tage);
1005 if (f_pos >= (off_t)tracefile_size)
1007 else if (f_pos > cfs_filp_size(filp))
1008 f_pos = cfs_filp_size(filp);
1010 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1011 tage->used, &f_pos);
1012 if (rc != (int)tage->used) {
1013 printk(KERN_WARNING "wanted to write %u but "
1014 "wrote %d\n", tage->used, rc);
1015 put_pages_back(&pc);
1016 __LASSERT(list_empty(&pc.pc_pages));
1021 cfs_filp_close(filp);
1022 put_pages_on_daemon_list(&pc);
1023 __LASSERT(list_empty(&pc.pc_pages));
1025 complete(&tctl->tctl_stop);
1029 int trace_start_thread(void)
1031 struct tracefiled_ctl *tctl = &trace_tctl;
1034 mutex_down(&trace_thread_sem);
1038 init_completion(&tctl->tctl_start);
1039 init_completion(&tctl->tctl_stop);
1040 cfs_waitq_init(&tctl->tctl_waitq);
1041 atomic_set(&tctl->tctl_shutdown, 0);
1043 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1048 wait_for_completion(&tctl->tctl_start);
1051 mutex_up(&trace_thread_sem);
1055 void trace_stop_thread(void)
1057 struct tracefiled_ctl *tctl = &trace_tctl;
1059 mutex_down(&trace_thread_sem);
1060 if (thread_running) {
1061 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1062 atomic_set(&tctl->tctl_shutdown, 1);
1063 wait_for_completion(&tctl->tctl_stop);
1066 mutex_up(&trace_thread_sem);
1069 int tracefile_init(int max_pages)
1071 struct trace_cpu_data *tcd;
1077 rc = tracefile_init_arch();
1081 tcd_for_each(tcd, i, j) {
1082 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1083 factor = tcd->tcd_pages_factor;
1084 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1085 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1086 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1087 tcd->tcd_cur_pages = 0;
1088 tcd->tcd_cur_stock_pages = 0;
1089 tcd->tcd_cur_daemon_pages = 0;
1090 tcd->tcd_max_pages = (max_pages * factor) / 100;
1091 LASSERT(tcd->tcd_max_pages > 0);
1092 tcd->tcd_shutting_down = 0;
1098 static void trace_cleanup_on_cpu(void *info)
1100 struct trace_cpu_data *tcd;
1101 struct trace_page *tage;
1102 struct trace_page *tmp;
1105 tcd_for_each_type_lock(tcd, i) {
1106 tcd->tcd_shutting_down = 1;
1108 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1109 __LASSERT_TAGE_INVARIANT(tage);
1111 list_del(&tage->linkage);
1114 tcd->tcd_cur_pages = 0;
1118 static void trace_cleanup(void)
1120 struct page_collection pc;
1122 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1123 spin_lock_init(&pc.pc_lock);
1125 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1127 tracefile_fini_arch();
1130 void tracefile_exit(void)
1132 trace_stop_thread();