1 /*
2 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/5.3/sys/kern/subr_devstat.c 136588 2004-10-16 08:43:07Z cvs2svn $");
31
32 #include <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/bio.h>
36 #include <sys/devicestat.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
39 #include <sys/lock.h>
40 #include <sys/mutex.h>
41 #include <sys/conf.h>
42 #include <vm/vm.h>
43 #include <vm/pmap.h>
44
45 #include <machine/atomic.h>
46
47 static int devstat_num_devs;
48 static long devstat_generation = 1;
49 static int devstat_version = DEVSTAT_VERSION;
50 static int devstat_current_devnumber;
51 static struct mtx devstat_mutex;
52
53 static struct devstatlist device_statq;
54 static struct devstat *devstat_alloc(void);
55 static void devstat_free(struct devstat *);
56 static void devstat_add_entry(struct devstat *ds, const void *dev_name,
57 int unit_number, u_int32_t block_size,
58 devstat_support_flags flags,
59 devstat_type_flags device_type,
60 devstat_priority priority);
61
62 /*
63 * Allocate a devstat and initialize it
64 */
65 struct devstat *
66 devstat_new_entry(const void *dev_name,
67 int unit_number, u_int32_t block_size,
68 devstat_support_flags flags,
69 devstat_type_flags device_type,
70 devstat_priority priority)
71 {
72 struct devstat *ds;
73 static int once;
74
75 if (!once) {
76 STAILQ_INIT(&device_statq);
77 mtx_init(&devstat_mutex, "devstat", NULL, MTX_DEF);
78 once = 1;
79 }
80 mtx_assert(&devstat_mutex, MA_NOTOWNED);
81
82 ds = devstat_alloc();
83 mtx_lock(&devstat_mutex);
84 if (unit_number == -1) {
85 ds->id = dev_name;
86 binuptime(&ds->creation_time);
87 devstat_generation++;
88 } else {
89 devstat_add_entry(ds, dev_name, unit_number, block_size,
90 flags, device_type, priority);
91 }
92 mtx_unlock(&devstat_mutex);
93 return (ds);
94 }
95
96 /*
97 * Take a malloced and zeroed devstat structure given to us, fill it in
98 * and add it to the queue of devices.
99 */
100 static void
101 devstat_add_entry(struct devstat *ds, const void *dev_name,
102 int unit_number, u_int32_t block_size,
103 devstat_support_flags flags,
104 devstat_type_flags device_type,
105 devstat_priority priority)
106 {
107 struct devstatlist *devstat_head;
108 struct devstat *ds_tmp;
109
110 mtx_assert(&devstat_mutex, MA_OWNED);
111 devstat_num_devs++;
112
113 devstat_head = &device_statq;
114
115 /*
116 * Priority sort. Each driver passes in its priority when it adds
117 * its devstat entry. Drivers are sorted first by priority, and
118 * then by probe order.
119 *
120 * For the first device, we just insert it, since the priority
121 * doesn't really matter yet. Subsequent devices are inserted into
122 * the list using the order outlined above.
123 */
124 if (devstat_num_devs == 1)
125 STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
126 else {
127 STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
128 struct devstat *ds_next;
129
130 ds_next = STAILQ_NEXT(ds_tmp, dev_links);
131
132 /*
133 * If we find a break between higher and lower
134 * priority items, and if this item fits in the
135 * break, insert it. This also applies if the
136 * "lower priority item" is the end of the list.
137 */
138 if ((priority <= ds_tmp->priority)
139 && ((ds_next == NULL)
140 || (priority > ds_next->priority))) {
141 STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
142 dev_links);
143 break;
144 } else if (priority > ds_tmp->priority) {
145 /*
146 * If this is the case, we should be able
147 * to insert ourselves at the head of the
148 * list. If we can't, something is wrong.
149 */
150 if (ds_tmp == STAILQ_FIRST(devstat_head)) {
151 STAILQ_INSERT_HEAD(devstat_head,
152 ds, dev_links);
153 break;
154 } else {
155 STAILQ_INSERT_TAIL(devstat_head,
156 ds, dev_links);
157 printf("devstat_add_entry: HELP! "
158 "sorting problem detected "
159 "for name %p unit %d\n",
160 dev_name, unit_number);
161 break;
162 }
163 }
164 }
165 }
166
167 ds->device_number = devstat_current_devnumber++;
168 ds->unit_number = unit_number;
169 strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
170 ds->block_size = block_size;
171 ds->flags = flags;
172 ds->device_type = device_type;
173 ds->priority = priority;
174 binuptime(&ds->creation_time);
175 devstat_generation++;
176 }
177
178 /*
179 * Remove a devstat structure from the list of devices.
180 */
181 void
182 devstat_remove_entry(struct devstat *ds)
183 {
184 struct devstatlist *devstat_head;
185
186 mtx_assert(&devstat_mutex, MA_NOTOWNED);
187 if (ds == NULL)
188 return;
189
190 mtx_lock(&devstat_mutex);
191
192 devstat_head = &device_statq;
193
194 /* Remove this entry from the devstat queue */
195 atomic_add_acq_int(&ds->sequence1, 1);
196 if (ds->id == NULL) {
197 devstat_num_devs--;
198 STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
199 }
200 devstat_free(ds);
201 devstat_generation++;
202 mtx_unlock(&devstat_mutex);
203 }
204
205 /*
206 * Record a transaction start.
207 *
208 * See comments for devstat_end_transaction(). Ordering is very important
209 * here.
210 */
211 void
212 devstat_start_transaction(struct devstat *ds, struct bintime *now)
213 {
214
215 mtx_assert(&devstat_mutex, MA_NOTOWNED);
216
217 /* sanity check */
218 if (ds == NULL)
219 return;
220
221 atomic_add_acq_int(&ds->sequence1, 1);
222 /*
223 * We only want to set the start time when we are going from idle
224 * to busy. The start time is really the start of the latest busy
225 * period.
226 */
227 if (ds->start_count == ds->end_count) {
228 if (now != NULL)
229 ds->busy_from = *now;
230 else
231 binuptime(&ds->busy_from);
232 }
233 ds->start_count++;
234 atomic_add_rel_int(&ds->sequence0, 1);
235 }
236
237 void
238 devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
239 {
240
241 mtx_assert(&devstat_mutex, MA_NOTOWNED);
242
243 /* sanity check */
244 if (ds == NULL)
245 return;
246
247 binuptime(&bp->bio_t0);
248 devstat_start_transaction(ds, &bp->bio_t0);
249 }
250
251 /*
252 * Record the ending of a transaction, and incrment the various counters.
253 *
254 * Ordering in this function, and in devstat_start_transaction() is VERY
255 * important. The idea here is to run without locks, so we are very
256 * careful to only modify some fields on the way "down" (i.e. at
257 * transaction start) and some fields on the way "up" (i.e. at transaction
258 * completion). One exception is busy_from, which we only modify in
259 * devstat_start_transaction() when there are no outstanding transactions,
260 * and thus it can't be modified in devstat_end_transaction()
261 * simultaneously.
262 *
263 * The sequence0 and sequence1 fields are provided to enable an application
264 * spying on the structures with mmap(2) to tell when a structure is in a
265 * consistent state or not.
266 *
267 * For this to work 100% reliably, it is important that the two fields
268 * are at opposite ends of the structure and that they are incremented
269 * in the opposite order of how a memcpy(3) in userland would copy them.
270 * We assume that the copying happens front to back, but there is actually
271 * no way short of writing your own memcpy(3) replacement to guarantee
272 * this will be the case.
273 *
274 * In addition to this, being a kind of locks, they must be updated with
275 * atomic instructions using appropriate memory barriers.
276 */
277 void
278 devstat_end_transaction(struct devstat *ds, u_int32_t bytes,
279 devstat_tag_type tag_type, devstat_trans_flags flags,
280 struct bintime *now, struct bintime *then)
281 {
282 struct bintime dt, lnow;
283
284 mtx_assert(&devstat_mutex, MA_NOTOWNED);
285
286 /* sanity check */
287 if (ds == NULL)
288 return;
289
290 if (now == NULL) {
291 now = &lnow;
292 binuptime(now);
293 }
294
295 atomic_add_acq_int(&ds->sequence1, 1);
296 /* Update byte and operations counts */
297 ds->bytes[flags] += bytes;
298 ds->operations[flags]++;
299
300 /*
301 * Keep a count of the various tag types sent.
302 */
303 if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
304 tag_type != DEVSTAT_TAG_NONE)
305 ds->tag_types[tag_type]++;
306
307 if (then != NULL) {
308 /* Update duration of operations */
309 dt = *now;
310 bintime_sub(&dt, then);
311 bintime_add(&ds->duration[flags], &dt);
312 }
313
314 /* Accumulate busy time */
315 dt = *now;
316 bintime_sub(&dt, &ds->busy_from);
317 bintime_add(&ds->busy_time, &dt);
318 ds->busy_from = *now;
319
320 ds->end_count++;
321 atomic_add_rel_int(&ds->sequence0, 1);
322 }
323
324 void
325 devstat_end_transaction_bio(struct devstat *ds, struct bio *bp)
326 {
327 devstat_trans_flags flg;
328
329 mtx_assert(&devstat_mutex, MA_NOTOWNED);
330
331 /* sanity check */
332 if (ds == NULL)
333 return;
334
335 if (bp->bio_cmd == BIO_DELETE)
336 flg = DEVSTAT_FREE;
337 else if (bp->bio_cmd == BIO_READ)
338 flg = DEVSTAT_READ;
339 else if (bp->bio_cmd == BIO_WRITE)
340 flg = DEVSTAT_WRITE;
341 else
342 flg = DEVSTAT_NO_DATA;
343
344 devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
345 DEVSTAT_TAG_SIMPLE, flg, NULL, &bp->bio_t0);
346 }
347
348 /*
349 * This is the sysctl handler for the devstat package. The data pushed out
350 * on the kern.devstat.all sysctl variable consists of the current devstat
351 * generation number, and then an array of devstat structures, one for each
352 * device in the system.
353 *
354 * This is more cryptic that obvious, but basically we neither can nor
355 * want to hold the devstat_mutex for any amount of time, so we grab it
356 * only when we need to and keep an eye on devstat_generation all the time.
357 */
358 static int
359 sysctl_devstat(SYSCTL_HANDLER_ARGS)
360 {
361 int error;
362 long mygen;
363 struct devstat *nds;
364
365 mtx_assert(&devstat_mutex, MA_NOTOWNED);
366
367 /*
368 * XXX devstat_generation should really be "volatile" but that
369 * XXX freaks out the sysctl macro below. The places where we
370 * XXX change it and inspect it are bracketed in the mutex which
371 * XXX guarantees us proper write barriers. I don't belive the
372 * XXX compiler is allowed to optimize mygen away across calls
373 * XXX to other functions, so the following is belived to be safe.
374 */
375 mygen = devstat_generation;
376
377 error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
378
379 if (devstat_num_devs == 0)
380 return(0);
381
382 if (error != 0)
383 return (error);
384
385 mtx_lock(&devstat_mutex);
386 nds = STAILQ_FIRST(&device_statq);
387 if (mygen != devstat_generation)
388 error = EBUSY;
389 mtx_unlock(&devstat_mutex);
390
391 if (error != 0)
392 return (error);
393
394 for (;nds != NULL;) {
395 error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
396 if (error != 0)
397 return (error);
398 mtx_lock(&devstat_mutex);
399 if (mygen != devstat_generation)
400 error = EBUSY;
401 else
402 nds = STAILQ_NEXT(nds, dev_links);
403 mtx_unlock(&devstat_mutex);
404 if (error != 0)
405 return (error);
406 }
407 return(error);
408 }
409
410 /*
411 * Sysctl entries for devstat. The first one is a node that all the rest
412 * hang off of.
413 */
414 SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics");
415
416 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
417 0, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list");
418 /*
419 * Export the number of devices in the system so that userland utilities
420 * can determine how much memory to allocate to hold all the devices.
421 */
422 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
423 &devstat_num_devs, 0, "Number of devices in the devstat list");
424 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
425 &devstat_generation, 0, "Devstat list generation");
426 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
427 &devstat_version, 0, "Devstat list version number");
428
429 /*
430 * Allocator for struct devstat structures. We sub-allocate these from pages
431 * which we get from malloc. These pages are exported for mmap(2)'ing through
432 * a miniature device driver
433 */
434
435 #define statsperpage (PAGE_SIZE / sizeof(struct devstat))
436
437 static d_mmap_t devstat_mmap;
438
439 static struct cdevsw devstat_cdevsw = {
440 .d_version = D_VERSION,
441 .d_flags = D_NEEDGIANT,
442 .d_mmap = devstat_mmap,
443 .d_name = "devstat",
444 };
445
446 struct statspage {
447 TAILQ_ENTRY(statspage) list;
448 struct devstat *stat;
449 u_int nfree;
450 };
451
452 static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
453 static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
454
455 static int
456 devstat_mmap(struct cdev *dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
457 {
458 struct statspage *spp;
459
460 if (nprot != VM_PROT_READ)
461 return (-1);
462 TAILQ_FOREACH(spp, &pagelist, list) {
463 if (offset == 0) {
464 *paddr = vtophys(spp->stat);
465 return (0);
466 }
467 offset -= PAGE_SIZE;
468 }
469 return (-1);
470 }
471
472 static struct devstat *
473 devstat_alloc(void)
474 {
475 struct devstat *dsp;
476 struct statspage *spp;
477 u_int u;
478 static int once;
479
480 mtx_assert(&devstat_mutex, MA_NOTOWNED);
481 if (!once) {
482 make_dev(&devstat_cdevsw, 0,
483 UID_ROOT, GID_WHEEL, 0400, DEVSTAT_DEVICE_NAME);
484 once = 1;
485 }
486 mtx_lock(&devstat_mutex);
487 for (;;) {
488 TAILQ_FOREACH(spp, &pagelist, list) {
489 if (spp->nfree > 0)
490 break;
491 }
492 if (spp != NULL)
493 break;
494 /*
495 * We had no free slot in any of our pages, drop the mutex
496 * and get another page. In theory we could have more than
497 * one process doing this at the same time and consequently
498 * we may allocate more pages than we will need. That is
499 * Just Too Bad[tm], we can live with that.
500 */
501 mtx_unlock(&devstat_mutex);
502 spp = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
503 spp->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
504 spp->nfree = statsperpage;
505 mtx_lock(&devstat_mutex);
506 /*
507 * It would make more sense to add the new page at the head
508 * but the order on the list determine the sequence of the
509 * mapping so we can't do that.
510 */
511 TAILQ_INSERT_TAIL(&pagelist, spp, list);
512 }
513 dsp = spp->stat;
514 for (u = 0; u < statsperpage; u++) {
515 if (dsp->allocated == 0)
516 break;
517 dsp++;
518 }
519 spp->nfree--;
520 dsp->allocated = 1;
521 mtx_unlock(&devstat_mutex);
522 return (dsp);
523 }
524
525 static void
526 devstat_free(struct devstat *dsp)
527 {
528 struct statspage *spp;
529
530 mtx_assert(&devstat_mutex, MA_OWNED);
531 bzero(dsp, sizeof *dsp);
532 TAILQ_FOREACH(spp, &pagelist, list) {
533 if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
534 spp->nfree++;
535 return;
536 }
537 }
538 }
539
540 SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD,
541 0, sizeof(struct devstat), "sizeof(struct devstat)");
Cache object: 4004bc1b1172b9bf507b1c13d2deb48a
|