#define _KERNEL_STRUCTURES_
#include <sys/param.h>
#include <sys/user.h>
#include <sys/buf.h>
#include <sys/bio.h>
#include <sys/queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stddef.h>
#include <fcntl.h>
#include <kvm.h>
#include <nlist.h>
#include <getopt.h>
#include <math.h>
struct nlist Nl[] = {
{ "_bufpcpu" },
{ "_ncpus" },
{ "_nbuf" },
{ NULL }
};
TAILQ_HEAD(bqueues, buf);
enum bufq_type {
BQUEUE_NONE,
BQUEUE_LOCKED,
BQUEUE_CLEAN,
BQUEUE_DIRTY,
BQUEUE_DIRTY_HW,
BQUEUE_EMPTYKVA,
BQUEUE_EMPTY,
BUFFER_QUEUES
};
struct bufpcpu {
struct spinlock spin;
struct bqueues bufqueues[BUFFER_QUEUES];
} __cachealign;
int verboseopt;
static int kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes, int out);
static void scan_queues(kvm_t *kd, int cpu, struct bufpcpu *bqp);
static void loaddelay(struct timespec *ts, const char *arg);
static const char *q2s(int queue);
int qcounter[BUFFER_QUEUES];
int failcount;
int totalcount;
int nbuf;
int
main(int ac, char **av)
{
const char *corefile = NULL;
const char *sysfile = NULL;
struct bufpcpu bpcpu;
struct timespec delay = { 1, 0 };
kvm_t *kd;
int count;
int ncpus;
int ch;
int cpu;
while ((ch = getopt(ac, av, "M:N:v")) != -1) {
switch(ch) {
case 'v':
++verboseopt;
break;
case 'M':
corefile = optarg;
break;
case 'N':
sysfile = optarg;
break;
default:
fprintf(stderr, "%s [-M core] [-N system]\n", av[0]);
exit(1);
}
}
ac -= optind;
av += optind;
if ((kd = kvm_open(sysfile, corefile, NULL, O_RDONLY, "kvm:")) == NULL) {
perror("kvm_open");
exit(1);
}
if (kvm_nlist(kd, Nl) != 0) {
perror("kvm_nlist");
exit(1);
}
if (ac > 0)
loaddelay(&delay, av[0]);
kkread(kd, Nl[1].n_value, &ncpus, sizeof(ncpus), 1);
kkread(kd, Nl[2].n_value, &nbuf, sizeof(nbuf), 1);
for (count = 0; ; ++count) {
for (cpu = 0; cpu < ncpus; cpu++) {
kkread(kd, Nl[0].n_value + cpu * sizeof(struct bufpcpu),
&bpcpu, sizeof(struct bufpcpu), 1);
scan_queues(kd, cpu, &bpcpu);
}
if (count && !verboseopt) {
if ((count & 15) == 1)
printf(" NONE LOCKED CLEAN DIRTY "
"DIRTY_HW EMPTYKVA EMPTY OFF-QUEUE KVMFAIL\n");
printf("%6d %7d %6d %6d %9d %9d %6d %10d %7d\n",
qcounter[0], qcounter[1], qcounter[2],
qcounter[3], qcounter[4], qcounter[5],
qcounter[6], (nbuf - totalcount), failcount);
}
if (verboseopt)
break;
nanosleep(&delay, NULL);
bzero(&qcounter, sizeof(qcounter));
totalcount = 0;
failcount = 0;
}
return(0);
}
static const char *q2s(int queue)
{
switch(queue) {
case BQUEUE_NONE:
return "NONE";
case BQUEUE_LOCKED:
return "LOCKED";
case BQUEUE_CLEAN:
return "CLEAN";
case BQUEUE_DIRTY:
return "DIRTY";
case BQUEUE_DIRTY_HW:
return "DIRTY_HW";
case BQUEUE_EMPTYKVA:
return "EMPTYKVA";
case BQUEUE_EMPTY:
return "EMPTY";
default:
return "INVALID";
}
}
void
scan_queues(kvm_t *kd, int cpu, struct bufpcpu *bqp)
{
struct buf b, *tmp;
int q;
for (q = 0; q < BUFFER_QUEUES; q++) {
if (bqp->bufqueues[q].tqh_first == NULL)
continue;
kkread(kd, (u_long)bqp->bufqueues[q].tqh_first, &b, sizeof(b), 1);
tmp = bqp->bufqueues[q].tqh_first;
if (tmp != NULL)
qcounter[q]++;
while (tmp != NULL) {
if (verboseopt)
printf("cpu=%d queue=%8s buf=%p", cpu, q2s(q), tmp);
tmp = b.b_freelist.tqe_next;
if (kkread(kd, (u_long)tmp, &b, sizeof(b), 0) == -1) {
if (verboseopt)
printf(" [F] ");
failcount++;
}
if (verboseopt)
printf("\n");
qcounter[q]++;
totalcount++;
}
}
}
static
void
loaddelay(struct timespec *ts, const char *arg)
{
double d;
d = strtod(arg, NULL);
if (d < 0.001)
d = 0.001;
ts->tv_sec = (int)d;
ts->tv_nsec = (int)(modf(d, &d) * 1000000000.0);
}
static int
kkread(kvm_t *kd, u_long addr, void *buf, size_t nbytes, int out)
{
if (kvm_read(kd, addr, buf, nbytes) != nbytes) {
if (out) {
perror("kvm_read");
exit(1);
}
return -1;
}
return 0;
}