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_NOPERCPU = 0x0001, /* stats have no percpu
144 * area and need locking */
147 struct lprocfs_stats {
148 unsigned int ls_num; /* # of counters */
149 unsigned int ls_percpu_size;
150 int ls_flags; /* See LPROCFS_STATS_FLAG_* */
151 spinlock_t ls_lock; /* Lock used only when there are
152 * no percpu stats areas */
153 struct lprocfs_percpu *ls_percpu[0];
158 extern cfs_proc_dir_entry_t *proc_lustre_root;
162 struct obd_histogram;
166 static inline int lprocfs_stats_lock(struct lprocfs_stats *stats, int type)
170 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU) {
171 if (type & LPROCFS_GET_NUM_CPU)
173 if (type & LPROCFS_GET_SMP_ID)
175 spin_lock(&stats->ls_lock);
177 if (type & LPROCFS_GET_NUM_CPU)
178 rc = num_possible_cpus();
179 if (type & LPROCFS_GET_SMP_ID)
180 rc = smp_processor_id();
185 static inline void lprocfs_stats_unlock(struct lprocfs_stats *stats)
187 if (stats->ls_flags & LPROCFS_STATS_FLAG_NOPERCPU)
188 spin_unlock(&stats->ls_lock);
191 /* Two optimized LPROCFS counter increment functions are provided:
192 * lprocfs_counter_incr(cntr, value) - optimized for by-one counters
193 * lprocfs_counter_add(cntr) - use for multi-valued counters
194 * Counter data layout allows config flag, counter lock and the
195 * count itself to reside within a single cache line.
198 static inline void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
201 struct lprocfs_counter *percpu_cntr;
207 /* With per-client stats, statistics are allocated only for
208 * single CPU area, so the smp_id should be 0 always. */
209 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
211 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
212 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
213 percpu_cntr->lc_count++;
215 if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
216 percpu_cntr->lc_sum += amount;
217 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
218 percpu_cntr->lc_sumsquare += (__u64)amount * amount;
219 if (amount < percpu_cntr->lc_min)
220 percpu_cntr->lc_min = amount;
221 if (amount > percpu_cntr->lc_max)
222 percpu_cntr->lc_max = amount;
224 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
225 lprocfs_stats_unlock(stats);
228 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats, int idx)
230 struct lprocfs_counter *percpu_cntr;
236 smp_id = lprocfs_stats_lock(stats, LPROCFS_GET_SMP_ID);
237 percpu_cntr = &(stats->ls_percpu[smp_id]->lp_cntr[idx]);
239 atomic_inc(&percpu_cntr->lc_cntl.la_entry);
240 percpu_cntr->lc_count++;
241 atomic_inc(&percpu_cntr->lc_cntl.la_exit);
242 lprocfs_stats_unlock(stats);
245 extern struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num,
246 enum lprocfs_stats_flags flags);
247 extern void lprocfs_clear_stats(struct lprocfs_stats *stats);
248 extern void lprocfs_free_stats(struct lprocfs_stats **stats);
249 extern void lprocfs_init_ops_stats(int num_private_stats,
250 struct lprocfs_stats *stats);
251 extern int lprocfs_alloc_obd_stats(struct obd_device *obddev,
252 unsigned int num_private_stats);
253 extern void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
254 unsigned conf, const char *name,
256 extern void lprocfs_free_obd_stats(struct obd_device *obddev);
258 extern int lprocfs_exp_setup(struct obd_export *exp);
259 extern int lprocfs_exp_cleanup(struct obd_export *exp);
260 extern int lprocfs_register_stats(cfs_proc_dir_entry_t *root, const char *name,
261 struct lprocfs_stats *stats);
263 #define LPROCFS_INIT_VARS(name, vclass, vinstance) \
264 void lprocfs_##name##_init_vars(struct lprocfs_static_vars *x) \
266 x->module_vars = vclass; \
267 x->obd_vars = vinstance; \
270 #define lprocfs_init_vars(NAME, VAR) \
272 extern void lprocfs_##NAME##_init_vars(struct lprocfs_static_vars *); \
273 lprocfs_##NAME##_init_vars(VAR); \
276 /* lprocfs_status.c */
277 extern int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
278 struct lprocfs_vars *var,
281 extern cfs_proc_dir_entry_t *lprocfs_register(const char *name,
282 cfs_proc_dir_entry_t *parent,
283 struct lprocfs_vars *list,
286 extern void lprocfs_remove(cfs_proc_dir_entry_t **root);
288 extern cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *root,
291 extern int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list);
292 extern int lprocfs_obd_cleanup(struct obd_device *obd);
293 extern struct file_operations lprocfs_evict_client_fops;
295 extern int lprocfs_seq_create(cfs_proc_dir_entry_t *parent, char *name,
296 mode_t mode, struct file_operations *seq_fops,
298 extern int lprocfs_obd_seq_create(struct obd_device *dev, char *name,
299 mode_t mode, struct file_operations *seq_fops,
302 /* Generic callbacks */
304 extern int lprocfs_rd_u64(char *page, char **start, off_t off,
305 int count, int *eof, void *data);
306 extern int lprocfs_rd_atomic(char *page, char **start, off_t off,
307 int count, int *eof, void *data);
308 extern int lprocfs_wr_atomic(struct file *file, const char *buffer,
309 unsigned long count, void *data);
310 extern int lprocfs_rd_uint(char *page, char **start, off_t off,
311 int count, int *eof, void *data);
312 extern int lprocfs_wr_uint(struct file *file, const char *buffer,
313 unsigned long count, void *data);
314 extern int lprocfs_rd_uuid(char *page, char **start, off_t off,
315 int count, int *eof, void *data);
316 extern int lprocfs_rd_name(char *page, char **start, off_t off,
317 int count, int *eof, void *data);
318 extern int lprocfs_rd_fstype(char *page, char **start, off_t off,
319 int count, int *eof, void *data);
320 extern int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
321 int count, int *eof, void *data);
322 extern int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
323 int count, int *eof, void *data);
324 extern int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
325 int count, int *eof, void *data);
326 extern int lprocfs_rd_num_exports(char *page, char **start, off_t off,
327 int count, int *eof, void *data);
328 extern int lprocfs_rd_numrefs(char *page, char **start, off_t off,
329 int count, int *eof, void *data);
330 extern int lprocfs_wr_evict_client(struct file *file, const char *buffer,
331 unsigned long count, void *data);
332 extern int lprocfs_wr_ping(struct file *file, const char *buffer,
333 unsigned long count, void *data);
336 extern int lprocfs_rd_blksize(char *page, char **start, off_t off,
337 int count, int *eof, void *data);
338 extern int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
339 int count, int *eof, void *data);
340 extern int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
341 int count, int *eof, void *data);
342 extern int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
343 int count, int *eof, void *data);
344 extern int lprocfs_rd_filestotal(char *page, char **start, off_t off,
345 int count, int *eof, void *data);
346 extern int lprocfs_rd_filesfree(char *page, char **start, off_t off,
347 int count, int *eof, void *data);
348 extern int lprocfs_rd_filegroups(char *page, char **start, off_t off,
349 int count, int *eof, void *data);
351 extern int lprocfs_write_helper(const char *buffer, unsigned long count,
353 extern int lprocfs_write_frac_helper(const char *buffer, unsigned long count,
355 extern int lprocfs_read_frac_helper(char *buffer, unsigned long count,
357 extern int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
359 extern int lprocfs_write_frac_u64_helper(const char *buffer, unsigned long count,
360 __u64 *val, int mult);
361 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value);
362 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value);
363 void lprocfs_oh_clear(struct obd_histogram *oh);
364 unsigned long lprocfs_oh_sum(struct obd_histogram *oh);
366 /* lprocfs_status.c: counter read/write functions */
367 extern int lprocfs_counter_read(char *page, char **start, off_t off,
368 int count, int *eof, void *data);
369 extern int lprocfs_counter_write(struct file *file, const char *buffer,
370 unsigned long count, void *data);
372 /* lprocfs_status.c: recovery status */
373 int lprocfs_obd_rd_recovery_status(char *page, char **start, off_t off,
374 int count, int *eof, void *data);
376 extern int lprocfs_seq_release(struct inode *, struct file *);
378 /* in lprocfs_stat.c, to protect the private data for proc entries */
379 extern struct rw_semaphore _lprocfs_lock;
380 #define LPROCFS_ENTRY() do { \
381 down_read(&_lprocfs_lock); \
383 #define LPROCFS_EXIT() do { \
384 up_read(&_lprocfs_lock); \
386 #define LPROCFS_ENTRY_AND_CHECK(dp) do { \
387 typecheck(struct proc_dir_entry *, dp); \
389 if ((dp)->deleted) { \
395 /* You must use these macros when you want to refer to
396 * the import in a client obd_device for a lprocfs entry */
397 #define LPROCFS_CLIMP_CHECK(obd) do { \
398 typecheck(struct obd_device *, obd); \
399 mutex_down(&(obd)->u.cli.cl_sem); \
400 if ((obd)->u.cli.cl_import == NULL) { \
401 mutex_up(&(obd)->u.cli.cl_sem); \
405 #define LPROCFS_CLIMP_EXIT(obd) \
406 mutex_up(&(obd)->u.cli.cl_sem);
409 /* write the name##_seq_show function, call LPROC_SEQ_FOPS_RO for read-only
410 proc entries; otherwise, you will define name##_seq_write function also for
411 a read-write proc entry, and then call LPROC_SEQ_SEQ instead. Finally,
412 call lprocfs_obd_seq_create(obd, filename, 0444, &name#_fops, data); */
413 #define __LPROC_SEQ_FOPS(name, custom_seq_write) \
414 static int name##_seq_open(struct inode *inode, struct file *file) { \
415 struct proc_dir_entry *dp = PDE(inode); \
417 LPROCFS_ENTRY_AND_CHECK(dp); \
418 rc = single_open(file, name##_seq_show, dp->data); \
425 struct file_operations name##_fops = { \
426 .owner = THIS_MODULE, \
427 .open = name##_seq_open, \
429 .write = custom_seq_write, \
430 .llseek = seq_lseek, \
431 .release = lprocfs_seq_release, \
434 #define LPROC_SEQ_FOPS_RO(name) __LPROC_SEQ_FOPS(name, NULL)
435 #define LPROC_SEQ_FOPS(name) __LPROC_SEQ_FOPS(name, name##_seq_write)
438 /* LPROCFS is not defined */
439 static inline void lprocfs_counter_add(struct lprocfs_stats *stats,
440 int index, long amount) { return; }
441 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats,
442 int index) { return; }
443 static inline void lprocfs_counter_init(struct lprocfs_stats *stats,
444 int index, unsigned conf,
445 const char *name, const char *units)
448 static inline struct lprocfs_stats* lprocfs_alloc_stats(unsigned int num,
451 static inline void lprocfs_clear_stats(struct lprocfs_stats *stats)
453 static inline void lprocfs_free_stats(struct lprocfs_stats **stats)
455 static inline int lprocfs_register_stats(cfs_proc_dir_entry_t *root,
457 struct lprocfs_stats *stats)
459 static inline void lprocfs_init_ops_stats(int num_private_stats,
460 struct lprocfs_stats *stats)
462 static inline int lprocfs_alloc_obd_stats(struct obd_device *obddev,
463 unsigned int num_private_stats)
465 static inline void lprocfs_free_obd_stats(struct obd_device *obddev)
469 static inline int lprocfs_exp_setup(struct obd_export *exp)
471 static inline int lprocfs_exp_cleanup(struct obd_export *exp)
474 static inline cfs_proc_dir_entry_t *
475 lprocfs_register(const char *name, cfs_proc_dir_entry_t *parent,
476 struct lprocfs_vars *list, void *data) { return NULL; }
477 #define LPROCFS_INIT_VARS(name, vclass, vinstance)
478 #define lprocfs_init_vars(...) do {} while (0)
479 static inline int lprocfs_add_vars(cfs_proc_dir_entry_t *root,
480 struct lprocfs_vars *var,
481 void *data) { return 0; }
482 static inline void lprocfs_remove(cfs_proc_dir_entry_t **root) {};
483 static inline cfs_proc_dir_entry_t *lprocfs_srch(cfs_proc_dir_entry_t *head,
484 const char *name) {return 0;}
485 static inline int lprocfs_obd_setup(struct obd_device *dev,
486 struct lprocfs_vars *list) { return 0; }
487 static inline int lprocfs_obd_cleanup(struct obd_device *dev) { return 0; }
488 static inline int lprocfs_rd_u64(char *page, char **start, off_t off,
489 int count, int *eof, void *data) { return 0; }
490 static inline int lprocfs_rd_uuid(char *page, char **start, off_t off,
491 int count, int *eof, void *data) { return 0; }
492 static inline int lprocfs_rd_name(char *page, char **start, off_t off,
493 int count, int *eof, void *data) { return 0; }
494 static inline int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
495 int count, int *eof, void *data)
497 static inline int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
498 int count, int *eof, void *data)
500 static inline int lprocfs_rd_connect_flags(char *page, char **start, off_t off,
501 int count, int *eof, void *data)
503 static inline int lprocfs_rd_num_exports(char *page, char **start, off_t off,
504 int count, int *eof, void *data)
506 static inline int lprocfs_rd_numrefs(char *page, char **start, off_t off,
507 int count, int *eof, void *data)
509 static inline int lprocfs_wr_evict_client(struct file *file, const char *buffer,
510 unsigned long count, void *data)
512 static inline int lprocfs_wr_ping(struct file *file, const char *buffer,
513 unsigned long count, void *data)
519 int lprocfs_rd_blksize(char *page, char **start, off_t off,
520 int count, int *eof, void *data) { return 0; }
522 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
523 int count, int *eof, void *data) { return 0; }
525 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
526 int count, int *eof, void *data) { return 0; }
528 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
529 int count, int *eof, void *data) { return 0; }
531 int lprocfs_rd_filestotal(char *page, char **start, off_t off,
532 int count, int *eof, void *data) { return 0; }
534 int lprocfs_rd_filesfree(char *page, char **start, off_t off,
535 int count, int *eof, void *data) { return 0; }
537 int lprocfs_rd_filegroups(char *page, char **start, off_t off,
538 int count, int *eof, void *data) { return 0; }
540 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value) {}
542 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value) {}
544 void lprocfs_oh_clear(struct obd_histogram *oh) {}
546 unsigned long lprocfs_oh_sum(struct obd_histogram *oh) { return 0; }
548 int lprocfs_counter_read(char *page, char **start, off_t off,
549 int count, int *eof, void *data) { return 0; }
551 int lprocfs_counter_write(struct file *file, const char *buffer,
552 unsigned long count, void *data) { return 0; }
554 #define LPROCFS_ENTRY()
555 #define LPROCFS_EXIT()
556 #define LPROCFS_ENTRY_AND_CHECK(dp)
557 #define LPROC_SEQ_FOPS_RO(name)
558 #define LPROC_SEQ_FOPS(name)
562 #endif /* LPROCFS_SNMP_H */