]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/module/os/freebsd/spl/spl_kstat.c
Update OpenZFS to 2.0.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         sbuf_trim(sb);
235         rc = sbuf_finish(sb);
236         if (rc == 0)
237                 rc = SYSCTL_OUT(req, sbuf_data(sb), sbuf_len(sb));
238         sbuf_delete(sb);
239         return (rc);
240 }
241
242 kstat_t *
243 __kstat_create(const char *module, int instance, const char *name,
244     const char *class, uchar_t ks_type, uint_t ks_ndata, uchar_t flags)
245 {
246         char buf[KSTAT_STRLEN];
247         struct sysctl_oid *root;
248         kstat_t *ksp;
249         char *pool;
250
251         KASSERT(instance == 0, ("instance=%d", instance));
252         if ((ks_type == KSTAT_TYPE_INTR) || (ks_type == KSTAT_TYPE_IO))
253                 ASSERT(ks_ndata == 1);
254
255         if (class == NULL)
256                 class = "misc";
257
258         /*
259          * Allocate the main structure. We don't need to keep a copy of
260          * module in here, because it is only used for sysctl node creation
261          * done in this function.
262          */
263         ksp = malloc(sizeof (*ksp), M_KSTAT, M_WAITOK|M_ZERO);
264
265         ksp->ks_crtime = gethrtime();
266         ksp->ks_snaptime = ksp->ks_crtime;
267         ksp->ks_instance = instance;
268         (void) strlcpy(ksp->ks_name, name, KSTAT_STRLEN);
269         (void) strlcpy(ksp->ks_class, class, KSTAT_STRLEN);
270         ksp->ks_type = ks_type;
271         ksp->ks_flags = flags;
272         ksp->ks_update = kstat_default_update;
273
274         mutex_init(&ksp->ks_private_lock, NULL, MUTEX_DEFAULT, NULL);
275         ksp->ks_lock = &ksp->ks_private_lock;
276
277         switch (ksp->ks_type) {
278         case KSTAT_TYPE_RAW:
279                 ksp->ks_ndata = 1;
280                 ksp->ks_data_size = ks_ndata;
281                 break;
282         case KSTAT_TYPE_NAMED:
283                 ksp->ks_ndata = ks_ndata;
284                 ksp->ks_data_size = ks_ndata * sizeof (kstat_named_t);
285                 break;
286         case KSTAT_TYPE_INTR:
287                 ksp->ks_ndata = ks_ndata;
288                 ksp->ks_data_size = ks_ndata * sizeof (kstat_intr_t);
289                 break;
290         case KSTAT_TYPE_IO:
291                 ksp->ks_ndata = ks_ndata;
292                 ksp->ks_data_size = ks_ndata * sizeof (kstat_io_t);
293                 break;
294         case KSTAT_TYPE_TIMER:
295                 ksp->ks_ndata = ks_ndata;
296                 ksp->ks_data_size = ks_ndata * sizeof (kstat_timer_t);
297                 break;
298         default:
299                 panic("Undefined kstat type %d\n", ksp->ks_type);
300         }
301
302         if (ksp->ks_flags & KSTAT_FLAG_VIRTUAL) {
303                 ksp->ks_data = NULL;
304         } else {
305                 ksp->ks_data = kmem_zalloc(ksp->ks_data_size, KM_SLEEP);
306                 if (ksp->ks_data == NULL) {
307                         kmem_free(ksp, sizeof (*ksp));
308                         ksp = NULL;
309                 }
310         }
311
312         /*
313          * Some kstats use a module name like "zfs/poolname" to distinguish a
314          * set of kstats belonging to a specific pool.  Split on '/' to add an
315          * extra node for the pool name if needed.
316          */
317         (void) strlcpy(buf, module, KSTAT_STRLEN);
318         module = buf;
319         pool = strchr(module, '/');
320         if (pool != NULL)
321                 *pool++ = '\0';
322
323         /*
324          * Create sysctl tree for those statistics:
325          *
326          *      kstat.<module>[.<pool>].<class>.<name>
327          */
328         sysctl_ctx_init(&ksp->ks_sysctl_ctx);
329         root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
330             SYSCTL_STATIC_CHILDREN(_kstat), OID_AUTO, module, CTLFLAG_RW, 0,
331             "");
332         if (root == NULL) {
333                 printf("%s: Cannot create kstat.%s tree!\n", __func__, module);
334                 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
335                 free(ksp, M_KSTAT);
336                 return (NULL);
337         }
338         if (pool != NULL) {
339                 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
340                     SYSCTL_CHILDREN(root), OID_AUTO, pool, CTLFLAG_RW, 0, "");
341                 if (root == NULL) {
342                         printf("%s: Cannot create kstat.%s.%s tree!\n",
343                             __func__, module, pool);
344                         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
345                         free(ksp, M_KSTAT);
346                         return (NULL);
347                 }
348         }
349         root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx, SYSCTL_CHILDREN(root),
350             OID_AUTO, class, CTLFLAG_RW, 0, "");
351         if (root == NULL) {
352                 if (pool != NULL)
353                         printf("%s: Cannot create kstat.%s.%s.%s tree!\n",
354                             __func__, module, pool, class);
355                 else
356                         printf("%s: Cannot create kstat.%s.%s tree!\n",
357                             __func__, module, class);
358                 sysctl_ctx_free(&ksp->ks_sysctl_ctx);
359                 free(ksp, M_KSTAT);
360                 return (NULL);
361         }
362         if (ksp->ks_type == KSTAT_TYPE_NAMED) {
363                 root = SYSCTL_ADD_NODE(&ksp->ks_sysctl_ctx,
364                     SYSCTL_CHILDREN(root),
365                     OID_AUTO, name, CTLFLAG_RW, 0, "");
366                 if (root == NULL) {
367                         if (pool != NULL)
368                                 printf("%s: Cannot create kstat.%s.%s.%s.%s "
369                                     "tree!\n", __func__, module, pool, class,
370                                     name);
371                         else
372                                 printf("%s: Cannot create kstat.%s.%s.%s "
373                                     "tree!\n", __func__, module, class, name);
374                         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
375                         free(ksp, M_KSTAT);
376                         return (NULL);
377                 }
378
379         }
380         ksp->ks_sysctl_root = root;
381
382         return (ksp);
383 }
384
385 static void
386 kstat_install_named(kstat_t *ksp)
387 {
388         kstat_named_t *ksent;
389         char *namelast;
390         int typelast;
391
392         ksent = ksp->ks_data;
393
394         VERIFY((ksp->ks_flags & KSTAT_FLAG_VIRTUAL) || ksent != NULL);
395
396         typelast = 0;
397         namelast = NULL;
398
399         for (int i = 0; i < ksp->ks_ndata; i++, ksent++) {
400                 if (ksent->data_type != 0) {
401                         typelast = ksent->data_type;
402                         namelast = ksent->name;
403                 }
404                 switch (typelast) {
405                 case KSTAT_DATA_CHAR:
406                         /* Not Implemented */
407                         break;
408                 case KSTAT_DATA_INT32:
409                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
410                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
411                             OID_AUTO, namelast,
412                             CTLTYPE_S32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
413                             ksp, i, kstat_sysctl, "I", namelast);
414                         break;
415                 case KSTAT_DATA_UINT32:
416                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
417                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
418                             OID_AUTO, namelast,
419                             CTLTYPE_U32 | CTLFLAG_RD | CTLFLAG_MPSAFE,
420                             ksp, i, kstat_sysctl, "IU", namelast);
421                         break;
422                 case KSTAT_DATA_INT64:
423                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
424                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
425                             OID_AUTO, namelast,
426                             CTLTYPE_S64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
427                             ksp, i, kstat_sysctl, "Q", namelast);
428                         break;
429                 case KSTAT_DATA_UINT64:
430                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
431                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
432                             OID_AUTO, namelast,
433                             CTLTYPE_U64 | CTLFLAG_RD | CTLFLAG_MPSAFE,
434                             ksp, i, kstat_sysctl, "QU", namelast);
435                         break;
436                 case KSTAT_DATA_LONG:
437                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
438                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
439                             OID_AUTO, namelast,
440                             CTLTYPE_LONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
441                             ksp, i, kstat_sysctl, "L", namelast);
442                         break;
443                 case KSTAT_DATA_ULONG:
444                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
445                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
446                             OID_AUTO, namelast,
447                             CTLTYPE_ULONG | CTLFLAG_RD | CTLFLAG_MPSAFE,
448                             ksp, i, kstat_sysctl, "LU", namelast);
449                         break;
450                 case KSTAT_DATA_STRING:
451                         SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
452                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
453                             OID_AUTO, namelast,
454                             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
455                             ksp, i, kstat_sysctl_string, "A", namelast);
456                         break;
457                 default:
458                         panic("unsupported type: %d", typelast);
459                 }
460         }
461 }
462
463 void
464 kstat_install(kstat_t *ksp)
465 {
466         struct sysctl_oid *root;
467
468         if (ksp->ks_ndata == UINT32_MAX)
469                 VERIFY(ksp->ks_type == KSTAT_TYPE_RAW);
470
471         switch (ksp->ks_type) {
472         case KSTAT_TYPE_NAMED:
473                 return (kstat_install_named(ksp));
474         case KSTAT_TYPE_RAW:
475                 if (ksp->ks_raw_ops.data) {
476                         root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
477                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
478                             OID_AUTO, ksp->ks_name,
479                             CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
480                             ksp, 0, kstat_sysctl_raw, "A", ksp->ks_name);
481                 } else {
482                         root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
483                             SYSCTL_CHILDREN(ksp->ks_sysctl_root),
484                             OID_AUTO, ksp->ks_name,
485                             CTLTYPE_OPAQUE | CTLFLAG_RD | CTLFLAG_MPSAFE,
486                             ksp, 0, kstat_sysctl_raw, "", ksp->ks_name);
487                 }
488                 break;
489         case KSTAT_TYPE_IO:
490                 root = SYSCTL_ADD_PROC(&ksp->ks_sysctl_ctx,
491                     SYSCTL_CHILDREN(ksp->ks_sysctl_root),
492                     OID_AUTO, ksp->ks_name,
493                     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
494                     ksp, 0, kstat_sysctl_io, "A", ksp->ks_name);
495                 break;
496         case KSTAT_TYPE_TIMER:
497         case KSTAT_TYPE_INTR:
498         default:
499                 panic("unsupported kstat type %d\n", ksp->ks_type);
500         }
501         VERIFY(root != NULL);
502         ksp->ks_sysctl_root = root;
503 }
504
505 void
506 kstat_delete(kstat_t *ksp)
507 {
508
509         sysctl_ctx_free(&ksp->ks_sysctl_ctx);
510         ksp->ks_lock = NULL;
511         mutex_destroy(&ksp->ks_private_lock);
512         free(ksp, M_KSTAT);
513 }
514
515 void
516 kstat_waitq_enter(kstat_io_t *kiop)
517 {
518         hrtime_t new, delta;
519         ulong_t wcnt;
520
521         new = gethrtime();
522         delta = new - kiop->wlastupdate;
523         kiop->wlastupdate = new;
524         wcnt = kiop->wcnt++;
525         if (wcnt != 0) {
526                 kiop->wlentime += delta * wcnt;
527                 kiop->wtime += delta;
528         }
529 }
530
531 void
532 kstat_waitq_exit(kstat_io_t *kiop)
533 {
534         hrtime_t new, delta;
535         ulong_t wcnt;
536
537         new = gethrtime();
538         delta = new - kiop->wlastupdate;
539         kiop->wlastupdate = new;
540         wcnt = kiop->wcnt--;
541         ASSERT((int)wcnt > 0);
542         kiop->wlentime += delta * wcnt;
543         kiop->wtime += delta;
544 }
545
546 void
547 kstat_runq_enter(kstat_io_t *kiop)
548 {
549         hrtime_t new, delta;
550         ulong_t rcnt;
551
552         new = gethrtime();
553         delta = new - kiop->rlastupdate;
554         kiop->rlastupdate = new;
555         rcnt = kiop->rcnt++;
556         if (rcnt != 0) {
557                 kiop->rlentime += delta * rcnt;
558                 kiop->rtime += delta;
559         }
560 }
561
562 void
563 kstat_runq_exit(kstat_io_t *kiop)
564 {
565         hrtime_t new, delta;
566         ulong_t rcnt;
567
568         new = gethrtime();
569         delta = new - kiop->rlastupdate;
570         kiop->rlastupdate = new;
571         rcnt = kiop->rcnt--;
572         ASSERT((int)rcnt > 0);
573         kiop->rlentime += delta * rcnt;
574         kiop->rtime += delta;
575 }