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 <sys/param.h>
33 #include <sys/kernel.h>
34 #include <sys/systm.h>
36 #include <sys/devicestat.h>
37 #include <sys/sysctl.h>
38 #include <sys/malloc.h>
40 #include <sys/mutex.h>
45 #include <machine/atomic.h>
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 MTX_SYSINIT(devstat_mutex, &devstat_mutex, "devstat", MTX_DEF);
54 static struct devstatlist device_statq = STAILQ_HEAD_INITIALIZER(device_statq);
55 static struct devstat *devstat_alloc(void);
56 static void devstat_free(struct devstat *);
57 static void devstat_add_entry(struct devstat *ds, const void *dev_name,
58 int unit_number, uint32_t block_size,
59 devstat_support_flags flags,
60 devstat_type_flags device_type,
61 devstat_priority priority);
64 * Allocate a devstat and initialize it
67 devstat_new_entry(const void *dev_name,
68 int unit_number, uint32_t block_size,
69 devstat_support_flags flags,
70 devstat_type_flags device_type,
71 devstat_priority priority)
75 mtx_assert(&devstat_mutex, MA_NOTOWNED);
78 mtx_lock(&devstat_mutex);
79 if (unit_number == -1) {
81 binuptime(&ds->creation_time);
84 devstat_add_entry(ds, dev_name, unit_number, block_size,
85 flags, device_type, priority);
87 mtx_unlock(&devstat_mutex);
92 * Take a malloced and zeroed devstat structure given to us, fill it in
93 * and add it to the queue of devices.
96 devstat_add_entry(struct devstat *ds, const void *dev_name,
97 int unit_number, uint32_t block_size,
98 devstat_support_flags flags,
99 devstat_type_flags device_type,
100 devstat_priority priority)
102 struct devstatlist *devstat_head;
103 struct devstat *ds_tmp;
105 mtx_assert(&devstat_mutex, MA_OWNED);
108 devstat_head = &device_statq;
111 * Priority sort. Each driver passes in its priority when it adds
112 * its devstat entry. Drivers are sorted first by priority, and
113 * then by probe order.
115 * For the first device, we just insert it, since the priority
116 * doesn't really matter yet. Subsequent devices are inserted into
117 * the list using the order outlined above.
119 if (devstat_num_devs == 1)
120 STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
122 STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
123 struct devstat *ds_next;
125 ds_next = STAILQ_NEXT(ds_tmp, dev_links);
128 * If we find a break between higher and lower
129 * priority items, and if this item fits in the
130 * break, insert it. This also applies if the
131 * "lower priority item" is the end of the list.
133 if ((priority <= ds_tmp->priority)
134 && ((ds_next == NULL)
135 || (priority > ds_next->priority))) {
136 STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
139 } else if (priority > ds_tmp->priority) {
141 * If this is the case, we should be able
142 * to insert ourselves at the head of the
143 * list. If we can't, something is wrong.
145 if (ds_tmp == STAILQ_FIRST(devstat_head)) {
146 STAILQ_INSERT_HEAD(devstat_head,
150 STAILQ_INSERT_TAIL(devstat_head,
152 printf("devstat_add_entry: HELP! "
153 "sorting problem detected "
154 "for name %p unit %d\n",
155 dev_name, unit_number);
162 ds->device_number = devstat_current_devnumber++;
163 ds->unit_number = unit_number;
164 strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
165 ds->block_size = block_size;
167 ds->device_type = device_type;
168 ds->priority = priority;
169 binuptime(&ds->creation_time);
170 devstat_generation++;
174 * Remove a devstat structure from the list of devices.
177 devstat_remove_entry(struct devstat *ds)
179 struct devstatlist *devstat_head;
181 mtx_assert(&devstat_mutex, MA_NOTOWNED);
185 mtx_lock(&devstat_mutex);
187 devstat_head = &device_statq;
189 /* Remove this entry from the devstat queue */
190 atomic_add_acq_int(&ds->sequence1, 1);
191 if (ds->id == NULL) {
193 STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
196 devstat_generation++;
197 mtx_unlock(&devstat_mutex);
201 * Record a transaction start.
203 * See comments for devstat_end_transaction(). Ordering is very important
207 devstat_start_transaction(struct devstat *ds, struct bintime *now)
210 mtx_assert(&devstat_mutex, MA_NOTOWNED);
216 atomic_add_acq_int(&ds->sequence1, 1);
218 * We only want to set the start time when we are going from idle
219 * to busy. The start time is really the start of the latest busy
222 if (ds->start_count == ds->end_count) {
224 ds->busy_from = *now;
226 binuptime(&ds->busy_from);
229 atomic_add_rel_int(&ds->sequence0, 1);
233 devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
236 mtx_assert(&devstat_mutex, MA_NOTOWNED);
242 binuptime(&bp->bio_t0);
243 devstat_start_transaction(ds, &bp->bio_t0);
247 * Record the ending of a transaction, and incrment the various counters.
249 * Ordering in this function, and in devstat_start_transaction() is VERY
250 * important. The idea here is to run without locks, so we are very
251 * careful to only modify some fields on the way "down" (i.e. at
252 * transaction start) and some fields on the way "up" (i.e. at transaction
253 * completion). One exception is busy_from, which we only modify in
254 * devstat_start_transaction() when there are no outstanding transactions,
255 * and thus it can't be modified in devstat_end_transaction()
258 * The sequence0 and sequence1 fields are provided to enable an application
259 * spying on the structures with mmap(2) to tell when a structure is in a
260 * consistent state or not.
262 * For this to work 100% reliably, it is important that the two fields
263 * are at opposite ends of the structure and that they are incremented
264 * in the opposite order of how a memcpy(3) in userland would copy them.
265 * We assume that the copying happens front to back, but there is actually
266 * no way short of writing your own memcpy(3) replacement to guarantee
267 * this will be the case.
269 * In addition to this, being a kind of locks, they must be updated with
270 * atomic instructions using appropriate memory barriers.
273 devstat_end_transaction(struct devstat *ds, uint32_t bytes,
274 devstat_tag_type tag_type, devstat_trans_flags flags,
275 struct bintime *now, struct bintime *then)
277 struct bintime dt, lnow;
288 atomic_add_acq_int(&ds->sequence1, 1);
289 /* Update byte and operations counts */
290 ds->bytes[flags] += bytes;
291 ds->operations[flags]++;
294 * Keep a count of the various tag types sent.
296 if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
297 tag_type != DEVSTAT_TAG_NONE)
298 ds->tag_types[tag_type]++;
301 /* Update duration of operations */
303 bintime_sub(&dt, then);
304 bintime_add(&ds->duration[flags], &dt);
307 /* Accumulate busy time */
309 bintime_sub(&dt, &ds->busy_from);
310 bintime_add(&ds->busy_time, &dt);
311 ds->busy_from = *now;
314 atomic_add_rel_int(&ds->sequence0, 1);
318 devstat_end_transaction_bio(struct devstat *ds, struct bio *bp)
320 devstat_trans_flags flg;
326 if (bp->bio_cmd == BIO_DELETE)
328 else if (bp->bio_cmd == BIO_READ)
330 else if (bp->bio_cmd == BIO_WRITE)
333 flg = DEVSTAT_NO_DATA;
335 devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
336 DEVSTAT_TAG_SIMPLE, flg, NULL, &bp->bio_t0);
340 * This is the sysctl handler for the devstat package. The data pushed out
341 * on the kern.devstat.all sysctl variable consists of the current devstat
342 * generation number, and then an array of devstat structures, one for each
343 * device in the system.
345 * This is more cryptic that obvious, but basically we neither can nor
346 * want to hold the devstat_mutex for any amount of time, so we grab it
347 * only when we need to and keep an eye on devstat_generation all the time.
350 sysctl_devstat(SYSCTL_HANDLER_ARGS)
356 mtx_assert(&devstat_mutex, MA_NOTOWNED);
359 * XXX devstat_generation should really be "volatile" but that
360 * XXX freaks out the sysctl macro below. The places where we
361 * XXX change it and inspect it are bracketed in the mutex which
362 * XXX guarantees us proper write barriers. I don't belive the
363 * XXX compiler is allowed to optimize mygen away across calls
364 * XXX to other functions, so the following is belived to be safe.
366 mygen = devstat_generation;
368 error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
370 if (devstat_num_devs == 0)
376 mtx_lock(&devstat_mutex);
377 nds = STAILQ_FIRST(&device_statq);
378 if (mygen != devstat_generation)
380 mtx_unlock(&devstat_mutex);
385 for (;nds != NULL;) {
386 error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
389 mtx_lock(&devstat_mutex);
390 if (mygen != devstat_generation)
393 nds = STAILQ_NEXT(nds, dev_links);
394 mtx_unlock(&devstat_mutex);
402 * Sysctl entries for devstat. The first one is a node that all the rest
405 static SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, NULL,
406 "Device Statistics");
408 SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
409 NULL, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list");
411 * Export the number of devices in the system so that userland utilities
412 * can determine how much memory to allocate to hold all the devices.
414 SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
415 &devstat_num_devs, 0, "Number of devices in the devstat list");
416 SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
417 &devstat_generation, 0, "Devstat list generation");
418 SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
419 &devstat_version, 0, "Devstat list version number");
422 * Allocator for struct devstat structures. We sub-allocate these from pages
423 * which we get from malloc. These pages are exported for mmap(2)'ing through
424 * a miniature device driver
427 #define statsperpage (PAGE_SIZE / sizeof(struct devstat))
429 static d_mmap_t devstat_mmap;
431 static struct cdevsw devstat_cdevsw = {
432 .d_version = D_VERSION,
433 .d_flags = D_NEEDGIANT,
434 .d_mmap = devstat_mmap,
439 TAILQ_ENTRY(statspage) list;
440 struct devstat *stat;
444 static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
445 static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
448 devstat_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
449 int nprot, vm_memattr_t *memattr)
451 struct statspage *spp;
453 if (nprot != VM_PROT_READ)
455 TAILQ_FOREACH(spp, &pagelist, list) {
457 *paddr = vtophys(spp->stat);
465 static struct devstat *
469 struct statspage *spp, *spp2;
473 mtx_assert(&devstat_mutex, MA_NOTOWNED);
475 make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME,
476 &devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0400,
477 DEVSTAT_DEVICE_NAME);
481 mtx_lock(&devstat_mutex);
483 TAILQ_FOREACH(spp, &pagelist, list) {
489 mtx_unlock(&devstat_mutex);
490 spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
491 spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
492 spp2->nfree = statsperpage;
495 * If free statspages were added while the lock was released
498 mtx_lock(&devstat_mutex);
499 TAILQ_FOREACH(spp, &pagelist, list)
506 * It would make more sense to add the new page at the
507 * head but the order on the list determine the
508 * sequence of the mapping so we can't do that.
510 TAILQ_INSERT_TAIL(&pagelist, spp, list);
515 for (u = 0; u < statsperpage; u++) {
516 if (dsp->allocated == 0)
522 mtx_unlock(&devstat_mutex);
523 if (spp2 != NULL && spp2 != spp) {
524 free(spp2->stat, M_DEVSTAT);
525 free(spp2, M_DEVSTAT);
531 devstat_free(struct devstat *dsp)
533 struct statspage *spp;
535 mtx_assert(&devstat_mutex, MA_OWNED);
536 bzero(dsp, sizeof *dsp);
537 TAILQ_FOREACH(spp, &pagelist, list) {
538 if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
545 SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD,
546 NULL, sizeof(struct devstat), "sizeof(struct devstat)");