1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Zach Brown <zab@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_LNET
26 #define LUSTRE_TRACEFILE_PRIVATE
27 #include "tracefile.h"
29 #include <libcfs/libcfs.h>
31 /* XXX move things up to the top, comment */
32 union trace_data_union (*trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
34 char tracefile[TRACEFILE_NAME_SIZE];
35 long long tracefile_size = TRACEFILE_SIZE;
36 static struct tracefiled_ctl trace_tctl;
37 struct semaphore trace_thread_sem;
38 static int thread_running = 0;
40 atomic_t tage_allocated = ATOMIC_INIT(0);
42 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
43 struct trace_cpu_data *tcd);
45 static inline struct trace_page *tage_from_list(struct list_head *list)
47 return list_entry(list, struct trace_page, linkage);
50 static struct trace_page *tage_alloc(int gfp)
53 struct trace_page *tage;
56 * Don't spam console with allocation failures: they will be reported
57 * by upper layer anyway.
59 gfp |= CFS_ALLOC_NOWARN;
60 page = cfs_alloc_page(gfp);
64 tage = cfs_alloc(sizeof(*tage), gfp);
71 atomic_inc(&tage_allocated);
75 static void tage_free(struct trace_page *tage)
77 __LASSERT(tage != NULL);
78 __LASSERT(tage->page != NULL);
80 cfs_free_page(tage->page);
82 atomic_dec(&tage_allocated);
85 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
87 __LASSERT(tage != NULL);
88 __LASSERT(queue != NULL);
90 list_move_tail(&tage->linkage, queue);
93 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
94 struct list_head *stock)
99 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
100 * from here: this will lead to infinite recursion.
103 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
104 struct trace_page *tage;
106 tage = tage_alloc(gfp);
109 list_add_tail(&tage->linkage, stock);
114 /* return a page that has 'len' bytes left at the end */
115 static struct trace_page *trace_get_tage_try(struct trace_cpu_data *tcd,
118 struct trace_page *tage;
120 if (tcd->tcd_cur_pages > 0) {
121 __LASSERT(!list_empty(&tcd->tcd_pages));
122 tage = tage_from_list(tcd->tcd_pages.prev);
123 if (tage->used + len <= CFS_PAGE_SIZE)
127 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
128 if (tcd->tcd_cur_stock_pages > 0) {
129 tage = tage_from_list(tcd->tcd_stock_pages.prev);
130 -- tcd->tcd_cur_stock_pages;
131 list_del_init(&tage->linkage);
133 tage = tage_alloc(CFS_ALLOC_ATOMIC);
136 "failure to allocate a tage (%ld)\n",
143 tage->cpu = smp_processor_id();
144 tage->type = tcd->tcd_type;
145 list_add_tail(&tage->linkage, &tcd->tcd_pages);
146 tcd->tcd_cur_pages++;
148 if (tcd->tcd_cur_pages > 8 && thread_running) {
149 struct tracefiled_ctl *tctl = &trace_tctl;
151 * wake up tracefiled to process some pages.
153 cfs_waitq_signal(&tctl->tctl_waitq);
160 static void tcd_shrink(struct trace_cpu_data *tcd)
162 int pgcount = tcd->tcd_cur_pages / 10;
163 struct page_collection pc;
164 struct trace_page *tage;
165 struct trace_page *tmp;
168 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
169 * from here: this will lead to infinite recursion.
172 printk(KERN_WARNING "debug daemon buffer overflowed; discarding"
173 " 10%% of pages (%d of %ld)\n", pgcount + 1, tcd->tcd_cur_pages);
175 CFS_INIT_LIST_HEAD(&pc.pc_pages);
176 spin_lock_init(&pc.pc_lock);
178 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
182 list_move_tail(&tage->linkage, &pc.pc_pages);
183 tcd->tcd_cur_pages--;
185 put_pages_on_tcd_daemon_list(&pc, tcd);
188 /* return a page that has 'len' bytes left at the end */
189 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
192 struct trace_page *tage;
195 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
196 * from here: this will lead to infinite recursion.
199 if (len > CFS_PAGE_SIZE) {
201 "cowardly refusing to write %lu bytes in a page\n", len);
205 tage = trace_get_tage_try(tcd, len);
210 if (tcd->tcd_cur_pages > 0) {
211 tage = tage_from_list(tcd->tcd_pages.next);
213 tage_to_tail(tage, &tcd->tcd_pages);
218 int libcfs_debug_vmsg2(cfs_debug_limit_state_t *cdls, int subsys, int mask,
219 const char *file, const char *fn, const int line,
220 const char *format1, va_list args,
221 const char *format2, ...)
223 struct trace_cpu_data *tcd = NULL;
224 struct ptldebug_header header;
225 struct trace_page *tage;
226 /* string_buf is used only if tcd != NULL, and is always set then */
227 char *string_buf = NULL;
230 int needed = 85; /* average message length */
237 if (strchr(file, '/'))
238 file = strrchr(file, '/') + 1;
241 set_ptldebug_header(&header, subsys, mask, line, CDEBUG_STACK());
243 tcd = trace_get_tcd();
244 if (tcd == NULL) /* arch may not log in IRQ context */
247 if (tcd->tcd_shutting_down) {
253 depth = __current_nesting_level();
254 known_size = strlen(file) + 1 + depth;
256 known_size += strlen(fn) + 1;
258 if (libcfs_debug_binary)
259 known_size += sizeof(header);
262 * '2' used because vsnprintf return real size required for output
263 * _without_ terminating NULL.
264 * if needed is to small for this format.
267 tage = trace_get_tage(tcd, needed + known_size + 1);
269 if (needed + known_size > CFS_PAGE_SIZE)
277 string_buf = (char *)cfs_page_address(tage->page)+tage->used+known_size;
279 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
281 printk(KERN_EMERG "negative max_nob: %i\n", max_nob);
291 needed = vsnprintf(string_buf, max_nob, format1, ap);
297 remain = max_nob - needed;
301 va_start(ap, format2);
302 needed += vsnprintf(string_buf+needed, remain, format2, ap);
306 if (needed < max_nob) /* well. printing ok.. */
310 if (*(string_buf+needed-1) != '\n')
311 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
314 header.ph_len = known_size + needed;
315 debug_buf = (char *)cfs_page_address(tage->page) + tage->used;
317 if (libcfs_debug_binary) {
318 memcpy(debug_buf, &header, sizeof(header));
319 tage->used += sizeof(header);
320 debug_buf += sizeof(header);
323 /* indent message according to the nesting level */
324 while (depth-- > 0) {
325 *(debug_buf++) = '.';
329 strcpy(debug_buf, file);
330 tage->used += strlen(file) + 1;
331 debug_buf += strlen(file) + 1;
334 strcpy(debug_buf, fn);
335 tage->used += strlen(fn) + 1;
336 debug_buf += strlen(fn) + 1;
339 __LASSERT(debug_buf == string_buf);
341 tage->used += needed;
342 __LASSERT (tage->used <= CFS_PAGE_SIZE);
345 if ((mask & libcfs_printk) == 0) {
346 /* no console output requested */
353 if (libcfs_console_ratelimit &&
354 cdls->cdls_next != 0 && /* not first time ever */
355 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
356 /* skipping a console message */
363 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
364 libcfs_console_max_delay
365 + cfs_time_seconds(10))) {
366 /* last timeout was a long time ago */
367 cdls->cdls_delay /= libcfs_console_backoff * 4;
369 cdls->cdls_delay *= libcfs_console_backoff;
371 if (cdls->cdls_delay < libcfs_console_min_delay)
372 cdls->cdls_delay = libcfs_console_min_delay;
373 else if (cdls->cdls_delay > libcfs_console_max_delay)
374 cdls->cdls_delay = libcfs_console_max_delay;
377 /* ensure cdls_next is never zero after it's been seen */
378 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
382 print_to_console(&header, mask, string_buf, needed, file, fn);
385 string_buf = trace_get_console_buffer();
388 if (format1 != NULL) {
390 needed = vsnprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE, format1, ap);
393 if (format2 != NULL) {
394 remain = TRACE_CONSOLE_BUFFER_SIZE - needed;
396 va_start(ap, format2);
397 needed += vsnprintf(string_buf+needed, remain, format2, ap);
401 print_to_console(&header, mask,
402 string_buf, needed, file, fn);
404 trace_put_console_buffer(string_buf);
407 if (cdls != NULL && cdls->cdls_count != 0) {
408 string_buf = trace_get_console_buffer();
410 needed = snprintf(string_buf, TRACE_CONSOLE_BUFFER_SIZE,
411 "Skipped %d previous similar message%s\n",
412 cdls->cdls_count, (cdls->cdls_count > 1) ? "s" : "");
414 print_to_console(&header, mask,
415 string_buf, needed, file, fn);
417 trace_put_console_buffer(string_buf);
418 cdls->cdls_count = 0;
423 EXPORT_SYMBOL(libcfs_debug_vmsg2);
426 libcfs_assertion_failed(const char *expr, const char *file,
427 const char *func, const int line)
429 libcfs_debug_msg(NULL, 0, D_EMERG, file, func, line,
430 "ASSERTION(%s) failed\n", expr);
433 EXPORT_SYMBOL(libcfs_assertion_failed);
436 trace_assertion_failed(const char *str,
437 const char *fn, const char *file, int line)
439 struct ptldebug_header hdr;
441 libcfs_panic_in_progress = 1;
442 libcfs_catastrophe = 1;
445 set_ptldebug_header(&hdr, DEBUG_SUBSYSTEM, D_EMERG, line,
448 print_to_console(&hdr, D_EMERG, str, strlen(str), file, fn);
450 LIBCFS_PANIC("Lustre debug assertion failure\n");
456 panic_collect_pages(struct page_collection *pc)
458 /* Do the collect_pages job on a single CPU: assumes that all other
459 * CPUs have been stopped during a panic. If this isn't true for some
460 * arch, this will have to be implemented separately in each arch. */
463 struct trace_cpu_data *tcd;
465 CFS_INIT_LIST_HEAD(&pc->pc_pages);
467 tcd_for_each(tcd, i, j) {
468 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
469 tcd->tcd_cur_pages = 0;
471 if (pc->pc_want_daemon_pages) {
472 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
473 tcd->tcd_cur_daemon_pages = 0;
478 static void collect_pages_on_cpu(void *info)
480 struct trace_cpu_data *tcd;
481 struct page_collection *pc = info;
484 spin_lock(&pc->pc_lock);
485 tcd_for_each_type_lock(tcd, i) {
486 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
487 tcd->tcd_cur_pages = 0;
488 if (pc->pc_want_daemon_pages) {
489 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
490 tcd->tcd_cur_daemon_pages = 0;
493 spin_unlock(&pc->pc_lock);
496 static void collect_pages(struct page_collection *pc)
498 CFS_INIT_LIST_HEAD(&pc->pc_pages);
500 if (libcfs_panic_in_progress)
501 panic_collect_pages(pc);
503 trace_call_on_all_cpus(collect_pages_on_cpu, pc);
506 static void put_pages_back_on_cpu(void *info)
508 struct page_collection *pc = info;
509 struct trace_cpu_data *tcd;
510 struct list_head *cur_head;
511 struct trace_page *tage;
512 struct trace_page *tmp;
515 spin_lock(&pc->pc_lock);
516 tcd_for_each_type_lock(tcd, i) {
517 cur_head = tcd->tcd_pages.next;
519 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
521 __LASSERT_TAGE_INVARIANT(tage);
523 if (tage->cpu != smp_processor_id() || tage->type != i)
526 tage_to_tail(tage, cur_head);
527 tcd->tcd_cur_pages++;
530 spin_unlock(&pc->pc_lock);
533 static void put_pages_back(struct page_collection *pc)
535 if (!libcfs_panic_in_progress)
536 trace_call_on_all_cpus(put_pages_back_on_cpu, pc);
539 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
540 * we have a good amount of data at all times for dumping during an LBUG, even
541 * if we have been steadily writing (and otherwise discarding) pages via the
543 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
544 struct trace_cpu_data *tcd)
546 struct trace_page *tage;
547 struct trace_page *tmp;
549 spin_lock(&pc->pc_lock);
550 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
552 __LASSERT_TAGE_INVARIANT(tage);
554 if (tage->cpu != smp_processor_id() ||
555 tage->type != tcd->tcd_type)
558 tage_to_tail(tage, &tcd->tcd_daemon_pages);
559 tcd->tcd_cur_daemon_pages++;
561 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
562 struct trace_page *victim;
564 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
565 victim = tage_from_list(tcd->tcd_daemon_pages.next);
567 __LASSERT_TAGE_INVARIANT(victim);
569 list_del(&victim->linkage);
571 tcd->tcd_cur_daemon_pages--;
574 spin_unlock(&pc->pc_lock);
577 static void put_pages_on_daemon_list_on_cpu(void *info)
579 struct trace_cpu_data *tcd;
582 tcd_for_each_type_lock(tcd, i)
583 put_pages_on_tcd_daemon_list(info, tcd);
586 static void put_pages_on_daemon_list(struct page_collection *pc)
588 trace_call_on_all_cpus(put_pages_on_daemon_list_on_cpu, pc);
591 void trace_debug_print(void)
593 struct page_collection pc;
594 struct trace_page *tage;
595 struct trace_page *tmp;
597 spin_lock_init(&pc.pc_lock);
599 pc.pc_want_daemon_pages = 1;
601 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
605 __LASSERT_TAGE_INVARIANT(tage);
608 p = cfs_page_address(page);
609 while (p < ((char *)cfs_page_address(page) + tage->used)) {
610 struct ptldebug_header *hdr;
615 p += strlen(file) + 1;
618 len = hdr->ph_len - (p - (char *)hdr);
620 print_to_console(hdr, D_EMERG, p, len, file, fn);
625 list_del(&tage->linkage);
630 int tracefile_dump_all_pages(char *filename)
632 struct page_collection pc;
634 struct trace_page *tage;
635 struct trace_page *tmp;
640 tracefile_write_lock();
642 filp = cfs_filp_open(filename,
643 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
646 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
651 spin_lock_init(&pc.pc_lock);
652 pc.pc_want_daemon_pages = 1;
654 if (list_empty(&pc.pc_pages)) {
659 /* ok, for now, just write the pages. in the future we'll be building
660 * iobufs with the pages and calling generic_direct_IO */
662 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
664 __LASSERT_TAGE_INVARIANT(tage);
666 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
667 tage->used, cfs_filp_poff(filp));
668 if (rc != (int)tage->used) {
669 printk(KERN_WARNING "wanted to write %u but wrote "
670 "%d\n", tage->used, rc);
672 __LASSERT(list_empty(&pc.pc_pages));
675 list_del(&tage->linkage);
679 rc = cfs_filp_fsync(filp);
681 printk(KERN_ERR "sync returns %d\n", rc);
683 cfs_filp_close(filp);
685 tracefile_write_unlock();
689 void trace_flush_pages(void)
691 struct page_collection pc;
692 struct trace_page *tage;
693 struct trace_page *tmp;
695 spin_lock_init(&pc.pc_lock);
697 pc.pc_want_daemon_pages = 1;
699 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
701 __LASSERT_TAGE_INVARIANT(tage);
703 list_del(&tage->linkage);
708 int trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
709 const char *usr_buffer, int usr_buffer_nob)
713 if (usr_buffer_nob > knl_buffer_nob)
716 if (copy_from_user((void *)knl_buffer,
717 (void *)usr_buffer, usr_buffer_nob))
720 nob = strnlen(knl_buffer, usr_buffer_nob);
721 while (nob-- >= 0) /* strip trailing whitespace */
722 if (!isspace(knl_buffer[nob]))
725 if (nob < 0) /* empty string */
728 if (nob == knl_buffer_nob) /* no space to terminate */
731 knl_buffer[nob + 1] = 0; /* terminate */
735 int trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
736 const char *knl_buffer, char *append)
738 /* NB if 'append' != NULL, it's a single character to append to the
739 * copied out string - usually "\n", for /proc entries and "" (i.e. a
740 * terminating zero byte) for sysctl entries */
741 int nob = strlen(knl_buffer);
743 if (nob > usr_buffer_nob)
744 nob = usr_buffer_nob;
746 if (copy_to_user(usr_buffer, knl_buffer, nob))
749 if (append != NULL && nob < usr_buffer_nob) {
750 if (copy_to_user(usr_buffer + nob, append, 1))
759 int trace_allocate_string_buffer(char **str, int nob)
761 if (nob > 2 * CFS_PAGE_SIZE) /* string must be "sensible" */
764 *str = cfs_alloc(nob, CFS_ALLOC_STD | CFS_ALLOC_ZERO);
771 void trace_free_string_buffer(char *str, int nob)
776 int trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob)
781 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
785 rc = trace_copyin_string(str, usr_str_nob + 1,
786 usr_str, usr_str_nob);
790 #if !defined(__WINNT__)
796 rc = tracefile_dump_all_pages(str);
798 trace_free_string_buffer(str, usr_str_nob + 1);
802 int trace_daemon_command(char *str)
806 tracefile_write_lock();
808 if (strcmp(str, "stop") == 0) {
810 memset(tracefile, 0, sizeof(tracefile));
812 } else if (strncmp(str, "size=", 5) == 0) {
813 tracefile_size = simple_strtoul(str + 5, NULL, 0);
814 if (tracefile_size < 10 || tracefile_size > 20480)
815 tracefile_size = TRACEFILE_SIZE;
817 tracefile_size <<= 20;
819 } else if (strlen(str) >= sizeof(tracefile)) {
822 } else if (str[0] != '/') {
826 strcpy(tracefile, str);
828 printk(KERN_INFO "Lustre: debug daemon will attempt to start writing "
829 "to %s (%lukB max)\n", tracefile,
830 (long)(tracefile_size >> 10));
832 trace_start_thread();
835 tracefile_write_unlock();
839 int trace_daemon_command_usrstr(void *usr_str, int usr_str_nob)
844 rc = trace_allocate_string_buffer(&str, usr_str_nob + 1);
848 rc = trace_copyin_string(str, usr_str_nob + 1,
849 usr_str, usr_str_nob);
851 rc = trace_daemon_command(str);
853 trace_free_string_buffer(str, usr_str_nob + 1);
857 int trace_set_debug_mb(int mb)
862 int limit = trace_max_debug_mb();
863 struct trace_cpu_data *tcd;
865 if (mb < num_possible_cpus())
869 printk(KERN_ERR "Lustre: Refusing to set debug buffer size to "
870 "%dMB - limit is %d\n", mb, limit);
874 mb /= num_possible_cpus();
875 pages = mb << (20 - CFS_PAGE_SHIFT);
877 tracefile_write_lock();
879 tcd_for_each(tcd, i, j)
880 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
882 tracefile_write_unlock();
887 int trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob)
892 rc = trace_copyin_string(str, sizeof(str), usr_str, usr_str_nob);
896 return trace_set_debug_mb(simple_strtoul(str, NULL, 0));
899 int trace_get_debug_mb(void)
903 struct trace_cpu_data *tcd;
906 tracefile_read_lock();
908 tcd_for_each(tcd, i, j)
909 total_pages += tcd->tcd_max_pages;
911 tracefile_read_unlock();
913 return (total_pages >> (20 - CFS_PAGE_SHIFT)) + 1;
916 static int tracefiled(void *arg)
918 struct page_collection pc;
919 struct tracefiled_ctl *tctl = arg;
920 struct trace_page *tage;
921 struct trace_page *tmp;
922 struct ptldebug_header *hdr;
928 /* we're started late enough that we pick up init's fs context */
929 /* this is so broken in uml? what on earth is going on? */
930 cfs_daemonize("ktracefiled");
932 spin_lock_init(&pc.pc_lock);
933 complete(&tctl->tctl_start);
936 cfs_waitlink_t __wait;
938 cfs_waitlink_init(&__wait);
939 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
940 set_current_state(TASK_INTERRUPTIBLE);
941 cfs_waitq_timedwait(&__wait, CFS_TASK_INTERRUPTIBLE,
942 cfs_time_seconds(1));
943 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
945 if (atomic_read(&tctl->tctl_shutdown))
948 pc.pc_want_daemon_pages = 0;
950 if (list_empty(&pc.pc_pages))
954 tracefile_read_lock();
955 if (tracefile[0] != 0) {
956 filp = cfs_filp_open(tracefile,
957 O_CREAT | O_RDWR | O_LARGEFILE,
960 printk(KERN_WARNING "couldn't open %s: %d\n",
963 tracefile_read_unlock();
965 put_pages_on_daemon_list(&pc);
966 __LASSERT(list_empty(&pc.pc_pages));
972 /* mark the first header, so we can sort in chunks */
973 tage = tage_from_list(pc.pc_pages.next);
974 __LASSERT_TAGE_INVARIANT(tage);
976 hdr = cfs_page_address(tage->page);
977 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
979 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
982 __LASSERT_TAGE_INVARIANT(tage);
984 if (f_pos >= (off_t)tracefile_size)
986 else if (f_pos > cfs_filp_size(filp))
987 f_pos = cfs_filp_size(filp);
989 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
991 if (rc != (int)tage->used) {
992 printk(KERN_WARNING "wanted to write %u but "
993 "wrote %d\n", tage->used, rc);
995 __LASSERT(list_empty(&pc.pc_pages));
1000 cfs_filp_close(filp);
1001 put_pages_on_daemon_list(&pc);
1002 __LASSERT(list_empty(&pc.pc_pages));
1004 complete(&tctl->tctl_stop);
1008 int trace_start_thread(void)
1010 struct tracefiled_ctl *tctl = &trace_tctl;
1013 mutex_down(&trace_thread_sem);
1017 init_completion(&tctl->tctl_start);
1018 init_completion(&tctl->tctl_stop);
1019 cfs_waitq_init(&tctl->tctl_waitq);
1020 atomic_set(&tctl->tctl_shutdown, 0);
1022 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
1027 wait_for_completion(&tctl->tctl_start);
1030 mutex_up(&trace_thread_sem);
1034 void trace_stop_thread(void)
1036 struct tracefiled_ctl *tctl = &trace_tctl;
1038 mutex_down(&trace_thread_sem);
1039 if (thread_running) {
1040 printk(KERN_INFO "Lustre: shutting down debug daemon thread...\n");
1041 atomic_set(&tctl->tctl_shutdown, 1);
1042 wait_for_completion(&tctl->tctl_stop);
1045 mutex_up(&trace_thread_sem);
1048 int tracefile_init(int max_pages)
1050 struct trace_cpu_data *tcd;
1056 rc = tracefile_init_arch();
1060 tcd_for_each(tcd, i, j) {
1061 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1062 factor = tcd->tcd_pages_factor;
1063 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
1064 CFS_INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1065 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1066 tcd->tcd_cur_pages = 0;
1067 tcd->tcd_cur_stock_pages = 0;
1068 tcd->tcd_cur_daemon_pages = 0;
1069 tcd->tcd_max_pages = (max_pages * factor) / 100;
1070 LASSERT(tcd->tcd_max_pages > 0);
1071 tcd->tcd_shutting_down = 0;
1077 static void trace_cleanup_on_cpu(void *info)
1079 struct trace_cpu_data *tcd;
1080 struct trace_page *tage;
1081 struct trace_page *tmp;
1084 tcd_for_each_type_lock(tcd, i) {
1085 tcd->tcd_shutting_down = 1;
1087 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1088 __LASSERT_TAGE_INVARIANT(tage);
1090 list_del(&tage->linkage);
1093 tcd->tcd_cur_pages = 0;
1097 static void trace_cleanup(void)
1099 struct page_collection pc;
1101 CFS_INIT_LIST_HEAD(&pc.pc_pages);
1102 spin_lock_init(&pc.pc_lock);
1104 trace_call_on_all_cpus(trace_cleanup_on_cpu, &pc);
1106 tracefile_fini_arch();
1109 void tracefile_exit(void)
1111 trace_stop_thread();