Whamcloud - gitweb
7c0095d2e35a27143492390c899d40cc0434463e
[fs/lustre-release.git] / libcfs / libcfs / darwin / darwin-tracefile.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  */
36
37 #define DEBUG_SUBSYSTEM S_LNET
38 #define LUSTRE_TRACEFILE_PRIVATE
39 #include <libcfs/libcfs.h>
40 #include "tracefile.h"
41
42 /*
43  * We can't support smp tracefile currently.
44  * Everything is put on one cpu.
45  */
46
47 #define M_TCD_MAX_PAGES (128 * 1280)
48
49 static long max_permit_mb = (64 * 1024);
50
51 spinlock_t trace_cpu_serializer;
52
53 /*
54  * thread currently executing tracefile code or NULL if none does. Used to
55  * detect recursive calls to libcfs_debug_msg().
56  */
57 static thread_t trace_owner = NULL;
58
59 extern int get_preemption_level(void);
60 extern atomic_t tage_allocated;
61
62 struct rw_semaphore tracefile_sem;
63
64 int tracefile_init_arch() {
65     init_rwsem(&tracefile_sem);
66 #error "Todo: initialise per-cpu console buffers"
67     return 0;
68 }
69
70 void tracefile_fini_arch() {
71 }
72
73 void tracefile_read_lock() {
74     down_read(&tracefile_sem);
75 }
76
77 void tracefile_read_unlock() {
78     up_read(&tracefile_sem);
79 }
80
81 void tracefile_write_lock() {
82     down_write(&tracefile_sem);
83 }
84
85 void tracefile_write_unlock() {
86     up_write(&tracefile_sem);
87 }
88
89 char *trace_get_console_buffer(void)
90 {
91 #error "todo: return a per-cpu/interrupt console buffer and disable pre-emption"
92 }
93
94 void trace_put_console_buffer(char *buffer)
95 {
96 #error "todo: re-enable pre-emption"
97 }
98
99 struct trace_cpu_data *trace_get_tcd(void)
100 {
101         struct trace_cpu_data *tcd;
102         int nr_pages;
103         struct list_head pages;
104
105         /*
106          * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
107          * from here: this will lead to infinite recursion.
108          */
109
110         /*
111          * debugging check for recursive call to libcfs_debug_msg()
112          */
113         if (trace_owner == current_thread()) {
114                 /*
115                  * Cannot assert here.
116                  */
117                 printk(KERN_EMERG "recursive call to %s", __FUNCTION__);
118                 /*
119                  * "The death of God left the angels in a strange position."
120                  */
121                 cfs_enter_debugger();
122         }
123         tcd = &trace_data[0].tcd;
124         CFS_INIT_LIST_HEAD(&pages);
125         if (get_preemption_level() == 0)
126                 nr_pages = trace_refill_stock(tcd, CFS_ALLOC_STD, &pages);
127         else
128                 nr_pages = 0;
129         spin_lock(&trace_cpu_serializer);
130         trace_owner = current_thread();
131         tcd->tcd_cur_stock_pages += nr_pages;
132         list_splice(&pages, &tcd->tcd_stock_pages);
133         return tcd;
134 }
135
136 extern void raw_page_death_row_clean(void);
137
138 void __trace_put_tcd(struct trace_cpu_data *tcd)
139 {
140         /*
141          * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
142          * from here: this will lead to infinite recursion.
143          */
144         LASSERT(trace_owner == current_thread());
145         trace_owner = NULL;
146         spin_unlock(&trace_cpu_serializer);
147         if (get_preemption_level() == 0)
148                 /* purge all pending pages */
149                 raw_page_death_row_clean();
150 }
151
152 int tcd_owns_tage(struct trace_cpu_data *tcd, struct trace_page *tage)
153 {
154         /*
155          * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
156          * from here: this will lead to infinite recursion.
157          */
158         /* XNU has global tcd, and all pages are owned by it */
159         return 1;
160 }
161
162 void
163 set_ptldebug_header(struct ptldebug_header *header, int subsys, int mask,
164                     const int line, unsigned long stack)
165 {
166         struct timeval tv;
167         
168         /*
169          * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
170          * from here: this will lead to infinite recursion.
171          */
172         do_gettimeofday(&tv);
173         header->ph_subsys = subsys;
174         header->ph_mask = mask;
175         header->ph_cpu_id = smp_processor_id();
176         header->ph_sec = (__u32)tv.tv_sec;
177         header->ph_usec = tv.tv_usec;
178         header->ph_stack = stack;
179         header->ph_pid = cfs_curproc_pid();
180         header->ph_line_num = line;
181         header->ph_extern_pid = (__u32)current_thread();
182 }
183
184 void print_to_console(struct ptldebug_header *hdr, int mask, const char *buf,
185                       int len, const char *file, const char *fn)
186 {
187         char *prefix = "Lustre", *ptype = KERN_INFO;
188
189         /*
190          * XXX nikita: do NOT call libcfs_debug_msg() (CDEBUG/ENTRY/EXIT)
191          * from here: this will lead to infinite recursion.
192          */
193         if ((mask & D_EMERG) != 0) {
194                 prefix = "LustreError";
195                 ptype = KERN_EMERG;
196         } else if ((mask & D_ERROR) != 0) {
197                 prefix = "LustreError";
198                 ptype = KERN_ERR;
199         } else if ((mask & D_WARNING) != 0) {
200                 prefix = "Lustre";
201                 ptype = KERN_WARNING;
202         } else if ((mask & libcfs_printk) != 0 || (mask & D_CONSOLE)) {
203                 prefix = "Lustre";
204                 ptype = KERN_INFO;
205         }
206
207         if ((mask & D_CONSOLE) != 0) {
208                 printk("%s%s: %.*s", ptype, prefix, len, buf);
209         } else {
210                 printk("%s%s: %d:%d:(%s:%d:%s()) %*s",
211                        ptype, prefix, hdr->ph_pid, hdr->ph_extern_pid,
212                        file, hdr->ph_line_num, fn, len, buf);
213         }
214 }
215
216 int trace_max_debug_mb(void)
217 {
218         return max_permit_mb;
219 }
220
221 void
222 trace_call_on_all_cpus(void (*fn)(void *arg), void *arg)
223 {
224 #error "tbd"
225 }