]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/module/os/freebsd/spl/spl_kstat.c
OpenZFS: MFV 2.0-rc3-gfc5966
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / module / os / freebsd / spl / spl_kstat.c
1 /*
2  * Copyright (c) 2007 Pawel Jakub Dawidek <pjd@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  * Links to Illumos.org for more information on kstat function:
27  * [1] https://illumos.org/man/1M/kstat
28  * [2] https://illumos.org/man/9f/kstat_create
29  */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
34 #include <sys/types.h>
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38 #include <sys/malloc.h>
39 #include <sys/sysctl.h>
40 #include <sys/kstat.h>
41 #include <sys/sbuf.h>
42
43 static MALLOC_DEFINE(M_KSTAT, "kstat_data", "Kernel statistics");
44
45 SYSCTL_ROOT_NODE(OID_AUTO, kstat, CTLFLAG_RW, 0, "Kernel statistics");
46
47 void
48 __kstat_set_raw_ops(kstat_t *ksp,
49     int (*headers)(char *buf, size_t size),
50     int (*data)(char *buf, size_t size, void *data),
51     void *(*addr)(kstat_t *ksp, loff_t index))
52 {
53         ksp->ks_raw_ops.headers = headers;
54         ksp->ks_raw_ops.data    = data;
55         ksp->ks_raw_ops.addr    = addr;
56 }
57
58 void
59 __kstat_set_seq_raw_ops(kstat_t *ksp,
60     int (*headers)(struct seq_file *f),
61     int (*data)(char *buf, size_t size, void *data),
62     void *(*addr)(kstat_t *ksp, loff_t index))
63 {
64         ksp->ks_raw_ops.seq_headers = headers;
65         ksp->ks_raw_ops.data    = data;
66         ksp->ks_raw_ops.addr    = addr;
67 }
68
69 static int
70 kstat_default_update(kstat_t *ksp, int rw)
71 {
72         ASSERT(ksp != NULL);
73
74         if (rw == KSTAT_WRITE)
75                 return (EACCES);
76
77         return (0);
78 }
79
80 static int
81 kstat_resize_raw(kstat_t *ksp)
82 {
83         if (ksp->ks_raw_bufsize == KSTAT_RAW_MAX)
84                 return (ENOMEM);
85
86         free(ksp->ks_raw_buf, M_TEMP);
87         ksp->ks_raw_bufsize = MIN(ksp->ks_raw_bufsize * 2, KSTAT_RAW_MAX);
88         ksp->ks_raw_buf = malloc(ksp->ks_raw_bufsize, M_TEMP, M_WAITOK);
89
90         return (0);
91 }
92
93 static void *
94 kstat_raw_default_addr(kstat_t *ksp, loff_t n)
95 {
96         if (n == 0)
97                 return (ksp->ks_data);
98         return (NULL);
99 }
100
101 static int
102 kstat_sysctl(SYSCTL_HANDLER_ARGS)
103 {
104         kstat_t *ksp = arg1;
105         kstat_named_t *ksent;
106         uint64_t val;
107
108         ksent = ksp->ks_data;
109         /* Select the correct element */
110         ksent += arg2;
111         /* Update the aggsums before reading */
112         (void) ksp->ks_update(ksp, KSTAT_READ);
113         val = ksent->value.ui64;
114
115         return (sysctl_handle_64(oidp, &val, 0, req));
116 }
117
118 static int
119 kstat_sysctl_string(SYSCTL_HANDLER_ARGS)
120 {
121         kstat_t *ksp = arg1;
122         kstat_named_t *ksent = ksp->ks_data;
123         char *val;
124         uint32_t len = 0;
125
126         /* Select the correct element */
127         ksent += arg2;
128         /* Update the aggsums before reading */
129         (void) ksp->ks_update(ksp, KSTAT_READ);
130         val = KSTAT_NAMED_STR_PTR(ksent);
131         len = KSTAT_NAMED_STR_BUFLEN(ksent);
132         val[len-1] = '\0';
133
134         return (sysctl_handle_string(oidp, val, len, req));
135 }
136
137 static int
138 kstat_sysctl_io(SYSCTL_HANDLER_ARGS)
139 {
140         struct sbuf *sb;
141         kstat_t *ksp = arg1;
142         kstat_io_t *kip = ksp->ks_data;
143         int rc;
144
145         sb = sbuf_new_auto();
146         if (sb == NULL)
147                 return (ENOMEM);
148         /* Update the aggsums before reading */
149         (void) ksp->ks_update(ksp, KSTAT_READ);
150
151         /* though wlentime & friends are signed, they will never be negative */
152         sbuf_printf(sb,
153             "%-8llu %-8llu %-8u %-8u %-8llu %-8llu "
154             "%-8llu %-8llu %-8llu %-8llu %-8u %-8u\n",
155             kip->nread, kip->nwritten,
156             kip->reads, kip->writes,
157             kip->wtime, kip->wlentime, kip->wlastupdate,
158             kip->rtime, kip->rlentime, kip->rlastupdate,
159             kip->wcnt,  kip->rcnt);
160         rc = sbuf_finish(sb);
161         if (rc == 0)
162                 rc = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
163         sbuf_delete(sb);
164         return (rc);
165 }
166
167 static int
168 kstat_sysctl_raw(SYSCTL_HANDLER_ARGS)
169 {
170         struct sbuf *sb;
171         void *data;
172         kstat_t *ksp = arg1;
173         void *(*addr_op)(kstat_t *ksp, loff_t index);
174         int n, has_header, rc = 0;
175
176         sb = sbuf_new_auto();
177         if (sb == NULL)
178                 return (ENOMEM);
179
180         if (ksp->ks_raw_ops.addr)
181                 addr_op = ksp->ks_raw_ops.addr;
182         else
183                 addr_op = kstat_raw_default_addr;
184
185         mutex_enter(ksp->ks_lock);
186
187         /* Update the aggsums before reading */
188         (void) ksp->ks_update(ksp, KSTAT_READ);
189
190         ksp->ks_raw_bufsize = PAGE_SIZE;
191         ksp->ks_raw_buf = malloc(PAGE_SIZE, M_TEMP, M_WAITOK);
192
193         n = 0;
194         has_header = (ksp->ks_raw_ops.headers ||
195             ksp->ks_raw_ops.seq_headers);
196
197 restart_headers:
198         if (ksp->ks_raw_ops.headers) {
199                 rc = ksp->ks_raw_ops.headers(
200                     ksp->ks_raw_buf, ksp->ks_raw_bufsize);
201         } else if (ksp->ks_raw_ops.seq_headers) {
202                 struct seq_file f;
203
204                 f.sf_buf = ksp->ks_raw_buf;
205                 f.sf_size = ksp->ks_raw_bufsize;
206                 rc = ksp->ks_raw_ops.seq_headers(&f);
207         }
208         if (has_header) {
209                 if (rc == ENOMEM && !kstat_resize_raw(ksp))
210                         goto restart_headers;
211                 if (rc == 0)
212                         sbuf_printf(sb, "\n%s", ksp->ks_raw_buf);
213         }
214
215         while ((data = addr_op(ksp, n)) != NULL) {
216 restart:
217                 if (ksp->ks_raw_ops.data) {
218                         rc = ksp->ks_raw_ops.data(ksp->ks_raw_buf,
219                             ksp->ks_raw_bufsize, data);
220                         if (rc == ENOMEM && !kstat_resize_raw(ksp))
221                                 goto restart;
222                         if (rc == 0)
223                                 sbuf_printf(sb, "%s", ksp->ks_raw_buf);
224
225                 } else {
226                         ASSERT(ksp->ks_ndata == 1);
227                         sbuf_hexdump(sb, ksp->ks_data,
228                             ksp->ks_data_size, NULL, 0);
229                 }
230                 n++;
231         }
232         free(ksp->ks_raw_buf, M_TEMP);
233         mutex_exit(ksp->ks_lock);
234         rc = sbuf_finish(sb);
235         if (rc == 0)
236                 rc = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
237         sbuf_delete(sb);
238         return (rc);
239 }
240
241 kstat_t *
242 __kstat_create(const char *module, int instance, const char *name,
243     const char *class, uchar_t ks_type, uint_t ks_ndata, uchar_t flags)
244 {
245         char buf[KSTAT_STRLEN];
246         struct sysctl_oid *root;
247         kstat_t *ksp;
248         char *pool;
249
250         KASSERT(instance == 0, ("instance=%d", instance));
251         if ((ks_type == KSTAT_TYPE_INTR) || (ks_type == KSTAT_TYPE_IO))
252                 ASSERT(ks_ndata == 1);
253
254         if (class == NULL)
255                 class = "misc";
256
257         /*
258          * Allocate the main structure. We don't need to keep a copy of
259          * module in here, because it is only used for sysctl node creation
260          * done in this function.
261          */
262         ksp = malloc(sizeof (*ksp), M_KSTAT, M_WAITOK|M_ZERO);
263
264         ksp->ks_crtime = gethrtime();
265         ksp->ks_snaptime = ksp->ks_crtime;
266         ksp->ks_instance = instance;
267         (void) strlcpy(ksp->ks_name, name, KSTAT_STRLEN);
268         (void) strlcpy(ksp->ks_class, class, KSTAT_STRLEN);
269         ksp->ks_type = ks_type;
270         ksp->ks_flags = flags;
271         ksp->ks_update = kstat_default_update;
272
273         mutex_init(&ksp->ks_private_lock, NULL, MUTEX_DEFAULT, NULL);
274         ksp->ks_lock = &ksp->ks_private_lock;
275
276         switch (ksp->ks_type) {
277         case KSTAT_TYPE_RAW:
278                 ksp->ks_ndata = 1;
279                 ksp->ks_data_size = ks_ndata;
280                 break;
281         case KSTAT_TYPE_NAMED:
282                 ksp->ks_ndata = ks_ndata;
283                 ksp->ks_data_size = ks_ndata * sizeof (kstat_named_t);
284                 break;
285         case KSTAT_TYPE_INTR:
286                 ksp->ks_ndata = ks_ndata;
287                 ksp->ks_data_size = ks_ndata * sizeof (kstat_intr_t);
288                 break;
289         case KSTAT_TYPE_IO:
290                 ksp->ks_ndata = ks_ndata;
291                 ksp->ks_data_size = ks_ndata * sizeof (kstat_io_t);
292                 break;
293         case KSTAT_TYPE_TIMER:
294                 ksp->ks_ndata = ks_ndata;
295                 ksp->ks_data_size = ks_ndata * sizeof (kstat_timer_t);
296                 break;
297         default:
298                 panic("Undefined kstat type %d\n", ksp->ks_type);
299         }
300
301         if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
302                 ksp->ks_data = NULL;
303         } else {
304                 ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
305                 if (ksp->ks_data == NULL) {
306                         kmem_free(ksp, sizeof (*ksp));
307                         ksp = NULL;
308                 }
309         }
310
311         /*
312          * Some kstats use a module name like "zfs/poolname" to distinguish a
313          * set of kstats belonging to a specific pool.  Split on '/' to add an
314          * extra node for the pool name if needed.
315          */
316         (void) strlcpy(buf, module, KSTAT_STRLEN);
317         module = buf;
318         pool = strchr(module, '/');
319         if (pool != NULL)
320                 *pool++ = '\0';
321
322         /*
323          * Create sysctl tree for those statistics:
324          *
325          *      kstat.<module>[.<pool>].<class>.<name>
326          */
327         sysctl_ctx_init(&ksp->ks_sysctl_ctx);
328         root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
329             SYSCTL_STATIC_CHILDREN(_kstat), OID_AUTO, module, CTLFLAG_RW, 0,
330             "");
331         if (root == NULL) {
332                 printf("%s: Cannot create kstat.%s tree!\n", __func__, module);
333                 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
334                 free(ksp, M_KSTAT);
335                 return (NULL);
336         }
337         if (pool != NULL) {
338                 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
339                     SYSCTL_CHILDREN(root), OID_AUTO, pool, CTLFLAG_RW, 0, "");
340                 if (root == NULL) {
341                         printf("%s: Cannot create kstat.%s.%s tree!\n",
342                             __func__, module, pool);
343                         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
344                         free(ksp, M_KSTAT);
345                         return (NULL);
346                 }
347         }
348         root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx, SYSCTL_CHILDREN(root),
349             OID_AUTO, class, CTLFLAG_RW, 0, "");
350         if (root == NULL) {
351                 if (pool != NULL)
352                         printf("%s: Cannot create kstat.%s.%s.%s tree!\n",
353                             __func__, module, pool, class);
354                 else
355                         printf("%s: Cannot create kstat.%s.%s tree!\n",
356                             __func__, module, class);
357                 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
358                 free(ksp, M_KSTAT);
359                 return (NULL);
360         }
361         if (ksp->ks_type == KSTAT_TYPE_NAMED) {
362                 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
363                     SYSCTL_CHILDREN(root),
364                     OID_AUTO, name, CTLFLAG_RW, 0, "");
365                 if (root == NULL) {
366                         if (pool != NULL)
367                                 printf("%s: Cannot create kstat.%s.%s.%s.%s "
368                                     "tree!\n", __func__, module, pool, class,
369                                     name);
370                         else
371                                 printf("%s: Cannot create kstat.%s.%s.%s "
372                                     "tree!\n", __func__, module, class, name);
373                         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
374                         free(ksp, M_KSTAT);
375                         return (NULL);
376                 }
377
378         }
379         ksp->ks_sysctl_root = root;
380
381         return (ksp);
382 }
383
384 static void
385 kstat_install_named(kstat_t *ksp)
386 {
387         kstat_named_t *ksent;
388         char *namelast;
389         int typelast;
390
391         ksent = ksp->ks_data;
392
393         VERIFY((ksp->ks_flags & KSTAT_FLAG_VIRTUAL) || ksent != NULL);
394
395         typelast = 0;
396         namelast = NULL;
397
398         for (int i = 0; i < ksp->ks_ndata; i++, ksent++) {
399                 if (ksent->data_type != 0) {
400                         typelast = ksent->data_type;
401                         namelast = ksent->name;
402                 }
403                 switch (typelast) {
404                 case KSTAT_DATA_CHAR:
405                         /* Not Implemented */
406                         break;
407                 case KSTAT_DATA_INT32:
408                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
409                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
410                             OID_AUTO, namelast,
411                             CTLTYPE_S32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
412                             ksp, i, kstat_sysctl, "I", namelast);
413                         break;
414                 case KSTAT_DATA_UINT32:
415                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
416                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
417                             OID_AUTO, namelast,
418                             CTLTYPE_U32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
419                             ksp, i, kstat_sysctl, "IU", namelast);
420                         break;
421                 case KSTAT_DATA_INT64:
422                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
423                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
424                             OID_AUTO, namelast,
425                             CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
426                             ksp, i, kstat_sysctl, "Q", namelast);
427                         break;
428                 case KSTAT_DATA_UINT64:
429                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
430                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
431                             OID_AUTO, namelast,
432                             CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
433                             ksp, i, kstat_sysctl, "QU", namelast);
434                         break;
435                 case KSTAT_DATA_LONG:
436                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
437                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
438                             OID_AUTO, namelast,
439                             CTLTYPE_LONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
440                             ksp, i, kstat_sysctl, "L", namelast);
441                         break;
442                 case KSTAT_DATA_ULONG:
443                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
444                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
445                             OID_AUTO, namelast,
446                             CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
447                             ksp, i, kstat_sysctl, "LU", namelast);
448                         break;
449                 case KSTAT_DATA_STRING:
450                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
451                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
452                             OID_AUTO, namelast,
453                             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
454                             ksp, i, kstat_sysctl_string, "A", namelast);
455                         break;
456                 default:
457                         panic("unsupported type: %d", typelast);
458                 }
459         }
460 }
461
462 void
463 kstat_install(kstat_t *ksp)
464 {
465         struct sysctl_oid *root;
466
467         if (ksp->ks_ndata == UINT32_MAX)
468                 VERIFY(ksp->ks_type == KSTAT_TYPE_RAW);
469
470         switch (ksp->ks_type) {
471         case KSTAT_TYPE_NAMED:
472                 return (kstat_install_named(ksp));
473         case KSTAT_TYPE_RAW:
474                 if (ksp->ks_raw_ops.data) {
475                         root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
476                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
477                             OID_AUTO, ksp->ks_name,
478                             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
479                             ksp, 0, kstat_sysctl_raw, "A", ksp->ks_name);
480                 } else {
481                         root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
482                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
483                             OID_AUTO, ksp->ks_name,
484                             CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
485                             ksp, 0, kstat_sysctl_raw, "", ksp->ks_name);
486                 }
487                 break;
488         case KSTAT_TYPE_IO:
489                 root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
490                     SYSCTL_CHILDREN(ksp->ks_sysctl_root),
491                     OID_AUTO, ksp->ks_name,
492                     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
493                     ksp, 0, kstat_sysctl_io, "A", ksp->ks_name);
494                 break;
495         case KSTAT_TYPE_TIMER:
496         case KSTAT_TYPE_INTR:
497         default:
498                 panic("unsupported kstat type %d\n", ksp->ks_type);
499         }
500         VERIFY(root != NULL);
501         ksp->ks_sysctl_root = root;
502 }
503
504 void
505 kstat_delete(kstat_t *ksp)
506 {
507
508         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
509         ksp->ks_lock = NULL;
510         mutex_destroy(&ksp->ks_private_lock);
511         free(ksp, M_KSTAT);
512 }
513
514 void
515 kstat_waitq_enter(kstat_io_t *kiop)
516 {
517         hrtime_t new, delta;
518         ulong_t wcnt;
519
520         new = gethrtime();
521         delta = new - kiop->wlastupdate;
522         kiop->wlastupdate = new;
523         wcnt = kiop->wcnt++;
524         if (wcnt != 0) {
525                 kiop->wlentime += delta * wcnt;
526                 kiop->wtime += delta;
527         }
528 }
529
530 void
531 kstat_waitq_exit(kstat_io_t *kiop)
532 {
533         hrtime_t new, delta;
534         ulong_t wcnt;
535
536         new = gethrtime();
537         delta = new - kiop->wlastupdate;
538         kiop->wlastupdate = new;
539         wcnt = kiop->wcnt--;
540         ASSERT((int)wcnt > 0);
541         kiop->wlentime += delta * wcnt;
542         kiop->wtime += delta;
543 }
544
545 void
546 kstat_runq_enter(kstat_io_t *kiop)
547 {
548         hrtime_t new, delta;
549         ulong_t rcnt;
550
551         new = gethrtime();
552         delta = new - kiop->rlastupdate;
553         kiop->rlastupdate = new;
554         rcnt = kiop->rcnt++;
555         if (rcnt != 0) {
556                 kiop->rlentime += delta * rcnt;
557                 kiop->rtime += delta;
558         }
559 }
560
561 void
562 kstat_runq_exit(kstat_io_t *kiop)
563 {
564         hrtime_t new, delta;
565         ulong_t rcnt;
566
567         new = gethrtime();
568         delta = new - kiop->rlastupdate;
569         kiop->rlastupdate = new;
570         rcnt = kiop->rcnt--;
571         ASSERT((int)rcnt > 0);
572         kiop->rlentime += delta * rcnt;
573         kiop->rtime += delta;
574 }