1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2002 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
21 * Top level header file for LProc SNMP
22 * Author: Hariharan Thantry thantry@users.sourceforge.net
24 #ifndef _LPROCFS_SNMP_H
25 #define _LPROCFS_SNMP_H
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>
34 #error Unsupported operating system.
38 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
44 cfs_read_proc_t *read_fptr;
45 cfs_write_proc_t *write_fptr;
47 struct file_operations *fops;
50 struct lprocfs_static_vars {
51 struct lprocfs_vars *module_vars;
52 struct lprocfs_vars *obd_vars;
55 /* if we find more consumers this could be generalized */
56 #define OBD_HIST_MAX 32
57 struct obd_histogram {
59 unsigned long oh_buckets[OBD_HIST_MAX];
81 struct obd_histogram hist[BRW_LAST];
85 /* An lprocfs counter can be configured using the enum bit masks below.
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.
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.
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.
107 LPROCFS_CNTR_EXTERNALLOCK = 0x0001,
108 LPROCFS_CNTR_AVGMINMAX = 0x0002,
109 LPROCFS_CNTR_STDDEV = 0x0004,
111 /* counter data type */
112 LPROCFS_TYPE_REGS = 0x0100,
113 LPROCFS_TYPE_BYTES = 0x0200,
114 LPROCFS_TYPE_PAGES = 0x0400,
115 LPROCFS_TYPE_CYCLE = 0x0800,
118 struct lprocfs_atomic {
123 struct lprocfs_counter {
124 struct lprocfs_atomic lc_cntl; /* may need to move to per set */
125 unsigned int lc_config;
131 const char *lc_name; /* must be static */
132 const char *lc_units; /* must be static */
135 struct lprocfs_percpu {
136 struct lprocfs_counter lp_cntr[0];
139 #define LPROCFS_GET_NUM_CPU 0x0001
140 #define LPROCFS_GET_SMP_ID 0x0002
142 enum lprocfs_stats_flags {
143 LPROCFS_STATS_FLAG_PERCPU = 0x0000, /* per cpu counter */
144 LPROCFS_STATS_FLAG_NOPERCPU = 0x0001, /* stats have no percpu
145 * area and need locking */
148 enum lprocfs_fields_flags {
149 LPROCFS_FIELDS_FLAGS_CONFIG = 0x0001,
150 LPROCFS_FIELDS_FLAGS_SUM = 0x0002,
151 LPROCFS_FIELDS_FLAGS_MIN = 0x0003,
152 LPROCFS_FIELDS_FLAGS_MAX = 0x0004,
153 LPROCFS_FIELDS_FLAGS_AVG = 0x0005,
154 LPROCFS_FIELDS_FLAGS_SUMSQUARE = 0x0006,
155 LPROCFS_FIELDS_FLAGS_COUNT = 0x0007,
158 struct lprocfs_stats {
159 unsigned int ls_num; /* # of counters */
160 unsigned int ls_percpu_size;
161 int ls_flags; /* See LPROCFS_STATS_FLAG_* */
162 spinlock_t ls_lock; /* Lock used only when there are
163 * no percpu stats areas */
164 struct lprocfs_percpu *ls_percpu[0];
169 extern cfs_proc_dir_entry_t *proc_lustre_root;
173 struct obd_histogram;
177 static inline int lprocfs_stats_lock(struct lprocfs_stats *stats, int type)
181 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
182 if (type & LPROCFS_GET_NUM_CPU)
184 if (type & LPROCFS_GET_SMP_ID)
186 spin_lock(&stats->ls_lock);
188 if (type & LPROCFS_GET_NUM_CPU)
189 rc = num_possible_cpus();
190 if (type & LPROCFS_GET_SMP_ID)
191 rc = smp_processor_id();
196 static inline void lprocfs_stats_unlock(struct lprocfs_stats *stats)
198 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
199 spin_unlock(&stats->ls_lock);
202 /* Two optimized LPROCFS counter increment functions are provided:
203 * lprocfs_counter_incr(cntr, value) - optimized for by-one counters
204 * lprocfs_counter_add(cntr) - use for multi-valued counters
205 * Counter data layout allows config flag, counter lock and the
206 * count itself to reside within a single cache line.
209 static inline void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
212 struct lprocfs_counter *percpu_cntr;
218 /* With per-client stats, statistics are allocated only for
219 * single CPU area, so the smp_id should be 0 always. */
220 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
222 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
223 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
224 percpu_cntr->lc_count++;
226 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
227 percpu_cntr->lc_sum += amount;
228 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
229 percpu_cntr->lc_sumsquare += (__u64)amount * amount;
230 if (amount < percpu_cntr->lc_min)
231 percpu_cntr->lc_min = amount;
232 if (amount > percpu_cntr->lc_max)
233 percpu_cntr->lc_max = amount;
235 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
236 lprocfs_stats_unlock(stats);
239 #define lprocfs_counter_incr(stats, idx) \
240 lprocfs_counter_add(stats, idx, 1)
242 static inline void lprocfs_counter_sub(struct lprocfs_stats *stats, int idx,
245 struct lprocfs_counter *percpu_cntr;
251 /* With per-client stats, statistics are allocated only for
252 * single CPU area, so the smp_id should be 0 always. */
253 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
255 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
256 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
257 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX)
258 percpu_cntr->lc_sum -= amount;
259 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
260 lprocfs_stats_unlock(stats);
262 #define lprocfs_counter_decr(stats, idx) \
263 lprocfs_counter_sub(stats, idx, 1)
265 extern __s64 lprocfs_read_helper(struct lprocfs_counter *lc,
266 enum lprocfs_fields_flags field);
267 static inline __u64 lprocfs_stats_collector(struct lprocfs_stats *stats,
269 enum lprocfs_fields_flags field)
274 LASSERT(stats != NULL);
275 for (i = 0; i < num_possible_cpus(); i++)
276 ret += lprocfs_read_helper(&(stats->ls_percpu[i]->lp_cntr[idx]),
281 extern struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
282 enum lprocfs_stats_flags flags);
283 extern void lprocfs_clear_stats(struct lprocfs_stats *stats);
284 extern void lprocfs_free_stats(struct lprocfs_stats **stats);
285 extern void lprocfs_init_ops_stats(int num_private_stats,
286 struct lprocfs_stats *stats);
287 extern int lprocfs_alloc_obd_stats(struct obd_device *obddev,
288 unsigned int num_private_stats);
289 extern void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
290 unsigned conf, const char *name,
292 extern void lprocfs_free_obd_stats(struct obd_device *obddev);
294 extern int lprocfs_exp_setup(struct obd_export *exp);
295 extern int lprocfs_exp_cleanup(struct obd_export *exp);
296 extern int lprocfs_register_stats(cfs_proc_dir_entry_t *root, const char *name,
297 struct lprocfs_stats *stats);
299 #define LPROCFS_INIT_VARS(name, vclass, vinstance) \
300 void lprocfs_##name##_init_vars(struct lprocfs_static_vars *x) \
302 x->module_vars = vclass; \
303 x->obd_vars = vinstance; \
306 #define lprocfs_init_vars(NAME, VAR) \
308 extern void lprocfs_##NAME##_init_vars(struct lprocfs_static_vars *); \
309 lprocfs_##NAME##_init_vars(VAR); \
312 /* lprocfs_status.c */
313 extern int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
314 struct lprocfs_vars *var,
317 extern cfs_proc_dir_entry_t *lprocfs_register(const char *name,
318 cfs_proc_dir_entry_t *parent,
319 struct lprocfs_vars *list,
322 extern void lprocfs_remove(cfs_proc_dir_entry_t **root);
324 extern cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *root,
327 extern int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list);
328 extern int lprocfs_obd_cleanup(struct obd_device *obd);
329 extern struct file_operations lprocfs_evict_client_fops;
331 extern int lprocfs_seq_create(cfs_proc_dir_entry_t *parent, char *name,
332 mode_t mode, struct file_operations *seq_fops,
334 extern int lprocfs_obd_seq_create(struct obd_device *dev, char *name,
335 mode_t mode, struct file_operations *seq_fops,
338 /* Generic callbacks */
340 extern int lprocfs_rd_u64(char *page, char **start, off_t off,
341 int count, int *eof, void *data);
342 extern int lprocfs_rd_atomic(char *page, char **start, off_t off,
343 int count, int *eof, void *data);
344 extern int lprocfs_wr_atomic(struct file *file, const char *buffer,
345 unsigned long count, void *data);
346 extern int lprocfs_rd_uint(char *page, char **start, off_t off,
347 int count, int *eof, void *data);
348 extern int lprocfs_wr_uint(struct file *file, const char *buffer,
349 unsigned long count, void *data);
350 extern int lprocfs_rd_uuid(char *page, char **start, off_t off,
351 int count, int *eof, void *data);
352 extern int lprocfs_rd_name(char *page, char **start, off_t off,
353 int count, int *eof, void *data);
354 extern int lprocfs_rd_fstype(char *page, char **start, off_t off,
355 int count, int *eof, void *data);
356 extern int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
357 int count, int *eof, void *data);
358 extern int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
359 int count, int *eof, void *data);
360 extern int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
361 int count, int *eof, void *data);
362 extern int lprocfs_rd_num_exports(char *page, char **start, off_t off,
363 int count, int *eof, void *data);
364 extern int lprocfs_rd_numrefs(char *page, char **start, off_t off,
365 int count, int *eof, void *data);
366 extern int lprocfs_wr_evict_client(struct file *file, const char *buffer,
367 unsigned long count, void *data);
368 extern int lprocfs_wr_ping(struct file *file, const char *buffer,
369 unsigned long count, void *data);
372 extern int lprocfs_rd_blksize(char *page, char **start, off_t off,
373 int count, int *eof, void *data);
374 extern int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
375 int count, int *eof, void *data);
376 extern int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
377 int count, int *eof, void *data);
378 extern int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
379 int count, int *eof, void *data);
380 extern int lprocfs_rd_filestotal(char *page, char **start, off_t off,
381 int count, int *eof, void *data);
382 extern int lprocfs_rd_filesfree(char *page, char **start, off_t off,
383 int count, int *eof, void *data);
384 extern int lprocfs_rd_filegroups(char *page, char **start, off_t off,
385 int count, int *eof, void *data);
387 extern int lprocfs_write_helper(const char *buffer, unsigned long count,
389 extern int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
391 extern int lprocfs_read_frac_helper(char *buffer, unsigned long count,
393 extern int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
395 extern int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
396 __u64 *val, int mult);
397 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value);
398 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value);
399 void lprocfs_oh_clear(struct obd_histogram *oh);
400 unsigned long lprocfs_oh_sum(struct obd_histogram *oh);
402 /* lprocfs_status.c: counter read/write functions */
403 extern int lprocfs_counter_read(char *page, char **start, off_t off,
404 int count, int *eof, void *data);
405 extern int lprocfs_counter_write(struct file *file, const char *buffer,
406 unsigned long count, void *data);
408 /* lprocfs_status.c: recovery status */
409 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
410 int count, int *eof, void *data);
412 extern int lprocfs_seq_release(struct inode *, struct file *);
414 /* in lprocfs_stat.c, to protect the private data for proc entries */
415 extern struct rw_semaphore _lprocfs_lock;
416 #define LPROCFS_ENTRY() do { \
417 down_read(&_lprocfs_lock); \
419 #define LPROCFS_EXIT() do { \
420 up_read(&_lprocfs_lock); \
422 #define LPROCFS_ENTRY_AND_CHECK(dp) do { \
423 typecheck(struct proc_dir_entry *, dp); \
425 if ((dp)->deleted) { \
431 /* You must use these macros when you want to refer to
432 * the import in a client obd_device for a lprocfs entry */
433 #define LPROCFS_CLIMP_CHECK(obd) do { \
434 typecheck(struct obd_device *, obd); \
435 mutex_down(&(obd)->u.cli.cl_sem); \
436 if ((obd)->u.cli.cl_import == NULL) { \
437 mutex_up(&(obd)->u.cli.cl_sem); \
441 #define LPROCFS_CLIMP_EXIT(obd) \
442 mutex_up(&(obd)->u.cli.cl_sem);
445 /* write the name##_seq_show function, call LPROC_SEQ_FOPS_RO for read-only
446 proc entries; otherwise, you will define name##_seq_write function also for
447 a read-write proc entry, and then call LPROC_SEQ_SEQ instead. Finally,
448 call lprocfs_obd_seq_create(obd, filename, 0444, &name#_fops, data); */
449 #define __LPROC_SEQ_FOPS(name, custom_seq_write) \
450 static int name##_seq_open(struct inode *inode, struct file *file) { \
451 struct proc_dir_entry *dp = PDE(inode); \
453 LPROCFS_ENTRY_AND_CHECK(dp); \
454 rc = single_open(file, name##_seq_show, dp->data); \
461 struct file_operations name##_fops = { \
462 .owner = THIS_MODULE, \
463 .open = name##_seq_open, \
465 .write = custom_seq_write, \
466 .llseek = seq_lseek, \
467 .release = lprocfs_seq_release, \
470 #define LPROC_SEQ_FOPS_RO(name) __LPROC_SEQ_FOPS(name, NULL)
471 #define LPROC_SEQ_FOPS(name) __LPROC_SEQ_FOPS(name, name##_seq_write)
474 /* LPROCFS is not defined */
475 static inline void lprocfs_counter_add(struct lprocfs_stats *stats,
476 int index, long amount) { return; }
477 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats,
478 int index) { return; }
479 static inline void lprocfs_counter_sub(struct lprocfs_stats *stats,
480 int index, long amount) { return; }
481 static inline void lprocfs_counter_init(struct lprocfs_stats *stats,
482 int index, unsigned conf,
483 const char *name, const char *units)
486 static inline __u64 lc_read_helper(struct lprocfs_counter *lc,
487 enum lprocfs_fields_flags field)
490 static inline struct lprocfs_stats* lprocfs_alloc_stats(unsigned int num,
491 enum lprocfs_stats_flags flags)
493 static inline void lprocfs_clear_stats(struct lprocfs_stats *stats)
495 static inline void lprocfs_free_stats(struct lprocfs_stats **stats)
497 static inline int lprocfs_register_stats(cfs_proc_dir_entry_t *root,
499 struct lprocfs_stats *stats)
501 static inline void lprocfs_init_ops_stats(int num_private_stats,
502 struct lprocfs_stats *stats)
504 static inline int lprocfs_alloc_obd_stats(struct obd_device *obddev,
505 unsigned int num_private_stats)
507 static inline void lprocfs_free_obd_stats(struct obd_device *obddev)
511 static inline int lprocfs_exp_setup(struct obd_export *exp)
513 static inline int lprocfs_exp_cleanup(struct obd_export *exp)
516 static inline cfs_proc_dir_entry_t *
517 lprocfs_register(const char *name, cfs_proc_dir_entry_t *parent,
518 struct lprocfs_vars *list, void *data) { return NULL; }
519 #define LPROCFS_INIT_VARS(name, vclass, vinstance)
520 #define lprocfs_init_vars(...) do {} while (0)
521 static inline int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
522 struct lprocfs_vars *var,
523 void *data) { return 0; }
524 static inline void lprocfs_remove(cfs_proc_dir_entry_t **root) {};
525 static inline cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *head,
526 const char *name) {return 0;}
527 static inline int lprocfs_obd_setup(struct obd_device *dev,
528 struct lprocfs_vars *list) { return 0; }
529 static inline int lprocfs_obd_cleanup(struct obd_device *dev) { return 0; }
530 static inline int lprocfs_rd_u64(char *page, char **start, off_t off,
531 int count, int *eof, void *data) { return 0; }
532 static inline int lprocfs_rd_uuid(char *page, char **start, off_t off,
533 int count, int *eof, void *data) { return 0; }
534 static inline int lprocfs_rd_name(char *page, char **start, off_t off,
535 int count, int *eof, void *data) { return 0; }
536 static inline int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
537 int count, int *eof, void *data)
539 static inline int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
540 int count, int *eof, void *data)
542 static inline int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
543 int count, int *eof, void *data)
545 static inline int lprocfs_rd_num_exports(char *page, char **start, off_t off,
546 int count, int *eof, void *data)
548 static inline int lprocfs_rd_numrefs(char *page, char **start, off_t off,
549 int count, int *eof, void *data)
551 static inline int lprocfs_wr_evict_client(struct file *file, const char *buffer,
552 unsigned long count, void *data)
554 static inline int lprocfs_wr_ping(struct file *file, const char *buffer,
555 unsigned long count, void *data)
561 int lprocfs_rd_blksize(char *page, char **start, off_t off,
562 int count, int *eof, void *data) { return 0; }
564 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
565 int count, int *eof, void *data) { return 0; }
567 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
568 int count, int *eof, void *data) { return 0; }
570 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
571 int count, int *eof, void *data) { return 0; }
573 int lprocfs_rd_filestotal(char *page, char **start, off_t off,
574 int count, int *eof, void *data) { return 0; }
576 int lprocfs_rd_filesfree(char *page, char **start, off_t off,
577 int count, int *eof, void *data) { return 0; }
579 int lprocfs_rd_filegroups(char *page, char **start, off_t off,
580 int count, int *eof, void *data) { return 0; }
582 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value) {}
584 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value) {}
586 void lprocfs_oh_clear(struct obd_histogram *oh) {}
588 unsigned long lprocfs_oh_sum(struct obd_histogram *oh) { return 0; }
590 int lprocfs_counter_read(char *page, char **start, off_t off,
591 int count, int *eof, void *data) { return 0; }
593 int lprocfs_counter_write(struct file *file, const char *buffer,
594 unsigned long count, void *data) { return 0; }
597 __u64 lprocfs_stats_collector(struct lprocfs_stats *stats, int idx,
598 enum lprocfs_fields_flags field)
601 #define LPROCFS_ENTRY()
602 #define LPROCFS_EXIT()
603 #define LPROCFS_ENTRY_AND_CHECK(dp)
604 #define LPROC_SEQ_FOPS_RO(name)
605 #define LPROC_SEQ_FOPS(name)
609 #endif /* LPROCFS_SNMP_H */