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