213 lines
4.6 KiB
C
Executable File
213 lines
4.6 KiB
C
Executable File
/*
|
|
* Copyright (c) 1985,1991 by Sun Microsystems, Inc.
|
|
*/
|
|
|
|
#ident "@(#)syncstat.c 1.6 92/09/11 SMI"
|
|
|
|
/*
|
|
* Display synchronous serial line statistics
|
|
*/
|
|
|
|
#include <sys/types.h>
|
|
#include <sys/ioctl.h>
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
#include <sys/stream.h>
|
|
#include <sys/stropts.h>
|
|
#include <fcntl.h>
|
|
#include <sys/ser_sync.h>
|
|
#include "dltest.h"
|
|
|
|
struct scc_mode sm;
|
|
struct sl_stats st;
|
|
|
|
char errstr[128] = "syncstat: /dev/";
|
|
char *ifdevice = errstr + 10;
|
|
char *ifname = errstr + 15;
|
|
int fd;
|
|
|
|
main(argc, argv)
|
|
int argc;
|
|
char **argv;
|
|
{
|
|
char *cp;
|
|
int do_clear = 0;
|
|
int period = 0;
|
|
int isize, osize;
|
|
int count;
|
|
struct strioctl sioc;
|
|
u_long ppa;
|
|
long buf[MAXDLBUF]; /* aligned on long */
|
|
|
|
if (argc == 1) {
|
|
usage();
|
|
exit(1);
|
|
}
|
|
argc--; /* skip the command name */
|
|
argv++;
|
|
|
|
/*
|
|
* The following loop processes command line arguments.
|
|
* If the argument begins with a '-', it is trated as an option.
|
|
* The only option currently implemented is "-c" (clears statistics).
|
|
* If the argument begins with a numeral, it is treated as an interval.
|
|
* Intervals must be positive integers greater than zero.
|
|
* Any argument that survives this is treated as a device name to be
|
|
* found under /dev.
|
|
*/
|
|
while (argc > 0) {
|
|
if (argv[0][0] == '-') {
|
|
if (argc == 1) {
|
|
usage();
|
|
exit(1);
|
|
}
|
|
if (argv[0][1] != 'c') {
|
|
usage();
|
|
exit(1);
|
|
}
|
|
do_clear = 1;
|
|
} else if ((argv[0][0] >= '0') && (argv[0][0] <= '9')) {
|
|
period = atoi(*argv);
|
|
if (period == 0) {
|
|
fprintf(stderr,
|
|
"syncstat: bad interval: %s\n", *argv);
|
|
exit(1);
|
|
}
|
|
} else {
|
|
sprintf(ifname, "%s", *argv);
|
|
}
|
|
argc--;
|
|
argv++;
|
|
}
|
|
|
|
for (cp = ifname; (*cp) && (!isdigit(*cp)); cp++) {}
|
|
if (*cp == '\0') { /* hit the end without finding a number */
|
|
fprintf(stderr,
|
|
"syncinit: %s missing minor device number\n", ifname);
|
|
exit(1);
|
|
}
|
|
ppa = strtoul(cp, NULL, 10);
|
|
*cp = '\0'; /* drop number, leaving name of clone device. */
|
|
fd = open(ifdevice, O_RDWR);
|
|
if (fd < 0) {
|
|
perror(errstr);
|
|
exit(1);
|
|
}
|
|
dlattachreq(fd, ppa);
|
|
dlokack(fd, buf);
|
|
printf("syncstat: control device: %s, ppa=%d\n", ifdevice, ppa);
|
|
|
|
sioc.ic_cmd = S_IOCGETMODE;
|
|
sioc.ic_timout = -1;
|
|
sioc.ic_len = sizeof(struct scc_mode);
|
|
sioc.ic_dp = (char *)&sm;
|
|
if (ioctl(fd, I_STR, &sioc) < 0) {
|
|
perror("S_IOCGETMODE");
|
|
fprintf(stderr, "syncstat: can't get sync mode info for %s\n",
|
|
ifname);
|
|
exit(1);
|
|
}
|
|
if (do_clear) {
|
|
sioc.ic_cmd = S_IOCCLRSTATS;
|
|
sioc.ic_timout = -1;
|
|
sioc.ic_len = sizeof(struct sl_stats);
|
|
sioc.ic_dp = (char *)&st;
|
|
if (ioctl(fd, I_STR, &sioc) < 0) {
|
|
perror("S_IOCCLRSTATS");
|
|
fprintf(stderr, "syncstat: can't clear stats for %s\n",
|
|
ifname);
|
|
exit(1);
|
|
}
|
|
}
|
|
|
|
sioc.ic_cmd = S_IOCGETSTATS;
|
|
sioc.ic_timout = -1;
|
|
sioc.ic_len = sizeof(struct sl_stats);
|
|
sioc.ic_dp = (char *)&st;
|
|
if (ioctl(fd, I_STR, &sioc) < 0) {
|
|
perror("S_IOCGETSTATS");
|
|
fprintf(stderr, "syncstat: can't get stats for %s\n",
|
|
ifname);
|
|
exit(1);
|
|
}
|
|
if (period) {
|
|
if (sm.sm_baudrate == 0) {
|
|
fprintf(stderr, "syncstat: baud rate not set\n");
|
|
exit(1);
|
|
}
|
|
for (count = 0; ; count++) {
|
|
fflush(stdout);
|
|
sleep(period);
|
|
sample(count, period);
|
|
}
|
|
}
|
|
isize = osize = 0;
|
|
if (st.opack)
|
|
osize = st.ochar / st.opack;
|
|
if (st.ipack)
|
|
isize = st.ichar / st.ipack;
|
|
printf(
|
|
" speed ipkts opkts undrun ovrrun abort crc isize osize\n");
|
|
printf(" %7d %7d %7d %7d %7d %7d %7d %7d %7d\n",
|
|
sm.sm_baudrate,
|
|
st.ipack,
|
|
st.opack,
|
|
st.underrun,
|
|
st.overrun,
|
|
st.abort,
|
|
st.crc,
|
|
isize, osize);
|
|
exit(0);
|
|
}
|
|
|
|
sample(count, period)
|
|
int count, period;
|
|
{
|
|
struct sl_stats nst;
|
|
struct strioctl sioc;
|
|
int iutil, outil;
|
|
|
|
sioc.ic_cmd = S_IOCGETSTATS;
|
|
sioc.ic_timout = -1;
|
|
sioc.ic_len = sizeof(struct sl_stats);
|
|
sioc.ic_dp = (char *)&nst;
|
|
if (ioctl(fd, I_STR, &sioc) < 0) {
|
|
perror("S_IOCGETSTATS");
|
|
fprintf(stderr, "syncstat: can't get stats for %s\n",
|
|
ifname);
|
|
exit(1);
|
|
}
|
|
|
|
st.ipack = nst.ipack - st.ipack;
|
|
st.opack = nst.opack - st.opack;
|
|
st.ichar = nst.ichar - st.ichar;
|
|
st.ochar = nst.ochar - st.ochar;
|
|
st.crc = nst.crc - st.crc;
|
|
st.overrun = nst.overrun - st.overrun;
|
|
st.underrun = nst.underrun - st.underrun;
|
|
st.abort = nst.abort - st.abort;
|
|
iutil = 8 * st.ichar / period;
|
|
iutil = 100 * iutil / sm.sm_baudrate;
|
|
outil = 8 * st.ochar / period;
|
|
outil = 100 * outil / sm.sm_baudrate;
|
|
if ((count % 20) == 0) printf(
|
|
" ipkts opkts undrun ovrrun abort crc iutil outil\n");
|
|
printf(" %7d %7d %7d %7d %7d %7d %6d%% %6d%%\n",
|
|
st.ipack,
|
|
st.opack,
|
|
st.underrun,
|
|
st.overrun,
|
|
st.abort,
|
|
st.crc,
|
|
iutil, outil);
|
|
|
|
st = nst;
|
|
}
|
|
|
|
usage()
|
|
{
|
|
fprintf(stderr, "%s\n%s\n",
|
|
"Usage: syncstat [-c] device [period]");
|
|
}
|