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