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