Whamcloud - gitweb
7944f183767cba309f45a57d7448c36dc2ddaafd
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/osd-zfs/osd_lproc.c
33  *
34  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
35  * Author: Mike Pershin <tappro@whamcloud.com>
36  */
37
38 #define DEBUG_SUBSYSTEM S_OSD
39
40 #include <obd.h>
41 #include <obd_class.h>
42 #include <lprocfs_status.h>
43 #include <lustre_scrub.h>
44
45 #include "osd_internal.h"
46
47 #ifdef CONFIG_PROC_FS
48
49 #define pct(a, b) (b ? a * 100 / b : 0)
50
51 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
52                               struct obd_histogram *read,
53                               struct obd_histogram *write, int scale)
54 {
55         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
56         int i;
57
58         seq_printf(seq, "\n%26s read      |     write\n", " ");
59         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
60                    name, units, units);
61
62         read_tot = lprocfs_oh_sum(read);
63         write_tot = lprocfs_oh_sum(write);
64         for (i = 0; i < OBD_HIST_MAX; i++) {
65                 r = read->oh_buckets[i];
66                 w = write->oh_buckets[i];
67                 read_cum += r;
68                 write_cum += w;
69                 if (read_cum == 0 && write_cum == 0)
70                         continue;
71
72                 if (!scale)
73                         seq_printf(seq, "%u", i);
74                 else if (i < 10)
75                         seq_printf(seq, "%u", scale << i);
76                 else if (i < 20)
77                         seq_printf(seq, "%uK", scale << (i-10));
78                 else
79                         seq_printf(seq, "%uM", scale << (i-20));
80
81                 seq_printf(seq, ":\t\t%10lu %3lu %3lu   | %4lu %3lu %3lu\n",
82                            r, pct(r, read_tot), pct(read_cum, read_tot),
83                            w, pct(w, write_tot), pct(write_cum, write_tot));
84
85                 if (read_cum == read_tot && write_cum == write_tot)
86                         break;
87         }
88 }
89
90 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
91 {
92         struct timespec64 now;
93
94         /* this sampling races with updates */
95         ktime_get_real_ts64(&now);
96         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
97                    (s64)now.tv_sec, now.tv_nsec);
98
99         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
100                           &brw_stats->hist[BRW_R_PAGES],
101                           &brw_stats->hist[BRW_W_PAGES], 1);
102         display_brw_stats(seq, "discontiguous pages", "rpcs",
103                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
104                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
105 #if 0
106         display_brw_stats(seq, "discontiguous blocks", "rpcs",
107                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
108                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
109
110         display_brw_stats(seq, "disk fragmented I/Os", "ios",
111                           &brw_stats->hist[BRW_R_DIO_FRAGS],
112                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
113 #endif
114         display_brw_stats(seq, "disk I/Os in flight", "ios",
115                           &brw_stats->hist[BRW_R_RPC_HIST],
116                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
117
118         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
119                           &brw_stats->hist[BRW_R_IO_TIME],
120                           &brw_stats->hist[BRW_W_IO_TIME], 1000 / HZ);
121
122         display_brw_stats(seq, "disk I/O size", "ios",
123                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
124                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
125 }
126
127 #undef pct
128
129 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
130 {
131         struct osd_device *osd = seq->private;
132
133         brw_stats_show(seq, &osd->od_brw_stats);
134
135         return 0;
136 }
137
138 static ssize_t osd_brw_stats_seq_write(struct file *file,
139                                        const char __user *buf,
140                                        size_t len, loff_t *off)
141 {
142         struct seq_file *seq = file->private_data;
143         struct osd_device *osd = seq->private;
144         int i;
145
146         for (i = 0; i < BRW_LAST; i++)
147                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
148
149         return len;
150 }
151
152 LPROC_SEQ_FOPS(osd_brw_stats);
153
154 static int osd_stats_init(struct osd_device *osd)
155 {
156         int result, i;
157         ENTRY;
158
159         for (i = 0; i < BRW_LAST; i++)
160                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
161
162         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
163         if (osd->od_stats != NULL) {
164                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
165                                 osd->od_stats);
166                 if (result)
167                         GOTO(out, result);
168
169                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
170                                 LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
171                                 "get_page", "usec");
172                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
173                                 LPROCFS_CNTR_AVGMINMAX,
174                                 "get_page_failures", "num");
175                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
176                                 LPROCFS_CNTR_AVGMINMAX,
177                                 "cache_access", "pages");
178                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
179                                 LPROCFS_CNTR_AVGMINMAX,
180                                 "cache_hit", "pages");
181                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
182                                 LPROCFS_CNTR_AVGMINMAX,
183                                 "cache_miss", "pages");
184                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_COPY_IO,
185                                 LPROCFS_CNTR_AVGMINMAX,
186                                 "copy", "pages");
187                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_ZEROCOPY_IO,
188                                 LPROCFS_CNTR_AVGMINMAX,
189                                 "zerocopy", "pages");
190                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TAIL_IO,
191                                 LPROCFS_CNTR_AVGMINMAX,
192                                 "tail", "pages");
193 #ifdef OSD_THANDLE_STATS
194                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
195                                 LPROCFS_CNTR_AVGMINMAX,
196                                 "thandle_starting", "usec");
197                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
198                                 LPROCFS_CNTR_AVGMINMAX,
199                                 "thandle_open", "usec");
200                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
201                                 LPROCFS_CNTR_AVGMINMAX,
202                                 "thandle_closing", "usec");
203 #endif
204                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
205                                             0644, &osd_brw_stats_fops, osd);
206         } else {
207                 result = -ENOMEM;
208         }
209
210 out:
211         RETURN(result);
212 }
213
214 static int zfs_osd_auto_scrub_seq_show(struct seq_file *m, void *data)
215 {
216         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
217
218         LASSERT(dev != NULL);
219         if (!dev->od_os)
220                 return -EINPROGRESS;
221
222         seq_printf(m, "%lld\n", dev->od_auto_scrub_interval);
223         return 0;
224 }
225
226 static ssize_t
227 zfs_osd_auto_scrub_seq_write(struct file *file, const char __user *buffer,
228                              size_t count, loff_t *off)
229 {
230         struct seq_file *m = file->private_data;
231         struct dt_device *dt = m->private;
232         struct osd_device *dev = osd_dt_dev(dt);
233         int rc;
234         __s64 val;
235
236         LASSERT(dev != NULL);
237         if (!dev->od_os)
238                 return -EINPROGRESS;
239
240         rc = kstrtoull_from_user(buffer, count, 0, &val);
241         if (rc)
242                 return rc;
243
244         dev->od_auto_scrub_interval = val;
245         return count;
246 }
247 LPROC_SEQ_FOPS(zfs_osd_auto_scrub);
248
249 static int zfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
250 {
251         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
252
253         LASSERT(dev != NULL);
254         if (!dev->od_os)
255                 return -EINPROGRESS;
256
257         scrub_dump(m, &dev->od_scrub);
258         return 0;
259 }
260 LPROC_SEQ_FOPS_RO(zfs_osd_oi_scrub);
261
262 static int zfs_osd_fstype_seq_show(struct seq_file *m, void *data)
263 {
264         seq_puts(m, "zfs\n");
265         return 0;
266 }
267 LPROC_SEQ_FOPS_RO(zfs_osd_fstype);
268
269 static int zfs_osd_mntdev_seq_show(struct seq_file *m, void *data)
270 {
271         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
272
273         LASSERT(osd != NULL);
274         seq_printf(m, "%s\n", osd->od_mntdev);
275         return 0;
276 }
277 LPROC_SEQ_FOPS_RO(zfs_osd_mntdev);
278
279 static ssize_t
280 lprocfs_osd_force_sync_seq_write(struct file *file, const char __user *buffer,
281                                 size_t count, loff_t *off)
282 {
283         struct seq_file   *m = file->private_data;
284         struct dt_device  *dt = m->private;
285         struct lu_env      env;
286         int rc;
287
288         rc = lu_env_init(&env, LCT_LOCAL);
289         if (rc)
290                 return rc;
291         rc = dt_sync(&env, dt);
292         lu_env_fini(&env);
293
294         return rc == 0 ? count : rc;
295 }
296 LPROC_SEQ_FOPS_WR_ONLY(zfs, osd_force_sync);
297
298 static int zfs_osd_index_backup_seq_show(struct seq_file *m, void *data)
299 {
300         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
301
302         LASSERT(dev != NULL);
303         if (!dev->od_os)
304                 return -EINPROGRESS;
305
306         seq_printf(m, "%d\n", dev->od_index_backup_policy);
307         return 0;
308 }
309
310 static ssize_t zfs_osd_index_backup_seq_write(struct file *file,
311                                               const char __user *buffer,
312                                               size_t count, loff_t *off)
313 {
314         struct seq_file *m = file->private_data;
315         struct dt_device *dt = m->private;
316         struct osd_device *dev = osd_dt_dev(dt);
317         int val;
318         int rc;
319
320         LASSERT(dev != NULL);
321         if (!dev->od_os)
322                 return -EINPROGRESS;
323
324         rc = kstrtoint_from_user(buffer, count, 0, &val);
325         if (rc)
326                 return rc;
327
328         dev->od_index_backup_policy = val;
329         return count;
330 }
331 LPROC_SEQ_FOPS(zfs_osd_index_backup);
332
333 static int zfs_osd_readcache_seq_show(struct seq_file *m, void *data)
334 {
335         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
336
337         LASSERT(osd != NULL);
338         if (unlikely(osd->od_os == NULL))
339                 return -EINPROGRESS;
340
341         seq_printf(m, "%llu\n", osd->od_readcache_max_filesize);
342         return 0;
343 }
344
345 static ssize_t
346 zfs_osd_readcache_seq_write(struct file *file, const char __user *buffer,
347                             size_t count, loff_t *off)
348 {
349         struct seq_file *m = file->private_data;
350         struct dt_device *dt = m->private;
351         struct osd_device *osd = osd_dt_dev(dt);
352         s64 val;
353         int rc;
354
355         LASSERT(osd != NULL);
356         if (unlikely(osd->od_os == NULL))
357                 return -EINPROGRESS;
358
359         rc = lprocfs_str_with_units_to_s64(buffer, count, &val, '1');
360         if (rc)
361                 return rc;
362         if (val < 0)
363                 return -ERANGE;
364
365         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
366                                          OSD_MAX_CACHE_SIZE : val;
367         return count;
368 }
369 LPROC_SEQ_FOPS(zfs_osd_readcache);
370
371 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_blksize);
372 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytestotal);
373 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytesfree);
374 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_kbytesavail);
375 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_filestotal);
376 LPROC_SEQ_FOPS_RO_TYPE(zfs, dt_filesfree);
377
378 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
379         { .name =       "blocksize",
380           .fops =       &zfs_dt_blksize_fops            },
381         { .name =       "kbytestotal",
382           .fops =       &zfs_dt_kbytestotal_fops        },
383         { .name =       "kbytesfree",
384           .fops =       &zfs_dt_kbytesfree_fops         },
385         { .name =       "kbytesavail",
386           .fops =       &zfs_dt_kbytesavail_fops        },
387         { .name =       "filestotal",
388           .fops =       &zfs_dt_filestotal_fops         },
389         { .name =       "filesfree",
390           .fops =       &zfs_dt_filesfree_fops          },
391         { .name =       "auto_scrub",
392           .fops =       &zfs_osd_auto_scrub_fops        },
393         { .name =       "oi_scrub",
394           .fops =       &zfs_osd_oi_scrub_fops          },
395         { .name =       "fstype",
396           .fops =       &zfs_osd_fstype_fops            },
397         { .name =       "mntdev",
398           .fops =       &zfs_osd_mntdev_fops            },
399         { .name =       "force_sync",
400           .fops =       &zfs_osd_force_sync_fops        },
401         { .name =       "index_backup",
402           .fops =       &zfs_osd_index_backup_fops      },
403         { .name =       "readcache_max_filesize",
404           .fops =       &zfs_osd_readcache_fops },
405         { 0 }
406 };
407
408 int osd_procfs_init(struct osd_device *osd, const char *name)
409 {
410         struct obd_type *type;
411         int              rc;
412         ENTRY;
413
414         if (osd->od_proc_entry)
415                 RETURN(0);
416
417         /* at the moment there is no linkage between lu_type
418          * and obd_type, so we lookup obd_type this way */
419         type = class_search_type(LUSTRE_OSD_ZFS_NAME);
420
421         LASSERT(name != NULL);
422         LASSERT(type != NULL);
423
424         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
425                                               lprocfs_osd_obd_vars,
426                                               &osd->od_dt_dev);
427         if (IS_ERR(osd->od_proc_entry)) {
428                 rc = PTR_ERR(osd->od_proc_entry);
429                 CERROR("Error %d setting up lprocfs for %s\n", rc, name);
430                 osd->od_proc_entry = NULL;
431                 GOTO(out, rc);
432         }
433
434         rc = osd_stats_init(osd);
435
436         GOTO(out, rc);
437 out:
438         if (rc)
439                 osd_procfs_fini(osd);
440         return rc;
441 }
442
443 int osd_procfs_fini(struct osd_device *osd)
444 {
445         ENTRY;
446
447         if (osd->od_stats)
448                 lprocfs_free_stats(&osd->od_stats);
449
450         if (osd->od_proc_entry) {
451                 lprocfs_remove(&osd->od_proc_entry);
452                 osd->od_proc_entry = NULL;
453         }
454
455         RETURN(0);
456 }
457
458 #endif