4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2017, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
32 * libcfs/libcfs/tracefile.c
34 * Author: Zach Brown <zab@clusterfs.com>
35 * Author: Phil Schwan <phil@clusterfs.com>
39 #define DEBUG_SUBSYSTEM S_LNET
40 #define LUSTRE_TRACEFILE_PRIVATE
41 #include "tracefile.h"
43 #include <linux/ctype.h>
45 #include <linux/kthread.h>
46 #include <linux/pagemap.h>
47 #include <linux/uaccess.h>
48 #include <libcfs/linux/linux-fs.h>
49 #include <libcfs/linux/linux-misc.h>
50 #include <libcfs/libcfs.h>
52 /* XXX move things up to the top, comment */
53 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
55 char cfs_tracefile[TRACEFILE_NAME_SIZE];
56 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
57 static struct tracefiled_ctl trace_tctl;
58 static DEFINE_MUTEX(cfs_trace_thread_mutex);
59 static int thread_running = 0;
61 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
63 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
64 struct cfs_trace_cpu_data *tcd);
66 static inline struct cfs_trace_page *
67 cfs_tage_from_list(struct list_head *list)
69 return list_entry(list, struct cfs_trace_page, linkage);
72 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
75 struct cfs_trace_page *tage;
77 /* My caller is trying to free memory */
78 if (!in_interrupt() && memory_pressure_get())
82 * Don't spam console with allocation failures: they will be reported
83 * by upper layer anyway.
86 page = alloc_page(gfp);
90 tage = kmalloc(sizeof(*tage), gfp);
97 atomic_inc(&cfs_tage_allocated);
101 static void cfs_tage_free(struct cfs_trace_page *tage)
103 __LASSERT(tage != NULL);
104 __LASSERT(tage->page != NULL);
106 __free_page(tage->page);
108 atomic_dec(&cfs_tage_allocated);
111 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
112 struct list_head *queue)
114 __LASSERT(tage != NULL);
115 __LASSERT(queue != NULL);
117 list_move_tail(&tage->linkage, queue);
120 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
121 struct list_head *stock)
126 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
127 * from here: this will lead to infinite recursion.
130 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++ i) {
131 struct cfs_trace_page *tage;
133 tage = cfs_tage_alloc(gfp);
136 list_add_tail(&tage->linkage, stock);
141 /* return a page that has 'len' bytes left at the end */
142 static struct cfs_trace_page *
143 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
145 struct cfs_trace_page *tage;
147 if (tcd->tcd_cur_pages > 0) {
148 __LASSERT(!list_empty(&tcd->tcd_pages));
149 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
150 if (tage->used + len <= PAGE_SIZE)
154 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
155 if (tcd->tcd_cur_stock_pages > 0) {
156 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
157 --tcd->tcd_cur_stock_pages;
158 list_del_init(&tage->linkage);
160 tage = cfs_tage_alloc(GFP_ATOMIC);
161 if (unlikely(tage == NULL)) {
162 if ((!memory_pressure_get() ||
163 in_interrupt()) && printk_ratelimit())
165 "cannot allocate a tage (%ld)\n",
172 tage->cpu = smp_processor_id();
173 tage->type = tcd->tcd_type;
174 list_add_tail(&tage->linkage, &tcd->tcd_pages);
175 tcd->tcd_cur_pages++;
177 if (tcd->tcd_cur_pages > 8 && thread_running) {
178 struct tracefiled_ctl *tctl = &trace_tctl;
180 * wake up tracefiled to process some pages.
182 wake_up(&tctl->tctl_waitq);
189 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
191 int pgcount = tcd->tcd_cur_pages / 10;
192 struct page_collection pc;
193 struct cfs_trace_page *tage;
194 struct cfs_trace_page *tmp;
197 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
198 * from here: this will lead to infinite recursion.
201 if (printk_ratelimit())
202 printk(KERN_WARNING "debug daemon buffer overflowed; "
203 "discarding 10%% of pages (%d of %ld)\n",
204 pgcount + 1, tcd->tcd_cur_pages);
206 INIT_LIST_HEAD(&pc.pc_pages);
208 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
212 list_move_tail(&tage->linkage, &pc.pc_pages);
213 tcd->tcd_cur_pages--;
215 put_pages_on_tcd_daemon_list(&pc, tcd);
218 /* return a page that has 'len' bytes left at the end */
219 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
222 struct cfs_trace_page *tage;
225 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
226 * from here: this will lead to infinite recursion.
229 if (len > PAGE_SIZE) {
231 "cowardly refusing to write %lu bytes in a page\n", len);
235 tage = cfs_trace_get_tage_try(tcd, len);
240 if (tcd->tcd_cur_pages > 0) {
241 tage = cfs_tage_from_list(tcd->tcd_pages.next);
243 cfs_tage_to_tail(tage, &tcd->tcd_pages);
248 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
249 const char *format, ...)
254 va_start(args, format);
255 rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
260 EXPORT_SYMBOL(libcfs_debug_msg);
262 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
263 const char *format1, va_list args,
264 const char *format2, ...)
266 struct cfs_trace_cpu_data *tcd = NULL;
267 struct ptldebug_header header = {0};
268 struct cfs_trace_page *tage;
269 /* string_buf is used only if tcd != NULL, and is always set then */
270 char *string_buf = NULL;
273 int needed = 85; /* average message length */
278 int mask = msgdata->msg_mask;
279 char *file = (char *)msgdata->msg_file;
280 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
282 if (strchr(file, '/'))
283 file = strrchr(file, '/') + 1;
285 tcd = cfs_trace_get_tcd();
287 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
288 * pins us to a particular CPU. This avoids an smp_processor_id()
289 * warning on Linux when debugging is enabled. */
290 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
292 if (tcd == NULL) /* arch may not log in IRQ context */
295 if (tcd->tcd_cur_pages == 0)
296 header.ph_flags |= PH_FLAG_FIRST_RECORD;
298 if (tcd->tcd_shutting_down) {
299 cfs_trace_put_tcd(tcd);
304 known_size = strlen(file) + 1;
306 known_size += strlen(msgdata->msg_fn) + 1;
308 if (libcfs_debug_binary)
309 known_size += sizeof(header);
312 * '2' used because vsnprintf return real size required for output
313 * _without_ terminating NULL.
314 * if needed is to small for this format.
316 for (i = 0; i < 2; i++) {
317 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
319 if (needed + known_size > PAGE_SIZE)
322 cfs_trace_put_tcd(tcd);
327 string_buf = (char *)page_address(tage->page) +
328 tage->used + known_size;
330 max_nob = PAGE_SIZE - tage->used - known_size;
332 printk(KERN_EMERG "negative max_nob: %d\n",
335 cfs_trace_put_tcd(tcd);
343 needed = vsnprintf(string_buf, max_nob, format1, ap);
348 remain = max_nob - needed;
352 va_start(ap, format2);
353 needed += vsnprintf(string_buf + needed, remain,
358 if (needed < max_nob) /* well. printing ok.. */
362 if (*(string_buf+needed-1) != '\n')
363 printk(KERN_INFO "format at %s:%d:%s doesn't end in "
364 "newline\n", file, msgdata->msg_line, msgdata->msg_fn);
366 header.ph_len = known_size + needed;
367 debug_buf = (char *)page_address(tage->page) + tage->used;
369 if (libcfs_debug_binary) {
370 memcpy(debug_buf, &header, sizeof(header));
371 tage->used += sizeof(header);
372 debug_buf += sizeof(header);
375 strcpy(debug_buf, file);
376 tage->used += strlen(file) + 1;
377 debug_buf += strlen(file) + 1;
379 if (msgdata->msg_fn) {
380 strcpy(debug_buf, msgdata->msg_fn);
381 tage->used += strlen(msgdata->msg_fn) + 1;
382 debug_buf += strlen(msgdata->msg_fn) + 1;
385 __LASSERT(debug_buf == string_buf);
387 tage->used += needed;
388 __LASSERT(tage->used <= PAGE_SIZE);
391 if ((mask & libcfs_printk) == 0) {
392 /* no console output requested */
394 cfs_trace_put_tcd(tcd);
399 if (libcfs_console_ratelimit &&
400 cdls->cdls_next != 0 && /* not first time ever */
401 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
402 /* skipping a console message */
405 cfs_trace_put_tcd(tcd);
409 if (cfs_time_after(cfs_time_current(), cdls->cdls_next +
410 libcfs_console_max_delay
411 + cfs_time_seconds(10))) {
412 /* last timeout was a long time ago */
413 cdls->cdls_delay /= libcfs_console_backoff * 4;
415 cdls->cdls_delay *= libcfs_console_backoff;
418 if (cdls->cdls_delay < libcfs_console_min_delay)
419 cdls->cdls_delay = libcfs_console_min_delay;
420 else if (cdls->cdls_delay > libcfs_console_max_delay)
421 cdls->cdls_delay = libcfs_console_max_delay;
423 /* ensure cdls_next is never zero after it's been seen */
424 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
428 cfs_print_to_console(&header, mask, string_buf, needed, file,
430 cfs_trace_put_tcd(tcd);
432 string_buf = cfs_trace_get_console_buffer();
435 if (format1 != NULL) {
437 needed = vsnprintf(string_buf,
438 CFS_TRACE_CONSOLE_BUFFER_SIZE,
442 if (format2 != NULL) {
443 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
445 va_start(ap, format2);
446 needed += vsnprintf(string_buf+needed, remain,
451 cfs_print_to_console(&header, mask,
452 string_buf, needed, file, msgdata->msg_fn);
457 if (cdls != NULL && cdls->cdls_count != 0) {
458 string_buf = cfs_trace_get_console_buffer();
460 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
461 "Skipped %d previous similar message%s\n",
463 (cdls->cdls_count > 1) ? "s" : "");
465 cfs_print_to_console(&header, mask,
466 string_buf, needed, file, msgdata->msg_fn);
469 cdls->cdls_count = 0;
474 EXPORT_SYMBOL(libcfs_debug_vmsg2);
477 cfs_trace_assertion_failed(const char *str,
478 struct libcfs_debug_msg_data *msgdata)
480 struct ptldebug_header hdr;
482 libcfs_panic_in_progress = 1;
483 libcfs_catastrophe = 1;
486 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
488 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
489 msgdata->msg_file, msgdata->msg_fn);
491 panic("Lustre debug assertion failure\n");
497 panic_collect_pages(struct page_collection *pc)
499 /* Do the collect_pages job on a single CPU: assumes that all other
500 * CPUs have been stopped during a panic. If this isn't true for some
501 * arch, this will have to be implemented separately in each arch. */
504 struct cfs_trace_cpu_data *tcd;
506 INIT_LIST_HEAD(&pc->pc_pages);
508 cfs_tcd_for_each(tcd, i, j) {
509 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
510 tcd->tcd_cur_pages = 0;
512 if (pc->pc_want_daemon_pages) {
513 list_splice_init(&tcd->tcd_daemon_pages,
515 tcd->tcd_cur_daemon_pages = 0;
520 static void collect_pages_on_all_cpus(struct page_collection *pc)
522 struct cfs_trace_cpu_data *tcd;
525 for_each_possible_cpu(cpu) {
526 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
527 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
528 tcd->tcd_cur_pages = 0;
529 if (pc->pc_want_daemon_pages) {
530 list_splice_init(&tcd->tcd_daemon_pages,
532 tcd->tcd_cur_daemon_pages = 0;
538 static void collect_pages(struct page_collection *pc)
540 INIT_LIST_HEAD(&pc->pc_pages);
542 if (libcfs_panic_in_progress)
543 panic_collect_pages(pc);
545 collect_pages_on_all_cpus(pc);
548 static void put_pages_back_on_all_cpus(struct page_collection *pc)
550 struct cfs_trace_cpu_data *tcd;
551 struct list_head *cur_head;
552 struct cfs_trace_page *tage;
553 struct cfs_trace_page *tmp;
556 for_each_possible_cpu(cpu) {
557 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
558 cur_head = tcd->tcd_pages.next;
560 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
563 __LASSERT_TAGE_INVARIANT(tage);
565 if (tage->cpu != cpu || tage->type != i)
568 cfs_tage_to_tail(tage, cur_head);
569 tcd->tcd_cur_pages++;
575 static void put_pages_back(struct page_collection *pc)
577 if (!libcfs_panic_in_progress)
578 put_pages_back_on_all_cpus(pc);
581 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
582 * we have a good amount of data at all times for dumping during an LBUG, even
583 * if we have been steadily writing (and otherwise discarding) pages via the
585 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
586 struct cfs_trace_cpu_data *tcd)
588 struct cfs_trace_page *tage;
589 struct cfs_trace_page *tmp;
591 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
592 __LASSERT_TAGE_INVARIANT(tage);
594 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
597 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
598 tcd->tcd_cur_daemon_pages++;
600 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
601 struct cfs_trace_page *victim;
603 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
604 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
606 __LASSERT_TAGE_INVARIANT(victim);
608 list_del(&victim->linkage);
609 cfs_tage_free(victim);
610 tcd->tcd_cur_daemon_pages--;
615 static void put_pages_on_daemon_list(struct page_collection *pc)
617 struct cfs_trace_cpu_data *tcd;
620 for_each_possible_cpu(cpu) {
621 cfs_tcd_for_each_type_lock(tcd, i, cpu)
622 put_pages_on_tcd_daemon_list(pc, tcd);
626 void cfs_trace_debug_print(void)
628 struct page_collection pc;
629 struct cfs_trace_page *tage;
630 struct cfs_trace_page *tmp;
632 pc.pc_want_daemon_pages = 1;
634 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
638 __LASSERT_TAGE_INVARIANT(tage);
641 p = page_address(page);
642 while (p < ((char *)page_address(page) + tage->used)) {
643 struct ptldebug_header *hdr;
648 p += strlen(file) + 1;
651 len = hdr->ph_len - (int)(p - (char *)hdr);
653 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
658 list_del(&tage->linkage);
663 int cfs_tracefile_dump_all_pages(char *filename)
665 struct page_collection pc;
667 struct cfs_trace_page *tage;
668 struct cfs_trace_page *tmp;
672 cfs_tracefile_write_lock();
674 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
678 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
683 pc.pc_want_daemon_pages = 1;
685 if (list_empty(&pc.pc_pages)) {
690 /* ok, for now, just write the pages. in the future we'll be building
691 * iobufs with the pages and calling generic_direct_IO */
692 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
694 __LASSERT_TAGE_INVARIANT(tage);
696 buf = kmap(tage->page);
697 rc = cfs_kernel_write(filp, buf, tage->used, &filp->f_pos);
699 if (rc != (int)tage->used) {
700 printk(KERN_WARNING "wanted to write %u but wrote "
701 "%d\n", tage->used, rc);
703 __LASSERT(list_empty(&pc.pc_pages));
706 list_del(&tage->linkage);
710 rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
712 printk(KERN_ERR "sync returns %d\n", rc);
714 filp_close(filp, NULL);
716 cfs_tracefile_write_unlock();
720 void cfs_trace_flush_pages(void)
722 struct page_collection pc;
723 struct cfs_trace_page *tage;
724 struct cfs_trace_page *tmp;
726 pc.pc_want_daemon_pages = 1;
728 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
730 __LASSERT_TAGE_INVARIANT(tage);
732 list_del(&tage->linkage);
737 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
738 const char __user *usr_buffer, int usr_buffer_nob)
742 if (usr_buffer_nob > knl_buffer_nob)
745 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
748 nob = strnlen(knl_buffer, usr_buffer_nob);
749 while (nob-- >= 0) /* strip trailing whitespace */
750 if (!isspace(knl_buffer[nob]))
753 if (nob < 0) /* empty string */
756 if (nob == knl_buffer_nob) /* no space to terminate */
759 knl_buffer[nob + 1] = 0; /* terminate */
762 EXPORT_SYMBOL(cfs_trace_copyin_string);
764 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
765 const char *knl_buffer, char *append)
767 /* NB if 'append' != NULL, it's a single character to append to the
768 * copied out string - usually "\n", for /proc entries and "" (i.e. a
769 * terminating zero byte) for sysctl entries */
770 int nob = strlen(knl_buffer);
772 if (nob > usr_buffer_nob)
773 nob = usr_buffer_nob;
775 if (copy_to_user(usr_buffer, knl_buffer, nob))
778 if (append != NULL && nob < usr_buffer_nob) {
779 if (copy_to_user(usr_buffer + nob, append, 1))
787 EXPORT_SYMBOL(cfs_trace_copyout_string);
789 int cfs_trace_allocate_string_buffer(char **str, int nob)
791 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
794 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
801 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
806 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
810 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
811 usr_str, usr_str_nob);
819 rc = cfs_tracefile_dump_all_pages(str);
825 int cfs_trace_daemon_command(char *str)
829 cfs_tracefile_write_lock();
831 if (strcmp(str, "stop") == 0) {
832 cfs_tracefile_write_unlock();
833 cfs_trace_stop_thread();
834 cfs_tracefile_write_lock();
835 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
837 } else if (strncmp(str, "size=", 5) == 0) {
838 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
839 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
840 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
842 cfs_tracefile_size <<= 20;
844 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
846 } else if (str[0] != '/') {
849 strcpy(cfs_tracefile, str);
852 "Lustre: debug daemon will attempt to start writing "
853 "to %s (%lukB max)\n", cfs_tracefile,
854 (long)(cfs_tracefile_size >> 10));
856 cfs_trace_start_thread();
859 cfs_tracefile_write_unlock();
863 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
868 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
872 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
873 usr_str, usr_str_nob);
875 rc = cfs_trace_daemon_command(str);
881 int cfs_trace_set_debug_mb(int mb)
886 int limit = cfs_trace_max_debug_mb();
887 struct cfs_trace_cpu_data *tcd;
889 if (mb < num_possible_cpus()) {
891 "Lustre: %d MB is too small for debug buffer size, "
892 "setting it to %d MB.\n", mb, num_possible_cpus());
893 mb = num_possible_cpus();
898 "Lustre: %d MB is too large for debug buffer size, "
899 "setting it to %d MB.\n", mb, limit);
903 mb /= num_possible_cpus();
904 pages = mb << (20 - PAGE_SHIFT);
906 cfs_tracefile_write_lock();
908 cfs_tcd_for_each(tcd, i, j)
909 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
911 cfs_tracefile_write_unlock();
916 int cfs_trace_get_debug_mb(void)
920 struct cfs_trace_cpu_data *tcd;
923 cfs_tracefile_read_lock();
925 cfs_tcd_for_each(tcd, i, j)
926 total_pages += tcd->tcd_max_pages;
928 cfs_tracefile_read_unlock();
930 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
933 static int tracefiled(void *arg)
935 struct page_collection pc;
936 struct tracefiled_ctl *tctl = arg;
937 struct cfs_trace_page *tage;
938 struct cfs_trace_page *tmp;
944 /* we're started late enough that we pick up init's fs context */
945 /* this is so broken in uml? what on earth is going on? */
947 complete(&tctl->tctl_start);
950 wait_queue_entry_t __wait;
952 pc.pc_want_daemon_pages = 0;
954 if (list_empty(&pc.pc_pages))
958 cfs_tracefile_read_lock();
959 if (cfs_tracefile[0] != 0) {
960 filp = filp_open(cfs_tracefile,
961 O_CREAT | O_RDWR | O_LARGEFILE,
966 printk(KERN_WARNING "couldn't open %s: "
967 "%d\n", cfs_tracefile, rc);
970 cfs_tracefile_read_unlock();
972 put_pages_on_daemon_list(&pc);
973 __LASSERT(list_empty(&pc.pc_pages));
977 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
978 struct dentry *de = file_dentry(filp);
981 __LASSERT_TAGE_INVARIANT(tage);
983 if (f_pos >= (off_t)cfs_tracefile_size)
985 else if (f_pos > i_size_read(de->d_inode))
986 f_pos = i_size_read(de->d_inode);
988 buf = kmap(tage->page);
989 rc = cfs_kernel_write(filp, buf, tage->used, &f_pos);
991 if (rc != (int)tage->used) {
992 printk(KERN_WARNING "wanted to write %u "
993 "but wrote %d\n", tage->used, rc);
995 __LASSERT(list_empty(&pc.pc_pages));
1000 filp_close(filp, NULL);
1001 put_pages_on_daemon_list(&pc);
1002 if (!list_empty(&pc.pc_pages)) {
1005 printk(KERN_ALERT "Lustre: trace pages aren't "
1007 printk(KERN_ERR "total cpus(%d): ",
1008 num_possible_cpus());
1009 for (i = 0; i < num_possible_cpus(); i++)
1011 printk(KERN_ERR "%d(on) ", i);
1013 printk(KERN_ERR "%d(off) ", i);
1014 printk(KERN_ERR "\n");
1017 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1019 printk(KERN_ERR "page %d belongs to cpu "
1020 "%d\n", ++i, tage->cpu);
1021 printk(KERN_ERR "There are %d pages unwritten\n",
1024 __LASSERT(list_empty(&pc.pc_pages));
1026 if (atomic_read(&tctl->tctl_shutdown)) {
1027 if (last_loop == 0) {
1034 init_waitqueue_entry(&__wait, current);
1035 add_wait_queue(&tctl->tctl_waitq, &__wait);
1036 set_current_state(TASK_INTERRUPTIBLE);
1037 schedule_timeout(cfs_time_seconds(1));
1038 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1040 complete(&tctl->tctl_stop);
1044 int cfs_trace_start_thread(void)
1046 struct tracefiled_ctl *tctl = &trace_tctl;
1049 mutex_lock(&cfs_trace_thread_mutex);
1053 init_completion(&tctl->tctl_start);
1054 init_completion(&tctl->tctl_stop);
1055 init_waitqueue_head(&tctl->tctl_waitq);
1056 atomic_set(&tctl->tctl_shutdown, 0);
1058 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1063 wait_for_completion(&tctl->tctl_start);
1066 mutex_unlock(&cfs_trace_thread_mutex);
1070 void cfs_trace_stop_thread(void)
1072 struct tracefiled_ctl *tctl = &trace_tctl;
1074 mutex_lock(&cfs_trace_thread_mutex);
1075 if (thread_running) {
1077 "Lustre: shutting down debug daemon thread...\n");
1078 atomic_set(&tctl->tctl_shutdown, 1);
1079 wait_for_completion(&tctl->tctl_stop);
1082 mutex_unlock(&cfs_trace_thread_mutex);
1085 int cfs_tracefile_init(int max_pages)
1087 struct cfs_trace_cpu_data *tcd;
1093 rc = cfs_tracefile_init_arch();
1097 cfs_tcd_for_each(tcd, i, j) {
1098 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1099 factor = tcd->tcd_pages_factor;
1100 INIT_LIST_HEAD(&tcd->tcd_pages);
1101 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1102 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1103 tcd->tcd_cur_pages = 0;
1104 tcd->tcd_cur_stock_pages = 0;
1105 tcd->tcd_cur_daemon_pages = 0;
1106 tcd->tcd_max_pages = (max_pages * factor) / 100;
1107 LASSERT(tcd->tcd_max_pages > 0);
1108 tcd->tcd_shutting_down = 0;
1113 static void trace_cleanup_on_all_cpus(void)
1115 struct cfs_trace_cpu_data *tcd;
1116 struct cfs_trace_page *tage;
1117 struct cfs_trace_page *tmp;
1120 for_each_possible_cpu(cpu) {
1121 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1122 tcd->tcd_shutting_down = 1;
1124 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1125 __LASSERT_TAGE_INVARIANT(tage);
1127 list_del(&tage->linkage);
1128 cfs_tage_free(tage);
1130 tcd->tcd_cur_pages = 0;
1135 static void cfs_trace_cleanup(void)
1137 struct page_collection pc;
1139 INIT_LIST_HEAD(&pc.pc_pages);
1141 trace_cleanup_on_all_cpus();
1143 cfs_tracefile_fini_arch();
1146 void cfs_tracefile_exit(void)
1148 cfs_trace_stop_thread();
1149 cfs_trace_cleanup();