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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 2012, Joyent, Inc. All rights reserved.
*/
#include "umem.h"
#include <libproc.h>
#include <mdb/mdb_modapi.h>
#include "kgrep.h"
#include "leaky.h"
#include "misc.h"
#include "proc_kludges.h"
#include <umem_impl.h>
#include <sys/vmem_impl_user.h>
#include <thr_uberdata.h>
#include "umem_pagesize.h"
typedef struct datafmt {
char *hdr1;
char *hdr2;
char *dashes;
char *fmt;
} datafmt_t;
static datafmt_t ptcfmt[] = {
{ " ", "tid", "---", "%3u " },
{ " memory", " cached", "-------", "%7lH " },
{ " %", "cap", "---", "%3u " },
{ " %", NULL, "---", "%3u " },
{ NULL, NULL, NULL, NULL }
};
static datafmt_t umemfmt[] = {
{ "cache ", "name ",
"-------------------------", "%-25s " },
{ " buf", " size", "------", "%6u " },
{ " buf", " in use", "-------", "%7u " },
{ " buf", " in ptc", "-------", "%7s " },
{ " buf", " total", "-------", "%7u " },
{ " memory", " in use", "-------", "%7H " },
{ " alloc", " succeed", "---------", "%9u " },
{ "alloc", " fail", "-----", "%5llu" },
{ NULL, NULL, NULL, NULL }
};
static datafmt_t vmemfmt[] = {
{ "vmem ", "name ",
"-------------------------", "%-*s " },
{ " memory", " in use", "---------", "%9H " },
{ " memory", " total", "----------", "%10H " },
{ " memory", " import", "---------", "%9H " },
{ " alloc", " succeed", "---------", "%9llu " },
{ "alloc", " fail", "-----", "%5llu " },
{ NULL, NULL, NULL, NULL }
};
/*ARGSUSED*/
static int
umastat_cpu_avail(uintptr_t addr, const umem_cpu_cache_t *ccp, int *avail)
{
if (ccp->cc_rounds > 0)
*avail += ccp->cc_rounds;
if (ccp->cc_prounds > 0)
*avail += ccp->cc_prounds;
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_cpu_alloc(uintptr_t addr, const umem_cpu_cache_t *ccp, int *alloc)
{
*alloc += ccp->cc_alloc;
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_slab_avail(uintptr_t addr, const umem_slab_t *sp, int *avail)
{
*avail += sp->slab_chunks - sp->slab_refcnt;
return (WALK_NEXT);
}
typedef struct umastat_vmem {
uintptr_t kv_addr;
struct umastat_vmem *kv_next;
int kv_meminuse;
int kv_alloc;
int kv_fail;
} umastat_vmem_t;
/*ARGSUSED*/
static int
umastat_cache_nptc(uintptr_t addr, const umem_cache_t *cp, int *nptc)
{
if (!(cp->cache_flags & UMF_PTC))
return (WALK_NEXT);
(*nptc)++;
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_cache_hdr(uintptr_t addr, const umem_cache_t *cp, void *ignored)
{
if (!(cp->cache_flags & UMF_PTC))
return (WALK_NEXT);
mdb_printf("%3d ", cp->cache_bufsize);
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_lwp_ptc(uintptr_t addr, void *buf, int *nbufs)
{
(*nbufs)++;
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_lwp_cache(uintptr_t addr, const umem_cache_t *cp, ulwp_t *ulwp)
{
char walk[60];
int nbufs = 0;
if (!(cp->cache_flags & UMF_PTC))
return (WALK_NEXT);
(void) mdb_snprintf(walk, sizeof (walk), "umem_ptc_%d",
cp->cache_bufsize);
if (mdb_pwalk(walk, (mdb_walk_cb_t)umastat_lwp_ptc,
&nbufs, (uintptr_t)ulwp->ul_self) == -1) {
mdb_warn("unable to walk '%s'", walk);
return (WALK_ERR);
}
mdb_printf("%3d ", ulwp->ul_tmem.tm_size ?
(nbufs * cp->cache_bufsize * 100) / ulwp->ul_tmem.tm_size : 0);
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_lwp(uintptr_t addr, const ulwp_t *ulwp, void *ignored)
{
size_t size;
datafmt_t *dfp = ptcfmt;
mdb_printf((dfp++)->fmt, ulwp->ul_lwpid);
mdb_printf((dfp++)->fmt, ulwp->ul_tmem.tm_size);
if (umem_readvar(&size, "umem_ptc_size") == -1) {
mdb_warn("unable to read 'umem_ptc_size'");
return (WALK_ERR);
}
mdb_printf((dfp++)->fmt, (ulwp->ul_tmem.tm_size * 100) / size);
if (mdb_walk("umem_cache",
(mdb_walk_cb_t)umastat_lwp_cache, (void *)ulwp) == -1) {
mdb_warn("can't walk 'umem_cache'");
return (WALK_ERR);
}
mdb_printf("\n");
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_cache_ptc(uintptr_t addr, const void *ignored, int *nptc)
{
(*nptc)++;
return (WALK_NEXT);
}
static int
umastat_cache(uintptr_t addr, const umem_cache_t *cp, umastat_vmem_t **kvp)
{
umastat_vmem_t *kv;
datafmt_t *dfp = umemfmt;
char buf[10];
int magsize;
int avail, alloc, total, nptc = 0;
size_t meminuse = (cp->cache_slab_create - cp->cache_slab_destroy) *
cp->cache_slabsize;
mdb_walk_cb_t cpu_avail = (mdb_walk_cb_t)umastat_cpu_avail;
mdb_walk_cb_t cpu_alloc = (mdb_walk_cb_t)umastat_cpu_alloc;
mdb_walk_cb_t slab_avail = (mdb_walk_cb_t)umastat_slab_avail;
magsize = umem_get_magsize(cp);
alloc = cp->cache_slab_alloc + cp->cache_full.ml_alloc;
avail = cp->cache_full.ml_total * magsize;
total = cp->cache_buftotal;
(void) mdb_pwalk("umem_cpu_cache", cpu_alloc, &alloc, addr);
(void) mdb_pwalk("umem_cpu_cache", cpu_avail, &avail, addr);
(void) mdb_pwalk("umem_slab_partial", slab_avail, &avail, addr);
if (cp->cache_flags & UMF_PTC) {
char walk[60];
(void) mdb_snprintf(walk, sizeof (walk),
"umem_ptc_%d", cp->cache_bufsize);
if (mdb_walk(walk,
(mdb_walk_cb_t)umastat_cache_ptc, &nptc) == -1) {
mdb_warn("unable to walk '%s'", walk);
return (WALK_ERR);
}
(void) mdb_snprintf(buf, sizeof (buf), "%d", nptc);
}
for (kv = *kvp; kv != NULL; kv = kv->kv_next) {
if (kv->kv_addr == (uintptr_t)cp->cache_arena)
goto out;
}
kv = mdb_zalloc(sizeof (umastat_vmem_t), UM_SLEEP | UM_GC);
kv->kv_next = *kvp;
kv->kv_addr = (uintptr_t)cp->cache_arena;
*kvp = kv;
out:
kv->kv_meminuse += meminuse;
kv->kv_alloc += alloc;
kv->kv_fail += cp->cache_alloc_fail;
mdb_printf((dfp++)->fmt, cp->cache_name);
mdb_printf((dfp++)->fmt, cp->cache_bufsize);
mdb_printf((dfp++)->fmt, total - avail);
mdb_printf((dfp++)->fmt, cp->cache_flags & UMF_PTC ? buf : "-");
mdb_printf((dfp++)->fmt, total);
mdb_printf((dfp++)->fmt, meminuse);
mdb_printf((dfp++)->fmt, alloc);
mdb_printf((dfp++)->fmt, cp->cache_alloc_fail);
mdb_printf("\n");
return (WALK_NEXT);
}
static int
umastat_vmem_totals(uintptr_t addr, const vmem_t *v, umastat_vmem_t *kv)
{
while (kv != NULL && kv->kv_addr != addr)
kv = kv->kv_next;
if (kv == NULL || kv->kv_alloc == 0)
return (WALK_NEXT);
mdb_printf("Total [%s]%*s %6s %7s %7s %7s %7H %9u %5u\n", v->vm_name,
17 - strlen(v->vm_name), "", "", "", "", "",
kv->kv_meminuse, kv->kv_alloc, kv->kv_fail);
return (WALK_NEXT);
}
/*ARGSUSED*/
static int
umastat_vmem(uintptr_t addr, const vmem_t *v, void *ignored)
{
datafmt_t *dfp = vmemfmt;
uintptr_t paddr;
vmem_t parent;
int ident = 0;
for (paddr = (uintptr_t)v->vm_source; paddr != 0; ident += 4) {
if (mdb_vread(&parent, sizeof (parent), paddr) == -1) {
mdb_warn("couldn't trace %p's ancestry", addr);
ident = 0;
break;
}
paddr = (uintptr_t)parent.vm_source;
}
mdb_printf("%*s", ident, "");
mdb_printf((dfp++)->fmt, 25 - ident, v->vm_name);
mdb_printf((dfp++)->fmt, v->vm_kstat.vk_mem_inuse);
mdb_printf((dfp++)->fmt, v->vm_kstat.vk_mem_total);
mdb_printf((dfp++)->fmt, v->vm_kstat.vk_mem_import);
mdb_printf((dfp++)->fmt, v->vm_kstat.vk_alloc);
mdb_printf((dfp++)->fmt, v->vm_kstat.vk_fail);
mdb_printf("\n");
return (WALK_NEXT);
}
/*ARGSUSED*/
int
umastat(uintptr_t addr, uint_t flags, int argc, const mdb_arg_t *argv)
{
umastat_vmem_t *kv = NULL;
datafmt_t *dfp;
int nptc = 0, i;
if (argc != 0)
return (DCMD_USAGE);
/*
* We need to determine if we have any caches that have per-thread
* caching enabled.
*/
if (mdb_walk("umem_cache",
(mdb_walk_cb_t)umastat_cache_nptc, &nptc) == -1) {
mdb_warn("can't walk 'umem_cache'");
return (DCMD_ERR);
}
if (nptc) {
for (dfp = ptcfmt; dfp->hdr2 != NULL; dfp++)
mdb_printf("%s ", dfp->hdr1);
for (i = 0; i < nptc; i++)
mdb_printf("%s ", dfp->hdr1);
mdb_printf("\n");
for (dfp = ptcfmt; dfp->hdr2 != NULL; dfp++)
mdb_printf("%s ", dfp->hdr2);
if (mdb_walk("umem_cache",
(mdb_walk_cb_t)umastat_cache_hdr, NULL) == -1) {
mdb_warn("can't walk 'umem_cache'");
return (DCMD_ERR);
}
mdb_printf("\n");
for (dfp = ptcfmt; dfp->hdr2 != NULL; dfp++)
mdb_printf("%s ", dfp->dashes);
for (i = 0; i < nptc; i++)
mdb_printf("%s ", dfp->dashes);
mdb_printf("\n");
if (mdb_walk("ulwp", (mdb_walk_cb_t)umastat_lwp, NULL) == -1) {
mdb_warn("can't walk 'ulwp'");
return (DCMD_ERR);
}
mdb_printf("\n");
}
for (dfp = umemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s%s", dfp == umemfmt ? "" : " ", dfp->hdr1);
mdb_printf("\n");
for (dfp = umemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s%s", dfp == umemfmt ? "" : " ", dfp->hdr2);
mdb_printf("\n");
for (dfp = umemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s%s", dfp == umemfmt ? "" : " ", dfp->dashes);
mdb_printf("\n");
if (mdb_walk("umem_cache", (mdb_walk_cb_t)umastat_cache, &kv) == -1) {
mdb_warn("can't walk 'umem_cache'");
return (DCMD_ERR);
}
for (dfp = umemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s%s", dfp == umemfmt ? "" : " ", dfp->dashes);
mdb_printf("\n");
if (mdb_walk("vmem", (mdb_walk_cb_t)umastat_vmem_totals, kv) == -1) {
mdb_warn("can't walk 'vmem'");
return (DCMD_ERR);
}
for (dfp = umemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s ", dfp->dashes);
mdb_printf("\n");
mdb_printf("\n");
for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s ", dfp->hdr1);
mdb_printf("\n");
for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s ", dfp->hdr2);
mdb_printf("\n");
for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s ", dfp->dashes);
mdb_printf("\n");
if (mdb_walk("vmem", (mdb_walk_cb_t)umastat_vmem, NULL) == -1) {
mdb_warn("can't walk 'vmem'");
return (DCMD_ERR);
}
for (dfp = vmemfmt; dfp->hdr1 != NULL; dfp++)
mdb_printf("%s ", dfp->dashes);
mdb_printf("\n");
return (DCMD_OK);
}
/*
* kmdb doesn't use libproc, and thus doesn't have any prmap_t's to walk.
* We have other ways to grep kmdb's address range.
*/
#ifndef _KMDB
typedef struct ugrep_walk_data {
kgrep_cb_func *ug_cb;
void *ug_cbdata;
} ugrep_walk_data_t;
/*ARGSUSED*/
int
ugrep_mapping_cb(uintptr_t addr, const void *prm_arg, void *data)
{
ugrep_walk_data_t *ug = data;
const prmap_t *prm = prm_arg;
return (ug->ug_cb(prm->pr_vaddr, prm->pr_vaddr + prm->pr_size,
ug->ug_cbdata));
}
int
kgrep_subr(kgrep_cb_func *cb, void *cbdata)
{
ugrep_walk_data_t ug;
prockludge_add_walkers();
ug.ug_cb = cb;
ug.ug_cbdata = cbdata;
if (mdb_walk(KLUDGE_MAPWALK_NAME, ugrep_mapping_cb, &ug) == -1) {
mdb_warn("Unable to walk "KLUDGE_MAPWALK_NAME);
return (DCMD_ERR);
}
prockludge_remove_walkers();
return (DCMD_OK);
}
size_t
kgrep_subr_pagesize(void)
{
return (PAGESIZE);
}
#endif /* !_KMDB */
static const mdb_dcmd_t dcmds[] = {
/* from libumem.c */
{ "umastat", NULL, "umem allocator stats", umastat },
/* from misc.c */
{ "umem_debug", NULL, "toggle umem dcmd/walk debugging", umem_debug},
/* from umem.c */
{ "umem_status", NULL, "Print umem status and message buffer",
umem_status },
{ "allocdby", ":", "given a thread, print its allocated buffers",
allocdby },
{ "bufctl", ":[-vh] [-a addr] [-c caller] [-e earliest] [-l latest] "
"[-t thd]", "print or filter a bufctl", bufctl, bufctl_help },
{ "bufctl_audit", ":", "print a bufctl_audit", bufctl_audit },
{ "freedby", ":", "given a thread, print its freed buffers", freedby },
{ "umalog", "[ fail | slab ]",
"display umem transaction log and stack traces", umalog },
{ "umausers", "[-ef] [cache ...]", "display current medium and large "
"users of the umem allocator", umausers },
{ "umem_cache", "?", "print a umem cache", umem_cache },
{ "umem_log", "?", "dump umem transaction log", umem_log },
{ "umem_malloc_dist", "[-dg] [-b maxbins] [-B minbinsize]",
"report distribution of outstanding malloc()s",
umem_malloc_dist, umem_malloc_dist_help },
{ "umem_malloc_info", "?[-dg] [-b maxbins] [-B minbinsize]",
"report information about malloc()s by cache",
umem_malloc_info, umem_malloc_info_help },
{ "umem_verify", "?", "check integrity of umem-managed memory",
umem_verify },
{ "vmem", "?", "print a vmem_t", vmem },
{ "vmem_seg", ":[-sv] [-c caller] [-e earliest] [-l latest] "
"[-m minsize] [-M maxsize] [-t thread] [-T type]",
"print or filter a vmem_seg", vmem_seg, vmem_seg_help },
#ifndef _KMDB
/* from ../genunix/kgrep.c + libumem.c */
{ "ugrep", KGREP_USAGE, "search user address space for a pointer",
kgrep, kgrep_help },
/* from ../genunix/leaky.c + leaky_subr.c */
{ "findleaks", FINDLEAKS_USAGE, "search for potential memory leaks",
findleaks, findleaks_help },
#endif
{ NULL }
};
static const mdb_walker_t walkers[] = {
/* from umem.c */
{ "allocdby", "given a thread, walk its allocated bufctls",
allocdby_walk_init, allocdby_walk_step, allocdby_walk_fini },
{ "bufctl", "walk a umem cache's bufctls",
bufctl_walk_init, umem_walk_step, umem_walk_fini },
{ "bufctl_history", "walk the available history of a bufctl",
bufctl_history_walk_init, bufctl_history_walk_step,
bufctl_history_walk_fini },
{ "freectl", "walk a umem cache's free bufctls",
freectl_walk_init, umem_walk_step, umem_walk_fini },
{ "freedby", "given a thread, walk its freed bufctls",
freedby_walk_init, allocdby_walk_step, allocdby_walk_fini },
{ "freemem", "walk a umem cache's free memory",
freemem_walk_init, umem_walk_step, umem_walk_fini },
{ "umem", "walk a umem cache",
umem_walk_init, umem_walk_step, umem_walk_fini },
{ "umem_cpu", "walk the umem CPU structures",
umem_cpu_walk_init, umem_cpu_walk_step, umem_cpu_walk_fini },
{ "umem_cpu_cache", "given a umem cache, walk its per-CPU caches",
umem_cpu_cache_walk_init, umem_cpu_cache_walk_step, NULL },
{ "umem_hash", "given a umem cache, walk its allocated hash table",
umem_hash_walk_init, umem_hash_walk_step, umem_hash_walk_fini },
{ "umem_log", "walk the umem transaction log",
umem_log_walk_init, umem_log_walk_step, umem_log_walk_fini },
{ "umem_slab", "given a umem cache, walk its slabs",
umem_slab_walk_init, umem_slab_walk_step, NULL },
{ "umem_slab_partial",
"given a umem cache, walk its partially allocated slabs (min 1)",
umem_slab_walk_partial_init, umem_slab_walk_step, NULL },
{ "vmem", "walk vmem structures in pre-fix, depth-first order",
vmem_walk_init, vmem_walk_step, vmem_walk_fini },
{ "vmem_alloc", "given a vmem_t, walk its allocated vmem_segs",
vmem_alloc_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
{ "vmem_free", "given a vmem_t, walk its free vmem_segs",
vmem_free_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
{ "vmem_postfix", "walk vmem structures in post-fix, depth-first order",
vmem_walk_init, vmem_postfix_walk_step, vmem_walk_fini },
{ "vmem_seg", "given a vmem_t, walk all of its vmem_segs",
vmem_seg_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
{ "vmem_span", "given a vmem_t, walk its spanning vmem_segs",
vmem_span_walk_init, vmem_seg_walk_step, vmem_seg_walk_fini },
#ifndef _KMDB
/* from ../genunix/leaky.c + leaky_subr.c */
{ "leak", "given a leak ctl, walk other leaks w/ that stacktrace",
leaky_walk_init, leaky_walk_step, leaky_walk_fini },
{ "leakbuf", "given a leak ctl, walk addr of leaks w/ that stacktrace",
leaky_walk_init, leaky_buf_walk_step, leaky_walk_fini },
#endif
{ NULL }
};
static const mdb_modinfo_t modinfo = {MDB_API_VERSION, dcmds, walkers};
const mdb_modinfo_t *
_mdb_init(void)
{
if (umem_init() != 0)
return (NULL);
return (&modinfo);
}
void
_mdb_fini(void)
{
#ifndef _KMDB
leaky_cleanup(1);
#endif
}
|