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