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 >
C/C++ Source or Header  |  1991-05-06  |  2KB  |  82 lines

  1. /* History:
  2. 5/1/91 DJB baseline public domain
  3. */
  4.  
  5. /* getavenrun() returns a pointer into an array of 3 doubles giving the */
  6. /* current load average (i.e., AVErage Number of RUNning jobs). The first */
  7. /* is typically the 1-minute average, the second is the 5-minute average, */
  8. /* and the third is the 15-minute average. getavenrun returns 0 on error. */
  9.  
  10. /* avenruninit() does optional initialization to cooperate with other */
  11. /* libraries that use nlistlist. avenruninit returns -1 on error. */
  12.  
  13. /* avenrunstrerr is a strerrfun for use with the strerr library. */
  14.  
  15. #include "avenrun.h"
  16. #include "kmem.h"
  17. #include "nlistlist.h"
  18. #include "strerr.h"
  19. #include "confloadavglong.h"
  20.  
  21. #define AVENRUNNLIST "_avenrun"
  22.  
  23. static int avinit = 0;
  24.  
  25. static double av[3];
  26. static short avtype;
  27. static unsigned long avvalue;
  28. static int avenrunerrno = 0;
  29.  
  30. static struct strerrtab e[] = {
  31.   { 0, "avenrun error 0", 0 }
  32. #define AV_INIT 1
  33. , { AV_INIT, "cannot init avenrun: ", nliststrerr }
  34. #define AV_NLIST 2
  35. , { AV_NLIST, "cannot nlist: ", nliststrerr }
  36. #define AV_NLISTFOUND 3
  37. , { AV_NLISTFOUND, AVENRUNNLIST, nlistnotin }
  38. #define AV_KMEM 4
  39. , { AV_KMEM, "cannot read avenrun: ", kmemstrerr }
  40. };
  41.  
  42. char *avenrunstrerr(ke)
  43. strerrfun *ke;
  44. {
  45.  return strerrtaberr(ke,avenrunerrno,e,sizeof(e)/sizeof(*e),"unknown avenrun error");
  46. }
  47.  
  48. int avenruninit()
  49. {
  50.  if (nlistadd(AVENRUNNLIST,&avtype,&avvalue) == -1)
  51.    RETERN(-1,avenrunerrno,AV_INIT)
  52.  avinit = 1;
  53.  return 0;
  54. }
  55.  
  56. #ifdef LOADAVGISLONG
  57. static long avd[3];
  58. #endif
  59.  
  60. double *getavenrun()
  61. {
  62.  if (!avinit)
  63.    if (avenruninit() == -1)
  64.      return 0;
  65.  if (avtype == -1)
  66.    if (nlistdo() == -1)
  67.      RETERN(0,avenrunerrno,AV_NLIST)
  68.  if (!avtype)
  69.    RETERN(0,avenrunerrno,AV_NLISTFOUND)
  70. #ifdef LOADAVGISLONG
  71.  if (kmemcpy((char *) &(avd[0]),(char *) avvalue,sizeof(avd)) == -1)
  72.    RETERN(0,avenrunerrno,AV_KMEM)
  73.  av[0] = ((double) avd[0]) / LOADAVGISLONG;
  74.  av[1] = ((double) avd[1]) / LOADAVGISLONG;
  75.  av[2] = ((double) avd[2]) / LOADAVGISLONG;
  76. #else
  77.  if (kmemcpy((char *) &(av[0]),(char *) avvalue,sizeof(av)) == -1)
  78.    RETERN(0,avenrunerrno,AV_KMEM)
  79. #endif
  80.  return av;
  81. }
  82.