Whamcloud - gitweb
e3539a82390fa71b63437428c702adc3b3b1dd01
[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, 2012, 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 = CFS_PAGE_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         cfs_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 / CFS_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                 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 #ifdef HAVE_LDISKFS_PDO
392 static int lprocfs_osd_rd_pdo(char *page, char **start, off_t off, int count,
393                               int *eof, void *data)
394 {
395         *eof = 1;
396
397         return snprintf(page, count, "%s\n", ldiskfs_pdo ? "ON" : "OFF");
398 }
399
400 static int lprocfs_osd_wr_pdo(struct file *file, const char *buffer,
401                               unsigned long count, void *data)
402 {
403         int     pdo;
404         int     rc;
405
406         rc = lprocfs_write_helper(buffer, count, &pdo);
407         if (rc != 0)
408                 return rc;
409
410         ldiskfs_pdo = !!pdo;
411
412         return count;
413 }
414 #endif
415
416 static int lprocfs_osd_rd_auto_scrub(char *page, char **start, off_t off,
417                                      int count, int *eof, void *data)
418 {
419         struct osd_device *dev = osd_dt_dev(data);
420
421         LASSERT(dev != NULL);
422         if (unlikely(dev->od_mnt == NULL))
423                 return -EINPROGRESS;
424
425         *eof = 1;
426         return snprintf(page, count, "%d\n", !dev->od_noscrub);
427 }
428
429 static int lprocfs_osd_wr_auto_scrub(struct file *file, const char *buffer,
430                                      unsigned long count, void *data)
431 {
432         struct osd_device *dev = osd_dt_dev(data);
433         int val, rc;
434
435         LASSERT(dev != NULL);
436         if (unlikely(dev->od_mnt == NULL))
437                 return -EINPROGRESS;
438
439         rc = lprocfs_write_helper(buffer, count, &val);
440         if (rc)
441                 return rc;
442
443         dev->od_noscrub = !val;
444         return count;
445 }
446
447 static int lprocfs_osd_rd_oi_scrub(char *page, char **start, off_t off,
448                                    int count, int *eof, void *data)
449 {
450         struct osd_device *dev = osd_dt_dev(data);
451
452         LASSERT(dev != NULL);
453         if (unlikely(dev->od_mnt == NULL))
454                 return -EINPROGRESS;
455
456         *eof = 1;
457         return osd_scrub_dump(dev, page, count);
458 }
459
460 int lprocfs_osd_rd_readcache(char *page, char **start, off_t off, int count,
461                              int *eof, void *data)
462 {
463         struct osd_device       *osd = osd_dt_dev(data);
464         int                      rc;
465
466         LASSERT(osd != NULL);
467         if (unlikely(osd->od_mnt == NULL))
468                 return -EINPROGRESS;
469
470         rc = snprintf(page, count, LPU64"\n", osd->od_readcache_max_filesize);
471         return rc;
472 }
473
474 int lprocfs_osd_wr_readcache(struct file *file, const char *buffer,
475                              unsigned long count, void *data)
476 {
477         struct osd_device       *osd = osd_dt_dev(data);
478         __u64                    val;
479         int                      rc;
480
481         LASSERT(osd != NULL);
482         if (unlikely(osd->od_mnt == NULL))
483                 return -EINPROGRESS;
484
485         rc = lprocfs_write_u64_helper(buffer, count, &val);
486         if (rc)
487                 return rc;
488
489         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
490                                          OSD_MAX_CACHE_SIZE : val;
491         return count;
492 }
493
494 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
495         { "blocksize",       lprocfs_osd_rd_blksize,     0, 0 },
496         { "kbytestotal",     lprocfs_osd_rd_kbytestotal, 0, 0 },
497         { "kbytesfree",      lprocfs_osd_rd_kbytesfree,  0, 0 },
498         { "kbytesavail",     lprocfs_osd_rd_kbytesavail, 0, 0 },
499         { "filestotal",      lprocfs_osd_rd_filestotal,  0, 0 },
500         { "filesfree",       lprocfs_osd_rd_filesfree,   0, 0 },
501         { "fstype",          lprocfs_osd_rd_fstype,      0, 0 },
502         { "mntdev",          lprocfs_osd_rd_mntdev,      0, 0 },
503         { "force_sync",      0, lprocfs_osd_wr_force_sync     },
504 #ifdef HAVE_LDISKFS_PDO
505         { "pdo",             lprocfs_osd_rd_pdo, lprocfs_osd_wr_pdo, 0 },
506 #endif
507         { "auto_scrub",      lprocfs_osd_rd_auto_scrub,
508                              lprocfs_osd_wr_auto_scrub,  0 },
509         { "oi_scrub",        lprocfs_osd_rd_oi_scrub,    0, 0 },
510         { "force_sync",         0, lprocfs_osd_wr_force_sync },
511         { "read_cache_enable",  lprocfs_osd_rd_cache, lprocfs_osd_wr_cache, 0 },
512         { "writethrough_cache_enable",  lprocfs_osd_rd_wcache,
513                                         lprocfs_osd_wr_wcache, 0 },
514         { "readcache_max_filesize",     lprocfs_osd_rd_readcache,
515                                         lprocfs_osd_wr_readcache, 0 },
516         { 0 }
517 };
518
519 struct lprocfs_vars lprocfs_osd_module_vars[] = {
520         { "num_refs",        lprocfs_rd_numrefs,     0, 0 },
521         { 0 }
522 };
523
524 void lprocfs_osd_init_vars(struct lprocfs_static_vars *lvars)
525 {
526         lvars->module_vars = lprocfs_osd_module_vars;
527         lvars->obd_vars = lprocfs_osd_obd_vars;
528 }
529 #endif