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