Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.2.8.1
      1  1.2.8.1  skrll /*	$NetBSD: tprof.c,v 1.2.8.1 2009/03/03 18:31:52 skrll 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.2.8.1  skrll __KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.2.8.1 2009/03/03 18:31:52 skrll 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.2.8.1  skrll /*
     73  1.2.8.1  skrll  * locking order:
     74  1.2.8.1  skrll  *	tprof_reader_lock -> tprof_lock
     75  1.2.8.1  skrll  *	tprof_startstop_lock -> tprof_lock
     76  1.2.8.1  skrll  */
     77  1.2.8.1  skrll 
     78      1.1   yamt static kmutex_t tprof_lock;
     79      1.1   yamt static bool tprof_running;
     80      1.1   yamt static u_int tprof_nworker;
     81      1.1   yamt static lwp_t *tprof_owner;
     82      1.1   yamt static STAILQ_HEAD(, tprof_buf) tprof_list;
     83      1.1   yamt static u_int tprof_nbuf_on_list;
     84      1.1   yamt static struct workqueue *tprof_wq;
     85      1.1   yamt static tprof_cpu_t tprof_cpus[MAXCPUS] __aligned(CACHE_LINE_SIZE);
     86      1.1   yamt static u_int tprof_samples_per_buf;
     87      1.1   yamt 
     88      1.1   yamt static kmutex_t tprof_reader_lock;
     89      1.1   yamt static kcondvar_t tprof_reader_cv;
     90      1.1   yamt static off_t tprof_reader_offset;
     91      1.1   yamt 
     92      1.1   yamt static kmutex_t tprof_startstop_lock;
     93      1.1   yamt static kcondvar_t tprof_cv;
     94      1.1   yamt 
     95      1.1   yamt static struct tprof_stat tprof_stat;
     96      1.1   yamt 
     97      1.1   yamt static tprof_cpu_t *
     98      1.1   yamt tprof_cpu(struct cpu_info *ci)
     99      1.1   yamt {
    100      1.1   yamt 
    101      1.1   yamt 	return &tprof_cpus[cpu_index(ci)];
    102      1.1   yamt }
    103      1.1   yamt 
    104      1.1   yamt static tprof_cpu_t *
    105      1.1   yamt tprof_curcpu(void)
    106      1.1   yamt {
    107      1.1   yamt 
    108      1.1   yamt 	return tprof_cpu(curcpu());
    109      1.1   yamt }
    110      1.1   yamt 
    111      1.1   yamt static tprof_buf_t *
    112      1.1   yamt tprof_buf_alloc(void)
    113      1.1   yamt {
    114      1.1   yamt 	tprof_buf_t *new;
    115      1.1   yamt 	u_int size = tprof_samples_per_buf;
    116      1.1   yamt 
    117      1.1   yamt 	new = kmem_alloc(TPROF_BUF_BYTESIZE(size), KM_SLEEP);
    118      1.1   yamt 	new->b_used = 0;
    119      1.1   yamt 	new->b_size = size;
    120      1.1   yamt 	new->b_overflow = 0;
    121      1.1   yamt 	return new;
    122      1.1   yamt }
    123      1.1   yamt 
    124      1.1   yamt static void
    125      1.1   yamt tprof_buf_free(tprof_buf_t *buf)
    126      1.1   yamt {
    127      1.1   yamt 
    128      1.1   yamt 	kmem_free(buf, TPROF_BUF_BYTESIZE(buf->b_size));
    129      1.1   yamt }
    130      1.1   yamt 
    131      1.1   yamt static tprof_buf_t *
    132      1.1   yamt tprof_buf_switch(tprof_cpu_t *c, tprof_buf_t *new)
    133      1.1   yamt {
    134      1.1   yamt 	tprof_buf_t *old;
    135      1.1   yamt 
    136      1.1   yamt 	old = c->c_buf;
    137      1.1   yamt 	c->c_buf = new;
    138      1.1   yamt 	return old;
    139      1.1   yamt }
    140      1.1   yamt 
    141      1.1   yamt static tprof_buf_t *
    142      1.1   yamt tprof_buf_refresh(void)
    143      1.1   yamt {
    144      1.1   yamt 	tprof_cpu_t * const c = tprof_curcpu();
    145      1.1   yamt 	tprof_buf_t *new;
    146      1.1   yamt 
    147      1.1   yamt 	new = tprof_buf_alloc();
    148      1.1   yamt 	return tprof_buf_switch(c, new);
    149      1.1   yamt }
    150      1.1   yamt 
    151      1.1   yamt static void
    152      1.1   yamt tprof_worker(struct work *wk, void *dummy)
    153      1.1   yamt {
    154      1.1   yamt 	tprof_cpu_t * const c = tprof_curcpu();
    155      1.1   yamt 	tprof_buf_t *buf;
    156      1.1   yamt 	bool shouldstop;
    157      1.1   yamt 
    158      1.1   yamt 	KASSERT(wk == &c->c_work);
    159      1.1   yamt 	KASSERT(dummy == NULL);
    160      1.1   yamt 
    161      1.1   yamt 	/*
    162      1.1   yamt 	 * get a per cpu buffer.
    163      1.1   yamt 	 */
    164      1.1   yamt 	buf = tprof_buf_refresh();
    165      1.1   yamt 
    166      1.1   yamt 	/*
    167      1.1   yamt 	 * and put it on the global list for read(2).
    168      1.1   yamt 	 */
    169      1.1   yamt 	mutex_enter(&tprof_lock);
    170      1.1   yamt 	shouldstop = !tprof_running;
    171      1.1   yamt 	if (shouldstop) {
    172      1.1   yamt 		KASSERT(tprof_nworker > 0);
    173      1.1   yamt 		tprof_nworker--;
    174      1.1   yamt 		cv_broadcast(&tprof_cv);
    175      1.1   yamt 		cv_broadcast(&tprof_reader_cv);
    176      1.1   yamt 	}
    177      1.1   yamt 	if (buf->b_used == 0) {
    178      1.1   yamt 		tprof_stat.ts_emptybuf++;
    179      1.1   yamt 	} else if (tprof_nbuf_on_list < TPROF_MAX_BUF) {
    180      1.1   yamt 		tprof_stat.ts_sample += buf->b_used;
    181      1.1   yamt 		tprof_stat.ts_overflow += buf->b_overflow;
    182      1.1   yamt 		tprof_stat.ts_buf++;
    183      1.1   yamt 		STAILQ_INSERT_TAIL(&tprof_list, buf, b_list);
    184      1.1   yamt 		tprof_nbuf_on_list++;
    185      1.1   yamt 		buf = NULL;
    186      1.1   yamt 		cv_broadcast(&tprof_reader_cv);
    187      1.1   yamt 	} else {
    188      1.1   yamt 		tprof_stat.ts_dropbuf_sample += buf->b_used;
    189      1.1   yamt 		tprof_stat.ts_dropbuf++;
    190      1.1   yamt 	}
    191      1.1   yamt 	mutex_exit(&tprof_lock);
    192      1.1   yamt 	if (buf) {
    193      1.1   yamt 		tprof_buf_free(buf);
    194      1.1   yamt 	}
    195      1.1   yamt 	if (!shouldstop) {
    196      1.1   yamt 		callout_schedule(&c->c_callout, hz);
    197      1.1   yamt 	}
    198      1.1   yamt }
    199      1.1   yamt 
    200      1.1   yamt static void
    201      1.1   yamt tprof_kick(void *vp)
    202      1.1   yamt {
    203      1.1   yamt 	struct cpu_info * const ci = vp;
    204      1.1   yamt 	tprof_cpu_t * const c = tprof_cpu(ci);
    205      1.1   yamt 
    206      1.1   yamt 	workqueue_enqueue(tprof_wq, &c->c_work, ci);
    207      1.1   yamt }
    208      1.1   yamt 
    209      1.1   yamt static void
    210      1.1   yamt tprof_stop1(void)
    211      1.1   yamt {
    212      1.1   yamt 	CPU_INFO_ITERATOR cii;
    213      1.1   yamt 	struct cpu_info *ci;
    214      1.1   yamt 
    215      1.1   yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    216      1.1   yamt 
    217      1.1   yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    218      1.1   yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    219      1.1   yamt 		tprof_buf_t *old;
    220      1.1   yamt 
    221      1.1   yamt 		old = tprof_buf_switch(c, NULL);
    222      1.1   yamt 		if (old != NULL) {
    223      1.1   yamt 			tprof_buf_free(old);
    224      1.1   yamt 		}
    225      1.1   yamt 		callout_destroy(&c->c_callout);
    226      1.1   yamt 	}
    227      1.1   yamt 	workqueue_destroy(tprof_wq);
    228      1.1   yamt }
    229      1.1   yamt 
    230      1.1   yamt static int
    231      1.1   yamt tprof_start(const struct tprof_param *param)
    232      1.1   yamt {
    233      1.1   yamt 	CPU_INFO_ITERATOR cii;
    234      1.1   yamt 	struct cpu_info *ci;
    235      1.1   yamt 	int error;
    236      1.1   yamt 	uint64_t freq;
    237      1.1   yamt 
    238      1.1   yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    239      1.1   yamt 	if (tprof_running) {
    240      1.1   yamt 		error = EBUSY;
    241      1.1   yamt 		goto done;
    242      1.1   yamt 	}
    243      1.1   yamt 
    244      1.1   yamt 	freq = tprof_backend_estimate_freq();
    245      1.1   yamt 	tprof_samples_per_buf = MIN(freq * 2, TPROF_MAX_SAMPLES_PER_BUF);
    246      1.1   yamt 
    247      1.1   yamt 	error = workqueue_create(&tprof_wq, "tprofmv", tprof_worker, NULL,
    248      1.2   yamt 	    PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE | WQ_PERCPU);
    249      1.1   yamt 	if (error != 0) {
    250      1.1   yamt 		goto done;
    251      1.1   yamt 	}
    252      1.1   yamt 
    253      1.1   yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    254      1.1   yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    255      1.1   yamt 		tprof_buf_t *new;
    256      1.1   yamt 		tprof_buf_t *old;
    257      1.1   yamt 
    258      1.1   yamt 		new = tprof_buf_alloc();
    259      1.1   yamt 		old = tprof_buf_switch(c, new);
    260      1.1   yamt 		if (old != NULL) {
    261      1.1   yamt 			tprof_buf_free(old);
    262      1.1   yamt 		}
    263      1.1   yamt 		callout_init(&c->c_callout, CALLOUT_MPSAFE);
    264      1.1   yamt 		callout_setfunc(&c->c_callout, tprof_kick, ci);
    265      1.1   yamt 	}
    266      1.1   yamt 
    267      1.1   yamt 	error = tprof_backend_start();
    268      1.1   yamt 	if (error != 0) {
    269      1.1   yamt 		tprof_stop1();
    270      1.1   yamt 		goto done;
    271      1.1   yamt 	}
    272      1.1   yamt 
    273      1.1   yamt 	mutex_enter(&tprof_lock);
    274      1.1   yamt 	tprof_running = true;
    275      1.1   yamt 	mutex_exit(&tprof_lock);
    276      1.1   yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    277      1.1   yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    278      1.1   yamt 
    279      1.1   yamt 		mutex_enter(&tprof_lock);
    280      1.1   yamt 		tprof_nworker++;
    281      1.1   yamt 		mutex_exit(&tprof_lock);
    282      1.1   yamt 		workqueue_enqueue(tprof_wq, &c->c_work, ci);
    283      1.1   yamt 	}
    284      1.1   yamt done:
    285      1.1   yamt 	return error;
    286      1.1   yamt }
    287      1.1   yamt 
    288      1.1   yamt static void
    289      1.1   yamt tprof_stop(void)
    290      1.1   yamt {
    291      1.1   yamt 	CPU_INFO_ITERATOR cii;
    292      1.1   yamt 	struct cpu_info *ci;
    293      1.1   yamt 
    294      1.1   yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    295      1.1   yamt 	if (!tprof_running) {
    296      1.1   yamt 		goto done;
    297      1.1   yamt 	}
    298      1.1   yamt 
    299      1.1   yamt 	tprof_backend_stop();
    300      1.1   yamt 
    301      1.1   yamt 	mutex_enter(&tprof_lock);
    302      1.1   yamt 	tprof_running = false;
    303      1.1   yamt 	cv_broadcast(&tprof_reader_cv);
    304      1.1   yamt 	mutex_exit(&tprof_lock);
    305      1.1   yamt 
    306      1.1   yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    307      1.1   yamt 		mutex_enter(&tprof_lock);
    308      1.1   yamt 		while (tprof_nworker > 0) {
    309      1.1   yamt 			cv_wait(&tprof_cv, &tprof_lock);
    310      1.1   yamt 		}
    311      1.1   yamt 		mutex_exit(&tprof_lock);
    312      1.1   yamt 	}
    313      1.1   yamt 
    314      1.1   yamt 	tprof_stop1();
    315      1.1   yamt done:
    316      1.1   yamt 	;
    317      1.1   yamt }
    318      1.1   yamt 
    319      1.1   yamt static void
    320      1.1   yamt tprof_clear(void)
    321      1.1   yamt {
    322      1.1   yamt 	tprof_buf_t *buf;
    323      1.1   yamt 
    324      1.1   yamt 	mutex_enter(&tprof_reader_lock);
    325      1.1   yamt 	mutex_enter(&tprof_lock);
    326      1.1   yamt 	while ((buf = STAILQ_FIRST(&tprof_list)) != NULL) {
    327      1.1   yamt 		if (buf != NULL) {
    328      1.1   yamt 			STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    329      1.1   yamt 			KASSERT(tprof_nbuf_on_list > 0);
    330      1.1   yamt 			tprof_nbuf_on_list--;
    331      1.1   yamt 			mutex_exit(&tprof_lock);
    332      1.1   yamt 			tprof_buf_free(buf);
    333      1.1   yamt 			mutex_enter(&tprof_lock);
    334      1.1   yamt 		}
    335      1.1   yamt 	}
    336      1.1   yamt 	KASSERT(tprof_nbuf_on_list == 0);
    337      1.1   yamt 	mutex_exit(&tprof_lock);
    338      1.1   yamt 	tprof_reader_offset = 0;
    339      1.1   yamt 	mutex_exit(&tprof_reader_lock);
    340      1.1   yamt 
    341      1.1   yamt 	memset(&tprof_stat, 0, sizeof(tprof_stat));
    342      1.1   yamt }
    343      1.1   yamt 
    344      1.1   yamt /* -------------------- backend interfaces */
    345      1.1   yamt 
    346      1.1   yamt /*
    347      1.1   yamt  * tprof_sample: record a sample on the per-cpu buffer.
    348      1.1   yamt  *
    349      1.1   yamt  * be careful; can be called in NMI context.
    350      1.1   yamt  * we are assuming that curcpu() is safe.
    351      1.1   yamt  */
    352      1.1   yamt 
    353      1.1   yamt void
    354      1.1   yamt tprof_sample(const struct trapframe *tf)
    355      1.1   yamt {
    356      1.1   yamt 	tprof_cpu_t * const c = tprof_curcpu();
    357      1.1   yamt 	tprof_buf_t * const buf = c->c_buf;
    358      1.1   yamt 	const uintptr_t pc = PC_REGS(tf);
    359      1.1   yamt 	u_int idx;
    360      1.1   yamt 
    361      1.1   yamt 	idx = buf->b_used;
    362      1.1   yamt 	if (__predict_false(idx >= buf->b_size)) {
    363      1.1   yamt 		buf->b_overflow++;
    364      1.1   yamt 		return;
    365      1.1   yamt 	}
    366      1.1   yamt 	buf->b_data[idx].s_pc = pc;
    367      1.1   yamt 	buf->b_used = idx + 1;
    368      1.1   yamt }
    369      1.1   yamt 
    370      1.1   yamt /* -------------------- cdevsw interfaces */
    371      1.1   yamt 
    372      1.1   yamt void tprofattach(int);
    373      1.1   yamt 
    374      1.1   yamt static int
    375      1.1   yamt tprof_open(dev_t dev, int flags, int type, struct lwp *l)
    376      1.1   yamt {
    377      1.1   yamt 
    378      1.1   yamt 	if (minor(dev) != 0) {
    379      1.1   yamt 		return EXDEV;
    380      1.1   yamt 	}
    381      1.1   yamt 	mutex_enter(&tprof_lock);
    382      1.1   yamt 	if (tprof_owner != NULL) {
    383      1.1   yamt 		mutex_exit(&tprof_lock);
    384      1.1   yamt 		return  EBUSY;
    385      1.1   yamt 	}
    386      1.1   yamt 	tprof_owner = curlwp;
    387      1.1   yamt 	mutex_exit(&tprof_lock);
    388      1.1   yamt 
    389      1.1   yamt 	return 0;
    390      1.1   yamt }
    391      1.1   yamt 
    392      1.1   yamt static int
    393      1.1   yamt tprof_close(dev_t dev, int flags, int type, struct lwp *l)
    394      1.1   yamt {
    395      1.1   yamt 
    396      1.1   yamt 	KASSERT(minor(dev) == 0);
    397      1.1   yamt 
    398      1.1   yamt 	mutex_enter(&tprof_startstop_lock);
    399      1.1   yamt 	mutex_enter(&tprof_lock);
    400      1.1   yamt 	tprof_owner = NULL;
    401      1.1   yamt 	mutex_exit(&tprof_lock);
    402      1.1   yamt 	tprof_stop();
    403      1.1   yamt 	tprof_clear();
    404      1.1   yamt 	mutex_exit(&tprof_startstop_lock);
    405      1.1   yamt 
    406      1.1   yamt 	return 0;
    407      1.1   yamt }
    408      1.1   yamt 
    409      1.1   yamt static int
    410      1.1   yamt tprof_read(dev_t dev, struct uio *uio, int flags)
    411      1.1   yamt {
    412      1.1   yamt 	tprof_buf_t *buf;
    413      1.1   yamt 	size_t bytes;
    414      1.1   yamt 	size_t resid;
    415      1.1   yamt 	size_t done;
    416      1.1   yamt 	int error = 0;
    417      1.1   yamt 
    418      1.1   yamt 	KASSERT(minor(dev) == 0);
    419      1.1   yamt 	mutex_enter(&tprof_reader_lock);
    420      1.1   yamt 	while (uio->uio_resid > 0 && error == 0) {
    421      1.1   yamt 		/*
    422      1.1   yamt 		 * take the first buffer from the list.
    423      1.1   yamt 		 */
    424      1.1   yamt 		mutex_enter(&tprof_lock);
    425      1.1   yamt 		buf = STAILQ_FIRST(&tprof_list);
    426      1.1   yamt 		if (buf == NULL) {
    427      1.1   yamt 			if (tprof_nworker == 0) {
    428      1.1   yamt 				mutex_exit(&tprof_lock);
    429      1.1   yamt 				error = 0;
    430      1.1   yamt 				break;
    431      1.1   yamt 			}
    432      1.1   yamt 			mutex_exit(&tprof_reader_lock);
    433      1.1   yamt 			error = cv_wait_sig(&tprof_reader_cv, &tprof_lock);
    434      1.1   yamt 			mutex_exit(&tprof_lock);
    435      1.1   yamt 			mutex_enter(&tprof_reader_lock);
    436      1.1   yamt 			continue;
    437      1.1   yamt 		}
    438      1.1   yamt 		STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    439      1.1   yamt 		KASSERT(tprof_nbuf_on_list > 0);
    440      1.1   yamt 		tprof_nbuf_on_list--;
    441      1.1   yamt 		mutex_exit(&tprof_lock);
    442      1.1   yamt 
    443      1.1   yamt 		/*
    444      1.1   yamt 		 * copy it out.
    445      1.1   yamt 		 */
    446      1.1   yamt 		bytes = MIN(buf->b_used * sizeof(tprof_sample_t) -
    447      1.1   yamt 		    tprof_reader_offset, uio->uio_resid);
    448      1.1   yamt 		resid = uio->uio_resid;
    449      1.1   yamt 		error = uiomove((char *)buf->b_data + tprof_reader_offset,
    450      1.1   yamt 		    bytes, uio);
    451      1.1   yamt 		done = resid - uio->uio_resid;
    452      1.1   yamt 		tprof_reader_offset += done;
    453      1.1   yamt 
    454      1.1   yamt 		/*
    455      1.1   yamt 		 * if we didn't consume the whole buffer,
    456      1.1   yamt 		 * put it back to the list.
    457      1.1   yamt 		 */
    458      1.1   yamt 		if (tprof_reader_offset <
    459      1.1   yamt 		    buf->b_used * sizeof(tprof_sample_t)) {
    460      1.1   yamt 			mutex_enter(&tprof_lock);
    461      1.1   yamt 			STAILQ_INSERT_HEAD(&tprof_list, buf, b_list);
    462      1.1   yamt 			tprof_nbuf_on_list++;
    463      1.1   yamt 			cv_broadcast(&tprof_reader_cv);
    464      1.1   yamt 			mutex_exit(&tprof_lock);
    465      1.1   yamt 		} else {
    466      1.1   yamt 			tprof_buf_free(buf);
    467      1.1   yamt 			tprof_reader_offset = 0;
    468      1.1   yamt 		}
    469      1.1   yamt 	}
    470      1.1   yamt 	mutex_exit(&tprof_reader_lock);
    471      1.1   yamt 
    472      1.1   yamt 	return error;
    473      1.1   yamt }
    474      1.1   yamt 
    475      1.1   yamt static int
    476      1.1   yamt tprof_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    477      1.1   yamt {
    478      1.1   yamt 	const struct tprof_param *param;
    479      1.1   yamt 	int error = 0;
    480      1.1   yamt 
    481      1.1   yamt 	KASSERT(minor(dev) == 0);
    482      1.1   yamt 
    483      1.1   yamt 	switch (cmd) {
    484      1.1   yamt 	case TPROF_IOC_GETVERSION:
    485      1.1   yamt 		*(int *)data = TPROF_VERSION;
    486      1.1   yamt 		break;
    487      1.1   yamt 	case TPROF_IOC_START:
    488      1.1   yamt 		param = data;
    489      1.1   yamt 		mutex_enter(&tprof_startstop_lock);
    490      1.1   yamt 		error = tprof_start(param);
    491      1.1   yamt 		mutex_exit(&tprof_startstop_lock);
    492      1.1   yamt 		break;
    493      1.1   yamt 	case TPROF_IOC_STOP:
    494      1.1   yamt 		mutex_enter(&tprof_startstop_lock);
    495      1.1   yamt 		tprof_stop();
    496      1.1   yamt 		mutex_exit(&tprof_startstop_lock);
    497      1.1   yamt 		break;
    498      1.1   yamt 	case TPROF_IOC_GETSTAT:
    499      1.1   yamt 		mutex_enter(&tprof_lock);
    500      1.1   yamt 		memcpy(data, &tprof_stat, sizeof(tprof_stat));
    501      1.1   yamt 		mutex_exit(&tprof_lock);
    502      1.1   yamt 		break;
    503      1.1   yamt 	default:
    504      1.1   yamt 		error = EINVAL;
    505      1.1   yamt 		break;
    506      1.1   yamt 	}
    507      1.1   yamt 
    508      1.1   yamt 	return error;
    509      1.1   yamt }
    510      1.1   yamt 
    511      1.1   yamt const struct cdevsw tprof_cdevsw = {
    512      1.1   yamt 	.d_open = tprof_open,
    513      1.1   yamt 	.d_close = tprof_close,
    514      1.1   yamt 	.d_read = tprof_read,
    515      1.1   yamt 	.d_write = nowrite,
    516      1.1   yamt 	.d_ioctl = tprof_ioctl,
    517      1.1   yamt 	.d_stop = nostop,
    518      1.1   yamt 	.d_tty = notty,
    519      1.1   yamt 	.d_poll = nopoll,
    520      1.1   yamt 	.d_mmap = nommap,
    521      1.1   yamt 	.d_kqfilter = nokqfilter,
    522      1.1   yamt 	.d_flag = D_OTHER | D_MPSAFE,
    523      1.1   yamt };
    524      1.1   yamt 
    525      1.1   yamt void
    526      1.1   yamt tprofattach(int nunits)
    527      1.1   yamt {
    528      1.1   yamt 
    529      1.1   yamt 	mutex_init(&tprof_lock, MUTEX_DEFAULT, IPL_NONE);
    530      1.1   yamt 	mutex_init(&tprof_reader_lock, MUTEX_DEFAULT, IPL_NONE);
    531      1.1   yamt 	mutex_init(&tprof_startstop_lock, MUTEX_DEFAULT, IPL_NONE);
    532      1.1   yamt 	cv_init(&tprof_cv, "tprof");
    533      1.1   yamt 	cv_init(&tprof_reader_cv, "tprofread");
    534      1.1   yamt 	STAILQ_INIT(&tprof_list);
    535      1.1   yamt }
    536