Whamcloud - gitweb
b=13499
[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 the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  *
24  */
25 #define DEBUG_SUBSYSTEM S_CLASS
26
27 #include <linux/version.h>
28 #include <lprocfs_status.h>
29 #include <obd.h>
30 #include <linux/seq_file.h>
31 #include <linux/version.h>
32
33 #include "filter_internal.h"
34
35 #ifdef LPROCFS
36 static int lprocfs_filter_rd_groups(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         *eof = 1;
41         return snprintf(page, count, "%u\n", obd->u.filter.fo_group_count);
42 }
43
44 static int lprocfs_filter_rd_tot_dirty(char *page, char **start, off_t off,
45                                        int count, int *eof, void *data)
46 {
47         struct obd_device *obd = (struct obd_device *)data;
48
49         LASSERT(obd != NULL);
50         *eof = 1;
51         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_dirty);
52 }
53
54 static int lprocfs_filter_rd_tot_granted(char *page, char **start, off_t off,
55                                          int count, int *eof, void *data)
56 {
57         struct obd_device *obd = (struct obd_device *)data;
58
59         LASSERT(obd != NULL);
60         *eof = 1;
61         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_granted);
62 }
63
64 static int lprocfs_filter_rd_tot_pending(char *page, char **start, off_t off,
65                                          int count, int *eof, void *data)
66 {
67         struct obd_device *obd = (struct obd_device *)data;
68
69         LASSERT(obd != NULL);
70         *eof = 1;
71         return snprintf(page, count, LPU64"\n", obd->u.filter.fo_tot_pending);
72 }
73
74 static int lprocfs_filter_rd_mntdev(char *page, char **start, off_t off,
75                                     int count, int *eof, void *data)
76 {
77         struct obd_device *obd = (struct obd_device *)data;
78
79         LASSERT(obd != NULL);
80         LASSERT(obd->u.filter.fo_vfsmnt->mnt_devname);
81         *eof = 1;
82         return snprintf(page, count, "%s\n",
83                         obd->u.filter.fo_vfsmnt->mnt_devname);
84 }
85
86 static int lprocfs_filter_rd_last_id(char *page, char **start, off_t off,
87                                      int count, int *eof, void *data)
88 {
89         struct obd_device *obd = data;
90         struct filter_obd *filter = &obd->u.filter;
91         int retval = 0, rc, i;
92
93         if (obd == NULL)
94                 return 0;
95
96         for (i = FILTER_GROUP_MDS0; i < filter->fo_group_count; i++) {
97                 rc = snprintf(page, count, LPU64"\n",filter_last_id(filter, i));
98                 if (rc < 0) {
99                         retval = rc;
100                         break;
101                 }
102                 page += rc;
103                 count -= rc;
104                 retval += rc;
105         }
106         return retval;
107 }
108
109 int lprocfs_filter_rd_readcache(char *page, char **start, off_t off, int count,
110                                 int *eof, void *data)
111 {
112         struct obd_device *obd = data;
113         int rc;
114
115         rc = snprintf(page, count, LPU64"\n",
116                       obd->u.filter.fo_readcache_max_filesize);
117         return rc;
118 }
119
120 int lprocfs_filter_wr_readcache(struct file *file, const char *buffer,
121                                 unsigned long count, void *data)
122 {
123         struct obd_device *obd = data;
124         __u64 val;
125         int rc;
126
127         rc = lprocfs_write_u64_helper(buffer, count, &val);
128         if (rc)
129                 return rc;
130
131         obd->u.filter.fo_readcache_max_filesize = val;
132         return count;
133 }
134
135
136 int lprocfs_filter_rd_fmd_max_num(char *page, char **start, off_t off,
137                                   int count, int *eof, void *data)
138 {
139         struct obd_device *obd = data;
140         int rc;
141
142         rc = snprintf(page, count, "%u\n", obd->u.filter.fo_fmd_max_num);
143         return rc;
144 }
145
146 int lprocfs_filter_wr_fmd_max_num(struct file *file, const char *buffer,
147                                   unsigned long count, void *data)
148 {
149         struct obd_device *obd = data;
150         int val;
151         int rc;
152
153         rc = lprocfs_write_helper(buffer, count, &val);
154         if (rc)
155                 return rc;
156
157         if (val > 65536 || val < 1)
158                 return -EINVAL;
159
160         obd->u.filter.fo_fmd_max_num = val;
161         return count;
162 }
163
164 int lprocfs_filter_rd_fmd_max_age(char *page, char **start, off_t off,
165                                   int count, int *eof, void *data)
166 {
167         struct obd_device *obd = data;
168         int rc;
169
170         rc = snprintf(page, count, "%u\n", obd->u.filter.fo_fmd_max_age / HZ);
171         return rc;
172 }
173
174 int lprocfs_filter_wr_fmd_max_age(struct file *file, const char *buffer,
175                                   unsigned long count, void *data)
176 {
177         struct obd_device *obd = data;
178         int val;
179         int rc;
180
181         rc = lprocfs_write_helper(buffer, count, &val);
182         if (rc)
183                 return rc;
184
185         if (val > 65536 || val < 1)
186                 return -EINVAL;
187
188         obd->u.filter.fo_fmd_max_age = val * HZ;
189         return count;
190 }
191
192 static int lprocfs_filter_rd_capa(char *page, char **start, off_t off,
193                                   int count, int *eof, void *data)
194 {
195         struct obd_device *obd = data;
196         int rc;
197
198         rc = snprintf(page, count, "capability on: %s\n",
199                       obd->u.filter.fo_fl_oss_capa ? "oss" : "");
200         return rc;
201 }
202
203 static int lprocfs_filter_wr_capa(struct file *file, const char *buffer,
204                                   unsigned long count, void *data)
205 {
206         struct obd_device *obd = data;
207         int val, rc;
208
209         rc = lprocfs_write_helper(buffer, count, &val);
210         if (rc)
211                 return rc;
212
213         if (val & ~0x1) {
214                 CERROR("invalid capability mode, only 0/1 are accepted.\n"
215                        " 1: enable oss fid capability\n"
216                        " 0: disable oss fid capability\n");
217                 return -EINVAL;
218         }
219
220         obd->u.filter.fo_fl_oss_capa = val;
221         LCONSOLE_INFO("OSS %s %s fid capability.\n", obd->obd_name,
222                       val ? "enabled" : "disabled");
223         return count;
224 }
225
226 static int lprocfs_filter_rd_capa_count(char *page, char **start, off_t off,
227                                         int count, int *eof, void *data)
228 {
229         return snprintf(page, count, "%d %d\n",
230                         capa_count[CAPA_SITE_CLIENT],
231                         capa_count[CAPA_SITE_SERVER]);
232 }
233
234 static
235 int lprocfs_filter_rd_blacklist(char *page, char **start, off_t off, int count,
236                                 int *eof, void *data)
237 {
238         int rc;
239
240         rc = blacklist_display(page, count);
241         *eof = 1;
242         return rc;
243 }
244
245 static
246 int lprocfs_filter_wr_blacklist(struct file *file, const char *buffer,
247                                 unsigned long count, void *data)
248 {
249         int add;
250         uid_t uid = -1;
251
252         if (count < 2)
253                 return count;
254         if (buffer[0] == '+')
255                 add = 1;
256         else if (buffer[0] == '-')
257                 add = 0;
258         else
259                 return count;
260
261         sscanf(buffer + 1, "%u", &uid);
262         if (add)
263                 blacklist_add(uid);
264         else
265                 blacklist_del(uid);
266         return count;
267 }
268
269 static struct lprocfs_vars lprocfs_obd_vars[] = {
270         { "uuid",         lprocfs_rd_uuid,          0, 0 },
271         { "blocksize",    lprocfs_rd_blksize,       0, 0 },
272         { "kbytestotal",  lprocfs_rd_kbytestotal,   0, 0 },
273         { "kbytesfree",   lprocfs_rd_kbytesfree,    0, 0 },
274         { "kbytesavail",  lprocfs_rd_kbytesavail,   0, 0 },
275         { "filestotal",   lprocfs_rd_filestotal,    0, 0 },
276         { "filesfree",    lprocfs_rd_filesfree,     0, 0 },
277         { "filegroups",   lprocfs_filter_rd_groups, 0, 0 },
278         { "fstype",       lprocfs_rd_fstype,        0, 0 },
279         { "mntdev",       lprocfs_filter_rd_mntdev, 0, 0 },
280         { "last_id",      lprocfs_filter_rd_last_id,0, 0 },
281         { "tot_dirty",    lprocfs_filter_rd_tot_dirty,   0, 0 },
282         { "tot_pending",  lprocfs_filter_rd_tot_pending, 0, 0 },
283         { "tot_granted",  lprocfs_filter_rd_tot_granted, 0, 0 },
284         { "recovery_status", lprocfs_obd_rd_recovery_status, 0, 0 },
285         { "evict_client", 0, lprocfs_wr_evict_client, 0,
286                                 &lprocfs_evict_client_fops},
287         { "num_exports",  lprocfs_rd_num_exports,   0, 0 },
288         { "readcache_max_filesize",
289                           lprocfs_filter_rd_readcache,
290                           lprocfs_filter_wr_readcache, 0 },
291 #ifdef HAVE_QUOTA_SUPPORT
292         { "quota_bunit_sz", lprocfs_rd_bunit, lprocfs_wr_bunit, 0},
293         { "quota_btune_sz", lprocfs_rd_btune, lprocfs_wr_btune, 0},
294         { "quota_iunit_sz", lprocfs_rd_iunit, lprocfs_wr_iunit, 0},
295         { "quota_itune_sz", lprocfs_rd_itune, lprocfs_wr_itune, 0},
296         { "quota_type",     lprocfs_rd_type, lprocfs_wr_type, 0},
297 #endif
298         { "client_cache_count", lprocfs_filter_rd_fmd_max_num,
299                           lprocfs_filter_wr_fmd_max_num, 0 },
300         { "client_cache_seconds", lprocfs_filter_rd_fmd_max_age,
301                           lprocfs_filter_wr_fmd_max_age, 0 },
302         { "capa",         lprocfs_filter_rd_capa,
303                           lprocfs_filter_wr_capa, 0 },
304         { "capa_count",   lprocfs_filter_rd_capa_count, 0, 0 },
305         { "blacklist",    lprocfs_filter_rd_blacklist,
306                           lprocfs_filter_wr_blacklist, 0 },
307         { 0 }
308 };
309
310 static struct lprocfs_vars lprocfs_module_vars[] = {
311         { "num_refs",     lprocfs_rd_numrefs,       0, 0 },
312         { 0 }
313 };
314
315 void filter_tally(struct obd_export *exp, struct page **pages, int nr_pages,
316                   unsigned long *blocks, int blocks_per_page, int wr)
317 {
318         struct filter_obd *filter = &exp->exp_obd->u.filter;
319         struct filter_export_data *fed = &exp->exp_filter_data;
320         struct page *last_page = NULL;
321         unsigned long *last_block = NULL;
322         unsigned long discont_pages = 0;
323         unsigned long discont_blocks = 0;
324         int i;
325
326         if (nr_pages == 0)
327                 return;
328
329         lprocfs_oh_tally_log2(&filter->fo_filter_stats.hist[BRW_R_PAGES + wr],
330                               nr_pages);
331         lprocfs_oh_tally_log2(&fed->fed_brw_stats.hist[BRW_R_PAGES + wr],
332                               nr_pages);
333
334         while (nr_pages-- > 0) {
335                 if (last_page && (*pages)->index != (last_page->index + 1))
336                         discont_pages++;
337                 last_page = *pages;
338                 pages++;
339                 for (i = 0; i < blocks_per_page; i++) {
340                         if (last_block && *blocks != (*last_block + 1))
341                                 discont_blocks++;
342                         last_block = blocks++;
343                 }
344         }
345
346         lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_R_DISCONT_PAGES +wr],
347                          discont_pages);
348         lprocfs_oh_tally(&fed->fed_brw_stats.hist[BRW_R_DISCONT_PAGES + wr],
349                          discont_pages);
350         lprocfs_oh_tally(&filter->fo_filter_stats.hist[BRW_R_DISCONT_BLOCKS+wr],
351                          discont_blocks);
352         lprocfs_oh_tally(&fed->fed_brw_stats.hist[BRW_R_DISCONT_BLOCKS + wr],
353                          discont_blocks);
354 }
355
356 #define pct(a,b) (b ? a * 100 / b : 0)
357
358 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
359         struct obd_histogram *read, struct obd_histogram *write, int log2)
360 {
361         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
362         int i;
363
364         seq_printf(seq, "\n%26s read      |     write\n", " ");
365         seq_printf(seq, "%-22s %-5s %% cum %% |  %-5s %% cum %%\n", 
366                    name, units, units);
367
368         read_tot = lprocfs_oh_sum(read);
369         write_tot = lprocfs_oh_sum(write);
370         for (i = 0; i < OBD_HIST_MAX; i++) {
371                 r = read->oh_buckets[i];
372                 w = write->oh_buckets[i];
373                 read_cum += r;
374                 write_cum += w;
375                 if (read_cum == 0 && write_cum == 0)
376                         continue;
377
378                 if (!log2) 
379                         seq_printf(seq, "%u", i);
380                 else if (i < 10)
381                         seq_printf(seq, "%u", 1<<i);
382                 else if (i < 20)
383                         seq_printf(seq, "%uK", 1<<(i-10));
384                 else
385                         seq_printf(seq, "%uM", 1<<(i-20));
386
387                 seq_printf(seq, ":\t\t%10lu %3lu %3lu   | %4lu %3lu %3lu\n",
388                            r, pct(r, read_tot), pct(read_cum, read_tot), 
389                            w, pct(w, write_tot), pct(write_cum, write_tot));
390
391                 if (read_cum == read_tot && write_cum == write_tot)
392                         break;
393         }
394 }
395
396 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
397 {
398         struct timeval now;
399
400         /* this sampling races with updates */
401         do_gettimeofday(&now);
402         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
403                    now.tv_sec, now.tv_usec);
404
405         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
406                           &brw_stats->hist[BRW_R_PAGES],
407                           &brw_stats->hist[BRW_W_PAGES], 1);
408
409         display_brw_stats(seq, "discontiguous pages", "rpcs",
410                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
411                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
412
413         display_brw_stats(seq, "discontiguous blocks", "rpcs",
414                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
415                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
416
417         display_brw_stats(seq, "disk fragmented I/Os", "ios",
418                           &brw_stats->hist[BRW_R_DIO_FRAGS],
419                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
420
421 #if (LINUX_VERSION_CODE > KERNEL_VERSION(2,5,0))
422         display_brw_stats(seq, "disk I/Os in flight", "ios",
423                           &brw_stats->hist[BRW_R_RPC_HIST],
424                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
425
426         {
427                 char title[24];
428                 sprintf(title, "I/O time (1/%ds)", HZ);
429                 display_brw_stats(seq, title, "ios",
430                                   &brw_stats->hist[BRW_R_IO_TIME],
431                                   &brw_stats->hist[BRW_W_IO_TIME], 1);
432         }
433
434         display_brw_stats(seq, "disk I/O size", "ios",
435                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
436                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
437 #endif
438 }
439
440 static int filter_brw_stats_seq_show(struct seq_file *seq, void *v)
441 {
442         struct obd_device *dev = seq->private;
443         struct filter_obd *filter = &dev->u.filter;
444
445         brw_stats_show(seq, &filter->fo_filter_stats);
446
447         return 0;
448 }
449
450 static ssize_t filter_brw_stats_seq_write(struct file *file, const char *buf,
451                                        size_t len, loff_t *off)
452 {
453         struct seq_file *seq = file->private_data;
454         struct obd_device *dev = seq->private;
455         struct filter_obd *filter = &dev->u.filter;
456         int i;
457
458         for (i = 0; i < BRW_LAST; i++)
459                 lprocfs_oh_clear(&filter->fo_filter_stats.hist[i]);
460
461         return len;
462 }
463
464 LPROC_SEQ_FOPS(filter_brw_stats);
465
466 int lproc_filter_attach_seqstat(struct obd_device *dev)
467 {
468         return lprocfs_obd_seq_create(dev, "brw_stats", 0444,
469                                       &filter_brw_stats_fops, dev);
470 }
471
472 static int filter_per_export_stats_seq_show(struct seq_file *seq, void *v)
473 {
474         struct filter_export_data *fed = seq->private;
475
476         brw_stats_show(seq, &fed->fed_brw_stats);
477
478         return 0;
479 }
480
481 static ssize_t filter_per_export_stats_seq_write(struct file *file,
482                                        const char *buf, size_t len, loff_t *off)
483 {
484         struct seq_file *seq = file->private_data;
485         struct filter_export_data *fed = seq->private;
486         int i;
487
488         for (i = 0; i < BRW_LAST; i++)
489                 lprocfs_oh_clear(&fed->fed_brw_stats.hist[i]);
490
491         return len;
492 }
493
494 LPROC_SEQ_FOPS(filter_per_export_stats);
495
496 LPROCFS_INIT_VARS(filter, lprocfs_module_vars, lprocfs_obd_vars)
497 #endif /* LPROCFS */