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