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