1998-09-15 08:16:17 +00:00
|
|
|
/*
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
* Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
|
1998-09-15 08:16:17 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The name of the author may not be used to endorse or promote products
|
|
|
|
* derived from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
1999-08-28 01:08:13 +00:00
|
|
|
* $FreeBSD$
|
1998-09-15 08:16:17 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/kernel.h>
|
|
|
|
#include <sys/systm.h>
|
2000-05-05 09:59:14 +00:00
|
|
|
#include <sys/bio.h>
|
1998-09-15 08:16:17 +00:00
|
|
|
#include <sys/sysctl.h>
|
2003-03-08 19:58:57 +00:00
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/conf.h>
|
|
|
|
#include <vm/vm.h>
|
|
|
|
#include <vm/pmap.h>
|
1998-09-15 08:16:17 +00:00
|
|
|
|
|
|
|
#include <sys/devicestat.h>
|
|
|
|
|
|
|
|
static int devstat_num_devs;
|
1998-09-20 00:11:23 +00:00
|
|
|
static long devstat_generation;
|
1998-09-15 08:16:17 +00:00
|
|
|
static int devstat_version = DEVSTAT_VERSION;
|
|
|
|
static int devstat_current_devnumber;
|
|
|
|
|
2001-08-04 18:02:47 +00:00
|
|
|
static struct devstatlist device_statq;
|
2003-03-08 19:58:57 +00:00
|
|
|
static struct devstat *devstat_alloc(void);
|
|
|
|
static void devstat_free(struct devstat *);
|
2003-03-08 21:46:43 +00:00
|
|
|
static void devstat_add_entry(struct devstat *ds, const char *dev_name,
|
|
|
|
int unit_number, u_int32_t block_size,
|
|
|
|
devstat_support_flags flags,
|
|
|
|
devstat_type_flags device_type,
|
|
|
|
devstat_priority priority);
|
2003-03-08 19:58:57 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Allocate a devstat and initialize it
|
|
|
|
*/
|
|
|
|
struct devstat *
|
|
|
|
devstat_new_entry(const char *dev_name,
|
|
|
|
int unit_number, u_int32_t block_size,
|
|
|
|
devstat_support_flags flags,
|
|
|
|
devstat_type_flags device_type,
|
|
|
|
devstat_priority priority)
|
|
|
|
{
|
|
|
|
struct devstat *ds;
|
|
|
|
|
|
|
|
ds = devstat_alloc();
|
|
|
|
devstat_add_entry(ds, dev_name, unit_number, block_size,
|
|
|
|
flags, device_type, priority);
|
|
|
|
return (ds);
|
|
|
|
}
|
1998-09-15 08:16:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Take a malloced and zeroed devstat structure given to us, fill it in
|
|
|
|
* and add it to the queue of devices.
|
|
|
|
*/
|
2003-03-08 21:46:43 +00:00
|
|
|
static void
|
1998-11-14 21:58:51 +00:00
|
|
|
devstat_add_entry(struct devstat *ds, const char *dev_name,
|
1998-09-15 08:16:17 +00:00
|
|
|
int unit_number, u_int32_t block_size,
|
|
|
|
devstat_support_flags flags,
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
devstat_type_flags device_type,
|
|
|
|
devstat_priority priority)
|
1998-09-15 08:16:17 +00:00
|
|
|
{
|
|
|
|
struct devstatlist *devstat_head;
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
struct devstat *ds_tmp;
|
1998-09-15 08:16:17 +00:00
|
|
|
|
|
|
|
if (ds == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (devstat_num_devs == 0)
|
|
|
|
STAILQ_INIT(&device_statq);
|
|
|
|
|
|
|
|
devstat_generation++;
|
|
|
|
devstat_num_devs++;
|
|
|
|
|
|
|
|
devstat_head = &device_statq;
|
|
|
|
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
/*
|
|
|
|
* Priority sort. Each driver passes in its priority when it adds
|
|
|
|
* its devstat entry. Drivers are sorted first by priority, and
|
|
|
|
* then by probe order.
|
|
|
|
*
|
|
|
|
* For the first device, we just insert it, since the priority
|
|
|
|
* doesn't really matter yet. Subsequent devices are inserted into
|
|
|
|
* the list using the order outlined above.
|
|
|
|
*/
|
|
|
|
if (devstat_num_devs == 1)
|
|
|
|
STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
|
|
|
|
else {
|
2001-02-04 16:08:18 +00:00
|
|
|
STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
struct devstat *ds_next;
|
|
|
|
|
|
|
|
ds_next = STAILQ_NEXT(ds_tmp, dev_links);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we find a break between higher and lower
|
|
|
|
* priority items, and if this item fits in the
|
|
|
|
* break, insert it. This also applies if the
|
|
|
|
* "lower priority item" is the end of the list.
|
|
|
|
*/
|
|
|
|
if ((priority <= ds_tmp->priority)
|
|
|
|
&& ((ds_next == NULL)
|
|
|
|
|| (priority > ds_next->priority))) {
|
|
|
|
STAILQ_INSERT_AFTER(devstat_head, ds_tmp, ds,
|
|
|
|
dev_links);
|
|
|
|
break;
|
|
|
|
} else if (priority > ds_tmp->priority) {
|
|
|
|
/*
|
|
|
|
* If this is the case, we should be able
|
|
|
|
* to insert ourselves at the head of the
|
|
|
|
* list. If we can't, something is wrong.
|
|
|
|
*/
|
|
|
|
if (ds_tmp == STAILQ_FIRST(devstat_head)) {
|
|
|
|
STAILQ_INSERT_HEAD(devstat_head,
|
|
|
|
ds, dev_links);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
STAILQ_INSERT_TAIL(devstat_head,
|
|
|
|
ds, dev_links);
|
|
|
|
printf("devstat_add_entry: HELP! "
|
|
|
|
"sorting problem detected "
|
|
|
|
"for %s%d\n", dev_name,
|
|
|
|
unit_number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1998-09-15 08:16:17 +00:00
|
|
|
|
|
|
|
ds->device_number = devstat_current_devnumber++;
|
|
|
|
ds->unit_number = unit_number;
|
2002-10-17 20:03:38 +00:00
|
|
|
strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
|
1998-09-15 08:16:17 +00:00
|
|
|
ds->block_size = block_size;
|
|
|
|
ds->flags = flags;
|
|
|
|
ds->device_type = device_type;
|
Add a prioritization field to the devstat_add_entry() call so that
peripheral drivers can determine where in the devstat(9) list they are
inserted.
This requires recompilation of libdevstat, systat, vmstat, rpc.rstatd, and
any ports that depend on the devstat code, since the size of the devstat
structure has changed. The devstat version number has been incremented as
well to reflect the change.
This sorts devices in the devstat list in "more interesting" to "less
interesting" order. So, for instance, da devices are now more important
than floppy drives, and so will appear before floppy drives in the default
output from systat, iostat, vmstat, etc.
The order of devices is, for now, kept in a central table in devicestat.h.
If individual drivers were able to make a meaningful decision on what
priority they should be at attach time, we could consider splitting the
priority information out into the various drivers. For now, though, they
have no way of knowing that, so it's easier to put them in an easy to find
table.
Also, move the checkversion() call in vmstat(8) to a more logical place.
Thanks to Bruce and David O'Brien for suggestions, for reviewing this, and
for putting up with the long time it has taken me to commit it. Bruce did
object somewhat to the central priority table (he would rather the
priorities be distributed in each driver), so his objection is duly noted
here.
Reviewed by: bde, obrien
1999-02-10 00:04:13 +00:00
|
|
|
ds->priority = priority;
|
1998-09-15 08:16:17 +00:00
|
|
|
getmicrotime(&ds->dev_creation_time);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove a devstat structure from the list of devices.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
devstat_remove_entry(struct devstat *ds)
|
|
|
|
{
|
|
|
|
struct devstatlist *devstat_head;
|
|
|
|
|
|
|
|
if (ds == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
devstat_generation++;
|
|
|
|
devstat_num_devs--;
|
|
|
|
|
|
|
|
devstat_head = &device_statq;
|
|
|
|
|
|
|
|
/* Remove this entry from the devstat queue */
|
2000-05-26 02:09:24 +00:00
|
|
|
STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
|
2003-03-08 19:58:57 +00:00
|
|
|
if (ds->allocated)
|
|
|
|
devstat_free(ds);
|
1998-09-15 08:16:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Record a transaction start.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
devstat_start_transaction(struct devstat *ds)
|
|
|
|
{
|
|
|
|
/* sanity check */
|
|
|
|
if (ds == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only want to set the start time when we are going from idle
|
|
|
|
* to busy. The start time is really the start of the latest busy
|
|
|
|
* period.
|
|
|
|
*/
|
1999-10-02 07:34:11 +00:00
|
|
|
if (ds->busy_count == 0)
|
1998-09-15 08:16:17 +00:00
|
|
|
getmicrouptime(&ds->start_time);
|
|
|
|
ds->busy_count++;
|
|
|
|
}
|
|
|
|
|
2003-03-15 10:33:32 +00:00
|
|
|
void
|
|
|
|
devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
|
|
|
|
{
|
|
|
|
|
|
|
|
devstat_start_transaction(ds);
|
|
|
|
}
|
|
|
|
|
1998-09-15 08:16:17 +00:00
|
|
|
/*
|
|
|
|
* Record the ending of a transaction, and incrment the various counters.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
devstat_end_transaction(struct devstat *ds, u_int32_t bytes,
|
|
|
|
devstat_tag_type tag_type, devstat_trans_flags flags)
|
|
|
|
{
|
|
|
|
struct timeval busy_time;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (ds == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
getmicrouptime(&ds->last_comp_time);
|
|
|
|
ds->busy_count--;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* There might be some transactions (DEVSTAT_NO_DATA) that don't
|
|
|
|
* transfer any data.
|
|
|
|
*/
|
|
|
|
if (flags == DEVSTAT_READ) {
|
|
|
|
ds->bytes_read += bytes;
|
|
|
|
ds->num_reads++;
|
|
|
|
} else if (flags == DEVSTAT_WRITE) {
|
|
|
|
ds->bytes_written += bytes;
|
|
|
|
ds->num_writes++;
|
1999-09-18 21:28:09 +00:00
|
|
|
} else if (flags == DEVSTAT_FREE) {
|
|
|
|
ds->bytes_freed += bytes;
|
|
|
|
ds->num_frees++;
|
1998-09-15 08:16:17 +00:00
|
|
|
} else
|
|
|
|
ds->num_other++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Keep a count of the various tag types sent.
|
|
|
|
*/
|
1999-09-19 08:28:49 +00:00
|
|
|
if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
|
1999-09-18 21:28:09 +00:00
|
|
|
tag_type != DEVSTAT_TAG_NONE)
|
1998-09-15 08:16:17 +00:00
|
|
|
ds->tag_types[tag_type]++;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We only update the busy time when we go idle. Otherwise, this
|
|
|
|
* calculation would require many more clock cycles.
|
|
|
|
*/
|
|
|
|
if (ds->busy_count == 0) {
|
|
|
|
/* Calculate how long we were busy */
|
|
|
|
busy_time = ds->last_comp_time;
|
|
|
|
timevalsub(&busy_time, &ds->start_time);
|
|
|
|
|
|
|
|
/* Add our busy time to the total busy time. */
|
|
|
|
timevaladd(&ds->busy_time, &busy_time);
|
1998-11-15 23:57:22 +00:00
|
|
|
} else if (ds->busy_count < 0)
|
1998-10-06 04:16:07 +00:00
|
|
|
printf("devstat_end_transaction: HELP!! busy_count "
|
|
|
|
"for %s%d is < 0 (%d)!\n", ds->device_name,
|
|
|
|
ds->unit_number, ds->busy_count);
|
1998-09-15 08:16:17 +00:00
|
|
|
}
|
|
|
|
|
2000-04-02 19:08:05 +00:00
|
|
|
void
|
|
|
|
devstat_end_transaction_bio(struct devstat *ds, struct bio *bp)
|
|
|
|
{
|
|
|
|
devstat_trans_flags flg;
|
|
|
|
|
|
|
|
if (bp->bio_cmd == BIO_DELETE)
|
|
|
|
flg = DEVSTAT_FREE;
|
|
|
|
else if (bp->bio_cmd == BIO_READ)
|
|
|
|
flg = DEVSTAT_READ;
|
|
|
|
else
|
|
|
|
flg = DEVSTAT_WRITE;
|
|
|
|
|
|
|
|
devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
|
2002-02-22 09:26:35 +00:00
|
|
|
DEVSTAT_TAG_SIMPLE, flg);
|
2000-04-02 19:08:05 +00:00
|
|
|
}
|
|
|
|
|
1998-09-15 08:16:17 +00:00
|
|
|
/*
|
|
|
|
* This is the sysctl handler for the devstat package. The data pushed out
|
|
|
|
* on the kern.devstat.all sysctl variable consists of the current devstat
|
|
|
|
* generation number, and then an array of devstat structures, one for each
|
|
|
|
* device in the system.
|
|
|
|
*
|
|
|
|
* I'm really not too fond of this method of doing things, but there really
|
|
|
|
* aren't that many alternatives. We must have some method of making sure
|
|
|
|
* that the generation number the user gets corresponds with the data the
|
|
|
|
* user gets. If the user makes a separate sysctl call to get the
|
|
|
|
* generation, and then a sysctl call to get the device statistics, the
|
|
|
|
* device list could have changed in that brief period of time. By
|
|
|
|
* supplying the generation number along with the statistics output, we can
|
|
|
|
* guarantee that the generation number and the statistics match up.
|
|
|
|
*/
|
|
|
|
static int
|
2000-07-04 11:25:35 +00:00
|
|
|
sysctl_devstat(SYSCTL_HANDLER_ARGS)
|
1998-09-15 08:16:17 +00:00
|
|
|
{
|
|
|
|
int error, i;
|
|
|
|
struct devstat *nds;
|
|
|
|
struct devstatlist *devstat_head;
|
|
|
|
|
|
|
|
if (devstat_num_devs == 0)
|
|
|
|
return(EINVAL);
|
|
|
|
|
|
|
|
error = 0;
|
|
|
|
devstat_head = &device_statq;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* First push out the generation number.
|
|
|
|
*/
|
1998-09-20 00:11:23 +00:00
|
|
|
error = SYSCTL_OUT(req, &devstat_generation, sizeof(long));
|
1998-09-15 08:16:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Now push out all the devices.
|
|
|
|
*/
|
1999-11-16 10:56:05 +00:00
|
|
|
for (i = 0, nds = STAILQ_FIRST(devstat_head);
|
1998-09-15 08:16:17 +00:00
|
|
|
(nds != NULL) && (i < devstat_num_devs) && (error == 0);
|
1999-11-16 10:56:05 +00:00
|
|
|
nds = STAILQ_NEXT(nds, dev_links), i++)
|
1998-09-15 08:16:17 +00:00
|
|
|
error = SYSCTL_OUT(req, nds, sizeof(struct devstat));
|
|
|
|
|
|
|
|
return(error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sysctl entries for devstat. The first one is a node that all the rest
|
|
|
|
* hang off of.
|
|
|
|
*/
|
|
|
|
SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD, 0, "Device Statistics");
|
|
|
|
|
|
|
|
SYSCTL_PROC(_kern_devstat, OID_AUTO, all, CTLFLAG_RD|CTLTYPE_OPAQUE,
|
1999-05-03 23:57:32 +00:00
|
|
|
0, 0, sysctl_devstat, "S,devstat", "All devices in the devstat list");
|
1998-09-15 08:16:17 +00:00
|
|
|
/*
|
|
|
|
* Export the number of devices in the system so that userland utilities
|
|
|
|
* can determine how much memory to allocate to hold all the devices.
|
|
|
|
*/
|
1999-05-03 23:57:32 +00:00
|
|
|
SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
|
|
|
|
&devstat_num_devs, 0, "Number of devices in the devstat list");
|
1998-09-20 00:11:23 +00:00
|
|
|
SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
|
2000-07-05 07:46:41 +00:00
|
|
|
&devstat_generation, 0, "Devstat list generation");
|
1999-05-03 23:57:32 +00:00
|
|
|
SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
|
|
|
|
&devstat_version, 0, "Devstat list version number");
|
2003-03-08 19:58:57 +00:00
|
|
|
|
|
|
|
#define statsperpage (PAGE_SIZE / sizeof(struct devstat))
|
|
|
|
|
|
|
|
static d_mmap_t devstat_mmap;
|
|
|
|
|
|
|
|
static struct cdevsw devstat_cdevsw = {
|
|
|
|
.d_open = nullopen,
|
|
|
|
.d_close = nullclose,
|
|
|
|
.d_mmap = devstat_mmap,
|
|
|
|
.d_name = "devstat",
|
|
|
|
};
|
|
|
|
|
|
|
|
struct statspage {
|
|
|
|
TAILQ_ENTRY(statspage) list;
|
|
|
|
struct devstat *stat;
|
|
|
|
u_int nfree;
|
|
|
|
};
|
|
|
|
|
|
|
|
static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
|
|
|
|
static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
|
|
|
|
|
|
|
|
static int
|
|
|
|
devstat_mmap(dev_t dev, vm_offset_t offset, vm_offset_t *paddr, int nprot)
|
|
|
|
{
|
|
|
|
struct statspage *spp;
|
|
|
|
|
|
|
|
if (nprot != VM_PROT_READ)
|
|
|
|
return (-1);
|
|
|
|
TAILQ_FOREACH(spp, &pagelist, list) {
|
|
|
|
if (offset == 0) {
|
|
|
|
*paddr = vtophys(spp->stat);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
offset -= PAGE_SIZE;
|
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static struct devstat *
|
|
|
|
devstat_alloc(void)
|
|
|
|
{
|
|
|
|
struct devstat *dsp;
|
|
|
|
struct statspage *spp;
|
|
|
|
u_int u;
|
|
|
|
static int once;
|
|
|
|
|
|
|
|
if (!once) {
|
|
|
|
make_dev(&devstat_cdevsw, 0,
|
|
|
|
UID_ROOT, GID_WHEEL, 0400, "devstat");
|
|
|
|
once++;
|
|
|
|
}
|
|
|
|
TAILQ_FOREACH(spp, &pagelist, list) {
|
|
|
|
if (spp->nfree > 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (spp == NULL) {
|
|
|
|
spp = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
|
|
|
|
TAILQ_INSERT_TAIL(&pagelist, spp, list);
|
|
|
|
spp->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
|
|
|
|
spp->nfree = statsperpage;
|
|
|
|
}
|
|
|
|
dsp = spp->stat;
|
|
|
|
for (u = 0; u < statsperpage; u++) {
|
|
|
|
if (dsp->allocated == 0)
|
|
|
|
break;
|
|
|
|
dsp++;
|
|
|
|
}
|
|
|
|
spp->nfree--;
|
|
|
|
dsp->allocated = 1;
|
|
|
|
return (dsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
devstat_free(struct devstat *dsp)
|
|
|
|
{
|
|
|
|
struct statspage *spp;
|
|
|
|
|
|
|
|
bzero(dsp, sizeof *dsp);
|
|
|
|
TAILQ_FOREACH(spp, &pagelist, list) {
|
|
|
|
if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
|
|
|
|
spp->nfree++;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|