1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
5 * Author: Zach Brown <zab@clusterfs.com>
6 * Author: Phil Schwan <phil@clusterfs.com>
8 * This file is part of Lustre, http://www.lustre.org.
10 * Lustre is free software; you can redistribute it and/or
11 * modify it under the terms of version 2 of the GNU General Public
12 * License as published by the Free Software Foundation.
14 * Lustre is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Lustre; if not, write to the Free Software
21 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #define DEBUG_SUBSYSTEM S_PORTALS
26 #define LUSTRE_TRACEFILE_PRIVATE
27 #include "tracefile.h"
29 #include <libcfs/kp30.h>
30 #include <libcfs/libcfs.h>
32 /* XXX move things up to the top, comment */
33 union trace_data_union trace_data[NR_CPUS] __cacheline_aligned;
35 struct rw_semaphore tracefile_sem;
36 char *tracefile = NULL;
37 long long tracefile_size = TRACEFILE_SIZE;
38 static struct tracefiled_ctl trace_tctl;
39 struct semaphore trace_thread_sem;
40 static int thread_running = 0;
42 static void put_pages_on_daemon_list_on_cpu(void *info);
44 static inline struct trace_page *tage_from_list(struct list_head *list)
46 return list_entry(list, struct trace_page, linkage);
49 static struct trace_page *tage_alloc(int gfp)
52 struct trace_page *tage;
54 page = cfs_alloc_page(gfp);
58 tage = cfs_alloc(sizeof(*tage), gfp);
68 static void tage_free(struct trace_page *tage)
70 LASSERT(tage != NULL);
71 LASSERT(tage->page != NULL);
73 cfs_free_page(tage->page);
77 static void tage_to_tail(struct trace_page *tage, struct list_head *queue)
79 LASSERT(tage != NULL);
80 LASSERT(queue != NULL);
82 list_move_tail(&tage->linkage, queue);
85 static int tage_invariant(struct trace_page *tage)
87 return (tage != NULL &&
89 tage->used <= CFS_PAGE_SIZE &&
90 cfs_page_count(tage->page) > 0);
93 /* return a page that has 'len' bytes left at the end */
94 static struct trace_page *trace_get_tage(struct trace_cpu_data *tcd,
97 struct trace_page *tage;
99 if (len > CFS_PAGE_SIZE) {
100 printk(KERN_ERR "cowardly refusing to write %lu bytes in a "
105 if (!list_empty(&tcd->tcd_pages)) {
106 tage = tage_from_list(tcd->tcd_pages.prev);
107 if (tage->used + len <= CFS_PAGE_SIZE)
111 if (tcd->tcd_cur_pages < tcd->tcd_max_pages) {
112 tage = tage_alloc(CFS_ALLOC_ATOMIC);
114 /* the kernel should print a message for us. fall back
115 * to using the last page in the ring buffer. */
120 tage->cpu = smp_processor_id();
121 list_add_tail(&tage->linkage, &tcd->tcd_pages);
122 tcd->tcd_cur_pages++;
124 if (tcd->tcd_cur_pages > 8 && thread_running) {
125 struct tracefiled_ctl *tctl = &trace_tctl;
126 cfs_waitq_signal(&tctl->tctl_waitq);
132 if (thread_running) {
133 int pgcount = tcd->tcd_cur_pages / 10;
134 struct page_collection pc;
135 struct trace_page *tage;
136 struct trace_page *tmp;
138 printk(KERN_WARNING "debug daemon buffer overflowed; discarding"
139 " 10%% of pages (%d)\n", pgcount + 1);
141 CFS_INIT_LIST_HEAD(&pc.pc_pages);
142 spin_lock_init(&pc.pc_lock);
144 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
148 list_move_tail(&tage->linkage, &pc.pc_pages);
149 tcd->tcd_cur_pages--;
151 put_pages_on_daemon_list_on_cpu(&pc);
153 LASSERT(!list_empty(&tcd->tcd_pages));
156 if (list_empty(&tcd->tcd_pages))
159 tage = tage_from_list(tcd->tcd_pages.next);
161 tage_to_tail(tage, &tcd->tcd_pages);
166 void portals_debug_msg(int subsys, int mask, char *file, const char *fn,
167 const int line, unsigned long stack, char *format, ...)
169 struct trace_cpu_data *tcd;
170 struct ptldebug_header header;
171 struct trace_page *tage;
172 char *debug_buf = format;
173 int known_size, needed = 85 /* average message length */, max_nob;
178 if (mask == D_PORTALS && !(portal_debug & D_PORTALS))
181 if (strchr(file, '/'))
182 file = strrchr(file, '/') + 1;
184 if (*(format + strlen(format) - 1) != '\n')
185 printk(KERN_INFO "format at %s:%d:%s doesn't end in newline\n",
188 tcd = trace_get_tcd(flags);
189 if (tcd->tcd_shutting_down)
192 set_ptldebug_header(&header, subsys, mask, line, stack);
193 known_size = sizeof(header) + strlen(file) + strlen(fn) + 2; // nulls
196 tage = trace_get_tage(tcd, needed + known_size);
199 if (needed + known_size > CFS_PAGE_SIZE)
201 needed = strlen(format);
205 debug_buf = cfs_page_address(tage->page) + tage->used + known_size;
207 max_nob = CFS_PAGE_SIZE - tage->used - known_size;
208 LASSERT(max_nob > 0);
209 va_start(ap, format);
210 needed = vsnprintf(debug_buf, max_nob, format, ap);
213 if (needed > max_nob) /* overflow. oh poop. */
216 header.ph_len = known_size + needed;
217 debug_buf = cfs_page_address(tage->page) + tage->used;
219 memcpy(debug_buf, &header, sizeof(header));
220 tage->used += sizeof(header);
221 debug_buf += sizeof(header);
223 strcpy(debug_buf, file);
224 tage->used += strlen(file) + 1;
225 debug_buf += strlen(file) + 1;
227 strcpy(debug_buf, fn);
228 tage->used += strlen(fn) + 1;
229 debug_buf += strlen(fn) + 1;
231 tage->used += needed;
232 if (tage->used > CFS_PAGE_SIZE)
234 "tage->used == %u in portals_debug_msg\n", tage->used);
237 if ((mask & (D_EMERG | D_ERROR | D_WARNING | D_CONSOLE)) || portal_printk)
238 print_to_console(&header, mask, debug_buf, needed, file, fn);
240 trace_put_tcd(tcd, flags);
242 EXPORT_SYMBOL(portals_debug_msg);
244 static void collect_pages_on_cpu(void *info)
246 struct trace_cpu_data *tcd;
248 struct page_collection *pc = info;
250 tcd = trace_get_tcd(flags);
252 spin_lock(&pc->pc_lock);
253 list_splice(&tcd->tcd_pages, &pc->pc_pages);
254 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
255 tcd->tcd_cur_pages = 0;
256 if (pc->pc_want_daemon_pages) {
257 list_splice(&tcd->tcd_daemon_pages, &pc->pc_pages);
258 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
259 tcd->tcd_cur_daemon_pages = 0;
261 spin_unlock(&pc->pc_lock);
263 trace_put_tcd(tcd, flags);
266 static void collect_pages(struct page_collection *pc)
268 /* needs to be fixed up for preempt */
269 CFS_INIT_LIST_HEAD(&pc->pc_pages);
270 collect_pages_on_cpu(pc);
271 smp_call_function(collect_pages_on_cpu, pc, 0, 1);
274 static void put_pages_back_on_cpu(void *info)
276 struct page_collection *pc = info;
277 struct trace_cpu_data *tcd;
278 struct list_head *cur_head;
280 struct trace_page *tage;
281 struct trace_page *tmp;
283 tcd = trace_get_tcd(flags);
285 cur_head = tcd->tcd_pages.next;
287 spin_lock(&pc->pc_lock);
288 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
290 LASSERT(tage_invariant(tage));
292 if (tage->cpu != smp_processor_id())
295 tage_to_tail(tage, cur_head);
296 tcd->tcd_cur_pages++;
298 spin_unlock(&pc->pc_lock);
300 trace_put_tcd(tcd, flags);
303 static void put_pages_back(struct page_collection *pc)
305 /* needs to be fixed up for preempt */
306 put_pages_back_on_cpu(pc);
307 smp_call_function(put_pages_back_on_cpu, pc, 0, 1);
310 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
311 * we have a good amount of data at all times for dumping during an LBUG, even
312 * if we have been steadily writing (and otherwise discarding) pages via the
314 static void put_pages_on_daemon_list_on_cpu(void *info)
316 struct page_collection *pc = info;
317 struct trace_cpu_data *tcd;
318 struct trace_page *tage;
319 struct trace_page *tmp;
322 tcd = trace_get_tcd(flags);
324 spin_lock(&pc->pc_lock);
325 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
327 LASSERT(tage_invariant(tage));
329 if (tage->cpu != smp_processor_id())
332 tage_to_tail(tage, &tcd->tcd_daemon_pages);
333 tcd->tcd_cur_daemon_pages++;
335 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
336 struct trace_page *victim;
338 LASSERT(!list_empty(&tcd->tcd_daemon_pages));
339 victim = tage_from_list(tcd->tcd_daemon_pages.next);
341 LASSERT(tage_invariant(victim));
343 list_del(&victim->linkage);
345 tcd->tcd_cur_daemon_pages--;
348 spin_unlock(&pc->pc_lock);
350 trace_put_tcd(tcd, flags);
353 static void put_pages_on_daemon_list(struct page_collection *pc)
355 put_pages_on_daemon_list_on_cpu(pc);
356 smp_call_function(put_pages_on_daemon_list_on_cpu, pc, 0, 1);
359 void trace_debug_print(void)
361 struct page_collection pc;
362 struct trace_page *tage;
363 struct trace_page *tmp;
365 spin_lock_init(&pc.pc_lock);
368 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
372 LASSERT(tage_invariant(tage));
375 p = cfs_page_address(page);
376 while (p < ((char *)cfs_page_address(page) + CFS_PAGE_SIZE)) {
377 struct ptldebug_header *hdr;
382 p += strlen(file) + 1;
385 len = hdr->ph_len - (p - (char *)hdr);
387 print_to_console(hdr, D_EMERG, p, len, file, fn);
390 list_del(&tage->linkage);
395 int tracefile_dump_all_pages(char *filename)
397 struct page_collection pc;
399 struct trace_page *tage;
400 struct trace_page *tmp;
404 down_write(&tracefile_sem);
406 filp = cfs_filp_open(filename,
407 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
409 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
414 spin_lock_init(&pc.pc_lock);
415 pc.pc_want_daemon_pages = 1;
417 if (list_empty(&pc.pc_pages)) {
422 /* ok, for now, just write the pages. in the future we'll be building
423 * iobufs with the pages and calling generic_direct_IO */
425 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
427 LASSERT(tage_invariant(tage));
429 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
430 tage->used, cfs_filp_poff(filp));
431 if (rc != tage->used) {
432 printk(KERN_WARNING "wanted to write %u but wrote "
433 "%d\n", tage->used, rc);
437 list_del(&tage->linkage);
441 rc = cfs_filp_fsync(filp);
443 printk(KERN_ERR "sync returns %d\n", rc);
445 cfs_filp_close(filp);
447 up_write(&tracefile_sem);
451 void trace_flush_pages(void)
453 struct page_collection pc;
454 struct trace_page *tage;
455 struct trace_page *tmp;
457 spin_lock_init(&pc.pc_lock);
460 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
462 LASSERT(tage_invariant(tage));
464 list_del(&tage->linkage);
469 int trace_dk(struct file *file, const char *buffer, unsigned long count,
476 name = cfs_alloc(count + 1, CFS_ALLOC_STD);
480 if (copy_from_user(name, buffer, count)) {
485 if (name[0] != '/') {
490 /* be nice and strip out trailing '\n' */
491 for (off = count ; off > 2 && isspace(name[off - 1]); off--)
495 rc = tracefile_dump_all_pages(name);
501 EXPORT_SYMBOL(trace_dk);
503 static int tracefiled(void *arg)
505 struct page_collection pc;
506 struct tracefiled_ctl *tctl = arg;
507 struct trace_page *tage;
508 struct trace_page *tmp;
509 struct ptldebug_header *hdr;
514 /* we're started late enough that we pick up init's fs context */
515 /* this is so broken in uml? what on earth is going on? */
516 kportal_daemonize("ktracefiled");
519 spin_lock_init(&pc.pc_lock);
520 complete(&tctl->tctl_start);
523 cfs_waitlink_t __wait;
525 cfs_waitlink_init(&__wait);
526 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
527 set_current_state(TASK_INTERRUPTIBLE);
528 cfs_waitq_timedwait(&__wait, cfs_time_seconds(1));
529 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
531 if (atomic_read(&tctl->tctl_shutdown))
534 pc.pc_want_daemon_pages = 0;
536 if (list_empty(&pc.pc_pages))
540 down_read(&tracefile_sem);
541 if (tracefile != NULL) {
542 filp = cfs_filp_open(tracefile, O_CREAT|O_RDWR|O_LARGEFILE,
545 printk("couldn't open %s: %d\n", tracefile, rc);
547 up_read(&tracefile_sem);
549 put_pages_on_daemon_list(&pc);
555 /* mark the first header, so we can sort in chunks */
556 tage = tage_from_list(pc.pc_pages.next);
557 LASSERT(tage_invariant(tage));
559 hdr = cfs_page_address(tage->page);
560 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
562 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
565 LASSERT(tage_invariant(tage));
567 if (f_pos >= tracefile_size)
569 else if (f_pos > cfs_filp_size(filp))
570 f_pos = cfs_filp_size(filp);
572 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
574 if (rc != tage->used) {
575 printk(KERN_WARNING "wanted to write %u but "
576 "wrote %d\n", tage->used, rc);
582 cfs_filp_close(filp);
583 put_pages_on_daemon_list(&pc);
585 complete(&tctl->tctl_stop);
589 int trace_start_thread(void)
591 struct tracefiled_ctl *tctl = &trace_tctl;
594 mutex_down(&trace_thread_sem);
598 init_completion(&tctl->tctl_start);
599 init_completion(&tctl->tctl_stop);
600 cfs_waitq_init(&tctl->tctl_waitq);
601 atomic_set(&tctl->tctl_shutdown, 0);
603 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
608 wait_for_completion(&tctl->tctl_start);
611 mutex_up(&trace_thread_sem);
615 void trace_stop_thread(void)
617 struct tracefiled_ctl *tctl = &trace_tctl;
619 mutex_down(&trace_thread_sem);
620 if (thread_running) {
621 printk(KERN_INFO "Shutting down debug daemon thread...\n");
622 atomic_set(&tctl->tctl_shutdown, 1);
623 wait_for_completion(&tctl->tctl_stop);
626 mutex_up(&trace_thread_sem);
629 int tracefile_init(void)
631 struct trace_cpu_data *tcd;
634 for (i = 0; i < NR_CPUS; i++) {
635 tcd = &trace_data[i].tcd;
636 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
637 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
638 tcd->tcd_cur_pages = 0;
639 tcd->tcd_cur_daemon_pages = 0;
640 tcd->tcd_max_pages = TCD_MAX_PAGES;
641 tcd->tcd_shutting_down = 0;
646 static void trace_cleanup_on_cpu(void *info)
648 struct trace_cpu_data *tcd;
649 struct trace_page *tage;
650 struct trace_page *tmp;
653 tcd = trace_get_tcd(flags);
655 tcd->tcd_shutting_down = 1;
657 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
658 LASSERT(tage_invariant(tage));
660 list_del(&tage->linkage);
663 tcd->tcd_cur_pages = 0;
665 trace_put_tcd(tcd, flags);
668 static void trace_cleanup(void)
670 struct page_collection pc;
672 CFS_INIT_LIST_HEAD(&pc.pc_pages);
673 spin_lock_init(&pc.pc_lock);
675 trace_cleanup_on_cpu(&pc);
676 smp_call_function(trace_cleanup_on_cpu, &pc, 0, 1);
679 void tracefile_exit(void)