1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
|
/* $OpenBSD: kstat.c,v 1.4 2024/09/04 07:54:52 mglocker Exp $ */
/*
* Copyright (c) 2020 David Gwynne <dlg@openbsd.org>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/types.h>
#include <sys/malloc.h>
#include <sys/pool.h>
#include <sys/time.h>
/* for kstat_set_cpu */
#include <sys/proc.h>
#include <sys/sched.h>
#include <sys/kstat.h>
RBT_HEAD(kstat_id_tree, kstat);
static inline int
kstat_id_cmp(const struct kstat *a, const struct kstat *b)
{
if (a->ks_id > b->ks_id)
return (1);
if (a->ks_id < b->ks_id)
return (-1);
return (0);
}
RBT_PROTOTYPE(kstat_id_tree, kstat, ks_id_entry, kstat_id_cmp);
RBT_HEAD(kstat_pv_tree, kstat);
static inline int
kstat_pv_cmp(const struct kstat *a, const struct kstat *b)
{
int rv;
rv = strcmp(a->ks_provider, b->ks_provider);
if (rv != 0)
return (rv);
if (a->ks_instance > b->ks_instance)
return (1);
if (a->ks_instance < b->ks_instance)
return (-1);
rv = strcmp(a->ks_name, b->ks_name);
if (rv != 0)
return (rv);
if (a->ks_unit > b->ks_unit)
return (1);
if (a->ks_unit < b->ks_unit)
return (-1);
return (0);
}
RBT_PROTOTYPE(kstat_pv_tree, kstat, ks_pv_entry, kstat_pv_cmp);
RBT_HEAD(kstat_nm_tree, kstat);
static inline int
kstat_nm_cmp(const struct kstat *a, const struct kstat *b)
{
int rv;
rv = strcmp(a->ks_name, b->ks_name);
if (rv != 0)
return (rv);
if (a->ks_unit > b->ks_unit)
return (1);
if (a->ks_unit < b->ks_unit)
return (-1);
rv = strcmp(a->ks_provider, b->ks_provider);
if (rv != 0)
return (rv);
if (a->ks_instance > b->ks_instance)
return (1);
if (a->ks_instance < b->ks_instance)
return (-1);
return (0);
}
RBT_PROTOTYPE(kstat_nm_tree, kstat, ks_nm_entry, kstat_nm_cmp);
struct kstat_lock_ops {
void (*enter)(void *);
void (*leave)(void *);
};
#define kstat_enter(_ks) (_ks)->ks_lock_ops->enter((_ks)->ks_lock)
#define kstat_leave(_ks) (_ks)->ks_lock_ops->leave((_ks)->ks_lock)
const struct kstat_lock_ops kstat_rlock_ops = {
(void (*)(void *))rw_enter_read,
(void (*)(void *))rw_exit_read,
};
const struct kstat_lock_ops kstat_wlock_ops = {
(void (*)(void *))rw_enter_write,
(void (*)(void *))rw_exit_write,
};
const struct kstat_lock_ops kstat_mutex_ops = {
(void (*)(void *))mtx_enter,
(void (*)(void *))mtx_leave,
};
void kstat_cpu_enter(void *);
void kstat_cpu_leave(void *);
const struct kstat_lock_ops kstat_cpu_ops = {
kstat_cpu_enter,
kstat_cpu_leave,
};
struct rwlock kstat_lock = RWLOCK_INITIALIZER("kstat");
/*
* The global state is versioned so changes to the set of kstats
* can be detected. This is an int so it can be read atomically on
* any arch, which is a ridiculous optimisation, really.
*/
unsigned int kstat_version = 0;
/*
* kstat structures have a unique identifier so they can be found
* quickly. Identifiers are 64bit in the hope that it won't wrap
* during the runtime of a system. The identifiers start at 1 so that
* 0 can be used as the first value for userland to iterate with.
*/
uint64_t kstat_next_id = 1;
struct kstat_id_tree kstat_id_tree = RBT_INITIALIZER();
struct kstat_pv_tree kstat_pv_tree = RBT_INITIALIZER();
struct kstat_nm_tree kstat_nm_tree = RBT_INITIALIZER();
struct pool kstat_pool;
struct rwlock kstat_default_lock = RWLOCK_INITIALIZER("kstatlk");
int kstat_read(struct kstat *);
int kstat_copy(struct kstat *, void *);
int
kstatattach(int num)
{
/* XXX install system stats here */
return (0);
}
int
kstatopen(dev_t dev, int flag, int mode, struct proc *p)
{
return (0);
}
int
kstatclose(dev_t dev, int flag, int mode, struct proc *p)
{
return (0);
}
int
kstatioc_enter(struct kstat_req *ksreq)
{
int error;
error = rw_enter(&kstat_lock, RW_READ | RW_INTR);
if (error != 0)
return (error);
if (!ISSET(ksreq->ks_rflags, KSTATIOC_F_IGNVER) &&
ksreq->ks_version != kstat_version) {
error = EINVAL;
goto error;
}
return (0);
error:
rw_exit(&kstat_lock);
return (error);
}
int
kstatioc_leave(struct kstat_req *ksreq, struct kstat *ks)
{
void *buf = NULL;
size_t klen = 0, ulen = 0;
struct timespec updated;
int error = 0;
if (ks == NULL) {
error = ENOENT;
goto error;
}
switch (ks->ks_state) {
case KSTAT_S_CREATED:
ksreq->ks_updated = ks->ks_created;
ksreq->ks_interval.tv_sec = 0;
ksreq->ks_interval.tv_nsec = 0;
ksreq->ks_datalen = 0;
ksreq->ks_dataver = 0;
break;
case KSTAT_S_INSTALLED:
ksreq->ks_dataver = ks->ks_dataver;
ksreq->ks_interval = ks->ks_interval;
if (ksreq->ks_data == NULL) {
/* userland doesn't want actual data, so shortcut */
kstat_enter(ks);
ksreq->ks_datalen = ks->ks_datalen;
ksreq->ks_updated = ks->ks_updated;
kstat_leave(ks);
break;
}
klen = ks->ks_datalen; /* KSTAT_F_REALLOC */
buf = malloc(klen, M_TEMP, M_WAITOK|M_CANFAIL);
if (buf == NULL) {
error = ENOMEM;
goto error;
}
kstat_enter(ks);
error = (*ks->ks_read)(ks);
if (error == 0) {
updated = ks->ks_updated;
/* KSTAT_F_REALLOC */
KASSERTMSG(ks->ks_datalen == klen,
"kstat doesn't support resized data yet");
error = (*ks->ks_copy)(ks, buf);
}
kstat_leave(ks);
if (error != 0)
goto error;
ulen = ksreq->ks_datalen;
ksreq->ks_datalen = klen; /* KSTAT_F_REALLOC */
ksreq->ks_updated = updated;
break;
default:
panic("ks %p unexpected state %u", ks, ks->ks_state);
}
ksreq->ks_version = kstat_version;
ksreq->ks_id = ks->ks_id;
if (strlcpy(ksreq->ks_provider, ks->ks_provider,
sizeof(ksreq->ks_provider)) >= sizeof(ksreq->ks_provider))
panic("kstat %p provider string has grown", ks);
ksreq->ks_instance = ks->ks_instance;
if (strlcpy(ksreq->ks_name, ks->ks_name,
sizeof(ksreq->ks_name)) >= sizeof(ksreq->ks_name))
panic("kstat %p name string has grown", ks);
ksreq->ks_unit = ks->ks_unit;
ksreq->ks_created = ks->ks_created;
ksreq->ks_type = ks->ks_type;
ksreq->ks_state = ks->ks_state;
error:
rw_exit(&kstat_lock);
if (buf != NULL) {
if (error == 0)
error = copyout(buf, ksreq->ks_data, min(klen, ulen));
free(buf, M_TEMP, klen);
}
return (error);
}
int
kstatioc_find_id(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_id = ksreq->ks_id;
ks = RBT_FIND(kstat_id_tree, &kstat_id_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioc_nfind_id(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_id = ksreq->ks_id;
ks = RBT_NFIND(kstat_id_tree, &kstat_id_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioc_find_pv(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_provider = ksreq->ks_provider;
key.ks_instance = ksreq->ks_instance;
key.ks_name = ksreq->ks_name;
key.ks_unit = ksreq->ks_unit;
ks = RBT_FIND(kstat_pv_tree, &kstat_pv_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioc_nfind_pv(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_provider = ksreq->ks_provider;
key.ks_instance = ksreq->ks_instance;
key.ks_name = ksreq->ks_name;
key.ks_unit = ksreq->ks_unit;
ks = RBT_NFIND(kstat_pv_tree, &kstat_pv_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioc_find_nm(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_name = ksreq->ks_name;
key.ks_unit = ksreq->ks_unit;
key.ks_provider = ksreq->ks_provider;
key.ks_instance = ksreq->ks_instance;
ks = RBT_FIND(kstat_nm_tree, &kstat_nm_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioc_nfind_nm(struct kstat_req *ksreq)
{
struct kstat *ks, key;
int error;
error = kstatioc_enter(ksreq);
if (error != 0)
return (error);
key.ks_name = ksreq->ks_name;
key.ks_unit = ksreq->ks_unit;
key.ks_provider = ksreq->ks_provider;
key.ks_instance = ksreq->ks_instance;
ks = RBT_NFIND(kstat_nm_tree, &kstat_nm_tree, &key);
return (kstatioc_leave(ksreq, ks));
}
int
kstatioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
{
struct kstat_req *ksreq = (struct kstat_req *)data;
int error = 0;
KERNEL_UNLOCK();
switch (cmd) {
case KSTATIOC_VERSION:
*(unsigned int *)data = kstat_version;
break;
case KSTATIOC_FIND_ID:
error = kstatioc_find_id(ksreq);
break;
case KSTATIOC_NFIND_ID:
error = kstatioc_nfind_id(ksreq);
break;
case KSTATIOC_FIND_PROVIDER:
error = kstatioc_find_pv(ksreq);
break;
case KSTATIOC_NFIND_PROVIDER:
error = kstatioc_nfind_pv(ksreq);
break;
case KSTATIOC_FIND_NAME:
error = kstatioc_find_nm(ksreq);
break;
case KSTATIOC_NFIND_NAME:
error = kstatioc_nfind_nm(ksreq);
break;
default:
error = ENOTTY;
break;
}
KERNEL_LOCK();
return (error);
}
void
kstat_init(void)
{
static int initialized = 0;
if (initialized)
return;
pool_init(&kstat_pool, sizeof(struct kstat), 0, IPL_NONE,
PR_WAITOK | PR_RWLOCK, "kstatmem", NULL);
initialized = 1;
}
int
kstat_strcheck(const char *str)
{
size_t i, l;
l = strlen(str);
if (l == 0 || l >= KSTAT_STRLEN)
return (-1);
for (i = 0; i < l; i++) {
int ch = str[i];
if (ch >= 'a' && ch <= 'z')
continue;
if (ch >= 'A' && ch <= 'Z')
continue;
if (ch >= '0' && ch <= '9')
continue;
switch (ch) {
case '-':
case '_':
case '.':
break;
default:
return (-1);
}
}
return (0);
}
struct kstat *
kstat_create(const char *provider, unsigned int instance,
const char *name, unsigned int unit,
unsigned int type, unsigned int flags)
{
struct kstat *ks, *oks;
if (kstat_strcheck(provider) == -1)
panic("invalid provider string");
if (kstat_strcheck(name) == -1)
panic("invalid name string");
kstat_init();
ks = pool_get(&kstat_pool, PR_WAITOK|PR_ZERO);
ks->ks_provider = provider;
ks->ks_instance = instance;
ks->ks_name = name;
ks->ks_unit = unit;
ks->ks_flags = flags;
ks->ks_type = type;
ks->ks_state = KSTAT_S_CREATED;
getnanouptime(&ks->ks_created);
ks->ks_updated = ks->ks_created;
ks->ks_lock = &kstat_default_lock;
ks->ks_lock_ops = &kstat_wlock_ops;
ks->ks_read = kstat_read;
ks->ks_copy = kstat_copy;
rw_enter_write(&kstat_lock);
ks->ks_id = kstat_next_id;
oks = RBT_INSERT(kstat_pv_tree, &kstat_pv_tree, ks);
if (oks == NULL) {
/* commit */
kstat_next_id++;
kstat_version++;
oks = RBT_INSERT(kstat_nm_tree, &kstat_nm_tree, ks);
if (oks != NULL)
panic("kstat name collision! (%llu)", ks->ks_id);
oks = RBT_INSERT(kstat_id_tree, &kstat_id_tree, ks);
if (oks != NULL)
panic("kstat id collision! (%llu)", ks->ks_id);
}
rw_exit_write(&kstat_lock);
if (oks != NULL) {
pool_put(&kstat_pool, ks);
return (NULL);
}
return (ks);
}
void
kstat_set_rlock(struct kstat *ks, struct rwlock *rwl)
{
KASSERT(ks->ks_state == KSTAT_S_CREATED);
ks->ks_lock = rwl;
ks->ks_lock_ops = &kstat_rlock_ops;
}
void
kstat_set_wlock(struct kstat *ks, struct rwlock *rwl)
{
KASSERT(ks->ks_state == KSTAT_S_CREATED);
ks->ks_lock = rwl;
ks->ks_lock_ops = &kstat_wlock_ops;
}
void
kstat_set_mutex(struct kstat *ks, struct mutex *mtx)
{
KASSERT(ks->ks_state == KSTAT_S_CREATED);
ks->ks_lock = mtx;
ks->ks_lock_ops = &kstat_mutex_ops;
}
void
kstat_cpu_enter(void *p)
{
struct cpu_info *ci = p;
sched_peg_curproc(ci);
}
void
kstat_cpu_leave(void *p)
{
sched_unpeg_curproc();
}
void
kstat_set_cpu(struct kstat *ks, struct cpu_info *ci)
{
KASSERT(ks->ks_state == KSTAT_S_CREATED);
ks->ks_lock = ci;
ks->ks_lock_ops = &kstat_cpu_ops;
}
int
kstat_read_nop(struct kstat *ks)
{
return (0);
}
void
kstat_install(struct kstat *ks)
{
if (!ISSET(ks->ks_flags, KSTAT_F_REALLOC)) {
KASSERTMSG(ks->ks_copy != NULL || ks->ks_data != NULL,
"kstat %p %s:%u:%s:%u must provide ks_copy or ks_data", ks,
ks->ks_provider, ks->ks_instance, ks->ks_name, ks->ks_unit);
KASSERT(ks->ks_datalen > 0);
}
rw_enter_write(&kstat_lock);
ks->ks_state = KSTAT_S_INSTALLED;
rw_exit_write(&kstat_lock);
}
void
kstat_remove(struct kstat *ks)
{
rw_enter_write(&kstat_lock);
KASSERTMSG(ks->ks_state == KSTAT_S_INSTALLED,
"kstat %p %s:%u:%s:%u is not installed", ks,
ks->ks_provider, ks->ks_instance, ks->ks_name, ks->ks_unit);
ks->ks_state = KSTAT_S_CREATED;
rw_exit_write(&kstat_lock);
}
void
kstat_destroy(struct kstat *ks)
{
rw_enter_write(&kstat_lock);
RBT_REMOVE(kstat_id_tree, &kstat_id_tree, ks);
RBT_REMOVE(kstat_pv_tree, &kstat_pv_tree, ks);
RBT_REMOVE(kstat_nm_tree, &kstat_nm_tree, ks);
kstat_version++;
rw_exit_write(&kstat_lock);
pool_put(&kstat_pool, ks);
}
int
kstat_read(struct kstat *ks)
{
getnanouptime(&ks->ks_updated);
return (0);
}
int
kstat_copy(struct kstat *ks, void *buf)
{
memcpy(buf, ks->ks_data, ks->ks_datalen);
return (0);
}
RBT_GENERATE(kstat_id_tree, kstat, ks_id_entry, kstat_id_cmp);
RBT_GENERATE(kstat_pv_tree, kstat, ks_pv_entry, kstat_pv_cmp);
RBT_GENERATE(kstat_nm_tree, kstat, ks_nm_entry, kstat_nm_cmp);
void
kstat_kv_init(struct kstat_kv *kv, const char *name, enum kstat_kv_type type)
{
memset(kv, 0, sizeof(*kv));
strlcpy(kv->kv_key, name, sizeof(kv->kv_key)); /* XXX truncated? */
kv->kv_type = type;
kv->kv_unit = KSTAT_KV_U_NONE;
}
void
kstat_kv_unit_init(struct kstat_kv *kv, const char *name,
enum kstat_kv_type type, enum kstat_kv_unit unit)
{
switch (type) {
case KSTAT_KV_T_COUNTER64:
case KSTAT_KV_T_COUNTER32:
case KSTAT_KV_T_UINT64:
case KSTAT_KV_T_INT64:
case KSTAT_KV_T_UINT32:
case KSTAT_KV_T_INT32:
break;
default:
panic("kv unit init %s: unit for non-integer type", name);
}
memset(kv, 0, sizeof(*kv));
strlcpy(kv->kv_key, name, sizeof(kv->kv_key)); /* XXX truncated? */
kv->kv_type = type;
kv->kv_unit = unit;
}
|