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