2 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include "opt_kdtrace.h"
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
38 #include <sys/devicestat.h>
40 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
43 #include <sys/mutex.h>
48 #include <machine/atomic.h>
50 SDT_PROVIDER_DEFINE(io);
52 SDT_PROBE_DEFINE2(io, , , start, "struct bio *", "struct devstat *");
53 SDT_PROBE_DEFINE2(io, , , done, "struct bio *", "struct devstat *");
54 SDT_PROBE_DEFINE2(io, , , wait__start, "struct bio *",
56 SDT_PROBE_DEFINE2(io, , , wait__done, "struct bio *",
59 #define DTRACE_DEVSTAT_START() SDT_PROBE2(io, , , start, NULL, ds)
60 #define DTRACE_DEVSTAT_BIO_START() SDT_PROBE2(io, , , start, bp, ds)
61 #define DTRACE_DEVSTAT_DONE() SDT_PROBE2(io, , , done, NULL, ds)
62 #define DTRACE_DEVSTAT_BIO_DONE() SDT_PROBE2(io, , , done, bp, ds)
63 #define DTRACE_DEVSTAT_WAIT_START() SDT_PROBE2(io, , , wait__start, NULL, ds)
64 #define DTRACE_DEVSTAT_WAIT_DONE() SDT_PROBE2(io, , , wait__done, NULL, ds)
66 static int devstat_num_devs;
67 static long devstat_generation = 1;
68 static int devstat_version = DEVSTAT_VERSION;
69 static int devstat_current_devnumber;
70 static struct mtx devstat_mutex;
71 MTX_SYSINIT(devstat_mutex, &devstat_mutex, "devstat", MTX_DEF);
73 static struct devstatlist device_statq = STAILQ_HEAD_INITIALIZER(device_statq);
74 static struct devstat *devstat_alloc(void);
75 static void devstat_free(struct devstat *);
76 static void devstat_add_entry(struct devstat *ds, const void *dev_name,
77 int unit_number, uint32_t block_size,
78 devstat_support_flags flags,
79 devstat_type_flags device_type,
80 devstat_priority priority);
83 * Allocate a devstat and initialize it
86 devstat_new_entry(const void *dev_name,
87 int unit_number, uint32_t block_size,
88 devstat_support_flags flags,
89 devstat_type_flags device_type,
90 devstat_priority priority)
94 mtx_assert(&devstat_mutex, MA_NOTOWNED);
97 mtx_lock(&devstat_mutex);
98 if (unit_number == -1) {
99 ds->unit_number = unit_number;
101 binuptime(&ds->creation_time);
102 devstat_generation++;
104 devstat_add_entry(ds, dev_name, unit_number, block_size,
105 flags, device_type, priority);
107 mtx_unlock(&devstat_mutex);
112 * Take a malloced and zeroed devstat structure given to us, fill it in
113 * and add it to the queue of devices.
116 devstat_add_entry(struct devstat *ds, const void *dev_name,
117 int unit_number, uint32_t block_size,
118 devstat_support_flags flags,
119 devstat_type_flags device_type,
120 devstat_priority priority)
122 struct devstatlist *devstat_head;
123 struct devstat *ds_tmp;
125 mtx_assert(&devstat_mutex, MA_OWNED);
128 devstat_head = &device_statq;
131 * Priority sort. Each driver passes in its priority when it adds
132 * its devstat entry. Drivers are sorted first by priority, and
133 * then by probe order.
135 * For the first device, we just insert it, since the priority
136 * doesn't really matter yet. Subsequent devices are inserted into
137 * the list using the order outlined above.
139 if (devstat_num_devs == 1)
140 STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
142 STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
143 struct devstat *ds_next;
145 ds_next = STAILQ_NEXT(ds_tmp, dev_links);
148 * If we find a break between higher and lower
149 * priority items, and if this item fits in the
150 * break, insert it. This also applies if the
151 * "lower priority item" is the end of the list.
153 if ((priority <= ds_tmp->priority)
154 && ((ds_next == NULL)
155 || (priority > ds_next->priority))) {
156 STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
159 } else if (priority > ds_tmp->priority) {
161 * If this is the case, we should be able
162 * to insert ourselves at the head of the
163 * list. If we can't, something is wrong.
165 if (ds_tmp == STAILQ_FIRST(devstat_head)) {
166 STAILQ_INSERT_HEAD(devstat_head,
170 STAILQ_INSERT_TAIL(devstat_head,
172 printf("devstat_add_entry: HELP! "
173 "sorting problem detected "
174 "for name %p unit %d\n",
175 dev_name, unit_number);
182 ds->device_number = devstat_current_devnumber++;
183 ds->unit_number = unit_number;
184 strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
185 ds->block_size = block_size;
187 ds->device_type = device_type;
188 ds->priority = priority;
189 binuptime(&ds->creation_time);
190 devstat_generation++;
194 * Remove a devstat structure from the list of devices.
197 devstat_remove_entry(struct devstat *ds)
199 struct devstatlist *devstat_head;
201 mtx_assert(&devstat_mutex, MA_NOTOWNED);
205 mtx_lock(&devstat_mutex);
207 devstat_head = &device_statq;
209 /* Remove this entry from the devstat queue */
210 atomic_add_acq_int(&ds->sequence1, 1);
211 if (ds->unit_number != -1) {
213 STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
216 devstat_generation++;
217 mtx_unlock(&devstat_mutex);
221 * Record a transaction start.
223 * See comments for devstat_end_transaction(). Ordering is very important
227 devstat_start_transaction(struct devstat *ds, struct bintime *now)
230 mtx_assert(&devstat_mutex, MA_NOTOWNED);
236 atomic_add_acq_int(&ds->sequence1, 1);
238 * We only want to set the start time when we are going from idle
239 * to busy. The start time is really the start of the latest busy
242 if (ds->start_count == ds->end_count) {
244 ds->busy_from = *now;
246 binuptime(&ds->busy_from);
249 atomic_add_rel_int(&ds->sequence0, 1);
250 DTRACE_DEVSTAT_START();
254 devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
257 mtx_assert(&devstat_mutex, MA_NOTOWNED);
263 binuptime(&bp->bio_t0);
264 devstat_start_transaction(ds, &bp->bio_t0);
265 DTRACE_DEVSTAT_BIO_START();
269 * Record the ending of a transaction, and incrment the various counters.
271 * Ordering in this function, and in devstat_start_transaction() is VERY
272 * important. The idea here is to run without locks, so we are very
273 * careful to only modify some fields on the way "down" (i.e. at
274 * transaction start) and some fields on the way "up" (i.e. at transaction
275 * completion). One exception is busy_from, which we only modify in
276 * devstat_start_transaction() when there are no outstanding transactions,
277 * and thus it can't be modified in devstat_end_transaction()
280 * The sequence0 and sequence1 fields are provided to enable an application
281 * spying on the structures with mmap(2) to tell when a structure is in a
282 * consistent state or not.
284 * For this to work 100% reliably, it is important that the two fields
285 * are at opposite ends of the structure and that they are incremented
286 * in the opposite order of how a memcpy(3) in userland would copy them.
287 * We assume that the copying happens front to back, but there is actually
288 * no way short of writing your own memcpy(3) replacement to guarantee
289 * this will be the case.
291 * In addition to this, being a kind of locks, they must be updated with
292 * atomic instructions using appropriate memory barriers.
295 devstat_end_transaction(struct devstat *ds, uint32_t bytes,
296 devstat_tag_type tag_type, devstat_trans_flags flags,
297 struct bintime *now, struct bintime *then)
299 struct bintime dt, lnow;
310 atomic_add_acq_int(&ds->sequence1, 1);
311 /* Update byte and operations counts */
312 ds->bytes[flags] += bytes;
313 ds->operations[flags]++;
316 * Keep a count of the various tag types sent.
318 if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
319 tag_type != DEVSTAT_TAG_NONE)
320 ds->tag_types[tag_type]++;
323 /* Update duration of operations */
325 bintime_sub(&dt, then);
326 bintime_add(&ds->duration[flags], &dt);
329 /* Accumulate busy time */
331 bintime_sub(&dt, &ds->busy_from);
332 bintime_add(&ds->busy_time, &dt);
333 ds->busy_from = *now;
336 atomic_add_rel_int(&ds->sequence0, 1);
337 DTRACE_DEVSTAT_DONE();
341 devstat_end_transaction_bio(struct devstat *ds, struct bio *bp)
344 devstat_end_transaction_bio_bt(ds, bp, NULL);
348 devstat_end_transaction_bio_bt(struct devstat *ds, struct bio *bp,
351 devstat_trans_flags flg;
357 if (bp->bio_cmd == BIO_DELETE)
359 else if (bp->bio_cmd == BIO_READ)
361 else if (bp->bio_cmd == BIO_WRITE)
364 flg = DEVSTAT_NO_DATA;
366 devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
367 DEVSTAT_TAG_SIMPLE, flg, now, &bp->bio_t0);
368 DTRACE_DEVSTAT_BIO_DONE();
372 * This is the sysctl handler for the devstat package. The data pushed out
373 * on the kern.devstat.all sysctl variable consists of the current devstat
374 * generation number, and then an array of devstat structures, one for each
375 * device in the system.
377 * This is more cryptic that obvious, but basically we neither can nor
378 * want to hold the devstat_mutex for any amount of time, so we grab it
379 * only when we need to and keep an eye on devstat_generation all the time.
382 sysctl_devstat(SYSCTL_HANDLER_ARGS)
388 mtx_assert(&devstat_mutex, MA_NOTOWNED);
391 * XXX devstat_generation should really be "volatile" but that
392 * XXX freaks out the sysctl macro below. The places where we
393 * XXX change it and inspect it are bracketed in the mutex which
394 * XXX guarantees us proper write barriers. I don't belive the
395 * XXX compiler is allowed to optimize mygen away across calls
396 * XXX to other functions, so the following is belived to be safe.
398 mygen = devstat_generation;
400 error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
402 if (devstat_num_devs == 0)
408 mtx_lock(&devstat_mutex);
409 nds = STAILQ_FIRST(&device_statq);
410 if (mygen != devstat_generation)
412 mtx_unlock(&devstat_mutex);
417 for (;nds != NULL;) {
418 error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
421 mtx_lock(&devstat_mutex);
422 if (mygen != devstat_generation)
425 nds = STAILQ_NEXT(nds, dev_links);
426 mtx_unlock(&devstat_mutex);
434 * Sysctl entries for devstat. The first one is a node that all the rest
437 static SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, NULL,
438 "Device Statistics");
440 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
441 NULL, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list");
443 * Export the number of devices in the system so that userland utilities
444 * can determine how much memory to allocate to hold all the devices.
446 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
447 &devstat_num_devs, 0, "Number of devices in the devstat list");
448 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
449 &devstat_generation, 0, "Devstat list generation");
450 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
451 &devstat_version, 0, "Devstat list version number");
454 * Allocator for struct devstat structures. We sub-allocate these from pages
455 * which we get from malloc. These pages are exported for mmap(2)'ing through
456 * a miniature device driver
459 #define statsperpage (PAGE_SIZE / sizeof(struct devstat))
461 static d_mmap_t devstat_mmap;
463 static struct cdevsw devstat_cdevsw = {
464 .d_version = D_VERSION,
465 .d_mmap = devstat_mmap,
470 TAILQ_ENTRY(statspage) list;
471 struct devstat *stat;
475 static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
476 static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
479 devstat_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
480 int nprot, vm_memattr_t *memattr)
482 struct statspage *spp;
484 if (nprot != VM_PROT_READ)
486 mtx_lock(&devstat_mutex);
487 TAILQ_FOREACH(spp, &pagelist, list) {
489 *paddr = vtophys(spp->stat);
490 mtx_unlock(&devstat_mutex);
495 mtx_unlock(&devstat_mutex);
499 static struct devstat *
503 struct statspage *spp, *spp2;
507 mtx_assert(&devstat_mutex, MA_NOTOWNED);
509 make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME,
510 &devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0444,
511 DEVSTAT_DEVICE_NAME);
515 mtx_lock(&devstat_mutex);
517 TAILQ_FOREACH(spp, &pagelist, list) {
523 mtx_unlock(&devstat_mutex);
524 spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
525 spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
526 spp2->nfree = statsperpage;
529 * If free statspages were added while the lock was released
532 mtx_lock(&devstat_mutex);
533 TAILQ_FOREACH(spp, &pagelist, list)
540 * It would make more sense to add the new page at the
541 * head but the order on the list determine the
542 * sequence of the mapping so we can't do that.
544 TAILQ_INSERT_TAIL(&pagelist, spp, list);
549 for (u = 0; u < statsperpage; u++) {
550 if (dsp->allocated == 0)
556 mtx_unlock(&devstat_mutex);
557 if (spp2 != NULL && spp2 != spp) {
558 free(spp2->stat, M_DEVSTAT);
559 free(spp2, M_DEVSTAT);
565 devstat_free(struct devstat *dsp)
567 struct statspage *spp;
569 mtx_assert(&devstat_mutex, MA_OWNED);
570 bzero(dsp, sizeof *dsp);
571 TAILQ_FOREACH(spp, &pagelist, list) {
572 if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
579 SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD,
580 SYSCTL_NULL_INT_PTR, sizeof(struct devstat), "sizeof(struct devstat)");