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