Whamcloud - gitweb
Landing b_hd_newconfig on HEAD
[fs/lustre-release.git] / lnet / libcfs / tracefile.h
1 #ifndef __LIBCFS_TRACEFILE_H__
2 #define __LIBCFS_TRACEFILE_H__
3
4 #include <libcfs/libcfs.h>
5
6 /* trace file lock routines */
7
8 int  tracefile_init_arch(void);
9 void tracefile_fini_arch(void);
10
11 void tracefile_read_lock(void);
12 void tracefile_read_unlock(void);
13 void tracefile_write_lock(void);
14 void tracefile_write_unlock(void);
15
16 int tracefile_dump_all_pages(char *filename);
17 void trace_debug_print(void);
18 void trace_flush_pages(void);
19 int trace_start_thread(void);
20 void trace_stop_thread(void);
21 int tracefile_init(void);
22 void tracefile_exit(void);
23 int trace_write_daemon_file(struct file *file, const char *buffer,
24                             unsigned long count, void *data);
25 int trace_read_daemon_file(char *page, char **start, off_t off, int count,
26                            int *eof, void *data);
27 int trace_write_debug_mb(struct file *file, const char *buffer,
28                          unsigned long count, void *data);
29 int trace_read_debug_mb(char *page, char **start, off_t off, int count,
30                         int *eof, void *data);
31 int trace_dk(struct file *file, const char *buffer, unsigned long count,
32              void *data);
33
34 extern void libcfs_debug_dumplog_internal(void *arg);
35 extern void libcfs_register_panic_notifier(void);
36 extern void libcfs_unregister_panic_notifier(void);
37 extern int  libcfs_panic_in_progress;
38
39 #ifdef LUSTRE_TRACEFILE_PRIVATE
40 /*
41  * Private declare for tracefile
42  */
43 #define TCD_MAX_PAGES (5 << (20 - CFS_PAGE_SHIFT))
44 #define TCD_STOCK_PAGES (TCD_MAX_PAGES)
45
46 #define TRACEFILE_SIZE (500 << 20)
47
48 /* Size of a buffer for sprinting console messages to in IRQ context (no
49  * logging in IRQ context) */
50 #define TRACE_CONSOLE_BUFFER_SIZE   1024
51
52 union trace_data_union {
53         struct trace_cpu_data {
54                 /*
55                  * pages with trace records not yet processed by tracefiled.
56                  */
57                 struct list_head        tcd_pages;
58                 /* number of pages on ->tcd_pages */
59                 unsigned long           tcd_cur_pages;
60
61                 /*
62                  * pages with trace records already processed by
63                  * tracefiled. These pages are kept in memory, so that some
64                  * portion of log can be written in the event of LBUG. This
65                  * list is maintained in LRU order.
66                  *
67                  * Pages are moved to ->tcd_daemon_pages by tracefiled()
68                  * (put_pages_on_daemon_list()). LRU pages from this list are
69                  * discarded when list grows too large.
70                  */
71                 struct list_head        tcd_daemon_pages;
72                 /* number of pages on ->tcd_daemon_pages */
73                 unsigned long           tcd_cur_daemon_pages;
74
75                 /*
76                  * Maximal number of pages allowed on ->tcd_pages and
77                  * ->tcd_daemon_pages each. Always TCD_MAX_PAGES in current
78                  * implementation.
79                  */
80                 unsigned long           tcd_max_pages;
81
82                 /*
83                  * preallocated pages to write trace records into. Pages from
84                  * ->tcd_stock_pages are moved to ->tcd_pages by
85                  * portals_debug_msg().
86                  *
87                  * This list is necessary, because on some platforms it's
88                  * impossible to perform efficient atomic page allocation in a
89                  * non-blockable context.
90                  *
91                  * Such platforms fill ->tcd_stock_pages "on occasion", when
92                  * tracing code is entered in blockable context.
93                  *
94                  * trace_get_tage_try() tries to get a page from
95                  * ->tcd_stock_pages first and resorts to atomic page
96                  * allocation only if this queue is empty. ->tcd_stock_pages
97                  * is replenished when tracing code is entered in blocking
98                  * context (darwin-tracefile.c:trace_get_tcd()). We try to
99                  * maintain TCD_STOCK_PAGES (40 by default) pages in this
100                  * queue. Atomic allocation is only required if more than
101                  * TCD_STOCK_PAGES pagesful are consumed by trace records all
102                  * emitted in non-blocking contexts. Which is quite unlikely.
103                  */
104                 struct list_head        tcd_stock_pages;
105                 /* number of pages on ->tcd_stock_pages */
106                 unsigned long           tcd_cur_stock_pages;
107
108                 int                     tcd_shutting_down;
109                 int                     tcd_cpu;
110         } tcd;
111         char __pad[SMP_CACHE_BYTES];
112 };
113
114 /* XXX nikita: this declaration is internal to tracefile.c and should probably
115  * be moved there */
116 struct page_collection {
117         struct list_head        pc_pages;
118         /*
119          * spin-lock protecting ->pc_pages. It is taken by smp_call_function()
120          * call-back functions. XXX nikita: Which is horrible: all processors
121          * receive NMI at the same time only to be serialized by this
122          * lock. Probably ->pc_pages should be replaced with an array of
123          * NR_CPUS elements accessed locklessly.
124          */
125         spinlock_t              pc_lock;
126         /*
127          * if this flag is set, collect_pages() will spill both
128          * ->tcd_daemon_pages and ->tcd_pages to the ->pc_pages. Otherwise,
129          * only ->tcd_pages are spilled.
130          */
131         int                     pc_want_daemon_pages;
132 };
133
134 /* XXX nikita: this declaration is internal to tracefile.c and should probably
135  * be moved there */
136 struct tracefiled_ctl {
137         struct completion       tctl_start;
138         struct completion       tctl_stop;
139         cfs_waitq_t             tctl_waitq;
140         pid_t                   tctl_pid;
141         atomic_t                tctl_shutdown;
142 };
143
144 /*
145  * small data-structure for each page owned by tracefiled.
146  */
147 /* XXX nikita: this declaration is internal to tracefile.c and should probably
148  * be moved there */
149 struct trace_page {
150         /*
151          * page itself
152          */
153         cfs_page_t      *page;
154         /*
155          * linkage into one of the lists in trace_data_union or
156          * page_collection
157          */
158         struct list_head linkage;
159         /*
160          * number of bytes used within this page
161          */
162         unsigned int     used;
163         /*
164          * cpu that owns this page
165          */
166         int              cpu;
167 };
168
169 extern void set_ptldebug_header(struct ptldebug_header *header,
170                            int subsys, int mask, const int line,
171                            unsigned long stack);
172 extern void print_to_console(struct ptldebug_header *hdr, int mask, const char *buf,
173                              int len, const char *file, const char *fn);
174
175 extern struct trace_cpu_data *trace_get_tcd(void);
176 extern void trace_put_tcd(struct trace_cpu_data *tcd);
177 extern char *trace_get_console_buffer(void);
178 extern void trace_put_console_buffer(char *buffer);
179
180 extern void trace_call_on_all_cpus(void (*fn)(void *arg), void *arg);
181
182 int trace_refill_stock(struct trace_cpu_data *tcd, int gfp,
183                        struct list_head *stock);
184
185
186 int tcd_owns_tage(struct trace_cpu_data *tcd, struct trace_page *tage);
187
188 extern void trace_assertion_failed(const char *str, const char *fn,
189                                    const char *file, int line);
190
191 /* ASSERTION that is safe to use within the debug system */
192 #define __LASSERT(cond)                                                         \
193 ({                                                                              \
194         if (unlikely(!(cond))) {                                                \
195                 trace_assertion_failed("ASSERTION("#cond") failed",             \
196                                        __FUNCTION__, __FILE__, __LINE__);       \
197         }                                                                       \
198 })
199
200 #define __LASSERT_TAGE_INVARIANT(tage)                  \
201 ({                                                      \
202         __LASSERT(tage != NULL);                        \
203         __LASSERT(tage->page != NULL);                  \
204         __LASSERT(tage->used <= CFS_PAGE_SIZE);         \
205         __LASSERT(cfs_page_count(tage->page) > 0);      \
206 })
207
208 #endif  /* LUSTRE_TRACEFILE_PRIVATE */
209
210 #endif /* __LIBCFS_TRACEFILE_H__ */