Whamcloud - gitweb
This patch is to slove OSS hangs after "All ost request buffers busy"
[fs/lustre-release.git] / lustre / include / lprocfs_status.h
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  *
21  *   Top level header file for LProc SNMP
22  *   Author: Hariharan Thantry thantry@users.sourceforge.net
23  */
24 #ifndef _LPROCFS_SNMP_H
25 #define _LPROCFS_SNMP_H
26
27 #if defined(__linux__)
28 #include <linux/lprocfs_status.h>
29 #elif defined(__APPLE__)
30 #include <darwin/lprocfs_status.h>
31 #elif defined(__WINNT__)
32 #include <winnt/lprocfs_status.h>
33 #else
34 #error Unsupported operating system.
35 #endif
36
37 #undef LPROCFS
38 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
39 # define LPROCFS
40 #endif
41
42 struct lprocfs_vars {
43         const char   *name;
44         cfs_read_proc_t *read_fptr;
45         cfs_write_proc_t *write_fptr;
46         void *data;
47 };
48
49 struct lprocfs_static_vars {
50         struct lprocfs_vars *module_vars;
51         struct lprocfs_vars *obd_vars;
52 };
53
54 /* if we find more consumers this could be generalized */
55 #define OBD_HIST_MAX 32
56 struct obd_histogram {
57         spinlock_t      oh_lock;
58         unsigned long   oh_buckets[OBD_HIST_MAX];
59 };
60
61 enum {
62         BRW_R_PAGES = 0,
63         BRW_W_PAGES,
64         BRW_R_RPC_HIST,
65         BRW_W_RPC_HIST,
66         BRW_R_IO_TIME,
67         BRW_W_IO_TIME,
68         BRW_R_DISCONT_PAGES,
69         BRW_W_DISCONT_PAGES,
70         BRW_R_DISCONT_BLOCKS,
71         BRW_W_DISCONT_BLOCKS,
72         BRW_R_DISK_IOSIZE,
73         BRW_W_DISK_IOSIZE,
74         BRW_R_DIO_FRAGS,
75         BRW_W_DIO_FRAGS,
76         BRW_LAST,
77 };
78
79 struct brw_stats {
80         struct obd_histogram hist[BRW_LAST];
81 };
82
83
84 /* An lprocfs counter can be configured using the enum bit masks below.
85  *
86  * LPROCFS_CNTR_EXTERNALLOCK indicates that an external lock already
87  * protects this counter from concurrent updates. If not specified,
88  * lprocfs an internal per-counter lock variable. External locks are
89  * not used to protect counter increments, but are used to protect
90  * counter readout and resets.
91  *
92  * LPROCFS_CNTR_AVGMINMAX indicates a multi-valued counter samples,
93  * (i.e. counter can be incremented by more than "1"). When specified,
94  * the counter maintains min, max and sum in addition to a simple
95  * invocation count. This allows averages to be be computed.
96  * If not specified, the counter is an increment-by-1 counter.
97  * min, max, sum, etc. are not maintained.
98  *
99  * LPROCFS_CNTR_STDDEV indicates that the counter should track sum of
100  * squares (for multi-valued counter samples only). This allows
101  * external computation of standard deviation, but involves a 64-bit
102  * multiply per counter increment.
103  */
104
105 enum {
106         LPROCFS_CNTR_EXTERNALLOCK = 0x0001,
107         LPROCFS_CNTR_AVGMINMAX    = 0x0002,
108         LPROCFS_CNTR_STDDEV       = 0x0004,
109
110         /* counter data type */
111         LPROCFS_TYPE_REGS         = 0x0100,
112         LPROCFS_TYPE_BYTES        = 0x0200,
113         LPROCFS_TYPE_PAGES        = 0x0400,
114         LPROCFS_TYPE_CYCLE        = 0x0800,
115 };
116
117 struct lprocfs_atomic {
118         atomic_t               la_entry;
119         atomic_t               la_exit;
120 };
121
122 struct lprocfs_counter {
123         struct lprocfs_atomic  lc_cntl;  /* may need to move to per set */
124         unsigned int           lc_config;
125         __u64                  lc_count;
126         __u64                  lc_sum;
127         __u64                  lc_min;
128         __u64                  lc_max;
129         __u64                  lc_sumsquare;
130         const char            *lc_name;   /* must be static */
131         const char            *lc_units;  /* must be static */
132 };
133
134 struct lprocfs_percpu {
135         struct lprocfs_counter lp_cntr[0];
136 };
137
138
139 struct lprocfs_stats {
140         unsigned int           ls_num;     /* # of counters */
141         unsigned int           ls_percpu_size;
142         struct lprocfs_percpu *ls_percpu[0];
143 };
144
145
146 /* class_obd.c */
147 extern cfs_proc_dir_entry_t *proc_lustre_root;
148
149 struct obd_device;
150 struct file;
151 struct obd_histogram;
152
153 #ifdef LPROCFS
154
155 /* Two optimized LPROCFS counter increment functions are provided:
156  *     lprocfs_counter_incr(cntr, value) - optimized for by-one counters
157  *     lprocfs_counter_add(cntr) - use for multi-valued counters
158  * Counter data layout allows config flag, counter lock and the
159  * count itself to reside within a single cache line.
160  */
161
162 static inline void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
163                                        long amount)
164 {
165         struct lprocfs_counter *percpu_cntr;
166
167         if (!stats)
168                 return;
169         percpu_cntr = &(stats->ls_percpu[smp_processor_id()]->lp_cntr[idx]);
170         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
171         percpu_cntr->lc_count++;
172
173         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
174                 percpu_cntr->lc_sum += amount;
175                 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
176                         percpu_cntr->lc_sumsquare += (__u64)amount * amount;
177                 if (amount < percpu_cntr->lc_min)
178                         percpu_cntr->lc_min = amount;
179                 if (amount > percpu_cntr->lc_max)
180                         percpu_cntr->lc_max = amount;
181         }
182         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
183 }
184
185 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats, int idx)
186 {
187         struct lprocfs_counter *percpu_cntr;
188
189         if (!stats)
190                 return;
191         percpu_cntr = &(stats->ls_percpu[smp_processor_id()]->lp_cntr[idx]);
192         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
193         percpu_cntr->lc_count++;
194         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
195 }
196
197 extern struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num);
198 extern void lprocfs_clear_stats(struct lprocfs_stats *stats);
199 extern void lprocfs_free_stats(struct lprocfs_stats **stats);
200 extern void lprocfs_init_ops_stats(int num_private_stats, 
201                                    struct lprocfs_stats *stats);
202 extern int lprocfs_alloc_obd_stats(struct obd_device *obddev,
203                                    unsigned int num_private_stats);
204 extern void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
205                                  unsigned conf, const char *name,
206                                  const char *units);
207 extern void lprocfs_free_obd_stats(struct obd_device *obddev);
208 struct obd_export;
209 extern int lprocfs_exp_setup(struct obd_export *exp);
210 extern int lprocfs_exp_cleanup(struct obd_export *exp);
211 extern int lprocfs_register_stats(cfs_proc_dir_entry_t *root, const char *name,
212                                   struct lprocfs_stats *stats);
213
214 #define LPROCFS_INIT_VARS(name, vclass, vinstance)           \
215 void lprocfs_##name##_init_vars(struct lprocfs_static_vars *x)  \
216 {                                                      \
217         x->module_vars = vclass;                       \
218         x->obd_vars = vinstance;                       \
219 }                                                      \
220
221 #define lprocfs_init_vars(NAME, VAR)     \
222 do {      \
223         extern void lprocfs_##NAME##_init_vars(struct lprocfs_static_vars *);  \
224         lprocfs_##NAME##_init_vars(VAR);                                       \
225 } while (0)
226
227 /* lprocfs_status.c */
228 extern int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
229                             struct lprocfs_vars *var,
230                             void *data);
231
232 extern cfs_proc_dir_entry_t *lprocfs_register(const char *name,
233                                                cfs_proc_dir_entry_t *parent,
234                                                struct lprocfs_vars *list,
235                                                void *data);
236
237 extern void lprocfs_remove(cfs_proc_dir_entry_t **root);
238
239 extern cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *root,
240                                            const char *name);
241
242 extern int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list);
243 extern int lprocfs_obd_cleanup(struct obd_device *obd);
244
245 extern int lprocfs_seq_create(cfs_proc_dir_entry_t *parent, char *name, 
246                               mode_t mode, struct file_operations *seq_fops,
247                               void *data);
248 extern int lprocfs_obd_seq_create(struct obd_device *dev, char *name,
249                                   mode_t mode, struct file_operations *seq_fops,
250                                   void *data);
251
252 /* Generic callbacks */
253
254 extern int lprocfs_rd_u64(char *page, char **start, off_t off,
255                           int count, int *eof, void *data);
256 extern int lprocfs_rd_atomic(char *page, char **start, off_t off,
257                           int count, int *eof, void *data);
258 extern int lprocfs_rd_uuid(char *page, char **start, off_t off,
259                            int count, int *eof, void *data);
260 extern int lprocfs_rd_name(char *page, char **start, off_t off,
261                            int count, int *eof, void *data);
262 extern int lprocfs_rd_fstype(char *page, char **start, off_t off,
263                              int count, int *eof, void *data);
264 extern int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
265                                   int count, int *eof, void *data);
266 extern int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
267                                 int count, int *eof, void *data);
268 extern int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
269                                     int count, int *eof, void *data);
270 extern int lprocfs_rd_num_exports(char *page, char **start, off_t off,
271                                   int count, int *eof, void *data);
272 extern int lprocfs_rd_numrefs(char *page, char **start, off_t off,
273                               int count, int *eof, void *data);
274 extern int lprocfs_wr_evict_client(struct file *file, const char *buffer,
275                                    unsigned long count, void *data);
276 extern int lprocfs_wr_ping(struct file *file, const char *buffer,
277                            unsigned long count, void *data);
278
279 /* Statfs helpers */
280 extern int lprocfs_rd_blksize(char *page, char **start, off_t off,
281                               int count, int *eof, void *data);
282 extern int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
283                                   int count, int *eof, void *data);
284 extern int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
285                                  int count, int *eof, void *data);
286 extern int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
287                                  int count, int *eof, void *data);
288 extern int lprocfs_rd_filestotal(char *page, char **start, off_t off,
289                                  int count, int *eof, void *data);
290 extern int lprocfs_rd_filesfree(char *page, char **start, off_t off,
291                                 int count, int *eof, void *data);
292 extern int lprocfs_rd_filegroups(char *page, char **start, off_t off,
293                                  int count, int *eof, void *data);
294
295 extern int lprocfs_write_helper(const char *buffer, unsigned long count,
296                                 int *val);
297 extern int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
298                                      int *val, int mult);
299 extern int lprocfs_read_frac_helper(char *buffer, unsigned long count, 
300                                     long val, int mult);
301 extern int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
302                                     __u64 *val);
303 extern int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
304                                          __u64 *val, int mult);
305 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value);
306 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value);
307 void lprocfs_oh_clear(struct obd_histogram *oh);
308 unsigned long lprocfs_oh_sum(struct obd_histogram *oh);
309
310 /* lprocfs_status.c: counter read/write functions */
311 extern int lprocfs_counter_read(char *page, char **start, off_t off,
312                                 int count, int *eof, void *data);
313 extern int lprocfs_counter_write(struct file *file, const char *buffer,
314                                  unsigned long count, void *data);
315
316 /* lprocfs_status.c: recovery status */
317 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
318                                    int count, int *eof, void *data);
319
320 extern int lprocfs_seq_release(struct inode *, struct file *);
321
322 /* in lprocfs_stat.c, to protect the private data for proc entries */
323 extern struct rw_semaphore _lprocfs_lock;
324 #define LPROCFS_ENTRY()           do {  \
325         down_read(&_lprocfs_lock);      \
326 } while(0)
327 #define LPROCFS_EXIT()            do {  \
328         up_read(&_lprocfs_lock);        \
329 } while(0)
330 #define LPROCFS_ENTRY_AND_CHECK(dp) do {        \
331         typecheck(struct proc_dir_entry *, dp); \
332         LPROCFS_ENTRY();                        \
333         if ((dp)->deleted) {                    \
334                 LPROCFS_EXIT();                 \
335                 return -ENODEV;                 \
336         }                                       \
337 } while(0)
338
339 /* You must use these macros when you want to refer to 
340  * the import in a client obd_device for a lprocfs entry */
341 #define LPROCFS_CLIMP_CHECK(obd) do {           \
342         typecheck(struct obd_device *, obd);    \
343         mutex_down(&(obd)->u.cli.cl_sem);       \
344         if ((obd)->u.cli.cl_import == NULL) {   \
345              mutex_up(&(obd)->u.cli.cl_sem);    \
346              return -ENODEV;                    \
347         }                                       \
348 } while(0)
349 #define LPROCFS_CLIMP_EXIT(obd)                 \
350         mutex_up(&(obd)->u.cli.cl_sem);
351
352
353 /* write the name##_seq_show function, call LPROC_SEQ_FOPS_RO for read-only 
354   proc entries; otherwise, you will define name##_seq_write function also for 
355   a read-write proc entry, and then call LPROC_SEQ_SEQ instead. Finally,
356   call lprocfs_obd_seq_create(obd, filename, 0444, &name#_fops, data); */
357 #define __LPROC_SEQ_FOPS(name, custom_seq_write)                           \
358 static int name##_seq_open(struct inode *inode, struct file *file) {       \
359         struct proc_dir_entry *dp = PDE(inode);                            \
360         int rc;                                                            \
361         LPROCFS_ENTRY_AND_CHECK(dp);                                       \
362         rc = single_open(file, name##_seq_show, dp->data);                 \
363         if (rc) {                                                          \
364                 LPROCFS_EXIT();                                            \
365                 return rc;                                                 \
366         }                                                                  \
367         return 0;                                                          \
368 }                                                                          \
369 struct file_operations name##_fops = {                                     \
370         .owner   = THIS_MODULE,                                            \
371         .open    = name##_seq_open,                                        \
372         .read    = seq_read,                                               \
373         .write   = custom_seq_write,                                       \
374         .llseek  = seq_lseek,                                              \
375         .release = lprocfs_seq_release,                                    \
376 }
377
378 #define LPROC_SEQ_FOPS_RO(name)         __LPROC_SEQ_FOPS(name, NULL)
379 #define LPROC_SEQ_FOPS(name)            __LPROC_SEQ_FOPS(name, name##_seq_write)
380
381 #else
382 /* LPROCFS is not defined */
383 static inline void lprocfs_counter_add(struct lprocfs_stats *stats,
384                                        int index, long amount) { return; }
385 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats,
386                                         int index) { return; }
387 static inline void lprocfs_counter_init(struct lprocfs_stats *stats,
388                                         int index, unsigned conf,
389                                         const char *name, const char *units)
390 { return; }
391
392 static inline struct lprocfs_stats* lprocfs_alloc_stats(unsigned int num)
393 { return NULL; }
394 static inline void lprocfs_clear_stats(struct lprocfs_stats *stats)
395 { return; }
396 static inline void lprocfs_free_stats(struct lprocfs_stats **stats)
397 { return; }
398 static inline int lprocfs_register_stats(cfs_proc_dir_entry_t *root,
399                                             const char *name,
400                                             struct lprocfs_stats *stats)
401 { return 0; }
402 static inline void lprocfs_init_ops_stats(int num_private_stats, 
403                                           struct lprocfs_stats *stats)
404 { return; }
405 static inline int lprocfs_alloc_obd_stats(struct obd_device *obddev,
406                                           unsigned int num_private_stats)
407 { return 0; }
408 static inline void lprocfs_free_obd_stats(struct obd_device *obddev)
409 { return; }
410
411 struct obd_export;
412 static inline int lprocfs_exp_setup(struct obd_export *exp)
413 { return 0; }
414 static inline int lprocfs_exp_cleanup(struct obd_export *exp)
415 { return 0; }
416
417 static inline cfs_proc_dir_entry_t *
418 lprocfs_register(const char *name, cfs_proc_dir_entry_t *parent,
419                  struct lprocfs_vars *list, void *data) { return NULL; }
420 #define LPROCFS_INIT_VARS(name, vclass, vinstance)
421 #define lprocfs_init_vars(...) do {} while (0)
422 static inline int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
423                                    struct lprocfs_vars *var,
424                                    void *data) { return 0; }
425 static inline void lprocfs_remove(cfs_proc_dir_entry_t **root) {};
426 static inline cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *head,
427                                     const char *name) {return 0;}
428 static inline int lprocfs_obd_setup(struct obd_device *dev,
429                                     struct lprocfs_vars *list) { return 0; }
430 static inline int lprocfs_obd_cleanup(struct obd_device *dev)  { return 0; }
431 static inline int lprocfs_rd_u64(char *page, char **start, off_t off,
432                                  int count, int *eof, void *data) { return 0; }
433 static inline int lprocfs_rd_uuid(char *page, char **start, off_t off,
434                                   int count, int *eof, void *data) { return 0; }
435 static inline int lprocfs_rd_name(char *page, char **start, off_t off,
436                                   int count, int *eof, void *data) { return 0; }
437 static inline int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
438                                          int count, int *eof, void *data)
439 { return 0; }
440 static inline int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
441                                        int count, int *eof, void *data)
442 { return 0; }
443 static inline int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
444                                            int count, int *eof, void *data)
445 { return 0; }
446 static inline int lprocfs_rd_num_exports(char *page, char **start, off_t off,
447                                          int count, int *eof, void *data)
448 { return 0; }
449 static inline int lprocfs_rd_numrefs(char *page, char **start, off_t off,
450                                      int count, int *eof, void *data)
451 { return 0; }
452 static inline int lprocfs_wr_evict_client(struct file *file, const char *buffer,
453                                           unsigned long count, void *data)
454 { return 0; }
455 static inline int lprocfs_wr_ping(struct file *file, const char *buffer,
456                                   unsigned long count, void *data)
457 { return 0; }
458
459
460 /* Statfs helpers */
461 static inline
462 int lprocfs_rd_blksize(char *page, char **start, off_t off,
463                        int count, int *eof, void *data) { return 0; }
464 static inline
465 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
466                            int count, int *eof, void *data) { return 0; }
467 static inline
468 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
469                           int count, int *eof, void *data) { return 0; }
470 static inline
471 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
472                            int count, int *eof, void *data) { return 0; }
473 static inline
474 int lprocfs_rd_filestotal(char *page, char **start, off_t off,
475                           int count, int *eof, void *data) { return 0; }
476 static inline
477 int lprocfs_rd_filesfree(char *page, char **start, off_t off,
478                          int count, int *eof, void *data)  { return 0; }
479 static inline
480 int lprocfs_rd_filegroups(char *page, char **start, off_t off,
481                           int count, int *eof, void *data) { return 0; }
482 static inline
483 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value) {}
484 static inline
485 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value) {}
486 static inline
487 void lprocfs_oh_clear(struct obd_histogram *oh) {}
488 static inline
489 unsigned long lprocfs_oh_sum(struct obd_histogram *oh) { return 0; }
490 static inline
491 int lprocfs_counter_read(char *page, char **start, off_t off,
492                          int count, int *eof, void *data) { return 0; }
493 static inline
494 int lprocfs_counter_write(struct file *file, const char *buffer,
495                           unsigned long count, void *data) { return 0; }
496
497 #define LPROCFS_ENTRY()
498 #define LPROCFS_EXIT()
499 #define LPROCFS_ENTRY_AND_CHECK(dp)
500 #define LPROC_SEQ_FOPS_RO(name)
501 #define LPROC_SEQ_FOPS(name)
502
503 #endif /* LPROCFS */
504
505 #endif /* LPROCFS_SNMP_H */