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
|
/* $OpenBSD: pool.c,v 1.14 2017/07/29 16:03:10 florian Exp $ */
/*
* Copyright (c) 2008 Can Erkin Acar <canacar@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/types.h>
#include <sys/signal.h>
#include <sys/sysctl.h>
#include <sys/pool.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
#include "systat.h"
#ifndef nitems
#define nitems(_a) (sizeof((_a)) / sizeof((_a)[0]))
#endif
static int sysctl_rdint(const int *, unsigned int);
static int hw_ncpusfound(void);
static int pool_get_npools(void);
static int pool_get_name(int, char *, size_t);
static int pool_get_cache(int, struct kinfo_pool_cache *);
static int pool_get_cache_cpus(int, struct kinfo_pool_cache_cpu *,
unsigned int);
void print_pool(void);
int read_pool(void);
void sort_pool(void);
int select_pool(void);
void showpool(int k);
int pool_keyboard_callback(int);
/* qsort callbacks */
int sort_name_callback(const void *s1, const void *s2);
int sort_req_callback(const void *s1, const void *s2);
int sort_psize_callback(const void *s1, const void *s2);
int sort_npage_callback(const void *s1, const void *s2);
struct pool_info {
char name[32];
struct kinfo_pool pool;
};
/*
* the kernel gives an array of ncpusfound * kinfo_pool_cache structs, but
* it's idea of how big that struct is may differ from here. we fetch both
* ncpusfound and the size it thinks kinfo_pool_cache is from sysctl, and
* then allocate the memory for this here.
*/
struct pool_cache_info {
char name[32];
struct kinfo_pool_cache cache;
struct kinfo_pool_cache_cpu *cache_cpus;
};
int print_all = 0;
int num_pools = 0;
struct pool_info *pools = NULL;
int num_pool_caches = 0;
struct pool_cache_info *pool_caches = NULL;
int ncpusfound = 0;
field_def fields_pool[] = {
{"NAME", 12, 32, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
{"SIZE", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"REQUESTS", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"FAIL", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"INUSE", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"PGREQ", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"PGREL", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"NPAGE", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"HIWAT", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"MINPG", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"MAXPG", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"IDLE", 8, 24, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0}
};
#define FLD_POOL_NAME FIELD_ADDR(fields_pool,0)
#define FLD_POOL_SIZE FIELD_ADDR(fields_pool,1)
#define FLD_POOL_REQS FIELD_ADDR(fields_pool,2)
#define FLD_POOL_FAIL FIELD_ADDR(fields_pool,3)
#define FLD_POOL_INUSE FIELD_ADDR(fields_pool,4)
#define FLD_POOL_PGREQ FIELD_ADDR(fields_pool,5)
#define FLD_POOL_PGREL FIELD_ADDR(fields_pool,6)
#define FLD_POOL_NPAGE FIELD_ADDR(fields_pool,7)
#define FLD_POOL_HIWAT FIELD_ADDR(fields_pool,8)
#define FLD_POOL_MINPG FIELD_ADDR(fields_pool,9)
#define FLD_POOL_MAXPG FIELD_ADDR(fields_pool,10)
#define FLD_POOL_IDLE FIELD_ADDR(fields_pool,11)
/* Define views */
field_def *view_pool_0[] = {
FLD_POOL_NAME, FLD_POOL_SIZE, FLD_POOL_REQS, FLD_POOL_FAIL,
FLD_POOL_INUSE, FLD_POOL_PGREQ, FLD_POOL_PGREL, FLD_POOL_NPAGE,
FLD_POOL_HIWAT, FLD_POOL_MINPG, FLD_POOL_MAXPG, FLD_POOL_IDLE, NULL
};
order_type pool_order_list[] = {
{"name", "name", 'N', sort_name_callback},
{"requests", "requests", 'Q', sort_req_callback},
{"size", "size", 'Z', sort_psize_callback},
{"npages", "npages", 'P', sort_npage_callback},
{NULL, NULL, 0, NULL}
};
/* Define view managers */
struct view_manager pool_mgr = {
"Pool", select_pool, read_pool, sort_pool, print_header,
print_pool, pool_keyboard_callback, pool_order_list, pool_order_list
};
field_view pool_view = {
view_pool_0,
"pool",
'5',
&pool_mgr
};
void pool_cache_print(void);
int pool_cache_read(void);
void pool_cache_sort(void);
void pool_cache_show(const struct pool_cache_info *);
int pool_cache_kbd_cb(int);
field_def pool_cache_fields[] = {
{"NAME", 12, 32, 1, FLD_ALIGN_LEFT, -1, 0, 0, 0},
{"LEN", 4, 4, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"IDLE", 4, 4, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"NGC", 4, 4, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"CPU", 4, 4, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"REQ", 8, 12, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"REL", 8, 12, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"LREQ", 8, 12, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
{"LREL", 8, 12, 1, FLD_ALIGN_RIGHT, -1, 0, 0, 0},
};
#define FLD_POOL_CACHE_NAME FIELD_ADDR(pool_cache_fields, 0)
#define FLD_POOL_CACHE_LEN FIELD_ADDR(pool_cache_fields, 1)
#define FLD_POOL_CACHE_IDLE FIELD_ADDR(pool_cache_fields, 2)
#define FLD_POOL_CACHE_NGC FIELD_ADDR(pool_cache_fields, 3)
#define FLD_POOL_CACHE_CPU FIELD_ADDR(pool_cache_fields, 4)
#define FLD_POOL_CACHE_GET FIELD_ADDR(pool_cache_fields, 5)
#define FLD_POOL_CACHE_PUT FIELD_ADDR(pool_cache_fields, 6)
#define FLD_POOL_CACHE_LGET FIELD_ADDR(pool_cache_fields, 7)
#define FLD_POOL_CACHE_LPUT FIELD_ADDR(pool_cache_fields, 8)
field_def *view_pool_cache_0[] = {
FLD_POOL_CACHE_NAME,
FLD_POOL_CACHE_LEN,
FLD_POOL_CACHE_IDLE,
FLD_POOL_CACHE_NGC,
FLD_POOL_CACHE_CPU,
FLD_POOL_CACHE_GET,
FLD_POOL_CACHE_PUT,
FLD_POOL_CACHE_LGET,
FLD_POOL_CACHE_LPUT,
NULL,
};
order_type pool_cache_order_list[] = {
{"name", "name", 'N', sort_name_callback},
{"requests", "requests", 'G', sort_req_callback},
{"releases", "releases", 'P', sort_req_callback},
{NULL, NULL, 0, NULL}
};
/* Define view managers */
struct view_manager pool_cache_mgr = {
"PoolCache",
select_pool,
pool_cache_read,
pool_cache_sort,
print_header,
pool_cache_print,
pool_keyboard_callback,
pool_cache_order_list,
pool_cache_order_list
};
field_view pool_cache_view = {
view_pool_cache_0,
"pcaches",
'5',
&pool_cache_mgr
};
int
sort_name_callback(const void *s1, const void *s2)
{
struct pool_info *p1, *p2;
p1 = (struct pool_info *)s1;
p2 = (struct pool_info *)s2;
return strcmp(p1->name, p2->name) * sortdir;
}
int
sort_req_callback(const void *s1, const void *s2)
{
struct pool_info *p1, *p2;
p1 = (struct pool_info *)s1;
p2 = (struct pool_info *)s2;
if (p1->pool.pr_nget < p2->pool.pr_nget)
return sortdir;
if (p1->pool.pr_nget > p2->pool.pr_nget)
return -sortdir;
return sort_name_callback(s1, s2);
}
int
sort_npage_callback(const void *s1, const void *s2)
{
struct pool_info *p1, *p2;
p1 = (struct pool_info *)s1;
p2 = (struct pool_info *)s2;
if (p1->pool.pr_npages < p2->pool.pr_npages)
return sortdir;
if (p1->pool.pr_npages > p2->pool.pr_npages)
return -sortdir;
return sort_name_callback(s1, s2);
}
int
sort_psize_callback(const void *s1, const void *s2)
{
struct pool_info *p1, *p2;
size_t ps1, ps2;
p1 = (struct pool_info *)s1;
p2 = (struct pool_info *)s2;
ps1 = (size_t)(p1->pool.pr_nget - p1->pool.pr_nput) *
(size_t)p1->pool.pr_size;
ps2 = (size_t)(p2->pool.pr_nget - p2->pool.pr_nput) *
(size_t)p2->pool.pr_size;
if (ps1 < ps2)
return sortdir;
if (ps1 > ps2)
return -sortdir;
return sort_npage_callback(s1, s2);
}
void
sort_pool(void)
{
order_type *ordering;
if (curr_mgr == NULL)
return;
ordering = curr_mgr->order_curr;
if (ordering == NULL)
return;
if (ordering->func == NULL)
return;
if (pools == NULL)
return;
if (num_pools <= 0)
return;
mergesort(pools, num_pools, sizeof(struct pool_info), ordering->func);
}
int
select_pool(void)
{
num_disp = num_pools;
return (0);
}
int
read_pool(void)
{
int mib[] = { CTL_KERN, KERN_POOL, KERN_POOL_POOL, 0 };
struct pool_info *p;
int np, i;
size_t size;
np = pool_get_npools();
if (np == -1) {
error("sysctl(npools): %s", strerror(errno));
return (-1);
}
if (np == 0) {
free(pools);
pools = NULL;
num_pools = 0;
return (0);
}
if (np > num_pools || pools == NULL) {
p = reallocarray(pools, np, sizeof(*pools));
if (p == NULL) {
error("realloc: %s", strerror(errno));
return (-1);
}
/* commit */
pools = p;
num_pools = np;
}
num_disp = num_pools;
for (i = 0; i < num_pools; i++) {
p = &pools[i];
np = i + 1;
mib[3] = np;
size = sizeof(pools[i].pool);
if (sysctl(mib, nitems(mib), &p->pool, &size, NULL, 0) < 0) {
p->name[0] = '\0';
num_disp--;
continue;
}
if (pool_get_name(np, p->name, sizeof(p->name)) < 0)
snprintf(p->name, sizeof(p->name), "#%d#", i + 1);
}
return 0;
}
void
print_pool(void)
{
struct pool_info *p;
int i, n, count = 0;
if (pools == NULL)
return;
for (n = i = 0; i < num_pools; i++) {
p = &pools[i];
if (p->name[0] == 0)
continue;
if (!print_all &&
(p->pool.pr_nget == 0 && p->pool.pr_npagealloc == 0))
continue;
if (n++ < dispstart)
continue;
showpool(i);
count++;
if (maxprint > 0 && count >= maxprint)
break;
}
}
int
initpool(void)
{
field_view *v;
add_view(&pool_view);
read_pool();
ncpusfound = hw_ncpusfound();
if (ncpusfound == -1) {
error("sysctl(ncpusfound): %s", strerror(errno));
exit(1);
}
add_view(&pool_cache_view);
pool_cache_read();
return(0);
}
void
showpool(int k)
{
struct pool_info *p = pools + k;
if (k < 0 || k >= num_pools)
return;
print_fld_str(FLD_POOL_NAME, p->name);
print_fld_uint(FLD_POOL_SIZE, p->pool.pr_size);
print_fld_size(FLD_POOL_REQS, p->pool.pr_nget);
print_fld_size(FLD_POOL_FAIL, p->pool.pr_nfail);
print_fld_ssize(FLD_POOL_INUSE, p->pool.pr_nget - p->pool.pr_nput);
print_fld_size(FLD_POOL_PGREQ, p->pool.pr_npagealloc);
print_fld_size(FLD_POOL_PGREL, p->pool.pr_npagefree);
print_fld_size(FLD_POOL_NPAGE, p->pool.pr_npages);
print_fld_size(FLD_POOL_HIWAT, p->pool.pr_hiwat);
print_fld_size(FLD_POOL_MINPG, p->pool.pr_minpages);
if (p->pool.pr_maxpages == UINT_MAX)
print_fld_str(FLD_POOL_MAXPG, "inf");
else
print_fld_size(FLD_POOL_MAXPG, p->pool.pr_maxpages);
print_fld_size(FLD_POOL_IDLE, p->pool.pr_nidle);
end_line();
}
int
pool_keyboard_callback(int ch)
{
switch (ch) {
case 'A':
print_all ^= 1;
gotsig_alarm = 1;
default:
return keyboard_callback(ch);
};
return (1);
}
int
pool_cache_read(void)
{
struct pool_cache_info *pc;
int np, i;
np = pool_get_npools();
if (np == -1) {
error("sysctl(npools): %s", strerror(errno));
return (-1);
}
if (np > num_pool_caches) {
pc = reallocarray(pool_caches, np, sizeof(*pc));
if (pc == NULL) {
error("realloc: %s", strerror(errno));
return (-1);
}
/* commit to using the new memory */
pool_caches = pc;
for (i = num_pool_caches; i < np; i++) {
pc = &pool_caches[i];
pc->name[0] = '\0';
pc->cache_cpus = reallocarray(NULL, ncpusfound,
sizeof(*pc->cache_cpus));
if (pc->cache_cpus == NULL) {
error("malloc cache cpus: %s", strerror(errno));
goto unalloc;
}
}
/* commit to using the new cache_infos */
num_pool_caches = np;
}
for (i = 0; i < num_pool_caches; i++) {
pc = &pool_caches[i];
np = i + 1;
if (pool_get_cache(np, &pc->cache) < 0 ||
pool_get_cache_cpus(np, pc->cache_cpus, ncpusfound) < 0) {
pc->name[0] = '\0';
continue;
}
if (pool_get_name(np, pc->name, sizeof(pc->name)) < 0)
snprintf(pc->name, sizeof(pc->name), "#%d#", i + 1);
}
return 0;
unalloc:
while (i > num_pool_caches) {
pc = &pool_caches[--i];
free(pc->cache_cpus);
}
return (-1);
}
void
pool_cache_sort(void)
{
/* XXX */
order_type *ordering;
if (curr_mgr == NULL)
return;
ordering = curr_mgr->order_curr;
if (ordering == NULL)
return;
if (ordering->func == NULL)
return;
if (pools == NULL)
return;
if (num_pools <= 0)
return;
mergesort(pools, num_pools, sizeof(struct pool_info), ordering->func);
}
void
pool_cache_print(void)
{
struct pool_cache_info *pc;
int i, n, count = 0;
if (pool_caches == NULL)
return;
for (n = i = 0; i < num_pool_caches; i++) {
pc = &pool_caches[i];
if (pc->name[0] == '\0')
continue;
if (n++ < dispstart)
continue;
pool_cache_show(pc);
count++;
if (maxprint > 0 && count >= maxprint)
break;
}
}
void
pool_cache_show(const struct pool_cache_info *pc)
{
const struct kinfo_pool_cache *kpc;
const struct kinfo_pool_cache_cpu *kpcc;
int cpu;
kpc = &pc->cache;
print_fld_str(FLD_POOL_CACHE_NAME, pc->name);
print_fld_uint(FLD_POOL_CACHE_LEN, kpc->pr_len);
print_fld_uint(FLD_POOL_CACHE_IDLE, kpc->pr_nitems);
print_fld_uint(FLD_POOL_CACHE_NGC, kpc->pr_ngc);
for (cpu = 0; cpu < ncpusfound; cpu++) {
kpcc = &pc->cache_cpus[cpu];
print_fld_uint(FLD_POOL_CACHE_CPU, kpcc->pr_cpu);
print_fld_size(FLD_POOL_CACHE_GET, kpcc->pr_nget);
print_fld_size(FLD_POOL_CACHE_PUT, kpcc->pr_nput);
print_fld_size(FLD_POOL_CACHE_LGET, kpcc->pr_nlget);
print_fld_size(FLD_POOL_CACHE_LPUT, kpcc->pr_nlput);
end_line();
}
}
static int
pool_get_npools(void)
{
int mib[] = { CTL_KERN, KERN_POOL, KERN_POOL_NPOOLS };
return (sysctl_rdint(mib, nitems(mib)));
}
static int
pool_get_cache(int pool, struct kinfo_pool_cache *kpc)
{
int mib[] = { CTL_KERN, KERN_POOL, KERN_POOL_CACHE, pool };
size_t len = sizeof(*kpc);
return (sysctl(mib, nitems(mib), kpc, &len, NULL, 0));
}
static int
pool_get_cache_cpus(int pool, struct kinfo_pool_cache_cpu *kpcc,
unsigned int ncpus)
{
int mib[] = { CTL_KERN, KERN_POOL, KERN_POOL_CACHE_CPUS, pool };
size_t len = sizeof(*kpcc) * ncpus;
return (sysctl(mib, nitems(mib), kpcc, &len, NULL, 0));
}
static int
pool_get_name(int pool, char *name, size_t len)
{
int mib[] = { CTL_KERN, KERN_POOL, KERN_POOL_NAME, pool };
return (sysctl(mib, nitems(mib), name, &len, NULL, 0));
}
static int
hw_ncpusfound(void)
{
int mib[] = { CTL_HW, HW_NCPUFOUND };
return (sysctl_rdint(mib, nitems(mib)));
}
static int
sysctl_rdint(const int *mib, unsigned int nmib)
{
int i;
size_t size = sizeof(i);
if (sysctl(mib, nmib, &i, &size, NULL, 0) == -1)
return (-1);
return (i);
}
|