remove unused files.
This commit is contained in:
parent
159e915384
commit
1c625a0e79
|
@ -1,431 +0,0 @@
|
|||
/* $NetBSD: dkstats.c,v 1.23 2006/02/05 09:54:50 dsl Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996 John M. Vinopal
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed for the NetBSD Project
|
||||
* by John M. Vinopal.
|
||||
* 4. 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 ``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 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.
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/sched.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/disk.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <kvm.h>
|
||||
#include <limits.h>
|
||||
#include <nlist.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "dkstats.h"
|
||||
|
||||
static struct nlist namelist[] = {
|
||||
#define X_TK_NIN 0
|
||||
{ "_tk_nin" }, /* tty characters in */
|
||||
#define X_TK_NOUT 1
|
||||
{ "_tk_nout" }, /* tty characters out */
|
||||
#define X_HZ 2
|
||||
{ "_hz" }, /* ticks per second */
|
||||
#define X_STATHZ 3
|
||||
{ "_stathz" },
|
||||
#define X_DISK_COUNT 4
|
||||
{ "_disk_count" }, /* number of disks */
|
||||
#define X_DISKLIST 5
|
||||
{ "_disklist" }, /* TAILQ of disks */
|
||||
{ NULL },
|
||||
};
|
||||
|
||||
/* Structures to hold the statistics. */
|
||||
struct _vminfo cur, last;
|
||||
|
||||
/* Kernel pointers: nlistf and memf defined in calling program. */
|
||||
static kvm_t *kd = NULL;
|
||||
extern char *nlistf;
|
||||
extern char *memf;
|
||||
extern int hz;
|
||||
|
||||
/* Pointer to list of disks. */
|
||||
static struct disk *dk_drivehead = NULL;
|
||||
/* sysctl hw.diskstats buffer. */
|
||||
static struct disk_sysctl *dk_drives = NULL;
|
||||
|
||||
/* Backward compatibility references. */
|
||||
int dk_ndrive = 0;
|
||||
int *dk_select;
|
||||
char **dr_name;
|
||||
|
||||
#define KVM_ERROR(_string) do { \
|
||||
warnx("%s", (_string)); \
|
||||
errx(1, "%s", kvm_geterr(kd)); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
/*
|
||||
* Dereference the namelist pointer `v' and fill in the local copy
|
||||
* 'p' which is of size 's'.
|
||||
*/
|
||||
#define deref_nl(v, p, s) do { \
|
||||
deref_kptr((void *)namelist[(v)].n_value, (p), (s)); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
/* Missing from <sys/time.h> */
|
||||
#define timerset(tvp, uvp) do { \
|
||||
((uvp)->tv_sec = (tvp)->tv_sec); \
|
||||
((uvp)->tv_usec = (tvp)->tv_usec); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
static void deref_kptr(void *, void *, size_t);
|
||||
|
||||
/*
|
||||
* Take the delta between the present values and the last recorded
|
||||
* values, storing the present values in the 'last' structure, and
|
||||
* the delta values in the 'cur' structure.
|
||||
*/
|
||||
|
||||
#define SWAP(fld) do { \
|
||||
tmp = cur.fld; \
|
||||
cur.fld -= last.fld; \
|
||||
last.fld = tmp; \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
void
|
||||
dkswap(void)
|
||||
{
|
||||
u_int64_t tmp;
|
||||
int i;
|
||||
|
||||
for (i = 0; i < dk_ndrive; i++) {
|
||||
struct timeval tmp_timer;
|
||||
|
||||
if (!cur.dk_select[i])
|
||||
continue;
|
||||
|
||||
/* Delta Values. */
|
||||
SWAP(dk_rxfer[i]);
|
||||
SWAP(dk_wxfer[i]);
|
||||
SWAP(dk_seek[i]);
|
||||
SWAP(dk_rbytes[i]);
|
||||
SWAP(dk_wbytes[i]);
|
||||
|
||||
/* Delta Time. */
|
||||
timerclear(&tmp_timer);
|
||||
timerset(&(cur.dk_time[i]), &tmp_timer);
|
||||
timersub(&tmp_timer, &(last.dk_time[i]), &(cur.dk_time[i]));
|
||||
timerclear(&(last.dk_time[i]));
|
||||
timerset(&tmp_timer, &(last.dk_time[i]));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void
|
||||
tkswap(void)
|
||||
{
|
||||
u_int64_t tmp;
|
||||
|
||||
SWAP(tk_nin);
|
||||
SWAP(tk_nout);
|
||||
}
|
||||
|
||||
void
|
||||
cpuswap(void)
|
||||
{
|
||||
double etime;
|
||||
u_int64_t tmp;
|
||||
int i, state;
|
||||
|
||||
for (i = 0; i < CPUSTATES; i++)
|
||||
SWAP(cp_time[i]);
|
||||
|
||||
etime = 0;
|
||||
for (state = 0; state < CPUSTATES; ++state) {
|
||||
etime += cur.cp_time[state];
|
||||
}
|
||||
if (etime == 0)
|
||||
etime = 1;
|
||||
etime /= hz;
|
||||
etime /= cur.cp_ncpu;
|
||||
|
||||
cur.cp_etime = etime;
|
||||
}
|
||||
#undef SWAP
|
||||
|
||||
/*
|
||||
* Read the disk statistics for each disk in the disk list.
|
||||
*/
|
||||
void
|
||||
dkreadstats(void)
|
||||
{
|
||||
struct disk cur_disk, *p;
|
||||
size_t size;
|
||||
int mib[3];
|
||||
int i;
|
||||
|
||||
p = dk_drivehead;
|
||||
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_DISKSTATS;
|
||||
mib[2] = sizeof(struct disk_sysctl);
|
||||
|
||||
size = dk_ndrive * sizeof(struct disk_sysctl);
|
||||
if (sysctl(mib, 3, dk_drives, &size, NULL, 0) < 0)
|
||||
err(1, "sysctl hw.diskstats failed");
|
||||
for (i = 0; i < dk_ndrive; i++) {
|
||||
cur.dk_rxfer[i] = dk_drives[i].dk_rxfer;
|
||||
cur.dk_wxfer[i] = dk_drives[i].dk_wxfer;
|
||||
cur.dk_seek[i] = dk_drives[i].dk_seek;
|
||||
cur.dk_rbytes[i] = dk_drives[i].dk_rbytes;
|
||||
cur.dk_wbytes[i] = dk_drives[i].dk_wbytes;
|
||||
cur.dk_time[i].tv_sec = dk_drives[i].dk_time_sec;
|
||||
cur.dk_time[i].tv_usec = dk_drives[i].dk_time_usec;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < dk_ndrive; i++) {
|
||||
deref_kptr(p, &cur_disk, sizeof(cur_disk));
|
||||
cur.dk_rxfer[i] = cur_disk.dk_rxfer;
|
||||
cur.dk_wxfer[i] = cur_disk.dk_wxfer;
|
||||
cur.dk_seek[i] = cur_disk.dk_seek;
|
||||
cur.dk_rbytes[i] = cur_disk.dk_rbytes;
|
||||
cur.dk_wbytes[i] = cur_disk.dk_wbytes;
|
||||
timerset(&(cur_disk.dk_time), &(cur.dk_time[i]));
|
||||
p = cur_disk.dk_link.tqe_next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read collect statistics for tty i/o.
|
||||
*/
|
||||
|
||||
void
|
||||
tkreadstats(void)
|
||||
{
|
||||
size_t size;
|
||||
int mib[3];
|
||||
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_TKSTAT;
|
||||
mib[2] = KERN_TKSTAT_NIN;
|
||||
size = sizeof(cur.tk_nin);
|
||||
if (sysctl(mib, 3, &cur.tk_nin, &size, NULL, 0) < 0)
|
||||
cur.tk_nin = 0;
|
||||
|
||||
mib[2] = KERN_TKSTAT_NOUT;
|
||||
size = sizeof(cur.tk_nout);
|
||||
if (sysctl(mib, 3, &cur.tk_nout, &size, NULL, 0) < 0)
|
||||
cur.tk_nout = 0;
|
||||
} else {
|
||||
deref_nl(X_TK_NIN, &cur.tk_nin, sizeof(cur.tk_nin));
|
||||
deref_nl(X_TK_NOUT, &cur.tk_nout, sizeof(cur.tk_nout));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Read collect statistics for CPU ticks.
|
||||
*/
|
||||
|
||||
void
|
||||
cpureadstats(void)
|
||||
{
|
||||
size_t size;
|
||||
int mib[2];
|
||||
|
||||
/*
|
||||
* XXX Need to locate the `correct' CPU when looking for this
|
||||
* XXX in crash dumps. Just don't report it for now, in that
|
||||
* XXX case.
|
||||
*/
|
||||
size = sizeof(cur.cp_time);
|
||||
memset(cur.cp_time, 0, size);
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_CP_TIME;
|
||||
if (sysctl(mib, 2, cur.cp_time, &size, NULL, 0) < 0)
|
||||
memset(cur.cp_time, 0, sizeof(cur.cp_time));
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform all of the initialization and memory allocation needed to
|
||||
* track disk statistics.
|
||||
*/
|
||||
int
|
||||
dkinit(int selected)
|
||||
{
|
||||
struct disklist_head disk_head;
|
||||
struct disk cur_disk, *p;
|
||||
struct clockinfo clockinfo;
|
||||
char errbuf[_POSIX2_LINE_MAX];
|
||||
size_t size;
|
||||
static int once = 0;
|
||||
int i, mib[3];
|
||||
|
||||
if (once)
|
||||
return (1);
|
||||
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_NCPU;
|
||||
size = sizeof(cur.cp_ncpu);
|
||||
if (sysctl(mib, 2, &cur.cp_ncpu, &size, NULL, 0) == -1)
|
||||
err(1, "sysctl hw.ncpu failed");
|
||||
|
||||
mib[0] = CTL_KERN;
|
||||
mib[1] = KERN_CLOCKRATE;
|
||||
size = sizeof(clockinfo);
|
||||
if (sysctl(mib, 2, &clockinfo, &size, NULL, 0) == -1)
|
||||
err(1, "sysctl kern.clockrate failed");
|
||||
hz = clockinfo.stathz;
|
||||
if (!hz)
|
||||
hz = clockinfo.hz;
|
||||
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_DISKSTATS;
|
||||
mib[2] = sizeof(struct disk_sysctl);
|
||||
if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1)
|
||||
err(1, "sysctl hw.diskstats failed");
|
||||
dk_ndrive = size / sizeof(struct disk_sysctl);
|
||||
|
||||
if (size == 0) {
|
||||
warnx("No drives attached.");
|
||||
} else {
|
||||
dk_drives = (struct disk_sysctl *)malloc(size);
|
||||
if (dk_drives == NULL)
|
||||
errx(1, "Memory allocation failure.");
|
||||
}
|
||||
} else {
|
||||
/* Open the kernel. */
|
||||
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
|
||||
errbuf)) == NULL)
|
||||
errx(1, "kvm_openfiles: %s", errbuf);
|
||||
|
||||
/* Obtain the namelist symbols from the kernel. */
|
||||
if (kvm_nlist(kd, namelist))
|
||||
KVM_ERROR("kvm_nlist failed to read symbols.");
|
||||
|
||||
/* Get the number of attached drives. */
|
||||
deref_nl(X_DISK_COUNT, &dk_ndrive, sizeof(dk_ndrive));
|
||||
|
||||
if (dk_ndrive < 0)
|
||||
errx(1, "invalid _disk_count %d.", dk_ndrive);
|
||||
else if (dk_ndrive == 0) {
|
||||
warnx("No drives attached.");
|
||||
} else {
|
||||
/* Get a pointer to the first disk. */
|
||||
deref_nl(X_DISKLIST, &disk_head, sizeof(disk_head));
|
||||
dk_drivehead = disk_head.tqh_first;
|
||||
}
|
||||
|
||||
/* Get ticks per second. */
|
||||
deref_nl(X_STATHZ, &hz, sizeof(hz));
|
||||
if (!hz)
|
||||
deref_nl(X_HZ, &hz, sizeof(hz));
|
||||
}
|
||||
|
||||
/* Allocate space for the statistics. */
|
||||
cur.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
|
||||
cur.dk_rxfer = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
cur.dk_wxfer = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
cur.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
cur.dk_rbytes = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
cur.dk_wbytes = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
last.dk_time = calloc(dk_ndrive, sizeof(struct timeval));
|
||||
last.dk_rxfer = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
last.dk_wxfer = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
last.dk_seek = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
last.dk_rbytes = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
last.dk_wbytes = calloc(dk_ndrive, sizeof(u_int64_t));
|
||||
cur.dk_select = calloc(dk_ndrive, sizeof(int));
|
||||
cur.dk_name = calloc(dk_ndrive, sizeof(char *));
|
||||
|
||||
if (cur.dk_time == NULL || cur.dk_rxfer == NULL ||
|
||||
cur.dk_wxfer == NULL || cur.dk_seek == NULL ||
|
||||
cur.dk_rbytes == NULL || cur.dk_wbytes == NULL ||
|
||||
last.dk_time == NULL || last.dk_rxfer == NULL ||
|
||||
last.dk_wxfer == NULL || last.dk_seek == NULL ||
|
||||
last.dk_rbytes == NULL || last.dk_wbytes == NULL ||
|
||||
cur.dk_select == NULL || cur.dk_name == NULL)
|
||||
errx(1, "Memory allocation failure.");
|
||||
|
||||
/* Set up the compatibility interfaces. */
|
||||
dk_select = cur.dk_select;
|
||||
dr_name = cur.dk_name;
|
||||
|
||||
/* Read the disk names and set intial selection. */
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW; /* Should be still set from */
|
||||
mib[1] = HW_DISKSTATS; /* ... above, but be safe... */
|
||||
mib[2] = sizeof(struct disk_sysctl);
|
||||
if (sysctl(mib, 3, dk_drives, &size, NULL, 0) == -1)
|
||||
err(1, "sysctl hw.diskstats failed");
|
||||
for (i = 0; i < dk_ndrive; i++) {
|
||||
cur.dk_name[i] = dk_drives[i].dk_name;
|
||||
cur.dk_select[i] = selected;
|
||||
}
|
||||
} else {
|
||||
p = dk_drivehead;
|
||||
for (i = 0; i < dk_ndrive; i++) {
|
||||
char buf[10];
|
||||
deref_kptr(p, &cur_disk, sizeof(cur_disk));
|
||||
deref_kptr(cur_disk.dk_name, buf, sizeof(buf));
|
||||
cur.dk_name[i] = strdup(buf);
|
||||
if (!cur.dk_name[i])
|
||||
err(1, "strdup");
|
||||
cur.dk_select[i] = selected;
|
||||
|
||||
p = cur_disk.dk_link.tqe_next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Never do this initialization again. */
|
||||
once = 1;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Dereference the kernel pointer `kptr' and fill in the local copy
|
||||
* pointed to by `ptr'. The storage space must be pre-allocated,
|
||||
* and the size of the copy passed in `len'.
|
||||
*/
|
||||
static void
|
||||
deref_kptr(void *kptr, void *ptr, size_t len)
|
||||
{
|
||||
char buf[128];
|
||||
|
||||
if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
snprintf(buf, sizeof buf, "can't dereference kptr 0x%lx",
|
||||
(u_long)kptr);
|
||||
KVM_ERROR(buf);
|
||||
}
|
||||
}
|
|
@ -1,66 +0,0 @@
|
|||
/* $NetBSD: dkstats.h,v 1.7 2006/02/05 09:54:50 dsl Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 1996 John M. Vinopal
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed for the NetBSD Project
|
||||
* by John M. Vinopal.
|
||||
* 4. 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 ``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 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.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/sched.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* poseur disk entry to hold the information we're interested in. */
|
||||
struct _vminfo {
|
||||
int *dk_select; /* Display stats for selected disks. */
|
||||
char **dk_name; /* Disk names (sd0, wd1, etc). */
|
||||
u_int64_t *dk_rxfer; /* # of read transfers. */
|
||||
u_int64_t *dk_wxfer; /* # of write transfers. */
|
||||
u_int64_t *dk_seek; /* # of seeks (currently unused). */
|
||||
u_int64_t *dk_rbytes; /* # of bytes read. */
|
||||
u_int64_t *dk_wbytes; /* # of bytes written. */
|
||||
struct timeval *dk_time; /* Time spent in disk i/o. */
|
||||
u_int64_t tk_nin; /* TTY Chars in. */
|
||||
u_int64_t tk_nout; /* TTY Chars out. */
|
||||
u_int64_t cp_time[CPUSTATES]; /* System timer ticks. */
|
||||
int cp_ncpu; /* Number of cpu's */
|
||||
double cp_etime; /* Elapsed time */
|
||||
};
|
||||
|
||||
extern struct _vminfo cur;
|
||||
extern char **dr_name;
|
||||
extern int *dk_select, dk_ndrive;
|
||||
|
||||
int dkinit(int);
|
||||
void cpureadstats(void);
|
||||
void dkreadstats(void);
|
||||
void tkreadstats(void);
|
||||
void cpuswap(void);
|
||||
void dkswap(void);
|
||||
void tkswap(void);
|
|
@ -1,315 +0,0 @@
|
|||
/* $NetBSD: tpstats.c,v 1.3 2005/09/10 11:10:21 blymn Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright 2005 Brett Lymn
|
||||
*
|
||||
* This file is derived dkstats.c
|
||||
*
|
||||
* Copyright (c) 1996 John M. Vinopal
|
||||
* 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. All advertising materials mentioning features or use of this software
|
||||
* must display the following acknowledgement:
|
||||
* This product includes software developed for the NetBSD Project
|
||||
* by John M. Vinopal.
|
||||
* 4. 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 ``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 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.
|
||||
*/
|
||||
|
||||
#include <sys/param.h>
|
||||
#include <sys/sched.h>
|
||||
#include <sys/sysctl.h>
|
||||
#include <sys/time.h>
|
||||
#include <sys/tape.h>
|
||||
|
||||
#include <err.h>
|
||||
#include <fcntl.h>
|
||||
#include <kvm.h>
|
||||
#include <limits.h>
|
||||
#include <nlist.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include "tpstats.h"
|
||||
|
||||
static struct nlist namelist[] = {
|
||||
#define X_TAPE_COUNT 0
|
||||
{ "_tape_count" }, /* number of tapes */
|
||||
#define X_TAPELIST 1
|
||||
{ "_tapelist" }, /* TAILQ of tapes */
|
||||
{ NULL },
|
||||
};
|
||||
|
||||
/* Structures to hold the statistics. */
|
||||
struct _tape cur_tape, last_tape;
|
||||
|
||||
/* Kernel pointers: nlistf and memf defined in calling program. */
|
||||
static kvm_t *kd = NULL;
|
||||
extern char *nlistf;
|
||||
extern char *memf;
|
||||
extern int hz;
|
||||
|
||||
static int tp_once = 0;
|
||||
|
||||
/* Pointer to list of tapes. */
|
||||
static struct tape *tapehead = NULL;
|
||||
/* sysctl hw.tapestats buffer. */
|
||||
static struct tape_sysctl *tapes = NULL;
|
||||
|
||||
/* Backward compatibility references. */
|
||||
int tp_ndrive = 0;
|
||||
int *tp_select;
|
||||
char **tp_name;
|
||||
|
||||
#define KVM_ERROR(_string) do { \
|
||||
warnx("%s", (_string)); \
|
||||
errx(1, "%s", kvm_geterr(kd)); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
/*
|
||||
* Dereference the namelist pointer `v' and fill in the local copy
|
||||
* 'p' which is of size 's'.
|
||||
*/
|
||||
#define deref_nl(v, p, s) do { \
|
||||
deref_kptr((void *)namelist[(v)].n_value, (p), (s)); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
/* Missing from <sys/time.h> */
|
||||
#define timerset(tvp, uvp) do { \
|
||||
((uvp)->tv_sec = (tvp)->tv_sec); \
|
||||
((uvp)->tv_usec = (tvp)->tv_usec); \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
static void deref_kptr(void *, void *, size_t);
|
||||
|
||||
/*
|
||||
* Take the delta between the present values and the last recorded
|
||||
* values, storing the present values in the 'last_tape' structure, and
|
||||
* the delta values in the 'cur_tape' structure.
|
||||
*/
|
||||
void
|
||||
tpswap(void)
|
||||
{
|
||||
u_int64_t tmp;
|
||||
int i;
|
||||
|
||||
#define SWAP(fld) do { \
|
||||
tmp = cur_tape.fld; \
|
||||
cur_tape.fld -= last_tape.fld; \
|
||||
last_tape.fld = tmp; \
|
||||
} while (/* CONSTCOND */0)
|
||||
|
||||
for (i = 0; i < tp_ndrive; i++) {
|
||||
struct timeval tmp_timer;
|
||||
|
||||
if (!cur_tape.select[i])
|
||||
continue;
|
||||
|
||||
/* Delta Values. */
|
||||
SWAP(rxfer[i]);
|
||||
SWAP(wxfer[i]);
|
||||
SWAP(rbytes[i]);
|
||||
SWAP(wbytes[i]);
|
||||
|
||||
/* Delta Time. */
|
||||
timerclear(&tmp_timer);
|
||||
timerset(&(cur_tape.time[i]), &tmp_timer);
|
||||
timersub(&tmp_timer, &(last_tape.time[i]), &(cur_tape.time[i]));
|
||||
timerclear(&(last_tape.time[i]));
|
||||
timerset(&tmp_timer, &(last_tape.time[i]));
|
||||
}
|
||||
|
||||
#undef SWAP
|
||||
}
|
||||
|
||||
/*
|
||||
* Read the tape statistics for each tape drive in the tape list.
|
||||
*/
|
||||
void
|
||||
tpreadstats(void)
|
||||
{
|
||||
struct tape current, *p;
|
||||
size_t size;
|
||||
int mib[3];
|
||||
int i;
|
||||
|
||||
p = tapehead;
|
||||
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_TAPESTATS;
|
||||
mib[2] = sizeof(struct tape_sysctl);
|
||||
|
||||
size = tp_ndrive * sizeof(struct tape_sysctl);
|
||||
if (sysctl(mib, 3, tapes, &size, NULL, 0) < 0)
|
||||
tp_ndrive = 0;
|
||||
for (i = 0; i < tp_ndrive; i++) {
|
||||
cur_tape.rxfer[i] = tapes[i].rxfer;
|
||||
cur_tape.wxfer[i] = tapes[i].wxfer;
|
||||
cur_tape.rbytes[i] = tapes[i].rbytes;
|
||||
cur_tape.wbytes[i] = tapes[i].wbytes;
|
||||
cur_tape.time[i].tv_sec = tapes[i].time_sec;
|
||||
cur_tape.time[i].tv_usec = tapes[i].time_usec;
|
||||
}
|
||||
} else {
|
||||
for (i = 0; i < tp_ndrive; i++) {
|
||||
deref_kptr(p, ¤t, sizeof(current));
|
||||
cur_tape.rxfer[i] = current.rxfer;
|
||||
cur_tape.wxfer[i] = current.wxfer;
|
||||
cur_tape.rbytes[i] = current.rbytes;
|
||||
cur_tape.wbytes[i] = current.wbytes;
|
||||
timerset(&(current.time), &(cur_tape.time[i]));
|
||||
p = current.link.tqe_next;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Perform all of the initialization and memory allocation needed to
|
||||
* track disk statistics.
|
||||
*/
|
||||
int
|
||||
tpinit(int selected)
|
||||
{
|
||||
struct tapelist_head tapelist_head;
|
||||
struct tape *p, current;
|
||||
char errbuf[_POSIX2_LINE_MAX];
|
||||
size_t size;
|
||||
int i, mib[3];
|
||||
|
||||
if (tp_once)
|
||||
return (1);
|
||||
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW;
|
||||
mib[1] = HW_TAPESTATS;
|
||||
mib[2] = sizeof(struct tape_sysctl);
|
||||
if (sysctl(mib, 3, NULL, &size, NULL, 0) == -1)
|
||||
size = 0;
|
||||
|
||||
tp_ndrive = size / sizeof(struct tape_sysctl);
|
||||
|
||||
if (size != 0) {
|
||||
tapes = (struct tape_sysctl *)malloc(size);
|
||||
if (tapes == NULL)
|
||||
errx(1, "Memory allocation failure.");
|
||||
}
|
||||
} else {
|
||||
/* Open the kernel. */
|
||||
if ((kd = kvm_openfiles(nlistf, memf, NULL, O_RDONLY,
|
||||
errbuf)) == NULL)
|
||||
errx(1, "kvm_openfiles: %s", errbuf);
|
||||
|
||||
/* Obtain the namelist symbols from the kernel. */
|
||||
if (kvm_nlist(kd, namelist))
|
||||
KVM_ERROR("kvm_nlist failed to read symbols.");
|
||||
|
||||
/* Get the number of attached drives. */
|
||||
deref_nl(X_TAPE_COUNT, &tp_ndrive, sizeof(tp_ndrive));
|
||||
|
||||
if (tp_ndrive < 0)
|
||||
errx(1, "invalid _tape_count %d.", tp_ndrive);
|
||||
else if (tp_ndrive != 0) {
|
||||
/* Get a pointer to the first disk. */
|
||||
deref_nl(X_TAPELIST, &tapelist_head,
|
||||
sizeof(tapelist_head));
|
||||
tapehead = tapelist_head.tqh_first;
|
||||
}
|
||||
}
|
||||
|
||||
/* Allocate space for the statistics. */
|
||||
cur_tape.time = calloc(tp_ndrive, sizeof(struct timeval));
|
||||
cur_tape.rxfer = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
cur_tape.wxfer = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
cur_tape.rbytes = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
cur_tape.wbytes = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
last_tape.time = calloc(tp_ndrive, sizeof(struct timeval));
|
||||
last_tape.rxfer = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
last_tape.wxfer = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
last_tape.rbytes = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
last_tape.wbytes = calloc(tp_ndrive, sizeof(u_int64_t));
|
||||
cur_tape.select = calloc(tp_ndrive, sizeof(int));
|
||||
cur_tape.name = calloc(tp_ndrive, sizeof(char *));
|
||||
|
||||
if (cur_tape.time == NULL || cur_tape.rxfer == NULL ||
|
||||
cur_tape.wxfer == NULL ||
|
||||
cur_tape.rbytes == NULL || cur_tape.wbytes == NULL ||
|
||||
last_tape.time == NULL || last_tape.rxfer == NULL ||
|
||||
last_tape.wxfer == NULL ||
|
||||
last_tape.rbytes == NULL || last_tape.wbytes == NULL ||
|
||||
cur_tape.select == NULL || cur_tape.name == NULL)
|
||||
errx(1, "Memory allocation failure.");
|
||||
|
||||
/* Set up the compatibility interfaces. */
|
||||
tp_select = cur_tape.select;
|
||||
tp_name = cur_tape.name;
|
||||
|
||||
/* Read the disk names and set intial selection. */
|
||||
if (memf == NULL) {
|
||||
mib[0] = CTL_HW; /* Should be still set from */
|
||||
mib[1] = HW_TAPESTATS; /* ... above, but be safe... */
|
||||
mib[2] = sizeof(struct tape_sysctl);
|
||||
if (sysctl(mib, 3, tapes, &size, NULL, 0) == -1)
|
||||
tp_ndrive = 0;
|
||||
for (i = 0; i < tp_ndrive; i++) {
|
||||
cur_tape.name[i] = tapes[i].name;
|
||||
cur_tape.select[i] = selected;
|
||||
}
|
||||
} else {
|
||||
p = tapehead;
|
||||
for (i = 0; i < tp_ndrive; i++) {
|
||||
char buf[TAPENAMELEN];
|
||||
deref_kptr(p, ¤t, sizeof(current));
|
||||
deref_kptr(current.name, buf, sizeof(buf));
|
||||
cur_tape.name[i] = strdup(buf);
|
||||
if (!cur_tape.name[i])
|
||||
err(1, "strdup");
|
||||
cur_tape.select[i] = selected;
|
||||
|
||||
p = current.link.tqe_next;
|
||||
}
|
||||
}
|
||||
|
||||
/* Never do this initialization again. */
|
||||
tp_once = 1;
|
||||
return (1);
|
||||
}
|
||||
|
||||
/*
|
||||
* Dereference the kernel pointer `kptr' and fill in the local copy
|
||||
* pointed to by `ptr'. The storage space must be pre-allocated,
|
||||
* and the size of the copy passed in `len'.
|
||||
*/
|
||||
static void
|
||||
deref_kptr(void *kptr, void *ptr, size_t len)
|
||||
{
|
||||
char buf[128];
|
||||
|
||||
if (kvm_read(kd, (u_long)kptr, (char *)ptr, len) != len) {
|
||||
memset(buf, 0, sizeof(buf));
|
||||
snprintf(buf, sizeof buf, "can't dereference kptr 0x%lx",
|
||||
(u_long)kptr);
|
||||
KVM_ERROR(buf);
|
||||
}
|
||||
}
|
|
@ -1,54 +0,0 @@
|
|||
/* $NetBSD: tpstats.h,v 1.1 2005/08/07 12:21:46 blymn Exp $ */
|
||||
|
||||
/*
|
||||
* Copyright (c) 2005 The NetBSD Foundation, Inc.
|
||||
* All rights reserved.
|
||||
*
|
||||
* This code is derived from code donated to the NetBSD Foundation, Inc.
|
||||
* by Brett Lymn.
|
||||
*
|
||||
* 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. 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 ``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 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.
|
||||
*/
|
||||
|
||||
#include <sys/time.h>
|
||||
#include <sys/sched.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/* Tape drive entry to hold the information we're interested in. */
|
||||
struct _tape {
|
||||
int *select; /* Display stats for selected tapes. */
|
||||
char **name; /* Tape drive names (st0, etc). */
|
||||
u_int64_t *rxfer; /* # of read transfers. */
|
||||
u_int64_t *wxfer; /* # of write transfers. */
|
||||
u_int64_t *rbytes; /* # of bytes read. */
|
||||
u_int64_t *wbytes; /* # of bytes written. */
|
||||
struct timeval *time; /* Time spent in tape i/o. */
|
||||
};
|
||||
|
||||
extern struct _tape cur_tape;
|
||||
extern char **tp_name;
|
||||
extern int *tp_select, tp_ndrive;
|
||||
|
||||
int tpinit(int);
|
||||
void tpreadstats(void);
|
||||
void tpswap(void);
|
||||
|
||||
|
Loading…
Reference in New Issue