1 /*****************************************************************************\
2 * Copyright (C) 2007-2010 Lawrence Livermore National Security, LLC.
3 * Copyright (C) 2007 The Regents of the University of California.
4 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER).
5 * Written by Brian Behlendorf <behlendorf1@llnl.gov>.
8 * This file is part of the SPL, Solaris Porting Layer.
9 * For details, see <http://zfsonlinux.org/>.
11 * The SPL is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the
13 * Free Software Foundation; either version 2 of the License, or (at your
14 * option) any later version.
16 * The SPL is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * You should have received a copy of the GNU General Public License along
22 * with the SPL. If not, see <http://www.gnu.org/licenses/>.
23 *****************************************************************************
24 * Solaris Porting Layer (SPL) Kstat Implementation.
25 \*****************************************************************************/
27 #include <linux/seq_file.h>
28 #include <sys/kstat.h>
30 #include <sys/cmn_err.h>
33 #define PDE_DATA(x) (PDE(x)->data)
36 static kmutex_t kstat_module_lock
;
37 static struct list_head kstat_module_list
;
38 static kid_t kstat_id
;
41 kstat_resize_raw(kstat_t
*ksp
)
43 if (ksp
->ks_raw_bufsize
== KSTAT_RAW_MAX
)
46 vmem_free(ksp
->ks_raw_buf
, ksp
->ks_raw_bufsize
);
47 ksp
->ks_raw_bufsize
= MIN(ksp
->ks_raw_bufsize
* 2, KSTAT_RAW_MAX
);
48 ksp
->ks_raw_buf
= vmem_alloc(ksp
->ks_raw_bufsize
, KM_SLEEP
);
54 kstat_waitq_enter(kstat_io_t
*kiop
)
60 delta
= new - kiop
->wlastupdate
;
61 kiop
->wlastupdate
= new;
64 kiop
->wlentime
+= delta
* wcnt
;
68 EXPORT_SYMBOL(kstat_waitq_enter
);
71 kstat_waitq_exit(kstat_io_t
*kiop
)
77 delta
= new - kiop
->wlastupdate
;
78 kiop
->wlastupdate
= new;
80 ASSERT((int)wcnt
> 0);
81 kiop
->wlentime
+= delta
* wcnt
;
84 EXPORT_SYMBOL(kstat_waitq_exit
);
87 kstat_runq_enter(kstat_io_t
*kiop
)
93 delta
= new - kiop
->rlastupdate
;
94 kiop
->rlastupdate
= new;
97 kiop
->rlentime
+= delta
* rcnt
;
101 EXPORT_SYMBOL(kstat_runq_enter
);
104 kstat_runq_exit(kstat_io_t
*kiop
)
110 delta
= new - kiop
->rlastupdate
;
111 kiop
->rlastupdate
= new;
113 ASSERT((int)rcnt
> 0);
114 kiop
->rlentime
+= delta
* rcnt
;
115 kiop
->rtime
+= delta
;
117 EXPORT_SYMBOL(kstat_runq_exit
);
120 kstat_seq_show_headers(struct seq_file
*f
)
122 kstat_t
*ksp
= (kstat_t
*)f
->private;
125 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
127 seq_printf(f
, "%d %d 0x%02x %d %d %lld %lld\n",
128 ksp
->ks_kid
, ksp
->ks_type
, ksp
->ks_flags
,
129 ksp
->ks_ndata
, (int)ksp
->ks_data_size
,
130 ksp
->ks_crtime
, ksp
->ks_snaptime
);
132 switch (ksp
->ks_type
) {
135 if (ksp
->ks_raw_ops
.headers
) {
136 rc
= ksp
->ks_raw_ops
.headers(
137 ksp
->ks_raw_buf
, ksp
->ks_raw_bufsize
);
138 if (rc
== ENOMEM
&& !kstat_resize_raw(ksp
))
141 seq_puts(f
, ksp
->ks_raw_buf
);
143 seq_printf(f
, "raw data\n");
146 case KSTAT_TYPE_NAMED
:
147 seq_printf(f
, "%-31s %-4s %s\n",
148 "name", "type", "data");
150 case KSTAT_TYPE_INTR
:
151 seq_printf(f
, "%-8s %-8s %-8s %-8s %-8s\n",
152 "hard", "soft", "watchdog",
153 "spurious", "multsvc");
157 "%-8s %-8s %-8s %-8s %-8s %-8s "
158 "%-8s %-8s %-8s %-8s %-8s %-8s\n",
159 "nread", "nwritten", "reads", "writes",
160 "wtime", "wlentime", "wupdate",
161 "rtime", "rlentime", "rupdate",
164 case KSTAT_TYPE_TIMER
:
167 "%-8s %-8s %-8s %-8s %-8s\n",
168 "name", "events", "elapsed",
169 "min", "max", "start", "stop");
172 PANIC("Undefined kstat type %d\n", ksp
->ks_type
);
179 kstat_seq_show_raw(struct seq_file
*f
, unsigned char *p
, int l
)
184 seq_printf(f
, "%03x:", i
);
186 for (j
= 0; j
< 16; j
++) {
187 if (i
* 16 + j
>= l
) {
192 seq_printf(f
, " %02x", (unsigned char)p
[i
* 16 + j
]);
201 kstat_seq_show_named(struct seq_file
*f
, kstat_named_t
*knp
)
203 seq_printf(f
, "%-31s %-4d ", knp
->name
, knp
->data_type
);
205 switch (knp
->data_type
) {
206 case KSTAT_DATA_CHAR
:
207 knp
->value
.c
[15] = '\0'; /* NULL terminate */
208 seq_printf(f
, "%-16s", knp
->value
.c
);
210 /* XXX - We need to be more careful able what tokens are
211 * used for each arch, for now this is correct for x86_64.
213 case KSTAT_DATA_INT32
:
214 seq_printf(f
, "%d", knp
->value
.i32
);
216 case KSTAT_DATA_UINT32
:
217 seq_printf(f
, "%u", knp
->value
.ui32
);
219 case KSTAT_DATA_INT64
:
220 seq_printf(f
, "%lld", (signed long long)knp
->value
.i64
);
222 case KSTAT_DATA_UINT64
:
223 seq_printf(f
, "%llu", (unsigned long long)knp
->value
.ui64
);
225 case KSTAT_DATA_LONG
:
226 seq_printf(f
, "%ld", knp
->value
.l
);
228 case KSTAT_DATA_ULONG
:
229 seq_printf(f
, "%lu", knp
->value
.ul
);
231 case KSTAT_DATA_STRING
:
232 KSTAT_NAMED_STR_PTR(knp
)
233 [KSTAT_NAMED_STR_BUFLEN(knp
)-1] = '\0';
234 seq_printf(f
, "%s", KSTAT_NAMED_STR_PTR(knp
));
237 PANIC("Undefined kstat data type %d\n", knp
->data_type
);
246 kstat_seq_show_intr(struct seq_file
*f
, kstat_intr_t
*kip
)
248 seq_printf(f
, "%-8u %-8u %-8u %-8u %-8u\n",
249 kip
->intrs
[KSTAT_INTR_HARD
],
250 kip
->intrs
[KSTAT_INTR_SOFT
],
251 kip
->intrs
[KSTAT_INTR_WATCHDOG
],
252 kip
->intrs
[KSTAT_INTR_SPURIOUS
],
253 kip
->intrs
[KSTAT_INTR_MULTSVC
]);
259 kstat_seq_show_io(struct seq_file
*f
, kstat_io_t
*kip
)
262 "%-8llu %-8llu %-8u %-8u %-8lld %-8lld "
263 "%-8lld %-8lld %-8lld %-8lld %-8u %-8u\n",
264 kip
->nread
, kip
->nwritten
,
265 kip
->reads
, kip
->writes
,
266 kip
->wtime
, kip
->wlentime
, kip
->wlastupdate
,
267 kip
->rtime
, kip
->wlentime
, kip
->rlastupdate
,
268 kip
->wcnt
, kip
->rcnt
);
274 kstat_seq_show_timer(struct seq_file
*f
, kstat_timer_t
*ktp
)
277 "%-31s %-8llu %-8lld %-8lld %-8lld %-8lld %-8lld\n",
278 ktp
->name
, ktp
->num_events
, ktp
->elapsed_time
,
279 ktp
->min_time
, ktp
->max_time
,
280 ktp
->start_time
, ktp
->stop_time
);
286 kstat_seq_show(struct seq_file
*f
, void *p
)
288 kstat_t
*ksp
= (kstat_t
*)f
->private;
291 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
293 switch (ksp
->ks_type
) {
296 if (ksp
->ks_raw_ops
.data
) {
297 rc
= ksp
->ks_raw_ops
.data(
298 ksp
->ks_raw_buf
, ksp
->ks_raw_bufsize
, p
);
299 if (rc
== ENOMEM
&& !kstat_resize_raw(ksp
))
302 seq_puts(f
, ksp
->ks_raw_buf
);
304 ASSERT(ksp
->ks_ndata
== 1);
305 rc
= kstat_seq_show_raw(f
, ksp
->ks_data
,
309 case KSTAT_TYPE_NAMED
:
310 rc
= kstat_seq_show_named(f
, (kstat_named_t
*)p
);
312 case KSTAT_TYPE_INTR
:
313 rc
= kstat_seq_show_intr(f
, (kstat_intr_t
*)p
);
316 rc
= kstat_seq_show_io(f
, (kstat_io_t
*)p
);
318 case KSTAT_TYPE_TIMER
:
319 rc
= kstat_seq_show_timer(f
, (kstat_timer_t
*)p
);
322 PANIC("Undefined kstat type %d\n", ksp
->ks_type
);
329 kstat_default_update(kstat_t
*ksp
, int rw
)
333 if (rw
== KSTAT_WRITE
)
340 kstat_seq_data_addr(kstat_t
*ksp
, loff_t n
)
344 switch (ksp
->ks_type
) {
346 if (ksp
->ks_raw_ops
.addr
)
347 rc
= ksp
->ks_raw_ops
.addr(ksp
, n
);
351 case KSTAT_TYPE_NAMED
:
352 rc
= ksp
->ks_data
+ n
* sizeof(kstat_named_t
);
354 case KSTAT_TYPE_INTR
:
355 rc
= ksp
->ks_data
+ n
* sizeof(kstat_intr_t
);
358 rc
= ksp
->ks_data
+ n
* sizeof(kstat_io_t
);
360 case KSTAT_TYPE_TIMER
:
361 rc
= ksp
->ks_data
+ n
* sizeof(kstat_timer_t
);
364 PANIC("Undefined kstat type %d\n", ksp
->ks_type
);
371 kstat_seq_start(struct seq_file
*f
, loff_t
*pos
)
374 kstat_t
*ksp
= (kstat_t
*)f
->private;
375 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
377 mutex_enter(ksp
->ks_lock
);
379 if (ksp
->ks_type
== KSTAT_TYPE_RAW
) {
380 ksp
->ks_raw_bufsize
= PAGE_SIZE
;
381 ksp
->ks_raw_buf
= vmem_alloc(ksp
->ks_raw_bufsize
, KM_SLEEP
);
384 /* Dynamically update kstat, on error existing kstats are used */
385 (void) ksp
->ks_update(ksp
, KSTAT_READ
);
387 ksp
->ks_snaptime
= gethrtime();
389 if (!n
&& kstat_seq_show_headers(f
))
392 if (n
>= ksp
->ks_ndata
)
395 return (kstat_seq_data_addr(ksp
, n
));
399 kstat_seq_next(struct seq_file
*f
, void *p
, loff_t
*pos
)
401 kstat_t
*ksp
= (kstat_t
*)f
->private;
402 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
405 if (*pos
>= ksp
->ks_ndata
)
408 return (kstat_seq_data_addr(ksp
, *pos
));
412 kstat_seq_stop(struct seq_file
*f
, void *v
)
414 kstat_t
*ksp
= (kstat_t
*)f
->private;
415 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
417 if (ksp
->ks_type
== KSTAT_TYPE_RAW
)
418 vmem_free(ksp
->ks_raw_buf
, ksp
->ks_raw_bufsize
);
420 mutex_exit(ksp
->ks_lock
);
423 static struct seq_operations kstat_seq_ops
= {
424 .show
= kstat_seq_show
,
425 .start
= kstat_seq_start
,
426 .next
= kstat_seq_next
,
427 .stop
= kstat_seq_stop
,
430 static kstat_module_t
*
431 kstat_find_module(char *name
)
433 kstat_module_t
*module
;
435 list_for_each_entry(module
, &kstat_module_list
, ksm_module_list
)
436 if (strncmp(name
, module
->ksm_name
, KSTAT_STRLEN
) == 0)
442 static kstat_module_t
*
443 kstat_create_module(char *name
)
445 kstat_module_t
*module
;
446 struct proc_dir_entry
*pde
;
448 pde
= proc_mkdir(name
, proc_spl_kstat
);
452 module
= kmem_alloc(sizeof (kstat_module_t
), KM_SLEEP
);
453 module
->ksm_proc
= pde
;
454 strlcpy(module
->ksm_name
, name
, KSTAT_STRLEN
+1);
455 INIT_LIST_HEAD(&module
->ksm_kstat_list
);
456 list_add_tail(&module
->ksm_module_list
, &kstat_module_list
);
463 kstat_delete_module(kstat_module_t
*module
)
465 ASSERT(list_empty(&module
->ksm_kstat_list
));
466 remove_proc_entry(module
->ksm_name
, proc_spl_kstat
);
467 list_del(&module
->ksm_module_list
);
468 kmem_free(module
, sizeof(kstat_module_t
));
472 proc_kstat_open(struct inode
*inode
, struct file
*filp
)
477 rc
= seq_open(filp
, &kstat_seq_ops
);
481 f
= filp
->private_data
;
482 f
->private = PDE_DATA(inode
);
488 proc_kstat_write(struct file
*filp
, const char __user
*buf
,
489 size_t len
, loff_t
*ppos
)
491 struct seq_file
*f
= filp
->private_data
;
492 kstat_t
*ksp
= f
->private;
495 ASSERT(ksp
->ks_magic
== KS_MAGIC
);
497 mutex_enter(ksp
->ks_lock
);
498 rc
= ksp
->ks_update(ksp
, KSTAT_WRITE
);
499 mutex_exit(ksp
->ks_lock
);
508 static struct file_operations proc_kstat_operations
= {
509 .open
= proc_kstat_open
,
510 .write
= proc_kstat_write
,
513 .release
= seq_release
,
517 __kstat_set_raw_ops(kstat_t
*ksp
,
518 int (*headers
)(char *buf
, size_t size
),
519 int (*data
)(char *buf
, size_t size
, void *data
),
520 void *(*addr
)(kstat_t
*ksp
, loff_t index
))
522 ksp
->ks_raw_ops
.headers
= headers
;
523 ksp
->ks_raw_ops
.data
= data
;
524 ksp
->ks_raw_ops
.addr
= addr
;
526 EXPORT_SYMBOL(__kstat_set_raw_ops
);
529 __kstat_create(const char *ks_module
, int ks_instance
, const char *ks_name
,
530 const char *ks_class
, uchar_t ks_type
, uint_t ks_ndata
,
536 ASSERT(ks_instance
== 0);
538 ASSERT(!(ks_flags
& KSTAT_FLAG_UNSUPPORTED
));
540 if ((ks_type
== KSTAT_TYPE_INTR
) || (ks_type
== KSTAT_TYPE_IO
))
541 ASSERT(ks_ndata
== 1);
543 ksp
= kmem_zalloc(sizeof(*ksp
), KM_SLEEP
);
547 mutex_enter(&kstat_module_lock
);
548 ksp
->ks_kid
= kstat_id
;
550 mutex_exit(&kstat_module_lock
);
552 ksp
->ks_magic
= KS_MAGIC
;
553 mutex_init(&ksp
->ks_private_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
554 ksp
->ks_lock
= &ksp
->ks_private_lock
;
555 INIT_LIST_HEAD(&ksp
->ks_list
);
557 ksp
->ks_crtime
= gethrtime();
558 ksp
->ks_snaptime
= ksp
->ks_crtime
;
559 strncpy(ksp
->ks_module
, ks_module
, KSTAT_STRLEN
);
560 ksp
->ks_instance
= ks_instance
;
561 strncpy(ksp
->ks_name
, ks_name
, KSTAT_STRLEN
);
562 strncpy(ksp
->ks_class
, ks_class
, KSTAT_STRLEN
);
563 ksp
->ks_type
= ks_type
;
564 ksp
->ks_flags
= ks_flags
;
565 ksp
->ks_update
= kstat_default_update
;
566 ksp
->ks_private
= NULL
;
567 ksp
->ks_raw_ops
.headers
= NULL
;
568 ksp
->ks_raw_ops
.data
= NULL
;
569 ksp
->ks_raw_ops
.addr
= NULL
;
570 ksp
->ks_raw_buf
= NULL
;
571 ksp
->ks_raw_bufsize
= 0;
573 switch (ksp
->ks_type
) {
576 ksp
->ks_data_size
= ks_ndata
;
578 case KSTAT_TYPE_NAMED
:
579 ksp
->ks_ndata
= ks_ndata
;
580 ksp
->ks_data_size
= ks_ndata
* sizeof(kstat_named_t
);
582 case KSTAT_TYPE_INTR
:
583 ksp
->ks_ndata
= ks_ndata
;
584 ksp
->ks_data_size
= ks_ndata
* sizeof(kstat_intr_t
);
587 ksp
->ks_ndata
= ks_ndata
;
588 ksp
->ks_data_size
= ks_ndata
* sizeof(kstat_io_t
);
590 case KSTAT_TYPE_TIMER
:
591 ksp
->ks_ndata
= ks_ndata
;
592 ksp
->ks_data_size
= ks_ndata
* sizeof(kstat_timer_t
);
595 PANIC("Undefined kstat type %d\n", ksp
->ks_type
);
598 if (ksp
->ks_flags
& KSTAT_FLAG_VIRTUAL
) {
601 ksp
->ks_data
= kmem_zalloc(ksp
->ks_data_size
, KM_SLEEP
);
602 if (ksp
->ks_data
== NULL
) {
603 kmem_free(ksp
, sizeof(*ksp
));
610 EXPORT_SYMBOL(__kstat_create
);
613 kstat_detect_collision(kstat_t
*ksp
)
615 kstat_module_t
*module
;
617 char parent
[KSTAT_STRLEN
+1];
620 (void) strlcpy(parent
, ksp
->ks_module
, sizeof(parent
));
622 if ((cp
= strrchr(parent
, '/')) == NULL
)
626 if ((module
= kstat_find_module(parent
)) != NULL
) {
627 list_for_each_entry(tmp
, &module
->ksm_kstat_list
, ks_list
)
628 if (strncmp(tmp
->ks_name
, cp
+1, KSTAT_STRLEN
) == 0)
636 __kstat_install(kstat_t
*ksp
)
638 kstat_module_t
*module
;
643 mutex_enter(&kstat_module_lock
);
645 module
= kstat_find_module(ksp
->ks_module
);
646 if (module
== NULL
) {
647 if (kstat_detect_collision(ksp
) != 0) {
648 cmn_err(CE_WARN
, "kstat_create('%s', '%s'): namespace" \
649 " collision", ksp
->ks_module
, ksp
->ks_name
);
652 module
= kstat_create_module(ksp
->ks_module
);
658 * Only one entry by this name per-module, on failure the module
659 * shouldn't be deleted because we know it has at least one entry.
661 list_for_each_entry(tmp
, &module
->ksm_kstat_list
, ks_list
)
662 if (strncmp(tmp
->ks_name
, ksp
->ks_name
, KSTAT_STRLEN
) == 0)
665 list_add_tail(&ksp
->ks_list
, &module
->ksm_kstat_list
);
667 mutex_enter(ksp
->ks_lock
);
668 ksp
->ks_owner
= module
;
669 ksp
->ks_proc
= proc_create_data(ksp
->ks_name
, 0644,
670 module
->ksm_proc
, &proc_kstat_operations
, (void *)ksp
);
671 if (ksp
->ks_proc
== NULL
) {
672 list_del_init(&ksp
->ks_list
);
673 if (list_empty(&module
->ksm_kstat_list
))
674 kstat_delete_module(module
);
676 mutex_exit(ksp
->ks_lock
);
678 mutex_exit(&kstat_module_lock
);
680 EXPORT_SYMBOL(__kstat_install
);
683 __kstat_delete(kstat_t
*ksp
)
685 kstat_module_t
*module
= ksp
->ks_owner
;
687 mutex_enter(&kstat_module_lock
);
688 list_del_init(&ksp
->ks_list
);
689 mutex_exit(&kstat_module_lock
);
692 remove_proc_entry(ksp
->ks_name
, module
->ksm_proc
);
694 /* Remove top level module directory if it's empty */
695 if (list_empty(&module
->ksm_kstat_list
))
696 kstat_delete_module(module
);
699 if (!(ksp
->ks_flags
& KSTAT_FLAG_VIRTUAL
))
700 kmem_free(ksp
->ks_data
, ksp
->ks_data_size
);
703 mutex_destroy(&ksp
->ks_private_lock
);
704 kmem_free(ksp
, sizeof(*ksp
));
708 EXPORT_SYMBOL(__kstat_delete
);
713 mutex_init(&kstat_module_lock
, NULL
, MUTEX_DEFAULT
, NULL
);
714 INIT_LIST_HEAD(&kstat_module_list
);
722 ASSERT(list_empty(&kstat_module_list
));
723 mutex_destroy(&kstat_module_lock
);