home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
1
/
1865
/
lavd.c
next >
Wrap
C/C++ Source or Header
|
1990-12-28
|
2KB
|
91 lines
#include <stdio.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <signal.h>
#include <nlist.h>
#include <fcntl.h>
#define INTERVAL 5
#define NSAMPLES (15 /* minutes */ * 60 /* sec/min */ / INTERVAL)
int ringload[NSAMPLES];
int ringindex = 0;
int id;
float *lav;
void
die()
{
if (lav)
(void) shmdt(lav);
if (id)
(void) shmctl(id, IPC_RMID, (struct shmid_ds *)NULL);
exit(1);
}
struct nlist sym[] = {
{ "sysinfo" },
{ NULL }
};
main()
{
struct sysinfo sinfo;
int flag = 0, now, last1, last5, last15;
extern int shmget();
extern char *shmat();
extern key_t ftok();
int kmem;
if(nlist("/unix", sym) < 0) {
fprintf(stderr, "/unix: no namelist.\n");
exit(1);
}
kmem=open("/dev/kmem", O_RDONLY);
if(kmem<0) {
perror("/dev/kmem");
exit(2);
}
if(!fork()) {
setpgrp();
if (signal(SIGINT, SIG_IGN) != SIG_IGN)
(void) signal(SIGINT, die);
if (signal(SIGHUP, SIG_IGN) != SIG_IGN)
(void) signal(SIGHUP, die);
if (signal(SIGTERM, SIG_IGN) != SIG_IGN)
(void) signal(SIGTERM, die);
id = shmget(ftok("/dev/kmem", 'lav'), 4 * sizeof (float), 0644|IPC_CREAT);
lav = (float *)shmat(id, (char *)0, 0);
for (;;) {
now = ringindex;
lseek(kmem, (long) sym[0].n_value, 0);
read(kmem, (char *) &sinfo, sizeof sinfo);
/*sysmp(MP_SAGET, MPSA_SINFO, &sinfo, sizeof sinfo);*/
ringload[ringindex++] = sinfo.runque;
if (!flag) {
while (flag < NSAMPLES)
ringload[flag++] = sinfo.runque;
if (!flag)
break; /* shut up the compiler */
}
ringindex %= NSAMPLES;
last1 = (NSAMPLES + now - 60 / INTERVAL)%NSAMPLES;
last5 = (NSAMPLES + now - 5*60 / INTERVAL)%NSAMPLES;
last15 = ringindex;
lav[0] = (ringload[now] - ringload[last1])/60.0;
lav[1] = (ringload[now] - ringload[last5])/(5*60.0),
lav[2] = (ringload[now] - ringload[last15])/(15*60.0);
lav[3] = (ringload[now] - ringload[(now-1+NSAMPLES)%NSAMPLES])/(float)INTERVAL;
/* printf("%6.2f %6.2f %6.2f\n", lav[0], lav[1], lav[2]); */
sleep(INTERVAL);
}
/* NOTREACHED */
}
exit(0);
}