Whamcloud - gitweb
Land b_smallfix onto HEAD (20040414_1359)
[fs/lustre-release.git] / lustre / obdfilter / lproc_obdfilter.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2002, 2003 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  */
22 #define DEBUG_SUBSYSTEM S_CLASS
23
24 #include <linux/version.h>
25 #include <linux/lprocfs_status.h>
26 #include <linux/obd.h>
27 #include <linux/seq_file.h>
28 #include <linux/version.h>
29
30 #include "filter_internal.h"
31
32 #ifndef LPROCFS
33 static struct lprocfs_vars lprocfs_obd_vars[]  = { {0} };
34 static struct lprocfs_vars lprocfs_module_vars[] = { {0} };
35 #else
36
37 static int lprocfs_filter_rd_groups(char *page, char **start, off_t off,
38                                     int count, int *eof, void *data)
39 {
40         *eof = 1;
41         return snprintf(page, count, "%u\n", FILTER_GROUPS);
42 }
43
44 static int lprocfs_filter_rd_tot_dirty(char *page, char **start, off_t off,
45                                        int count, int *eof, void *data)
46 {
47         struct obd_device *obd = (struct obd_device *)data;
48
49         LASSERT(obd != NULL);
50         *eof = 1;
51         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_dirty);
52 }
53
54 static int lprocfs_filter_rd_tot_granted(char *page, char **start, off_t off,
55                                          int count, int *eof, void *data)
56 {
57         struct obd_device *obd = (struct obd_device *)data;
58
59         LASSERT(obd != NULL);
60         *eof = 1;
61         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_granted);
62 }
63
64 static int lprocfs_filter_rd_tot_pending(char *page, char **start, off_t off,
65                                          int count, int *eof, void *data)
66 {
67         struct obd_device *obd = (struct obd_device *)data;
68
69         LASSERT(obd != NULL);
70         *eof = 1;
71         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_pending);
72 }
73
74 static int lprocfs_filter_rd_mntdev(char *page, char **start, off_t off,
75                                     int count, int *eof, void *data)
76 {
77         struct obd_device *obd = (struct obd_device *)data;
78
79         LASSERT(obd != NULL);
80         LASSERT(obd->u.filter.fo_vfsmnt->mnt_devname);
81         *eof = 1;
82         return snprintf(page, count, "%s\n",
83                         obd->u.filter.fo_vfsmnt->mnt_devname);
84 }
85
86 static int lprocfs_filter_rd_last_id(char *page, char **start, off_t off,
87                                      int count, int *eof, void *data)
88 {
89         struct obd_device *obd = data;
90
91         if (obd == NULL)
92                 return 0;
93
94         return snprintf(page, count, LPU64"\n",
95                         filter_last_id(&obd->u.filter, 0));
96 }
97
98 int lprocfs_filter_rd_readcache(char *page, char **start, off_t off, int count,
99                                 int *eof, void *data)
100 {
101         struct obd_device *obd = data;
102         int rc;
103
104         rc = snprintf(page, count, LPU64"\n",
105                       obd->u.filter.fo_readcache_max_filesize);
106         return rc;
107 }
108
109 int lprocfs_filter_wr_readcache(struct file *file, const char *buffer,
110                                 unsigned long count, void *data)
111 {
112         struct obd_device *obd = data;
113         __u64 val;
114         int rc;
115
116         rc = lprocfs_write_u64_helper(buffer, count, &val);
117         if (rc)
118                 return rc;
119
120         obd->u.filter.fo_readcache_max_filesize = val;
121         return count;
122 }
123
124 static struct lprocfs_vars lprocfs_obd_vars[] = {
125         { "uuid",         lprocfs_rd_uuid,          0, 0 },
126         { "blocksize",    lprocfs_rd_blksize,       0, 0 },
127         { "kbytestotal",  lprocfs_rd_kbytestotal,   0, 0 },
128         { "kbytesfree",   lprocfs_rd_kbytesfree,    0, 0 },
129         { "kbytesavail",  lprocfs_rd_kbytesavail,   0, 0 },
130         { "filestotal",   lprocfs_rd_filestotal,    0, 0 },
131         { "filesfree",    lprocfs_rd_filesfree,     0, 0 },
132         { "filegroups",   lprocfs_filter_rd_groups, 0, 0 },
133         { "fstype",       lprocfs_rd_fstype,        0, 0 },
134         { "mntdev",       lprocfs_filter_rd_mntdev, 0, 0 },
135         { "last_id",      lprocfs_filter_rd_last_id,0, 0 },
136         { "tot_dirty",    lprocfs_filter_rd_tot_dirty,   0, 0 },
137         { "tot_pending",  lprocfs_filter_rd_tot_pending, 0, 0 },
138         { "tot_granted",  lprocfs_filter_rd_tot_granted, 0, 0 },
139         { "num_exports",  lprocfs_rd_num_exports,   0, 0 },
140         { "readcache_max_filesize",
141                           lprocfs_filter_rd_readcache,
142                           lprocfs_filter_wr_readcache, 0 },
143         { 0 }
144 };
145
146 static struct lprocfs_vars lprocfs_module_vars[] = {
147         { "num_refs",     lprocfs_rd_numrefs,       0, 0 },
148         { 0 }
149 };
150
151 void filter_tally_write(struct filter_obd *filter, struct page **pages,
152                      int nr_pages, unsigned long *blocks, int blocks_per_page)
153 {
154         struct page *last_page = NULL;
155         unsigned long *last_block = NULL;
156         unsigned long discont_pages = 0;
157         unsigned long discont_blocks = 0;
158         int i;
159
160         if (nr_pages == 0)
161                 return;
162
163         lprocfs_oh_tally_log2(&filter->fo_w_pages, nr_pages);
164
165         while (nr_pages-- > 0) {
166                 if (last_page && (*pages)->index != (last_page->index + 1))
167                         discont_pages++;
168                 last_page = *pages;
169                 pages++;
170                 for (i = 0; i < blocks_per_page; i++) {
171                         if (last_block && *blocks != (*last_block + 1))
172                                 discont_blocks++;
173                         last_block = blocks++;
174                 }
175         }
176
177         lprocfs_oh_tally(&filter->fo_w_discont_pages, discont_pages);
178         lprocfs_oh_tally(&filter->fo_w_discont_blocks, discont_blocks);
179 }
180
181 void filter_tally_read(struct filter_obd *filter, struct niobuf_local *lnb,
182                        int niocount)
183 {
184         struct niobuf_local *end;
185         struct page *last_page = NULL;
186         unsigned long discont_pages = 0;
187         unsigned long discont_blocks = 0;
188
189         if (niocount == 0)
190                 return;
191
192         for (end = lnb + niocount; lnb < end && lnb->page; lnb++) {
193                 struct page *page = lnb->page;
194                 if (last_page) {
195                        if (page->index != (last_page->index + 1))
196                                 discont_pages++;
197                         /* XXX not so smart for now */
198 #if (LINUX_VERSION_CODE < KERNEL_VERSION(2,5,0))
199                         if ((page->buffers && last_page->buffers) &&
200                             (page->buffers->b_blocknr !=
201                              (last_page->buffers->b_blocknr + 1)))
202                                 discont_blocks++;
203 #else
204 #warning "port on 2.6 -bzzz"
205 #endif
206                 }
207                 last_page = page;
208         }
209
210         lprocfs_oh_tally_log2(&filter->fo_r_pages, niocount);
211         lprocfs_oh_tally(&filter->fo_r_discont_pages, discont_pages);
212         lprocfs_oh_tally(&filter->fo_r_discont_blocks, discont_blocks);
213 }
214
215 #define pct(a,b) (b ? a * 100 / b : 0)
216
217 static int filter_brw_stats_seq_show(struct seq_file *seq, void *v)
218 {
219         struct timeval now;
220         struct obd_device *dev = seq->private;
221         struct filter_obd *filter = &dev->u.filter;
222         unsigned long read_tot = 0, write_tot = 0, read_cum, write_cum;
223         int i;
224
225         do_gettimeofday(&now);
226
227         /* this sampling races with updates */
228
229         seq_printf(seq, "snapshot_time:         %lu:%lu (secs:usecs)\n",
230                    now.tv_sec, now.tv_usec);
231
232         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
233         seq_printf(seq, "pages per brw         brws   %% cum %% |");
234         seq_printf(seq, "       rpcs   %% cum %%\n");
235
236         read_tot = lprocfs_oh_sum(&filter->fo_r_pages);
237         write_tot = lprocfs_oh_sum(&filter->fo_w_pages);
238
239         read_cum = 0;
240         write_cum = 0;
241         for (i = 0; i < OBD_HIST_MAX; i++) {
242                 unsigned long r = filter->fo_r_pages.oh_buckets[i];
243                 unsigned long w = filter->fo_w_pages.oh_buckets[i];
244                 read_cum += r;
245                 write_cum += w;
246                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
247                                  1 << i, r, pct(r, read_tot),
248                                  pct(read_cum, read_tot), w,
249                                  pct(w, write_tot),
250                                  pct(write_cum, write_tot));
251                 if (read_cum == read_tot && write_cum == write_tot)
252                         break;
253         }
254
255         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
256         seq_printf(seq, "discont pages        rpcs   %% cum %% |");
257         seq_printf(seq, "       rpcs   %% cum %%\n");
258
259         read_tot = lprocfs_oh_sum(&filter->fo_r_discont_pages);
260         write_tot = lprocfs_oh_sum(&filter->fo_w_discont_pages);
261
262         read_cum = 0;
263         write_cum = 0;
264
265         for (i = 0; i < OBD_HIST_MAX; i++) {
266                 unsigned long r = filter->fo_r_discont_pages.oh_buckets[i];
267                 unsigned long w = filter->fo_w_discont_pages.oh_buckets[i];
268                 read_cum += r;
269                 write_cum += w;
270                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
271                                  i, r, pct(r, read_tot),
272                                  pct(read_cum, read_tot), w,
273                                  pct(w, write_tot),
274                                  pct(write_cum, write_tot));
275                 if (read_cum == read_tot && write_cum == write_tot)
276                         break;
277         }
278
279         seq_printf(seq, "\n\t\t\tread\t\t\twrite\n");
280         seq_printf(seq, "discont blocks        rpcs   %% cum %% |");
281         seq_printf(seq, "       rpcs   %% cum %%\n");
282
283         read_tot = lprocfs_oh_sum(&filter->fo_r_discont_blocks);
284         write_tot = lprocfs_oh_sum(&filter->fo_w_discont_blocks);
285
286         read_cum = 0;
287         write_cum = 0;
288         for (i = 0; i < OBD_HIST_MAX; i++) {
289                 unsigned long r = filter->fo_r_discont_blocks.oh_buckets[i];
290                 unsigned long w = filter->fo_w_discont_blocks.oh_buckets[i];
291                 read_cum += r;
292                 write_cum += w;
293                 seq_printf(seq, "%d:\t\t%10lu %3lu %3lu   | %10lu %3lu %3lu\n",
294                                  i, r, pct(r, read_tot),
295                                  pct(read_cum, read_tot), w,
296                                  pct(w, write_tot),
297                                  pct(write_cum, write_tot));
298                 if (read_cum == read_tot && write_cum == write_tot)
299                         break;
300         }
301
302         return 0;
303 }
304 #undef pct
305
306 static void *filter_brw_stats_seq_start(struct seq_file *p, loff_t *pos)
307 {
308         if (*pos == 0)
309                 return (void *)1;
310         return NULL;
311 }
312 static void *filter_brw_stats_seq_next(struct seq_file *p, void *v, loff_t *pos)
313 {
314         ++*pos;
315         return NULL;
316 }
317 static void filter_brw_stats_seq_stop(struct seq_file *p, void *v)
318 {
319 }
320 struct seq_operations filter_brw_stats_seq_sops = {
321         .start = filter_brw_stats_seq_start,
322         .stop = filter_brw_stats_seq_stop,
323         .next = filter_brw_stats_seq_next,
324         .show = filter_brw_stats_seq_show,
325 };
326
327 static int filter_brw_stats_seq_open(struct inode *inode, struct file *file)
328 {
329         struct proc_dir_entry *dp = PDE(inode);
330         struct seq_file *seq;
331         int rc;
332
333         rc = seq_open(file, &filter_brw_stats_seq_sops);
334         if (rc)
335                 return rc;
336         seq = file->private_data;
337         seq->private = dp->data;
338         return 0;
339 }
340
341 static ssize_t filter_brw_stats_seq_write(struct file *file, const char *buf,
342                                        size_t len, loff_t *off)
343 {
344         struct seq_file *seq = file->private_data;
345         struct obd_device *dev = seq->private;
346         struct filter_obd *filter = &dev->u.filter;
347
348         lprocfs_oh_clear(&filter->fo_r_pages);
349         lprocfs_oh_clear(&filter->fo_w_pages);
350         lprocfs_oh_clear(&filter->fo_r_discont_pages);
351         lprocfs_oh_clear(&filter->fo_w_discont_pages);
352         lprocfs_oh_clear(&filter->fo_r_discont_blocks);
353         lprocfs_oh_clear(&filter->fo_w_discont_blocks);
354
355         return len;
356 }
357
358 struct file_operations filter_brw_stats_fops = {
359         .owner   = THIS_MODULE,
360         .open    = filter_brw_stats_seq_open,
361         .read    = seq_read,
362         .write   = filter_brw_stats_seq_write,
363         .llseek  = seq_lseek,
364         .release = seq_release,
365 };
366
367 int lproc_filter_attach_seqstat(struct obd_device *dev)
368 {
369         return lprocfs_obd_seq_create(dev, "brw_stats", 0444,
370                                       &filter_brw_stats_fops, dev);
371 }
372
373
374
375 #endif /* LPROCFS */
376 LPROCFS_INIT_VARS(filter, lprocfs_module_vars, lprocfs_obd_vars)