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