Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[fs/lustre-release.git] / libcfs / libcfs / tracefile.h
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef __LIBCFS_TRACEFILE_H__
36 #define __LIBCFS_TRACEFILE_H__
37
38 #include <libcfs/libcfs.h>
39
40 #if defined(__linux__)
41 #include "linux/linux-tracefile.h"
42 #elif defined(__WINNT__)
43 #include "winnt/winnt-tracefile.h"
44 #else
45 #error Unsupported operating system.
46 #endif
47
48 /* trace file lock routines */
49
50 #define TRACEFILE_NAME_SIZE 1024
51 extern char      cfs_tracefile[TRACEFILE_NAME_SIZE];
52 extern long long cfs_tracefile_size;
53
54 extern void libcfs_run_debug_log_upcall(char *file);
55
56 int  cfs_tracefile_init_arch(void);
57 void cfs_tracefile_fini_arch(void);
58
59 void cfs_tracefile_read_lock(void);
60 void cfs_tracefile_read_unlock(void);
61 void cfs_tracefile_write_lock(void);
62 void cfs_tracefile_write_unlock(void);
63
64 int cfs_tracefile_dump_all_pages(char *filename);
65 void cfs_trace_debug_print(void);
66 void cfs_trace_flush_pages(void);
67 int cfs_trace_start_thread(void);
68 void cfs_trace_stop_thread(void);
69 int cfs_tracefile_init(int max_pages);
70 void cfs_tracefile_exit(void);
71
72
73
74 int cfs_trace_copyin_string(char *knl_buffer, int knl_buffer_nob,
75                             const char *usr_buffer, int usr_buffer_nob);
76 int cfs_trace_copyout_string(char *usr_buffer, int usr_buffer_nob,
77                              const char *knl_str, char *append);
78 int cfs_trace_allocate_string_buffer(char **str, int nob);
79 void cfs_trace_free_string_buffer(char *str, int nob);
80 int cfs_trace_dump_debug_buffer_usrstr(void *usr_str, int usr_str_nob);
81 int cfs_trace_daemon_command(char *str);
82 int cfs_trace_daemon_command_usrstr(void *usr_str, int usr_str_nob);
83 int cfs_trace_set_debug_mb(int mb);
84 int cfs_trace_set_debug_mb_usrstr(void *usr_str, int usr_str_nob);
85 int cfs_trace_get_debug_mb(void);
86
87 extern void libcfs_debug_dumplog_internal(void *arg);
88 extern void libcfs_register_panic_notifier(void);
89 extern void libcfs_unregister_panic_notifier(void);
90 extern int  libcfs_panic_in_progress;
91 extern int  cfs_trace_max_debug_mb(void);
92
93 #define TCD_MAX_PAGES (5 << (20 - CFS_PAGE_SHIFT))
94 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
95 #define CFS_TRACEFILE_SIZE (500 << 20)
96
97 #ifdef LUSTRE_TRACEFILE_PRIVATE
98
99 /*
100  * Private declare for tracefile
101  */
102 #define TCD_MAX_PAGES (5 << (20 - CFS_PAGE_SHIFT))
103 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
104
105 #define CFS_TRACEFILE_SIZE (500 << 20)
106
107 /* Size of a buffer for sprinting console messages if we can't get a page
108  * from system */
109 #define CFS_TRACE_CONSOLE_BUFFER_SIZE   1024
110
111 union cfs_trace_data_union {
112         struct cfs_trace_cpu_data {
113                 /*
114                  * Even though this structure is meant to be per-CPU, locking
115                  * is needed because in some places the data may be accessed
116                  * from other CPUs. This lock is directly used in trace_get_tcd
117                  * and trace_put_tcd, which are called in libcfs_debug_vmsg2 and
118                  * tcd_for_each_type_lock
119                  */
120                 spinlock_t              tcd_lock;
121                 unsigned long           tcd_lock_flags;
122
123                 /*
124                  * pages with trace records not yet processed by tracefiled.
125                  */
126                 cfs_list_t              tcd_pages;
127                 /* number of pages on ->tcd_pages */
128                 unsigned long           tcd_cur_pages;
129
130                 /*
131                  * pages with trace records already processed by
132                  * tracefiled. These pages are kept in memory, so that some
133                  * portion of log can be written in the event of LBUG. This
134                  * list is maintained in LRU order.
135                  *
136                  * Pages are moved to ->tcd_daemon_pages by tracefiled()
137                  * (put_pages_on_daemon_list()). LRU pages from this list are
138                  * discarded when list grows too large.
139                  */
140                 cfs_list_t              tcd_daemon_pages;
141                 /* number of pages on ->tcd_daemon_pages */
142                 unsigned long           tcd_cur_daemon_pages;
143
144                 /*
145                  * Maximal number of pages allowed on ->tcd_pages and
146                  * ->tcd_daemon_pages each.
147                  * Always TCD_MAX_PAGES * tcd_pages_factor / 100 in current
148                  * implementation.
149                  */
150                 unsigned long           tcd_max_pages;
151
152                 /*
153                  * preallocated pages to write trace records into. Pages from
154                  * ->tcd_stock_pages are moved to ->tcd_pages by
155                  * portals_debug_msg().
156                  *
157                  * This list is necessary, because on some platforms it's
158                  * impossible to perform efficient atomic page allocation in a
159                  * non-blockable context.
160                  *
161                  * Such platforms fill ->tcd_stock_pages "on occasion", when
162                  * tracing code is entered in blockable context.
163                  *
164                  * trace_get_tage_try() tries to get a page from
165                  * ->tcd_stock_pages first and resorts to atomic page
166                  * allocation only if this queue is empty. ->tcd_stock_pages
167                  * is replenished when tracing code is entered in blocking
168                  * context (darwin-tracefile.c:trace_get_tcd()). We try to
169                  * maintain TCD_STOCK_PAGES (40 by default) pages in this
170                  * queue. Atomic allocation is only required if more than
171                  * TCD_STOCK_PAGES pagesful are consumed by trace records all
172                  * emitted in non-blocking contexts. Which is quite unlikely.
173                  */
174                 cfs_list_t              tcd_stock_pages;
175                 /* number of pages on ->tcd_stock_pages */
176                 unsigned long           tcd_cur_stock_pages;
177
178                 unsigned short          tcd_shutting_down;
179                 unsigned short          tcd_cpu;
180                 unsigned short          tcd_type;
181                 /* The factors to share debug memory. */
182                 unsigned short          tcd_pages_factor;
183         } tcd;
184         char __pad[CFS_L1_CACHE_ALIGN(sizeof(struct cfs_trace_cpu_data))];
185 };
186
187 #define TCD_MAX_TYPES      8
188 extern union cfs_trace_data_union (*cfs_trace_data[TCD_MAX_TYPES])[CFS_NR_CPUS];
189
190 #define cfs_tcd_for_each(tcd, i, j)                                       \
191     for (i = 0; cfs_trace_data[i] != NULL; i++)                           \
192         for (j = 0, ((tcd) = &(*cfs_trace_data[i])[j].tcd);               \
193              j < cfs_num_possible_cpus();                                 \
194              j++, (tcd) = &(*cfs_trace_data[i])[j].tcd)
195
196 #define cfs_tcd_for_each_type_lock(tcd, i, cpu)                           \
197     for (i = 0; cfs_trace_data[i] &&                                      \
198          (tcd = &(*cfs_trace_data[i])[cpu].tcd) &&                        \
199          cfs_trace_lock_tcd(tcd, 1); cfs_trace_unlock_tcd(tcd, 1), i++)
200
201 /* XXX nikita: this declaration is internal to tracefile.c and should probably
202  * be moved there */
203 struct page_collection {
204         cfs_list_t      pc_pages;
205         /*
206          * spin-lock protecting ->pc_pages. It is taken by smp_call_function()
207          * call-back functions. XXX nikita: Which is horrible: all processors
208          * receive NMI at the same time only to be serialized by this
209          * lock. Probably ->pc_pages should be replaced with an array of
210          * NR_CPUS elements accessed locklessly.
211          */
212         spinlock_t      pc_lock;
213         /*
214          * if this flag is set, collect_pages() will spill both
215          * ->tcd_daemon_pages and ->tcd_pages to the ->pc_pages. Otherwise,
216          * only ->tcd_pages are spilled.
217          */
218         int             pc_want_daemon_pages;
219 };
220
221 /* XXX nikita: this declaration is internal to tracefile.c and should probably
222  * be moved there */
223 struct tracefiled_ctl {
224         struct completion       tctl_start;
225         struct completion       tctl_stop;
226         cfs_waitq_t             tctl_waitq;
227         pid_t                   tctl_pid;
228         cfs_atomic_t            tctl_shutdown;
229 };
230
231 /*
232  * small data-structure for each page owned by tracefiled.
233  */
234 /* XXX nikita: this declaration is internal to tracefile.c and should probably
235  * be moved there */
236 struct cfs_trace_page {
237         /*
238          * page itself
239          */
240         cfs_page_t          *page;
241         /*
242          * linkage into one of the lists in trace_data_union or
243          * page_collection
244          */
245         cfs_list_t           linkage;
246         /*
247          * number of bytes used within this page
248          */
249         unsigned int         used;
250         /*
251          * cpu that owns this page
252          */
253         unsigned short       cpu;
254         /*
255          * type(context) of this page
256          */
257         unsigned short       type;
258 };
259
260 extern void cfs_set_ptldebug_header(struct ptldebug_header *header,
261                                     struct libcfs_debug_msg_data *m,
262                                     unsigned long stack);
263 extern void cfs_print_to_console(struct ptldebug_header *hdr, int mask,
264                                  const char *buf, int len, const char *file,
265                                  const char *fn);
266
267 extern int cfs_trace_lock_tcd(struct cfs_trace_cpu_data *tcd, int walking);
268 extern void cfs_trace_unlock_tcd(struct cfs_trace_cpu_data *tcd, int walking);
269
270 /**
271  * trace_buf_type_t, trace_buf_idx_get() and trace_console_buffers[][]
272  * are not public libcfs API; they should be defined in
273  * platform-specific tracefile include files
274  * (see, for example, linux-tracefile.h).
275  */
276
277 extern char *cfs_trace_console_buffers[CFS_NR_CPUS][CFS_TCD_TYPE_MAX];
278 extern cfs_trace_buf_type_t cfs_trace_buf_idx_get(void);
279
280 static inline char *
281 cfs_trace_get_console_buffer(void)
282 {
283         unsigned int i = cfs_get_cpu();
284         unsigned int j = cfs_trace_buf_idx_get();
285
286         return cfs_trace_console_buffers[i][j];
287 }
288
289 static inline void
290 cfs_trace_put_console_buffer(char *buffer)
291 {
292         cfs_put_cpu();
293 }
294
295 static inline struct cfs_trace_cpu_data *
296 cfs_trace_get_tcd(void)
297 {
298         struct cfs_trace_cpu_data *tcd =
299                 &(*cfs_trace_data[cfs_trace_buf_idx_get()])[cfs_get_cpu()].tcd;
300
301         cfs_trace_lock_tcd(tcd, 0);
302
303         return tcd;
304 }
305
306 static inline void
307 cfs_trace_put_tcd (struct cfs_trace_cpu_data *tcd)
308 {
309         cfs_trace_unlock_tcd(tcd, 0);
310
311         cfs_put_cpu();
312 }
313
314 int cfs_trace_refill_stock(struct cfs_trace_cpu_data *tcd, int gfp,
315                            cfs_list_t *stock);
316
317
318 int cfs_tcd_owns_tage(struct cfs_trace_cpu_data *tcd,
319                       struct cfs_trace_page *tage);
320
321 extern void cfs_trace_assertion_failed(const char *str,
322                                        struct libcfs_debug_msg_data *m);
323
324 /* ASSERTION that is safe to use within the debug system */
325 #define __LASSERT(cond)                                                 \
326 do {                                                                    \
327         if (unlikely(!(cond))) {                                        \
328                 LIBCFS_DEBUG_MSG_DATA_DECL(msgdata, D_EMERG, NULL);     \
329                 cfs_trace_assertion_failed("ASSERTION("#cond") failed", \
330                                            &msgdata);                   \
331         }                                                               \
332 } while (0)
333
334 #define __LASSERT_TAGE_INVARIANT(tage)                                  \
335 do {                                                                    \
336         __LASSERT(tage != NULL);                                        \
337         __LASSERT(tage->page != NULL);                                  \
338         __LASSERT(tage->used <= CFS_PAGE_SIZE);                         \
339         __LASSERT(cfs_page_count(tage->page) > 0);                      \
340 } while (0)
341
342 #endif  /* LUSTRE_TRACEFILE_PRIVATE */
343
344 #endif /* __LIBCFS_TRACEFILE_H__ */