Home | History | Annotate | Line # | Download | only in tprof
tprof.c revision 1.14.14.1
      1  1.14.14.1   thorpej /*	$NetBSD: tprof.c,v 1.14.14.1 2020/12/14 14:38:09 thorpej Exp $	*/
      2        1.1      yamt 
      3        1.1      yamt /*-
      4        1.8      yamt  * Copyright (c)2008,2009,2010 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.14.14.1   thorpej __KERNEL_RCSID(0, "$NetBSD: tprof.c,v 1.14.14.1 2020/12/14 14:38:09 thorpej 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/callout.h>
     37  1.14.14.1   thorpej #include <sys/conf.h>
     38  1.14.14.1   thorpej #include <sys/cpu.h>
     39        1.1      yamt #include <sys/kmem.h>
     40        1.4      yamt #include <sys/module.h>
     41  1.14.14.1   thorpej #include <sys/percpu.h>
     42        1.8      yamt #include <sys/proc.h>
     43        1.1      yamt #include <sys/queue.h>
     44  1.14.14.1   thorpej #include <sys/workqueue.h>
     45        1.1      yamt 
     46        1.1      yamt #include <dev/tprof/tprof.h>
     47        1.1      yamt #include <dev/tprof/tprof_ioctl.h>
     48        1.1      yamt 
     49       1.13  christos #include "ioconf.h"
     50       1.13  christos 
     51        1.4      yamt /*
     52        1.4      yamt  * locking order:
     53        1.4      yamt  *	tprof_reader_lock -> tprof_lock
     54        1.4      yamt  *	tprof_startstop_lock -> tprof_lock
     55        1.4      yamt  */
     56        1.4      yamt 
     57        1.4      yamt /*
     58        1.4      yamt  * protected by:
     59        1.4      yamt  *	L: tprof_lock
     60        1.4      yamt  *	R: tprof_reader_lock
     61        1.4      yamt  *	S: tprof_startstop_lock
     62        1.8      yamt  *	s: writer should hold tprof_startstop_lock and tprof_lock
     63        1.8      yamt  *	   reader should hold tprof_startstop_lock or tprof_lock
     64        1.4      yamt  */
     65        1.4      yamt 
     66        1.1      yamt typedef struct tprof_buf {
     67        1.1      yamt 	u_int b_used;
     68        1.1      yamt 	u_int b_size;
     69        1.1      yamt 	u_int b_overflow;
     70        1.1      yamt 	u_int b_unused;
     71        1.1      yamt 	STAILQ_ENTRY(tprof_buf) b_list;
     72        1.1      yamt 	tprof_sample_t b_data[];
     73        1.1      yamt } tprof_buf_t;
     74        1.1      yamt #define	TPROF_BUF_BYTESIZE(sz) \
     75        1.1      yamt 	(sizeof(tprof_buf_t) + (sz) * sizeof(tprof_sample_t))
     76        1.1      yamt #define	TPROF_MAX_SAMPLES_PER_BUF	10000
     77        1.1      yamt 
     78        1.1      yamt #define	TPROF_MAX_BUF			100
     79        1.1      yamt 
     80        1.1      yamt typedef struct {
     81        1.1      yamt 	tprof_buf_t *c_buf;
     82       1.10      yamt 	uint32_t c_cpuid;
     83        1.1      yamt 	struct work c_work;
     84        1.1      yamt 	callout_t c_callout;
     85        1.1      yamt } __aligned(CACHE_LINE_SIZE) tprof_cpu_t;
     86        1.1      yamt 
     87        1.4      yamt typedef struct tprof_backend {
     88        1.4      yamt 	const char *tb_name;
     89        1.4      yamt 	const tprof_backend_ops_t *tb_ops;
     90        1.4      yamt 	LIST_ENTRY(tprof_backend) tb_list;
     91        1.4      yamt 	int tb_usecount;	/* S: */
     92        1.4      yamt } tprof_backend_t;
     93        1.3      yamt 
     94        1.1      yamt static kmutex_t tprof_lock;
     95        1.8      yamt static bool tprof_running;		/* s: */
     96        1.4      yamt static u_int tprof_nworker;		/* L: # of running worker LWPs */
     97        1.1      yamt static lwp_t *tprof_owner;
     98        1.4      yamt static STAILQ_HEAD(, tprof_buf) tprof_list; /* L: global buffer list */
     99        1.4      yamt static u_int tprof_nbuf_on_list;	/* L: # of buffers on tprof_list */
    100        1.1      yamt static struct workqueue *tprof_wq;
    101  1.14.14.1   thorpej static struct percpu *tprof_cpus __read_mostly;	/* tprof_cpu_t * */
    102        1.1      yamt static u_int tprof_samples_per_buf;
    103        1.1      yamt 
    104        1.4      yamt static tprof_backend_t *tprof_backend;	/* S: */
    105        1.4      yamt static LIST_HEAD(, tprof_backend) tprof_backends =
    106        1.4      yamt     LIST_HEAD_INITIALIZER(tprof_backend); /* S: */
    107        1.4      yamt 
    108        1.1      yamt static kmutex_t tprof_reader_lock;
    109        1.4      yamt static kcondvar_t tprof_reader_cv;	/* L: */
    110        1.4      yamt static off_t tprof_reader_offset;	/* R: */
    111        1.1      yamt 
    112        1.1      yamt static kmutex_t tprof_startstop_lock;
    113        1.4      yamt static kcondvar_t tprof_cv;		/* L: */
    114        1.1      yamt 
    115        1.4      yamt static struct tprof_stat tprof_stat;	/* L: */
    116        1.1      yamt 
    117        1.1      yamt static tprof_cpu_t *
    118        1.1      yamt tprof_cpu(struct cpu_info *ci)
    119        1.1      yamt {
    120  1.14.14.1   thorpej 	tprof_cpu_t **cp, *c;
    121        1.1      yamt 
    122  1.14.14.1   thorpej 	/*
    123  1.14.14.1   thorpej 	 * As long as xcalls are blocked -- e.g., by kpreempt_disable
    124  1.14.14.1   thorpej 	 * -- the percpu object will not be swapped and destroyed.  We
    125  1.14.14.1   thorpej 	 * can't write to it, because the data may have already been
    126  1.14.14.1   thorpej 	 * moved to a new buffer, but we can safely read from it.
    127  1.14.14.1   thorpej 	 */
    128  1.14.14.1   thorpej 	kpreempt_disable();
    129  1.14.14.1   thorpej 	cp = percpu_getptr_remote(tprof_cpus, ci);
    130  1.14.14.1   thorpej 	c = *cp;
    131  1.14.14.1   thorpej 	kpreempt_enable();
    132  1.14.14.1   thorpej 
    133  1.14.14.1   thorpej 	return c;
    134        1.1      yamt }
    135        1.1      yamt 
    136        1.1      yamt static tprof_cpu_t *
    137        1.1      yamt tprof_curcpu(void)
    138        1.1      yamt {
    139        1.1      yamt 
    140        1.1      yamt 	return tprof_cpu(curcpu());
    141        1.1      yamt }
    142        1.1      yamt 
    143        1.1      yamt static tprof_buf_t *
    144        1.1      yamt tprof_buf_alloc(void)
    145        1.1      yamt {
    146        1.1      yamt 	tprof_buf_t *new;
    147        1.1      yamt 	u_int size = tprof_samples_per_buf;
    148  1.14.14.1   thorpej 
    149        1.1      yamt 	new = kmem_alloc(TPROF_BUF_BYTESIZE(size), KM_SLEEP);
    150        1.1      yamt 	new->b_used = 0;
    151        1.1      yamt 	new->b_size = size;
    152        1.1      yamt 	new->b_overflow = 0;
    153        1.1      yamt 	return new;
    154        1.1      yamt }
    155        1.1      yamt 
    156        1.1      yamt static void
    157        1.1      yamt tprof_buf_free(tprof_buf_t *buf)
    158        1.1      yamt {
    159        1.1      yamt 
    160        1.1      yamt 	kmem_free(buf, TPROF_BUF_BYTESIZE(buf->b_size));
    161        1.1      yamt }
    162        1.1      yamt 
    163        1.1      yamt static tprof_buf_t *
    164        1.1      yamt tprof_buf_switch(tprof_cpu_t *c, tprof_buf_t *new)
    165        1.1      yamt {
    166        1.1      yamt 	tprof_buf_t *old;
    167        1.1      yamt 
    168        1.1      yamt 	old = c->c_buf;
    169        1.1      yamt 	c->c_buf = new;
    170        1.1      yamt 	return old;
    171        1.1      yamt }
    172        1.1      yamt 
    173        1.1      yamt static tprof_buf_t *
    174        1.1      yamt tprof_buf_refresh(void)
    175        1.1      yamt {
    176        1.1      yamt 	tprof_cpu_t * const c = tprof_curcpu();
    177        1.1      yamt 	tprof_buf_t *new;
    178        1.1      yamt 
    179        1.1      yamt 	new = tprof_buf_alloc();
    180        1.1      yamt 	return tprof_buf_switch(c, new);
    181        1.1      yamt }
    182        1.1      yamt 
    183        1.1      yamt static void
    184        1.1      yamt tprof_worker(struct work *wk, void *dummy)
    185        1.1      yamt {
    186        1.1      yamt 	tprof_cpu_t * const c = tprof_curcpu();
    187        1.1      yamt 	tprof_buf_t *buf;
    188        1.1      yamt 	bool shouldstop;
    189        1.1      yamt 
    190        1.1      yamt 	KASSERT(wk == &c->c_work);
    191        1.1      yamt 	KASSERT(dummy == NULL);
    192        1.1      yamt 
    193        1.1      yamt 	/*
    194        1.1      yamt 	 * get a per cpu buffer.
    195        1.1      yamt 	 */
    196        1.1      yamt 	buf = tprof_buf_refresh();
    197        1.1      yamt 
    198        1.1      yamt 	/*
    199        1.1      yamt 	 * and put it on the global list for read(2).
    200        1.1      yamt 	 */
    201        1.1      yamt 	mutex_enter(&tprof_lock);
    202        1.1      yamt 	shouldstop = !tprof_running;
    203        1.1      yamt 	if (shouldstop) {
    204        1.1      yamt 		KASSERT(tprof_nworker > 0);
    205        1.1      yamt 		tprof_nworker--;
    206        1.1      yamt 		cv_broadcast(&tprof_cv);
    207        1.1      yamt 		cv_broadcast(&tprof_reader_cv);
    208        1.1      yamt 	}
    209        1.1      yamt 	if (buf->b_used == 0) {
    210        1.1      yamt 		tprof_stat.ts_emptybuf++;
    211        1.1      yamt 	} else if (tprof_nbuf_on_list < TPROF_MAX_BUF) {
    212        1.1      yamt 		tprof_stat.ts_sample += buf->b_used;
    213        1.1      yamt 		tprof_stat.ts_overflow += buf->b_overflow;
    214        1.1      yamt 		tprof_stat.ts_buf++;
    215        1.1      yamt 		STAILQ_INSERT_TAIL(&tprof_list, buf, b_list);
    216        1.1      yamt 		tprof_nbuf_on_list++;
    217        1.1      yamt 		buf = NULL;
    218        1.1      yamt 		cv_broadcast(&tprof_reader_cv);
    219        1.1      yamt 	} else {
    220        1.1      yamt 		tprof_stat.ts_dropbuf_sample += buf->b_used;
    221        1.1      yamt 		tprof_stat.ts_dropbuf++;
    222        1.1      yamt 	}
    223        1.1      yamt 	mutex_exit(&tprof_lock);
    224        1.1      yamt 	if (buf) {
    225        1.1      yamt 		tprof_buf_free(buf);
    226        1.1      yamt 	}
    227        1.1      yamt 	if (!shouldstop) {
    228        1.1      yamt 		callout_schedule(&c->c_callout, hz);
    229        1.1      yamt 	}
    230        1.1      yamt }
    231        1.1      yamt 
    232        1.1      yamt static void
    233        1.1      yamt tprof_kick(void *vp)
    234        1.1      yamt {
    235        1.1      yamt 	struct cpu_info * const ci = vp;
    236        1.1      yamt 	tprof_cpu_t * const c = tprof_cpu(ci);
    237        1.1      yamt 
    238        1.1      yamt 	workqueue_enqueue(tprof_wq, &c->c_work, ci);
    239        1.1      yamt }
    240        1.1      yamt 
    241        1.1      yamt static void
    242        1.1      yamt tprof_stop1(void)
    243        1.1      yamt {
    244        1.1      yamt 	CPU_INFO_ITERATOR cii;
    245        1.1      yamt 	struct cpu_info *ci;
    246        1.1      yamt 
    247        1.1      yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    248        1.6      yamt 	KASSERT(tprof_nworker == 0);
    249        1.1      yamt 
    250        1.1      yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    251        1.1      yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    252        1.1      yamt 		tprof_buf_t *old;
    253        1.1      yamt 
    254        1.1      yamt 		old = tprof_buf_switch(c, NULL);
    255        1.1      yamt 		if (old != NULL) {
    256        1.1      yamt 			tprof_buf_free(old);
    257        1.1      yamt 		}
    258        1.1      yamt 		callout_destroy(&c->c_callout);
    259        1.1      yamt 	}
    260        1.1      yamt 	workqueue_destroy(tprof_wq);
    261        1.1      yamt }
    262        1.1      yamt 
    263       1.14      maxv static void
    264       1.14      maxv tprof_getinfo(struct tprof_info *info)
    265       1.14      maxv {
    266       1.14      maxv 	tprof_backend_t *tb;
    267       1.14      maxv 
    268       1.14      maxv 	KASSERT(mutex_owned(&tprof_startstop_lock));
    269       1.14      maxv 
    270       1.14      maxv 	memset(info, 0, sizeof(*info));
    271       1.14      maxv 	info->ti_version = TPROF_VERSION;
    272       1.14      maxv 	if ((tb = tprof_backend) != NULL) {
    273       1.14      maxv 		info->ti_ident = tb->tb_ops->tbo_ident();
    274       1.14      maxv 	}
    275       1.14      maxv }
    276       1.14      maxv 
    277        1.1      yamt static int
    278       1.14      maxv tprof_start(const tprof_param_t *param)
    279        1.1      yamt {
    280        1.1      yamt 	CPU_INFO_ITERATOR cii;
    281        1.1      yamt 	struct cpu_info *ci;
    282        1.1      yamt 	int error;
    283        1.1      yamt 	uint64_t freq;
    284        1.4      yamt 	tprof_backend_t *tb;
    285        1.1      yamt 
    286        1.1      yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    287        1.1      yamt 	if (tprof_running) {
    288        1.1      yamt 		error = EBUSY;
    289        1.1      yamt 		goto done;
    290        1.1      yamt 	}
    291        1.1      yamt 
    292        1.4      yamt 	tb = tprof_backend;
    293        1.4      yamt 	if (tb == NULL) {
    294        1.4      yamt 		error = ENOENT;
    295        1.4      yamt 		goto done;
    296        1.4      yamt 	}
    297        1.4      yamt 	if (tb->tb_usecount > 0) {
    298        1.4      yamt 		error = EBUSY;
    299        1.4      yamt 		goto done;
    300        1.4      yamt 	}
    301        1.4      yamt 
    302        1.4      yamt 	tb->tb_usecount++;
    303        1.4      yamt 	freq = tb->tb_ops->tbo_estimate_freq();
    304        1.1      yamt 	tprof_samples_per_buf = MIN(freq * 2, TPROF_MAX_SAMPLES_PER_BUF);
    305        1.1      yamt 
    306        1.1      yamt 	error = workqueue_create(&tprof_wq, "tprofmv", tprof_worker, NULL,
    307        1.2      yamt 	    PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE | WQ_PERCPU);
    308        1.1      yamt 	if (error != 0) {
    309        1.1      yamt 		goto done;
    310        1.1      yamt 	}
    311        1.1      yamt 
    312        1.1      yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    313        1.1      yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    314        1.1      yamt 		tprof_buf_t *new;
    315        1.1      yamt 		tprof_buf_t *old;
    316        1.1      yamt 
    317        1.1      yamt 		new = tprof_buf_alloc();
    318        1.1      yamt 		old = tprof_buf_switch(c, new);
    319        1.1      yamt 		if (old != NULL) {
    320        1.1      yamt 			tprof_buf_free(old);
    321        1.1      yamt 		}
    322        1.1      yamt 		callout_init(&c->c_callout, CALLOUT_MPSAFE);
    323        1.1      yamt 		callout_setfunc(&c->c_callout, tprof_kick, ci);
    324        1.1      yamt 	}
    325        1.1      yamt 
    326       1.14      maxv 	error = tb->tb_ops->tbo_start(param);
    327        1.1      yamt 	if (error != 0) {
    328        1.9      yamt 		KASSERT(tb->tb_usecount > 0);
    329        1.9      yamt 		tb->tb_usecount--;
    330        1.1      yamt 		tprof_stop1();
    331        1.1      yamt 		goto done;
    332        1.1      yamt 	}
    333        1.1      yamt 
    334        1.1      yamt 	mutex_enter(&tprof_lock);
    335        1.1      yamt 	tprof_running = true;
    336        1.1      yamt 	mutex_exit(&tprof_lock);
    337        1.1      yamt 	for (CPU_INFO_FOREACH(cii, ci)) {
    338        1.1      yamt 		tprof_cpu_t * const c = tprof_cpu(ci);
    339        1.1      yamt 
    340        1.1      yamt 		mutex_enter(&tprof_lock);
    341        1.1      yamt 		tprof_nworker++;
    342        1.1      yamt 		mutex_exit(&tprof_lock);
    343        1.1      yamt 		workqueue_enqueue(tprof_wq, &c->c_work, ci);
    344        1.1      yamt 	}
    345        1.1      yamt done:
    346        1.1      yamt 	return error;
    347        1.1      yamt }
    348        1.1      yamt 
    349        1.1      yamt static void
    350        1.1      yamt tprof_stop(void)
    351        1.1      yamt {
    352        1.4      yamt 	tprof_backend_t *tb;
    353        1.1      yamt 
    354        1.1      yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    355        1.1      yamt 	if (!tprof_running) {
    356        1.1      yamt 		goto done;
    357        1.1      yamt 	}
    358        1.1      yamt 
    359        1.4      yamt 	tb = tprof_backend;
    360        1.4      yamt 	KASSERT(tb->tb_usecount > 0);
    361        1.4      yamt 	tb->tb_ops->tbo_stop(NULL);
    362        1.4      yamt 	tb->tb_usecount--;
    363        1.1      yamt 
    364        1.1      yamt 	mutex_enter(&tprof_lock);
    365        1.1      yamt 	tprof_running = false;
    366        1.1      yamt 	cv_broadcast(&tprof_reader_cv);
    367        1.8      yamt 	while (tprof_nworker > 0) {
    368        1.8      yamt 		cv_wait(&tprof_cv, &tprof_lock);
    369        1.8      yamt 	}
    370        1.1      yamt 	mutex_exit(&tprof_lock);
    371        1.1      yamt 
    372        1.1      yamt 	tprof_stop1();
    373        1.1      yamt done:
    374        1.1      yamt 	;
    375        1.1      yamt }
    376        1.1      yamt 
    377        1.4      yamt /*
    378        1.4      yamt  * tprof_clear: drain unread samples.
    379        1.4      yamt  */
    380        1.4      yamt 
    381        1.1      yamt static void
    382        1.1      yamt tprof_clear(void)
    383        1.1      yamt {
    384        1.1      yamt 	tprof_buf_t *buf;
    385        1.1      yamt 
    386        1.1      yamt 	mutex_enter(&tprof_reader_lock);
    387        1.1      yamt 	mutex_enter(&tprof_lock);
    388        1.1      yamt 	while ((buf = STAILQ_FIRST(&tprof_list)) != NULL) {
    389        1.1      yamt 		if (buf != NULL) {
    390        1.1      yamt 			STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    391        1.1      yamt 			KASSERT(tprof_nbuf_on_list > 0);
    392        1.1      yamt 			tprof_nbuf_on_list--;
    393        1.1      yamt 			mutex_exit(&tprof_lock);
    394        1.1      yamt 			tprof_buf_free(buf);
    395        1.1      yamt 			mutex_enter(&tprof_lock);
    396        1.1      yamt 		}
    397        1.1      yamt 	}
    398        1.1      yamt 	KASSERT(tprof_nbuf_on_list == 0);
    399        1.1      yamt 	mutex_exit(&tprof_lock);
    400        1.1      yamt 	tprof_reader_offset = 0;
    401        1.1      yamt 	mutex_exit(&tprof_reader_lock);
    402        1.1      yamt 
    403        1.1      yamt 	memset(&tprof_stat, 0, sizeof(tprof_stat));
    404        1.1      yamt }
    405        1.1      yamt 
    406        1.4      yamt static tprof_backend_t *
    407        1.4      yamt tprof_backend_lookup(const char *name)
    408        1.4      yamt {
    409        1.4      yamt 	tprof_backend_t *tb;
    410        1.4      yamt 
    411        1.4      yamt 	KASSERT(mutex_owned(&tprof_startstop_lock));
    412        1.4      yamt 
    413        1.4      yamt 	LIST_FOREACH(tb, &tprof_backends, tb_list) {
    414        1.4      yamt 		if (!strcmp(tb->tb_name, name)) {
    415        1.4      yamt 			return tb;
    416        1.4      yamt 		}
    417        1.4      yamt 	}
    418        1.4      yamt 	return NULL;
    419        1.4      yamt }
    420        1.4      yamt 
    421        1.1      yamt /* -------------------- backend interfaces */
    422        1.1      yamt 
    423        1.1      yamt /*
    424        1.1      yamt  * tprof_sample: record a sample on the per-cpu buffer.
    425        1.1      yamt  *
    426        1.1      yamt  * be careful; can be called in NMI context.
    427       1.10      yamt  * we are bluntly assuming the followings are safe.
    428       1.10      yamt  *	curcpu()
    429       1.10      yamt  *	curlwp->l_lid
    430       1.10      yamt  *	curlwp->l_proc->p_pid
    431        1.1      yamt  */
    432        1.1      yamt 
    433        1.1      yamt void
    434       1.14      maxv tprof_sample(void *unused, const tprof_frame_info_t *tfi)
    435        1.1      yamt {
    436        1.1      yamt 	tprof_cpu_t * const c = tprof_curcpu();
    437        1.1      yamt 	tprof_buf_t * const buf = c->c_buf;
    438        1.8      yamt 	tprof_sample_t *sp;
    439        1.5      yamt 	const uintptr_t pc = tfi->tfi_pc;
    440       1.10      yamt 	const lwp_t * const l = curlwp;
    441        1.1      yamt 	u_int idx;
    442        1.1      yamt 
    443        1.1      yamt 	idx = buf->b_used;
    444        1.1      yamt 	if (__predict_false(idx >= buf->b_size)) {
    445        1.1      yamt 		buf->b_overflow++;
    446        1.1      yamt 		return;
    447        1.1      yamt 	}
    448        1.8      yamt 	sp = &buf->b_data[idx];
    449       1.10      yamt 	sp->s_pid = l->l_proc->p_pid;
    450       1.10      yamt 	sp->s_lwpid = l->l_lid;
    451       1.10      yamt 	sp->s_cpuid = c->c_cpuid;
    452        1.8      yamt 	sp->s_flags = (tfi->tfi_inkernel) ? TPROF_SAMPLE_INKERNEL : 0;
    453        1.8      yamt 	sp->s_pc = pc;
    454        1.1      yamt 	buf->b_used = idx + 1;
    455        1.1      yamt }
    456        1.1      yamt 
    457        1.4      yamt /*
    458        1.4      yamt  * tprof_backend_register:
    459        1.4      yamt  */
    460        1.4      yamt 
    461        1.4      yamt int
    462        1.4      yamt tprof_backend_register(const char *name, const tprof_backend_ops_t *ops,
    463        1.4      yamt     int vers)
    464        1.4      yamt {
    465        1.4      yamt 	tprof_backend_t *tb;
    466        1.4      yamt 
    467        1.4      yamt 	if (vers != TPROF_BACKEND_VERSION) {
    468        1.4      yamt 		return EINVAL;
    469        1.4      yamt 	}
    470        1.4      yamt 
    471        1.4      yamt 	mutex_enter(&tprof_startstop_lock);
    472        1.4      yamt 	tb = tprof_backend_lookup(name);
    473        1.4      yamt 	if (tb != NULL) {
    474        1.4      yamt 		mutex_exit(&tprof_startstop_lock);
    475        1.4      yamt 		return EEXIST;
    476        1.4      yamt 	}
    477        1.4      yamt #if 1 /* XXX for now */
    478        1.4      yamt 	if (!LIST_EMPTY(&tprof_backends)) {
    479        1.4      yamt 		mutex_exit(&tprof_startstop_lock);
    480        1.4      yamt 		return ENOTSUP;
    481        1.4      yamt 	}
    482        1.4      yamt #endif
    483        1.4      yamt 	tb = kmem_alloc(sizeof(*tb), KM_SLEEP);
    484        1.4      yamt 	tb->tb_name = name;
    485        1.4      yamt 	tb->tb_ops = ops;
    486        1.4      yamt 	tb->tb_usecount = 0;
    487        1.4      yamt 	LIST_INSERT_HEAD(&tprof_backends, tb, tb_list);
    488        1.4      yamt #if 1 /* XXX for now */
    489        1.4      yamt 	if (tprof_backend == NULL) {
    490        1.4      yamt 		tprof_backend = tb;
    491        1.4      yamt 	}
    492        1.4      yamt #endif
    493        1.4      yamt 	mutex_exit(&tprof_startstop_lock);
    494        1.4      yamt 
    495        1.4      yamt 	return 0;
    496        1.4      yamt }
    497        1.4      yamt 
    498        1.4      yamt /*
    499        1.4      yamt  * tprof_backend_unregister:
    500        1.4      yamt  */
    501        1.4      yamt 
    502        1.4      yamt int
    503        1.4      yamt tprof_backend_unregister(const char *name)
    504        1.4      yamt {
    505        1.4      yamt 	tprof_backend_t *tb;
    506        1.4      yamt 
    507        1.4      yamt 	mutex_enter(&tprof_startstop_lock);
    508        1.4      yamt 	tb = tprof_backend_lookup(name);
    509        1.4      yamt #if defined(DIAGNOSTIC)
    510        1.4      yamt 	if (tb == NULL) {
    511        1.4      yamt 		mutex_exit(&tprof_startstop_lock);
    512        1.4      yamt 		panic("%s: not found '%s'", __func__, name);
    513        1.4      yamt 	}
    514        1.4      yamt #endif /* defined(DIAGNOSTIC) */
    515        1.4      yamt 	if (tb->tb_usecount > 0) {
    516        1.4      yamt 		mutex_exit(&tprof_startstop_lock);
    517        1.4      yamt 		return EBUSY;
    518        1.4      yamt 	}
    519        1.4      yamt #if 1 /* XXX for now */
    520        1.4      yamt 	if (tprof_backend == tb) {
    521        1.4      yamt 		tprof_backend = NULL;
    522        1.4      yamt 	}
    523        1.4      yamt #endif
    524        1.4      yamt 	LIST_REMOVE(tb, tb_list);
    525        1.4      yamt 	mutex_exit(&tprof_startstop_lock);
    526        1.4      yamt 
    527        1.4      yamt 	kmem_free(tb, sizeof(*tb));
    528        1.4      yamt 
    529        1.4      yamt 	return 0;
    530        1.4      yamt }
    531        1.4      yamt 
    532        1.1      yamt /* -------------------- cdevsw interfaces */
    533        1.1      yamt 
    534        1.1      yamt static int
    535        1.1      yamt tprof_open(dev_t dev, int flags, int type, struct lwp *l)
    536        1.1      yamt {
    537        1.1      yamt 
    538        1.1      yamt 	if (minor(dev) != 0) {
    539        1.1      yamt 		return EXDEV;
    540        1.1      yamt 	}
    541        1.1      yamt 	mutex_enter(&tprof_lock);
    542        1.1      yamt 	if (tprof_owner != NULL) {
    543        1.1      yamt 		mutex_exit(&tprof_lock);
    544        1.1      yamt 		return  EBUSY;
    545        1.1      yamt 	}
    546        1.1      yamt 	tprof_owner = curlwp;
    547        1.1      yamt 	mutex_exit(&tprof_lock);
    548        1.1      yamt 
    549        1.1      yamt 	return 0;
    550        1.1      yamt }
    551        1.1      yamt 
    552        1.1      yamt static int
    553        1.1      yamt tprof_close(dev_t dev, int flags, int type, struct lwp *l)
    554        1.1      yamt {
    555        1.1      yamt 
    556        1.1      yamt 	KASSERT(minor(dev) == 0);
    557        1.1      yamt 
    558        1.1      yamt 	mutex_enter(&tprof_startstop_lock);
    559        1.1      yamt 	mutex_enter(&tprof_lock);
    560        1.1      yamt 	tprof_owner = NULL;
    561        1.1      yamt 	mutex_exit(&tprof_lock);
    562        1.1      yamt 	tprof_stop();
    563        1.1      yamt 	tprof_clear();
    564        1.1      yamt 	mutex_exit(&tprof_startstop_lock);
    565        1.1      yamt 
    566        1.1      yamt 	return 0;
    567        1.1      yamt }
    568        1.1      yamt 
    569        1.1      yamt static int
    570        1.1      yamt tprof_read(dev_t dev, struct uio *uio, int flags)
    571        1.1      yamt {
    572        1.1      yamt 	tprof_buf_t *buf;
    573        1.1      yamt 	size_t bytes;
    574        1.1      yamt 	size_t resid;
    575        1.1      yamt 	size_t done;
    576        1.1      yamt 	int error = 0;
    577        1.1      yamt 
    578        1.1      yamt 	KASSERT(minor(dev) == 0);
    579        1.1      yamt 	mutex_enter(&tprof_reader_lock);
    580        1.1      yamt 	while (uio->uio_resid > 0 && error == 0) {
    581        1.1      yamt 		/*
    582        1.1      yamt 		 * take the first buffer from the list.
    583        1.1      yamt 		 */
    584        1.1      yamt 		mutex_enter(&tprof_lock);
    585        1.1      yamt 		buf = STAILQ_FIRST(&tprof_list);
    586        1.1      yamt 		if (buf == NULL) {
    587        1.1      yamt 			if (tprof_nworker == 0) {
    588        1.1      yamt 				mutex_exit(&tprof_lock);
    589        1.1      yamt 				error = 0;
    590        1.1      yamt 				break;
    591        1.1      yamt 			}
    592        1.1      yamt 			mutex_exit(&tprof_reader_lock);
    593        1.1      yamt 			error = cv_wait_sig(&tprof_reader_cv, &tprof_lock);
    594        1.1      yamt 			mutex_exit(&tprof_lock);
    595        1.1      yamt 			mutex_enter(&tprof_reader_lock);
    596        1.1      yamt 			continue;
    597        1.1      yamt 		}
    598        1.1      yamt 		STAILQ_REMOVE_HEAD(&tprof_list, b_list);
    599        1.1      yamt 		KASSERT(tprof_nbuf_on_list > 0);
    600        1.1      yamt 		tprof_nbuf_on_list--;
    601        1.1      yamt 		mutex_exit(&tprof_lock);
    602        1.1      yamt 
    603        1.1      yamt 		/*
    604        1.1      yamt 		 * copy it out.
    605        1.1      yamt 		 */
    606        1.1      yamt 		bytes = MIN(buf->b_used * sizeof(tprof_sample_t) -
    607        1.1      yamt 		    tprof_reader_offset, uio->uio_resid);
    608        1.1      yamt 		resid = uio->uio_resid;
    609        1.1      yamt 		error = uiomove((char *)buf->b_data + tprof_reader_offset,
    610        1.1      yamt 		    bytes, uio);
    611        1.1      yamt 		done = resid - uio->uio_resid;
    612        1.1      yamt 		tprof_reader_offset += done;
    613        1.1      yamt 
    614        1.1      yamt 		/*
    615        1.1      yamt 		 * if we didn't consume the whole buffer,
    616        1.1      yamt 		 * put it back to the list.
    617        1.1      yamt 		 */
    618        1.1      yamt 		if (tprof_reader_offset <
    619        1.1      yamt 		    buf->b_used * sizeof(tprof_sample_t)) {
    620        1.1      yamt 			mutex_enter(&tprof_lock);
    621        1.1      yamt 			STAILQ_INSERT_HEAD(&tprof_list, buf, b_list);
    622        1.1      yamt 			tprof_nbuf_on_list++;
    623        1.1      yamt 			cv_broadcast(&tprof_reader_cv);
    624        1.1      yamt 			mutex_exit(&tprof_lock);
    625        1.1      yamt 		} else {
    626        1.1      yamt 			tprof_buf_free(buf);
    627        1.1      yamt 			tprof_reader_offset = 0;
    628        1.1      yamt 		}
    629        1.1      yamt 	}
    630        1.1      yamt 	mutex_exit(&tprof_reader_lock);
    631        1.1      yamt 
    632        1.1      yamt 	return error;
    633        1.1      yamt }
    634        1.1      yamt 
    635        1.1      yamt static int
    636        1.1      yamt tprof_ioctl(dev_t dev, u_long cmd, void *data, int flags, struct lwp *l)
    637        1.1      yamt {
    638       1.14      maxv 	const tprof_param_t *param;
    639        1.1      yamt 	int error = 0;
    640        1.1      yamt 
    641        1.1      yamt 	KASSERT(minor(dev) == 0);
    642        1.1      yamt 
    643        1.1      yamt 	switch (cmd) {
    644       1.14      maxv 	case TPROF_IOC_GETINFO:
    645       1.14      maxv 		mutex_enter(&tprof_startstop_lock);
    646       1.14      maxv 		tprof_getinfo(data);
    647       1.14      maxv 		mutex_exit(&tprof_startstop_lock);
    648        1.1      yamt 		break;
    649        1.1      yamt 	case TPROF_IOC_START:
    650        1.1      yamt 		param = data;
    651        1.1      yamt 		mutex_enter(&tprof_startstop_lock);
    652        1.1      yamt 		error = tprof_start(param);
    653        1.1      yamt 		mutex_exit(&tprof_startstop_lock);
    654        1.1      yamt 		break;
    655        1.1      yamt 	case TPROF_IOC_STOP:
    656        1.1      yamt 		mutex_enter(&tprof_startstop_lock);
    657        1.1      yamt 		tprof_stop();
    658        1.1      yamt 		mutex_exit(&tprof_startstop_lock);
    659        1.1      yamt 		break;
    660        1.1      yamt 	case TPROF_IOC_GETSTAT:
    661        1.1      yamt 		mutex_enter(&tprof_lock);
    662        1.1      yamt 		memcpy(data, &tprof_stat, sizeof(tprof_stat));
    663        1.1      yamt 		mutex_exit(&tprof_lock);
    664        1.1      yamt 		break;
    665        1.1      yamt 	default:
    666        1.1      yamt 		error = EINVAL;
    667        1.1      yamt 		break;
    668        1.1      yamt 	}
    669        1.1      yamt 
    670        1.1      yamt 	return error;
    671        1.1      yamt }
    672        1.1      yamt 
    673        1.1      yamt const struct cdevsw tprof_cdevsw = {
    674        1.1      yamt 	.d_open = tprof_open,
    675        1.1      yamt 	.d_close = tprof_close,
    676        1.1      yamt 	.d_read = tprof_read,
    677        1.1      yamt 	.d_write = nowrite,
    678        1.1      yamt 	.d_ioctl = tprof_ioctl,
    679        1.1      yamt 	.d_stop = nostop,
    680        1.1      yamt 	.d_tty = notty,
    681        1.1      yamt 	.d_poll = nopoll,
    682        1.1      yamt 	.d_mmap = nommap,
    683        1.1      yamt 	.d_kqfilter = nokqfilter,
    684       1.12  dholland 	.d_discard = nodiscard,
    685       1.11  dholland 	.d_flag = D_OTHER | D_MPSAFE
    686        1.1      yamt };
    687        1.1      yamt 
    688        1.1      yamt void
    689        1.1      yamt tprofattach(int nunits)
    690        1.1      yamt {
    691        1.1      yamt 
    692        1.4      yamt 	/* nothing */
    693        1.4      yamt }
    694        1.4      yamt 
    695        1.4      yamt MODULE(MODULE_CLASS_DRIVER, tprof, NULL);
    696        1.4      yamt 
    697        1.4      yamt static void
    698  1.14.14.1   thorpej tprof_cpu_init(void *vcp, void *vcookie, struct cpu_info *ci)
    699  1.14.14.1   thorpej {
    700  1.14.14.1   thorpej 	tprof_cpu_t **cp = vcp, *c;
    701  1.14.14.1   thorpej 
    702  1.14.14.1   thorpej 	c = kmem_zalloc(sizeof(*c), KM_SLEEP);
    703  1.14.14.1   thorpej 	c->c_buf = NULL;
    704  1.14.14.1   thorpej 	c->c_cpuid = cpu_index(ci);
    705  1.14.14.1   thorpej 	*cp = c;
    706  1.14.14.1   thorpej }
    707  1.14.14.1   thorpej 
    708  1.14.14.1   thorpej static void
    709  1.14.14.1   thorpej tprof_cpu_fini(void *vcp, void *vcookie, struct cpu_info *ci)
    710  1.14.14.1   thorpej {
    711  1.14.14.1   thorpej 	tprof_cpu_t **cp = vcp, *c;
    712  1.14.14.1   thorpej 
    713  1.14.14.1   thorpej 	c = *cp;
    714  1.14.14.1   thorpej 	KASSERT(c->c_cpuid == cpu_index(ci));
    715  1.14.14.1   thorpej 	KASSERT(c->c_buf == NULL);
    716  1.14.14.1   thorpej 	kmem_free(c, sizeof(*c));
    717  1.14.14.1   thorpej 	*cp = NULL;
    718  1.14.14.1   thorpej }
    719  1.14.14.1   thorpej 
    720  1.14.14.1   thorpej static void
    721        1.4      yamt tprof_driver_init(void)
    722        1.4      yamt {
    723        1.4      yamt 
    724        1.1      yamt 	mutex_init(&tprof_lock, MUTEX_DEFAULT, IPL_NONE);
    725        1.1      yamt 	mutex_init(&tprof_reader_lock, MUTEX_DEFAULT, IPL_NONE);
    726        1.1      yamt 	mutex_init(&tprof_startstop_lock, MUTEX_DEFAULT, IPL_NONE);
    727        1.1      yamt 	cv_init(&tprof_cv, "tprof");
    728        1.7  pgoyette 	cv_init(&tprof_reader_cv, "tprof_rd");
    729        1.1      yamt 	STAILQ_INIT(&tprof_list);
    730  1.14.14.1   thorpej 	tprof_cpus = percpu_create(sizeof(tprof_cpu_t *),
    731  1.14.14.1   thorpej 	    tprof_cpu_init, tprof_cpu_fini, NULL);
    732        1.1      yamt }
    733        1.4      yamt 
    734        1.4      yamt static void
    735        1.4      yamt tprof_driver_fini(void)
    736        1.4      yamt {
    737        1.4      yamt 
    738  1.14.14.1   thorpej 	percpu_free(tprof_cpus, sizeof(tprof_cpu_t *));
    739        1.4      yamt 	mutex_destroy(&tprof_lock);
    740        1.4      yamt 	mutex_destroy(&tprof_reader_lock);
    741        1.4      yamt 	mutex_destroy(&tprof_startstop_lock);
    742        1.4      yamt 	cv_destroy(&tprof_cv);
    743        1.4      yamt 	cv_destroy(&tprof_reader_cv);
    744        1.4      yamt }
    745        1.4      yamt 
    746        1.4      yamt static int
    747        1.4      yamt tprof_modcmd(modcmd_t cmd, void *arg)
    748        1.4      yamt {
    749        1.4      yamt 
    750        1.4      yamt 	switch (cmd) {
    751        1.4      yamt 	case MODULE_CMD_INIT:
    752        1.4      yamt 		tprof_driver_init();
    753        1.4      yamt #if defined(_MODULE)
    754        1.4      yamt 		{
    755        1.4      yamt 			devmajor_t bmajor = NODEVMAJOR;
    756        1.4      yamt 			devmajor_t cmajor = NODEVMAJOR;
    757        1.4      yamt 			int error;
    758        1.4      yamt 
    759        1.4      yamt 			error = devsw_attach("tprof", NULL, &bmajor,
    760        1.4      yamt 			    &tprof_cdevsw, &cmajor);
    761        1.4      yamt 			if (error) {
    762        1.4      yamt 				tprof_driver_fini();
    763        1.4      yamt 				return error;
    764        1.4      yamt 			}
    765        1.4      yamt 		}
    766        1.4      yamt #endif /* defined(_MODULE) */
    767        1.4      yamt 		return 0;
    768        1.4      yamt 
    769        1.4      yamt 	case MODULE_CMD_FINI:
    770        1.4      yamt #if defined(_MODULE)
    771        1.4      yamt 		{
    772        1.4      yamt 			int error;
    773        1.4      yamt 			error = devsw_detach(NULL, &tprof_cdevsw);
    774        1.4      yamt 			if (error) {
    775        1.4      yamt 				return error;
    776        1.4      yamt 			}
    777        1.4      yamt 		}
    778        1.4      yamt #endif /* defined(_MODULE) */
    779        1.4      yamt 		tprof_driver_fini();
    780        1.4      yamt 		return 0;
    781        1.4      yamt 
    782        1.4      yamt 	default:
    783        1.4      yamt 		return ENOTTY;
    784        1.4      yamt 	}
    785        1.4      yamt }
    786