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_init(&tcd->tcd_pages, &pc->pc_pages);
254 tcd->tcd_cur_pages = 0;
255 if (pc->pc_want_daemon_pages) {
256 list_splice_init(&tcd->tcd_daemon_pages, &pc->pc_pages);
257 tcd->tcd_cur_daemon_pages = 0;
259 spin_unlock(&pc->pc_lock);
261 trace_put_tcd(tcd, flags);
264 static void collect_pages(struct page_collection *pc)
266 /* needs to be fixed up for preempt */
267 CFS_INIT_LIST_HEAD(&pc->pc_pages);
268 collect_pages_on_cpu(pc);
269 smp_call_function(collect_pages_on_cpu, pc, 0, 1);
272 static void put_pages_back_on_cpu(void *info)
274 struct page_collection *pc = info;
275 struct trace_cpu_data *tcd;
276 struct list_head *cur_head;
278 struct trace_page *tage;
279 struct trace_page *tmp;
281 tcd = trace_get_tcd(flags);
283 cur_head = tcd->tcd_pages.next;
285 spin_lock(&pc->pc_lock);
286 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
288 LASSERT(tage_invariant(tage));
290 if (tage->cpu != smp_processor_id())
293 tage_to_tail(tage, cur_head);
294 tcd->tcd_cur_pages++;
296 spin_unlock(&pc->pc_lock);
298 trace_put_tcd(tcd, flags);
301 static void put_pages_back(struct page_collection *pc)
303 /* needs to be fixed up for preempt */
304 put_pages_back_on_cpu(pc);
305 smp_call_function(put_pages_back_on_cpu, pc, 0, 1);
308 /* Add pages to a per-cpu debug daemon ringbuffer. This buffer makes sure that
309 * we have a good amount of data at all times for dumping during an LBUG, even
310 * if we have been steadily writing (and otherwise discarding) pages via the
312 static void put_pages_on_daemon_list_on_cpu(void *info)
314 struct page_collection *pc = info;
315 struct trace_cpu_data *tcd;
316 struct trace_page *tage;
317 struct trace_page *tmp;
320 tcd = trace_get_tcd(flags);
322 spin_lock(&pc->pc_lock);
323 list_for_each_entry_safe(tage, tmp, &pc->pc_pages, linkage) {
325 LASSERT(tage_invariant(tage));
327 if (tage->cpu != smp_processor_id())
330 tage_to_tail(tage, &tcd->tcd_daemon_pages);
331 tcd->tcd_cur_daemon_pages++;
333 if (tcd->tcd_cur_daemon_pages > tcd->tcd_max_pages) {
334 struct trace_page *victim;
336 LASSERT(!list_empty(&tcd->tcd_daemon_pages));
337 victim = tage_from_list(tcd->tcd_daemon_pages.next);
339 LASSERT(tage_invariant(victim));
341 list_del(&victim->linkage);
343 tcd->tcd_cur_daemon_pages--;
346 spin_unlock(&pc->pc_lock);
348 trace_put_tcd(tcd, flags);
351 static void put_pages_on_daemon_list(struct page_collection *pc)
353 put_pages_on_daemon_list_on_cpu(pc);
354 smp_call_function(put_pages_on_daemon_list_on_cpu, pc, 0, 1);
357 void trace_debug_print(void)
359 struct page_collection pc;
360 struct trace_page *tage;
361 struct trace_page *tmp;
363 spin_lock_init(&pc.pc_lock);
366 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
370 LASSERT(tage_invariant(tage));
373 p = cfs_page_address(page);
374 while (p < ((char *)cfs_page_address(page) + CFS_PAGE_SIZE)) {
375 struct ptldebug_header *hdr;
380 p += strlen(file) + 1;
383 len = hdr->ph_len - (p - (char *)hdr);
385 print_to_console(hdr, D_EMERG, p, len, file, fn);
388 list_del(&tage->linkage);
393 int tracefile_dump_all_pages(char *filename)
395 struct page_collection pc;
397 struct trace_page *tage;
398 struct trace_page *tmp;
402 down_write(&tracefile_sem);
404 filp = cfs_filp_open(filename,
405 O_CREAT|O_EXCL|O_WRONLY|O_LARGEFILE, 0600, &rc);
407 printk(KERN_ERR "LustreError: can't open %s for dump: rc %d\n",
412 spin_lock_init(&pc.pc_lock);
413 pc.pc_want_daemon_pages = 1;
415 if (list_empty(&pc.pc_pages)) {
420 /* ok, for now, just write the pages. in the future we'll be building
421 * iobufs with the pages and calling generic_direct_IO */
423 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
425 LASSERT(tage_invariant(tage));
427 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
428 tage->used, cfs_filp_poff(filp));
429 if (rc != tage->used) {
430 printk(KERN_WARNING "wanted to write %u but wrote "
431 "%d\n", tage->used, rc);
435 list_del(&tage->linkage);
439 rc = cfs_filp_fsync(filp);
441 printk(KERN_ERR "sync returns %d\n", rc);
443 cfs_filp_close(filp);
445 up_write(&tracefile_sem);
449 void trace_flush_pages(void)
451 struct page_collection pc;
452 struct trace_page *tage;
453 struct trace_page *tmp;
455 spin_lock_init(&pc.pc_lock);
458 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
460 LASSERT(tage_invariant(tage));
462 list_del(&tage->linkage);
467 int trace_dk(struct file *file, const char *buffer, unsigned long count,
474 name = cfs_alloc(count + 1, CFS_ALLOC_STD);
478 if (copy_from_user(name, buffer, count)) {
483 if (name[0] != '/') {
488 /* be nice and strip out trailing '\n' */
489 for (off = count ; off > 2 && isspace(name[off - 1]); off--)
493 rc = tracefile_dump_all_pages(name);
499 EXPORT_SYMBOL(trace_dk);
501 static int tracefiled(void *arg)
503 struct page_collection pc;
504 struct tracefiled_ctl *tctl = arg;
505 struct trace_page *tage;
506 struct trace_page *tmp;
507 struct ptldebug_header *hdr;
512 /* we're started late enough that we pick up init's fs context */
513 /* this is so broken in uml? what on earth is going on? */
514 kportal_daemonize("ktracefiled");
517 spin_lock_init(&pc.pc_lock);
518 complete(&tctl->tctl_start);
521 cfs_waitlink_t __wait;
523 cfs_waitlink_init(&__wait);
524 cfs_waitq_add(&tctl->tctl_waitq, &__wait);
525 set_current_state(TASK_INTERRUPTIBLE);
526 cfs_waitq_timedwait(&__wait, cfs_time_seconds(1));
527 cfs_waitq_del(&tctl->tctl_waitq, &__wait);
529 if (atomic_read(&tctl->tctl_shutdown))
532 pc.pc_want_daemon_pages = 0;
534 if (list_empty(&pc.pc_pages))
538 down_read(&tracefile_sem);
539 if (tracefile != NULL) {
540 filp = cfs_filp_open(tracefile, O_CREAT|O_RDWR|O_LARGEFILE,
543 printk("couldn't open %s: %d\n", tracefile, rc);
545 up_read(&tracefile_sem);
547 put_pages_on_daemon_list(&pc);
553 /* mark the first header, so we can sort in chunks */
554 tage = tage_from_list(pc.pc_pages.next);
555 LASSERT(tage_invariant(tage));
557 hdr = cfs_page_address(tage->page);
558 hdr->ph_flags |= PH_FLAG_FIRST_RECORD;
560 list_for_each_entry_safe(tage, tmp, &pc.pc_pages, linkage) {
563 LASSERT(tage_invariant(tage));
565 if (f_pos >= tracefile_size)
567 else if (f_pos > cfs_filp_size(filp))
568 f_pos = cfs_filp_size(filp);
570 rc = cfs_filp_write(filp, cfs_page_address(tage->page),
572 if (rc != tage->used) {
573 printk(KERN_WARNING "wanted to write %u but "
574 "wrote %d\n", tage->used, rc);
579 cfs_filp_close(filp);
581 put_pages_on_daemon_list(&pc);
583 complete(&tctl->tctl_stop);
587 int trace_start_thread(void)
589 struct tracefiled_ctl *tctl = &trace_tctl;
592 mutex_down(&trace_thread_sem);
596 init_completion(&tctl->tctl_start);
597 init_completion(&tctl->tctl_stop);
598 cfs_waitq_init(&tctl->tctl_waitq);
599 atomic_set(&tctl->tctl_shutdown, 0);
601 if (cfs_kernel_thread(tracefiled, tctl, 0) < 0) {
606 wait_for_completion(&tctl->tctl_start);
609 mutex_up(&trace_thread_sem);
613 void trace_stop_thread(void)
615 struct tracefiled_ctl *tctl = &trace_tctl;
617 mutex_down(&trace_thread_sem);
618 if (thread_running) {
619 printk(KERN_INFO "Shutting down debug daemon thread...\n");
620 atomic_set(&tctl->tctl_shutdown, 1);
621 wait_for_completion(&tctl->tctl_stop);
624 mutex_up(&trace_thread_sem);
627 int tracefile_init(void)
629 struct trace_cpu_data *tcd;
632 for (i = 0; i < NR_CPUS; i++) {
633 tcd = &trace_data[i].tcd;
634 CFS_INIT_LIST_HEAD(&tcd->tcd_pages);
635 CFS_INIT_LIST_HEAD(&tcd->tcd_daemon_pages);
636 tcd->tcd_cur_pages = 0;
637 tcd->tcd_cur_daemon_pages = 0;
638 tcd->tcd_max_pages = TCD_MAX_PAGES;
639 tcd->tcd_shutting_down = 0;
644 static void trace_cleanup_on_cpu(void *info)
646 struct trace_cpu_data *tcd;
647 struct trace_page *tage;
648 struct trace_page *tmp;
651 tcd = trace_get_tcd(flags);
653 tcd->tcd_shutting_down = 1;
655 list_for_each_entry_safe(tage, tmp, &tcd->tcd_pages, linkage) {
656 LASSERT(tage_invariant(tage));
658 list_del(&tage->linkage);
661 tcd->tcd_cur_pages = 0;
663 trace_put_tcd(tcd, flags);
666 static void trace_cleanup(void)
668 struct page_collection pc;
670 CFS_INIT_LIST_HEAD(&pc.pc_pages);
671 spin_lock_init(&pc.pc_lock);
673 trace_cleanup_on_cpu(&pc);
674 smp_call_function(trace_cleanup_on_cpu, &pc, 0, 1);
677 void tracefile_exit(void)