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