Whamcloud - gitweb
LU-1346 libcfs: replace libcfs wrappers with kernel API
[fs/lustre-release.git] / lustre / osd-ldiskfs / osd_lproc.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/osd/osd_lproc.c
37  *
38  * Author: Mikhail Pershin <tappro@sun.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_CLASS
42
43 #include <lprocfs_status.h>
44 #include <lu_time.h>
45
46 #include <lustre/lustre_idl.h>
47
48 #include "osd_internal.h"
49
50 #ifdef LPROCFS
51
52 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf)
53 {
54         struct brw_stats *s = &osd->od_brw_stats;
55         unsigned long    *last_block = NULL;
56         struct page     **pages = iobuf->dr_pages;
57         struct page      *last_page = NULL;
58         unsigned long     discont_pages = 0;
59         unsigned long     discont_blocks = 0;
60         unsigned long    *blocks = iobuf->dr_blocks;
61         int               i, nr_pages = iobuf->dr_npages;
62         int               blocks_per_page;
63         int               rw = iobuf->dr_rw;
64
65         if (unlikely(nr_pages == 0))
66                 return;
67
68         blocks_per_page = CFS_PAGE_SIZE >> osd_sb(osd)->s_blocksize_bits;
69
70         lprocfs_oh_tally_log2(&s->hist[BRW_R_PAGES+rw], nr_pages);
71
72         while (nr_pages-- > 0) {
73                 if (last_page && (*pages)->index != (last_page->index + 1))
74                         discont_pages++;
75                 last_page = *pages;
76                 pages++;
77                 for (i = 0; i < blocks_per_page; i++) {
78                         if (last_block && *blocks != (*last_block + 1))
79                                 discont_blocks++;
80                         last_block = blocks++;
81                 }
82         }
83
84         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_PAGES+rw], discont_pages);
85         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_BLOCKS+rw], discont_blocks);
86 }
87
88 #define pct(a, b) (b ? a * 100 / b : 0)
89
90 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
91         struct obd_histogram *read, struct obd_histogram *write, int scale)
92 {
93         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
94         int i;
95
96         seq_printf(seq, "\n%26s read      |     write\n", " ");
97         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
98                    name, units, units);
99
100         read_tot = lprocfs_oh_sum(read);
101         write_tot = lprocfs_oh_sum(write);
102         for (i = 0; i < OBD_HIST_MAX; i++) {
103                 r = read->oh_buckets[i];
104                 w = write->oh_buckets[i];
105                 read_cum += r;
106                 write_cum += w;
107                 if (read_cum == 0 && write_cum == 0)
108                         continue;
109
110                 if (!scale)
111                         seq_printf(seq, "%u", i);
112                 else if (i < 10)
113                         seq_printf(seq, "%u", scale << i);
114                 else if (i < 20)
115                         seq_printf(seq, "%uK", scale << (i-10));
116                 else
117                         seq_printf(seq, "%uM", scale << (i-20));
118
119                 seq_printf(seq, ":\t\t%10lu %3lu %3lu   | %4lu %3lu %3lu\n",
120                            r, pct(r, read_tot), pct(read_cum, read_tot),
121                            w, pct(w, write_tot), pct(write_cum, write_tot));
122
123                 if (read_cum == read_tot && write_cum == write_tot)
124                         break;
125         }
126 }
127
128 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
129 {
130         struct timeval now;
131
132         /* this sampling races with updates */
133         cfs_gettimeofday(&now);
134         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
135                    now.tv_sec, now.tv_usec);
136
137         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
138                           &brw_stats->hist[BRW_R_PAGES],
139                           &brw_stats->hist[BRW_W_PAGES], 1);
140
141         display_brw_stats(seq, "discontiguous pages", "rpcs",
142                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
143                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
144
145         display_brw_stats(seq, "discontiguous blocks", "rpcs",
146                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
147                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
148
149         display_brw_stats(seq, "disk fragmented I/Os", "ios",
150                           &brw_stats->hist[BRW_R_DIO_FRAGS],
151                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
152
153         display_brw_stats(seq, "disk I/Os in flight", "ios",
154                           &brw_stats->hist[BRW_R_RPC_HIST],
155                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
156
157         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
158                           &brw_stats->hist[BRW_R_IO_TIME],
159                           &brw_stats->hist[BRW_W_IO_TIME], 1000 / CFS_HZ);
160
161         display_brw_stats(seq, "disk I/O size", "ios",
162                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
163                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
164 }
165
166 #undef pct
167
168 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
169 {
170         struct osd_device *osd = seq->private;
171
172         brw_stats_show(seq, &osd->od_brw_stats);
173
174         return 0;
175 }
176
177 static ssize_t osd_brw_stats_seq_write(struct file *file, const char *buf,
178                                        size_t len, loff_t *off)
179 {
180         struct seq_file *seq = file->private_data;
181         struct osd_device *osd = seq->private;
182         int i;
183
184         for (i = 0; i < BRW_LAST; i++)
185                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
186
187         return len;
188 }
189
190 LPROC_SEQ_FOPS(osd_brw_stats);
191
192 static int osd_stats_init(struct osd_device *osd)
193 {
194         int i, result;
195         ENTRY;
196
197         for (i = 0; i < BRW_LAST; i++)
198                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
199
200         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
201         if (osd->od_stats != NULL) {
202                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
203                                                 osd->od_stats);
204                 if (result)
205                         GOTO(out, result);
206
207                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
208                                      LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
209                                      "get_page", "usec");
210                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
211                                      LPROCFS_CNTR_AVGMINMAX,
212                                      "get_page_failures", "num");
213                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
214                                      LPROCFS_CNTR_AVGMINMAX,
215                                      "cache_access", "pages");
216                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
217                                      LPROCFS_CNTR_AVGMINMAX,
218                                      "cache_hit", "pages");
219                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
220                                      LPROCFS_CNTR_AVGMINMAX,
221                                      "cache_miss", "pages");
222 #if OSD_THANDLE_STATS
223                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
224                                      LPROCFS_CNTR_AVGMINMAX,
225                                      "thandle starting", "usec");
226                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
227                                      LPROCFS_CNTR_AVGMINMAX,
228                                      "thandle open", "usec");
229                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
230                                      LPROCFS_CNTR_AVGMINMAX,
231                                      "thandle closing", "usec");
232 #endif
233                 lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
234                                    0444, &osd_brw_stats_fops, osd);
235         } else
236                 result = -ENOMEM;
237
238 out:
239         RETURN(result);
240 }
241
242 static const char *osd_counter_names[] = {
243 #if OSD_THANDLE_STATS
244         [LPROC_OSD_THANDLE_STARTING] = "thandle starting",
245         [LPROC_OSD_THANDLE_OPEN]     = "thandle open",
246         [LPROC_OSD_THANDLE_CLOSING]  = "thandle closing"
247 #endif
248 };
249
250 int osd_procfs_init(struct osd_device *osd, const char *name)
251 {
252         struct lprocfs_static_vars lvars;
253         struct obd_type     *type;
254         int                  rc;
255         ENTRY;
256
257         /* at the moment there is no linkage between lu_type
258          * and obd_type, so we lookup obd_type this way */
259         type = class_search_type(LUSTRE_OSD_LDISKFS_NAME);
260
261         LASSERT(name != NULL);
262         LASSERT(type != NULL);
263
264         /* Find the type procroot and add the proc entry for this device */
265         lprocfs_osd_init_vars(&lvars);
266         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
267                                               lvars.obd_vars, &osd->od_dt_dev);
268         if (IS_ERR(osd->od_proc_entry)) {
269                 rc = PTR_ERR(osd->od_proc_entry);
270                 CERROR("Error %d setting up lprocfs for %s\n",
271                        rc, name);
272                 osd->od_proc_entry = NULL;
273                 GOTO(out, rc);
274         }
275
276         rc = lu_time_init(&osd->od_stats,
277                           osd->od_proc_entry,
278                           osd_counter_names, ARRAY_SIZE(osd_counter_names));
279
280         rc = osd_stats_init(osd);
281
282         EXIT;
283 out:
284         if (rc)
285                osd_procfs_fini(osd);
286         return rc;
287 }
288
289 int osd_procfs_fini(struct osd_device *osd)
290 {
291         if (osd->od_stats)
292                 lu_time_fini(&osd->od_stats);
293
294         if (osd->od_proc_entry) {
295                  lprocfs_remove(&osd->od_proc_entry);
296                  osd->od_proc_entry = NULL;
297         }
298         RETURN(0);
299 }
300
301 void osd_lprocfs_time_start(const struct lu_env *env)
302 {
303         lu_lprocfs_time_start(env);
304 }
305
306 void osd_lprocfs_time_end(const struct lu_env *env, struct osd_device *osd,
307                           int idx)
308 {
309         lu_lprocfs_time_end(env, osd->od_stats, idx);
310 }
311
312
313
314 static int lprocfs_osd_rd_fstype(char *page, char **start, off_t off, int count,
315                                  int *eof, void *data)
316 {
317         struct osd_device *osd = osd_dt_dev(data);
318
319         LASSERT(osd != NULL);
320         return snprintf(page, count, "ldiskfs\n");
321 }
322
323 static int lprocfs_osd_rd_mntdev(char *page, char **start, off_t off, int count,
324                                  int *eof, void *data)
325 {
326         struct osd_device *osd = osd_dt_dev(data);
327
328         LASSERT(osd != NULL);
329         if (unlikely(osd->od_mnt == NULL))
330                 return -EINPROGRESS;
331
332         LASSERT(mnt_get_devname(osd->od_mnt));
333         *eof = 1;
334
335         return snprintf(page, count, "%s\n",
336                         mnt_get_devname(osd->od_mnt));
337 }
338
339 static int lprocfs_osd_rd_cache(char *page, char **start, off_t off,
340                                 int count, int *eof, void *data)
341 {
342         struct osd_device *osd = osd_dt_dev(data);
343
344         LASSERT(osd != NULL);
345         if (unlikely(osd->od_mnt == NULL))
346                 return -EINPROGRESS;
347
348         return snprintf(page, count, "%u\n", osd->od_read_cache);
349 }
350
351 static int lprocfs_osd_wr_cache(struct file *file, const char *buffer,
352                                 unsigned long count, void *data)
353 {
354         struct osd_device       *osd = osd_dt_dev(data);
355         int                      val, rc;
356
357         LASSERT(osd != NULL);
358         if (unlikely(osd->od_mnt == NULL))
359                 return -EINPROGRESS;
360
361         rc = lprocfs_write_helper(buffer, count, &val);
362         if (rc)
363                 return rc;
364
365         osd->od_read_cache = !!val;
366         return count;
367 }
368
369 static int lprocfs_osd_rd_wcache(char *page, char **start, off_t off,
370                                  int count, int *eof, void *data)
371 {
372         struct osd_device *osd = osd_dt_dev(data);
373
374         LASSERT(osd != NULL);
375         if (unlikely(osd->od_mnt == NULL))
376                 return -EINPROGRESS;
377
378         return snprintf(page, count, "%u\n", osd->od_writethrough_cache);
379 }
380
381 static int lprocfs_osd_wr_wcache(struct file *file, const char *buffer,
382                                  unsigned long count, void *data)
383 {
384         struct osd_device       *osd = osd_dt_dev(data);
385         int                      val, rc;
386
387         LASSERT(osd != NULL);
388         if (unlikely(osd->od_mnt == NULL))
389                 return -EINPROGRESS;
390
391         rc = lprocfs_write_helper(buffer, count, &val);
392         if (rc)
393                 return rc;
394
395         osd->od_writethrough_cache = !!val;
396         return count;
397 }
398
399 static int lprocfs_osd_wr_force_sync(struct file *file, const char *buffer,
400                                      unsigned long count, void *data)
401 {
402         struct osd_device       *osd = osd_dt_dev(data);
403         struct dt_device        *dt = data;
404         struct lu_env            env;
405         int                      rc;
406
407         LASSERT(osd != NULL);
408         if (unlikely(osd->od_mnt == NULL))
409                 return -EINPROGRESS;
410
411         rc = lu_env_init(&env, LCT_LOCAL);
412         if (rc)
413                 return rc;
414         rc = dt_sync(&env, dt);
415         lu_env_fini(&env);
416
417         return rc == 0 ? count : rc;
418 }
419
420 #ifdef HAVE_LDISKFS_PDO
421 static int lprocfs_osd_rd_pdo(char *page, char **start, off_t off, int count,
422                               int *eof, void *data)
423 {
424         *eof = 1;
425
426         return snprintf(page, count, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
427 }
428
429 static int lprocfs_osd_wr_pdo(struct file *file, const char *buffer,
430                               unsigned long count, void *data)
431 {
432         int     pdo;
433         int     rc;
434
435         rc = lprocfs_write_helper(buffer, count, &pdo);
436         if (rc != 0)
437                 return rc;
438
439         ldiskfs_pdo = !!pdo;
440
441         return count;
442 }
443 #endif
444
445 static int lprocfs_osd_rd_auto_scrub(char *page, char **start, off_t off,
446                                      int count, int *eof, void *data)
447 {
448         struct osd_device *dev = osd_dt_dev(data);
449
450         LASSERT(dev != NULL);
451         if (unlikely(dev->od_mnt == NULL))
452                 return -EINPROGRESS;
453
454         *eof = 1;
455         return snprintf(page, count, "%d\n", !dev->od_noscrub);
456 }
457
458 static int lprocfs_osd_wr_auto_scrub(struct file *file, const char *buffer,
459                                      unsigned long count, void *data)
460 {
461         struct osd_device *dev = osd_dt_dev(data);
462         int val, rc;
463
464         LASSERT(dev != NULL);
465         if (unlikely(dev->od_mnt == NULL))
466                 return -EINPROGRESS;
467
468         rc = lprocfs_write_helper(buffer, count, &val);
469         if (rc)
470                 return rc;
471
472         dev->od_noscrub = !val;
473         return count;
474 }
475
476 static int lprocfs_osd_rd_oi_scrub(char *page, char **start, off_t off,
477                                    int count, int *eof, void *data)
478 {
479         struct osd_device *dev = osd_dt_dev(data);
480
481         LASSERT(dev != NULL);
482         if (unlikely(dev->od_mnt == NULL))
483                 return -EINPROGRESS;
484
485         *eof = 1;
486         return osd_scrub_dump(dev, page, count);
487 }
488
489 int lprocfs_osd_rd_readcache(char *page, char **start, off_t off, int count,
490                              int *eof, void *data)
491 {
492         struct osd_device       *osd = osd_dt_dev(data);
493         int                      rc;
494
495         LASSERT(osd != NULL);
496         if (unlikely(osd->od_mnt == NULL))
497                 return -EINPROGRESS;
498
499         rc = snprintf(page, count, LPU64"\n", osd->od_readcache_max_filesize);
500         return rc;
501 }
502
503 int lprocfs_osd_wr_readcache(struct file *file, const char *buffer,
504                              unsigned long count, void *data)
505 {
506         struct osd_device       *osd = osd_dt_dev(data);
507         __u64                    val;
508         int                      rc;
509
510         LASSERT(osd != NULL);
511         if (unlikely(osd->od_mnt == NULL))
512                 return -EINPROGRESS;
513
514         rc = lprocfs_write_u64_helper(buffer, count, &val);
515         if (rc)
516                 return rc;
517
518         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
519                                          OSD_MAX_CACHE_SIZE : val;
520         return count;
521 }
522
523 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
524         { "blocksize",       lprocfs_osd_rd_blksize,     0, 0 },
525         { "kbytestotal",     lprocfs_osd_rd_kbytestotal, 0, 0 },
526         { "kbytesfree",      lprocfs_osd_rd_kbytesfree,  0, 0 },
527         { "kbytesavail",     lprocfs_osd_rd_kbytesavail, 0, 0 },
528         { "filestotal",      lprocfs_osd_rd_filestotal,  0, 0 },
529         { "filesfree",       lprocfs_osd_rd_filesfree,   0, 0 },
530         { "fstype",          lprocfs_osd_rd_fstype,      0, 0 },
531         { "mntdev",          lprocfs_osd_rd_mntdev,      0, 0 },
532         { "force_sync",      0, lprocfs_osd_wr_force_sync     },
533 #ifdef HAVE_LDISKFS_PDO
534         { "pdo",             lprocfs_osd_rd_pdo, lprocfs_osd_wr_pdo, 0 },
535 #endif
536         { "auto_scrub",      lprocfs_osd_rd_auto_scrub,
537                              lprocfs_osd_wr_auto_scrub,  0 },
538         { "oi_scrub",        lprocfs_osd_rd_oi_scrub,    0, 0 },
539         { "force_sync",         0, lprocfs_osd_wr_force_sync },
540         { "read_cache_enable",  lprocfs_osd_rd_cache, lprocfs_osd_wr_cache, 0 },
541         { "writethrough_cache_enable",  lprocfs_osd_rd_wcache,
542                                         lprocfs_osd_wr_wcache, 0 },
543         { "readcache_max_filesize",     lprocfs_osd_rd_readcache,
544                                         lprocfs_osd_wr_readcache, 0 },
545         { 0 }
546 };
547
548 struct lprocfs_vars lprocfs_osd_module_vars[] = {
549         { "num_refs",        lprocfs_rd_numrefs,     0, 0 },
550         { 0 }
551 };
552
553 void lprocfs_osd_init_vars(struct lprocfs_static_vars *lvars)
554 {
555         lvars->module_vars = lprocfs_osd_module_vars;
556         lvars->obd_vars = lprocfs_osd_obd_vars;
557 }
558 #endif