Whamcloud - gitweb
Land b_smallfix onto HEAD (20040414_1359)
[fs/lustre-release.git] / lustre / include / linux / 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
28 #ifdef __KERNEL__
29 #include <linux/config.h>
30 #include <linux/autoconf.h>
31 #include <linux/proc_fs.h>
32 #include <linux/version.h>
33 #include <linux/smp.h>
34 #include <linux/kp30.h>
35
36 # if (LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,0))
37 #  include <linux/statfs.h>
38 # else 
39 #  define kstatfs statfs
40 # endif
41
42 #else 
43 #  define kstatfs statfs
44 #endif
45
46
47 #undef LPROCFS
48 #if (defined(__KERNEL__) && defined(CONFIG_PROC_FS))
49 # define LPROCFS
50 #endif
51
52 struct lprocfs_vars {
53         const char   *name;
54         read_proc_t *read_fptr;
55         write_proc_t *write_fptr;
56         void *data;
57 };
58
59 struct lprocfs_static_vars {
60         struct lprocfs_vars *module_vars;
61         struct lprocfs_vars *obd_vars;
62 };
63
64 /* An lprocfs counter can be configured using the enum bit masks below.
65  *
66  * LPROCFS_CNTR_EXTERNALLOCK indicates that an external lock already
67  * protects this counter from concurrent updates. If not specified,
68  * lprocfs an internal per-counter lock variable. External locks are
69  * not used to protect counter increments, but are used to protect
70  * counter readout and resets.
71  *
72  * LPROCFS_CNTR_AVGMINMAX indicates a multi-valued counter samples,
73  * (i.e. counter can be incremented by more than "1"). When specified,
74  * the counter maintains min, max and sum in addition to a simple
75  * invocation count. This allows averages to be be computed.
76  * If not specified, the counter is an increment-by-1 counter.
77  * min, max, sum, etc. are not maintained.
78  *
79  * LPROCFS_CNTR_STDDEV indicates that the counter should track sum of
80  * squares (for multi-valued counter samples only). This allows
81  * external computation of standard deviation, but involves a 64-bit
82  * multiply per counter increment.
83  */
84
85 enum {
86         LPROCFS_CNTR_EXTERNALLOCK = 0x0001,
87         LPROCFS_CNTR_AVGMINMAX    = 0x0002,
88         LPROCFS_CNTR_STDDEV       = 0x0004,
89
90         /* counter data type */
91         LPROCFS_TYPE_REGS         = 0x0100,
92         LPROCFS_TYPE_BYTES        = 0x0200,
93         LPROCFS_TYPE_PAGES        = 0x0400,
94         LPROCFS_TYPE_CYCLE        = 0x0800,
95 };
96
97 struct lprocfs_atomic {
98         atomic_t               la_entry;
99         atomic_t               la_exit;
100 };
101
102 struct lprocfs_counter {
103         struct lprocfs_atomic  lc_cntl;  /* may need to move to per set */
104         unsigned int           lc_config;
105         __u64                  lc_count;
106         __u64                  lc_sum;
107         __u64                  lc_min;
108         __u64                  lc_max;
109         __u64                  lc_sumsquare;
110         const char            *lc_name;   /* must be static */
111         const char            *lc_units;  /* must be static */
112 };
113
114 struct lprocfs_percpu {
115         struct lprocfs_counter lp_cntr[0];
116 };
117
118
119 struct lprocfs_stats {
120         unsigned int           ls_num;     /* # of counters */
121         unsigned int           ls_percpu_size;
122         struct lprocfs_percpu *ls_percpu[0];
123 };
124
125
126 /* class_obd.c */
127 extern struct proc_dir_entry *proc_lustre_root;
128
129 struct obd_device;
130 struct file;
131
132 #ifdef LPROCFS
133
134 /* Two optimized LPROCFS counter increment functions are provided:
135  *     lprocfs_counter_incr(cntr, value) - optimized for by-one counters
136  *     lprocfs_counter_add(cntr) - use for multi-valued counters
137  * Counter data layout allows config flag, counter lock and the
138  * count itself to reside within a single cache line.
139  */
140
141 static inline void lprocfs_counter_add(struct lprocfs_stats *stats, int idx,
142                                        long amount)
143 {
144         struct lprocfs_counter *percpu_cntr;
145
146         LASSERT(stats != NULL);
147         percpu_cntr = &(stats->ls_percpu[smp_processor_id()]->lp_cntr[idx]);
148         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
149         percpu_cntr->lc_count++;
150
151         if (percpu_cntr->lc_config & LPROCFS_CNTR_AVGMINMAX) {
152                 percpu_cntr->lc_sum += amount;
153                 if (percpu_cntr->lc_config & LPROCFS_CNTR_STDDEV)
154                         percpu_cntr->lc_sumsquare += (__u64)amount * amount;
155                 if (amount < percpu_cntr->lc_min)
156                         percpu_cntr->lc_min = amount;
157                 if (amount > percpu_cntr->lc_max)
158                         percpu_cntr->lc_max = amount;
159         }
160         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
161 }
162
163 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats, int idx)
164 {
165         struct lprocfs_counter *percpu_cntr;
166
167         LASSERT(stats != NULL);
168         percpu_cntr = &(stats->ls_percpu[smp_processor_id()]->lp_cntr[idx]);
169         atomic_inc(&percpu_cntr->lc_cntl.la_entry);
170         percpu_cntr->lc_count++;
171         atomic_inc(&percpu_cntr->lc_cntl.la_exit);
172 }
173
174 extern struct lprocfs_stats *lprocfs_alloc_stats(unsigned int num);
175 extern void lprocfs_free_stats(struct lprocfs_stats *stats);
176 extern int lprocfs_alloc_obd_stats(struct obd_device *obddev,
177                                    unsigned int num_private_stats);
178 extern void lprocfs_counter_init(struct lprocfs_stats *stats, int index,
179                                  unsigned conf, const char *name,
180                                  const char *units);
181 extern void lprocfs_free_obd_stats(struct obd_device *obddev);
182 extern int lprocfs_register_stats(struct proc_dir_entry *root, const char *name,
183                                   struct lprocfs_stats *stats);
184
185 #define LPROCFS_INIT_VARS(name, vclass, vinstance)           \
186 void lprocfs_##name##_init_vars(struct lprocfs_static_vars *x)  \
187 {                                                      \
188         x->module_vars = vclass;                       \
189         x->obd_vars = vinstance;                       \
190 }                                                      \
191
192 #define lprocfs_init_vars(NAME, VAR)     \
193 do {      \
194         extern void lprocfs_##NAME##_init_vars(struct lprocfs_static_vars *);  \
195         lprocfs_##NAME##_init_vars(VAR);                                       \
196 } while (0)
197 /* lprocfs_status.c */
198 extern int lprocfs_add_vars(struct proc_dir_entry *root,
199                             struct lprocfs_vars *var,
200                             void *data);
201
202 extern struct proc_dir_entry *lprocfs_register(const char *name,
203                                                struct proc_dir_entry *parent,
204                                                struct lprocfs_vars *list,
205                                                void *data);
206
207 extern void lprocfs_remove(struct proc_dir_entry *root);
208
209 extern struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *root,
210                                            const char *name);
211
212 extern int lprocfs_obd_setup(struct obd_device *obd, struct lprocfs_vars *list);
213 extern int lprocfs_obd_cleanup(struct obd_device *obd);
214
215 /* Generic callbacks */
216
217 extern int lprocfs_rd_u64(char *page, char **start, off_t off,
218                           int count, int *eof, void *data);
219 extern int lprocfs_rd_uuid(char *page, char **start, off_t off,
220                            int count, int *eof, void *data);
221 extern int lprocfs_rd_name(char *page, char **start, off_t off,
222                            int count, int *eof, void *data);
223 extern int lprocfs_rd_fstype(char *page, char **start, off_t off,
224                              int count, int *eof, void *data);
225 extern int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
226                                   int count, int *eof, void *data);
227 extern int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
228                                 int count, int *eof, void *data);
229 extern int lprocfs_rd_num_exports(char *page, char **start, off_t off,
230                                   int count, int *eof, void *data);
231 extern int lprocfs_rd_numrefs(char *page, char **start, off_t off,
232                               int count, int *eof, void *data);
233
234 /* Statfs helpers */
235 extern int lprocfs_rd_blksize(char *page, char **start, off_t off,
236                               int count, int *eof, void *data);
237 extern int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
238                                   int count, int *eof, void *data);
239 extern int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
240                                  int count, int *eof, void *data);
241 extern int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
242                                  int count, int *eof, void *data);
243 extern int lprocfs_rd_filestotal(char *page, char **start, off_t off,
244                                  int count, int *eof, void *data);
245 extern int lprocfs_rd_filesfree(char *page, char **start, off_t off,
246                                 int count, int *eof, void *data);
247 extern int lprocfs_rd_filegroups(char *page, char **start, off_t off,
248                                  int count, int *eof, void *data);
249
250 extern int lprocfs_write_helper(const char *buffer, unsigned long count,
251                                 int *val);
252 extern int lprocfs_write_u64_helper(const char *buffer, unsigned long count,
253                                     __u64 *val);
254 int lprocfs_obd_seq_create(struct obd_device *dev, char *name, mode_t mode,
255                            struct file_operations *seq_fops, void *data);
256 struct obd_histogram;
257 void lprocfs_oh_tally(struct obd_histogram *oh, unsigned int value);
258 void lprocfs_oh_tally_log2(struct obd_histogram *oh, unsigned int value);
259 void lprocfs_oh_clear(struct obd_histogram *oh);
260 unsigned long lprocfs_oh_sum(struct obd_histogram *oh);
261
262 /* lprocfs_status.c: counter read/write functions */
263 extern int lprocfs_counter_read(char *page, char **start, off_t off,
264                                 int count, int *eof, void *data);
265 extern int lprocfs_counter_write(struct file *file, const char *buffer,
266                                  unsigned long count, void *data);
267 #else
268 /* LPROCFS is not defined */
269 static inline void lprocfs_counter_add(struct lprocfs_stats *stats,
270                                        int index, long amount) { return; }
271 static inline void lprocfs_counter_incr(struct lprocfs_stats *stats,
272                                         int index) { return; }
273 static inline void lprocfs_counter_init(struct lprocfs_stats *stats,
274                                         int index, unsigned conf,
275                                         const char *name, const char *units)
276 { return; }
277
278 static inline struct lprocfs_stats* lprocfs_alloc_stats(unsigned int num)
279 { return NULL; }
280 static inline void lprocfs_free_stats(struct lprocfs_stats *stats)
281 { return; }
282
283 static inline int lprocfs_register_stats(struct proc_dir_entry *root,
284                                             const char *name,
285                                             struct lprocfs_stats *stats)
286 { return 0; }
287 static inline int lprocfs_alloc_obd_stats(struct obd_device *obddev,
288                                              unsigned int num_private_stats)
289 { return 0; }
290 static inline void lprocfs_free_obd_stats(struct obd_device *obddev)
291 { return; }
292
293 static inline struct proc_dir_entry *
294 lprocfs_register(const char *name, struct proc_dir_entry *parent,
295                  struct lprocfs_vars *list, void *data) { return NULL; }
296 #define LPROCFS_INIT_VARS(name, vclass, vinstance) do {} while (0)
297 #define lprocfs_init_vars(...) do {} while (0)
298 static inline int lprocfs_add_vars(struct proc_dir_entry *root,
299                                    struct lprocfs_vars *var,
300                                    void *data) { return 0; }
301 static inline void lprocfs_remove(struct proc_dir_entry *root) {};
302 static inline struct proc_dir_entry *lprocfs_srch(struct proc_dir_entry *head,
303                                     const char *name) {return 0;}
304 static inline int lprocfs_obd_setup(struct obd_device *dev,
305                                     struct lprocfs_vars *list) { return 0; }
306 static inline int lprocfs_obd_cleanup(struct obd_device *dev)  { return 0; }
307 static inline int lprocfs_rd_u64(char *page, char **start, off_t off,
308                                  int count, int *eof, void *data) { return 0; }
309 static inline int lprocfs_rd_uuid(char *page, char **start, off_t off,
310                                   int count, int *eof, void *data) { return 0; }
311 static inline int lprocfs_rd_name(char *page, char **start, off_t off,
312                                   int count, int *eof, void *data) { return 0; }
313 static inline int lprocfs_rd_server_uuid(char *page, char **start, off_t off,
314                                          int count, int *eof, void *data)
315 { return 0; }
316 static inline int lprocfs_rd_conn_uuid(char *page, char **start, off_t off,
317                                        int count, int *eof, void *data)
318 { return 0; }
319 static inline int lprocfs_rd_num_exports(char *page, char **start, off_t off,
320                                          int count, int *eof, void *data)
321 { return 0; }
322 static inline int lprocfs_rd_numrefs(char *page, char **start, off_t off,
323                                      int count, int *eof, void *data) { return 0; }
324
325 /* Statfs helpers */
326 static inline
327 int lprocfs_rd_blksize(char *page, char **start, off_t off,
328                        int count, int *eof, void *data) { return 0; }
329 static inline
330 int lprocfs_rd_kbytestotal(char *page, char **start, off_t off,
331                            int count, int *eof, void *data) { return 0; }
332 static inline
333 int lprocfs_rd_kbytesfree(char *page, char **start, off_t off,
334                           int count, int *eof, void *data) { return 0; }
335 static inline
336 int lprocfs_rd_kbytesavail(char *page, char **start, off_t off,
337                            int count, int *eof, void *data) { return 0; }
338 static inline
339 int lprocfs_rd_filestotal(char *page, char **start, off_t off,
340                           int count, int *eof, void *data) { return 0; }
341 static inline
342 int lprocfs_rd_filesfree(char *page, char **start, off_t off,
343                          int count, int *eof, void *data)  { return 0; }
344 static inline
345 int lprocfs_rd_filegroups(char *page, char **start, off_t off,
346                           int count, int *eof, void *data) { return 0; }
347 static inline
348 int lprocfs_counter_read(char *page, char **start, off_t off,
349                          int count, int *eof, void *data) { return 0; }
350 static inline
351 int lprocfs_counter_write(struct file *file, const char *buffer,
352                           unsigned long count, void *data) { return 0; }
353 #endif /* LPROCFS */
354
355 #endif /* LPROCFS_SNMP_H */