Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.1.16.1
      1  1.1.16.1  yamt /*	$NetBSD: tprof.c,v 1.1.16.1 2008/05/18 12:34:50 yamt Exp $	*/
      2       1.1  yamt 
      3       1.1  yamt /*-
      4       1.1  yamt  * Copyright (c)2008 YAMAMOTO Takashi,
      5       1.1  yamt  * All rights reserved.
      6       1.1  yamt  *
      7       1.1  yamt  * Redistribution and use in source and binary forms, with or without
      8       1.1  yamt  * modification, are permitted provided that the following conditions
      9       1.1  yamt  * are met:
     10       1.1  yamt  * 1. Redistributions of source code must retain the above copyright
     11       1.1  yamt  *    notice, this list of conditions and the following disclaimer.
     12       1.1  yamt  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1  yamt  *    notice, this list of conditions and the following disclaimer in the
     14       1.1  yamt  *    documentation and/or other materials provided with the distribution.
     15       1.1  yamt  *
     16       1.1  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
     17       1.1  yamt  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     18       1.1  yamt  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     19       1.1  yamt  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     20       1.1  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     21       1.1  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     22       1.1  yamt  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     23       1.1  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     24       1.1  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     25       1.1  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26       1.1  yamt  * SUCH DAMAGE.
     27       1.1  yamt  */
     28       1.1  yamt 
     29       1.1  yamt #include <sys/cdefs.h>
     30  1.1.16.1  yamt __KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.1.16.1 2008/05/18 12:34:50 yamt Exp $");
     31       1.1  yamt 
     32       1.1  yamt #include <sys/param.h>
     33       1.1  yamt #include <sys/systm.h>
     34       1.1  yamt #include <sys/kernel.h>
     35       1.1  yamt 
     36       1.1  yamt #include <sys/cpu.h>
     37       1.1  yamt #include <sys/conf.h>
     38       1.1  yamt #include <sys/callout.h>
     39       1.1  yamt #include <sys/kmem.h>
     40       1.1  yamt #include <sys/workqueue.h>
     41       1.1  yamt #include <sys/queue.h>
     42       1.1  yamt 
     43       1.1  yamt #include <dev/tprof/tprof.h>
     44       1.1  yamt #include <dev/tprof/tprof_ioctl.h>
     45       1.1  yamt 
     46       1.1  yamt #include <machine/db_machdep.h> /* PC_REGS */
     47       1.1  yamt 
     48       1.1  yamt typedef struct {
     49       1.1  yamt 	uintptr_t s_pc;	/* program counter */
     50       1.1  yamt } tprof_sample_t;
     51       1.1  yamt 
     52       1.1  yamt typedef struct tprof_buf {
     53       1.1  yamt 	u_int b_used;
     54       1.1  yamt 	u_int b_size;
     55       1.1  yamt 	u_int b_overflow;
     56       1.1  yamt 	u_int b_unused;
     57       1.1  yamt 	STAILQ_ENTRY(tprof_buf) b_list;
     58       1.1  yamt 	tprof_sample_t b_data[];
     59       1.1  yamt } tprof_buf_t;
     60       1.1  yamt #define	TPROF_BUF_BYTESIZE(sz) \
     61       1.1  yamt 	(sizeof(tprof_buf_t) + (sz) * sizeof(tprof_sample_t))
     62       1.1  yamt #define	TPROF_MAX_SAMPLES_PER_BUF	10000
     63       1.1  yamt 
     64       1.1  yamt #define	TPROF_MAX_BUF			100
     65       1.1  yamt 
     66       1.1  yamt typedef struct {
     67       1.1  yamt 	tprof_buf_t *c_buf;
     68       1.1  yamt 	struct work c_work;
     69       1.1  yamt 	callout_t c_callout;
     70       1.1  yamt } __aligned(CACHE_LINE_SIZE) tprof_cpu_t;
     71       1.1  yamt 
     72       1.1  yamt static kmutex_t tprof_lock;
     73       1.1  yamt static bool tprof_running;
     74       1.1  yamt static u_int tprof_nworker;
     75       1.1  yamt static lwp_t *tprof_owner;
     76       1.1  yamt static STAILQ_HEAD(, tprof_buf) tprof_list;
     77       1.1  yamt static u_int tprof_nbuf_on_list;
     78       1.1  yamt static struct workqueue *tprof_wq;
     79       1.1  yamt static tprof_cpu_t tprof_cpus[MAXCPUS] __aligned(CACHE_LINE_SIZE);
     80       1.1  yamt static u_int tprof_samples_per_buf;
     81       1.1  yamt 
     82       1.1  yamt static kmutex_t tprof_reader_lock;
     83       1.1  yamt static kcondvar_t tprof_reader_cv;
     84       1.1  yamt static off_t tprof_reader_offset;
     85       1.1  yamt 
     86       1.1  yamt static kmutex_t tprof_startstop_lock;
     87       1.1  yamt static kcondvar_t tprof_cv;
     88       1.1  yamt 
     89       1.1  yamt static struct tprof_stat tprof_stat;
     90       1.1  yamt 
     91       1.1  yamt static tprof_cpu_t *
     92       1.1  yamt tprof_cpu(struct cpu_info *ci)
     93       1.1  yamt {
     94       1.1  yamt 
     95       1.1  yamt 	return &tprof_cpus[cpu_index(ci)];
     96       1.1  yamt }
     97       1.1  yamt 
     98       1.1  yamt static tprof_cpu_t *
     99       1.1  yamt tprof_curcpu(void)
    100       1.1  yamt {
    101       1.1  yamt 
    102       1.1  yamt 	return tprof_cpu(curcpu());
    103       1.1  yamt }
    104       1.1  yamt 
    105       1.1  yamt static tprof_buf_t *
    106       1.1  yamt tprof_buf_alloc(void)
    107       1.1  yamt {
    108       1.1  yamt 	tprof_buf_t *new;
    109       1.1  yamt 	u_int size = tprof_samples_per_buf;
    110       1.1  yamt 
    111       1.1  yamt 	new = kmem_alloc(TPROF_BUF_BYTESIZE(size), KM_SLEEP);
    112       1.1  yamt 	new->b_used = 0;
    113       1.1  yamt 	new->b_size = size;
    114       1.1  yamt 	new->b_overflow = 0;
    115       1.1  yamt 	return new;
    116       1.1  yamt }
    117       1.1  yamt 
    118       1.1  yamt static void
    119       1.1  yamt tprof_buf_free(tprof_buf_t *buf)
    120       1.1  yamt {
    121       1.1  yamt 
    122       1.1  yamt 	kmem_free(buf, TPROF_BUF_BYTESIZE(buf->b_size));
    123       1.1  yamt }
    124       1.1  yamt 
    125       1.1  yamt static tprof_buf_t *
    126       1.1  yamt tprof_buf_switch(tprof_cpu_t *c, tprof_buf_t *new)
    127       1.1  yamt {
    128       1.1  yamt 	tprof_buf_t *old;
    129       1.1  yamt 
    130       1.1  yamt 	old = c->c_buf;
    131       1.1  yamt 	c->c_buf = new;
    132       1.1  yamt 	return old;
    133       1.1  yamt }
    134       1.1  yamt 
    135       1.1  yamt static tprof_buf_t *
    136       1.1  yamt tprof_buf_refresh(void)
    137       1.1  yamt {
    138       1.1  yamt 	tprof_cpu_t * const c = tprof_curcpu();
    139       1.1  yamt 	tprof_buf_t *new;
    140       1.1  yamt 
    141       1.1  yamt 	new = tprof_buf_alloc();
    142       1.1  yamt 	return tprof_buf_switch(c, new);
    143       1.1  yamt }
    144       1.1  yamt 
    145       1.1  yamt static void
    146       1.1  yamt tprof_worker(struct work *wk, void *dummy)
    147       1.1  yamt {
    148       1.1  yamt 	tprof_cpu_t * const c = tprof_curcpu();
    149       1.1  yamt 	tprof_buf_t *buf;
    150       1.1  yamt 	bool shouldstop;
    151       1.1  yamt 
    152       1.1  yamt 	KASSERT(wk == &c->c_work);
    153       1.1  yamt 	KASSERT(dummy == NULL);
    154       1.1  yamt 
    155       1.1  yamt 	/*
    156       1.1  yamt 	 * get a per cpu buffer.
    157       1.1  yamt 	 */
    158       1.1  yamt 	buf = tprof_buf_refresh();
    159       1.1  yamt 
    160       1.1  yamt 	/*
    161       1.1  yamt 	 * and put it on the global list for read(2).
    162       1.1  yamt 	 */
    163       1.1  yamt 	mutex_enter(&tprof_lock);
    164       1.1  yamt 	shouldstop = !tprof_running;
    165       1.1  yamt 	if (shouldstop) {
    166       1.1  yamt 		KASSERT(tprof_nworker > 0);
    167       1.1  yamt 		tprof_nworker--;
    168       1.1  yamt 		cv_broadcast(&tprof_cv);
    169       1.1  yamt 		cv_broadcast(&tprof_reader_cv);
    170       1.1  yamt 	}
    171       1.1  yamt 	if (buf->b_used == 0) {
    172       1.1  yamt 		tprof_stat.ts_emptybuf++;
    173       1.1  yamt 	} else if (tprof_nbuf_on_list < TPROF_MAX_BUF) {
    174       1.1  yamt 		tprof_stat.ts_sample += buf->b_used;
    175       1.1  yamt 		tprof_stat.ts_overflow += buf->b_overflow;
    176       1.1  yamt 		tprof_stat.ts_buf++;
    177       1.1  yamt 		STAILQ_INSERT_TAIL(&tprof_list, buf, b_list);
    178       1.1  yamt 		tprof_nbuf_on_list++;
    179       1.1  yamt 		buf = NULL;
    180       1.1  yamt 		cv_broadcast(&tprof_reader_cv);
    181       1.1  yamt 	} else {
    182       1.1  yamt 		tprof_stat.ts_dropbuf_sample += buf->b_used;
    183       1.1  yamt 		tprof_stat.ts_dropbuf++;
    184       1.1  yamt 	}
    185       1.1  yamt 	mutex_exit(&tprof_lock);
    186       1.1  yamt 	if (buf) {
    187       1.1  yamt 		tprof_buf_free(buf);
    188       1.1  yamt 	}
    189       1.1  yamt 	if (!shouldstop) {
    190       1.1  yamt 		callout_schedule(&c->c_callout, hz);
    191       1.1  yamt 	}
    192       1.1  yamt }
    193       1.1  yamt 
    194       1.1  yamt static void
    195       1.1  yamt tprof_kick(void *vp)
    196       1.1  yamt {
    197       1.1  yamt 	struct cpu_info * const ci = vp;
    198       1.1  yamt 	tprof_cpu_t * const c = tprof_cpu(ci);
    199       1.1  yamt 
    200       1.1  yamt 	workqueue_enqueue(tprof_wq, &c->c_work, ci);
    201       1.1  yamt }
    202       1.1  yamt 
    203       1.1  yamt static void
    204       1.1  yamt tprof_stop1(void)
    205       1.1  yamt {
    206       1.1  yamt 	CPU_INFO_ITERATOR cii;
    207       1.1  yamt 	struct cpu_info *ci;
    208       1.1  yamt 
    209       1.1  yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    210       1.1  yamt 
    211       1.1  yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    212       1.1  yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    213       1.1  yamt 		tprof_buf_t *old;
    214       1.1  yamt 
    215       1.1  yamt 		old = tprof_buf_switch(c, NULL);
    216       1.1  yamt 		if (old != NULL) {
    217       1.1  yamt 			tprof_buf_free(old);
    218       1.1  yamt 		}
    219       1.1  yamt 		callout_destroy(&c->c_callout);
    220       1.1  yamt 	}
    221       1.1  yamt 	workqueue_destroy(tprof_wq);
    222       1.1  yamt }
    223       1.1  yamt 
    224       1.1  yamt static int
    225       1.1  yamt tprof_start(const struct tprof_param *param)
    226       1.1  yamt {
    227       1.1  yamt 	CPU_INFO_ITERATOR cii;
    228       1.1  yamt 	struct cpu_info *ci;
    229       1.1  yamt 	int error;
    230       1.1  yamt 	uint64_t freq;
    231       1.1  yamt 
    232       1.1  yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    233       1.1  yamt 	if (tprof_running) {
    234       1.1  yamt 		error = EBUSY;
    235       1.1  yamt 		goto done;
    236       1.1  yamt 	}
    237       1.1  yamt 
    238       1.1  yamt 	freq = tprof_backend_estimate_freq();
    239       1.1  yamt 	tprof_samples_per_buf = MIN(freq * 2, TPROF_MAX_SAMPLES_PER_BUF);
    240       1.1  yamt 
    241       1.1  yamt 	error = workqueue_create(&tprof_wq, "tprofmv", tprof_worker, NULL,
    242  1.1.16.1  yamt 	    PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE | WQ_PERCPU);
    243       1.1  yamt 	if (error != 0) {
    244       1.1  yamt 		goto done;
    245       1.1  yamt 	}
    246       1.1  yamt 
    247       1.1  yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    248       1.1  yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    249       1.1  yamt 		tprof_buf_t *new;
    250       1.1  yamt 		tprof_buf_t *old;
    251       1.1  yamt 
    252       1.1  yamt 		new = tprof_buf_alloc();
    253       1.1  yamt 		old = tprof_buf_switch(c, new);
    254       1.1  yamt 		if (old != NULL) {
    255       1.1  yamt 			tprof_buf_free(old);
    256       1.1  yamt 		}
    257       1.1  yamt 		callout_init(&c->c_callout, CALLOUT_MPSAFE);
    258       1.1  yamt 		callout_setfunc(&c->c_callout, tprof_kick, ci);
    259       1.1  yamt 	}
    260       1.1  yamt 
    261       1.1  yamt 	error = tprof_backend_start();
    262       1.1  yamt 	if (error != 0) {
    263       1.1  yamt 		tprof_stop1();
    264       1.1  yamt 		goto done;
    265       1.1  yamt 	}
    266       1.1  yamt 
    267       1.1  yamt 	mutex_enter(&tprof_lock);
    268       1.1  yamt 	tprof_running = true;
    269       1.1  yamt 	mutex_exit(&tprof_lock);
    270       1.1  yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    271       1.1  yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    272       1.1  yamt 
    273       1.1  yamt 		mutex_enter(&tprof_lock);
    274       1.1  yamt 		tprof_nworker++;
    275       1.1  yamt 		mutex_exit(&tprof_lock);
    276       1.1  yamt 		workqueue_enqueue(tprof_wq, &c->c_work, ci);
    277       1.1  yamt 	}
    278       1.1  yamt done:
    279       1.1  yamt 	return error;
    280       1.1  yamt }
    281       1.1  yamt 
    282       1.1  yamt static void
    283       1.1  yamt tprof_stop(void)
    284       1.1  yamt {
    285       1.1  yamt 	CPU_INFO_ITERATOR cii;
    286       1.1  yamt 	struct cpu_info *ci;
    287       1.1  yamt 
    288       1.1  yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    289       1.1  yamt 	if (!tprof_running) {
    290       1.1  yamt 		goto done;
    291       1.1  yamt 	}
    292       1.1  yamt 
    293       1.1  yamt 	tprof_backend_stop();
    294       1.1  yamt 
    295       1.1  yamt 	mutex_enter(&tprof_lock);
    296       1.1  yamt 	tprof_running = false;
    297       1.1  yamt 	cv_broadcast(&tprof_reader_cv);
    298       1.1  yamt 	mutex_exit(&tprof_lock);
    299       1.1  yamt 
    300       1.1  yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    301       1.1  yamt 		mutex_enter(&tprof_lock);
    302       1.1  yamt 		while (tprof_nworker > 0) {
    303       1.1  yamt 			cv_wait(&tprof_cv, &tprof_lock);
    304       1.1  yamt 		}
    305       1.1  yamt 		mutex_exit(&tprof_lock);
    306       1.1  yamt 	}
    307       1.1  yamt 
    308       1.1  yamt 	tprof_stop1();
    309       1.1  yamt done:
    310       1.1  yamt 	;
    311       1.1  yamt }
    312       1.1  yamt 
    313       1.1  yamt static void
    314       1.1  yamt tprof_clear(void)
    315       1.1  yamt {
    316       1.1  yamt 	tprof_buf_t *buf;
    317       1.1  yamt 
    318       1.1  yamt 	mutex_enter(&tprof_reader_lock);
    319       1.1  yamt 	mutex_enter(&tprof_lock);
    320       1.1  yamt 	while ((buf = STAILQ_FIRST(&tprof_list)) != NULL) {
    321       1.1  yamt 		if (buf != NULL) {
    322       1.1  yamt 			STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    323       1.1  yamt 			KASSERT(tprof_nbuf_on_list > 0);
    324       1.1  yamt 			tprof_nbuf_on_list--;
    325       1.1  yamt 			mutex_exit(&tprof_lock);
    326       1.1  yamt 			tprof_buf_free(buf);
    327       1.1  yamt 			mutex_enter(&tprof_lock);
    328       1.1  yamt 		}
    329       1.1  yamt 	}
    330       1.1  yamt 	KASSERT(tprof_nbuf_on_list == 0);
    331       1.1  yamt 	mutex_exit(&tprof_lock);
    332       1.1  yamt 	tprof_reader_offset = 0;
    333       1.1  yamt 	mutex_exit(&tprof_reader_lock);
    334       1.1  yamt 
    335       1.1  yamt 	memset(&tprof_stat, 0, sizeof(tprof_stat));
    336       1.1  yamt }
    337       1.1  yamt 
    338       1.1  yamt /* -------------------- backend interfaces */
    339       1.1  yamt 
    340       1.1  yamt /*
    341       1.1  yamt  * tprof_sample: record a sample on the per-cpu buffer.
    342       1.1  yamt  *
    343       1.1  yamt  * be careful; can be called in NMI context.
    344       1.1  yamt  * we are assuming that curcpu() is safe.
    345       1.1  yamt  */
    346       1.1  yamt 
    347       1.1  yamt void
    348       1.1  yamt tprof_sample(const struct trapframe *tf)
    349       1.1  yamt {
    350       1.1  yamt 	tprof_cpu_t * const c = tprof_curcpu();
    351       1.1  yamt 	tprof_buf_t * const buf = c->c_buf;
    352       1.1  yamt 	const uintptr_t pc = PC_REGS(tf);
    353       1.1  yamt 	u_int idx;
    354       1.1  yamt 
    355       1.1  yamt 	idx = buf->b_used;
    356       1.1  yamt 	if (__predict_false(idx >= buf->b_size)) {
    357       1.1  yamt 		buf->b_overflow++;
    358       1.1  yamt 		return;
    359       1.1  yamt 	}
    360       1.1  yamt 	buf->b_data[idx].s_pc = pc;
    361       1.1  yamt 	buf->b_used = idx + 1;
    362       1.1  yamt }
    363       1.1  yamt 
    364       1.1  yamt /* -------------------- cdevsw interfaces */
    365       1.1  yamt 
    366       1.1  yamt void tprofattach(int);
    367       1.1  yamt 
    368       1.1  yamt static int
    369       1.1  yamt tprof_open(dev_t dev, int flags, int type, struct lwp *l)
    370       1.1  yamt {
    371       1.1  yamt 
    372       1.1  yamt 	if (minor(dev) != 0) {
    373       1.1  yamt 		return EXDEV;
    374       1.1  yamt 	}
    375       1.1  yamt 	mutex_enter(&tprof_lock);
    376       1.1  yamt 	if (tprof_owner != NULL) {
    377       1.1  yamt 		mutex_exit(&tprof_lock);
    378       1.1  yamt 		return  EBUSY;
    379       1.1  yamt 	}
    380       1.1  yamt 	tprof_owner = curlwp;
    381       1.1  yamt 	mutex_exit(&tprof_lock);
    382       1.1  yamt 
    383       1.1  yamt 	return 0;
    384       1.1  yamt }
    385       1.1  yamt 
    386       1.1  yamt static int
    387       1.1  yamt tprof_close(dev_t dev, int flags, int type, struct lwp *l)
    388       1.1  yamt {
    389       1.1  yamt 
    390       1.1  yamt 	KASSERT(minor(dev) == 0);
    391       1.1  yamt 
    392       1.1  yamt 	mutex_enter(&tprof_startstop_lock);
    393       1.1  yamt 	mutex_enter(&tprof_lock);
    394       1.1  yamt 	tprof_owner = NULL;
    395       1.1  yamt 	mutex_exit(&tprof_lock);
    396       1.1  yamt 	tprof_stop();
    397       1.1  yamt 	tprof_clear();
    398       1.1  yamt 	mutex_exit(&tprof_startstop_lock);
    399       1.1  yamt 
    400       1.1  yamt 	return 0;
    401       1.1  yamt }
    402       1.1  yamt 
    403       1.1  yamt static int
    404       1.1  yamt tprof_read(dev_t dev, struct uio *uio, int flags)
    405       1.1  yamt {
    406       1.1  yamt 	tprof_buf_t *buf;
    407       1.1  yamt 	size_t bytes;
    408       1.1  yamt 	size_t resid;
    409       1.1  yamt 	size_t done;
    410       1.1  yamt 	int error = 0;
    411       1.1  yamt 
    412       1.1  yamt 	KASSERT(minor(dev) == 0);
    413       1.1  yamt 	mutex_enter(&tprof_reader_lock);
    414       1.1  yamt 	while (uio->uio_resid > 0 && error == 0) {
    415       1.1  yamt 		/*
    416       1.1  yamt 		 * take the first buffer from the list.
    417       1.1  yamt 		 */
    418       1.1  yamt 		mutex_enter(&tprof_lock);
    419       1.1  yamt 		buf = STAILQ_FIRST(&tprof_list);
    420       1.1  yamt 		if (buf == NULL) {
    421       1.1  yamt 			if (tprof_nworker == 0) {
    422       1.1  yamt 				mutex_exit(&tprof_lock);
    423       1.1  yamt 				error = 0;
    424       1.1  yamt 				break;
    425       1.1  yamt 			}
    426       1.1  yamt 			mutex_exit(&tprof_reader_lock);
    427       1.1  yamt 			error = cv_wait_sig(&tprof_reader_cv, &tprof_lock);
    428       1.1  yamt 			mutex_exit(&tprof_lock);
    429       1.1  yamt 			mutex_enter(&tprof_reader_lock);
    430       1.1  yamt 			continue;
    431       1.1  yamt 		}
    432       1.1  yamt 		STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    433       1.1  yamt 		KASSERT(tprof_nbuf_on_list > 0);
    434       1.1  yamt 		tprof_nbuf_on_list--;
    435       1.1  yamt 		mutex_exit(&tprof_lock);
    436       1.1  yamt 
    437       1.1  yamt 		/*
    438       1.1  yamt 		 * copy it out.
    439       1.1  yamt 		 */
    440       1.1  yamt 		bytes = MIN(buf->b_used * sizeof(tprof_sample_t) -
    441       1.1  yamt 		    tprof_reader_offset, uio->uio_resid);
    442       1.1  yamt 		resid = uio->uio_resid;
    443       1.1  yamt 		error = uiomove((char *)buf->b_data + tprof_reader_offset,
    444       1.1  yamt 		    bytes, uio);
    445       1.1  yamt 		done = resid - uio->uio_resid;
    446       1.1  yamt 		tprof_reader_offset += done;
    447       1.1  yamt 
    448       1.1  yamt 		/*
    449       1.1  yamt 		 * if we didn't consume the whole buffer,
    450       1.1  yamt 		 * put it back to the list.
    451       1.1  yamt 		 */
    452       1.1  yamt 		if (tprof_reader_offset <
    453       1.1  yamt 		    buf->b_used * sizeof(tprof_sample_t)) {
    454       1.1  yamt 			mutex_enter(&tprof_lock);
    455       1.1  yamt 			STAILQ_INSERT_HEAD(&tprof_list, buf, b_list);
    456       1.1  yamt 			tprof_nbuf_on_list++;
    457       1.1  yamt 			cv_broadcast(&tprof_reader_cv);
    458       1.1  yamt 			mutex_exit(&tprof_lock);
    459       1.1  yamt 		} else {
    460       1.1  yamt 			tprof_buf_free(buf);
    461       1.1  yamt 			tprof_reader_offset = 0;
    462       1.1  yamt 		}
    463       1.1  yamt 	}
    464       1.1  yamt 	mutex_exit(&tprof_reader_lock);
    465       1.1  yamt 
    466       1.1  yamt 	return error;
    467       1.1  yamt }
    468       1.1  yamt 
    469       1.1  yamt static int
    470       1.1  yamt tprof_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    471       1.1  yamt {
    472       1.1  yamt 	const struct tprof_param *param;
    473       1.1  yamt 	int error = 0;
    474       1.1  yamt 
    475       1.1  yamt 	KASSERT(minor(dev) == 0);
    476       1.1  yamt 
    477       1.1  yamt 	switch (cmd) {
    478       1.1  yamt 	case TPROF_IOC_GETVERSION:
    479       1.1  yamt 		*(int *)data = TPROF_VERSION;
    480       1.1  yamt 		break;
    481       1.1  yamt 	case TPROF_IOC_START:
    482       1.1  yamt 		param = data;
    483       1.1  yamt 		mutex_enter(&tprof_startstop_lock);
    484       1.1  yamt 		error = tprof_start(param);
    485       1.1  yamt 		mutex_exit(&tprof_startstop_lock);
    486       1.1  yamt 		break;
    487       1.1  yamt 	case TPROF_IOC_STOP:
    488       1.1  yamt 		mutex_enter(&tprof_startstop_lock);
    489       1.1  yamt 		tprof_stop();
    490       1.1  yamt 		mutex_exit(&tprof_startstop_lock);
    491       1.1  yamt 		break;
    492       1.1  yamt 	case TPROF_IOC_GETSTAT:
    493       1.1  yamt 		mutex_enter(&tprof_lock);
    494       1.1  yamt 		memcpy(data, &tprof_stat, sizeof(tprof_stat));
    495       1.1  yamt 		mutex_exit(&tprof_lock);
    496       1.1  yamt 		break;
    497       1.1  yamt 	default:
    498       1.1  yamt 		error = EINVAL;
    499       1.1  yamt 		break;
    500       1.1  yamt 	}
    501       1.1  yamt 
    502       1.1  yamt 	return error;
    503       1.1  yamt }
    504       1.1  yamt 
    505       1.1  yamt const struct cdevsw tprof_cdevsw = {
    506       1.1  yamt 	.d_open = tprof_open,
    507       1.1  yamt 	.d_close = tprof_close,
    508       1.1  yamt 	.d_read = tprof_read,
    509       1.1  yamt 	.d_write = nowrite,
    510       1.1  yamt 	.d_ioctl = tprof_ioctl,
    511       1.1  yamt 	.d_stop = nostop,
    512       1.1  yamt 	.d_tty = notty,
    513       1.1  yamt 	.d_poll = nopoll,
    514       1.1  yamt 	.d_mmap = nommap,
    515       1.1  yamt 	.d_kqfilter = nokqfilter,
    516       1.1  yamt 	.d_flag = D_OTHER | D_MPSAFE,
    517       1.1  yamt };
    518       1.1  yamt 
    519       1.1  yamt void
    520       1.1  yamt tprofattach(int nunits)
    521       1.1  yamt {
    522       1.1  yamt 
    523       1.1  yamt 	mutex_init(&tprof_lock, MUTEX_DEFAULT, IPL_NONE);
    524       1.1  yamt 	mutex_init(&tprof_reader_lock, MUTEX_DEFAULT, IPL_NONE);
    525       1.1  yamt 	mutex_init(&tprof_startstop_lock, MUTEX_DEFAULT, IPL_NONE);
    526       1.1  yamt 	cv_init(&tprof_cv, "tprof");
    527       1.1  yamt 	cv_init(&tprof_reader_cv, "tprofread");
    528       1.1  yamt 	STAILQ_INIT(&tprof_list);
    529       1.1  yamt }
    530