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 #include "tracefile.h"
42 #include "../../include/linux/libcfs/libcfs.h"
44 /* XXX move things up to the top, comment */
45 union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[NR_CPUS] __cacheline_aligned;
47 char cfs_tracefile[TRACEFILE_NAME_SIZE];
48 long long cfs_tracefile_size = CFS_TRACEFILE_SIZE;
49 static struct tracefiled_ctl trace_tctl;
50 static DEFINE_MUTEX(cfs_trace_thread_mutex);
51 static int thread_running;
53 static atomic_t cfs_tage_allocated = ATOMIC_INIT(0);
55 struct page_collection {
56 struct list_head pc_pages;
58 * if this flag is set, collect_pages() will spill both
59 * ->tcd_daemon_pages and ->tcd_pages to the ->pc_pages. Otherwise,
60 * only ->tcd_pages are spilled.
62 int pc_want_daemon_pages;
65 struct tracefiled_ctl {
66 struct completion tctl_start;
67 struct completion tctl_stop;
68 wait_queue_head_t tctl_waitq;
70 atomic_t tctl_shutdown;
74 * small data-structure for each page owned by tracefiled.
76 struct cfs_trace_page {
82 * linkage into one of the lists in trace_data_union or
85 struct list_head linkage;
87 * number of bytes used within this page
91 * cpu that owns this page
95 * type(context) of this page
100 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
101 struct cfs_trace_cpu_data *tcd);
103 static inline struct cfs_trace_page *
104 cfs_tage_from_list(struct list_head *list)
106 return list_entry(list, struct cfs_trace_page, linkage);
109 static struct cfs_trace_page *cfs_tage_alloc(gfp_t gfp)
112 struct cfs_trace_page *tage;
114 /* My caller is trying to free memory */
115 if (!in_interrupt() && memory_pressure_get())
119 * Don't spam console with allocation failures: they will be reported
120 * by upper layer anyway.
123 page = alloc_page(gfp);
127 tage = kmalloc(sizeof(*tage), gfp);
134 atomic_inc(&cfs_tage_allocated);
138 static void cfs_tage_free(struct cfs_trace_page *tage)
140 __free_page(tage->page);
142 atomic_dec(&cfs_tage_allocated);
145 static void cfs_tage_to_tail(struct cfs_trace_page *tage,
146 struct list_head *queue)
148 list_move_tail(&tage->linkage, queue);
151 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, gfp_t gfp,
152 struct list_head *stock)
157 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
158 * from here: this will lead to infinite recursion.
161 for (i = 0; i + tcd->tcd_cur_stock_pages < TCD_STOCK_PAGES ; ++i) {
162 struct cfs_trace_page *tage;
164 tage = cfs_tage_alloc(gfp);
167 list_add_tail(&tage->linkage, stock);
172 /* return a page that has 'len' bytes left at the end */
173 static struct cfs_trace_page *
174 cfs_trace_get_tage_try(struct cfs_trace_cpu_data *tcd, unsigned long len)
176 struct cfs_trace_page *tage;
178 if (tcd->tcd_cur_pages > 0) {
179 __LASSERT(!list_empty(&tcd->tcd_pages));
180 tage = cfs_tage_from_list(tcd->tcd_pages.prev);
181 if (tage->used + len <= PAGE_SIZE)
185 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
186 if (tcd->tcd_cur_stock_pages > 0) {
187 tage = cfs_tage_from_list(tcd->tcd_stock_pages.prev);
188 --tcd->tcd_cur_stock_pages;
189 list_del_init(&tage->linkage);
191 tage = cfs_tage_alloc(GFP_ATOMIC);
192 if (unlikely(!tage)) {
193 if ((!memory_pressure_get() ||
194 in_interrupt()) && printk_ratelimit())
196 "cannot allocate a tage (%ld)\n",
203 tage->cpu = smp_processor_id();
204 tage->type = tcd->tcd_type;
205 list_add_tail(&tage->linkage, &tcd->tcd_pages);
206 tcd->tcd_cur_pages++;
208 if (tcd->tcd_cur_pages > 8 && thread_running) {
209 struct tracefiled_ctl *tctl = &trace_tctl;
211 * wake up tracefiled to process some pages.
213 wake_up(&tctl->tctl_waitq);
220 static void cfs_tcd_shrink(struct cfs_trace_cpu_data *tcd)
222 int pgcount = tcd->tcd_cur_pages / 10;
223 struct page_collection pc;
224 struct cfs_trace_page *tage;
225 struct cfs_trace_page *tmp;
228 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
229 * from here: this will lead to infinite recursion.
232 if (printk_ratelimit())
233 printk(KERN_WARNING "debug daemon buffer overflowed; discarding 10%% of pages (%d of %ld)\n",
234 pgcount + 1, tcd->tcd_cur_pages);
236 INIT_LIST_HEAD(&pc.pc_pages);
238 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
242 list_move_tail(&tage->linkage, &pc.pc_pages);
243 tcd->tcd_cur_pages--;
245 put_pages_on_tcd_daemon_list(&pc, tcd);
248 /* return a page that has 'len' bytes left at the end */
249 static struct cfs_trace_page *cfs_trace_get_tage(struct cfs_trace_cpu_data *tcd,
252 struct cfs_trace_page *tage;
255 * XXX nikita: do NOT call portals_debug_msg() (CDEBUG/ENTRY/EXIT)
256 * from here: this will lead to infinite recursion.
259 if (len > PAGE_SIZE) {
260 pr_err("cowardly refusing to write %lu bytes in a page\n", len);
264 tage = cfs_trace_get_tage_try(tcd, len);
269 if (tcd->tcd_cur_pages > 0) {
270 tage = cfs_tage_from_list(tcd->tcd_pages.next);
272 cfs_tage_to_tail(tage, &tcd->tcd_pages);
277 int libcfs_debug_msg(struct libcfs_debug_msg_data *msgdata,
278 const char *format, ...)
283 va_start(args, format);
284 rc = libcfs_debug_vmsg2(msgdata, format, args, NULL);
289 EXPORT_SYMBOL(libcfs_debug_msg);
291 int libcfs_debug_vmsg2(struct libcfs_debug_msg_data *msgdata,
292 const char *format1, va_list args,
293 const char *format2, ...)
295 struct cfs_trace_cpu_data *tcd = NULL;
296 struct ptldebug_header header = {0};
297 struct cfs_trace_page *tage;
298 /* string_buf is used only if tcd != NULL, and is always set then */
299 char *string_buf = NULL;
302 int needed = 85; /* average message length */
308 int mask = msgdata->msg_mask;
309 const char *file = kbasename(msgdata->msg_file);
310 struct cfs_debug_limit_state *cdls = msgdata->msg_cdls;
312 tcd = cfs_trace_get_tcd();
314 /* cfs_trace_get_tcd() grabs a lock, which disables preemption and
315 * pins us to a particular CPU. This avoids an smp_processor_id()
316 * warning on Linux when debugging is enabled.
318 cfs_set_ptldebug_header(&header, msgdata, CDEBUG_STACK());
320 if (!tcd) /* arch may not log in IRQ context */
323 if (tcd->tcd_cur_pages == 0)
324 header.ph_flags |= PH_FLAG_FIRST_RECORD;
326 if (tcd->tcd_shutting_down) {
327 cfs_trace_put_tcd(tcd);
332 depth = __current_nesting_level();
333 known_size = strlen(file) + 1 + depth;
335 known_size += strlen(msgdata->msg_fn) + 1;
337 if (libcfs_debug_binary)
338 known_size += sizeof(header);
341 * '2' used because vsnprintf return real size required for output
342 * _without_ terminating NULL.
343 * if needed is to small for this format.
345 for (i = 0; i < 2; i++) {
346 tage = cfs_trace_get_tage(tcd, needed + known_size + 1);
348 if (needed + known_size > PAGE_SIZE)
351 cfs_trace_put_tcd(tcd);
356 string_buf = (char *)page_address(tage->page) +
357 tage->used + known_size;
359 max_nob = PAGE_SIZE - tage->used - known_size;
361 printk(KERN_EMERG "negative max_nob: %d\n",
364 cfs_trace_put_tcd(tcd);
372 needed = vsnprintf(string_buf, max_nob, format1, ap);
377 remain = max_nob - needed;
381 va_start(ap, format2);
382 needed += vsnprintf(string_buf + needed, remain,
387 if (needed < max_nob) /* well. printing ok.. */
391 if (*(string_buf + needed - 1) != '\n')
392 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
393 file, msgdata->msg_line, msgdata->msg_fn);
395 header.ph_len = known_size + needed;
396 debug_buf = (char *)page_address(tage->page) + tage->used;
398 if (libcfs_debug_binary) {
399 memcpy(debug_buf, &header, sizeof(header));
400 tage->used += sizeof(header);
401 debug_buf += sizeof(header);
404 /* indent message according to the nesting level */
405 while (depth-- > 0) {
406 *(debug_buf++) = '.';
410 strcpy(debug_buf, file);
411 tage->used += strlen(file) + 1;
412 debug_buf += strlen(file) + 1;
414 if (msgdata->msg_fn) {
415 strcpy(debug_buf, msgdata->msg_fn);
416 tage->used += strlen(msgdata->msg_fn) + 1;
417 debug_buf += strlen(msgdata->msg_fn) + 1;
420 __LASSERT(debug_buf == string_buf);
422 tage->used += needed;
423 __LASSERT(tage->used <= PAGE_SIZE);
426 if ((mask & libcfs_printk) == 0) {
427 /* no console output requested */
429 cfs_trace_put_tcd(tcd);
434 if (libcfs_console_ratelimit &&
435 cdls->cdls_next != 0 && /* not first time ever */
436 !cfs_time_after(cfs_time_current(), cdls->cdls_next)) {
437 /* skipping a console message */
440 cfs_trace_put_tcd(tcd);
444 if (cfs_time_after(cfs_time_current(),
445 cdls->cdls_next + libcfs_console_max_delay +
446 cfs_time_seconds(10))) {
447 /* last timeout was a long time ago */
448 cdls->cdls_delay /= libcfs_console_backoff * 4;
450 cdls->cdls_delay *= libcfs_console_backoff;
453 if (cdls->cdls_delay < libcfs_console_min_delay)
454 cdls->cdls_delay = libcfs_console_min_delay;
455 else if (cdls->cdls_delay > libcfs_console_max_delay)
456 cdls->cdls_delay = libcfs_console_max_delay;
458 /* ensure cdls_next is never zero after it's been seen */
459 cdls->cdls_next = (cfs_time_current() + cdls->cdls_delay) | 1;
463 cfs_print_to_console(&header, mask, string_buf, needed, file,
465 cfs_trace_put_tcd(tcd);
467 string_buf = cfs_trace_get_console_buffer();
472 needed = vsnprintf(string_buf,
473 CFS_TRACE_CONSOLE_BUFFER_SIZE,
478 remain = CFS_TRACE_CONSOLE_BUFFER_SIZE - needed;
480 va_start(ap, format2);
481 needed += vsnprintf(string_buf + needed, remain,
486 cfs_print_to_console(&header, mask,
487 string_buf, needed, file, msgdata->msg_fn);
492 if (cdls && cdls->cdls_count != 0) {
493 string_buf = cfs_trace_get_console_buffer();
495 needed = snprintf(string_buf, CFS_TRACE_CONSOLE_BUFFER_SIZE,
496 "Skipped %d previous similar message%s\n",
498 (cdls->cdls_count > 1) ? "s" : "");
500 cfs_print_to_console(&header, mask,
501 string_buf, needed, file, msgdata->msg_fn);
504 cdls->cdls_count = 0;
509 EXPORT_SYMBOL(libcfs_debug_vmsg2);
512 cfs_trace_assertion_failed(const char *str,
513 struct libcfs_debug_msg_data *msgdata)
515 struct ptldebug_header hdr;
517 libcfs_panic_in_progress = 1;
518 libcfs_catastrophe = 1;
521 cfs_set_ptldebug_header(&hdr, msgdata, CDEBUG_STACK());
523 cfs_print_to_console(&hdr, D_EMERG, str, strlen(str),
524 msgdata->msg_file, msgdata->msg_fn);
526 panic("Lustre debug assertion failure\n");
532 panic_collect_pages(struct page_collection *pc)
534 /* Do the collect_pages job on a single CPU: assumes that all other
535 * CPUs have been stopped during a panic. If this isn't true for some
536 * arch, this will have to be implemented separately in each arch.
540 struct cfs_trace_cpu_data *tcd;
542 INIT_LIST_HEAD(&pc->pc_pages);
544 cfs_tcd_for_each(tcd, i, j) {
545 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
546 tcd->tcd_cur_pages = 0;
548 if (pc->pc_want_daemon_pages) {
549 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
550 tcd->tcd_cur_daemon_pages = 0;
555 static void collect_pages_on_all_cpus(struct page_collection *pc)
557 struct cfs_trace_cpu_data *tcd;
560 for_each_possible_cpu(cpu) {
561 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
562 list_splice_init(&tcd->tcd_pages, &pc->pc_pages);
563 tcd->tcd_cur_pages = 0;
564 if (pc->pc_want_daemon_pages) {
565 list_splice_init(&tcd->tcd_daemon_pages,
567 tcd->tcd_cur_daemon_pages = 0;
573 static void collect_pages(struct page_collection *pc)
575 INIT_LIST_HEAD(&pc->pc_pages);
577 if (libcfs_panic_in_progress)
578 panic_collect_pages(pc);
580 collect_pages_on_all_cpus(pc);
583 static void put_pages_back_on_all_cpus(struct page_collection *pc)
585 struct cfs_trace_cpu_data *tcd;
586 struct list_head *cur_head;
587 struct cfs_trace_page *tage;
588 struct cfs_trace_page *tmp;
591 for_each_possible_cpu(cpu) {
592 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
593 cur_head = tcd->tcd_pages.next;
595 list_for_each_entry_safe(tage, tmp, &pc->pc_pages,
597 __LASSERT_TAGE_INVARIANT(tage);
599 if (tage->cpu != cpu || tage->type != i)
602 cfs_tage_to_tail(tage, cur_head);
603 tcd->tcd_cur_pages++;
609 static void put_pages_back(struct page_collection *pc)
611 if (!libcfs_panic_in_progress)
612 put_pages_back_on_all_cpus(pc);
615 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
616 * we have a good amount of data at all times for dumping during an LBUG, even
617 * if we have been steadily writing (and otherwise discarding) pages via the
620 static void put_pages_on_tcd_daemon_list(struct page_collection *pc,
621 struct cfs_trace_cpu_data *tcd)
623 struct cfs_trace_page *tage;
624 struct cfs_trace_page *tmp;
626 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
627 __LASSERT_TAGE_INVARIANT(tage);
629 if (tage->cpu != tcd->tcd_cpu || tage->type != tcd->tcd_type)
632 cfs_tage_to_tail(tage, &tcd->tcd_daemon_pages);
633 tcd->tcd_cur_daemon_pages++;
635 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
636 struct cfs_trace_page *victim;
638 __LASSERT(!list_empty(&tcd->tcd_daemon_pages));
639 victim = cfs_tage_from_list(tcd->tcd_daemon_pages.next);
641 __LASSERT_TAGE_INVARIANT(victim);
643 list_del(&victim->linkage);
644 cfs_tage_free(victim);
645 tcd->tcd_cur_daemon_pages--;
650 static void put_pages_on_daemon_list(struct page_collection *pc)
652 struct cfs_trace_cpu_data *tcd;
655 for_each_possible_cpu(cpu) {
656 cfs_tcd_for_each_type_lock(tcd, i, cpu)
657 put_pages_on_tcd_daemon_list(pc, tcd);
661 void cfs_trace_debug_print(void)
663 struct page_collection pc;
664 struct cfs_trace_page *tage;
665 struct cfs_trace_page *tmp;
667 pc.pc_want_daemon_pages = 1;
669 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
673 __LASSERT_TAGE_INVARIANT(tage);
676 p = page_address(page);
677 while (p < ((char *)page_address(page) + tage->used)) {
678 struct ptldebug_header *hdr;
684 p += strlen(file) + 1;
687 len = hdr->ph_len - (int)(p - (char *)hdr);
689 cfs_print_to_console(hdr, D_EMERG, p, len, file, fn);
694 list_del(&tage->linkage);
699 int cfs_tracefile_dump_all_pages(char *filename)
701 struct page_collection pc;
703 struct cfs_trace_page *tage;
704 struct cfs_trace_page *tmp;
706 mm_segment_t __oldfs;
709 cfs_tracefile_write_lock();
711 filp = filp_open(filename, O_CREAT | O_EXCL | O_WRONLY | O_LARGEFILE,
716 pr_err("LustreError: can't open %s for dump: rc %d\n",
721 pc.pc_want_daemon_pages = 1;
723 if (list_empty(&pc.pc_pages)) {
730 /* ok, for now, just write the pages. in the future we'll be building
731 * iobufs with the pages and calling generic_direct_IO
733 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
734 __LASSERT_TAGE_INVARIANT(tage);
736 buf = kmap(tage->page);
737 rc = vfs_write(filp, (__force const char __user *)buf,
738 tage->used, &filp->f_pos);
741 if (rc != (int)tage->used) {
742 printk(KERN_WARNING "wanted to write %u but wrote %d\n",
745 __LASSERT(list_empty(&pc.pc_pages));
748 list_del(&tage->linkage);
752 rc = vfs_fsync(filp, 1);
754 pr_err("sync returns %d\n", rc);
756 filp_close(filp, NULL);
758 cfs_tracefile_write_unlock();
762 void cfs_trace_flush_pages(void)
764 struct page_collection pc;
765 struct cfs_trace_page *tage;
766 struct cfs_trace_page *tmp;
768 pc.pc_want_daemon_pages = 1;
770 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
771 __LASSERT_TAGE_INVARIANT(tage);
773 list_del(&tage->linkage);
778 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
779 const char __user *usr_buffer, int usr_buffer_nob)
783 if (usr_buffer_nob > knl_buffer_nob)
786 if (copy_from_user((void *)knl_buffer,
787 usr_buffer, usr_buffer_nob))
790 nob = strnlen(knl_buffer, usr_buffer_nob);
791 while (nob-- >= 0) /* strip trailing whitespace */
792 if (!isspace(knl_buffer[nob]))
795 if (nob < 0) /* empty string */
798 if (nob == knl_buffer_nob) /* no space to terminate */
801 knl_buffer[nob + 1] = 0; /* terminate */
804 EXPORT_SYMBOL(cfs_trace_copyin_string);
806 int cfs_trace_copyout_string(char __user *usr_buffer, int usr_buffer_nob,
807 const char *knl_buffer, char *append)
810 * NB if 'append' != NULL, it's a single character to append to the
811 * copied out string - usually "\n" or "" (i.e. a terminating zero byte)
813 int nob = strlen(knl_buffer);
815 if (nob > usr_buffer_nob)
816 nob = usr_buffer_nob;
818 if (copy_to_user(usr_buffer, knl_buffer, nob))
821 if (append && nob < usr_buffer_nob) {
822 if (copy_to_user(usr_buffer + nob, append, 1))
830 EXPORT_SYMBOL(cfs_trace_copyout_string);
832 int cfs_trace_allocate_string_buffer(char **str, int nob)
834 if (nob > 2 * PAGE_SIZE) /* string must be "sensible" */
837 *str = kmalloc(nob, GFP_KERNEL | __GFP_ZERO);
844 int cfs_trace_dump_debug_buffer_usrstr(void __user *usr_str, int usr_str_nob)
849 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
853 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
854 usr_str, usr_str_nob);
862 rc = cfs_tracefile_dump_all_pages(str);
868 int cfs_trace_daemon_command(char *str)
872 cfs_tracefile_write_lock();
874 if (strcmp(str, "stop") == 0) {
875 cfs_tracefile_write_unlock();
876 cfs_trace_stop_thread();
877 cfs_tracefile_write_lock();
878 memset(cfs_tracefile, 0, sizeof(cfs_tracefile));
880 } else if (strncmp(str, "size=", 5) == 0) {
883 rc = kstrtoul(str + 5, 10, &tmp);
885 if (tmp < 10 || tmp > 20480)
886 cfs_tracefile_size = CFS_TRACEFILE_SIZE;
888 cfs_tracefile_size = tmp << 20;
890 } else if (strlen(str) >= sizeof(cfs_tracefile)) {
892 } else if (str[0] != '/') {
895 strcpy(cfs_tracefile, str);
898 "Lustre: debug daemon will attempt to start writing to %s (%lukB max)\n",
900 (long)(cfs_tracefile_size >> 10));
902 cfs_trace_start_thread();
905 cfs_tracefile_write_unlock();
909 int cfs_trace_daemon_command_usrstr(void __user *usr_str, int usr_str_nob)
914 rc = cfs_trace_allocate_string_buffer(&str, usr_str_nob + 1);
918 rc = cfs_trace_copyin_string(str, usr_str_nob + 1,
919 usr_str, usr_str_nob);
921 rc = cfs_trace_daemon_command(str);
927 int cfs_trace_set_debug_mb(int mb)
932 int limit = cfs_trace_max_debug_mb();
933 struct cfs_trace_cpu_data *tcd;
935 if (mb < num_possible_cpus()) {
937 "Lustre: %d MB is too small for debug buffer size, setting it to %d MB.\n",
938 mb, num_possible_cpus());
939 mb = num_possible_cpus();
944 "Lustre: %d MB is too large for debug buffer size, setting it to %d MB.\n",
949 mb /= num_possible_cpus();
950 pages = mb << (20 - PAGE_SHIFT);
952 cfs_tracefile_write_lock();
954 cfs_tcd_for_each(tcd, i, j)
955 tcd->tcd_max_pages = (pages * tcd->tcd_pages_factor) / 100;
957 cfs_tracefile_write_unlock();
962 int cfs_trace_get_debug_mb(void)
966 struct cfs_trace_cpu_data *tcd;
969 cfs_tracefile_read_lock();
971 cfs_tcd_for_each(tcd, i, j)
972 total_pages += tcd->tcd_max_pages;
974 cfs_tracefile_read_unlock();
976 return (total_pages >> (20 - PAGE_SHIFT)) + 1;
979 static int tracefiled(void *arg)
981 struct page_collection pc;
982 struct tracefiled_ctl *tctl = arg;
983 struct cfs_trace_page *tage;
984 struct cfs_trace_page *tmp;
985 mm_segment_t __oldfs;
991 /* we're started late enough that we pick up init's fs context */
992 /* this is so broken in uml? what on earth is going on? */
994 complete(&tctl->tctl_start);
999 pc.pc_want_daemon_pages = 0;
1001 if (list_empty(&pc.pc_pages))
1005 cfs_tracefile_read_lock();
1006 if (cfs_tracefile[0] != 0) {
1007 filp = filp_open(cfs_tracefile,
1008 O_CREAT | O_RDWR | O_LARGEFILE,
1013 printk(KERN_WARNING "couldn't open %s: %d\n",
1017 cfs_tracefile_read_unlock();
1019 put_pages_on_daemon_list(&pc);
1020 __LASSERT(list_empty(&pc.pc_pages));
1026 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
1027 static loff_t f_pos;
1029 __LASSERT_TAGE_INVARIANT(tage);
1031 if (f_pos >= (off_t)cfs_tracefile_size)
1033 else if (f_pos > i_size_read(file_inode(filp)))
1034 f_pos = i_size_read(file_inode(filp));
1036 buf = kmap(tage->page);
1037 rc = vfs_write(filp, (__force const char __user *)buf,
1038 tage->used, &f_pos);
1041 if (rc != (int)tage->used) {
1042 printk(KERN_WARNING "wanted to write %u but wrote %d\n",
1044 put_pages_back(&pc);
1045 __LASSERT(list_empty(&pc.pc_pages));
1051 filp_close(filp, NULL);
1052 put_pages_on_daemon_list(&pc);
1053 if (!list_empty(&pc.pc_pages)) {
1056 printk(KERN_ALERT "Lustre: trace pages aren't empty\n");
1057 pr_err("total cpus(%d): ", num_possible_cpus());
1058 for (i = 0; i < num_possible_cpus(); i++)
1060 pr_cont("%d(on) ", i);
1062 pr_cont("%d(off) ", i);
1066 list_for_each_entry_safe(tage, tmp, &pc.pc_pages,
1068 pr_err("page %d belongs to cpu %d\n",
1070 pr_err("There are %d pages unwritten\n", i);
1072 __LASSERT(list_empty(&pc.pc_pages));
1074 if (atomic_read(&tctl->tctl_shutdown)) {
1075 if (last_loop == 0) {
1082 init_waitqueue_entry(&__wait, current);
1083 add_wait_queue(&tctl->tctl_waitq, &__wait);
1084 set_current_state(TASK_INTERRUPTIBLE);
1085 schedule_timeout(cfs_time_seconds(1));
1086 remove_wait_queue(&tctl->tctl_waitq, &__wait);
1088 complete(&tctl->tctl_stop);
1092 int cfs_trace_start_thread(void)
1094 struct tracefiled_ctl *tctl = &trace_tctl;
1095 struct task_struct *task;
1098 mutex_lock(&cfs_trace_thread_mutex);
1102 init_completion(&tctl->tctl_start);
1103 init_completion(&tctl->tctl_stop);
1104 init_waitqueue_head(&tctl->tctl_waitq);
1105 atomic_set(&tctl->tctl_shutdown, 0);
1107 task = kthread_run(tracefiled, tctl, "ktracefiled");
1113 wait_for_completion(&tctl->tctl_start);
1116 mutex_unlock(&cfs_trace_thread_mutex);
1120 void cfs_trace_stop_thread(void)
1122 struct tracefiled_ctl *tctl = &trace_tctl;
1124 mutex_lock(&cfs_trace_thread_mutex);
1125 if (thread_running) {
1127 "Lustre: shutting down debug daemon thread...\n");
1128 atomic_set(&tctl->tctl_shutdown, 1);
1129 wait_for_completion(&tctl->tctl_stop);
1132 mutex_unlock(&cfs_trace_thread_mutex);
1135 int cfs_tracefile_init(int max_pages)
1137 struct cfs_trace_cpu_data *tcd;
1143 rc = cfs_tracefile_init_arch();
1147 cfs_tcd_for_each(tcd, i, j) {
1148 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1149 factor = tcd->tcd_pages_factor;
1150 INIT_LIST_HEAD(&tcd->tcd_pages);
1151 INIT_LIST_HEAD(&tcd->tcd_stock_pages);
1152 INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
1153 tcd->tcd_cur_pages = 0;
1154 tcd->tcd_cur_stock_pages = 0;
1155 tcd->tcd_cur_daemon_pages = 0;
1156 tcd->tcd_max_pages = (max_pages * factor) / 100;
1157 LASSERT(tcd->tcd_max_pages > 0);
1158 tcd->tcd_shutting_down = 0;
1164 static void trace_cleanup_on_all_cpus(void)
1166 struct cfs_trace_cpu_data *tcd;
1167 struct cfs_trace_page *tage;
1168 struct cfs_trace_page *tmp;
1171 for_each_possible_cpu(cpu) {
1172 cfs_tcd_for_each_type_lock(tcd, i, cpu) {
1173 tcd->tcd_shutting_down = 1;
1175 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages,
1177 __LASSERT_TAGE_INVARIANT(tage);
1179 list_del(&tage->linkage);
1180 cfs_tage_free(tage);
1183 tcd->tcd_cur_pages = 0;
1188 static void cfs_trace_cleanup(void)
1190 struct page_collection pc;
1192 INIT_LIST_HEAD(&pc.pc_pages);
1194 trace_cleanup_on_all_cpus();
1196 cfs_tracefile_fini_arch();
1199 void cfs_tracefile_exit(void)
1201 cfs_trace_stop_thread();
1202 cfs_trace_cleanup();