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;
669 mm_segment_t __oldfs;
673 cfs_tracefile_write_lock();
675 filp = filp_open(filename, O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600);
679 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
684 pc.pc_want_daemon_pages = 1;
686 if (list_empty(&pc.pc_pages)) {
693 /* ok, for now, just write the pages. in the future we'll be building
694 * iobufs with the pages and calling generic_direct_IO */
695 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
697 __LASSERT_TAGE_INVARIANT(tage);
699 buf = kmap(tage->page);
700 rc = vfs_write(filp, (__force const char __user *)buf,
701 tage->used, &filp->f_pos);
703 if (rc != (int)tage->used) {
704 printk(KERN_WARNING "wanted to write %u but wrote "
705 "%d\n", tage->used, rc);
707 __LASSERT(list_empty(&pc.pc_pages));
710 list_del(&tage->linkage);
714 rc = ll_vfs_fsync_range(filp, 0, LLONG_MAX, 1);
716 printk(KERN_ERR "sync returns %d\n", rc);
718 filp_close(filp, NULL);
720 cfs_tracefile_write_unlock();
724 void cfs_trace_flush_pages(void)
726 struct page_collection pc;
727 struct cfs_trace_page *tage;
728 struct cfs_trace_page *tmp;
730 pc.pc_want_daemon_pages = 1;
732 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
734 __LASSERT_TAGE_INVARIANT(tage);
736 list_del(&tage->linkage);
741 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
742 const char __user *usr_buffer, int usr_buffer_nob)
746 if (usr_buffer_nob > knl_buffer_nob)
749 if (copy_from_user(knl_buffer, usr_buffer, usr_buffer_nob))
752 nob = strnlen(knl_buffer, usr_buffer_nob);
753 while (nob-- >= 0) /* strip trailing whitespace */
754 if (!isspace(knl_buffer[nob]))
757 if (nob < 0) /* empty string */
760 if (nob == knl_buffer_nob) /* no space to terminate */
763 knl_buffer[nob + 1] = 0; /* terminate */
766 EXPORT_SYMBOL(cfs_trace_copyin_string);
768 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
769 const char *knl_buffer, char *append)
771 /* NB if 'append' != NULL, it's a single character to append to the
772 * copied out string - usually "\n", for /proc entries and "" (i.e. a
773 * terminating zero byte) for sysctl entries */
774 int nob = strlen(knl_buffer);
776 if (nob > usr_buffer_nob)
777 nob = usr_buffer_nob;
779 if (copy_to_user(usr_buffer, knl_buffer, nob))
782 if (append != NULL && nob < usr_buffer_nob) {
783 if (copy_to_user(usr_buffer + nob, append, 1))
791 EXPORT_SYMBOL(cfs_trace_copyout_string);
793 int cfs_trace_allocate_string_buffer(char **str, int nob)
795 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
798 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
805 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
810 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
814 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
815 usr_str, usr_str_nob);
823 rc = cfs_tracefile_dump_all_pages(str);
829 int cfs_trace_daemon_command(char *str)
833 cfs_tracefile_write_lock();
835 if (strcmp(str, "stop") == 0) {
836 cfs_tracefile_write_unlock();
837 cfs_trace_stop_thread();
838 cfs_tracefile_write_lock();
839 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
841 } else if (strncmp(str, "size=", 5) == 0) {
842 cfs_tracefile_size = simple_strtoul(str + 5, NULL, 0);
843 if (cfs_tracefile_size < 10 || cfs_tracefile_size > 20480)
844 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
846 cfs_tracefile_size <<= 20;
848 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
850 } else if (str[0] != '/') {
853 strcpy(cfs_tracefile, str);
856 "Lustre: debug daemon will attempt to start writing "
857 "to %s (%lukB max)\n", cfs_tracefile,
858 (long)(cfs_tracefile_size >> 10));
860 cfs_trace_start_thread();
863 cfs_tracefile_write_unlock();
867 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
872 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
876 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
877 usr_str, usr_str_nob);
879 rc = cfs_trace_daemon_command(str);
885 int cfs_trace_set_debug_mb(int mb)
890 int limit = cfs_trace_max_debug_mb();
891 struct cfs_trace_cpu_data *tcd;
893 if (mb < num_possible_cpus()) {
895 "Lustre: %d MB is too small for debug buffer size, "
896 "setting it to %d MB.\n", mb, num_possible_cpus());
897 mb = num_possible_cpus();
902 "Lustre: %d MB is too large for debug buffer size, "
903 "setting it to %d MB.\n", mb, limit);
907 mb /= num_possible_cpus();
908 pages = mb << (20 - PAGE_SHIFT);
910 cfs_tracefile_write_lock();
912 cfs_tcd_for_each(tcd, i, j)
913 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
915 cfs_tracefile_write_unlock();
920 int cfs_trace_get_debug_mb(void)
924 struct cfs_trace_cpu_data *tcd;
927 cfs_tracefile_read_lock();
929 cfs_tcd_for_each(tcd, i, j)
930 total_pages += tcd->tcd_max_pages;
932 cfs_tracefile_read_unlock();
934 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
937 static int tracefiled(void *arg)
939 struct page_collection pc;
940 struct tracefiled_ctl *tctl = arg;
941 struct cfs_trace_page *tage;
942 struct cfs_trace_page *tmp;
943 mm_segment_t __oldfs;
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? */
952 complete(&tctl->tctl_start);
955 wait_queue_entry_t __wait;
957 pc.pc_want_daemon_pages = 0;
959 if (list_empty(&pc.pc_pages))
963 cfs_tracefile_read_lock();
964 if (cfs_tracefile[0] != 0) {
965 filp = filp_open(cfs_tracefile,
966 O_CREAT | O_RDWR | O_LARGEFILE,
971 printk(KERN_WARNING "couldn't open %s: "
972 "%d\n", cfs_tracefile, rc);
975 cfs_tracefile_read_unlock();
977 put_pages_on_daemon_list(&pc);
978 __LASSERT(list_empty(&pc.pc_pages));
984 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
985 struct dentry *de = file_dentry(filp);
988 __LASSERT_TAGE_INVARIANT(tage);
990 if (f_pos >= (off_t)cfs_tracefile_size)
992 else if (f_pos > i_size_read(de->d_inode))
993 f_pos = i_size_read(de->d_inode);
995 buf = kmap(tage->page);
996 rc = vfs_write(filp, (__force const char __user *)buf,
999 if (rc != (int)tage->used) {
1000 printk(KERN_WARNING "wanted to write %u "
1001 "but wrote %d\n", tage->used, rc);
1002 put_pages_back(&pc);
1003 __LASSERT(list_empty(&pc.pc_pages));
1009 filp_close(filp, NULL);
1010 put_pages_on_daemon_list(&pc);
1011 if (!list_empty(&pc.pc_pages)) {
1014 printk(KERN_ALERT "Lustre: trace pages aren't "
1016 printk(KERN_ERR "total cpus(%d): ",
1017 num_possible_cpus());
1018 for (i = 0; i < num_possible_cpus(); i++)
1020 printk(KERN_ERR "%d(on) ", i);
1022 printk(KERN_ERR "%d(off) ", i);
1023 printk(KERN_ERR "\n");
1026 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1028 printk(KERN_ERR "page %d belongs to cpu "
1029 "%d\n", ++i, tage->cpu);
1030 printk(KERN_ERR "There are %d pages unwritten\n",
1033 __LASSERT(list_empty(&pc.pc_pages));
1035 if (atomic_read(&tctl->tctl_shutdown)) {
1036 if (last_loop == 0) {
1043 init_waitqueue_entry(&__wait, current);
1044 add_wait_queue(&tctl->tctl_waitq, &__wait);
1045 set_current_state(TASK_INTERRUPTIBLE);
1046 schedule_timeout(cfs_time_seconds(1));
1047 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1049 complete(&tctl->tctl_stop);
1053 int cfs_trace_start_thread(void)
1055 struct tracefiled_ctl *tctl = &trace_tctl;
1058 mutex_lock(&cfs_trace_thread_mutex);
1062 init_completion(&tctl->tctl_start);
1063 init_completion(&tctl->tctl_stop);
1064 init_waitqueue_head(&tctl->tctl_waitq);
1065 atomic_set(&tctl->tctl_shutdown, 0);
1067 if (IS_ERR(kthread_run(tracefiled, tctl, "ktracefiled"))) {
1072 wait_for_completion(&tctl->tctl_start);
1075 mutex_unlock(&cfs_trace_thread_mutex);
1079 void cfs_trace_stop_thread(void)
1081 struct tracefiled_ctl *tctl = &trace_tctl;
1083 mutex_lock(&cfs_trace_thread_mutex);
1084 if (thread_running) {
1086 "Lustre: shutting down debug daemon thread...\n");
1087 atomic_set(&tctl->tctl_shutdown, 1);
1088 wait_for_completion(&tctl->tctl_stop);
1091 mutex_unlock(&cfs_trace_thread_mutex);
1094 int cfs_tracefile_init(int max_pages)
1096 struct cfs_trace_cpu_data *tcd;
1102 rc = cfs_tracefile_init_arch();
1106 cfs_tcd_for_each(tcd, i, j) {
1107 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1108 factor = tcd->tcd_pages_factor;
1109 INIT_LIST_HEAD(&tcd->tcd_pages);
1110 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1111 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1112 tcd->tcd_cur_pages = 0;
1113 tcd->tcd_cur_stock_pages = 0;
1114 tcd->tcd_cur_daemon_pages = 0;
1115 tcd->tcd_max_pages = (max_pages * factor) / 100;
1116 LASSERT(tcd->tcd_max_pages > 0);
1117 tcd->tcd_shutting_down = 0;
1122 static void trace_cleanup_on_all_cpus(void)
1124 struct cfs_trace_cpu_data *tcd;
1125 struct cfs_trace_page *tage;
1126 struct cfs_trace_page *tmp;
1129 for_each_possible_cpu(cpu) {
1130 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1131 tcd->tcd_shutting_down = 1;
1133 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
1134 __LASSERT_TAGE_INVARIANT(tage);
1136 list_del(&tage->linkage);
1137 cfs_tage_free(tage);
1139 tcd->tcd_cur_pages = 0;
1144 static void cfs_trace_cleanup(void)
1146 struct page_collection pc;
1148 INIT_LIST_HEAD(&pc.pc_pages);
1150 trace_cleanup_on_all_cpus();
1152 cfs_tracefile_fini_arch();
1155 void cfs_tracefile_exit(void)
1157 cfs_trace_stop_thread();
1158 cfs_trace_cleanup();