Whamcloud - gitweb
LU-7334 lprocfs: Refactored string to value helpers
[fs/lustre-release.git] / lustre / osd-zfs / 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) 2012, 2015, 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-zfs/osd_lproc.c
37  *
38  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
39  * Author: Mike Pershin <tappro@whamcloud.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_OSD
43
44 #include <obd.h>
45 #include <obd_class.h>
46 #include <lprocfs_status.h>
47 #include <lustre/lustre_idl.h>
48
49 #include "osd_internal.h"
50
51 #ifdef CONFIG_PROC_FS
52
53 #define pct(a, b) (b ? a * 100 / b : 0)
54
55 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
56                               struct obd_histogram *read,
57                               struct obd_histogram *write, int scale)
58 {
59         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
60         int i;
61
62         seq_printf(seq, "\n%26s read      |     write\n", " ");
63         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
64                    name, units, units);
65
66         read_tot = lprocfs_oh_sum(read);
67         write_tot = lprocfs_oh_sum(write);
68         for (i = 0; i < OBD_HIST_MAX; i++) {
69                 r = read->oh_buckets[i];
70                 w = write->oh_buckets[i];
71                 read_cum += r;
72                 write_cum += w;
73                 if (read_cum == 0 && write_cum == 0)
74                         continue;
75
76                 if (!scale)
77                         seq_printf(seq, "%u", i);
78                 else if (i < 10)
79                         seq_printf(seq, "%u", scale << i);
80                 else if (i < 20)
81                         seq_printf(seq, "%uK", scale << (i-10));
82                 else
83                         seq_printf(seq, "%uM", scale << (i-20));
84
85                 seq_printf(seq, ":\t\t%10lu %3lu %3lu   | %4lu %3lu %3lu\n",
86                            r, pct(r, read_tot), pct(read_cum, read_tot),
87                            w, pct(w, write_tot), pct(write_cum, write_tot));
88
89                 if (read_cum == read_tot && write_cum == write_tot)
90                         break;
91         }
92 }
93
94 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
95 {
96         struct timeval now;
97
98         /* this sampling races with updates */
99         do_gettimeofday(&now);
100         seq_printf(seq, "snapshot_time:         %lu.%lu (secs.usecs)\n",
101                    now.tv_sec, now.tv_usec);
102
103         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
104                           &brw_stats->hist[BRW_R_PAGES],
105                           &brw_stats->hist[BRW_W_PAGES], 1);
106         display_brw_stats(seq, "discontiguous pages", "rpcs",
107                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
108                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
109 #if 0
110         display_brw_stats(seq, "discontiguous blocks", "rpcs",
111                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
112                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
113
114         display_brw_stats(seq, "disk fragmented I/Os", "ios",
115                           &brw_stats->hist[BRW_R_DIO_FRAGS],
116                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
117 #endif
118         display_brw_stats(seq, "disk I/Os in flight", "ios",
119                           &brw_stats->hist[BRW_R_RPC_HIST],
120                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
121
122         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
123                           &brw_stats->hist[BRW_R_IO_TIME],
124                           &brw_stats->hist[BRW_W_IO_TIME], 1000 / HZ);
125
126         display_brw_stats(seq, "disk I/O size", "ios",
127                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
128                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
129 }
130
131 #undef pct
132
133 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
134 {
135         struct osd_device *osd = seq->private;
136
137         brw_stats_show(seq, &osd->od_brw_stats);
138
139         return 0;
140 }
141
142 static ssize_t osd_brw_stats_seq_write(struct file *file,
143                                        const char __user *buf,
144                                        size_t len, loff_t *off)
145 {
146         struct seq_file *seq = file->private_data;
147         struct osd_device *osd = seq->private;
148         int i;
149
150         for (i = 0; i < BRW_LAST; i++)
151                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
152
153         return len;
154 }
155
156 LPROC_SEQ_FOPS(osd_brw_stats);
157
158 static int osd_stats_init(struct osd_device *osd)
159 {
160         int result, i;
161         ENTRY;
162
163         for (i = 0; i < BRW_LAST; i++)
164                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
165
166         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
167         if (osd->od_stats != NULL) {
168                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
169                                 osd->od_stats);
170                 if (result)
171                         GOTO(out, result);
172
173                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
174                                 LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
175                                 "get_page", "usec");
176                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
177                                 LPROCFS_CNTR_AVGMINMAX,
178                                 "get_page_failures", "num");
179                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
180                                 LPROCFS_CNTR_AVGMINMAX,
181                                 "cache_access", "pages");
182                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
183                                 LPROCFS_CNTR_AVGMINMAX,
184                                 "cache_hit", "pages");
185                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
186                                 LPROCFS_CNTR_AVGMINMAX,
187                                 "cache_miss", "pages");
188                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_COPY_IO,
189                                 LPROCFS_CNTR_AVGMINMAX,
190                                 "copy", "pages");
191                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_ZEROCOPY_IO,
192                                 LPROCFS_CNTR_AVGMINMAX,
193                                 "zerocopy", "pages");
194                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TAIL_IO,
195                                 LPROCFS_CNTR_AVGMINMAX,
196                                 "tail", "pages");
197 #ifdef OSD_THANDLE_STATS
198                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
199                                 LPROCFS_CNTR_AVGMINMAX,
200                                 "thandle_starting", "usec");
201                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
202                                 LPROCFS_CNTR_AVGMINMAX,
203                                 "thandle_open", "usec");
204                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
205                                 LPROCFS_CNTR_AVGMINMAX,
206                                 "thandle_closing", "usec");
207 #endif
208                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
209                                             0644, &osd_brw_stats_fops, osd);
210         } else {
211                 result = -ENOMEM;
212         }
213
214 out:
215         RETURN(result);
216 }
217
218 static int zfs_osd_fstype_seq_show(struct seq_file *m, void *data)
219 {
220         seq_puts(m, "zfs\n");
221         return 0;
222 }
223 LPROC_SEQ_FOPS_RO(zfs_osd_fstype);
224
225 static int zfs_osd_mntdev_seq_show(struct seq_file *m, void *data)
226 {
227         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
228
229         LASSERT(osd != NULL);
230         seq_printf(m, "%s\n", osd->od_mntdev);
231         return 0;
232 }
233 LPROC_SEQ_FOPS_RO(zfs_osd_mntdev);
234
235 static ssize_t
236 lprocfs_osd_force_sync_seq_write(struct file *file, const char __user *buffer,
237                                 size_t count, loff_t *off)
238 {
239         struct seq_file   *m = file->private_data;
240         struct dt_device  *dt = m->private;
241         struct lu_env      env;
242         int rc;
243
244         rc = lu_env_init(&env, LCT_LOCAL);
245         if (rc)
246                 return rc;
247         rc = dt_sync(&env, dt);
248         lu_env_fini(&env);
249
250         return rc == 0 ? count : rc;
251 }
252 LPROC_SEQ_FOPS_WO_TYPE(zfs, osd_force_sync);
253
254 static int zfs_osd_iused_est_seq_show(struct seq_file *m, void *data)
255 {
256         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
257         LASSERT(osd != NULL);
258
259         seq_printf(m, "%d\n", osd->od_quota_iused_est);
260         return 0;
261 }
262
263 static ssize_t
264 zfs_osd_iused_est_seq_write(struct file *file, const char __user *buffer,
265                              size_t count, loff_t *off)
266 {
267         struct seq_file *m = file->private_data;
268         struct dt_device *dt = m->private;
269         struct osd_device *osd = osd_dt_dev(dt);
270         int rc;
271         __s64 val;
272
273         LASSERT(osd != NULL);
274
275         rc = lprocfs_str_to_s64(buffer, count, &val);
276         if (rc)
277                 return rc;
278
279         osd->od_quota_iused_est = !!val;
280
281         return count;
282 }
283 LPROC_SEQ_FOPS(zfs_osd_iused_est);
284
285 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_blksize);
286 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytestotal);
287 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytesfree);
288 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytesavail);
289 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_filestotal);
290 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_filesfree);
291
292 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
293         { .name =       "blocksize",
294           .fops =       &zfs_dt_blksize_fops            },
295         { .name =       "kbytestotal",
296           .fops =       &zfs_dt_kbytestotal_fops        },
297         { .name =       "kbytesfree",
298           .fops =       &zfs_dt_kbytesfree_fops         },
299         { .name =       "kbytesavail",
300           .fops =       &zfs_dt_kbytesavail_fops        },
301         { .name =       "filestotal",
302           .fops =       &zfs_dt_filestotal_fops         },
303         { .name =       "filesfree",
304           .fops =       &zfs_dt_filesfree_fops          },
305         { .name =       "fstype",
306           .fops =       &zfs_osd_fstype_fops            },
307         { .name =       "mntdev",
308           .fops =       &zfs_osd_mntdev_fops            },
309         { .name =       "force_sync",
310           .fops =       &zfs_osd_force_sync_fops        },
311         { .name =       "quota_iused_estimate",
312           .fops =       &zfs_osd_iused_est_fops         },
313         { 0 }
314 };
315
316 int osd_procfs_init(struct osd_device *osd, const char *name)
317 {
318         struct obd_type *type;
319         int              rc;
320         ENTRY;
321
322         if (osd->od_proc_entry)
323                 RETURN(0);
324
325         /* at the moment there is no linkage between lu_type
326          * and obd_type, so we lookup obd_type this way */
327         type = class_search_type(LUSTRE_OSD_ZFS_NAME);
328
329         LASSERT(name != NULL);
330         LASSERT(type != NULL);
331
332         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
333                                               lprocfs_osd_obd_vars,
334                                               &osd->od_dt_dev);
335         if (IS_ERR(osd->od_proc_entry)) {
336                 rc = PTR_ERR(osd->od_proc_entry);
337                 CERROR("Error %d setting up lprocfs for %s\n", rc, name);
338                 osd->od_proc_entry = NULL;
339                 GOTO(out, rc);
340         }
341
342         rc = osd_stats_init(osd);
343
344         GOTO(out, rc);
345 out:
346         if (rc)
347                 osd_procfs_fini(osd);
348         return rc;
349 }
350
351 int osd_procfs_fini(struct osd_device *osd)
352 {
353         ENTRY;
354
355         if (osd->od_stats)
356                 lprocfs_free_stats(&osd->od_stats);
357
358         if (osd->od_proc_entry) {
359                 lprocfs_remove(&osd->od_proc_entry);
360                 osd->od_proc_entry = NULL;
361         }
362
363         RETURN(0);
364 }
365
366 #endif