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;
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);
63 * Allocate a devstat and initialize it
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)
76 STAILQ_INIT(&device_statq);
77 mtx_init(&devstat_mutex, "devstat", NULL, MTX_DEF);
80 mtx_assert(&devstat_mutex, MA_NOTOWNED);
83 mtx_lock(&devstat_mutex);
84 if (unit_number == -1) {
86 binuptime(&ds->creation_time);
89 devstat_add_entry(ds, dev_name, unit_number, block_size,
90 flags, device_type, priority);
92 mtx_unlock(&devstat_mutex);
97 * Take a malloced and zeroed devstat structure given to us, fill it in
98 * and add it to the queue of devices.
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)
107 struct devstatlist *devstat_head;
108 struct devstat *ds_tmp;
110 mtx_assert(&devstat_mutex, MA_OWNED);
113 devstat_head = &device_statq;
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.
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.
124 if (devstat_num_devs == 1)
125 STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
127 STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
128 struct devstat *ds_next;
130 ds_next = STAILQ_NEXT(ds_tmp, dev_links);
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.
138 if ((priority <= ds_tmp->priority)
139 && ((ds_next == NULL)
140 || (priority > ds_next->priority))) {
141 STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
144 } else if (priority > ds_tmp->priority) {
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.
150 if (ds_tmp == STAILQ_FIRST(devstat_head)) {
151 STAILQ_INSERT_HEAD(devstat_head,
155 STAILQ_INSERT_TAIL(devstat_head,
157 printf("devstat_add_entry: HELP! "
158 "sorting problem detected "
159 "for name %p unit %d\n",
160 dev_name, unit_number);
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;
172 ds->device_type = device_type;
173 ds->priority = priority;
174 binuptime(&ds->creation_time);
175 devstat_generation++;
179 * Remove a devstat structure from the list of devices.
182 devstat_remove_entry(struct devstat *ds)
184 struct devstatlist *devstat_head;
186 mtx_assert(&devstat_mutex, MA_NOTOWNED);
190 mtx_lock(&devstat_mutex);
192 devstat_head = &device_statq;
194 /* Remove this entry from the devstat queue */
195 atomic_add_acq_int(&ds->sequence1, 1);
196 if (ds->id == NULL) {
198 STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
201 devstat_generation++;
202 mtx_unlock(&devstat_mutex);
206 * Record a transaction start.
208 * See comments for devstat_end_transaction(). Ordering is very important
212 devstat_start_transaction(struct devstat *ds, struct bintime *now)
215 mtx_assert(&devstat_mutex, MA_NOTOWNED);
221 atomic_add_acq_int(&ds->sequence1, 1);
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
227 if (ds->start_count == ds->end_count) {
229 ds->busy_from = *now;
231 binuptime(&ds->busy_from);
234 atomic_add_rel_int(&ds->sequence0, 1);
238 devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
241 mtx_assert(&devstat_mutex, MA_NOTOWNED);
247 binuptime(&bp->bio_t0);
248 devstat_start_transaction(ds, &bp->bio_t0);
252 * Record the ending of a transaction, and incrment the various counters.
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()
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.
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.
274 * In addition to this, being a kind of locks, they must be updated with
275 * atomic instructions using appropriate memory barriers.
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)
282 struct bintime dt, lnow;
284 mtx_assert(&devstat_mutex, MA_NOTOWNED);
295 atomic_add_acq_int(&ds->sequence1, 1);
296 /* Update byte and operations counts */
297 ds->bytes[flags] += bytes;
298 ds->operations[flags]++;
301 * Keep a count of the various tag types sent.
303 if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
304 tag_type != DEVSTAT_TAG_NONE)
305 ds->tag_types[tag_type]++;
308 /* Update duration of operations */
310 bintime_sub(&dt, then);
311 bintime_add(&ds->duration[flags], &dt);
314 /* Accumulate busy time */
316 bintime_sub(&dt, &ds->busy_from);
317 bintime_add(&ds->busy_time, &dt);
318 ds->busy_from = *now;
321 atomic_add_rel_int(&ds->sequence0, 1);
325 devstat_end_transaction_bio(struct devstat *ds, struct bio *bp)
327 devstat_trans_flags flg;
329 mtx_assert(&devstat_mutex, MA_NOTOWNED);
335 if (bp->bio_cmd == BIO_DELETE)
337 else if (bp->bio_cmd == BIO_READ)
339 else if (bp->bio_cmd == BIO_WRITE)
342 flg = DEVSTAT_NO_DATA;
344 devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
345 DEVSTAT_TAG_SIMPLE, flg, NULL, &bp->bio_t0);
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.
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.
359 sysctl_devstat(SYSCTL_HANDLER_ARGS)
365 mtx_assert(&devstat_mutex, MA_NOTOWNED);
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.
375 mygen = devstat_generation;
377 error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
379 if (devstat_num_devs == 0)
385 mtx_lock(&devstat_mutex);
386 nds = STAILQ_FIRST(&device_statq);
387 if (mygen != devstat_generation)
389 mtx_unlock(&devstat_mutex);
394 for (;nds != NULL;) {
395 error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
398 mtx_lock(&devstat_mutex);
399 if (mygen != devstat_generation)
402 nds = STAILQ_NEXT(nds, dev_links);
403 mtx_unlock(&devstat_mutex);
411 * Sysctl entries for devstat. The first one is a node that all the rest
414 SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics");
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");
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.
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");
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
435 #define statsperpage (PAGE_SIZE / sizeof(struct devstat))
437 static d_mmap_t devstat_mmap;
439 static struct cdevsw devstat_cdevsw = {
440 .d_mmap = devstat_mmap,
445 TAILQ_ENTRY(statspage) list;
446 struct devstat *stat;
450 static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
451 static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
454 devstat_mmap(dev_t dev, vm_offset_t offset, vm_paddr_t *paddr, int nprot)
456 struct statspage *spp;
458 if (nprot != VM_PROT_READ)
460 TAILQ_FOREACH(spp, &pagelist, list) {
462 *paddr = vtophys(spp->stat);
470 static struct devstat *
474 struct statspage *spp;
478 mtx_assert(&devstat_mutex, MA_NOTOWNED);
480 make_dev(&devstat_cdevsw, 0,
481 UID_ROOT, GID_WHEEL, 0400, DEVSTAT_DEVICE_NAME);
484 mtx_lock(&devstat_mutex);
486 TAILQ_FOREACH(spp, &pagelist, list) {
493 * We had no free slot in any of our pages, drop the mutex
494 * and get another page. In theory we could have more than
495 * one process doing this at the same time and consequently
496 * we may allocate more pages than we will need. That is
497 * Just Too Bad[tm], we can live with that.
499 mtx_unlock(&devstat_mutex);
500 spp = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
501 spp->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
502 spp->nfree = statsperpage;
503 mtx_lock(&devstat_mutex);
505 * It would make more sense to add the new page at the head
506 * but the order on the list determine the sequence of the
507 * mapping so we can't do that.
509 TAILQ_INSERT_TAIL(&pagelist, spp, list);
512 for (u = 0; u < statsperpage; u++) {
513 if (dsp->allocated == 0)
519 mtx_unlock(&devstat_mutex);
524 devstat_free(struct devstat *dsp)
526 struct statspage *spp;
528 mtx_assert(&devstat_mutex, MA_OWNED);
529 bzero(dsp, sizeof *dsp);
530 TAILQ_FOREACH(spp, &pagelist, list) {
531 if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
538 SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD,
539 0, sizeof(struct devstat), "sizeof(struct devstat)");