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 if (printk_ratelimit())
191 printk(KERN_WARNING "debug daemon buffer overflowed; "
192 "discarding 10%% of pages (%d of %ld)\n",
193 pgcount + 1, tcd->tcd_cur_pages);
195 CFS_INIT_LIST_HEAD(&pc.pc_pages);
196 spin_lock_init(&pc.pc_lock);
198 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
202 list_move_tail(&tage->linkage, &pc.pc_pages);
203 tcd->tcd_cur_pages--;
205 put_pages_on_tcd_daemon_list(&pc, tcd);
208 /* return a page that has 'len' bytes left at the end */
209 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
212 struct trace_page *tage;
215 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
216 * from here: this will lead to infinite recursion.
219 if (len > CFS_PAGE_SIZE) {
221 "cowardly refusing to write %lu bytes in a page\n", len);
225 tage = trace_get_tage_try(tcd, len);
230 if (tcd->tcd_cur_pages > 0) {
231 tage = tage_from_list(tcd->tcd_pages.next);
233 tage_to_tail(tage, &tcd->tcd_pages);
238 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
239 const char *file, const char *fn, const int line,
240 const char *format1, va_list args,
241 const char *format2, ...)
243 struct trace_cpu_data *tcd = NULL;
244 struct ptldebug_header header;
245 struct trace_page *tage;
246 /* string_buf is used only if tcd != NULL, and is always set then */
247 char *string_buf = NULL;
250 int needed = 85; /* average message length */
257 if (strchr(file, '/'))
258 file = strrchr(file, '/') + 1;
261 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
263 tcd = trace_get_tcd();
264 if (tcd == NULL) /* arch may not log in IRQ context */
267 if (tcd->tcd_shutting_down) {
273 depth = __current_nesting_level();
274 known_size = strlen(file) + 1 + depth;
276 known_size += strlen(fn) + 1;
278 if (libcfs_debug_binary)
279 known_size += sizeof(header);
282 * '2' used because vsnprintf return real size required for output
283 * _without_ terminating NULL.
284 * if needed is to small for this format.
287 tage = trace_get_tage(tcd, needed + known_size + 1);
289 if (needed + known_size > CFS_PAGE_SIZE)
297 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
299 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
301 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
311 needed = vsnprintf(string_buf, max_nob, format1, ap);
317 remain = max_nob - needed;
321 va_start(ap, format2);
322 needed += vsnprintf(string_buf+needed, remain, format2, ap);
326 if (needed < max_nob) /* well. printing ok.. */
330 if (*(string_buf+needed-1) != '\n')
331 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
334 header.ph_len = known_size + needed;
335 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
337 if (libcfs_debug_binary) {
338 memcpy(debug_buf, &header, sizeof(header));
339 tage->used += sizeof(header);
340 debug_buf += sizeof(header);
343 /* indent message according to the nesting level */
344 while (depth-- > 0) {
345 *(debug_buf++) = '.';
349 strcpy(debug_buf, file);
350 tage->used += strlen(file) + 1;
351 debug_buf += strlen(file) + 1;
354 strcpy(debug_buf, fn);
355 tage->used += strlen(fn) + 1;
356 debug_buf += strlen(fn) + 1;
359 __LASSERT(debug_buf == string_buf);
361 tage->used += needed;
362 __LASSERT (tage->used <= CFS_PAGE_SIZE);
365 if ((mask & libcfs_printk) == 0) {
366 /* no console output requested */
373 if (libcfs_console_ratelimit &&
374 cdls->cdls_next != 0 && /* not first time ever */
375 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
376 /* skipping a console message */
383 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
384 libcfs_console_max_delay
385 + cfs_time_seconds(10))) {
386 /* last timeout was a long time ago */
387 cdls->cdls_delay /= libcfs_console_backoff * 4;
389 cdls->cdls_delay *= libcfs_console_backoff;
391 if (cdls->cdls_delay < libcfs_console_min_delay)
392 cdls->cdls_delay = libcfs_console_min_delay;
393 else if (cdls->cdls_delay > libcfs_console_max_delay)
394 cdls->cdls_delay = libcfs_console_max_delay;
397 /* ensure cdls_next is never zero after it's been seen */
398 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
402 print_to_console(&header, mask, string_buf, needed, file, fn);
405 string_buf = trace_get_console_buffer();
408 if (format1 != NULL) {
410 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
413 if (format2 != NULL) {
414 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
416 va_start(ap, format2);
417 needed += vsnprintf(string_buf+needed, remain, format2, ap);
421 print_to_console(&header, mask,
422 string_buf, needed, file, fn);
424 trace_put_console_buffer(string_buf);
427 if (cdls != NULL && cdls->cdls_count != 0) {
428 string_buf = trace_get_console_buffer();
430 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
431 "Skipped %d previous similar message%s\n",
432 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
434 print_to_console(&header, mask,
435 string_buf, needed, file, fn);
437 trace_put_console_buffer(string_buf);
438 cdls->cdls_count = 0;
443 EXPORT_SYMBOL(libcfs_debug_vmsg2);
446 libcfs_assertion_failed(const char *expr, const char *file,
447 const char *func, const int line)
449 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
450 "ASSERTION(%s) failed\n", expr);
453 EXPORT_SYMBOL(libcfs_assertion_failed);
456 trace_assertion_failed(const char *str,
457 const char *fn, const char *file, int line)
459 struct ptldebug_header hdr;
461 libcfs_panic_in_progress = 1;
462 libcfs_catastrophe = 1;
465 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
468 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
470 LIBCFS_PANIC("Lustre debug assertion failure\n");
476 panic_collect_pages(struct page_collection *pc)
478 /* Do the collect_pages job on a single CPU: assumes that all other
479 * CPUs have been stopped during a panic. If this isn't true for some
480 * arch, this will have to be implemented separately in each arch. */
483 struct trace_cpu_data *tcd;
485 CFS_INIT_LIST_HEAD(&pc->pc_pages);
487 tcd_for_each(tcd, i, j) {
488 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
489 tcd->tcd_cur_pages = 0;
491 if (pc->pc_want_daemon_pages) {
492 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
493 tcd->tcd_cur_daemon_pages = 0;
498 static void collect_pages_on_cpu(void *info)
500 struct trace_cpu_data *tcd;
501 struct page_collection *pc = info;
504 spin_lock(&pc->pc_lock);
505 tcd_for_each_type_lock(tcd, i) {
506 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
507 tcd->tcd_cur_pages = 0;
508 if (pc->pc_want_daemon_pages) {
509 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
510 tcd->tcd_cur_daemon_pages = 0;
513 spin_unlock(&pc->pc_lock);
516 static void collect_pages(struct page_collection *pc)
518 CFS_INIT_LIST_HEAD(&pc->pc_pages);
520 if (libcfs_panic_in_progress)
521 panic_collect_pages(pc);
523 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
526 static void put_pages_back_on_cpu(void *info)
528 struct page_collection *pc = info;
529 struct trace_cpu_data *tcd;
530 struct list_head *cur_head;
531 struct trace_page *tage;
532 struct trace_page *tmp;
535 spin_lock(&pc->pc_lock);
536 tcd_for_each_type_lock(tcd, i) {
537 cur_head = tcd->tcd_pages.next;
539 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
541 __LASSERT_TAGE_INVARIANT(tage);
543 if (tage->cpu != smp_processor_id() || tage->type != i)
546 tage_to_tail(tage, cur_head);
547 tcd->tcd_cur_pages++;
550 spin_unlock(&pc->pc_lock);
553 static void put_pages_back(struct page_collection *pc)
555 if (!libcfs_panic_in_progress)
556 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
559 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
560 * we have a good amount of data at all times for dumping during an LBUG, even
561 * if we have been steadily writing (and otherwise discarding) pages via the
563 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
564 struct trace_cpu_data *tcd)
566 struct trace_page *tage;
567 struct trace_page *tmp;
569 spin_lock(&pc->pc_lock);
570 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
572 __LASSERT_TAGE_INVARIANT(tage);
574 if (tage->cpu != smp_processor_id() ||
575 tage->type != tcd->tcd_type)
578 tage_to_tail(tage, &tcd->tcd_daemon_pages);
579 tcd->tcd_cur_daemon_pages++;
581 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
582 struct trace_page *victim;
584 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
585 victim = tage_from_list(tcd->tcd_daemon_pages.next);
587 __LASSERT_TAGE_INVARIANT(victim);
589 list_del(&victim->linkage);
591 tcd->tcd_cur_daemon_pages--;
594 spin_unlock(&pc->pc_lock);
597 static void put_pages_on_daemon_list_on_cpu(void *info)
599 struct trace_cpu_data *tcd;
602 tcd_for_each_type_lock(tcd, i)
603 put_pages_on_tcd_daemon_list(info, tcd);
606 static void put_pages_on_daemon_list(struct page_collection *pc)
608 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
611 void trace_debug_print(void)
613 struct page_collection pc;
614 struct trace_page *tage;
615 struct trace_page *tmp;
617 spin_lock_init(&pc.pc_lock);
619 pc.pc_want_daemon_pages = 1;
621 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
625 __LASSERT_TAGE_INVARIANT(tage);
628 p = cfs_page_address(page);
629 while (p < ((char *)cfs_page_address(page) + tage->used)) {
630 struct ptldebug_header *hdr;
635 p += strlen(file) + 1;
638 len = hdr->ph_len - (p - (char *)hdr);
640 print_to_console(hdr, D_EMERG, p, len, file, fn);
645 list_del(&tage->linkage);
650 int tracefile_dump_all_pages(char *filename)
652 struct page_collection pc;
654 struct trace_page *tage;
655 struct trace_page *tmp;
660 tracefile_write_lock();
662 filp = cfs_filp_open(filename,
663 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
666 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
671 spin_lock_init(&pc.pc_lock);
672 pc.pc_want_daemon_pages = 1;
674 if (list_empty(&pc.pc_pages)) {
679 /* ok, for now, just write the pages. in the future we'll be building
680 * iobufs with the pages and calling generic_direct_IO */
682 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
684 __LASSERT_TAGE_INVARIANT(tage);
686 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
687 tage->used, cfs_filp_poff(filp));
688 if (rc != (int)tage->used) {
689 printk(KERN_WARNING "wanted to write %u but wrote "
690 "%d\n", tage->used, rc);
692 __LASSERT(list_empty(&pc.pc_pages));
695 list_del(&tage->linkage);
699 rc = cfs_filp_fsync(filp);
701 printk(KERN_ERR "sync returns %d\n", rc);
703 cfs_filp_close(filp);
705 tracefile_write_unlock();
709 void trace_flush_pages(void)
711 struct page_collection pc;
712 struct trace_page *tage;
713 struct trace_page *tmp;
715 spin_lock_init(&pc.pc_lock);
717 pc.pc_want_daemon_pages = 1;
719 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
721 __LASSERT_TAGE_INVARIANT(tage);
723 list_del(&tage->linkage);
728 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
729 const char *usr_buffer, int usr_buffer_nob)
733 if (usr_buffer_nob > knl_buffer_nob)
736 if (copy_from_user((void *)knl_buffer,
737 (void *)usr_buffer, usr_buffer_nob))
740 nob = strnlen(knl_buffer, usr_buffer_nob);
741 while (nob-- >= 0) /* strip trailing whitespace */
742 if (!isspace(knl_buffer[nob]))
745 if (nob < 0) /* empty string */
748 if (nob == knl_buffer_nob) /* no space to terminate */
751 knl_buffer[nob + 1] = 0; /* terminate */
755 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
756 const char *knl_buffer, char *append)
758 /* NB if 'append' != NULL, it's a single character to append to the
759 * copied out string - usually "\n", for /proc entries and "" (i.e. a
760 * terminating zero byte) for sysctl entries */
761 int nob = strlen(knl_buffer);
763 if (nob > usr_buffer_nob)
764 nob = usr_buffer_nob;
766 if (copy_to_user(usr_buffer, knl_buffer, nob))
769 if (append != NULL && nob < usr_buffer_nob) {
770 if (copy_to_user(usr_buffer + nob, append, 1))
779 int trace_allocate_string_buffer(char **str, int nob)
781 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
784 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
791 void trace_free_string_buffer(char *str, int nob)
796 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
801 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
805 rc = trace_copyin_string(str, usr_str_nob + 1,
806 usr_str, usr_str_nob);
810 #if !defined(__WINNT__)
816 rc = tracefile_dump_all_pages(str);
818 trace_free_string_buffer(str, usr_str_nob + 1);
822 int trace_daemon_command(char *str)
826 tracefile_write_lock();
828 if (strcmp(str, "stop") == 0) {
830 memset(tracefile, 0, sizeof(tracefile));
832 } else if (strncmp(str, "size=", 5) == 0) {
833 tracefile_size = simple_strtoul(str + 5, NULL, 0);
834 if (tracefile_size < 10 || tracefile_size > 20480)
835 tracefile_size = TRACEFILE_SIZE;
837 tracefile_size <<= 20;
839 } else if (strlen(str) >= sizeof(tracefile)) {
842 } else if (str[0] != '/') {
846 strcpy(tracefile, str);
848 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
849 "to %s (%lukB max)\n", tracefile,
850 (long)(tracefile_size >> 10));
852 trace_start_thread();
855 tracefile_write_unlock();
859 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
864 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
868 rc = trace_copyin_string(str, usr_str_nob + 1,
869 usr_str, usr_str_nob);
871 rc = trace_daemon_command(str);
873 trace_free_string_buffer(str, usr_str_nob + 1);
877 int trace_set_debug_mb(int mb)
882 int limit = trace_max_debug_mb();
883 struct trace_cpu_data *tcd;
885 if (mb < num_possible_cpus())
889 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
890 "%dMB - limit is %d\n", mb, limit);
894 mb /= num_possible_cpus();
895 pages = mb << (20 - CFS_PAGE_SHIFT);
897 tracefile_write_lock();
899 tcd_for_each(tcd, i, j)
900 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
902 tracefile_write_unlock();
907 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
912 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
916 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
919 int trace_get_debug_mb(void)
923 struct trace_cpu_data *tcd;
926 tracefile_read_lock();
928 tcd_for_each(tcd, i, j)
929 total_pages += tcd->tcd_max_pages;
931 tracefile_read_unlock();
933 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
936 static int tracefiled(void *arg)
938 struct page_collection pc;
939 struct tracefiled_ctl *tctl = arg;
940 struct trace_page *tage;
941 struct trace_page *tmp;
942 struct ptldebug_header *hdr;
948 /* we're started late enough that we pick up init's fs context */
949 /* this is so broken in uml? what on earth is going on? */
950 cfs_daemonize("ktracefiled");
952 spin_lock_init(&pc.pc_lock);
953 complete(&tctl->tctl_start);
956 cfs_waitlink_t __wait;
958 cfs_waitlink_init(&__wait);
959 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
960 set_current_state(TASK_INTERRUPTIBLE);
961 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
962 cfs_time_seconds(1));
963 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
965 if (atomic_read(&tctl->tctl_shutdown))
968 pc.pc_want_daemon_pages = 0;
970 if (list_empty(&pc.pc_pages))
974 tracefile_read_lock();
975 if (tracefile[0] != 0) {
976 filp = cfs_filp_open(tracefile,
977 O_CREAT | O_RDWR | O_LARGEFILE,
980 printk(KERN_WARNING "couldn't open %s: %d\n",
983 tracefile_read_unlock();
985 put_pages_on_daemon_list(&pc);
986 __LASSERT(list_empty(&pc.pc_pages));
992 /* mark the first header, so we can sort in chunks */
993 tage = tage_from_list(pc.pc_pages.next);
994 __LASSERT_TAGE_INVARIANT(tage);
996 hdr = cfs_page_address(tage->page);
997 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
999 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1000 static loff_t f_pos;
1002 __LASSERT_TAGE_INVARIANT(tage);
1004 if (f_pos >= (off_t)tracefile_size)
1006 else if (f_pos > cfs_filp_size(filp))
1007 f_pos = cfs_filp_size(filp);
1009 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
1010 tage->used, &f_pos);
1011 if (rc != (int)tage->used) {
1012 printk(KERN_WARNING "wanted to write %u but "
1013 "wrote %d\n", tage->used, rc);
1014 put_pages_back(&pc);
1015 __LASSERT(list_empty(&pc.pc_pages));
1020 cfs_filp_close(filp);
1021 put_pages_on_daemon_list(&pc);
1022 __LASSERT(list_empty(&pc.pc_pages));
1024 complete(&tctl->tctl_stop);
1028 int trace_start_thread(void)
1030 struct tracefiled_ctl *tctl = &trace_tctl;
1033 mutex_down(&trace_thread_sem);
1037 init_completion(&tctl->tctl_start);
1038 init_completion(&tctl->tctl_stop);
1039 cfs_waitq_init(&tctl->tctl_waitq);
1040 atomic_set(&tctl->tctl_shutdown, 0);
1042 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1047 wait_for_completion(&tctl->tctl_start);
1050 mutex_up(&trace_thread_sem);
1054 void trace_stop_thread(void)
1056 struct tracefiled_ctl *tctl = &trace_tctl;
1058 mutex_down(&trace_thread_sem);
1059 if (thread_running) {
1060 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1061 atomic_set(&tctl->tctl_shutdown, 1);
1062 wait_for_completion(&tctl->tctl_stop);
1065 mutex_up(&trace_thread_sem);
1068 int tracefile_init(int max_pages)
1070 struct trace_cpu_data *tcd;
1076 rc = tracefile_init_arch();
1080 tcd_for_each(tcd, i, j) {
1081 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1082 factor = tcd->tcd_pages_factor;
1083 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1084 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1085 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1086 tcd->tcd_cur_pages = 0;
1087 tcd->tcd_cur_stock_pages = 0;
1088 tcd->tcd_cur_daemon_pages = 0;
1089 tcd->tcd_max_pages = (max_pages * factor) / 100;
1090 LASSERT(tcd->tcd_max_pages > 0);
1091 tcd->tcd_shutting_down = 0;
1097 static void trace_cleanup_on_cpu(void *info)
1099 struct trace_cpu_data *tcd;
1100 struct trace_page *tage;
1101 struct trace_page *tmp;
1104 tcd_for_each_type_lock(tcd, i) {
1105 tcd->tcd_shutting_down = 1;
1107 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1108 __LASSERT_TAGE_INVARIANT(tage);
1110 list_del(&tage->linkage);
1113 tcd->tcd_cur_pages = 0;
1117 static void trace_cleanup(void)
1119 struct page_collection pc;
1121 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1122 spin_lock_init(&pc.pc_lock);
1124 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1126 tracefile_fini_arch();
1129 void tracefile_exit(void)
1131 trace_stop_thread();