Whamcloud - gitweb
LU-9091 sysfs: use string helper like functions for sysfs
[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 static void display_brw_stats(struct seq_file *seq, char *name, char *units,
50                               struct obd_histogram *read,
51                               struct obd_histogram *write, int scale)
52 {
53         unsigned long read_tot, write_tot, r, w, read_cum = 0, write_cum = 0;
54         int i;
55
56         seq_printf(seq, "\n%26s read      |     write\n", " ");
57         seq_printf(seq, "%-22s %-5s %% cum %% |  %-11s %% cum %%\n",
58                    name, units, units);
59
60         read_tot = lprocfs_oh_sum(read);
61         write_tot = lprocfs_oh_sum(write);
62         for (i = 0; i < OBD_HIST_MAX; i++) {
63                 r = read->oh_buckets[i];
64                 w = write->oh_buckets[i];
65                 read_cum += r;
66                 write_cum += w;
67                 if (read_cum == 0 && write_cum == 0)
68                         continue;
69
70                 if (!scale)
71                         seq_printf(seq, "%u", i);
72                 else if (i < 10)
73                         seq_printf(seq, "%u", scale << i);
74                 else if (i < 20)
75                         seq_printf(seq, "%uK", scale << (i-10));
76                 else
77                         seq_printf(seq, "%uM", scale << (i-20));
78
79                 seq_printf(seq, ":\t\t%10lu %3u %3u   | %4lu %3u %3u\n",
80                            r, pct(r, read_tot), pct(read_cum, read_tot),
81                            w, pct(w, write_tot), pct(write_cum, write_tot));
82
83                 if (read_cum == read_tot && write_cum == write_tot)
84                         break;
85         }
86 }
87
88 static void brw_stats_show(struct seq_file *seq, struct brw_stats *brw_stats)
89 {
90         struct timespec64 now;
91
92         /* this sampling races with updates */
93         ktime_get_real_ts64(&now);
94         seq_printf(seq, "snapshot_time:         %llu.%09lu (secs.nsecs)\n",
95                    (s64)now.tv_sec, now.tv_nsec);
96
97         display_brw_stats(seq, "pages per bulk r/w", "rpcs",
98                           &brw_stats->hist[BRW_R_PAGES],
99                           &brw_stats->hist[BRW_W_PAGES], 1);
100         display_brw_stats(seq, "discontiguous pages", "rpcs",
101                           &brw_stats->hist[BRW_R_DISCONT_PAGES],
102                           &brw_stats->hist[BRW_W_DISCONT_PAGES], 0);
103 #if 0
104         display_brw_stats(seq, "discontiguous blocks", "rpcs",
105                           &brw_stats->hist[BRW_R_DISCONT_BLOCKS],
106                           &brw_stats->hist[BRW_W_DISCONT_BLOCKS], 0);
107
108         display_brw_stats(seq, "disk fragmented I/Os", "ios",
109                           &brw_stats->hist[BRW_R_DIO_FRAGS],
110                           &brw_stats->hist[BRW_W_DIO_FRAGS], 0);
111 #endif
112         display_brw_stats(seq, "disk I/Os in flight", "ios",
113                           &brw_stats->hist[BRW_R_RPC_HIST],
114                           &brw_stats->hist[BRW_W_RPC_HIST], 0);
115
116         display_brw_stats(seq, "I/O time (1/1000s)", "ios",
117                           &brw_stats->hist[BRW_R_IO_TIME],
118                           &brw_stats->hist[BRW_W_IO_TIME], 1);
119
120         display_brw_stats(seq, "disk I/O size", "ios",
121                           &brw_stats->hist[BRW_R_DISK_IOSIZE],
122                           &brw_stats->hist[BRW_W_DISK_IOSIZE], 1);
123 }
124
125 static int osd_brw_stats_seq_show(struct seq_file *seq, void *v)
126 {
127         struct osd_device *osd = seq->private;
128
129         brw_stats_show(seq, &osd->od_brw_stats);
130
131         return 0;
132 }
133
134 static ssize_t osd_brw_stats_seq_write(struct file *file,
135                                        const char __user *buf,
136                                        size_t len, loff_t *off)
137 {
138         struct seq_file *seq = file->private_data;
139         struct osd_device *osd = seq->private;
140         int i;
141
142         for (i = 0; i < BRW_LAST; i++)
143                 lprocfs_oh_clear(&osd->od_brw_stats.hist[i]);
144
145         return len;
146 }
147
148 LPROC_SEQ_FOPS(osd_brw_stats);
149
150 static int osd_stats_init(struct osd_device *osd)
151 {
152         int result, i;
153         ENTRY;
154
155         for (i = 0; i < BRW_LAST; i++)
156                 spin_lock_init(&osd->od_brw_stats.hist[i].oh_lock);
157
158         osd->od_stats = lprocfs_alloc_stats(LPROC_OSD_LAST, 0);
159         if (osd->od_stats != NULL) {
160                 result = lprocfs_register_stats(osd->od_proc_entry, "stats",
161                                 osd->od_stats);
162                 if (result)
163                         GOTO(out, result);
164
165                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_GET_PAGE,
166                                 LPROCFS_CNTR_AVGMINMAX|LPROCFS_CNTR_STDDEV,
167                                 "get_page", "usec");
168                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_NO_PAGE,
169                                 LPROCFS_CNTR_AVGMINMAX,
170                                 "get_page_failures", "num");
171                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_ACCESS,
172                                 LPROCFS_CNTR_AVGMINMAX,
173                                 "cache_access", "pages");
174                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_HIT,
175                                 LPROCFS_CNTR_AVGMINMAX,
176                                 "cache_hit", "pages");
177                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_CACHE_MISS,
178                                 LPROCFS_CNTR_AVGMINMAX,
179                                 "cache_miss", "pages");
180                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_COPY_IO,
181                                 LPROCFS_CNTR_AVGMINMAX,
182                                 "copy", "pages");
183                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_ZEROCOPY_IO,
184                                 LPROCFS_CNTR_AVGMINMAX,
185                                 "zerocopy", "pages");
186                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_TAIL_IO,
187                                 LPROCFS_CNTR_AVGMINMAX,
188                                 "tail", "pages");
189 #ifdef OSD_THANDLE_STATS
190                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_STARTING,
191                                 LPROCFS_CNTR_AVGMINMAX,
192                                 "thandle_starting", "usec");
193                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_OPEN,
194                                 LPROCFS_CNTR_AVGMINMAX,
195                                 "thandle_open", "usec");
196                 lprocfs_counter_init(osd->od_stats, LPROC_OSD_THANDLE_CLOSING,
197                                 LPROCFS_CNTR_AVGMINMAX,
198                                 "thandle_closing", "usec");
199 #endif
200                 result = lprocfs_seq_create(osd->od_proc_entry, "brw_stats",
201                                             0644, &osd_brw_stats_fops, osd);
202         } else {
203                 result = -ENOMEM;
204         }
205
206 out:
207         RETURN(result);
208 }
209
210 static int zfs_osd_oi_scrub_seq_show(struct seq_file *m, void *data)
211 {
212         struct osd_device *dev = osd_dt_dev((struct dt_device *)m->private);
213
214         LASSERT(dev != NULL);
215         if (!dev->od_os)
216                 return -EINPROGRESS;
217
218         scrub_dump(m, &dev->od_scrub);
219         return 0;
220 }
221 LDEBUGFS_SEQ_FOPS_RO(zfs_osd_oi_scrub);
222
223 static ssize_t auto_scrub_show(struct kobject *kobj, struct attribute *attr,
224                               char *buf)
225 {
226         struct dt_device *dt = container_of(kobj, struct dt_device,
227                                             dd_kobj);
228         struct osd_device *dev = osd_dt_dev(dt);
229
230         LASSERT(dev);
231         if (!dev->od_os)
232                 return -EINPROGRESS;
233
234         return sprintf(buf, "%lld\n", dev->od_auto_scrub_interval);
235 }
236
237 static ssize_t auto_scrub_store(struct kobject *kobj, struct attribute *attr,
238                                 const char *buffer, size_t count)
239 {
240         struct dt_device *dt = container_of(kobj, struct dt_device,
241                                             dd_kobj);
242         struct osd_device *dev = osd_dt_dev(dt);
243         s64 val;
244         int rc;
245
246         LASSERT(dev);
247         if (!dev->od_os)
248                 return -EINPROGRESS;
249
250         rc = kstrtoull(buffer, 0, &val);
251         if (rc)
252                 return rc;
253
254         dev->od_auto_scrub_interval = val;
255         return count;
256 }
257 LUSTRE_RW_ATTR(auto_scrub);
258
259 static ssize_t fstype_show(struct kobject *kobj, struct attribute *attr,
260                           char *buf)
261 {
262         return sprintf(buf, "zfs\n");
263 }
264 LUSTRE_RO_ATTR(fstype);
265
266 static ssize_t mntdev_show(struct kobject *kobj, struct attribute *attr,
267                            char *buf)
268 {
269         struct dt_device *dt = container_of(kobj, struct dt_device,
270                                             dd_kobj);
271         struct osd_device *osd = osd_dt_dev(dt);
272
273         LASSERT(osd);
274
275         return sprintf(buf, "%s\n", osd->od_mntdev);
276 }
277 LUSTRE_RO_ATTR(mntdev);
278
279 ssize_t force_sync_store(struct kobject *kobj, struct attribute *attr,
280                          const char *buffer, size_t count)
281 {
282         struct dt_device *dt = container_of(kobj, struct dt_device,
283                                             dd_kobj);
284         struct lu_env env;
285         int rc;
286
287         rc = lu_env_init(&env, LCT_LOCAL);
288         if (rc)
289                 return rc;
290
291         rc = dt_sync(&env, dt);
292         lu_env_fini(&env);
293
294         return rc == 0 ? count : rc;
295 }
296 LUSTRE_WO_ATTR(force_sync);
297
298 static ssize_t nonrotational_show(struct kobject *kobj, struct attribute *attr,
299                                   char *buf)
300 {
301         struct dt_device *dt = container_of(kobj, struct dt_device,
302                                             dd_kobj);
303         struct osd_device *osd = osd_dt_dev(dt);
304
305         LASSERT(osd);
306         if (!osd->od_os)
307                 return -EINPROGRESS;
308
309         return sprintf(buf, "%u\n", osd->od_nonrotational);
310 }
311
312 static ssize_t nonrotational_store(struct kobject *kobj,
313                                    struct attribute *attr, const char *buffer,
314                                    size_t count)
315 {
316         struct dt_device *dt = container_of(kobj, struct dt_device,
317                                             dd_kobj);
318         struct osd_device *osd = osd_dt_dev(dt);
319         bool val;
320         int rc;
321
322         LASSERT(osd);
323         if (!osd->od_os)
324                 return -EINPROGRESS;
325
326         rc = kstrtobool(buffer, &val);
327         if (rc)
328                 return rc;
329
330         osd->od_nonrotational = val;
331         return count;
332 }
333 LUSTRE_RW_ATTR(nonrotational);
334
335 static ssize_t index_backup_show(struct kobject *kobj, struct attribute *attr,
336                                  char *buf)
337 {
338         struct dt_device *dt = container_of(kobj, struct dt_device,
339                                             dd_kobj);
340         struct osd_device *dev = osd_dt_dev(dt);
341
342         LASSERT(dev);
343         if (!dev->od_os)
344                 return -EINPROGRESS;
345
346         return sprintf(buf, "%d\n", dev->od_index_backup_policy);
347 }
348
349 ssize_t index_backup_store(struct kobject *kobj, struct attribute *attr,
350                            const char *buffer, size_t count)
351 {
352         struct dt_device *dt = container_of(kobj, struct dt_device,
353                                             dd_kobj);
354         struct osd_device *dev = osd_dt_dev(dt);
355         int val;
356         int rc;
357
358         LASSERT(dev);
359         if (!dev->od_os)
360                 return -EINPROGRESS;
361
362         rc = kstrtoint(buffer, 0, &val);
363         if (rc)
364                 return rc;
365
366         dev->od_index_backup_policy = val;
367         return count;
368 }
369 LUSTRE_RW_ATTR(index_backup);
370
371 static int zfs_osd_readcache_seq_show(struct seq_file *m, void *data)
372 {
373         struct osd_device *osd = osd_dt_dev((struct dt_device *)m->private);
374
375         LASSERT(osd != NULL);
376         if (unlikely(osd->od_os == NULL))
377                 return -EINPROGRESS;
378
379         seq_printf(m, "%llu\n", osd->od_readcache_max_filesize);
380         return 0;
381 }
382
383 static ssize_t
384 zfs_osd_readcache_seq_write(struct file *file, const char __user *buffer,
385                             size_t count, loff_t *off)
386 {
387         struct seq_file *m = file->private_data;
388         struct dt_device *dt = m->private;
389         struct osd_device *osd = osd_dt_dev(dt);
390         char kernbuf[22] = "";
391         u64 val;
392         int rc;
393
394         LASSERT(osd != NULL);
395         if (unlikely(osd->od_os == NULL))
396                 return -EINPROGRESS;
397
398         if (count >= sizeof(kernbuf))
399                 return -EINVAL;
400
401         if (copy_from_user(kernbuf, buffer, count))
402                 return -EFAULT;
403         kernbuf[count] = 0;
404
405         rc = sysfs_memparse(kernbuf, count, &val, "B");
406         if (rc < 0)
407                 return rc;
408
409         osd->od_readcache_max_filesize = val > OSD_MAX_CACHE_SIZE ?
410                                          OSD_MAX_CACHE_SIZE : val;
411         return count;
412 }
413 LDEBUGFS_SEQ_FOPS(zfs_osd_readcache);
414
415 static struct attribute *zfs_attrs[] = {
416         &lustre_attr_fstype.attr,
417         &lustre_attr_mntdev.attr,
418         &lustre_attr_force_sync.attr,
419         &lustre_attr_nonrotational.attr,
420         &lustre_attr_index_backup.attr,
421         &lustre_attr_auto_scrub.attr,
422         NULL,
423 };
424
425 struct lprocfs_vars lprocfs_osd_obd_vars[] = {
426         { .name =       "oi_scrub",
427           .fops =       &zfs_osd_oi_scrub_fops          },
428         { .name =       "readcache_max_filesize",
429           .fops =       &zfs_osd_readcache_fops         },
430         { 0 }
431 };
432
433 int osd_procfs_init(struct osd_device *osd, const char *name)
434 {
435         struct obd_type *type;
436         int rc;
437
438         ENTRY;
439
440         /* at the moment there is no linkage between lu_type
441          * and obd_type, so we lookup obd_type this way
442          */
443         type = class_search_type(LUSTRE_OSD_ZFS_NAME);
444
445         LASSERT(type);
446         LASSERT(name);
447
448         /* put reference taken by class_search_type */
449         kobject_put(&type->typ_kobj);
450
451         osd->od_dt_dev.dd_ktype.default_attrs = zfs_attrs;
452         rc = dt_tunables_init(&osd->od_dt_dev, type, name,
453                               lprocfs_osd_obd_vars);
454         if (rc) {
455                 CERROR("%s: cannot setup sysfs / debugfs entry: %d\n",
456                        name, rc);
457                 GOTO(out, rc);
458         }
459
460         if (osd->od_proc_entry)
461                 RETURN(0);
462
463         osd->od_proc_entry = lprocfs_register(name, type->typ_procroot,
464                                               NULL, &osd->od_dt_dev);
465         if (IS_ERR(osd->od_proc_entry)) {
466                 rc = PTR_ERR(osd->od_proc_entry);
467                 CERROR("Error %d setting up lprocfs for %s\n", rc, name);
468                 osd->od_proc_entry = NULL;
469                 GOTO(out, rc);
470         }
471
472         rc = osd_stats_init(osd);
473
474         GOTO(out, rc);
475 out:
476         if (rc)
477                 osd_procfs_fini(osd);
478         return rc;
479 }
480
481 int osd_procfs_fini(struct osd_device *osd)
482 {
483         ENTRY;
484
485         if (osd->od_stats)
486                 lprocfs_free_stats(&osd->od_stats);
487
488         if (osd->od_proc_entry) {
489                 lprocfs_remove(&osd->od_proc_entry);
490                 osd->od_proc_entry = NULL;
491         }
492
493         return dt_tunables_fini(&osd->od_dt_dev);
494 }
495
496 #endif