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