tprof.c revision 1.7.4.1 1 /* $NetBSD: tprof.c,v 1.7.4.1 2011/02/08 16:19:55 bouyer Exp $ */
2
3 /*-
4 * Copyright (c)2008,2009,2010 YAMAMOTO Takashi,
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.7.4.1 2011/02/08 16:19:55 bouyer Exp $");
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35
36 #include <sys/cpu.h>
37 #include <sys/conf.h>
38 #include <sys/callout.h>
39 #include <sys/kmem.h>
40 #include <sys/module.h>
41 #include <sys/proc.h>
42 #include <sys/workqueue.h>
43 #include <sys/queue.h>
44
45 #include <dev/tprof/tprof.h>
46 #include <dev/tprof/tprof_ioctl.h>
47
48 /*
49 * locking order:
50 * tprof_reader_lock -> tprof_lock
51 * tprof_startstop_lock -> tprof_lock
52 */
53
54 /*
55 * protected by:
56 * L: tprof_lock
57 * R: tprof_reader_lock
58 * S: tprof_startstop_lock
59 * s: writer should hold tprof_startstop_lock and tprof_lock
60 * reader should hold tprof_startstop_lock or tprof_lock
61 */
62
63 typedef struct tprof_buf {
64 u_int b_used;
65 u_int b_size;
66 u_int b_overflow;
67 u_int b_unused;
68 STAILQ_ENTRY(tprof_buf) b_list;
69 tprof_sample_t b_data[];
70 } tprof_buf_t;
71 #define TPROF_BUF_BYTESIZE(sz) \
72 (sizeof(tprof_buf_t) + (sz) * sizeof(tprof_sample_t))
73 #define TPROF_MAX_SAMPLES_PER_BUF 10000
74
75 #define TPROF_MAX_BUF 100
76
77 typedef struct {
78 tprof_buf_t *c_buf;
79 struct work c_work;
80 callout_t c_callout;
81 } __aligned(CACHE_LINE_SIZE) tprof_cpu_t;
82
83 typedef struct tprof_backend {
84 const char *tb_name;
85 const tprof_backend_ops_t *tb_ops;
86 LIST_ENTRY(tprof_backend) tb_list;
87 int tb_usecount; /* S: */
88 } tprof_backend_t;
89
90 static kmutex_t tprof_lock;
91 static bool tprof_running; /* s: */
92 static u_int tprof_nworker; /* L: # of running worker LWPs */
93 static lwp_t *tprof_owner;
94 static STAILQ_HEAD(, tprof_buf) tprof_list; /* L: global buffer list */
95 static u_int tprof_nbuf_on_list; /* L: # of buffers on tprof_list */
96 static struct workqueue *tprof_wq;
97 static tprof_cpu_t tprof_cpus[MAXCPUS] __aligned(CACHE_LINE_SIZE);
98 static u_int tprof_samples_per_buf;
99
100 static tprof_backend_t *tprof_backend; /* S: */
101 static LIST_HEAD(, tprof_backend) tprof_backends =
102 LIST_HEAD_INITIALIZER(tprof_backend); /* S: */
103
104 static kmutex_t tprof_reader_lock;
105 static kcondvar_t tprof_reader_cv; /* L: */
106 static off_t tprof_reader_offset; /* R: */
107
108 static kmutex_t tprof_startstop_lock;
109 static kcondvar_t tprof_cv; /* L: */
110
111 static struct tprof_stat tprof_stat; /* L: */
112
113 static tprof_cpu_t *
114 tprof_cpu(struct cpu_info *ci)
115 {
116
117 return &tprof_cpus[cpu_index(ci)];
118 }
119
120 static tprof_cpu_t *
121 tprof_curcpu(void)
122 {
123
124 return tprof_cpu(curcpu());
125 }
126
127 static tprof_buf_t *
128 tprof_buf_alloc(void)
129 {
130 tprof_buf_t *new;
131 u_int size = tprof_samples_per_buf;
132
133 new = kmem_alloc(TPROF_BUF_BYTESIZE(size), KM_SLEEP);
134 new->b_used = 0;
135 new->b_size = size;
136 new->b_overflow = 0;
137 return new;
138 }
139
140 static void
141 tprof_buf_free(tprof_buf_t *buf)
142 {
143
144 kmem_free(buf, TPROF_BUF_BYTESIZE(buf->b_size));
145 }
146
147 static tprof_buf_t *
148 tprof_buf_switch(tprof_cpu_t *c, tprof_buf_t *new)
149 {
150 tprof_buf_t *old;
151
152 old = c->c_buf;
153 c->c_buf = new;
154 return old;
155 }
156
157 static tprof_buf_t *
158 tprof_buf_refresh(void)
159 {
160 tprof_cpu_t * const c = tprof_curcpu();
161 tprof_buf_t *new;
162
163 new = tprof_buf_alloc();
164 return tprof_buf_switch(c, new);
165 }
166
167 static void
168 tprof_worker(struct work *wk, void *dummy)
169 {
170 tprof_cpu_t * const c = tprof_curcpu();
171 tprof_buf_t *buf;
172 bool shouldstop;
173
174 KASSERT(wk == &c->c_work);
175 KASSERT(dummy == NULL);
176
177 /*
178 * get a per cpu buffer.
179 */
180 buf = tprof_buf_refresh();
181
182 /*
183 * and put it on the global list for read(2).
184 */
185 mutex_enter(&tprof_lock);
186 shouldstop = !tprof_running;
187 if (shouldstop) {
188 KASSERT(tprof_nworker > 0);
189 tprof_nworker--;
190 cv_broadcast(&tprof_cv);
191 cv_broadcast(&tprof_reader_cv);
192 }
193 if (buf->b_used == 0) {
194 tprof_stat.ts_emptybuf++;
195 } else if (tprof_nbuf_on_list < TPROF_MAX_BUF) {
196 tprof_stat.ts_sample += buf->b_used;
197 tprof_stat.ts_overflow += buf->b_overflow;
198 tprof_stat.ts_buf++;
199 STAILQ_INSERT_TAIL(&tprof_list, buf, b_list);
200 tprof_nbuf_on_list++;
201 buf = NULL;
202 cv_broadcast(&tprof_reader_cv);
203 } else {
204 tprof_stat.ts_dropbuf_sample += buf->b_used;
205 tprof_stat.ts_dropbuf++;
206 }
207 mutex_exit(&tprof_lock);
208 if (buf) {
209 tprof_buf_free(buf);
210 }
211 if (!shouldstop) {
212 callout_schedule(&c->c_callout, hz);
213 }
214 }
215
216 static void
217 tprof_kick(void *vp)
218 {
219 struct cpu_info * const ci = vp;
220 tprof_cpu_t * const c = tprof_cpu(ci);
221
222 workqueue_enqueue(tprof_wq, &c->c_work, ci);
223 }
224
225 static void
226 tprof_stop1(void)
227 {
228 CPU_INFO_ITERATOR cii;
229 struct cpu_info *ci;
230
231 KASSERT(mutex_owned(&tprof_startstop_lock));
232 KASSERT(tprof_nworker == 0);
233
234 for (CPU_INFO_FOREACH(cii, ci)) {
235 tprof_cpu_t * const c = tprof_cpu(ci);
236 tprof_buf_t *old;
237
238 old = tprof_buf_switch(c, NULL);
239 if (old != NULL) {
240 tprof_buf_free(old);
241 }
242 callout_destroy(&c->c_callout);
243 }
244 workqueue_destroy(tprof_wq);
245 }
246
247 static int
248 tprof_start(const struct tprof_param *param)
249 {
250 CPU_INFO_ITERATOR cii;
251 struct cpu_info *ci;
252 int error;
253 uint64_t freq;
254 tprof_backend_t *tb;
255
256 KASSERT(mutex_owned(&tprof_startstop_lock));
257 if (tprof_running) {
258 error = EBUSY;
259 goto done;
260 }
261
262 tb = tprof_backend;
263 if (tb == NULL) {
264 error = ENOENT;
265 goto done;
266 }
267 if (tb->tb_usecount > 0) {
268 error = EBUSY;
269 goto done;
270 }
271
272 tb->tb_usecount++;
273 freq = tb->tb_ops->tbo_estimate_freq();
274 tprof_samples_per_buf = MIN(freq * 2, TPROF_MAX_SAMPLES_PER_BUF);
275
276 error = workqueue_create(&tprof_wq, "tprofmv", tprof_worker, NULL,
277 PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE | WQ_PERCPU);
278 if (error != 0) {
279 goto done;
280 }
281
282 for (CPU_INFO_FOREACH(cii, ci)) {
283 tprof_cpu_t * const c = tprof_cpu(ci);
284 tprof_buf_t *new;
285 tprof_buf_t *old;
286
287 new = tprof_buf_alloc();
288 old = tprof_buf_switch(c, new);
289 if (old != NULL) {
290 tprof_buf_free(old);
291 }
292 callout_init(&c->c_callout, CALLOUT_MPSAFE);
293 callout_setfunc(&c->c_callout, tprof_kick, ci);
294 }
295
296 error = tb->tb_ops->tbo_start(NULL);
297 if (error != 0) {
298 tprof_stop1();
299 goto done;
300 }
301
302 mutex_enter(&tprof_lock);
303 tprof_running = true;
304 mutex_exit(&tprof_lock);
305 for (CPU_INFO_FOREACH(cii, ci)) {
306 tprof_cpu_t * const c = tprof_cpu(ci);
307
308 mutex_enter(&tprof_lock);
309 tprof_nworker++;
310 mutex_exit(&tprof_lock);
311 workqueue_enqueue(tprof_wq, &c->c_work, ci);
312 }
313 done:
314 return error;
315 }
316
317 static void
318 tprof_stop(void)
319 {
320 tprof_backend_t *tb;
321
322 KASSERT(mutex_owned(&tprof_startstop_lock));
323 if (!tprof_running) {
324 goto done;
325 }
326
327 tb = tprof_backend;
328 KASSERT(tb->tb_usecount > 0);
329 tb->tb_ops->tbo_stop(NULL);
330 tb->tb_usecount--;
331
332 mutex_enter(&tprof_lock);
333 tprof_running = false;
334 cv_broadcast(&tprof_reader_cv);
335 while (tprof_nworker > 0) {
336 cv_wait(&tprof_cv, &tprof_lock);
337 }
338 mutex_exit(&tprof_lock);
339
340 tprof_stop1();
341 done:
342 ;
343 }
344
345 /*
346 * tprof_clear: drain unread samples.
347 */
348
349 static void
350 tprof_clear(void)
351 {
352 tprof_buf_t *buf;
353
354 mutex_enter(&tprof_reader_lock);
355 mutex_enter(&tprof_lock);
356 while ((buf = STAILQ_FIRST(&tprof_list)) != NULL) {
357 if (buf != NULL) {
358 STAILQ_REMOVE_HEAD(&tprof_list, b_list);
359 KASSERT(tprof_nbuf_on_list > 0);
360 tprof_nbuf_on_list--;
361 mutex_exit(&tprof_lock);
362 tprof_buf_free(buf);
363 mutex_enter(&tprof_lock);
364 }
365 }
366 KASSERT(tprof_nbuf_on_list == 0);
367 mutex_exit(&tprof_lock);
368 tprof_reader_offset = 0;
369 mutex_exit(&tprof_reader_lock);
370
371 memset(&tprof_stat, 0, sizeof(tprof_stat));
372 }
373
374 static tprof_backend_t *
375 tprof_backend_lookup(const char *name)
376 {
377 tprof_backend_t *tb;
378
379 KASSERT(mutex_owned(&tprof_startstop_lock));
380
381 LIST_FOREACH(tb, &tprof_backends, tb_list) {
382 if (!strcmp(tb->tb_name, name)) {
383 return tb;
384 }
385 }
386 return NULL;
387 }
388
389 /* -------------------- backend interfaces */
390
391 /*
392 * tprof_sample: record a sample on the per-cpu buffer.
393 *
394 * be careful; can be called in NMI context.
395 * we are bluntly assuming that curcpu() and curlwp->l_proc->p_pid are safe.
396 */
397
398 void
399 tprof_sample(tprof_backend_cookie_t *cookie, const tprof_frame_info_t *tfi)
400 {
401 tprof_cpu_t * const c = tprof_curcpu();
402 tprof_buf_t * const buf = c->c_buf;
403 tprof_sample_t *sp;
404 const uintptr_t pc = tfi->tfi_pc;
405 u_int idx;
406
407 idx = buf->b_used;
408 if (__predict_false(idx >= buf->b_size)) {
409 buf->b_overflow++;
410 return;
411 }
412 sp = &buf->b_data[idx];
413 sp->s_pid = curlwp->l_proc->p_pid;
414 sp->s_flags = (tfi->tfi_inkernel) ? TPROF_SAMPLE_INKERNEL : 0;
415 sp->s_pc = pc;
416 buf->b_used = idx + 1;
417 }
418
419 /*
420 * tprof_backend_register:
421 */
422
423 int
424 tprof_backend_register(const char *name, const tprof_backend_ops_t *ops,
425 int vers)
426 {
427 tprof_backend_t *tb;
428
429 if (vers != TPROF_BACKEND_VERSION) {
430 return EINVAL;
431 }
432
433 mutex_enter(&tprof_startstop_lock);
434 tb = tprof_backend_lookup(name);
435 if (tb != NULL) {
436 mutex_exit(&tprof_startstop_lock);
437 return EEXIST;
438 }
439 #if 1 /* XXX for now */
440 if (!LIST_EMPTY(&tprof_backends)) {
441 mutex_exit(&tprof_startstop_lock);
442 return ENOTSUP;
443 }
444 #endif
445 tb = kmem_alloc(sizeof(*tb), KM_SLEEP);
446 tb->tb_name = name;
447 tb->tb_ops = ops;
448 tb->tb_usecount = 0;
449 LIST_INSERT_HEAD(&tprof_backends, tb, tb_list);
450 #if 1 /* XXX for now */
451 if (tprof_backend == NULL) {
452 tprof_backend = tb;
453 }
454 #endif
455 mutex_exit(&tprof_startstop_lock);
456
457 return 0;
458 }
459
460 /*
461 * tprof_backend_unregister:
462 */
463
464 int
465 tprof_backend_unregister(const char *name)
466 {
467 tprof_backend_t *tb;
468
469 mutex_enter(&tprof_startstop_lock);
470 tb = tprof_backend_lookup(name);
471 #if defined(DIAGNOSTIC)
472 if (tb == NULL) {
473 mutex_exit(&tprof_startstop_lock);
474 panic("%s: not found '%s'", __func__, name);
475 }
476 #endif /* defined(DIAGNOSTIC) */
477 if (tb->tb_usecount > 0) {
478 mutex_exit(&tprof_startstop_lock);
479 return EBUSY;
480 }
481 #if 1 /* XXX for now */
482 if (tprof_backend == tb) {
483 tprof_backend = NULL;
484 }
485 #endif
486 LIST_REMOVE(tb, tb_list);
487 mutex_exit(&tprof_startstop_lock);
488
489 kmem_free(tb, sizeof(*tb));
490
491 return 0;
492 }
493
494 /* -------------------- cdevsw interfaces */
495
496 void tprofattach(int);
497
498 static int
499 tprof_open(dev_t dev, int flags, int type, struct lwp *l)
500 {
501
502 if (minor(dev) != 0) {
503 return EXDEV;
504 }
505 mutex_enter(&tprof_lock);
506 if (tprof_owner != NULL) {
507 mutex_exit(&tprof_lock);
508 return EBUSY;
509 }
510 tprof_owner = curlwp;
511 mutex_exit(&tprof_lock);
512
513 return 0;
514 }
515
516 static int
517 tprof_close(dev_t dev, int flags, int type, struct lwp *l)
518 {
519
520 KASSERT(minor(dev) == 0);
521
522 mutex_enter(&tprof_startstop_lock);
523 mutex_enter(&tprof_lock);
524 tprof_owner = NULL;
525 mutex_exit(&tprof_lock);
526 tprof_stop();
527 tprof_clear();
528 mutex_exit(&tprof_startstop_lock);
529
530 return 0;
531 }
532
533 static int
534 tprof_read(dev_t dev, struct uio *uio, int flags)
535 {
536 tprof_buf_t *buf;
537 size_t bytes;
538 size_t resid;
539 size_t done;
540 int error = 0;
541
542 KASSERT(minor(dev) == 0);
543 mutex_enter(&tprof_reader_lock);
544 while (uio->uio_resid > 0 && error == 0) {
545 /*
546 * take the first buffer from the list.
547 */
548 mutex_enter(&tprof_lock);
549 buf = STAILQ_FIRST(&tprof_list);
550 if (buf == NULL) {
551 if (tprof_nworker == 0) {
552 mutex_exit(&tprof_lock);
553 error = 0;
554 break;
555 }
556 mutex_exit(&tprof_reader_lock);
557 error = cv_wait_sig(&tprof_reader_cv, &tprof_lock);
558 mutex_exit(&tprof_lock);
559 mutex_enter(&tprof_reader_lock);
560 continue;
561 }
562 STAILQ_REMOVE_HEAD(&tprof_list, b_list);
563 KASSERT(tprof_nbuf_on_list > 0);
564 tprof_nbuf_on_list--;
565 mutex_exit(&tprof_lock);
566
567 /*
568 * copy it out.
569 */
570 bytes = MIN(buf->b_used * sizeof(tprof_sample_t) -
571 tprof_reader_offset, uio->uio_resid);
572 resid = uio->uio_resid;
573 error = uiomove((char *)buf->b_data + tprof_reader_offset,
574 bytes, uio);
575 done = resid - uio->uio_resid;
576 tprof_reader_offset += done;
577
578 /*
579 * if we didn't consume the whole buffer,
580 * put it back to the list.
581 */
582 if (tprof_reader_offset <
583 buf->b_used * sizeof(tprof_sample_t)) {
584 mutex_enter(&tprof_lock);
585 STAILQ_INSERT_HEAD(&tprof_list, buf, b_list);
586 tprof_nbuf_on_list++;
587 cv_broadcast(&tprof_reader_cv);
588 mutex_exit(&tprof_lock);
589 } else {
590 tprof_buf_free(buf);
591 tprof_reader_offset = 0;
592 }
593 }
594 mutex_exit(&tprof_reader_lock);
595
596 return error;
597 }
598
599 static int
600 tprof_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
601 {
602 const struct tprof_param *param;
603 int error = 0;
604
605 KASSERT(minor(dev) == 0);
606
607 switch (cmd) {
608 case TPROF_IOC_GETVERSION:
609 *(int *)data = TPROF_VERSION;
610 break;
611 case TPROF_IOC_START:
612 param = data;
613 mutex_enter(&tprof_startstop_lock);
614 error = tprof_start(param);
615 mutex_exit(&tprof_startstop_lock);
616 break;
617 case TPROF_IOC_STOP:
618 mutex_enter(&tprof_startstop_lock);
619 tprof_stop();
620 mutex_exit(&tprof_startstop_lock);
621 break;
622 case TPROF_IOC_GETSTAT:
623 mutex_enter(&tprof_lock);
624 memcpy(data, &tprof_stat, sizeof(tprof_stat));
625 mutex_exit(&tprof_lock);
626 break;
627 default:
628 error = EINVAL;
629 break;
630 }
631
632 return error;
633 }
634
635 const struct cdevsw tprof_cdevsw = {
636 .d_open = tprof_open,
637 .d_close = tprof_close,
638 .d_read = tprof_read,
639 .d_write = nowrite,
640 .d_ioctl = tprof_ioctl,
641 .d_stop = nostop,
642 .d_tty = notty,
643 .d_poll = nopoll,
644 .d_mmap = nommap,
645 .d_kqfilter = nokqfilter,
646 .d_flag = D_OTHER | D_MPSAFE,
647 };
648
649 void
650 tprofattach(int nunits)
651 {
652
653 /* nothing */
654 }
655
656 MODULE(MODULE_CLASS_DRIVER, tprof, NULL);
657
658 static void
659 tprof_driver_init(void)
660 {
661
662 mutex_init(&tprof_lock, MUTEX_DEFAULT, IPL_NONE);
663 mutex_init(&tprof_reader_lock, MUTEX_DEFAULT, IPL_NONE);
664 mutex_init(&tprof_startstop_lock, MUTEX_DEFAULT, IPL_NONE);
665 cv_init(&tprof_cv, "tprof");
666 cv_init(&tprof_reader_cv, "tprof_rd");
667 STAILQ_INIT(&tprof_list);
668 }
669
670 static void
671 tprof_driver_fini(void)
672 {
673
674 mutex_destroy(&tprof_lock);
675 mutex_destroy(&tprof_reader_lock);
676 mutex_destroy(&tprof_startstop_lock);
677 cv_destroy(&tprof_cv);
678 cv_destroy(&tprof_reader_cv);
679 }
680
681 static int
682 tprof_modcmd(modcmd_t cmd, void *arg)
683 {
684
685 switch (cmd) {
686 case MODULE_CMD_INIT:
687 tprof_driver_init();
688 #if defined(_MODULE)
689 {
690 devmajor_t bmajor = NODEVMAJOR;
691 devmajor_t cmajor = NODEVMAJOR;
692 int error;
693
694 error = devsw_attach("tprof", NULL, &bmajor,
695 &tprof_cdevsw, &cmajor);
696 if (error) {
697 tprof_driver_fini();
698 return error;
699 }
700 }
701 #endif /* defined(_MODULE) */
702 return 0;
703
704 case MODULE_CMD_FINI:
705 #if defined(_MODULE)
706 {
707 int error;
708 error = devsw_detach(NULL, &tprof_cdevsw);
709 if (error) {
710 return error;
711 }
712 }
713 #endif /* defined(_MODULE) */
714 tprof_driver_fini();
715 return 0;
716
717 default:
718 return ENOTTY;
719 }
720 }
721