home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Source Code 1992 March
/
Source_Code_CD-ROM_Walnut_Creek_March_1992.iso
/
usenet
/
altsrcs
/
3
/
3298
/
avenrun.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-05-06
|
2KB
|
82 lines
/* History:
5/1/91 DJB baseline public domain
*/
/* getavenrun() returns a pointer into an array of 3 doubles giving the */
/* current load average (i.e., AVErage Number of RUNning jobs). The first */
/* is typically the 1-minute average, the second is the 5-minute average, */
/* and the third is the 15-minute average. getavenrun returns 0 on error. */
/* avenruninit() does optional initialization to cooperate with other */
/* libraries that use nlistlist. avenruninit returns -1 on error. */
/* avenrunstrerr is a strerrfun for use with the strerr library. */
#include "avenrun.h"
#include "kmem.h"
#include "nlistlist.h"
#include "strerr.h"
#include "confloadavglong.h"
#define AVENRUNNLIST "_avenrun"
static int avinit = 0;
static double av[3];
static short avtype;
static unsigned long avvalue;
static int avenrunerrno = 0;
static struct strerrtab e[] = {
{ 0, "avenrun error 0", 0 }
#define AV_INIT 1
, { AV_INIT, "cannot init avenrun: ", nliststrerr }
#define AV_NLIST 2
, { AV_NLIST, "cannot nlist: ", nliststrerr }
#define AV_NLISTFOUND 3
, { AV_NLISTFOUND, AVENRUNNLIST, nlistnotin }
#define AV_KMEM 4
, { AV_KMEM, "cannot read avenrun: ", kmemstrerr }
};
char *avenrunstrerr(ke)
strerrfun *ke;
{
return strerrtaberr(ke,avenrunerrno,e,sizeof(e)/sizeof(*e),"unknown avenrun error");
}
int avenruninit()
{
if (nlistadd(AVENRUNNLIST,&avtype,&avvalue) == -1)
RETERN(-1,avenrunerrno,AV_INIT)
avinit = 1;
return 0;
}
#ifdef LOADAVGISLONG
static long avd[3];
#endif
double *getavenrun()
{
if (!avinit)
if (avenruninit() == -1)
return 0;
if (avtype == -1)
if (nlistdo() == -1)
RETERN(0,avenrunerrno,AV_NLIST)
if (!avtype)
RETERN(0,avenrunerrno,AV_NLISTFOUND)
#ifdef LOADAVGISLONG
if (kmemcpy((char *) &(avd[0]),(char *) avvalue,sizeof(avd)) == -1)
RETERN(0,avenrunerrno,AV_KMEM)
av[0] = ((double) avd[0]) / LOADAVGISLONG;
av[1] = ((double) avd[1]) / LOADAVGISLONG;
av[2] = ((double) avd[2]) / LOADAVGISLONG;
#else
if (kmemcpy((char *) &(av[0]),(char *) avvalue,sizeof(av)) == -1)
RETERN(0,avenrunerrno,AV_KMEM)
#endif
return av;
}