lockstat.c revision 1.13.6.2 1 /* $NetBSD: lockstat.c,v 1.13.6.2 2008/06/02 13:23:11 mjf 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.13.6.2 2008/06/02 13:23:11 mjf 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 50000
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 uintptr_t lockstat_csstart;
101 uintptr_t lockstat_csend;
102 uintptr_t lockstat_csmask;
103 uintptr_t lockstat_lamask;
104 uintptr_t lockstat_lockstart;
105 uintptr_t lockstat_lockend;
106 __cpu_simple_lock_t lockstat_lock;
107 lwp_t *lockstat_lwp;
108 lsbuf_t *lockstat_baseb;
109 size_t lockstat_sizeb;
110 int lockstat_busy;
111 struct timespec lockstat_stime;
112
113 const struct cdevsw lockstat_cdevsw = {
114 lockstat_open, lockstat_close, lockstat_read, nowrite, lockstat_ioctl,
115 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER | D_MPSAFE
116 };
117
118 /*
119 * Called when the pseudo-driver is attached.
120 */
121 void
122 lockstatattach(int nunits)
123 {
124 int maj = cdevsw_lookup_major(&lockstat_cdevsw);
125
126 (void)nunits;
127
128 __cpu_simple_lock_init(&lockstat_lock);
129
130 device_register_name(makedev(maj, 0), NULL, true, DEV_OTHER, "lockstat");
131 }
132
133 /*
134 * Prepare the per-CPU tables for use, or clear down tables when tracing is
135 * stopped.
136 */
137 void
138 lockstat_init_tables(lsenable_t *le)
139 {
140 int i, per, slop, cpuno;
141 CPU_INFO_ITERATOR cii;
142 struct cpu_info *ci;
143 lscpu_t *lc;
144 lsbuf_t *lb;
145
146 KASSERT(!lockstat_enabled);
147
148 for (CPU_INFO_FOREACH(cii, ci)) {
149 if (ci->ci_lockstat != NULL) {
150 kmem_free(ci->ci_lockstat, sizeof(lscpu_t));
151 ci->ci_lockstat = NULL;
152 }
153 }
154
155 if (le == NULL)
156 return;
157
158 lb = lockstat_baseb;
159 per = le->le_nbufs / ncpu;
160 slop = le->le_nbufs - (per * ncpu);
161 cpuno = 0;
162 for (CPU_INFO_FOREACH(cii, ci)) {
163 lc = kmem_alloc(sizeof(*lc), KM_SLEEP);
164 lc->lc_overflow = 0;
165 ci->ci_lockstat = lc;
166
167 SLIST_INIT(&lc->lc_free);
168 for (i = 0; i < LOCKSTAT_HASH_SIZE; i++)
169 LIST_INIT(&lc->lc_hash[i]);
170
171 for (i = per; i != 0; i--, lb++) {
172 lb->lb_cpu = (uint16_t)cpuno;
173 SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
174 }
175 if (--slop > 0) {
176 lb->lb_cpu = (uint16_t)cpuno;
177 SLIST_INSERT_HEAD(&lc->lc_free, lb, lb_chain.slist);
178 lb++;
179 }
180 cpuno++;
181 }
182 }
183
184 /*
185 * Start collecting lock statistics.
186 */
187 void
188 lockstat_start(lsenable_t *le)
189 {
190
191 KASSERT(!lockstat_enabled);
192
193 lockstat_init_tables(le);
194
195 if ((le->le_flags & LE_CALLSITE) != 0)
196 lockstat_csmask = (uintptr_t)-1LL;
197 else
198 lockstat_csmask = 0;
199
200 if ((le->le_flags & LE_LOCK) != 0)
201 lockstat_lamask = (uintptr_t)-1LL;
202 else
203 lockstat_lamask = 0;
204
205 lockstat_csstart = le->le_csstart;
206 lockstat_csend = le->le_csend;
207 lockstat_lockstart = le->le_lockstart;
208 lockstat_lockstart = le->le_lockstart;
209 lockstat_lockend = le->le_lockend;
210 membar_sync();
211 getnanotime(&lockstat_stime);
212 lockstat_enabled = le->le_mask;
213 membar_producer();
214 }
215
216 /*
217 * Stop collecting lock statistics.
218 */
219 int
220 lockstat_stop(lsdisable_t *ld)
221 {
222 CPU_INFO_ITERATOR cii;
223 struct cpu_info *ci;
224 u_int cpuno, overflow;
225 struct timespec ts;
226 int error;
227 lwp_t *l;
228
229 KASSERT(lockstat_enabled);
230
231 /*
232 * Set enabled false, force a write barrier, and wait for other CPUs
233 * to exit lockstat_event().
234 */
235 lockstat_enabled = 0;
236 membar_producer();
237 getnanotime(&ts);
238 tsleep(&lockstat_stop, PPAUSE, "lockstat", mstohz(10));
239
240 /*
241 * Did we run out of buffers while tracing?
242 */
243 overflow = 0;
244 for (CPU_INFO_FOREACH(cii, ci))
245 overflow += ((lscpu_t *)ci->ci_lockstat)->lc_overflow;
246
247 if (overflow != 0) {
248 error = EOVERFLOW;
249 log(LOG_NOTICE, "lockstat: %d buffer allocations failed\n",
250 overflow);
251 } else
252 error = 0;
253
254 lockstat_init_tables(NULL);
255
256 /* Run through all LWPs and clear the slate for the next run. */
257 mutex_enter(proc_lock);
258 LIST_FOREACH(l, &alllwp, l_list) {
259 l->l_pfailaddr = 0;
260 l->l_pfailtime = 0;
261 l->l_pfaillock = 0;
262 }
263 mutex_exit(proc_lock);
264
265 if (ld == NULL)
266 return error;
267
268 /*
269 * Fill out the disable struct for the caller.
270 */
271 timespecsub(&ts, &lockstat_stime, &ld->ld_time);
272 ld->ld_size = lockstat_sizeb;
273
274 cpuno = 0;
275 for (CPU_INFO_FOREACH(cii, ci)) {
276 if (cpuno > sizeof(ld->ld_freq) / sizeof(ld->ld_freq[0])) {
277 log(LOG_WARNING, "lockstat: too many CPUs\n");
278 break;
279 }
280 ld->ld_freq[cpuno++] = cpu_frequency(ci);
281 }
282
283 return error;
284 }
285
286 /*
287 * Allocate buffers for lockstat_start().
288 */
289 int
290 lockstat_alloc(lsenable_t *le)
291 {
292 lsbuf_t *lb;
293 size_t sz;
294
295 KASSERT(!lockstat_enabled);
296 lockstat_free();
297
298 sz = sizeof(*lb) * le->le_nbufs;
299
300 lb = kmem_zalloc(sz, KM_SLEEP);
301 if (lb == NULL)
302 return (ENOMEM);
303
304 KASSERT(!lockstat_enabled);
305 KASSERT(lockstat_baseb == NULL);
306 lockstat_sizeb = sz;
307 lockstat_baseb = lb;
308
309 return (0);
310 }
311
312 /*
313 * Free allocated buffers after tracing has stopped.
314 */
315 void
316 lockstat_free(void)
317 {
318
319 KASSERT(!lockstat_enabled);
320
321 if (lockstat_baseb != NULL) {
322 kmem_free(lockstat_baseb, lockstat_sizeb);
323 lockstat_baseb = NULL;
324 }
325 }
326
327 /*
328 * Main entry point from lock primatives.
329 */
330 void
331 lockstat_event(uintptr_t lock, uintptr_t callsite, u_int flags, u_int count,
332 uint64_t cycles)
333 {
334 lslist_t *ll;
335 lscpu_t *lc;
336 lsbuf_t *lb;
337 u_int event;
338 int s;
339
340 if ((flags & lockstat_enabled) != flags || count == 0)
341 return;
342 if (lock < lockstat_lockstart || lock > lockstat_lockend)
343 return;
344 if (callsite < lockstat_csstart || callsite > lockstat_csend)
345 return;
346
347 callsite &= lockstat_csmask;
348 lock &= lockstat_lamask;
349
350 /*
351 * Find the table for this lock+callsite pair, and try to locate a
352 * buffer with the same key.
353 */
354 s = splhigh();
355 lc = curcpu()->ci_lockstat;
356 ll = &lc->lc_hash[LOCKSTAT_HASH(lock ^ callsite)];
357 event = (flags & LB_EVENT_MASK) - 1;
358
359 LIST_FOREACH(lb, ll, lb_chain.list) {
360 if (lb->lb_lock == lock && lb->lb_callsite == callsite)
361 break;
362 }
363
364 if (lb != NULL) {
365 /*
366 * We found a record. Move it to the front of the list, as
367 * we're likely to hit it again soon.
368 */
369 if (lb != LIST_FIRST(ll)) {
370 LIST_REMOVE(lb, lb_chain.list);
371 LIST_INSERT_HEAD(ll, lb, lb_chain.list);
372 }
373 lb->lb_counts[event] += count;
374 lb->lb_times[event] += cycles;
375 } else if ((lb = SLIST_FIRST(&lc->lc_free)) != NULL) {
376 /*
377 * Pinch a new buffer and fill it out.
378 */
379 SLIST_REMOVE_HEAD(&lc->lc_free, lb_chain.slist);
380 LIST_INSERT_HEAD(ll, lb, lb_chain.list);
381 lb->lb_flags = (uint16_t)flags;
382 lb->lb_lock = lock;
383 lb->lb_callsite = callsite;
384 lb->lb_counts[event] = count;
385 lb->lb_times[event] = cycles;
386 } else {
387 /*
388 * We didn't find a buffer and there were none free.
389 * lockstat_stop() will notice later on and report the
390 * error.
391 */
392 lc->lc_overflow++;
393 }
394
395 splx(s);
396 }
397
398 /*
399 * Accept an open() on /dev/lockstat.
400 */
401 int
402 lockstat_open(dev_t dev, int flag, int mode, lwp_t *l)
403 {
404
405 if (!__cpu_simple_lock_try(&lockstat_lock))
406 return EBUSY;
407 lockstat_lwp = curlwp;
408 return 0;
409 }
410
411 /*
412 * Accept the last close() on /dev/lockstat.
413 */
414 int
415 lockstat_close(dev_t dev, int flag, int mode, lwp_t *l)
416 {
417
418 lockstat_lwp = NULL;
419 __cpu_simple_unlock(&lockstat_lock);
420 return 0;
421 }
422
423 /*
424 * Handle control operations.
425 */
426 int
427 lockstat_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
428 {
429 lsenable_t *le;
430 int error;
431
432 if (lockstat_lwp != curlwp)
433 return EBUSY;
434
435 switch (cmd) {
436 case IOC_LOCKSTAT_GVERSION:
437 *(int *)data = LS_VERSION;
438 error = 0;
439 break;
440
441 case IOC_LOCKSTAT_ENABLE:
442 le = (lsenable_t *)data;
443
444 if (!cpu_hascounter()) {
445 error = ENODEV;
446 break;
447 }
448 if (lockstat_enabled) {
449 error = EBUSY;
450 break;
451 }
452
453 /*
454 * Sanitize the arguments passed in and set up filtering.
455 */
456 if (le->le_nbufs == 0)
457 le->le_nbufs = LOCKSTAT_DEFBUFS;
458 else if (le->le_nbufs > LOCKSTAT_MAXBUFS ||
459 le->le_nbufs < LOCKSTAT_MINBUFS) {
460 error = EINVAL;
461 break;
462 }
463 if ((le->le_flags & LE_ONE_CALLSITE) == 0) {
464 le->le_csstart = 0;
465 le->le_csend = le->le_csstart - 1;
466 }
467 if ((le->le_flags & LE_ONE_LOCK) == 0) {
468 le->le_lockstart = 0;
469 le->le_lockend = le->le_lockstart - 1;
470 }
471 if ((le->le_mask & LB_EVENT_MASK) == 0)
472 return EINVAL;
473 if ((le->le_mask & LB_LOCK_MASK) == 0)
474 return EINVAL;
475
476 /*
477 * Start tracing.
478 */
479 if ((error = lockstat_alloc(le)) == 0)
480 lockstat_start(le);
481 break;
482
483 case IOC_LOCKSTAT_DISABLE:
484 if (!lockstat_enabled)
485 error = EINVAL;
486 else
487 error = lockstat_stop((lsdisable_t *)data);
488 break;
489
490 default:
491 error = ENOTTY;
492 break;
493 }
494
495 return error;
496 }
497
498 /*
499 * Copy buffers out to user-space.
500 */
501 int
502 lockstat_read(dev_t dev, struct uio *uio, int flag)
503 {
504
505 if (curlwp != lockstat_lwp || lockstat_enabled)
506 return EBUSY;
507 return uiomove(lockstat_baseb, lockstat_sizeb, uio);
508 }
509