#include <sys/param.h>
#include <sys/disk.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/bio.h>
#include <sys/devicestat.h>
#include <sys/sdt.h>
#include <sys/sysctl.h>
#include <sys/malloc.h>
#include <sys/lock.h>
#include <sys/mutex.h>
#include <sys/conf.h>
#include <vm/vm.h>
#include <vm/pmap.h>
#ifdef COMPAT_FREEBSD32
#include <compat/freebsd32/freebsd32.h>
#endif
#include <machine/atomic.h>
SDT_PROVIDER_DEFINE(io);
SDT_PROBE_DEFINE2(io, , , start, "struct bio *", "struct devstat *");
SDT_PROBE_DEFINE2(io, , , done, "struct bio *", "struct devstat *");
#define DTRACE_DEVSTAT_BIO_START() SDT_PROBE2(io, , , start, bp, ds)
#define DTRACE_DEVSTAT_BIO_DONE() SDT_PROBE2(io, , , done, bp, ds)
static int devstat_num_devs;
static long devstat_generation = 1;
static int devstat_version = DEVSTAT_VERSION;
static int devstat_current_devnumber;
static struct mtx devstat_mutex;
MTX_SYSINIT(devstat_mutex, &devstat_mutex, "devstat", MTX_DEF);
static struct devstatlist device_statq = STAILQ_HEAD_INITIALIZER(device_statq);
static struct devstat *devstat_alloc(void);
static void devstat_free(struct devstat *);
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);
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)
{
struct devstat *ds;
mtx_assert(&devstat_mutex, MA_NOTOWNED);
ds = devstat_alloc();
mtx_lock(&devstat_mutex);
if (unit_number == -1) {
ds->unit_number = unit_number;
ds->id = dev_name;
binuptime(&ds->creation_time);
devstat_generation++;
} else {
devstat_add_entry(ds, dev_name, unit_number, block_size,
flags, device_type, priority);
}
mtx_unlock(&devstat_mutex);
return (ds);
}
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)
{
struct devstatlist *devstat_head;
struct devstat *ds_tmp;
mtx_assert(&devstat_mutex, MA_OWNED);
devstat_num_devs++;
devstat_head = &device_statq;
if (devstat_num_devs == 1)
STAILQ_INSERT_TAIL(devstat_head, ds, dev_links);
else {
STAILQ_FOREACH(ds_tmp, devstat_head, dev_links) {
struct devstat *ds_next;
ds_next = STAILQ_NEXT(ds_tmp, dev_links);
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 (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 name %p unit %d\n",
dev_name, unit_number);
break;
}
}
}
}
ds->device_number = devstat_current_devnumber++;
ds->unit_number = unit_number;
strlcpy(ds->device_name, dev_name, DEVSTAT_NAME_LEN);
ds->block_size = block_size;
ds->flags = flags;
ds->device_type = device_type;
ds->priority = priority;
binuptime(&ds->creation_time);
devstat_generation++;
}
void
devstat_remove_entry(struct devstat *ds)
{
struct devstatlist *devstat_head;
mtx_assert(&devstat_mutex, MA_NOTOWNED);
if (ds == NULL)
return;
mtx_lock(&devstat_mutex);
devstat_head = &device_statq;
atomic_add_acq_int(&ds->sequence1, 1);
if (ds->unit_number != -1) {
devstat_num_devs--;
STAILQ_REMOVE(devstat_head, ds, devstat, dev_links);
}
devstat_free(ds);
devstat_generation++;
mtx_unlock(&devstat_mutex);
}
void
devstat_start_transaction(struct devstat *ds, const struct bintime *now)
{
if (ds == NULL)
return;
atomic_add_acq_int(&ds->sequence1, 1);
if (atomic_fetchadd_int(&ds->start_count, 1) == ds->end_count) {
if (now != NULL)
ds->busy_from = *now;
else
binuptime(&ds->busy_from);
}
atomic_add_rel_int(&ds->sequence0, 1);
}
void
devstat_start_transaction_bio(struct devstat *ds, struct bio *bp)
{
if (ds == NULL)
return;
binuptime(&bp->bio_t0);
devstat_start_transaction_bio_t0(ds, bp);
}
void
devstat_start_transaction_bio_t0(struct devstat *ds, struct bio *bp)
{
if (ds == NULL)
return;
devstat_start_transaction(ds, &bp->bio_t0);
DTRACE_DEVSTAT_BIO_START();
}
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)
{
struct bintime dt, lnow;
if (ds == NULL)
return;
if (now == NULL) {
binuptime(&lnow);
now = &lnow;
}
atomic_add_acq_int(&ds->sequence1, 1);
ds->bytes[flags] += bytes;
ds->operations[flags]++;
if ((ds->flags & DEVSTAT_NO_ORDERED_TAGS) == 0 &&
tag_type != DEVSTAT_TAG_NONE)
ds->tag_types[tag_type]++;
if (then != NULL) {
dt = *now;
bintime_sub(&dt, then);
bintime_add(&ds->duration[flags], &dt);
}
dt = *now;
bintime_sub(&dt, &ds->busy_from);
bintime_add(&ds->busy_time, &dt);
ds->busy_from = *now;
ds->end_count++;
atomic_add_rel_int(&ds->sequence0, 1);
}
void
devstat_end_transaction_bio(struct devstat *ds, const struct bio *bp)
{
devstat_end_transaction_bio_bt(ds, bp, NULL);
}
void
devstat_end_transaction_bio_bt(struct devstat *ds, const struct bio *bp,
const struct bintime *now)
{
devstat_trans_flags flg;
devstat_tag_type tag;
if (ds == NULL)
return;
if (bp->bio_flags & BIO_ORDERED)
tag = DEVSTAT_TAG_ORDERED;
else
tag = DEVSTAT_TAG_SIMPLE;
if (bp->bio_cmd == BIO_DELETE)
flg = DEVSTAT_FREE;
else if ((bp->bio_cmd == BIO_READ)
|| ((bp->bio_cmd == BIO_ZONE)
&& (bp->bio_zone.zone_cmd == DISK_ZONE_REPORT_ZONES)))
flg = DEVSTAT_READ;
else if (bp->bio_cmd == BIO_WRITE)
flg = DEVSTAT_WRITE;
else
flg = DEVSTAT_NO_DATA;
devstat_end_transaction(ds, bp->bio_bcount - bp->bio_resid,
tag, flg, now, &bp->bio_t0);
DTRACE_DEVSTAT_BIO_DONE();
}
static int
sysctl_devstat(SYSCTL_HANDLER_ARGS)
{
int error;
long mygen;
struct devstat *nds;
mtx_assert(&devstat_mutex, MA_NOTOWNED);
mygen = devstat_generation;
#ifdef COMPAT_FREEBSD32
if ((req->flags & SCTL_MASK32) != 0) {
int32_t mygen32 = (int32_t)mygen;
error = SYSCTL_OUT(req, &mygen32, sizeof(mygen32));
} else
#endif
error = SYSCTL_OUT(req, &mygen, sizeof(mygen));
if (error != 0)
return (error);
if (devstat_num_devs == 0)
return(0);
mtx_lock(&devstat_mutex);
nds = STAILQ_FIRST(&device_statq);
if (mygen != devstat_generation)
error = EBUSY;
mtx_unlock(&devstat_mutex);
if (error != 0)
return (error);
while (nds != NULL) {
#ifdef COMPAT_FREEBSD32
if ((req->flags & SCTL_MASK32) != 0) {
struct devstat32 ds32;
unsigned int i;
CP(*nds, ds32, sequence0);
CP(*nds, ds32, allocated);
CP(*nds, ds32, start_count);
CP(*nds, ds32, end_count);
BT_CP(*nds, ds32, busy_from);
PTROUT_CP(*nds, ds32, dev_links.stqe_next);
CP(*nds, ds32, device_number);
strcpy(ds32.device_name, nds->device_name);
CP(*nds, ds32, unit_number);
for (i = 0; i < DEVSTAT_N_TRANS_FLAGS; i++) {
FU64_CP(*nds, ds32, bytes[i]);
FU64_CP(*nds, ds32, operations[i]);
BT_CP(*nds, ds32, duration[i]);
}
BT_CP(*nds, ds32, busy_time);
BT_CP(*nds, ds32, creation_time);
CP(*nds, ds32, block_size);
for (i = 0; i < nitems(ds32.tag_types); i++) {
FU64_CP(*nds, ds32, tag_types[i]);
}
CP(*nds, ds32, flags);
CP(*nds, ds32, device_type);
CP(*nds, ds32, priority);
PTROUT_CP(*nds, ds32, id);
CP(*nds, ds32, sequence1);
error = SYSCTL_OUT(req, &ds32, sizeof(ds32));
} else
#endif
error = SYSCTL_OUT(req, nds, sizeof(*nds));
if (error != 0)
return (error);
mtx_lock(&devstat_mutex);
if (mygen != devstat_generation)
error = EBUSY;
else
nds = STAILQ_NEXT(nds, dev_links);
mtx_unlock(&devstat_mutex);
if (error != 0)
return (error);
}
return (error);
}
static SYSCTL_NODE(_kern, OID_AUTO, devstat, CTLFLAG_RD | CTLFLAG_MPSAFE, NULL,
"Device Statistics");
SYSCTL_PROC(_kern_devstat, OID_AUTO, all,
CTLFLAG_RD | CTLTYPE_OPAQUE | CTLFLAG_MPSAFE, NULL, 0,
sysctl_devstat, "S,devstat",
"All devices in the devstat list");
SYSCTL_INT(_kern_devstat, OID_AUTO, numdevs, CTLFLAG_RD,
&devstat_num_devs, 0, "Number of devices in the devstat list");
SYSCTL_LONG(_kern_devstat, OID_AUTO, generation, CTLFLAG_RD,
&devstat_generation, 0, "Devstat list generation");
SYSCTL_INT(_kern_devstat, OID_AUTO, version, CTLFLAG_RD,
&devstat_version, 0, "Devstat list version number");
#define statsperpage (PAGE_SIZE / sizeof(struct devstat))
static d_ioctl_t devstat_ioctl;
static d_mmap_t devstat_mmap;
static struct cdevsw devstat_cdevsw = {
.d_version = D_VERSION,
.d_ioctl = devstat_ioctl,
.d_mmap = devstat_mmap,
.d_name = "devstat",
};
struct statspage {
TAILQ_ENTRY(statspage) list;
struct devstat *stat;
u_int nfree;
};
static size_t pagelist_pages = 0;
static TAILQ_HEAD(, statspage) pagelist = TAILQ_HEAD_INITIALIZER(pagelist);
static MALLOC_DEFINE(M_DEVSTAT, "devstat", "Device statistics");
static int
devstat_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
struct thread *td)
{
int error = ENOTTY;
switch (cmd) {
case DIOCGMEDIASIZE:
error = 0;
*(off_t *)data = pagelist_pages * PAGE_SIZE;
break;
}
return (error);
}
static int
devstat_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
int nprot, vm_memattr_t *memattr)
{
struct statspage *spp;
if (nprot != VM_PROT_READ)
return (-1);
mtx_lock(&devstat_mutex);
TAILQ_FOREACH(spp, &pagelist, list) {
if (offset == 0) {
*paddr = vtophys(spp->stat);
mtx_unlock(&devstat_mutex);
return (0);
}
offset -= PAGE_SIZE;
}
mtx_unlock(&devstat_mutex);
return (-1);
}
static struct devstat *
devstat_alloc(void)
{
struct devstat *dsp;
struct statspage *spp, *spp2;
u_int u;
static int once;
mtx_assert(&devstat_mutex, MA_NOTOWNED);
if (!once) {
make_dev_credf(MAKEDEV_ETERNAL | MAKEDEV_CHECKNAME,
&devstat_cdevsw, 0, NULL, UID_ROOT, GID_WHEEL, 0444,
DEVSTAT_DEVICE_NAME);
once = 1;
}
spp2 = NULL;
mtx_lock(&devstat_mutex);
for (;;) {
TAILQ_FOREACH(spp, &pagelist, list) {
if (spp->nfree > 0)
break;
}
if (spp != NULL)
break;
mtx_unlock(&devstat_mutex);
spp2 = malloc(sizeof *spp, M_DEVSTAT, M_ZERO | M_WAITOK);
spp2->stat = malloc(PAGE_SIZE, M_DEVSTAT, M_ZERO | M_WAITOK);
spp2->nfree = statsperpage;
mtx_lock(&devstat_mutex);
TAILQ_FOREACH(spp, &pagelist, list)
if (spp->nfree > 0)
break;
if (spp == NULL) {
spp = spp2;
pagelist_pages++;
TAILQ_INSERT_TAIL(&pagelist, spp, list);
} else
break;
}
dsp = spp->stat;
for (u = 0; u < statsperpage; u++) {
if (dsp->allocated == 0)
break;
dsp++;
}
spp->nfree--;
dsp->allocated = 1;
mtx_unlock(&devstat_mutex);
if (spp2 != NULL && spp2 != spp) {
free(spp2->stat, M_DEVSTAT);
free(spp2, M_DEVSTAT);
}
return (dsp);
}
static void
devstat_free(struct devstat *dsp)
{
struct statspage *spp;
mtx_assert(&devstat_mutex, MA_OWNED);
bzero(dsp, sizeof *dsp);
TAILQ_FOREACH(spp, &pagelist, list) {
if (dsp >= spp->stat && dsp < (spp->stat + statsperpage)) {
spp->nfree++;
return;
}
}
}
SYSCTL_SIZEOF_STRUCT(devstat);