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