19454b2d8SWarner Losh /*-
28a36da99SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a36da99SPedro F. Giffuni *
42a888f93SKenneth D. Merry * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
57a59208dSJustin T. Gibbs * All rights reserved.
67a59208dSJustin T. Gibbs *
77a59208dSJustin T. Gibbs * Redistribution and use in source and binary forms, with or without
87a59208dSJustin T. Gibbs * modification, are permitted provided that the following conditions
97a59208dSJustin T. Gibbs * are met:
107a59208dSJustin T. Gibbs * 1. Redistributions of source code must retain the above copyright
117a59208dSJustin T. Gibbs * notice, this list of conditions and the following disclaimer.
127a59208dSJustin T. Gibbs * 2. Redistributions in binary form must reproduce the above copyright
137a59208dSJustin T. Gibbs * notice, this list of conditions and the following disclaimer in the
147a59208dSJustin T. Gibbs * documentation and/or other materials provided with the distribution.
157a59208dSJustin T. Gibbs * 3. The name of the author may not be used to endorse or promote products
167a59208dSJustin T. Gibbs * derived from this software without specific prior written permission.
177a59208dSJustin T. Gibbs *
187a59208dSJustin T. Gibbs * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
197a59208dSJustin T. Gibbs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
207a59208dSJustin T. Gibbs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
217a59208dSJustin T. Gibbs * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
227a59208dSJustin T. Gibbs * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
237a59208dSJustin T. Gibbs * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
247a59208dSJustin T. Gibbs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
257a59208dSJustin T. Gibbs * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
267a59208dSJustin T. Gibbs * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
277a59208dSJustin T. Gibbs * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
287a59208dSJustin T. Gibbs * SUCH DAMAGE.
297a59208dSJustin T. Gibbs */
307a59208dSJustin T. Gibbs
317a59208dSJustin T. Gibbs #include <sys/param.h>
32*ab63da35SAlan Somers #include <sys/disk.h>
337a59208dSJustin T. Gibbs #include <sys/kernel.h>
347a59208dSJustin T. Gibbs #include <sys/systm.h>
359626b608SPoul-Henning Kamp #include <sys/bio.h>
36224d5539SPoul-Henning Kamp #include <sys/devicestat.h>
372e1ae0b3SMark Johnston #include <sys/sdt.h>
387a59208dSJustin T. Gibbs #include <sys/sysctl.h>
39c7e73d59SPoul-Henning Kamp #include <sys/malloc.h>
40224d5539SPoul-Henning Kamp #include <sys/lock.h>
41224d5539SPoul-Henning Kamp #include <sys/mutex.h>
42c7e73d59SPoul-Henning Kamp #include <sys/conf.h>
43c7e73d59SPoul-Henning Kamp #include <vm/vm.h>
44c7e73d59SPoul-Henning Kamp #include <vm/pmap.h>
457a59208dSJustin T. Gibbs
46224d5539SPoul-Henning Kamp #include <machine/atomic.h>
477a59208dSJustin T. Gibbs
482e1ae0b3SMark Johnston SDT_PROVIDER_DEFINE(io);
493fac94baSGeorge V. Neville-Neil
50d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(io, , , start, "struct bio *", "struct devstat *");
51d9fae5abSAndriy Gapon SDT_PROBE_DEFINE2(io, , , done, "struct bio *", "struct devstat *");
523fac94baSGeorge V. Neville-Neil
532e1ae0b3SMark Johnston #define DTRACE_DEVSTAT_BIO_START() SDT_PROBE2(io, , , start, bp, ds)
542e1ae0b3SMark Johnston #define DTRACE_DEVSTAT_BIO_DONE() SDT_PROBE2(io, , , done, bp, ds)
553fac94baSGeorge V. Neville-Neil
567a59208dSJustin T. Gibbs static int devstat_num_devs;
57037c3d0fSPoul-Henning Kamp static long devstat_generation = 1;
587a59208dSJustin T. Gibbs static int devstat_version = DEVSTAT_VERSION;
597a59208dSJustin T. Gibbs static int devstat_current_devnumber;
60224d5539SPoul-Henning Kamp static struct mtx devstat_mutex;
6127c959cfSJustin T. Gibbs MTX_SYSINIT(devstat_mutex, &devstat_mutex, "devstat", MTX_DEF);
627a59208dSJustin T. Gibbs
6327c959cfSJustin T. Gibbs static struct devstatlist device_statq = STAILQ_HEAD_INITIALIZER(device_statq);
64c7e73d59SPoul-Henning Kamp static struct devstat *devstat_alloc(void);
65c7e73d59SPoul-Henning Kamp static void devstat_free(struct devstat *);
66538aabaaSPoul-Henning Kamp static void devstat_add_entry(struct devstat *ds, const void *dev_name,
6760ae52f7SEd Schouten int unit_number, uint32_t block_size,
68f37de122SPoul-Henning Kamp devstat_support_flags flags,
69f37de122SPoul-Henning Kamp devstat_type_flags device_type,
70f37de122SPoul-Henning Kamp devstat_priority priority);
71c7e73d59SPoul-Henning Kamp
72c7e73d59SPoul-Henning Kamp /*
73c7e73d59SPoul-Henning Kamp * Allocate a devstat and initialize it
74c7e73d59SPoul-Henning Kamp */
75c7e73d59SPoul-Henning Kamp struct devstat *
devstat_new_entry(const void * dev_name,int unit_number,uint32_t block_size,devstat_support_flags flags,devstat_type_flags device_type,devstat_priority priority)76538aabaaSPoul-Henning Kamp devstat_new_entry(const void *dev_name,
7760ae52f7SEd Schouten int unit_number, uint32_t block_size,
78c7e73d59SPoul-Henning Kamp devstat_support_flags flags,
79c7e73d59SPoul-Henning Kamp devstat_type_flags device_type,
80c7e73d59SPoul-Henning Kamp devstat_priority priority)
81c7e73d59SPoul-Henning Kamp {
82c7e73d59SPoul-Henning Kamp struct devstat *ds;
83224d5539SPoul-Henning Kamp
84224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_NOTOWNED);
85c7e73d59SPoul-Henning Kamp
86c7e73d59SPoul-Henning Kamp ds = devstat_alloc();
87224d5539SPoul-Henning Kamp mtx_lock(&devstat_mutex);
88c967bee7SPoul-Henning Kamp if (unit_number == -1) {
8940ea77a0SAlexander Motin ds->unit_number = unit_number;
90c967bee7SPoul-Henning Kamp ds->id = dev_name;
91c967bee7SPoul-Henning Kamp binuptime(&ds->creation_time);
92c967bee7SPoul-Henning Kamp devstat_generation++;
93c967bee7SPoul-Henning Kamp } else {
94c7e73d59SPoul-Henning Kamp devstat_add_entry(ds, dev_name, unit_number, block_size,
95c7e73d59SPoul-Henning Kamp flags, device_type, priority);
96c967bee7SPoul-Henning Kamp }
97224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
98c7e73d59SPoul-Henning Kamp return (ds);
99c7e73d59SPoul-Henning Kamp }
1007a59208dSJustin T. Gibbs
1017a59208dSJustin T. Gibbs /*
1027a59208dSJustin T. Gibbs * Take a malloced and zeroed devstat structure given to us, fill it in
1037a59208dSJustin T. Gibbs * and add it to the queue of devices.
1047a59208dSJustin T. Gibbs */
105f37de122SPoul-Henning Kamp static void
devstat_add_entry(struct devstat * ds,const void * dev_name,int unit_number,uint32_t block_size,devstat_support_flags flags,devstat_type_flags device_type,devstat_priority priority)106538aabaaSPoul-Henning Kamp devstat_add_entry(struct devstat *ds, const void *dev_name,
10760ae52f7SEd Schouten int unit_number, uint32_t block_size,
1087a59208dSJustin T. Gibbs devstat_support_flags flags,
1092a888f93SKenneth D. Merry devstat_type_flags device_type,
1102a888f93SKenneth D. Merry devstat_priority priority)
1117a59208dSJustin T. Gibbs {
1127a59208dSJustin T. Gibbs struct devstatlist *devstat_head;
1132a888f93SKenneth D. Merry struct devstat *ds_tmp;
1147a59208dSJustin T. Gibbs
115224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_OWNED);
1167a59208dSJustin T. Gibbs devstat_num_devs++;
1177a59208dSJustin T. Gibbs
1187a59208dSJustin T. Gibbs devstat_head = &device_statq;
1197a59208dSJustin T. Gibbs
1202a888f93SKenneth D. Merry /*
1212a888f93SKenneth D. Merry * Priority sort. Each driver passes in its priority when it adds
1222a888f93SKenneth D. Merry * its devstat entry. Drivers are sorted first by priority, and
1232a888f93SKenneth D. Merry * then by probe order.
1242a888f93SKenneth D. Merry *
1252a888f93SKenneth D. Merry * For the first device, we just insert it, since the priority
1262a888f93SKenneth D. Merry * doesn't really matter yet. Subsequent devices are inserted into
1272a888f93SKenneth D. Merry * the list using the order outlined above.
1282a888f93SKenneth D. Merry */
1292a888f93SKenneth D. Merry if (devstat_num_devs == 1)
1307a59208dSJustin T. Gibbs STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
1312a888f93SKenneth D. Merry else {
13237d40066SPoul-Henning Kamp STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
1332a888f93SKenneth D. Merry struct devstat *ds_next;
1342a888f93SKenneth D. Merry
1352a888f93SKenneth D. Merry ds_next = STAILQ_NEXT(ds_tmp, dev_links);
1362a888f93SKenneth D. Merry
1372a888f93SKenneth D. Merry /*
1382a888f93SKenneth D. Merry * If we find a break between higher and lower
1392a888f93SKenneth D. Merry * priority items, and if this item fits in the
1402a888f93SKenneth D. Merry * break, insert it. This also applies if the
1412a888f93SKenneth D. Merry * "lower priority item" is the end of the list.
1422a888f93SKenneth D. Merry */
1432a888f93SKenneth D. Merry if ((priority <= ds_tmp->priority)
1442a888f93SKenneth D. Merry && ((ds_next == NULL)
1452a888f93SKenneth D. Merry || (priority > ds_next->priority))) {
1462a888f93SKenneth D. Merry STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
1472a888f93SKenneth D. Merry dev_links);
1482a888f93SKenneth D. Merry break;
1492a888f93SKenneth D. Merry } else if (priority > ds_tmp->priority) {
1502a888f93SKenneth D. Merry /*
1512a888f93SKenneth D. Merry * If this is the case, we should be able
1522a888f93SKenneth D. Merry * to insert ourselves at the head of the
1532a888f93SKenneth D. Merry * list. If we can't, something is wrong.
1542a888f93SKenneth D. Merry */
1552a888f93SKenneth D. Merry if (ds_tmp == STAILQ_FIRST(devstat_head)) {
1562a888f93SKenneth D. Merry STAILQ_INSERT_HEAD(devstat_head,
1572a888f93SKenneth D. Merry ds, dev_links);
1582a888f93SKenneth D. Merry break;
1592a888f93SKenneth D. Merry } else {
1602a888f93SKenneth D. Merry STAILQ_INSERT_TAIL(devstat_head,
1612a888f93SKenneth D. Merry ds, dev_links);
1622a888f93SKenneth D. Merry printf("devstat_add_entry: HELP! "
1632a888f93SKenneth D. Merry "sorting problem detected "
164538aabaaSPoul-Henning Kamp "for name %p unit %d\n",
165538aabaaSPoul-Henning Kamp dev_name, unit_number);
1662a888f93SKenneth D. Merry break;
1672a888f93SKenneth D. Merry }
1682a888f93SKenneth D. Merry }
1692a888f93SKenneth D. Merry }
1702a888f93SKenneth D. Merry }
1717a59208dSJustin T. Gibbs
1727a59208dSJustin T. Gibbs ds->device_number = devstat_current_devnumber++;
1737a59208dSJustin T. Gibbs ds->unit_number = unit_number;
174e80fb434SRobert Drehmel strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
1757a59208dSJustin T. Gibbs ds->block_size = block_size;
1767a59208dSJustin T. Gibbs ds->flags = flags;
1777a59208dSJustin T. Gibbs ds->device_type = device_type;
1782a888f93SKenneth D. Merry ds->priority = priority;
1797194d335SPoul-Henning Kamp binuptime(&ds->creation_time);
180224d5539SPoul-Henning Kamp devstat_generation++;
1817a59208dSJustin T. Gibbs }
1827a59208dSJustin T. Gibbs
1837a59208dSJustin T. Gibbs /*
1847a59208dSJustin T. Gibbs * Remove a devstat structure from the list of devices.
1857a59208dSJustin T. Gibbs */
1867a59208dSJustin T. Gibbs void
devstat_remove_entry(struct devstat * ds)1877a59208dSJustin T. Gibbs devstat_remove_entry(struct devstat *ds)
1887a59208dSJustin T. Gibbs {
1897a59208dSJustin T. Gibbs struct devstatlist *devstat_head;
1907a59208dSJustin T. Gibbs
191224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_NOTOWNED);
1927a59208dSJustin T. Gibbs if (ds == NULL)
1937a59208dSJustin T. Gibbs return;
1947a59208dSJustin T. Gibbs
195224d5539SPoul-Henning Kamp mtx_lock(&devstat_mutex);
1967a59208dSJustin T. Gibbs
1977a59208dSJustin T. Gibbs devstat_head = &device_statq;
1987a59208dSJustin T. Gibbs
1997a59208dSJustin T. Gibbs /* Remove this entry from the devstat queue */
200224d5539SPoul-Henning Kamp atomic_add_acq_int(&ds->sequence1, 1);
20140ea77a0SAlexander Motin if (ds->unit_number != -1) {
202224d5539SPoul-Henning Kamp devstat_num_devs--;
203e3975643SJake Burkholder STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
204c967bee7SPoul-Henning Kamp }
205c7e73d59SPoul-Henning Kamp devstat_free(ds);
206224d5539SPoul-Henning Kamp devstat_generation++;
207224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
2087a59208dSJustin T. Gibbs }
2097a59208dSJustin T. Gibbs
2107a59208dSJustin T. Gibbs /*
2117a59208dSJustin T. Gibbs * Record a transaction start.
2127194d335SPoul-Henning Kamp *
2137194d335SPoul-Henning Kamp * See comments for devstat_end_transaction(). Ordering is very important
2147194d335SPoul-Henning Kamp * here.
2157a59208dSJustin T. Gibbs */
2167a59208dSJustin T. Gibbs void
devstat_start_transaction(struct devstat * ds,const struct bintime * now)217b6c7d9c3SConrad Meyer devstat_start_transaction(struct devstat *ds, const struct bintime *now)
2187a59208dSJustin T. Gibbs {
219224d5539SPoul-Henning Kamp
2207a59208dSJustin T. Gibbs /* sanity check */
2217a59208dSJustin T. Gibbs if (ds == NULL)
2227a59208dSJustin T. Gibbs return;
2237a59208dSJustin T. Gibbs
224224d5539SPoul-Henning Kamp atomic_add_acq_int(&ds->sequence1, 1);
2257a59208dSJustin T. Gibbs /*
2267a59208dSJustin T. Gibbs * We only want to set the start time when we are going from idle
2277a59208dSJustin T. Gibbs * to busy. The start time is really the start of the latest busy
2287a59208dSJustin T. Gibbs * period.
2297a59208dSJustin T. Gibbs */
230024932aaSAlexander Motin if (atomic_fetchadd_int(&ds->start_count, 1) == ds->end_count) {
2317194d335SPoul-Henning Kamp if (now != NULL)
2327194d335SPoul-Henning Kamp ds->busy_from = *now;
2337194d335SPoul-Henning Kamp else
2347194d335SPoul-Henning Kamp binuptime(&ds->busy_from);
2357194d335SPoul-Henning Kamp }
236224d5539SPoul-Henning Kamp atomic_add_rel_int(&ds->sequence0, 1);
2377194d335SPoul-Henning Kamp }
2387194d335SPoul-Henning Kamp
2397194d335SPoul-Henning Kamp void
devstat_start_transaction_bio(struct devstat * ds,struct bio * bp)2407194d335SPoul-Henning Kamp devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
2417194d335SPoul-Henning Kamp {
2427194d335SPoul-Henning Kamp
243224d5539SPoul-Henning Kamp /* sanity check */
244224d5539SPoul-Henning Kamp if (ds == NULL)
245224d5539SPoul-Henning Kamp return;
246224d5539SPoul-Henning Kamp
2477194d335SPoul-Henning Kamp binuptime(&bp->bio_t0);
2488b220f89SAlexander Motin devstat_start_transaction_bio_t0(ds, bp);
2498b220f89SAlexander Motin }
2508b220f89SAlexander Motin
2518b220f89SAlexander Motin void
devstat_start_transaction_bio_t0(struct devstat * ds,struct bio * bp)2528b220f89SAlexander Motin devstat_start_transaction_bio_t0(struct devstat *ds, struct bio *bp)
2538b220f89SAlexander Motin {
2548b220f89SAlexander Motin
2558b220f89SAlexander Motin /* sanity check */
2568b220f89SAlexander Motin if (ds == NULL)
2578b220f89SAlexander Motin return;
2588b220f89SAlexander Motin
2597194d335SPoul-Henning Kamp devstat_start_transaction(ds, &bp->bio_t0);
2603fac94baSGeorge V. Neville-Neil DTRACE_DEVSTAT_BIO_START();
2617a59208dSJustin T. Gibbs }
2627a59208dSJustin T. Gibbs
2637a59208dSJustin T. Gibbs /*
2647a59208dSJustin T. Gibbs * Record the ending of a transaction, and incrment the various counters.
2657194d335SPoul-Henning Kamp *
2667194d335SPoul-Henning Kamp * Ordering in this function, and in devstat_start_transaction() is VERY
2677194d335SPoul-Henning Kamp * important. The idea here is to run without locks, so we are very
2687194d335SPoul-Henning Kamp * careful to only modify some fields on the way "down" (i.e. at
2697194d335SPoul-Henning Kamp * transaction start) and some fields on the way "up" (i.e. at transaction
2707194d335SPoul-Henning Kamp * completion). One exception is busy_from, which we only modify in
2717194d335SPoul-Henning Kamp * devstat_start_transaction() when there are no outstanding transactions,
2727194d335SPoul-Henning Kamp * and thus it can't be modified in devstat_end_transaction()
2737194d335SPoul-Henning Kamp * simultaneously.
274224d5539SPoul-Henning Kamp *
275224d5539SPoul-Henning Kamp * The sequence0 and sequence1 fields are provided to enable an application
276224d5539SPoul-Henning Kamp * spying on the structures with mmap(2) to tell when a structure is in a
277224d5539SPoul-Henning Kamp * consistent state or not.
278224d5539SPoul-Henning Kamp *
279224d5539SPoul-Henning Kamp * For this to work 100% reliably, it is important that the two fields
280224d5539SPoul-Henning Kamp * are at opposite ends of the structure and that they are incremented
281224d5539SPoul-Henning Kamp * in the opposite order of how a memcpy(3) in userland would copy them.
282224d5539SPoul-Henning Kamp * We assume that the copying happens front to back, but there is actually
283224d5539SPoul-Henning Kamp * no way short of writing your own memcpy(3) replacement to guarantee
284224d5539SPoul-Henning Kamp * this will be the case.
285224d5539SPoul-Henning Kamp *
286224d5539SPoul-Henning Kamp * In addition to this, being a kind of locks, they must be updated with
287224d5539SPoul-Henning Kamp * atomic instructions using appropriate memory barriers.
2887a59208dSJustin T. Gibbs */
2897a59208dSJustin T. Gibbs void
devstat_end_transaction(struct devstat * ds,uint32_t bytes,devstat_tag_type tag_type,devstat_trans_flags flags,const struct bintime * now,const struct bintime * then)29060ae52f7SEd Schouten devstat_end_transaction(struct devstat *ds, uint32_t bytes,
2917194d335SPoul-Henning Kamp devstat_tag_type tag_type, devstat_trans_flags flags,
292b6c7d9c3SConrad Meyer const struct bintime *now, const struct bintime *then)
2937a59208dSJustin T. Gibbs {
2947194d335SPoul-Henning Kamp struct bintime dt, lnow;
2957a59208dSJustin T. Gibbs
2967a59208dSJustin T. Gibbs /* sanity check */
2977a59208dSJustin T. Gibbs if (ds == NULL)
2987a59208dSJustin T. Gibbs return;
2997a59208dSJustin T. Gibbs
3007194d335SPoul-Henning Kamp if (now == NULL) {
301b6c7d9c3SConrad Meyer binuptime(&lnow);
3027194d335SPoul-Henning Kamp now = &lnow;
3037194d335SPoul-Henning Kamp }
3047a59208dSJustin T. Gibbs
305224d5539SPoul-Henning Kamp atomic_add_acq_int(&ds->sequence1, 1);
3067194d335SPoul-Henning Kamp /* Update byte and operations counts */
3077194d335SPoul-Henning Kamp ds->bytes[flags] += bytes;
3087194d335SPoul-Henning Kamp ds->operations[flags]++;
3097a59208dSJustin T. Gibbs
3107a59208dSJustin T. Gibbs /*
3117a59208dSJustin T. Gibbs * Keep a count of the various tag types sent.
3127a59208dSJustin T. Gibbs */
3138db3b947SPoul-Henning Kamp if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
314f80d57eeSPoul-Henning Kamp tag_type != DEVSTAT_TAG_NONE)
3157a59208dSJustin T. Gibbs ds->tag_types[tag_type]++;
3167a59208dSJustin T. Gibbs
3177194d335SPoul-Henning Kamp if (then != NULL) {
3187194d335SPoul-Henning Kamp /* Update duration of operations */
3197194d335SPoul-Henning Kamp dt = *now;
3207194d335SPoul-Henning Kamp bintime_sub(&dt, then);
3217194d335SPoul-Henning Kamp bintime_add(&ds->duration[flags], &dt);
3227194d335SPoul-Henning Kamp }
3237a59208dSJustin T. Gibbs
3247194d335SPoul-Henning Kamp /* Accumulate busy time */
3257194d335SPoul-Henning Kamp dt = *now;
3267194d335SPoul-Henning Kamp bintime_sub(&dt, &ds->busy_from);
3277194d335SPoul-Henning Kamp bintime_add(&ds->busy_time, &dt);
3287194d335SPoul-Henning Kamp ds->busy_from = *now;
3297194d335SPoul-Henning Kamp
3307194d335SPoul-Henning Kamp ds->end_count++;
331224d5539SPoul-Henning Kamp atomic_add_rel_int(&ds->sequence0, 1);
3327a59208dSJustin T. Gibbs }
3337a59208dSJustin T. Gibbs
334f80d57eeSPoul-Henning Kamp void
devstat_end_transaction_bio(struct devstat * ds,const struct bio * bp)335b6c7d9c3SConrad Meyer devstat_end_transaction_bio(struct devstat *ds, const struct bio *bp)
336282ac69eSPoul-Henning Kamp {
337e431d66cSAlexander Motin
338e431d66cSAlexander Motin devstat_end_transaction_bio_bt(ds, bp, NULL);
339e431d66cSAlexander Motin }
340e431d66cSAlexander Motin
341e431d66cSAlexander Motin void
devstat_end_transaction_bio_bt(struct devstat * ds,const struct bio * bp,const struct bintime * now)342b6c7d9c3SConrad Meyer devstat_end_transaction_bio_bt(struct devstat *ds, const struct bio *bp,
343b6c7d9c3SConrad Meyer const struct bintime *now)
344e431d66cSAlexander Motin {
345282ac69eSPoul-Henning Kamp devstat_trans_flags flg;
346cb847b81SAlexander Motin devstat_tag_type tag;
347282ac69eSPoul-Henning Kamp
348224d5539SPoul-Henning Kamp /* sanity check */
349224d5539SPoul-Henning Kamp if (ds == NULL)
350224d5539SPoul-Henning Kamp return;
351224d5539SPoul-Henning Kamp
352cb847b81SAlexander Motin if (bp->bio_flags & BIO_ORDERED)
353cb847b81SAlexander Motin tag = DEVSTAT_TAG_ORDERED;
354cb847b81SAlexander Motin else
355cb847b81SAlexander Motin tag = DEVSTAT_TAG_SIMPLE;
356282ac69eSPoul-Henning Kamp if (bp->bio_cmd == BIO_DELETE)
357282ac69eSPoul-Henning Kamp flg = DEVSTAT_FREE;
3589a6844d5SKenneth D. Merry else if ((bp->bio_cmd == BIO_READ)
3599a6844d5SKenneth D. Merry || ((bp->bio_cmd == BIO_ZONE)
3609a6844d5SKenneth D. Merry && (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)))
361282ac69eSPoul-Henning Kamp flg = DEVSTAT_READ;
362224d5539SPoul-Henning Kamp else if (bp->bio_cmd == BIO_WRITE)
363282ac69eSPoul-Henning Kamp flg = DEVSTAT_WRITE;
364224d5539SPoul-Henning Kamp else
365224d5539SPoul-Henning Kamp flg = DEVSTAT_NO_DATA;
366282ac69eSPoul-Henning Kamp
367282ac69eSPoul-Henning Kamp devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
368cb847b81SAlexander Motin tag, flg, now, &bp->bio_t0);
3693fac94baSGeorge V. Neville-Neil DTRACE_DEVSTAT_BIO_DONE();
370282ac69eSPoul-Henning Kamp }
371282ac69eSPoul-Henning Kamp
3727a59208dSJustin T. Gibbs /*
3737a59208dSJustin T. Gibbs * This is the sysctl handler for the devstat package. The data pushed out
3747a59208dSJustin T. Gibbs * on the kern.devstat.all sysctl variable consists of the current devstat
3757a59208dSJustin T. Gibbs * generation number, and then an array of devstat structures, one for each
3767a59208dSJustin T. Gibbs * device in the system.
3777a59208dSJustin T. Gibbs *
378224d5539SPoul-Henning Kamp * This is more cryptic that obvious, but basically we neither can nor
379224d5539SPoul-Henning Kamp * want to hold the devstat_mutex for any amount of time, so we grab it
380224d5539SPoul-Henning Kamp * only when we need to and keep an eye on devstat_generation all the time.
3817a59208dSJustin T. Gibbs */
3827a59208dSJustin T. Gibbs static int
sysctl_devstat(SYSCTL_HANDLER_ARGS)38382d9ae4eSPoul-Henning Kamp sysctl_devstat(SYSCTL_HANDLER_ARGS)
3847a59208dSJustin T. Gibbs {
385224d5539SPoul-Henning Kamp int error;
3866e17a0d7SHartmut Brandt long mygen;
3877a59208dSJustin T. Gibbs struct devstat *nds;
388224d5539SPoul-Henning Kamp
389224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_NOTOWNED);
3907a59208dSJustin T. Gibbs
3917a59208dSJustin T. Gibbs /*
392224d5539SPoul-Henning Kamp * XXX devstat_generation should really be "volatile" but that
393224d5539SPoul-Henning Kamp * XXX freaks out the sysctl macro below. The places where we
394224d5539SPoul-Henning Kamp * XXX change it and inspect it are bracketed in the mutex which
395e3043798SPedro F. Giffuni * XXX guarantees us proper write barriers. I don't believe the
396224d5539SPoul-Henning Kamp * XXX compiler is allowed to optimize mygen away across calls
397224d5539SPoul-Henning Kamp * XXX to other functions, so the following is belived to be safe.
3987a59208dSJustin T. Gibbs */
399224d5539SPoul-Henning Kamp mygen = devstat_generation;
4007a59208dSJustin T. Gibbs
401224d5539SPoul-Henning Kamp error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
402224d5539SPoul-Henning Kamp
403037c3d0fSPoul-Henning Kamp if (devstat_num_devs == 0)
404037c3d0fSPoul-Henning Kamp return(0);
405037c3d0fSPoul-Henning Kamp
406224d5539SPoul-Henning Kamp if (error != 0)
407224d5539SPoul-Henning Kamp return (error);
408224d5539SPoul-Henning Kamp
409224d5539SPoul-Henning Kamp mtx_lock(&devstat_mutex);
410224d5539SPoul-Henning Kamp nds = STAILQ_FIRST(&device_statq);
411224d5539SPoul-Henning Kamp if (mygen != devstat_generation)
412224d5539SPoul-Henning Kamp error = EBUSY;
413224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
414224d5539SPoul-Henning Kamp
415224d5539SPoul-Henning Kamp if (error != 0)
416224d5539SPoul-Henning Kamp return (error);
417224d5539SPoul-Henning Kamp
418224d5539SPoul-Henning Kamp for (;nds != NULL;) {
4197a59208dSJustin T. Gibbs error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
420224d5539SPoul-Henning Kamp if (error != 0)
421224d5539SPoul-Henning Kamp return (error);
422224d5539SPoul-Henning Kamp mtx_lock(&devstat_mutex);
423224d5539SPoul-Henning Kamp if (mygen != devstat_generation)
424224d5539SPoul-Henning Kamp error = EBUSY;
425224d5539SPoul-Henning Kamp else
426224d5539SPoul-Henning Kamp nds = STAILQ_NEXT(nds, dev_links);
427224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
428224d5539SPoul-Henning Kamp if (error != 0)
429224d5539SPoul-Henning Kamp return (error);
430224d5539SPoul-Henning Kamp }
4317a59208dSJustin T. Gibbs return(error);
4327a59208dSJustin T. Gibbs }
4337a59208dSJustin T. Gibbs
4347a59208dSJustin T. Gibbs /*
4357a59208dSJustin T. Gibbs * Sysctl entries for devstat. The first one is a node that all the rest
4367a59208dSJustin T. Gibbs * hang off of.
4377a59208dSJustin T. Gibbs */
4387029da5cSPawel Biernacki static SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
4396472ac3dSEd Schouten "Device Statistics");
4407a59208dSJustin T. Gibbs
4417029da5cSPawel Biernacki SYSCTL_PROC(_kern_devstat, OID_AUTO, all,
4427029da5cSPawel Biernacki CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0,
4437029da5cSPawel Biernacki sysctl_devstat, "S,devstat",
4447029da5cSPawel Biernacki "All devices in the devstat list");
4457a59208dSJustin T. Gibbs /*
4467a59208dSJustin T. Gibbs * Export the number of devices in the system so that userland utilities
4477a59208dSJustin T. Gibbs * can determine how much memory to allocate to hold all the devices.
4487a59208dSJustin T. Gibbs */
4493d177f46SBill Fumerola SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
4503d177f46SBill Fumerola &devstat_num_devs, 0, "Number of devices in the devstat list");
4516e17a0d7SHartmut Brandt SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
4529701cd40SJohn Baldwin &devstat_generation, 0, "Devstat list generation");
4533d177f46SBill Fumerola SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
4543d177f46SBill Fumerola &devstat_version, 0, "Devstat list version number");
455c7e73d59SPoul-Henning Kamp
456224d5539SPoul-Henning Kamp /*
457224d5539SPoul-Henning Kamp * Allocator for struct devstat structures. We sub-allocate these from pages
458224d5539SPoul-Henning Kamp * which we get from malloc. These pages are exported for mmap(2)'ing through
459224d5539SPoul-Henning Kamp * a miniature device driver
460224d5539SPoul-Henning Kamp */
461224d5539SPoul-Henning Kamp
462c7e73d59SPoul-Henning Kamp #define statsperpage (PAGE_SIZE / sizeof(struct devstat))
463c7e73d59SPoul-Henning Kamp
464*ab63da35SAlan Somers static d_ioctl_t devstat_ioctl;
465c7e73d59SPoul-Henning Kamp static d_mmap_t devstat_mmap;
466c7e73d59SPoul-Henning Kamp
467c7e73d59SPoul-Henning Kamp static struct cdevsw devstat_cdevsw = {
468dc08ffecSPoul-Henning Kamp .d_version = D_VERSION,
469*ab63da35SAlan Somers .d_ioctl = devstat_ioctl,
470c7e73d59SPoul-Henning Kamp .d_mmap = devstat_mmap,
471c7e73d59SPoul-Henning Kamp .d_name = "devstat",
472c7e73d59SPoul-Henning Kamp };
473c7e73d59SPoul-Henning Kamp
474c7e73d59SPoul-Henning Kamp struct statspage {
475c7e73d59SPoul-Henning Kamp TAILQ_ENTRY(statspage) list;
476c7e73d59SPoul-Henning Kamp struct devstat *stat;
477c7e73d59SPoul-Henning Kamp u_int nfree;
478c7e73d59SPoul-Henning Kamp };
479c7e73d59SPoul-Henning Kamp
480*ab63da35SAlan Somers static size_t pagelist_pages = 0;
481c7e73d59SPoul-Henning Kamp static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
482c7e73d59SPoul-Henning Kamp static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
483c7e73d59SPoul-Henning Kamp
484c7e73d59SPoul-Henning Kamp static int
devstat_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)485*ab63da35SAlan Somers devstat_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
486*ab63da35SAlan Somers struct thread *td)
487*ab63da35SAlan Somers {
488*ab63da35SAlan Somers int error = ENOTTY;
489*ab63da35SAlan Somers
490*ab63da35SAlan Somers switch (cmd) {
491*ab63da35SAlan Somers case DIOCGMEDIASIZE:
492*ab63da35SAlan Somers error = 0;
493*ab63da35SAlan Somers *(off_t *)data = pagelist_pages * PAGE_SIZE;
494*ab63da35SAlan Somers break;
495*ab63da35SAlan Somers }
496*ab63da35SAlan Somers
497*ab63da35SAlan Somers return (error);
498*ab63da35SAlan Somers }
499*ab63da35SAlan Somers
500*ab63da35SAlan Somers static int
devstat_mmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)501cfd7baceSRobert Noland devstat_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
502cfd7baceSRobert Noland int nprot, vm_memattr_t *memattr)
503c7e73d59SPoul-Henning Kamp {
504c7e73d59SPoul-Henning Kamp struct statspage *spp;
505c7e73d59SPoul-Henning Kamp
506c7e73d59SPoul-Henning Kamp if (nprot != VM_PROT_READ)
507c7e73d59SPoul-Henning Kamp return (-1);
508b646225aSMaksim Yevmenkin mtx_lock(&devstat_mutex);
509c7e73d59SPoul-Henning Kamp TAILQ_FOREACH(spp, &pagelist, list) {
510c7e73d59SPoul-Henning Kamp if (offset == 0) {
511c7e73d59SPoul-Henning Kamp *paddr = vtophys(spp->stat);
512b646225aSMaksim Yevmenkin mtx_unlock(&devstat_mutex);
513c7e73d59SPoul-Henning Kamp return (0);
514c7e73d59SPoul-Henning Kamp }
515c7e73d59SPoul-Henning Kamp offset -= PAGE_SIZE;
516c7e73d59SPoul-Henning Kamp }
517b646225aSMaksim Yevmenkin mtx_unlock(&devstat_mutex);
518c7e73d59SPoul-Henning Kamp return (-1);
519c7e73d59SPoul-Henning Kamp }
520c7e73d59SPoul-Henning Kamp
521c7e73d59SPoul-Henning Kamp static struct devstat *
devstat_alloc(void)522c7e73d59SPoul-Henning Kamp devstat_alloc(void)
523c7e73d59SPoul-Henning Kamp {
524c7e73d59SPoul-Henning Kamp struct devstat *dsp;
52539df6da8SAttilio Rao struct statspage *spp, *spp2;
526c7e73d59SPoul-Henning Kamp u_int u;
527c7e73d59SPoul-Henning Kamp static int once;
528c7e73d59SPoul-Henning Kamp
529224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_NOTOWNED);
530094efe75SKenneth D. Merry if (!once) {
531094efe75SKenneth D. Merry make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME,
532b646225aSMaksim Yevmenkin &devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0444,
533094efe75SKenneth D. Merry DEVSTAT_DEVICE_NAME);
534094efe75SKenneth D. Merry once = 1;
535c7e73d59SPoul-Henning Kamp }
53639df6da8SAttilio Rao spp2 = NULL;
537224d5539SPoul-Henning Kamp mtx_lock(&devstat_mutex);
538224d5539SPoul-Henning Kamp for (;;) {
539c7e73d59SPoul-Henning Kamp TAILQ_FOREACH(spp, &pagelist, list) {
540c7e73d59SPoul-Henning Kamp if (spp->nfree > 0)
541c7e73d59SPoul-Henning Kamp break;
542c7e73d59SPoul-Henning Kamp }
543224d5539SPoul-Henning Kamp if (spp != NULL)
544224d5539SPoul-Henning Kamp break;
545224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
54639df6da8SAttilio Rao spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
54739df6da8SAttilio Rao spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
54839df6da8SAttilio Rao spp2->nfree = statsperpage;
54939df6da8SAttilio Rao
550224d5539SPoul-Henning Kamp /*
55139df6da8SAttilio Rao * If free statspages were added while the lock was released
55239df6da8SAttilio Rao * just reuse them.
55339df6da8SAttilio Rao */
55439df6da8SAttilio Rao mtx_lock(&devstat_mutex);
55539df6da8SAttilio Rao TAILQ_FOREACH(spp, &pagelist, list)
55639df6da8SAttilio Rao if (spp->nfree > 0)
55739df6da8SAttilio Rao break;
55839df6da8SAttilio Rao if (spp == NULL) {
55939df6da8SAttilio Rao spp = spp2;
56039df6da8SAttilio Rao
56139df6da8SAttilio Rao /*
56239df6da8SAttilio Rao * It would make more sense to add the new page at the
56339df6da8SAttilio Rao * head but the order on the list determine the
56439df6da8SAttilio Rao * sequence of the mapping so we can't do that.
565224d5539SPoul-Henning Kamp */
566*ab63da35SAlan Somers pagelist_pages++;
567224d5539SPoul-Henning Kamp TAILQ_INSERT_TAIL(&pagelist, spp, list);
56839df6da8SAttilio Rao } else
56939df6da8SAttilio Rao break;
570c7e73d59SPoul-Henning Kamp }
571c7e73d59SPoul-Henning Kamp dsp = spp->stat;
572c7e73d59SPoul-Henning Kamp for (u = 0; u < statsperpage; u++) {
573c7e73d59SPoul-Henning Kamp if (dsp->allocated == 0)
574c7e73d59SPoul-Henning Kamp break;
575c7e73d59SPoul-Henning Kamp dsp++;
576c7e73d59SPoul-Henning Kamp }
577c7e73d59SPoul-Henning Kamp spp->nfree--;
578c7e73d59SPoul-Henning Kamp dsp->allocated = 1;
579224d5539SPoul-Henning Kamp mtx_unlock(&devstat_mutex);
58039df6da8SAttilio Rao if (spp2 != NULL && spp2 != spp) {
58139df6da8SAttilio Rao free(spp2->stat, M_DEVSTAT);
58239df6da8SAttilio Rao free(spp2, M_DEVSTAT);
58339df6da8SAttilio Rao }
584c7e73d59SPoul-Henning Kamp return (dsp);
585c7e73d59SPoul-Henning Kamp }
586c7e73d59SPoul-Henning Kamp
587c7e73d59SPoul-Henning Kamp static void
devstat_free(struct devstat * dsp)588c7e73d59SPoul-Henning Kamp devstat_free(struct devstat *dsp)
589c7e73d59SPoul-Henning Kamp {
590c7e73d59SPoul-Henning Kamp struct statspage *spp;
591c7e73d59SPoul-Henning Kamp
592224d5539SPoul-Henning Kamp mtx_assert(&devstat_mutex, MA_OWNED);
593c7e73d59SPoul-Henning Kamp bzero(dsp, sizeof *dsp);
594c7e73d59SPoul-Henning Kamp TAILQ_FOREACH(spp, &pagelist, list) {
595c7e73d59SPoul-Henning Kamp if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
596c7e73d59SPoul-Henning Kamp spp->nfree++;
597c7e73d59SPoul-Henning Kamp return;
598c7e73d59SPoul-Henning Kamp }
599c7e73d59SPoul-Henning Kamp }
600c7e73d59SPoul-Henning Kamp }
6017194d335SPoul-Henning Kamp
6027194d335SPoul-Henning Kamp SYSCTL_INT(_debug_sizeof, OID_AUTO, devstat, CTLFLAG_RD,
603f0188618SHans Petter Selasky SYSCTL_NULL_INT_PTR, sizeof(struct devstat), "sizeof(struct devstat)");
604