lockstat.c revision 1.21 1 /* $NetBSD: lockstat.c,v 1.21 2015/03/09 01:41:41 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2006, 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Lock statistics driver, providing kernel support for the lockstat(8)
34 * command.
35 *
36 * We use a global lock word (lockstat_lock) to track device opens.
37 * Only one thread can hold the device at a time, providing a global lock.
38 *
39 * XXX Timings for contention on sleep locks are currently incorrect.
40 */
41
42 #include <sys/cdefs.h>
43 __KERNEL_RCSID(0, "$NetBSD: lockstat.c,v 1.21 2015/03/09 01:41:41 christos Exp $");
44
45 #include <sys/types.h>
46 #include <sys/param.h>
47 #include <sys/proc.h>
48 #include <sys/resourcevar.h>
49 #include <sys/systm.h>
50 #include <sys/kernel.h>
51 #include <sys/kmem.h>
52 #include <sys/conf.h>
53 #include <sys/syslog.h>
54 #include <sys/atomic.h>
55
56 #include <dev/lockstat.h>
57
58 #include <machine/lock.h>
59
60 #ifndef __HAVE_CPU_COUNTER
61 #error CPU counters not available
62 #endif
63
64 #if LONG_BIT == 64
65 #define LOCKSTAT_HASH_SHIFT 3
66 #elif LONG_BIT == 32
67 #define LOCKSTAT_HASH_SHIFT 2
68 #endif
69
70 #define LOCKSTAT_MINBUFS 1000
71 #define LOCKSTAT_DEFBUFS 10000
72 #define LOCKSTAT_MAXBUFS 1000000
73
74 #define LOCKSTAT_HASH_SIZE 128
75 #define LOCKSTAT_HASH_MASK (LOCKSTAT_HASH_SIZE - 1)
76 #define LOCKSTAT_HASH(key) \
77 ((key >> LOCKSTAT_HASH_SHIFT) & LOCKSTAT_HASH_MASK)
78
79 typedef struct lscpu {
80 SLIST_HEAD(, lsbuf) lc_free;
81 u_int lc_overflow;
82 LIST_HEAD(lslist, lsbuf) lc_hash[LOCKSTAT_HASH_SIZE];
83 } lscpu_t;
84
85 typedef struct lslist lslist_t;
86
87 void lockstatattach(int);
88 void lockstat_start(lsenable_t *);
89 int lockstat_alloc(lsenable_t *);
90 void lockstat_init_tables(lsenable_t *);
91 int lockstat_stop(lsdisable_t *);
92 void lockstat_free(void);
93
94 dev_type_open(lockstat_open);
95 dev_type_close(lockstat_close);
96 dev_type_read(lockstat_read);
97 dev_type_ioctl(lockstat_ioctl);
98
99 volatile u_int lockstat_enabled;
100 volatile u_int lockstat_dev_enabled;
101 uintptr_t lockstat_csstart;
102 uintptr_t lockstat_csend;
103 uintptr_t lockstat_csmask;
104 uintptr_t lockstat_lamask;
105 uintptr_t lockstat_lockstart;
106 uintptr_t lockstat_lockend;
107 __cpu_simple_lock_t lockstat_lock;
108 lwp_t *lockstat_lwp;
109 lsbuf_t *lockstat_baseb;
110 size_t lockstat_sizeb;
111 int lockstat_busy;
112 struct timespec lockstat_stime;
113
114 #ifdef KDTRACE_HOOKS
115 volatile u_int lockstat_dtrace_enabled;
116 CTASSERT(LB_NEVENT <= 3);
117 CTASSERT(LB_NLOCK <= (7 << LB_LOCK_SHIFT));
118 void
119 lockstat_probe_stub(uint32_t id, uintptr_t lock, uintptr_t callsite,
120 uintptr_t flags, uintptr_t count, uintptr_t cycles)
121 {
122 }
123
124 uint32_t lockstat_probemap[LS_NPROBES];
125 void (*lockstat_probe_func)(uint32_t, uintptr_t, uintptr_t,
126 uintptr_t, uintptr_t, uintptr_t) = &lockstat_probe_stub;
127 #endif
128
129 const struct cdevsw lockstat_cdevsw = {
130 .d_open = lockstat_open,
131 .d_close = lockstat_close,
132 .d_read = lockstat_read,
133 .d_write = nowrite,
134 .d_ioctl = lockstat_ioctl,
135 .d_stop = nostop,
136 .d_tty = notty,
137 .d_poll = nopoll,
138 .d_mmap = nommap,
139 .d_kqfilter = nokqfilter,
140 .d_discard = nodiscard,
141 .d_flag = D_OTHER | D_MPSAFE
142 };
143
144 /*
145 * Called when the pseudo-driver is attached.
146 */
147 void
148 lockstatattach(int nunits)
149 {
150
151 (void)nunits;
152
153 __cpu_simple_lock_init(&lockstat_lock);
154 }
155
156 /*
157 * Prepare the per-CPU tables for use, or clear down tables when tracing is
158 * stopped.
159 */
160 void
161 lockstat_init_tables(lsenable_t *le)
162 {
163 int i, per, slop, cpuno;
164 CPU_INFO_ITERATOR cii;
165 struct cpu_info *ci;
166 lscpu_t *lc;
167 lsbuf_t *lb;
168
169 KASSERT(!lockstat_dev_enabled);
170
171 for (CPU_INFO_FOREACH(cii, ci)) {
172 if (ci->ci_lockstat != NULL) {
173 kmem_free(ci->ci_lockstat, sizeof(lscpu_t));
174 ci->ci_lockstat = NULL;
175 }
176 }
177
178 if (le == NULL)
179 return;
180
181 lb = lockstat_baseb;
182 per = le->le_nbufs / ncpu;
183 slop = le->le_nbufs - (per * ncpu);
184 cpuno = 0;
185 for (CPU_INFO_FOREACH(cii, ci)) {
186 lc = kmem_alloc(sizeof(*lc), KM_SLEEP);
187 lc->lc_overflow = 0;
188 ci->ci_lockstat = lc;
189
190 SLIST_INIT(&lc->lc_free);
191 for (i = 0; i < LOCKSTAT_HASH_SIZE; i++)
192 LIST_INIT(&lc->lc_hash[i]);
193
194 for (i = per; i != 0; i--, lb++) {
195 lb->lb_cpu = (uint16_t)cpuno;
196 SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
197 }
198 if (--slop > 0) {
199 lb->lb_cpu = (uint16_t)cpuno;
200 SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
201 lb++;
202 }
203 cpuno++;
204 }
205 }
206
207 /*
208 * Start collecting lock statistics.
209 */
210 void
211 lockstat_start(lsenable_t *le)
212 {
213
214 KASSERT(!lockstat_dev_enabled);
215
216 lockstat_init_tables(le);
217
218 if ((le->le_flags & LE_CALLSITE) != 0)
219 lockstat_csmask = (uintptr_t)-1LL;
220 else
221 lockstat_csmask = 0;
222
223 if ((le->le_flags & LE_LOCK) != 0)
224 lockstat_lamask = (uintptr_t)-1LL;
225 else
226 lockstat_lamask = 0;
227
228 lockstat_csstart = le->le_csstart;
229 lockstat_csend = le->le_csend;
230 lockstat_lockstart = le->le_lockstart;
231 lockstat_lockstart = le->le_lockstart;
232 lockstat_lockend = le->le_lockend;
233 membar_sync();
234 getnanotime(&lockstat_stime);
235 lockstat_dev_enabled = le->le_mask;
236 LOCKSTAT_ENABLED_UPDATE();
237 }
238
239 /*
240 * Stop collecting lock statistics.
241 */
242 int
243 lockstat_stop(lsdisable_t *ld)
244 {
245 CPU_INFO_ITERATOR cii;
246 struct cpu_info *ci;
247 u_int cpuno, overflow;
248 struct timespec ts;
249 int error;
250 lwp_t *l;
251
252 KASSERT(lockstat_dev_enabled);
253
254 /*
255 * Set enabled false, force a write barrier, and wait for other CPUs
256 * to exit lockstat_event().
257 */
258 lockstat_dev_enabled = 0;
259 LOCKSTAT_ENABLED_UPDATE();
260 getnanotime(&ts);
261 tsleep(&lockstat_stop, PPAUSE, "lockstat", mstohz(10));
262
263 /*
264 * Did we run out of buffers while tracing?
265 */
266 overflow = 0;
267 for (CPU_INFO_FOREACH(cii, ci))
268 overflow += ((lscpu_t *)ci->ci_lockstat)->lc_overflow;
269
270 if (overflow != 0) {
271 error = EOVERFLOW;
272 log(LOG_NOTICE, "lockstat: %d buffer allocations failed\n",
273 overflow);
274 } else
275 error = 0;
276
277 lockstat_init_tables(NULL);
278
279 /* Run through all LWPs and clear the slate for the next run. */
280 mutex_enter(proc_lock);
281 LIST_FOREACH(l, &alllwp, l_list) {
282 l->l_pfailaddr = 0;
283 l->l_pfailtime = 0;
284 l->l_pfaillock = 0;
285 }
286 mutex_exit(proc_lock);
287
288 if (ld == NULL)
289 return error;
290
291 /*
292 * Fill out the disable struct for the caller.
293 */
294 timespecsub(&ts, &lockstat_stime, &ld->ld_time);
295 ld->ld_size = lockstat_sizeb;
296
297 cpuno = 0;
298 for (CPU_INFO_FOREACH(cii, ci)) {
299 if (cpuno >= sizeof(ld->ld_freq) / sizeof(ld->ld_freq[0])) {
300 log(LOG_WARNING, "lockstat: too many CPUs\n");
301 break;
302 }
303 ld->ld_freq[cpuno++] = cpu_frequency(ci);
304 }
305
306 return error;
307 }
308
309 /*
310 * Allocate buffers for lockstat_start().
311 */
312 int
313 lockstat_alloc(lsenable_t *le)
314 {
315 lsbuf_t *lb;
316 size_t sz;
317
318 KASSERT(!lockstat_dev_enabled);
319 lockstat_free();
320
321 sz = sizeof(*lb) * le->le_nbufs;
322
323 lb = kmem_zalloc(sz, KM_SLEEP);
324 if (lb == NULL)
325 return (ENOMEM);
326
327 KASSERT(!lockstat_dev_enabled);
328 KASSERT(lockstat_baseb == NULL);
329 lockstat_sizeb = sz;
330 lockstat_baseb = lb;
331
332 return (0);
333 }
334
335 /*
336 * Free allocated buffers after tracing has stopped.
337 */
338 void
339 lockstat_free(void)
340 {
341
342 KASSERT(!lockstat_dev_enabled);
343
344 if (lockstat_baseb != NULL) {
345 kmem_free(lockstat_baseb, lockstat_sizeb);
346 lockstat_baseb = NULL;
347 }
348 }
349
350 /*
351 * Main entry point from lock primatives.
352 */
353 void
354 lockstat_event(uintptr_t lock, uintptr_t callsite, u_int flags, u_int count,
355 uint64_t cycles)
356 {
357 lslist_t *ll;
358 lscpu_t *lc;
359 lsbuf_t *lb;
360 u_int event;
361 int s;
362
363 #ifdef KDTRACE_HOOKS
364 uint32_t id;
365 CTASSERT((LS_NPROBES & (LS_NPROBES - 1)) == 0);
366 if ((id = lockstat_probemap[LS_COMPRESS(flags)]) != 0)
367 (*lockstat_probe_func)(id, lock, callsite, flags, count,
368 cycles);
369 #endif
370
371 if ((flags & lockstat_dev_enabled) != flags || count == 0)
372 return;
373 if (lock < lockstat_lockstart || lock > lockstat_lockend)
374 return;
375 if (callsite < lockstat_csstart || callsite > lockstat_csend)
376 return;
377
378 callsite &= lockstat_csmask;
379 lock &= lockstat_lamask;
380
381 /*
382 * Find the table for this lock+callsite pair, and try to locate a
383 * buffer with the same key.
384 */
385 s = splhigh();
386 lc = curcpu()->ci_lockstat;
387 ll = &lc->lc_hash[LOCKSTAT_HASH(lock ^ callsite)];
388 event = (flags & LB_EVENT_MASK) - 1;
389
390 LIST_FOREACH(lb, ll, lb_chain.list) {
391 if (lb->lb_lock == lock && lb->lb_callsite == callsite)
392 break;
393 }
394
395 if (lb != NULL) {
396 /*
397 * We found a record. Move it to the front of the list, as
398 * we're likely to hit it again soon.
399 */
400 if (lb != LIST_FIRST(ll)) {
401 LIST_REMOVE(lb, lb_chain.list);
402 LIST_INSERT_HEAD(ll, lb, lb_chain.list);
403 }
404 lb->lb_counts[event] += count;
405 lb->lb_times[event] += cycles;
406 } else if ((lb = SLIST_FIRST(&lc->lc_free)) != NULL) {
407 /*
408 * Pinch a new buffer and fill it out.
409 */
410 SLIST_REMOVE_HEAD(&lc->lc_free, lb_chain.slist);
411 LIST_INSERT_HEAD(ll, lb, lb_chain.list);
412 lb->lb_flags = (uint16_t)flags;
413 lb->lb_lock = lock;
414 lb->lb_callsite = callsite;
415 lb->lb_counts[event] = count;
416 lb->lb_times[event] = cycles;
417 } else {
418 /*
419 * We didn't find a buffer and there were none free.
420 * lockstat_stop() will notice later on and report the
421 * error.
422 */
423 lc->lc_overflow++;
424 }
425
426 splx(s);
427 }
428
429 /*
430 * Accept an open() on /dev/lockstat.
431 */
432 int
433 lockstat_open(dev_t dev, int flag, int mode, lwp_t *l)
434 {
435
436 if (!__cpu_simple_lock_try(&lockstat_lock))
437 return EBUSY;
438 lockstat_lwp = curlwp;
439 return 0;
440 }
441
442 /*
443 * Accept the last close() on /dev/lockstat.
444 */
445 int
446 lockstat_close(dev_t dev, int flag, int mode, lwp_t *l)
447 {
448
449 lockstat_lwp = NULL;
450 __cpu_simple_unlock(&lockstat_lock);
451 return 0;
452 }
453
454 /*
455 * Handle control operations.
456 */
457 int
458 lockstat_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
459 {
460 lsenable_t *le;
461 int error;
462
463 if (lockstat_lwp != curlwp)
464 return EBUSY;
465
466 switch (cmd) {
467 case IOC_LOCKSTAT_GVERSION:
468 *(int *)data = LS_VERSION;
469 error = 0;
470 break;
471
472 case IOC_LOCKSTAT_ENABLE:
473 le = (lsenable_t *)data;
474
475 if (!cpu_hascounter()) {
476 error = ENODEV;
477 break;
478 }
479 if (lockstat_dev_enabled) {
480 error = EBUSY;
481 break;
482 }
483
484 /*
485 * Sanitize the arguments passed in and set up filtering.
486 */
487 if (le->le_nbufs == 0)
488 le->le_nbufs = LOCKSTAT_DEFBUFS;
489 else if (le->le_nbufs > LOCKSTAT_MAXBUFS ||
490 le->le_nbufs < LOCKSTAT_MINBUFS) {
491 error = EINVAL;
492 break;
493 }
494 if ((le->le_flags & LE_ONE_CALLSITE) == 0) {
495 le->le_csstart = 0;
496 le->le_csend = le->le_csstart - 1;
497 }
498 if ((le->le_flags & LE_ONE_LOCK) == 0) {
499 le->le_lockstart = 0;
500 le->le_lockend = le->le_lockstart - 1;
501 }
502 if ((le->le_mask & LB_EVENT_MASK) == 0)
503 return EINVAL;
504 if ((le->le_mask & LB_LOCK_MASK) == 0)
505 return EINVAL;
506
507 /*
508 * Start tracing.
509 */
510 if ((error = lockstat_alloc(le)) == 0)
511 lockstat_start(le);
512 break;
513
514 case IOC_LOCKSTAT_DISABLE:
515 if (!lockstat_dev_enabled)
516 error = EINVAL;
517 else
518 error = lockstat_stop((lsdisable_t *)data);
519 break;
520
521 default:
522 error = ENOTTY;
523 break;
524 }
525
526 return error;
527 }
528
529 /*
530 * Copy buffers out to user-space.
531 */
532 int
533 lockstat_read(dev_t dev, struct uio *uio, int flag)
534 {
535
536 if (curlwp != lockstat_lwp || lockstat_dev_enabled)
537 return EBUSY;
538 return uiomove(lockstat_baseb, lockstat_sizeb, uio);
539 }
540