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 trace_data_union (*trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
52 char tracefile[TRACEFILE_NAME_SIZE];
53 long long tracefile_size = TRACEFILE_SIZE;
54 static struct tracefiled_ctl trace_tctl;
55 struct semaphore trace_thread_sem;
56 static int thread_running = 0;
58 atomic_t tage_allocated = ATOMIC_INIT(0);
60 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
61 struct trace_cpu_data *tcd);
63 static inline struct trace_page *tage_from_list(struct list_head *list)
65 return list_entry(list, struct trace_page, linkage);
68 static struct trace_page *tage_alloc(int gfp)
71 struct trace_page *tage;
74 * Don't spam console with allocation failures: they will be reported
75 * by upper layer anyway.
77 gfp |= CFS_ALLOC_NOWARN;
78 page = cfs_alloc_page(gfp);
82 tage = cfs_alloc(sizeof(*tage), gfp);
89 atomic_inc(&tage_allocated);
93 static void tage_free(struct trace_page *tage)
95 __LASSERT(tage != NULL);
96 __LASSERT(tage->page != NULL);
98 cfs_free_page(tage->page);
100 atomic_dec(&tage_allocated);
103 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
105 __LASSERT(tage != NULL);
106 __LASSERT(queue != NULL);
108 list_move_tail(&tage->linkage, queue);
111 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
112 struct list_head *stock)
117 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
118 * from here: this will lead to infinite recursion.
121 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
122 struct trace_page *tage;
124 tage = tage_alloc(gfp);
127 list_add_tail(&tage->linkage, stock);
132 /* return a page that has 'len' bytes left at the end */
133 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
136 struct trace_page *tage;
138 if (tcd->tcd_cur_pages > 0) {
139 __LASSERT(!list_empty(&tcd->tcd_pages));
140 tage = tage_from_list(tcd->tcd_pages.prev);
141 if (tage->used + len <= CFS_PAGE_SIZE)
145 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
146 if (tcd->tcd_cur_stock_pages > 0) {
147 tage = tage_from_list(tcd->tcd_stock_pages.prev);
148 -- tcd->tcd_cur_stock_pages;
149 list_del_init(&tage->linkage);
151 tage = tage_alloc(CFS_ALLOC_ATOMIC);
154 "failure to allocate a tage (%ld)\n",
161 tage->cpu = smp_processor_id();
162 tage->type = tcd->tcd_type;
163 list_add_tail(&tage->linkage, &tcd->tcd_pages);
164 tcd->tcd_cur_pages++;
166 if (tcd->tcd_cur_pages > 8 && thread_running) {
167 struct tracefiled_ctl *tctl = &trace_tctl;
169 * wake up tracefiled to process some pages.
171 cfs_waitq_signal(&tctl->tctl_waitq);
178 static void tcd_shrink(struct trace_cpu_data *tcd)
180 int pgcount = tcd->tcd_cur_pages / 10;
181 struct page_collection pc;
182 struct trace_page *tage;
183 struct trace_page *tmp;
186 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
187 * from here: this will lead to infinite recursion.
190 printk(KERN_WARNING "debug daemon buffer overflowed; discarding"
191 " 10%% of pages (%d of %ld)\n", pgcount + 1, tcd->tcd_cur_pages);
193 CFS_INIT_LIST_HEAD(&pc.pc_pages);
194 spin_lock_init(&pc.pc_lock);
196 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
200 list_move_tail(&tage->linkage, &pc.pc_pages);
201 tcd->tcd_cur_pages--;
203 put_pages_on_tcd_daemon_list(&pc, tcd);
206 /* return a page that has 'len' bytes left at the end */
207 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
210 struct trace_page *tage;
213 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
214 * from here: this will lead to infinite recursion.
217 if (len > CFS_PAGE_SIZE) {
219 "cowardly refusing to write %lu bytes in a page\n", len);
223 tage = trace_get_tage_try(tcd, len);
228 if (tcd->tcd_cur_pages > 0) {
229 tage = tage_from_list(tcd->tcd_pages.next);
231 tage_to_tail(tage, &tcd->tcd_pages);
236 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
237 const char *file, const char *fn, const int line,
238 const char *format1, va_list args,
239 const char *format2, ...)
241 struct trace_cpu_data *tcd = NULL;
242 struct ptldebug_header header;
243 struct trace_page *tage;
244 /* string_buf is used only if tcd != NULL, and is always set then */
245 char *string_buf = NULL;
248 int needed = 85; /* average message length */
255 if (strchr(file, '/'))
256 file = strrchr(file, '/') + 1;
259 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
261 tcd = trace_get_tcd();
262 if (tcd == NULL) /* arch may not log in IRQ context */
265 if (tcd->tcd_shutting_down) {
271 depth = __current_nesting_level();
272 known_size = strlen(file) + 1 + depth;
274 known_size += strlen(fn) + 1;
276 if (libcfs_debug_binary)
277 known_size += sizeof(header);
280 * '2' used because vsnprintf return real size required for output
281 * _without_ terminating NULL.
282 * if needed is to small for this format.
285 tage = trace_get_tage(tcd, needed + known_size + 1);
287 if (needed + known_size > CFS_PAGE_SIZE)
295 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
297 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
299 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
309 needed = vsnprintf(string_buf, max_nob, format1, ap);
315 remain = max_nob - needed;
319 va_start(ap, format2);
320 needed += vsnprintf(string_buf+needed, remain, format2, ap);
324 if (needed < max_nob) /* well. printing ok.. */
328 if (*(string_buf+needed-1) != '\n')
329 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
332 header.ph_len = known_size + needed;
333 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
335 if (libcfs_debug_binary) {
336 memcpy(debug_buf, &header, sizeof(header));
337 tage->used += sizeof(header);
338 debug_buf += sizeof(header);
341 /* indent message according to the nesting level */
342 while (depth-- > 0) {
343 *(debug_buf++) = '.';
347 strcpy(debug_buf, file);
348 tage->used += strlen(file) + 1;
349 debug_buf += strlen(file) + 1;
352 strcpy(debug_buf, fn);
353 tage->used += strlen(fn) + 1;
354 debug_buf += strlen(fn) + 1;
357 __LASSERT(debug_buf == string_buf);
359 tage->used += needed;
360 __LASSERT (tage->used <= CFS_PAGE_SIZE);
363 if ((mask & libcfs_printk) == 0) {
364 /* no console output requested */
371 if (libcfs_console_ratelimit &&
372 cdls->cdls_next != 0 && /* not first time ever */
373 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
374 /* skipping a console message */
381 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
382 libcfs_console_max_delay
383 + cfs_time_seconds(10))) {
384 /* last timeout was a long time ago */
385 cdls->cdls_delay /= libcfs_console_backoff * 4;
387 cdls->cdls_delay *= libcfs_console_backoff;
389 if (cdls->cdls_delay < libcfs_console_min_delay)
390 cdls->cdls_delay = libcfs_console_min_delay;
391 else if (cdls->cdls_delay > libcfs_console_max_delay)
392 cdls->cdls_delay = libcfs_console_max_delay;
395 /* ensure cdls_next is never zero after it's been seen */
396 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
400 print_to_console(&header, mask, string_buf, needed, file, fn);
403 string_buf = trace_get_console_buffer();
406 if (format1 != NULL) {
408 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
411 if (format2 != NULL) {
412 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
414 va_start(ap, format2);
415 needed += vsnprintf(string_buf+needed, remain, format2, ap);
419 print_to_console(&header, mask,
420 string_buf, needed, file, fn);
422 trace_put_console_buffer(string_buf);
425 if (cdls != NULL && cdls->cdls_count != 0) {
426 string_buf = trace_get_console_buffer();
428 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
429 "Skipped %d previous similar message%s\n",
430 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
432 print_to_console(&header, mask,
433 string_buf, needed, file, fn);
435 trace_put_console_buffer(string_buf);
436 cdls->cdls_count = 0;
441 EXPORT_SYMBOL(libcfs_debug_vmsg2);
444 libcfs_assertion_failed(const char *expr, const char *file,
445 const char *func, const int line)
447 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
448 "ASSERTION(%s) failed\n", expr);
451 EXPORT_SYMBOL(libcfs_assertion_failed);
454 trace_assertion_failed(const char *str,
455 const char *fn, const char *file, int line)
457 struct ptldebug_header hdr;
459 libcfs_panic_in_progress = 1;
460 libcfs_catastrophe = 1;
463 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
466 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
468 LIBCFS_PANIC("Lustre debug assertion failure\n");
474 panic_collect_pages(struct page_collection *pc)
476 /* Do the collect_pages job on a single CPU: assumes that all other
477 * CPUs have been stopped during a panic. If this isn't true for some
478 * arch, this will have to be implemented separately in each arch. */
481 struct trace_cpu_data *tcd;
483 CFS_INIT_LIST_HEAD(&pc->pc_pages);
485 tcd_for_each(tcd, i, j) {
486 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
487 tcd->tcd_cur_pages = 0;
489 if (pc->pc_want_daemon_pages) {
490 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
491 tcd->tcd_cur_daemon_pages = 0;
496 static void collect_pages_on_cpu(void *info)
498 struct trace_cpu_data *tcd;
499 struct page_collection *pc = info;
502 spin_lock(&pc->pc_lock);
503 tcd_for_each_type_lock(tcd, i) {
504 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
505 tcd->tcd_cur_pages = 0;
506 if (pc->pc_want_daemon_pages) {
507 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
508 tcd->tcd_cur_daemon_pages = 0;
511 spin_unlock(&pc->pc_lock);
514 static void collect_pages(struct page_collection *pc)
516 CFS_INIT_LIST_HEAD(&pc->pc_pages);
518 if (libcfs_panic_in_progress)
519 panic_collect_pages(pc);
521 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
524 static void put_pages_back_on_cpu(void *info)
526 struct page_collection *pc = info;
527 struct trace_cpu_data *tcd;
528 struct list_head *cur_head;
529 struct trace_page *tage;
530 struct trace_page *tmp;
533 spin_lock(&pc->pc_lock);
534 tcd_for_each_type_lock(tcd, i) {
535 cur_head = tcd->tcd_pages.next;
537 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
539 __LASSERT_TAGE_INVARIANT(tage);
541 if (tage->cpu != smp_processor_id() || tage->type != i)
544 tage_to_tail(tage, cur_head);
545 tcd->tcd_cur_pages++;
548 spin_unlock(&pc->pc_lock);
551 static void put_pages_back(struct page_collection *pc)
553 if (!libcfs_panic_in_progress)
554 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
557 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
558 * we have a good amount of data at all times for dumping during an LBUG, even
559 * if we have been steadily writing (and otherwise discarding) pages via the
561 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
562 struct trace_cpu_data *tcd)
564 struct trace_page *tage;
565 struct trace_page *tmp;
567 spin_lock(&pc->pc_lock);
568 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
570 __LASSERT_TAGE_INVARIANT(tage);
572 if (tage->cpu != smp_processor_id() ||
573 tage->type != tcd->tcd_type)
576 tage_to_tail(tage, &tcd->tcd_daemon_pages);
577 tcd->tcd_cur_daemon_pages++;
579 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
580 struct trace_page *victim;
582 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
583 victim = tage_from_list(tcd->tcd_daemon_pages.next);
585 __LASSERT_TAGE_INVARIANT(victim);
587 list_del(&victim->linkage);
589 tcd->tcd_cur_daemon_pages--;
592 spin_unlock(&pc->pc_lock);
595 static void put_pages_on_daemon_list_on_cpu(void *info)
597 struct trace_cpu_data *tcd;
600 tcd_for_each_type_lock(tcd, i)
601 put_pages_on_tcd_daemon_list(info, tcd);
604 static void put_pages_on_daemon_list(struct page_collection *pc)
606 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
609 void trace_debug_print(void)
611 struct page_collection pc;
612 struct trace_page *tage;
613 struct trace_page *tmp;
615 spin_lock_init(&pc.pc_lock);
617 pc.pc_want_daemon_pages = 1;
619 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
623 __LASSERT_TAGE_INVARIANT(tage);
626 p = cfs_page_address(page);
627 while (p < ((char *)cfs_page_address(page) + tage->used)) {
628 struct ptldebug_header *hdr;
633 p += strlen(file) + 1;
636 len = hdr->ph_len - (p - (char *)hdr);
638 print_to_console(hdr, D_EMERG, p, len, file, fn);
643 list_del(&tage->linkage);
648 int tracefile_dump_all_pages(char *filename)
650 struct page_collection pc;
652 struct trace_page *tage;
653 struct trace_page *tmp;
658 tracefile_write_lock();
660 filp = cfs_filp_open(filename,
661 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
664 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
669 spin_lock_init(&pc.pc_lock);
670 pc.pc_want_daemon_pages = 1;
672 if (list_empty(&pc.pc_pages)) {
677 /* ok, for now, just write the pages. in the future we'll be building
678 * iobufs with the pages and calling generic_direct_IO */
680 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
682 __LASSERT_TAGE_INVARIANT(tage);
684 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
685 tage->used, cfs_filp_poff(filp));
686 if (rc != (int)tage->used) {
687 printk(KERN_WARNING "wanted to write %u but wrote "
688 "%d\n", tage->used, rc);
690 __LASSERT(list_empty(&pc.pc_pages));
693 list_del(&tage->linkage);
697 rc = cfs_filp_fsync(filp);
699 printk(KERN_ERR "sync returns %d\n", rc);
701 cfs_filp_close(filp);
703 tracefile_write_unlock();
707 void trace_flush_pages(void)
709 struct page_collection pc;
710 struct trace_page *tage;
711 struct trace_page *tmp;
713 spin_lock_init(&pc.pc_lock);
715 pc.pc_want_daemon_pages = 1;
717 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
719 __LASSERT_TAGE_INVARIANT(tage);
721 list_del(&tage->linkage);
726 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
727 const char *usr_buffer, int usr_buffer_nob)
731 if (usr_buffer_nob > knl_buffer_nob)
734 if (copy_from_user((void *)knl_buffer,
735 (void *)usr_buffer, usr_buffer_nob))
738 nob = strnlen(knl_buffer, usr_buffer_nob);
739 while (nob-- >= 0) /* strip trailing whitespace */
740 if (!isspace(knl_buffer[nob]))
743 if (nob < 0) /* empty string */
746 if (nob == knl_buffer_nob) /* no space to terminate */
749 knl_buffer[nob + 1] = 0; /* terminate */
753 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
754 const char *knl_buffer, char *append)
756 /* NB if 'append' != NULL, it's a single character to append to the
757 * copied out string - usually "\n", for /proc entries and "" (i.e. a
758 * terminating zero byte) for sysctl entries */
759 int nob = strlen(knl_buffer);
761 if (nob > usr_buffer_nob)
762 nob = usr_buffer_nob;
764 if (copy_to_user(usr_buffer, knl_buffer, nob))
767 if (append != NULL && nob < usr_buffer_nob) {
768 if (copy_to_user(usr_buffer + nob, append, 1))
777 int trace_allocate_string_buffer(char **str, int nob)
779 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
782 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
789 void trace_free_string_buffer(char *str, int nob)
794 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
799 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
803 rc = trace_copyin_string(str, usr_str_nob + 1,
804 usr_str, usr_str_nob);
808 #if !defined(__WINNT__)
814 rc = tracefile_dump_all_pages(str);
816 trace_free_string_buffer(str, usr_str_nob + 1);
820 int trace_daemon_command(char *str)
824 tracefile_write_lock();
826 if (strcmp(str, "stop") == 0) {
828 memset(tracefile, 0, sizeof(tracefile));
830 } else if (strncmp(str, "size=", 5) == 0) {
831 tracefile_size = simple_strtoul(str + 5, NULL, 0);
832 if (tracefile_size < 10 || tracefile_size > 20480)
833 tracefile_size = TRACEFILE_SIZE;
835 tracefile_size <<= 20;
837 } else if (strlen(str) >= sizeof(tracefile)) {
840 } else if (str[0] != '/') {
844 strcpy(tracefile, str);
846 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
847 "to %s (%lukB max)\n", tracefile,
848 (long)(tracefile_size >> 10));
850 trace_start_thread();
853 tracefile_write_unlock();
857 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
862 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
866 rc = trace_copyin_string(str, usr_str_nob + 1,
867 usr_str, usr_str_nob);
869 rc = trace_daemon_command(str);
871 trace_free_string_buffer(str, usr_str_nob + 1);
875 int trace_set_debug_mb(int mb)
880 int limit = trace_max_debug_mb();
881 struct trace_cpu_data *tcd;
883 if (mb < num_possible_cpus())
887 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
888 "%dMB - limit is %d\n", mb, limit);
892 mb /= num_possible_cpus();
893 pages = mb << (20 - CFS_PAGE_SHIFT);
895 tracefile_write_lock();
897 tcd_for_each(tcd, i, j)
898 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
900 tracefile_write_unlock();
905 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
910 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
914 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
917 int trace_get_debug_mb(void)
921 struct trace_cpu_data *tcd;
924 tracefile_read_lock();
926 tcd_for_each(tcd, i, j)
927 total_pages += tcd->tcd_max_pages;
929 tracefile_read_unlock();
931 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
934 static int tracefiled(void *arg)
936 struct page_collection pc;
937 struct tracefiled_ctl *tctl = arg;
938 struct trace_page *tage;
939 struct trace_page *tmp;
940 struct ptldebug_header *hdr;
946 /* we're started late enough that we pick up init's fs context */
947 /* this is so broken in uml? what on earth is going on? */
948 cfs_daemonize("ktracefiled");
950 spin_lock_init(&pc.pc_lock);
951 complete(&tctl->tctl_start);
954 cfs_waitlink_t __wait;
956 cfs_waitlink_init(&__wait);
957 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
958 set_current_state(TASK_INTERRUPTIBLE);
959 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
960 cfs_time_seconds(1));
961 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
963 if (atomic_read(&tctl->tctl_shutdown))
966 pc.pc_want_daemon_pages = 0;
968 if (list_empty(&pc.pc_pages))
972 tracefile_read_lock();
973 if (tracefile[0] != 0) {
974 filp = cfs_filp_open(tracefile,
975 O_CREAT | O_RDWR | O_LARGEFILE,
978 printk(KERN_WARNING "couldn't open %s: %d\n",
981 tracefile_read_unlock();
983 put_pages_on_daemon_list(&pc);
984 __LASSERT(list_empty(&pc.pc_pages));
990 /* mark the first header, so we can sort in chunks */
991 tage = tage_from_list(pc.pc_pages.next);
992 __LASSERT_TAGE_INVARIANT(tage);
994 hdr = cfs_page_address(tage->page);
995 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
997 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1000 __LASSERT_TAGE_INVARIANT(tage);
1002 if (f_pos >= (off_t)tracefile_size)
1004 else if (f_pos > cfs_filp_size(filp))
1005 f_pos = cfs_filp_size(filp);
1007 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1008 tage->used, &f_pos);
1009 if (rc != (int)tage->used) {
1010 printk(KERN_WARNING "wanted to write %u but "
1011 "wrote %d\n", tage->used, rc);
1012 put_pages_back(&pc);
1013 __LASSERT(list_empty(&pc.pc_pages));
1018 cfs_filp_close(filp);
1019 put_pages_on_daemon_list(&pc);
1020 __LASSERT(list_empty(&pc.pc_pages));
1022 complete(&tctl->tctl_stop);
1026 int trace_start_thread(void)
1028 struct tracefiled_ctl *tctl = &trace_tctl;
1031 mutex_down(&trace_thread_sem);
1035 init_completion(&tctl->tctl_start);
1036 init_completion(&tctl->tctl_stop);
1037 cfs_waitq_init(&tctl->tctl_waitq);
1038 atomic_set(&tctl->tctl_shutdown, 0);
1040 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1045 wait_for_completion(&tctl->tctl_start);
1048 mutex_up(&trace_thread_sem);
1052 void trace_stop_thread(void)
1054 struct tracefiled_ctl *tctl = &trace_tctl;
1056 mutex_down(&trace_thread_sem);
1057 if (thread_running) {
1058 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1059 atomic_set(&tctl->tctl_shutdown, 1);
1060 wait_for_completion(&tctl->tctl_stop);
1063 mutex_up(&trace_thread_sem);
1066 int tracefile_init(int max_pages)
1068 struct trace_cpu_data *tcd;
1074 rc = tracefile_init_arch();
1078 tcd_for_each(tcd, i, j) {
1079 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1080 factor = tcd->tcd_pages_factor;
1081 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1082 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1083 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1084 tcd->tcd_cur_pages = 0;
1085 tcd->tcd_cur_stock_pages = 0;
1086 tcd->tcd_cur_daemon_pages = 0;
1087 tcd->tcd_max_pages = (max_pages * factor) / 100;
1088 LASSERT(tcd->tcd_max_pages > 0);
1089 tcd->tcd_shutting_down = 0;
1095 static void trace_cleanup_on_cpu(void *info)
1097 struct trace_cpu_data *tcd;
1098 struct trace_page *tage;
1099 struct trace_page *tmp;
1102 tcd_for_each_type_lock(tcd, i) {
1103 tcd->tcd_shutting_down = 1;
1105 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1106 __LASSERT_TAGE_INVARIANT(tage);
1108 list_del(&tage->linkage);
1111 tcd->tcd_cur_pages = 0;
1115 static void trace_cleanup(void)
1117 struct page_collection pc;
1119 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1120 spin_lock_init(&pc.pc_lock);
1122 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1124 tracefile_fini_arch();
1127 void tracefile_exit(void)
1129 trace_stop_thread();