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