Whamcloud - gitweb
LU-3906 tests: fix space estimation codes in run_compilebench()
[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, 2013, Intel Corporation.
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 <lustre/lustre_idl.h>
45
46 #include "osd_internal.h"
47
48 #ifdef LPROCFS
49
50 void osd_brw_stats_update(struct osd_device *osd, struct osd_iobuf *iobuf)
51 {
52         struct brw_stats *s = &osd->od_brw_stats;
53         unsigned long    *last_block = NULL;
54         struct page     **pages = iobuf->dr_pages;
55         struct page      *last_page = NULL;
56         unsigned long     discont_pages = 0;
57         unsigned long     discont_blocks = 0;
58         unsigned long    *blocks = iobuf->dr_blocks;
59         int               i, nr_pages = iobuf->dr_npages;
60         int               blocks_per_page;
61         int               rw = iobuf->dr_rw;
62
63         if (unlikely(nr_pages == 0))
64                 return;
65
66         blocks_per_page = PAGE_CACHE_SIZE >> osd_sb(osd)->s_blocksize_bits;
67
68         lprocfs_oh_tally_log2(&s->hist[BRW_R_PAGES+rw], nr_pages);
69
70         while (nr_pages-- > 0) {
71                 if (last_page && (*pages)->index != (last_page->index + 1))
72                         discont_pages++;
73                 last_page = *pages;
74                 pages++;
75                 for (i = 0; i < blocks_per_page; i++) {
76                         if (last_block && *blocks != (*last_block + 1))
77                                 discont_blocks++;
78                         last_block = blocks++;
79                 }
80         }
81
82         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_PAGES+rw], discont_pages);
83         lprocfs_oh_tally(&s->hist[BRW_R_DISCONT_BLOCKS+rw], discont_blocks);
84 }
85
86 #define pct(a, b) (b ? a * 100 / b : 0)
87
88 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
89         struct obd_histogram *read, struct obd_histogram *write, int scale)
90 {
91         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
92         int i;
93
94         seq_printf(seq, "\n%26s read      |     write\n", " ");
95         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
96                    name, units, units);
97
98         read_tot = lprocfs_oh_sum(read);
99         write_tot = lprocfs_oh_sum(write);
100         for (i = 0; i < OBD_HIST_MAX; i++) {
101                 r = read->oh_buckets[i];
102                 w = write->oh_buckets[i];
103                 read_cum += r;
104                 write_cum += w;
105                 if (read_cum == 0 && write_cum == 0)
106                         continue;
107
108                 if (!scale)
109                         seq_printf(seq, "%u", i);
110                 else if (i < 10)
111                         seq_printf(seq, "%u", scale << i);
112                 else if (i < 20)
113                         seq_printf(seq, "%uK", scale << (i-10));
114                 else
115                         seq_printf(seq, "%uM", scale << (i-20));
116
117                 seq_printf(seq, ":\t\t%10lu %3lu %3lu   | %4lu %3lu %3lu\n",
118                            r, pct(r, read_tot), pct(read_cum, read_tot),
119                            w, pct(w, write_tot), pct(write_cum, write_tot));
120
121                 if (read_cum == read_tot && write_cum == write_tot)
122                         break;
123         }
124 }
125
126 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
127 {
128         struct timeval now;
129
130         /* this sampling races with updates */
131         do_gettimeofday(&now);
132         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
133                    now.tv_sec, now.tv_usec);
134
135         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
136                           &brw_stats->hist[BRW_R_PAGES],
137                           &brw_stats->hist[BRW_W_PAGES], 1);
138
139         display_brw_stats(seq, "discontiguous pages", "rpcs",
140                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
141                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
142
143         display_brw_stats(seq, "discontiguous blocks", "rpcs",
144                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
145                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
146
147         display_brw_stats(seq, "disk fragmented I/Os", "ios",
148                           &brw_stats->hist[BRW_R_DIO_FRAGS],
149                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
150
151         display_brw_stats(seq, "disk I/Os in flight", "ios",
152                           &brw_stats->hist[BRW_R_RPC_HIST],
153                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
154
155         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
156                           &brw_stats->hist[BRW_R_IO_TIME],
157                           &brw_stats->hist[BRW_W_IO_TIME], 1000 / HZ);
158
159         display_brw_stats(seq, "disk I/O size", "ios",
160                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
161                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
162 }
163
164 #undef pct
165
166 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
167 {
168         struct osd_device *osd = seq->private;
169
170         brw_stats_show(seq, &osd->od_brw_stats);
171
172         return 0;
173 }
174
175 static ssize_t osd_brw_stats_seq_write(struct file *file, const char *buf,
176                                        size_t len, loff_t *off)
177 {
178         struct seq_file *seq = file->private_data;
179         struct osd_device *osd = seq->private;
180         int i;
181
182         for (i = 0; i < BRW_LAST; i++)
183                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
184
185         return len;
186 }
187
188 LPROC_SEQ_FOPS(osd_brw_stats);
189
190 static int osd_stats_init(struct osd_device *osd)
191 {
192         int i, result;
193         ENTRY;
194
195         for (i = 0; i < BRW_LAST; i++)
196                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
197
198         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
199         if (osd->od_stats != NULL) {
200                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
201                                                 osd->od_stats);
202                 if (result)
203                         GOTO(out, result);
204
205                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
206                                      LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
207                                      "get_page", "usec");
208                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
209                                      LPROCFS_CNTR_AVGMINMAX,
210                                      "get_page_failures", "num");
211                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
212                                      LPROCFS_CNTR_AVGMINMAX,
213                                      "cache_access", "pages");
214                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
215                                      LPROCFS_CNTR_AVGMINMAX,
216                                      "cache_hit", "pages");
217                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
218                                      LPROCFS_CNTR_AVGMINMAX,
219                                      "cache_miss", "pages");
220 #if OSD_THANDLE_STATS
221                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
222                                      LPROCFS_CNTR_AVGMINMAX,
223                                      "thandle starting", "usec");
224                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
225                                      LPROCFS_CNTR_AVGMINMAX,
226                                      "thandle open", "usec");
227                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
228                                      LPROCFS_CNTR_AVGMINMAX,
229                                      "thandle closing", "usec");
230 #endif
231                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
232                                             0644, &osd_brw_stats_fops, osd);
233         } else
234                 result = -ENOMEM;
235
236 out:
237         RETURN(result);
238 }
239
240 int osd_procfs_init(struct osd_device *osd, const char *name)
241 {
242         struct lprocfs_static_vars lvars;
243         struct obd_type     *type;
244         int                  rc;
245         ENTRY;
246
247         /* at the moment there is no linkage between lu_type
248          * and obd_type, so we lookup obd_type this way */
249         type = class_search_type(LUSTRE_OSD_LDISKFS_NAME);
250
251         LASSERT(name != NULL);
252         LASSERT(type != NULL);
253
254         /* Find the type procroot and add the proc entry for this device */
255         lprocfs_osd_init_vars(&lvars);
256         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
257                                               lvars.obd_vars, &osd->od_dt_dev);
258         if (IS_ERR(osd->od_proc_entry)) {
259                 rc = PTR_ERR(osd->od_proc_entry);
260                 CERROR("Error %d setting up lprocfs for %s\n",
261                        rc, name);
262                 osd->od_proc_entry = NULL;
263                 GOTO(out, rc);
264         }
265
266         rc = osd_stats_init(osd);
267
268         EXIT;
269 out:
270         if (rc)
271                osd_procfs_fini(osd);
272         return rc;
273 }
274
275 int osd_procfs_fini(struct osd_device *osd)
276 {
277         if (osd->od_stats)
278                 lprocfs_free_stats(&osd->od_stats);
279
280         if (osd->od_proc_entry) {
281                  lprocfs_remove(&osd->od_proc_entry);
282                  osd->od_proc_entry = NULL;
283         }
284         RETURN(0);
285 }
286
287 static int lprocfs_osd_rd_fstype(char *page, char **start, off_t off, int count,
288                                  int *eof, void *data)
289 {
290         struct osd_device *osd = osd_dt_dev(data);
291
292         LASSERT(osd != NULL);
293         return snprintf(page, count, "ldiskfs\n");
294 }
295
296 static int lprocfs_osd_rd_mntdev(char *page, char **start, off_t off, int count,
297                                  int *eof, void *data)
298 {
299         struct osd_device *osd = osd_dt_dev(data);
300
301         LASSERT(osd != NULL);
302         if (unlikely(osd->od_mnt == NULL))
303                 return -EINPROGRESS;
304
305         *eof = 1;
306
307         return snprintf(page, count, "%s\n", osd->od_mntdev);
308 }
309
310 static int lprocfs_osd_rd_cache(char *page, char **start, off_t off,
311                                 int count, int *eof, void *data)
312 {
313         struct osd_device *osd = osd_dt_dev(data);
314
315         LASSERT(osd != NULL);
316         if (unlikely(osd->od_mnt == NULL))
317                 return -EINPROGRESS;
318
319         return snprintf(page, count, "%u\n", osd->od_read_cache);
320 }
321
322 static int lprocfs_osd_wr_cache(struct file *file, const char *buffer,
323                                 unsigned long count, void *data)
324 {
325         struct osd_device       *osd = osd_dt_dev(data);
326         int                      val, rc;
327
328         LASSERT(osd != NULL);
329         if (unlikely(osd->od_mnt == NULL))
330                 return -EINPROGRESS;
331
332         rc = lprocfs_write_helper(buffer, count, &val);
333         if (rc)
334                 return rc;
335
336         osd->od_read_cache = !!val;
337         return count;
338 }
339
340 static int lprocfs_osd_rd_wcache(char *page, char **start, off_t off,
341                                  int count, int *eof, void *data)
342 {
343         struct osd_device *osd = osd_dt_dev(data);
344
345         LASSERT(osd != NULL);
346         if (unlikely(osd->od_mnt == NULL))
347                 return -EINPROGRESS;
348
349         return snprintf(page, count, "%u\n", osd->od_writethrough_cache);
350 }
351
352 static int lprocfs_osd_wr_wcache(struct file *file, const char *buffer,
353                                  unsigned long count, void *data)
354 {
355         struct osd_device       *osd = osd_dt_dev(data);
356         int                      val, rc;
357
358         LASSERT(osd != NULL);
359         if (unlikely(osd->od_mnt == NULL))
360                 return -EINPROGRESS;
361
362         rc = lprocfs_write_helper(buffer, count, &val);
363         if (rc)
364                 return rc;
365
366         osd->od_writethrough_cache = !!val;
367         return count;
368 }
369
370 static int lprocfs_osd_wr_force_sync(struct file *file, const char *buffer,
371                                      unsigned long count, void *data)
372 {
373         struct osd_device       *osd = osd_dt_dev(data);
374         struct dt_device        *dt = data;
375         struct lu_env            env;
376         int                      rc;
377
378         LASSERT(osd != NULL);
379         if (unlikely(osd->od_mnt == NULL))
380                 return -EINPROGRESS;
381
382         rc = lu_env_init(&env, LCT_LOCAL);
383         if (rc)
384                 return rc;
385         rc = dt_sync(&env, dt);
386         lu_env_fini(&env);
387
388         return rc == 0 ? count : rc;
389 }
390
391 static int lprocfs_osd_rd_pdo(char *page, char **start, off_t off, int count,
392                               int *eof, void *data)
393 {
394         *eof = 1;
395
396         return snprintf(page, count, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
397 }
398
399 static int lprocfs_osd_wr_pdo(struct file *file, const char *buffer,
400                               unsigned long count, void *data)
401 {
402         int     pdo;
403         int     rc;
404
405         rc = lprocfs_write_helper(buffer, count, &pdo);
406         if (rc != 0)
407                 return rc;
408
409         ldiskfs_pdo = !!pdo;
410
411         return count;
412 }
413
414 static int lprocfs_osd_rd_auto_scrub(char *page, char **start, off_t off,
415                                      int count, int *eof, void *data)
416 {
417         struct osd_device *dev = osd_dt_dev(data);
418
419         LASSERT(dev != NULL);
420         if (unlikely(dev->od_mnt == NULL))
421                 return -EINPROGRESS;
422
423         *eof = 1;
424         return snprintf(page, count, "%d\n", !dev->od_noscrub);
425 }
426
427 static int lprocfs_osd_wr_auto_scrub(struct file *file, const char *buffer,
428                                      unsigned long count, void *data)
429 {
430         struct osd_device *dev = osd_dt_dev(data);
431         int val, rc;
432
433         LASSERT(dev != NULL);
434         if (unlikely(dev->od_mnt == NULL))
435                 return -EINPROGRESS;
436
437         rc = lprocfs_write_helper(buffer, count, &val);
438         if (rc)
439                 return rc;
440
441         dev->od_noscrub = !val;
442         return count;
443 }
444
445 static int lprocfs_osd_rd_track_declares_assert(char *page, char **start,
446                                                 off_t off, int count,
447                                                 int *eof, void *data)
448 {
449         *eof = 1;
450
451         return snprintf(page, count, "%d\n", ldiskfs_track_declares_assert);
452 }
453
454 static int lprocfs_osd_wr_track_declares_assert(struct file *file,
455                                                 const char *buffer,
456                                                 unsigned long count, void *data)
457 {
458         int     track_declares_assert;
459         int     rc;
460
461         rc = lprocfs_write_helper(buffer, count, &track_declares_assert);
462         if (rc != 0)
463                 return rc;
464
465         ldiskfs_track_declares_assert = !!track_declares_assert;
466
467         return count;
468 }
469
470 static int lprocfs_osd_rd_oi_scrub(char *page, char **start, off_t off,
471                                    int count, int *eof, void *data)
472 {
473         struct osd_device *dev = osd_dt_dev(data);
474
475         LASSERT(dev != NULL);
476         if (unlikely(dev->od_mnt == NULL))
477                 return -EINPROGRESS;
478
479         *eof = 1;
480         return osd_scrub_dump(dev, page, count);
481 }
482
483 int lprocfs_osd_rd_readcache(char *page, char **start, off_t off, int count,
484                              int *eof, void *data)
485 {
486         struct osd_device       *osd = osd_dt_dev(data);
487         int                      rc;
488
489         LASSERT(osd != NULL);
490         if (unlikely(osd->od_mnt == NULL))
491                 return -EINPROGRESS;
492
493         rc = snprintf(page, count, LPU64"\n", osd->od_readcache_max_filesize);
494         return rc;
495 }
496
497 int lprocfs_osd_wr_readcache(struct file *file, const char *buffer,
498                              unsigned long count, void *data)
499 {
500         struct osd_device       *osd = osd_dt_dev(data);
501         __u64                    val;
502         int                      rc;
503
504         LASSERT(osd != NULL);
505         if (unlikely(osd->od_mnt == NULL))
506                 return -EINPROGRESS;
507
508         rc = lprocfs_write_u64_helper(buffer, count, &val);
509         if (rc)
510                 return rc;
511
512         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
513                                          OSD_MAX_CACHE_SIZE : val;
514         return count;
515 }
516
517 static int lprocfs_osd_rd_lma_self_repair(char *page, char **start, off_t off,
518                                           int count, int *eof, void *data)
519 {
520         struct osd_device *dev = osd_dt_dev(data);
521
522         LASSERT(dev != NULL);
523         if (unlikely(dev->od_mnt == NULL))
524                 return -EINPROGRESS;
525
526         *eof = 1;
527         return snprintf(page, count, "%d\n", !!dev->od_lma_self_repair);
528 }
529
530 static int lprocfs_osd_wr_lma_self_repair(struct file *file, const char *buffer,
531                                           unsigned long count, void *data)
532 {
533         struct osd_device *dev = osd_dt_dev(data);
534         int                val;
535         int                rc;
536
537         LASSERT(dev != NULL);
538         if (unlikely(dev->od_mnt == NULL))
539                 return -EINPROGRESS;
540
541         rc = lprocfs_write_helper(buffer, count, &val);
542         if (rc)
543                 return rc;
544
545         dev->od_lma_self_repair = !!val;
546         return count;
547 }
548
549 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
550         { "blocksize",          lprocfs_dt_rd_blksize,  0, 0 },
551         { "kbytestotal",        lprocfs_dt_rd_kbytestotal,      0, 0 },
552         { "kbytesfree",         lprocfs_dt_rd_kbytesfree,       0, 0 },
553         { "kbytesavail",        lprocfs_dt_rd_kbytesavail,      0, 0 },
554         { "filestotal",         lprocfs_dt_rd_filestotal,       0, 0 },
555         { "filesfree",          lprocfs_dt_rd_filesfree,        0, 0 },
556         { "fstype",          lprocfs_osd_rd_fstype,      0, 0 },
557         { "mntdev",          lprocfs_osd_rd_mntdev,      0, 0 },
558         { "force_sync",      0, lprocfs_osd_wr_force_sync     },
559         { "pdo",             lprocfs_osd_rd_pdo, lprocfs_osd_wr_pdo, 0 },
560         { "auto_scrub",      lprocfs_osd_rd_auto_scrub,
561                              lprocfs_osd_wr_auto_scrub,  0 },
562         { "oi_scrub",        lprocfs_osd_rd_oi_scrub,    0, 0 },
563         { "force_sync",         0, lprocfs_osd_wr_force_sync },
564         { "read_cache_enable",  lprocfs_osd_rd_cache, lprocfs_osd_wr_cache, 0 },
565         { "writethrough_cache_enable",  lprocfs_osd_rd_wcache,
566                                         lprocfs_osd_wr_wcache, 0 },
567         { "readcache_max_filesize",     lprocfs_osd_rd_readcache,
568                                         lprocfs_osd_wr_readcache, 0 },
569         { "lma_self_repair",    lprocfs_osd_rd_lma_self_repair,
570                                 lprocfs_osd_wr_lma_self_repair, 0, 0 },
571         { 0 }
572 };
573
574 struct lprocfs_vars lprocfs_osd_module_vars[] = {
575         { "num_refs",        lprocfs_rd_numrefs,     0, 0 },
576         { "track_declares_assert",      lprocfs_osd_rd_track_declares_assert,
577                                         lprocfs_osd_wr_track_declares_assert,
578                                         0 },
579         { 0 }
580 };
581
582 void lprocfs_osd_init_vars(struct lprocfs_static_vars *lvars)
583 {
584         lvars->module_vars = lprocfs_osd_module_vars;
585         lvars->obd_vars = lprocfs_osd_obd_vars;
586 }
587 #endif