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 "../../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() ||
195 in_interrupt()) && printk_ratelimit())
196 pr_warn("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 pr_warn("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
)
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 pr_emerg("negative max_nob: %d\n", max_nob
);
363 cfs_trace_put_tcd(tcd
);
371 needed
= vsnprintf(string_buf
, max_nob
, format1
, ap
);
376 remain
= max_nob
- needed
;
380 va_start(ap
, format2
);
381 needed
+= vsnprintf(string_buf
+ needed
, remain
,
386 if (needed
< max_nob
) /* well. printing ok.. */
390 if (*(string_buf
+ needed
- 1) != '\n')
391 pr_info("format at %s:%d:%s doesn't end in newline\n", file
,
392 msgdata
->msg_line
, msgdata
->msg_fn
);
394 header
.ph_len
= known_size
+ needed
;
395 debug_buf
= (char *)page_address(tage
->page
) + tage
->used
;
397 if (libcfs_debug_binary
) {
398 memcpy(debug_buf
, &header
, sizeof(header
));
399 tage
->used
+= sizeof(header
);
400 debug_buf
+= sizeof(header
);
403 /* indent message according to the nesting level */
404 while (depth
-- > 0) {
405 *(debug_buf
++) = '.';
409 strcpy(debug_buf
, file
);
410 tage
->used
+= strlen(file
) + 1;
411 debug_buf
+= strlen(file
) + 1;
413 if (msgdata
->msg_fn
) {
414 strcpy(debug_buf
, msgdata
->msg_fn
);
415 tage
->used
+= strlen(msgdata
->msg_fn
) + 1;
416 debug_buf
+= strlen(msgdata
->msg_fn
) + 1;
419 __LASSERT(debug_buf
== string_buf
);
421 tage
->used
+= needed
;
422 __LASSERT(tage
->used
<= PAGE_SIZE
);
425 if (!(mask
& libcfs_printk
)) {
426 /* no console output requested */
428 cfs_trace_put_tcd(tcd
);
433 if (libcfs_console_ratelimit
&&
434 cdls
->cdls_next
&& /* not first time ever */
435 !cfs_time_after(cfs_time_current(), cdls
->cdls_next
)) {
436 /* skipping a console message */
439 cfs_trace_put_tcd(tcd
);
443 if (cfs_time_after(cfs_time_current(),
444 cdls
->cdls_next
+ libcfs_console_max_delay
+
445 cfs_time_seconds(10))) {
446 /* last timeout was a long time ago */
447 cdls
->cdls_delay
/= libcfs_console_backoff
* 4;
449 cdls
->cdls_delay
*= libcfs_console_backoff
;
452 if (cdls
->cdls_delay
< libcfs_console_min_delay
)
453 cdls
->cdls_delay
= libcfs_console_min_delay
;
454 else if (cdls
->cdls_delay
> libcfs_console_max_delay
)
455 cdls
->cdls_delay
= libcfs_console_max_delay
;
457 /* ensure cdls_next is never zero after it's been seen */
458 cdls
->cdls_next
= (cfs_time_current() + cdls
->cdls_delay
) | 1;
462 cfs_print_to_console(&header
, mask
, string_buf
, needed
, file
,
464 cfs_trace_put_tcd(tcd
);
466 string_buf
= cfs_trace_get_console_buffer();
471 needed
= vsnprintf(string_buf
,
472 CFS_TRACE_CONSOLE_BUFFER_SIZE
,
477 remain
= CFS_TRACE_CONSOLE_BUFFER_SIZE
- needed
;
479 va_start(ap
, format2
);
480 needed
+= vsnprintf(string_buf
+ needed
, remain
,
485 cfs_print_to_console(&header
, mask
,
486 string_buf
, needed
, file
, msgdata
->msg_fn
);
491 if (cdls
&& cdls
->cdls_count
) {
492 string_buf
= cfs_trace_get_console_buffer();
494 needed
= snprintf(string_buf
, CFS_TRACE_CONSOLE_BUFFER_SIZE
,
495 "Skipped %d previous similar message%s\n",
497 (cdls
->cdls_count
> 1) ? "s" : "");
499 cfs_print_to_console(&header
, mask
,
500 string_buf
, needed
, file
, msgdata
->msg_fn
);
503 cdls
->cdls_count
= 0;
508 EXPORT_SYMBOL(libcfs_debug_vmsg2
);
511 cfs_trace_assertion_failed(const char *str
,
512 struct libcfs_debug_msg_data
*msgdata
)
514 struct ptldebug_header hdr
;
516 libcfs_panic_in_progress
= 1;
517 libcfs_catastrophe
= 1;
520 cfs_set_ptldebug_header(&hdr
, msgdata
, CDEBUG_STACK());
522 cfs_print_to_console(&hdr
, D_EMERG
, str
, strlen(str
),
523 msgdata
->msg_file
, msgdata
->msg_fn
);
525 panic("Lustre debug assertion failure\n");
531 panic_collect_pages(struct page_collection
*pc
)
533 /* Do the collect_pages job on a single CPU: assumes that all other
534 * CPUs have been stopped during a panic. If this isn't true for some
535 * arch, this will have to be implemented separately in each arch.
537 struct cfs_trace_cpu_data
*tcd
;
541 INIT_LIST_HEAD(&pc
->pc_pages
);
543 cfs_tcd_for_each(tcd
, i
, j
) {
544 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
545 tcd
->tcd_cur_pages
= 0;
547 if (pc
->pc_want_daemon_pages
) {
548 list_splice_init(&tcd
->tcd_daemon_pages
, &pc
->pc_pages
);
549 tcd
->tcd_cur_daemon_pages
= 0;
554 static void collect_pages_on_all_cpus(struct page_collection
*pc
)
556 struct cfs_trace_cpu_data
*tcd
;
559 for_each_possible_cpu(cpu
) {
560 cfs_tcd_for_each_type_lock(tcd
, i
, cpu
) {
561 list_splice_init(&tcd
->tcd_pages
, &pc
->pc_pages
);
562 tcd
->tcd_cur_pages
= 0;
563 if (pc
->pc_want_daemon_pages
) {
564 list_splice_init(&tcd
->tcd_daemon_pages
,
566 tcd
->tcd_cur_daemon_pages
= 0;
572 static void collect_pages(struct page_collection
*pc
)
574 INIT_LIST_HEAD(&pc
->pc_pages
);
576 if (libcfs_panic_in_progress
)
577 panic_collect_pages(pc
);
579 collect_pages_on_all_cpus(pc
);
582 static void put_pages_back_on_all_cpus(struct page_collection
*pc
)
584 struct cfs_trace_cpu_data
*tcd
;
585 struct list_head
*cur_head
;
586 struct cfs_trace_page
*tage
;
587 struct cfs_trace_page
*tmp
;
590 for_each_possible_cpu(cpu
) {
591 cfs_tcd_for_each_type_lock(tcd
, i
, cpu
) {
592 cur_head
= tcd
->tcd_pages
.next
;
594 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
,
596 __LASSERT_TAGE_INVARIANT(tage
);
598 if (tage
->cpu
!= cpu
|| tage
->type
!= i
)
601 cfs_tage_to_tail(tage
, cur_head
);
602 tcd
->tcd_cur_pages
++;
608 static void put_pages_back(struct page_collection
*pc
)
610 if (!libcfs_panic_in_progress
)
611 put_pages_back_on_all_cpus(pc
);
614 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
615 * we have a good amount of data at all times for dumping during an LBUG, even
616 * if we have been steadily writing (and otherwise discarding) pages via the
619 static void put_pages_on_tcd_daemon_list(struct page_collection
*pc
,
620 struct cfs_trace_cpu_data
*tcd
)
622 struct cfs_trace_page
*tage
;
623 struct cfs_trace_page
*tmp
;
625 list_for_each_entry_safe(tage
, tmp
, &pc
->pc_pages
, linkage
) {
626 __LASSERT_TAGE_INVARIANT(tage
);
628 if (tage
->cpu
!= tcd
->tcd_cpu
|| tage
->type
!= tcd
->tcd_type
)
631 cfs_tage_to_tail(tage
, &tcd
->tcd_daemon_pages
);
632 tcd
->tcd_cur_daemon_pages
++;
634 if (tcd
->tcd_cur_daemon_pages
> tcd
->tcd_max_pages
) {
635 struct cfs_trace_page
*victim
;
637 __LASSERT(!list_empty(&tcd
->tcd_daemon_pages
));
638 victim
= cfs_tage_from_list(tcd
->tcd_daemon_pages
.next
);
640 __LASSERT_TAGE_INVARIANT(victim
);
642 list_del(&victim
->linkage
);
643 cfs_tage_free(victim
);
644 tcd
->tcd_cur_daemon_pages
--;
649 static void put_pages_on_daemon_list(struct page_collection
*pc
)
651 struct cfs_trace_cpu_data
*tcd
;
654 for_each_possible_cpu(cpu
) {
655 cfs_tcd_for_each_type_lock(tcd
, i
, cpu
)
656 put_pages_on_tcd_daemon_list(pc
, tcd
);
660 void cfs_trace_debug_print(void)
662 struct page_collection pc
;
663 struct cfs_trace_page
*tage
;
664 struct cfs_trace_page
*tmp
;
666 pc
.pc_want_daemon_pages
= 1;
668 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
672 __LASSERT_TAGE_INVARIANT(tage
);
675 p
= page_address(page
);
676 while (p
< ((char *)page_address(page
) + tage
->used
)) {
677 struct ptldebug_header
*hdr
;
683 p
+= strlen(file
) + 1;
686 len
= hdr
->ph_len
- (int)(p
- (char *)hdr
);
688 cfs_print_to_console(hdr
, D_EMERG
, p
, len
, file
, fn
);
693 list_del(&tage
->linkage
);
698 int cfs_tracefile_dump_all_pages(char *filename
)
700 struct page_collection pc
;
702 struct cfs_trace_page
*tage
;
703 struct cfs_trace_page
*tmp
;
705 mm_segment_t __oldfs
;
708 cfs_tracefile_write_lock();
710 filp
= filp_open(filename
, O_CREAT
| O_EXCL
| O_WRONLY
| O_LARGEFILE
,
715 pr_err("LustreError: can't open %s for dump: rc %d\n",
720 pc
.pc_want_daemon_pages
= 1;
722 if (list_empty(&pc
.pc_pages
)) {
729 /* ok, for now, just write the pages. in the future we'll be building
730 * iobufs with the pages and calling generic_direct_IO
732 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
733 __LASSERT_TAGE_INVARIANT(tage
);
735 buf
= kmap(tage
->page
);
736 rc
= vfs_write(filp
, (__force
const char __user
*)buf
,
737 tage
->used
, &filp
->f_pos
);
740 if (rc
!= (int)tage
->used
) {
741 pr_warn("wanted to write %u but wrote %d\n", tage
->used
,
744 __LASSERT(list_empty(&pc
.pc_pages
));
747 list_del(&tage
->linkage
);
751 rc
= vfs_fsync(filp
, 1);
753 pr_err("sync returns %d\n", rc
);
755 filp_close(filp
, NULL
);
757 cfs_tracefile_write_unlock();
761 void cfs_trace_flush_pages(void)
763 struct page_collection pc
;
764 struct cfs_trace_page
*tage
;
765 struct cfs_trace_page
*tmp
;
767 pc
.pc_want_daemon_pages
= 1;
769 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
770 __LASSERT_TAGE_INVARIANT(tage
);
772 list_del(&tage
->linkage
);
777 int cfs_trace_copyin_string(char *knl_buffer
, int knl_buffer_nob
,
778 const char __user
*usr_buffer
, int usr_buffer_nob
)
782 if (usr_buffer_nob
> knl_buffer_nob
)
785 if (copy_from_user((void *)knl_buffer
,
786 usr_buffer
, usr_buffer_nob
))
789 nob
= strnlen(knl_buffer
, usr_buffer_nob
);
790 while (nob
-- >= 0) /* strip trailing whitespace */
791 if (!isspace(knl_buffer
[nob
]))
794 if (nob
< 0) /* empty string */
797 if (nob
== knl_buffer_nob
) /* no space to terminate */
800 knl_buffer
[nob
+ 1] = 0; /* terminate */
803 EXPORT_SYMBOL(cfs_trace_copyin_string
);
805 int cfs_trace_copyout_string(char __user
*usr_buffer
, int usr_buffer_nob
,
806 const char *knl_buffer
, char *append
)
809 * NB if 'append' != NULL, it's a single character to append to the
810 * copied out string - usually "\n" or "" (i.e. a terminating zero byte)
812 int nob
= strlen(knl_buffer
);
814 if (nob
> usr_buffer_nob
)
815 nob
= usr_buffer_nob
;
817 if (copy_to_user(usr_buffer
, knl_buffer
, nob
))
820 if (append
&& nob
< usr_buffer_nob
) {
821 if (copy_to_user(usr_buffer
+ nob
, append
, 1))
829 EXPORT_SYMBOL(cfs_trace_copyout_string
);
831 int cfs_trace_allocate_string_buffer(char **str
, int nob
)
833 if (nob
> 2 * PAGE_SIZE
) /* string must be "sensible" */
836 *str
= kmalloc(nob
, GFP_KERNEL
| __GFP_ZERO
);
843 int cfs_trace_dump_debug_buffer_usrstr(void __user
*usr_str
, int usr_str_nob
)
848 rc
= cfs_trace_allocate_string_buffer(&str
, usr_str_nob
+ 1);
852 rc
= cfs_trace_copyin_string(str
, usr_str_nob
+ 1,
853 usr_str
, usr_str_nob
);
861 rc
= cfs_tracefile_dump_all_pages(str
);
867 int cfs_trace_daemon_command(char *str
)
871 cfs_tracefile_write_lock();
873 if (!strcmp(str
, "stop")) {
874 cfs_tracefile_write_unlock();
875 cfs_trace_stop_thread();
876 cfs_tracefile_write_lock();
877 memset(cfs_tracefile
, 0, sizeof(cfs_tracefile
));
879 } else if (!strncmp(str
, "size=", 5)) {
882 rc
= kstrtoul(str
+ 5, 10, &tmp
);
884 if (tmp
< 10 || tmp
> 20480)
885 cfs_tracefile_size
= CFS_TRACEFILE_SIZE
;
887 cfs_tracefile_size
= tmp
<< 20;
889 } else if (strlen(str
) >= sizeof(cfs_tracefile
)) {
891 } else if (str
[0] != '/') {
894 strcpy(cfs_tracefile
, str
);
896 pr_info("debug daemon will attempt to start writing to %s (%lukB max)\n",
898 (long)(cfs_tracefile_size
>> 10));
900 cfs_trace_start_thread();
903 cfs_tracefile_write_unlock();
907 int cfs_trace_daemon_command_usrstr(void __user
*usr_str
, int usr_str_nob
)
912 rc
= cfs_trace_allocate_string_buffer(&str
, usr_str_nob
+ 1);
916 rc
= cfs_trace_copyin_string(str
, usr_str_nob
+ 1,
917 usr_str
, usr_str_nob
);
919 rc
= cfs_trace_daemon_command(str
);
925 int cfs_trace_set_debug_mb(int mb
)
930 int limit
= cfs_trace_max_debug_mb();
931 struct cfs_trace_cpu_data
*tcd
;
933 if (mb
< num_possible_cpus()) {
934 pr_warn("%d MB is too small for debug buffer size, setting it to %d MB.\n",
935 mb
, num_possible_cpus());
936 mb
= num_possible_cpus();
940 pr_warn("%d MB is too large for debug buffer size, setting it to %d MB.\n",
945 mb
/= num_possible_cpus();
946 pages
= mb
<< (20 - PAGE_SHIFT
);
948 cfs_tracefile_write_lock();
950 cfs_tcd_for_each(tcd
, i
, j
)
951 tcd
->tcd_max_pages
= (pages
* tcd
->tcd_pages_factor
) / 100;
953 cfs_tracefile_write_unlock();
958 int cfs_trace_get_debug_mb(void)
962 struct cfs_trace_cpu_data
*tcd
;
965 cfs_tracefile_read_lock();
967 cfs_tcd_for_each(tcd
, i
, j
)
968 total_pages
+= tcd
->tcd_max_pages
;
970 cfs_tracefile_read_unlock();
972 return (total_pages
>> (20 - PAGE_SHIFT
)) + 1;
975 static int tracefiled(void *arg
)
977 struct page_collection pc
;
978 struct tracefiled_ctl
*tctl
= arg
;
979 struct cfs_trace_page
*tage
;
980 struct cfs_trace_page
*tmp
;
981 mm_segment_t __oldfs
;
987 /* we're started late enough that we pick up init's fs context */
988 /* this is so broken in uml? what on earth is going on? */
990 complete(&tctl
->tctl_start
);
993 wait_queue_entry_t __wait
;
995 pc
.pc_want_daemon_pages
= 0;
997 if (list_empty(&pc
.pc_pages
))
1001 cfs_tracefile_read_lock();
1002 if (cfs_tracefile
[0]) {
1003 filp
= filp_open(cfs_tracefile
,
1004 O_CREAT
| O_RDWR
| O_LARGEFILE
,
1009 pr_warn("couldn't open %s: %d\n", cfs_tracefile
,
1013 cfs_tracefile_read_unlock();
1015 put_pages_on_daemon_list(&pc
);
1016 __LASSERT(list_empty(&pc
.pc_pages
));
1022 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
, linkage
) {
1023 static loff_t f_pos
;
1025 __LASSERT_TAGE_INVARIANT(tage
);
1027 if (f_pos
>= (off_t
)cfs_tracefile_size
)
1029 else if (f_pos
> i_size_read(file_inode(filp
)))
1030 f_pos
= i_size_read(file_inode(filp
));
1032 buf
= kmap(tage
->page
);
1033 rc
= vfs_write(filp
, (__force
const char __user
*)buf
,
1034 tage
->used
, &f_pos
);
1037 if (rc
!= (int)tage
->used
) {
1038 pr_warn("wanted to write %u but wrote %d\n",
1040 put_pages_back(&pc
);
1041 __LASSERT(list_empty(&pc
.pc_pages
));
1047 filp_close(filp
, NULL
);
1048 put_pages_on_daemon_list(&pc
);
1049 if (!list_empty(&pc
.pc_pages
)) {
1052 pr_alert("trace pages aren't empty\n");
1053 pr_err("total cpus(%d): ", num_possible_cpus());
1054 for (i
= 0; i
< num_possible_cpus(); i
++)
1056 pr_cont("%d(on) ", i
);
1058 pr_cont("%d(off) ", i
);
1062 list_for_each_entry_safe(tage
, tmp
, &pc
.pc_pages
,
1064 pr_err("page %d belongs to cpu %d\n",
1066 pr_err("There are %d pages unwritten\n", i
);
1068 __LASSERT(list_empty(&pc
.pc_pages
));
1070 if (atomic_read(&tctl
->tctl_shutdown
)) {
1078 init_waitqueue_entry(&__wait
, current
);
1079 add_wait_queue(&tctl
->tctl_waitq
, &__wait
);
1080 set_current_state(TASK_INTERRUPTIBLE
);
1081 schedule_timeout(cfs_time_seconds(1));
1082 remove_wait_queue(&tctl
->tctl_waitq
, &__wait
);
1084 complete(&tctl
->tctl_stop
);
1088 int cfs_trace_start_thread(void)
1090 struct tracefiled_ctl
*tctl
= &trace_tctl
;
1091 struct task_struct
*task
;
1094 mutex_lock(&cfs_trace_thread_mutex
);
1098 init_completion(&tctl
->tctl_start
);
1099 init_completion(&tctl
->tctl_stop
);
1100 init_waitqueue_head(&tctl
->tctl_waitq
);
1101 atomic_set(&tctl
->tctl_shutdown
, 0);
1103 task
= kthread_run(tracefiled
, tctl
, "ktracefiled");
1109 wait_for_completion(&tctl
->tctl_start
);
1112 mutex_unlock(&cfs_trace_thread_mutex
);
1116 void cfs_trace_stop_thread(void)
1118 struct tracefiled_ctl
*tctl
= &trace_tctl
;
1120 mutex_lock(&cfs_trace_thread_mutex
);
1121 if (thread_running
) {
1122 pr_info("shutting down debug daemon thread...\n");
1123 atomic_set(&tctl
->tctl_shutdown
, 1);
1124 wait_for_completion(&tctl
->tctl_stop
);
1127 mutex_unlock(&cfs_trace_thread_mutex
);
1130 int cfs_tracefile_init(int max_pages
)
1132 struct cfs_trace_cpu_data
*tcd
;
1138 rc
= cfs_tracefile_init_arch();
1142 cfs_tcd_for_each(tcd
, i
, j
) {
1143 /* tcd_pages_factor is initialized int tracefile_init_arch. */
1144 factor
= tcd
->tcd_pages_factor
;
1145 INIT_LIST_HEAD(&tcd
->tcd_pages
);
1146 INIT_LIST_HEAD(&tcd
->tcd_stock_pages
);
1147 INIT_LIST_HEAD(&tcd
->tcd_daemon_pages
);
1148 tcd
->tcd_cur_pages
= 0;
1149 tcd
->tcd_cur_stock_pages
= 0;
1150 tcd
->tcd_cur_daemon_pages
= 0;
1151 tcd
->tcd_max_pages
= (max_pages
* factor
) / 100;
1152 LASSERT(tcd
->tcd_max_pages
> 0);
1153 tcd
->tcd_shutting_down
= 0;
1159 static void trace_cleanup_on_all_cpus(void)
1161 struct cfs_trace_cpu_data
*tcd
;
1162 struct cfs_trace_page
*tage
;
1163 struct cfs_trace_page
*tmp
;
1166 for_each_possible_cpu(cpu
) {
1167 cfs_tcd_for_each_type_lock(tcd
, i
, cpu
) {
1168 tcd
->tcd_shutting_down
= 1;
1170 list_for_each_entry_safe(tage
, tmp
, &tcd
->tcd_pages
,
1172 __LASSERT_TAGE_INVARIANT(tage
);
1174 list_del(&tage
->linkage
);
1175 cfs_tage_free(tage
);
1178 tcd
->tcd_cur_pages
= 0;
1183 static void cfs_trace_cleanup(void)
1185 struct page_collection pc
;
1187 INIT_LIST_HEAD(&pc
.pc_pages
);
1189 trace_cleanup_on_all_cpus();
1191 cfs_tracefile_fini_arch();
1194 void cfs_tracefile_exit(void)
1196 cfs_trace_stop_thread();
1197 cfs_trace_cleanup();