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, 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>
38 #define DEBUG_SUBSYSTEM S_LNET
39 #define LUSTRE_TRACEFILE_PRIVATE
40 #define pr_fmt(fmt) "Lustre: " fmt
41 #include "tracefile.h"
43 #include <linux/libcfs/libcfs.h>
45 /* XXX move things up to the top, comment */
46 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
48 char cfs_tracefile[TRACEFILE_NAME_SIZE];
49 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
50 static struct tracefiled_ctl trace_tctl;
51 static DEFINE_MUTEX(cfs_trace_thread_mutex);
52 static int thread_running;
54 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
56 struct page_collection {
57 struct list_head pc_pages;
59 * if this flag is set, collect_pages() will spill both
60 * ->tcd_daemon_pages and ->tcd_pages to the ->pc_pages. Otherwise,
61 * only ->tcd_pages are spilled.
63 int pc_want_daemon_pages;
66 struct tracefiled_ctl {
67 struct completion tctl_start;
68 struct completion tctl_stop;
69 wait_queue_head_t tctl_waitq;
71 atomic_t tctl_shutdown;
75 * small data-structure for each page owned by tracefiled.
77 struct cfs_trace_page {
83 * linkage into one of the lists in trace_data_union or
86 struct list_head linkage;
88 * number of bytes used within this page
92 * cpu that owns this page
96 * type(context) of this page
101 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
102 struct cfs_trace_cpu_data *tcd);
104 static inline struct cfs_trace_page *
105 cfs_tage_from_list(struct list_head *list)
107 return list_entry(list, struct cfs_trace_page, linkage);
110 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
113 struct cfs_trace_page *tage;
115 /* My caller is trying to free memory */
116 if (!in_interrupt() && memory_pressure_get())
120 * Don't spam console with allocation failures: they will be reported
121 * by upper layer anyway.
124 page = alloc_page(gfp);
128 tage = kmalloc(sizeof(*tage), gfp);
135 atomic_inc(&cfs_tage_allocated);
139 static void cfs_tage_free(struct cfs_trace_page *tage)
141 __free_page(tage->page);
143 atomic_dec(&cfs_tage_allocated);
146 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
147 struct list_head *queue)
149 list_move_tail(&tage->linkage, queue);
152 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
153 struct list_head *stock)
158 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
159 * from here: this will lead to infinite recursion.
162 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++i) {
163 struct cfs_trace_page *tage;
165 tage = cfs_tage_alloc(gfp);
168 list_add_tail(&tage->linkage, stock);
173 /* return a page that has 'len' bytes left at the end */
174 static struct cfs_trace_page *
175 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
177 struct cfs_trace_page *tage;
179 if (tcd->tcd_cur_pages > 0) {
180 __LASSERT(!list_empty(&tcd->tcd_pages));
181 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
182 if (tage->used + len <= PAGE_SIZE)
186 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
187 if (tcd->tcd_cur_stock_pages > 0) {
188 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
189 --tcd->tcd_cur_stock_pages;
190 list_del_init(&tage->linkage);
192 tage = cfs_tage_alloc(GFP_ATOMIC);
193 if (unlikely(!tage)) {
194 if (!memory_pressure_get() || in_interrupt())
195 pr_warn_ratelimited("cannot allocate a tage (%ld)\n",
202 tage->cpu = smp_processor_id();
203 tage->type = tcd->tcd_type;
204 list_add_tail(&tage->linkage, &tcd->tcd_pages);
205 tcd->tcd_cur_pages++;
207 if (tcd->tcd_cur_pages > 8 && thread_running) {
208 struct tracefiled_ctl *tctl = &trace_tctl;
210 * wake up tracefiled to process some pages.
212 wake_up(&tctl->tctl_waitq);
219 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
221 int pgcount = tcd->tcd_cur_pages / 10;
222 struct page_collection pc;
223 struct cfs_trace_page *tage;
224 struct cfs_trace_page *tmp;
227 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
228 * from here: this will lead to infinite recursion.
231 pr_warn_ratelimited("debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
232 pgcount + 1, tcd->tcd_cur_pages);
234 INIT_LIST_HEAD(&pc.pc_pages);
236 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
240 list_move_tail(&tage->linkage, &pc.pc_pages);
241 tcd->tcd_cur_pages--;
243 put_pages_on_tcd_daemon_list(&pc, tcd);
246 /* return a page that has 'len' bytes left at the end */
247 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
250 struct cfs_trace_page *tage;
253 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
254 * from here: this will lead to infinite recursion.
257 if (len > PAGE_SIZE) {
258 pr_err("cowardly refusing to write %lu bytes in a page\n", len);
262 tage = cfs_trace_get_tage_try(tcd, len);
267 if (tcd->tcd_cur_pages > 0) {
268 tage = cfs_tage_from_list(tcd->tcd_pages.next);
270 cfs_tage_to_tail(tage, &tcd->tcd_pages);
275 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
276 const char *format, ...)
281 va_start(args, format);
282 rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
287 EXPORT_SYMBOL(libcfs_debug_msg);
289 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
290 const char *format1, va_list args,
291 const char *format2, ...)
293 struct cfs_trace_cpu_data *tcd = NULL;
294 struct ptldebug_header header = { 0 };
295 struct cfs_trace_page *tage;
296 /* string_buf is used only if tcd != NULL, and is always set then */
297 char *string_buf = NULL;
300 int needed = 85; /* average message length */
306 int mask = msgdata->msg_mask;
307 const char *file = kbasename(msgdata->msg_file);
308 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
310 tcd = cfs_trace_get_tcd();
312 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
313 * pins us to a particular CPU. This avoids an smp_processor_id()
314 * warning on Linux when debugging is enabled.
316 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
318 if (!tcd) /* arch may not log in IRQ context */
321 if (!tcd->tcd_cur_pages)
322 header.ph_flags |= PH_FLAG_FIRST_RECORD;
324 if (tcd->tcd_shutting_down) {
325 cfs_trace_put_tcd(tcd);
330 depth = __current_nesting_level();
331 known_size = strlen(file) + 1 + depth;
333 known_size += strlen(msgdata->msg_fn) + 1;
335 if (libcfs_debug_binary)
336 known_size += sizeof(header);
339 * '2' used because vsnprintf return real size required for output
340 * _without_ terminating NULL.
341 * if needed is to small for this format.
343 for (i = 0; i < 2; i++) {
344 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
346 if (needed + known_size > PAGE_SIZE)
349 cfs_trace_put_tcd(tcd);
354 string_buf = (char *)page_address(tage->page) +
355 tage->used + known_size;
357 max_nob = PAGE_SIZE - tage->used - known_size;
359 pr_emerg("negative max_nob: %d\n", max_nob);
361 cfs_trace_put_tcd(tcd);
369 needed = vsnprintf(string_buf, max_nob, format1, ap);
374 remain = max_nob - needed;
378 va_start(ap, format2);
379 needed += vsnprintf(string_buf + needed, remain,
384 if (needed < max_nob) /* well. printing ok.. */
388 if (*(string_buf + needed - 1) != '\n')
389 pr_info("format at %s:%d:%s doesn't end in newline\n", file,
390 msgdata->msg_line, msgdata->msg_fn);
392 header.ph_len = known_size + needed;
393 debug_buf = (char *)page_address(tage->page) + tage->used;
395 if (libcfs_debug_binary) {
396 memcpy(debug_buf, &header, sizeof(header));
397 tage->used += sizeof(header);
398 debug_buf += sizeof(header);
401 /* indent message according to the nesting level */
402 while (depth-- > 0) {
403 *(debug_buf++) = '.';
407 strcpy(debug_buf, file);
408 tage->used += strlen(file) + 1;
409 debug_buf += strlen(file) + 1;
411 if (msgdata->msg_fn) {
412 strcpy(debug_buf, msgdata->msg_fn);
413 tage->used += strlen(msgdata->msg_fn) + 1;
414 debug_buf += strlen(msgdata->msg_fn) + 1;
417 __LASSERT(debug_buf == string_buf);
419 tage->used += needed;
420 __LASSERT(tage->used <= PAGE_SIZE);
423 if (!(mask & libcfs_printk)) {
424 /* no console output requested */
426 cfs_trace_put_tcd(tcd);
431 if (libcfs_console_ratelimit &&
432 cdls->cdls_next && /* not first time ever */
433 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
434 /* skipping a console message */
437 cfs_trace_put_tcd(tcd);
441 if (cfs_time_after(cfs_time_current(),
442 cdls->cdls_next + libcfs_console_max_delay +
443 cfs_time_seconds(10))) {
444 /* last timeout was a long time ago */
445 cdls->cdls_delay /= libcfs_console_backoff * 4;
447 cdls->cdls_delay *= libcfs_console_backoff;
450 if (cdls->cdls_delay < libcfs_console_min_delay)
451 cdls->cdls_delay = libcfs_console_min_delay;
452 else if (cdls->cdls_delay > libcfs_console_max_delay)
453 cdls->cdls_delay = libcfs_console_max_delay;
455 /* ensure cdls_next is never zero after it's been seen */
456 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
460 cfs_print_to_console(&header, mask, string_buf, needed, file,
462 cfs_trace_put_tcd(tcd);
464 string_buf = cfs_trace_get_console_buffer();
469 needed = vsnprintf(string_buf,
470 CFS_TRACE_CONSOLE_BUFFER_SIZE,
475 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
477 va_start(ap, format2);
478 needed += vsnprintf(string_buf + needed, remain,
483 cfs_print_to_console(&header, mask,
484 string_buf, needed, file, msgdata->msg_fn);
489 if (cdls && cdls->cdls_count) {
490 string_buf = cfs_trace_get_console_buffer();
492 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
493 "Skipped %d previous similar message%s\n",
495 (cdls->cdls_count > 1) ? "s" : "");
497 cfs_print_to_console(&header, mask,
498 string_buf, needed, file, msgdata->msg_fn);
501 cdls->cdls_count = 0;
506 EXPORT_SYMBOL(libcfs_debug_vmsg2);
509 cfs_trace_assertion_failed(const char *str,
510 struct libcfs_debug_msg_data *msgdata)
512 struct ptldebug_header hdr;
514 libcfs_panic_in_progress = 1;
515 libcfs_catastrophe = 1;
518 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
520 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
521 msgdata->msg_file, msgdata->msg_fn);
523 panic("Lustre debug assertion failure\n");
529 panic_collect_pages(struct page_collection *pc)
531 /* Do the collect_pages job on a single CPU: assumes that all other
532 * CPUs have been stopped during a panic. If this isn't true for some
533 * arch, this will have to be implemented separately in each arch.
535 struct cfs_trace_cpu_data *tcd;
539 INIT_LIST_HEAD(&pc->pc_pages);
541 cfs_tcd_for_each(tcd, i, j) {
542 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
543 tcd->tcd_cur_pages = 0;
545 if (pc->pc_want_daemon_pages) {
546 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
547 tcd->tcd_cur_daemon_pages = 0;
552 static void collect_pages_on_all_cpus(struct page_collection *pc)
554 struct cfs_trace_cpu_data *tcd;
557 for_each_possible_cpu(cpu) {
558 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
559 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
560 tcd->tcd_cur_pages = 0;
561 if (pc->pc_want_daemon_pages) {
562 list_splice_init(&tcd->tcd_daemon_pages,
564 tcd->tcd_cur_daemon_pages = 0;
570 static void collect_pages(struct page_collection *pc)
572 INIT_LIST_HEAD(&pc->pc_pages);
574 if (libcfs_panic_in_progress)
575 panic_collect_pages(pc);
577 collect_pages_on_all_cpus(pc);
580 static void put_pages_back_on_all_cpus(struct page_collection *pc)
582 struct cfs_trace_cpu_data *tcd;
583 struct list_head *cur_head;
584 struct cfs_trace_page *tage;
585 struct cfs_trace_page *tmp;
588 for_each_possible_cpu(cpu) {
589 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
590 cur_head = tcd->tcd_pages.next;
592 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
594 __LASSERT_TAGE_INVARIANT(tage);
596 if (tage->cpu != cpu || tage->type != i)
599 cfs_tage_to_tail(tage, cur_head);
600 tcd->tcd_cur_pages++;
606 static void put_pages_back(struct page_collection *pc)
608 if (!libcfs_panic_in_progress)
609 put_pages_back_on_all_cpus(pc);
612 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
613 * we have a good amount of data at all times for dumping during an LBUG, even
614 * if we have been steadily writing (and otherwise discarding) pages via the
617 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
618 struct cfs_trace_cpu_data *tcd)
620 struct cfs_trace_page *tage;
621 struct cfs_trace_page *tmp;
623 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
624 __LASSERT_TAGE_INVARIANT(tage);
626 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
629 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
630 tcd->tcd_cur_daemon_pages++;
632 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
633 struct cfs_trace_page *victim;
635 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
636 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
638 __LASSERT_TAGE_INVARIANT(victim);
640 list_del(&victim->linkage);
641 cfs_tage_free(victim);
642 tcd->tcd_cur_daemon_pages--;
647 static void put_pages_on_daemon_list(struct page_collection *pc)
649 struct cfs_trace_cpu_data *tcd;
652 for_each_possible_cpu(cpu) {
653 cfs_tcd_for_each_type_lock(tcd, i, cpu)
654 put_pages_on_tcd_daemon_list(pc, tcd);
658 void cfs_trace_debug_print(void)
660 struct page_collection pc;
661 struct cfs_trace_page *tage;
662 struct cfs_trace_page *tmp;
664 pc.pc_want_daemon_pages = 1;
666 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
670 __LASSERT_TAGE_INVARIANT(tage);
673 p = page_address(page);
674 while (p < ((char *)page_address(page) + tage->used)) {
675 struct ptldebug_header *hdr;
681 p += strlen(file) + 1;
684 len = hdr->ph_len - (int)(p - (char *)hdr);
686 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
691 list_del(&tage->linkage);
696 int cfs_tracefile_dump_all_pages(char *filename)
698 struct page_collection pc;
700 struct cfs_trace_page *tage;
701 struct cfs_trace_page *tmp;
703 mm_segment_t __oldfs;
706 cfs_tracefile_write_lock();
708 filp = filp_open(filename, O_CREAT | O_EXCL | O_WRONLY | O_LARGEFILE,
713 pr_err("LustreError: can't open %s for dump: rc %d\n",
718 pc.pc_want_daemon_pages = 1;
720 if (list_empty(&pc.pc_pages)) {
727 /* ok, for now, just write the pages. in the future we'll be building
728 * iobufs with the pages and calling generic_direct_IO
730 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
731 __LASSERT_TAGE_INVARIANT(tage);
733 buf = kmap(tage->page);
734 rc = kernel_write(filp, buf, tage->used, &filp->f_pos);
737 if (rc != (int)tage->used) {
738 pr_warn("wanted to write %u but wrote %d\n", tage->used,
741 __LASSERT(list_empty(&pc.pc_pages));
744 list_del(&tage->linkage);
748 rc = vfs_fsync(filp, 1);
750 pr_err("sync returns %d\n", rc);
752 filp_close(filp, NULL);
754 cfs_tracefile_write_unlock();
758 void cfs_trace_flush_pages(void)
760 struct page_collection pc;
761 struct cfs_trace_page *tage;
762 struct cfs_trace_page *tmp;
764 pc.pc_want_daemon_pages = 1;
766 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
767 __LASSERT_TAGE_INVARIANT(tage);
769 list_del(&tage->linkage);
774 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
775 const char __user *usr_buffer, int usr_buffer_nob)
779 if (usr_buffer_nob > knl_buffer_nob)
782 if (copy_from_user((void *)knl_buffer,
783 usr_buffer, usr_buffer_nob))
786 nob = strnlen(knl_buffer, usr_buffer_nob);
787 while (nob-- >= 0) /* strip trailing whitespace */
788 if (!isspace(knl_buffer[nob]))
791 if (nob < 0) /* empty string */
794 if (nob == knl_buffer_nob) /* no space to terminate */
797 knl_buffer[nob + 1] = 0; /* terminate */
800 EXPORT_SYMBOL(cfs_trace_copyin_string);
802 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
803 const char *knl_buffer, char *append)
806 * NB if 'append' != NULL, it's a single character to append to the
807 * copied out string - usually "\n" or "" (i.e. a terminating zero byte)
809 int nob = strlen(knl_buffer);
811 if (nob > usr_buffer_nob)
812 nob = usr_buffer_nob;
814 if (copy_to_user(usr_buffer, knl_buffer, nob))
817 if (append && nob < usr_buffer_nob) {
818 if (copy_to_user(usr_buffer + nob, append, 1))
826 EXPORT_SYMBOL(cfs_trace_copyout_string);
828 int cfs_trace_allocate_string_buffer(char **str, int nob)
830 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
833 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
840 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
845 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
849 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
850 usr_str, usr_str_nob);
858 rc = cfs_tracefile_dump_all_pages(str);
864 int cfs_trace_daemon_command(char *str)
868 cfs_tracefile_write_lock();
870 if (!strcmp(str, "stop")) {
871 cfs_tracefile_write_unlock();
872 cfs_trace_stop_thread();
873 cfs_tracefile_write_lock();
874 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
876 } else if (!strncmp(str, "size=", 5)) {
879 rc = kstrtoul(str + 5, 10, &tmp);
881 if (tmp < 10 || tmp > 20480)
882 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
884 cfs_tracefile_size = tmp << 20;
886 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
888 } else if (str[0] != '/') {
891 strcpy(cfs_tracefile, str);
893 pr_info("debug daemon will attempt to start writing to %s (%lukB max)\n",
895 (long)(cfs_tracefile_size >> 10));
897 cfs_trace_start_thread();
900 cfs_tracefile_write_unlock();
904 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
909 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
913 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
914 usr_str, usr_str_nob);
916 rc = cfs_trace_daemon_command(str);
922 int cfs_trace_set_debug_mb(int mb)
927 int limit = cfs_trace_max_debug_mb();
928 struct cfs_trace_cpu_data *tcd;
930 if (mb < num_possible_cpus()) {
931 pr_warn("%d MB is too small for debug buffer size, setting it to %d MB.\n",
932 mb, num_possible_cpus());
933 mb = num_possible_cpus();
937 pr_warn("%d MB is too large for debug buffer size, setting it to %d MB.\n",
942 mb /= num_possible_cpus();
943 pages = mb << (20 - PAGE_SHIFT);
945 cfs_tracefile_write_lock();
947 cfs_tcd_for_each(tcd, i, j)
948 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
950 cfs_tracefile_write_unlock();
955 int cfs_trace_get_debug_mb(void)
959 struct cfs_trace_cpu_data *tcd;
962 cfs_tracefile_read_lock();
964 cfs_tcd_for_each(tcd, i, j)
965 total_pages += tcd->tcd_max_pages;
967 cfs_tracefile_read_unlock();
969 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
972 static int tracefiled(void *arg)
974 struct page_collection pc;
975 struct tracefiled_ctl *tctl = arg;
976 struct cfs_trace_page *tage;
977 struct cfs_trace_page *tmp;
983 /* we're started late enough that we pick up init's fs context */
984 /* this is so broken in uml? what on earth is going on? */
986 complete(&tctl->tctl_start);
989 wait_queue_entry_t __wait;
991 pc.pc_want_daemon_pages = 0;
993 if (list_empty(&pc.pc_pages))
997 cfs_tracefile_read_lock();
998 if (cfs_tracefile[0]) {
999 filp = filp_open(cfs_tracefile,
1000 O_CREAT | O_RDWR | O_LARGEFILE,
1005 pr_warn("couldn't open %s: %d\n", cfs_tracefile,
1009 cfs_tracefile_read_unlock();
1011 put_pages_on_daemon_list(&pc);
1012 __LASSERT(list_empty(&pc.pc_pages));
1016 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1017 static loff_t f_pos;
1019 __LASSERT_TAGE_INVARIANT(tage);
1021 if (f_pos >= (off_t)cfs_tracefile_size)
1023 else if (f_pos > i_size_read(file_inode(filp)))
1024 f_pos = i_size_read(file_inode(filp));
1026 buf = kmap(tage->page);
1027 rc = kernel_write(filp, buf, tage->used, &f_pos);
1030 if (rc != (int)tage->used) {
1031 pr_warn("wanted to write %u but wrote %d\n",
1033 put_pages_back(&pc);
1034 __LASSERT(list_empty(&pc.pc_pages));
1039 filp_close(filp, NULL);
1040 put_pages_on_daemon_list(&pc);
1041 if (!list_empty(&pc.pc_pages)) {
1044 pr_alert("trace pages aren't empty\n");
1045 pr_err("total cpus(%d): ", num_possible_cpus());
1046 for (i = 0; i < num_possible_cpus(); i++)
1048 pr_cont("%d(on) ", i);
1050 pr_cont("%d(off) ", i);
1054 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1056 pr_err("page %d belongs to cpu %d\n",
1058 pr_err("There are %d pages unwritten\n", i);
1060 __LASSERT(list_empty(&pc.pc_pages));
1062 if (atomic_read(&tctl->tctl_shutdown)) {
1070 init_waitqueue_entry(&__wait, current);
1071 add_wait_queue(&tctl->tctl_waitq, &__wait);
1072 set_current_state(TASK_INTERRUPTIBLE);
1073 schedule_timeout(cfs_time_seconds(1));
1074 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1076 complete(&tctl->tctl_stop);
1080 int cfs_trace_start_thread(void)
1082 struct tracefiled_ctl *tctl = &trace_tctl;
1083 struct task_struct *task;
1086 mutex_lock(&cfs_trace_thread_mutex);
1090 init_completion(&tctl->tctl_start);
1091 init_completion(&tctl->tctl_stop);
1092 init_waitqueue_head(&tctl->tctl_waitq);
1093 atomic_set(&tctl->tctl_shutdown, 0);
1095 task = kthread_run(tracefiled, tctl, "ktracefiled");
1101 wait_for_completion(&tctl->tctl_start);
1104 mutex_unlock(&cfs_trace_thread_mutex);
1108 void cfs_trace_stop_thread(void)
1110 struct tracefiled_ctl *tctl = &trace_tctl;
1112 mutex_lock(&cfs_trace_thread_mutex);
1113 if (thread_running) {
1114 pr_info("shutting down debug daemon thread...\n");
1115 atomic_set(&tctl->tctl_shutdown, 1);
1116 wait_for_completion(&tctl->tctl_stop);
1119 mutex_unlock(&cfs_trace_thread_mutex);
1122 int cfs_tracefile_init(int max_pages)
1124 struct cfs_trace_cpu_data *tcd;
1130 rc = cfs_tracefile_init_arch();
1134 cfs_tcd_for_each(tcd, i, j) {
1135 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1136 factor = tcd->tcd_pages_factor;
1137 INIT_LIST_HEAD(&tcd->tcd_pages);
1138 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1139 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1140 tcd->tcd_cur_pages = 0;
1141 tcd->tcd_cur_stock_pages = 0;
1142 tcd->tcd_cur_daemon_pages = 0;
1143 tcd->tcd_max_pages = (max_pages * factor) / 100;
1144 LASSERT(tcd->tcd_max_pages > 0);
1145 tcd->tcd_shutting_down = 0;
1151 static void trace_cleanup_on_all_cpus(void)
1153 struct cfs_trace_cpu_data *tcd;
1154 struct cfs_trace_page *tage;
1155 struct cfs_trace_page *tmp;
1158 for_each_possible_cpu(cpu) {
1159 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1160 tcd->tcd_shutting_down = 1;
1162 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1164 __LASSERT_TAGE_INVARIANT(tage);
1166 list_del(&tage->linkage);
1167 cfs_tage_free(tage);
1170 tcd->tcd_cur_pages = 0;
1175 static void cfs_trace_cleanup(void)
1177 struct page_collection pc;
1179 INIT_LIST_HEAD(&pc.pc_pages);
1181 trace_cleanup_on_all_cpus();
1183 cfs_tracefile_fini_arch();
1186 void cfs_tracefile_exit(void)
1188 cfs_trace_stop_thread();
1189 cfs_trace_cleanup();