home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.004 / xemacs-1 / xemacs-19.13 / src / getloadavg.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-14  |  22.2 KB  |  937 lines

  1. /* Get the system load averages.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994
  3.    Free Software Foundation, Inc.
  4.  
  5. This file is part of XEmacs.
  6.  
  7. XEmacs is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. XEmacs is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with XEmacs; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. /* Synched up with: FSF 19.28. */
  22.  
  23. /* Compile-time symbols that this file uses:
  24.  
  25.    FIXUP_KERNEL_SYMBOL_ADDR()    Adjust address in returned struct nlist.
  26.    KERNEL_FILE            Pathname of the kernel to nlist.
  27.    LDAV_CVT()            Scale the load average from the kernel.
  28.                 Returns a double.
  29.    LDAV_SYMBOL            Name of kernel symbol giving load average.
  30.    LOAD_AVE_TYPE        Type of the load average array in the kernel.
  31.                 Must be defined unless one of
  32.                 apollo, DGUX, NeXT, or UMAX is defined;
  33.                 otherwise, no load average is available.
  34.                                 Does not need to be defined if HPUX is
  35.                                 defined and HPUX_PRE_8_0 is not defined.
  36.    NLIST_STRUCT            Include nlist.h, not a.out.h, and
  37.                 the nlist n_name element is a pointer,
  38.                 not an array.
  39.    NLIST_NAME_UNION        struct nlist has an n_un member, not n_name.
  40.    LINUX_LDAV_FILE        [__linux__]: File containing load averages.
  41.  
  42.    Specific system predefines this file uses, aside from setting
  43.    default values if not emacs:
  44.  
  45.    apollo
  46.    BSD                Real BSD, not just BSD-like.
  47.    DGUX
  48.    eunice            UNIX emulator under VMS.
  49.    hpux
  50.    NeXT
  51.    sgi
  52.    sequent            Sequent Dynix 3.x.x (BSD)
  53.    _SEQUENT_            Sequent DYNIX/ptx 1.x.x (SYSV)
  54.    sony_news                    NEWS-OS (works at least for 4.1C)
  55.    UMAX
  56.    UMAX4_3
  57.    VMS
  58.    __linux__            Linux: assumes /proc filesystem mounted.
  59.                    Support from Michael K. Johnson.
  60.    __NetBSD__            NetBSD: assumes /kern filesystem mounted.
  61.  
  62.    In addition, to avoid nesting many #ifdefs, we internally set
  63.    LDAV_DONE to indicate that the load average has been computed.
  64.  
  65.    We also #define LDAV_PRIVILEGED if a program will require
  66.    special installation to be able to call getloadavg.  */
  67.  
  68. #include <sys/types.h>
  69.  
  70. /* Both the Emacs and non-Emacs sections want this.  Some
  71.    configuration files' definitions for the LOAD_AVE_CVT macro (like
  72.    sparc.h's) use macros like FSCALE, defined here.  */
  73. #ifdef unix
  74. #include <sys/param.h>
  75. #endif
  76.  
  77.  
  78. #if defined(HAVE_CONFIG_H) || defined(emacs)
  79. #include <config.h>
  80. #include "lisp.h" /* for encapsulated open, close, read, write */
  81. #endif
  82.  
  83. /* Exclude all the code except the test program at the end
  84.    if the system has its own `getloadavg' function.
  85.  
  86.    The declaration of `errno' is needed by the test program
  87.    as well as the function itself, so it comes first.  */
  88.  
  89. #include <errno.h>
  90.  
  91. #ifndef errno
  92. extern int errno;
  93. #endif
  94.  
  95. #ifndef HAVE_GETLOADAVG
  96.  
  97. /* The existing Emacs configuration files define a macro called
  98.    LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  99.    returns the load average multiplied by 100.  What we actually want
  100.    is a macro called LDAV_CVT, which returns the load average as an
  101.    unmultiplied double.
  102.  
  103.    For backwards compatibility, we'll define LDAV_CVT in terms of
  104.    LOAD_AVE_CVT, but future machine config files should just define
  105.    LDAV_CVT directly.  */
  106.  
  107. #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
  108. #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  109. #endif
  110.  
  111. #if !defined (BSD) && defined (ultrix)
  112. /* Ultrix behaves like BSD on Vaxen.  */
  113. #define BSD
  114. #endif
  115.  
  116. #ifdef NeXT
  117. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  118.    conflicts with the definition understood in this file, that this
  119.    really is BSD. */
  120. #undef BSD
  121.  
  122. /* NeXT defines FSCALE in <sys/param.h>.  However, we take FSCALE being
  123.    defined to mean that the nlist method should be used, which is not true.  */
  124. #undef FSCALE
  125. #endif
  126.  
  127. /* Set values that are different from the defaults, which are
  128.    set a little farther down with #ifndef.  */
  129.  
  130.  
  131. /* Some shorthands.  */
  132.  
  133. #if defined (HPUX) && !defined (hpux)
  134. #define hpux
  135. #endif
  136.  
  137. #if defined(hp300) && !defined(hpux)
  138. #define MORE_BSD
  139. #endif
  140.  
  141. #if defined(ultrix) && defined(mips)
  142. #define decstation
  143. #endif
  144.  
  145. #if defined(sun) && defined(USG)
  146. #define SUNOS_5
  147. #endif
  148.  
  149. #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
  150. #define OSF_ALPHA
  151. /* XEmacs addition: */
  152. #include <sys/table.h>
  153. #endif
  154.  
  155. #if defined (__osf__) && (defined (mips) || defined (__mips__))
  156. #define OSF_MIPS
  157. #include <sys/table.h>
  158. #endif
  159.  
  160. /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
  161.    default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>.  Combine
  162.    that with a couple of other things and we'll have a unique match.  */
  163. #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
  164. #define tek4300            /* Define by emacs, but not by other users.  */
  165. #endif
  166.  
  167.  
  168. /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars.  */
  169. #ifndef LOAD_AVE_TYPE
  170.  
  171. #ifdef MORE_BSD
  172. #define LOAD_AVE_TYPE long
  173. #endif
  174.  
  175. #ifdef sun
  176. #define LOAD_AVE_TYPE long
  177. #endif
  178.  
  179. #ifdef decstation
  180. #define LOAD_AVE_TYPE long
  181. #endif
  182.  
  183. #ifdef _SEQUENT_
  184. #define LOAD_AVE_TYPE long
  185. #endif
  186.  
  187. #ifdef sgi
  188. #define LOAD_AVE_TYPE long
  189. #endif
  190.  
  191. #ifdef SVR4
  192. #define LOAD_AVE_TYPE long
  193. #endif
  194.  
  195. #ifdef sony_news
  196. #define LOAD_AVE_TYPE long
  197. #endif
  198.  
  199. #ifdef sequent
  200. #define LOAD_AVE_TYPE long
  201. #endif
  202.  
  203. #ifdef OSF_ALPHA
  204. #define LOAD_AVE_TYPE long
  205. #endif
  206.  
  207. #if defined (ardent) && defined (titan)
  208. #define LOAD_AVE_TYPE long
  209. #endif
  210.  
  211. #ifdef tek4300
  212. #define LOAD_AVE_TYPE long
  213. #endif
  214.  
  215. #endif /* No LOAD_AVE_TYPE.  */
  216.  
  217. #ifdef OSF_ALPHA
  218. /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
  219.    according to ghazi@noc.rutgers.edu.  */
  220. #undef FSCALE
  221. #define FSCALE 1024.0
  222. #endif
  223.  
  224.  
  225. #ifndef    FSCALE
  226.  
  227. /* SunOS and some others define FSCALE in sys/param.h.  */
  228.  
  229. #ifdef MORE_BSD
  230. #define FSCALE 2048.0
  231. #endif
  232.  
  233. #if defined(MIPS) || defined(SVR4) || defined(decstation)
  234. #define FSCALE 256
  235. #endif
  236.  
  237. #if defined (sgi) || defined (sequent)
  238. /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
  239.    above under #ifdef MIPS.  But we want the sgi value.  */
  240. #undef FSCALE
  241. #define    FSCALE 1000.0
  242. #endif
  243.  
  244. #if defined (ardent) && defined (titan)
  245. #define FSCALE 65536.0
  246. #endif
  247.  
  248. #ifdef tek4300
  249. #define FSCALE 100.0
  250. #endif
  251.  
  252. #endif    /* Not FSCALE.  */
  253.  
  254. #if !defined (LDAV_CVT) && defined (FSCALE)
  255. #define    LDAV_CVT(n) (((double) (n)) / FSCALE)
  256. #endif
  257.  
  258. /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters.  */
  259. #ifndef NLIST_STRUCT
  260.  
  261. /* XEmacs addition */
  262. #ifdef _AIX
  263. #define NLIST_STRUCT
  264. #endif
  265.  
  266. #ifdef MORE_BSD
  267. #define NLIST_STRUCT
  268. #endif
  269.  
  270. #ifdef sun
  271. #define NLIST_STRUCT
  272. #endif
  273.  
  274. #ifdef decstation
  275. #define NLIST_STRUCT
  276. #endif
  277.  
  278. #ifdef hpux
  279. #define NLIST_STRUCT
  280. #endif
  281.  
  282. #if defined (_SEQUENT_) || defined (sequent)
  283. #define NLIST_STRUCT
  284. #endif
  285.  
  286. #ifdef sgi
  287. #define NLIST_STRUCT
  288. #endif
  289.  
  290. #ifdef SVR4
  291. #define NLIST_STRUCT
  292. #endif
  293.  
  294. #ifdef sony_news
  295. #define NLIST_STRUCT
  296. #endif
  297.  
  298. #ifdef OSF_ALPHA
  299. #define NLIST_STRUCT
  300. #endif
  301.  
  302. #if defined (ardent) && defined (titan)
  303. #define NLIST_STRUCT
  304. #endif
  305.  
  306. #ifdef tex4300
  307. #define NLIST_STRUCT
  308. #endif
  309.  
  310. #ifdef butterfly
  311. #define NLIST_STRUCT
  312. #endif
  313.  
  314. #endif /* defined (NLIST_STRUCT) */
  315.  
  316.  
  317. #if defined(sgi) || (defined(mips) && !defined(BSD))
  318. #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
  319. #endif
  320.  
  321.  
  322. #if !defined (KERNEL_FILE) && defined (sequent)
  323. #define KERNEL_FILE "/dynix"
  324. #endif
  325.  
  326. #if !defined (KERNEL_FILE) && defined (hpux)
  327. #define KERNEL_FILE "/hp-ux"
  328. #endif
  329.  
  330. #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
  331. #define KERNEL_FILE "/unix"
  332. #endif
  333.  
  334.  
  335. #if !defined (LDAV_SYMBOL) && defined (alliant)
  336. #define LDAV_SYMBOL "_Loadavg"
  337. #endif
  338.  
  339. #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
  340. #define LDAV_SYMBOL "avenrun"
  341. #endif
  342.  
  343. #include <stdlib.h>
  344. #include <unistd.h>
  345.  
  346. #include <stdio.h>
  347.  
  348. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  349.    nlist method.  */
  350. #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
  351. #define LOAD_AVE_TYPE double
  352. #endif
  353.  
  354. #ifdef LOAD_AVE_TYPE
  355.  
  356. #ifndef VMS
  357. #ifndef NLIST_STRUCT
  358. #include <a.out.h>
  359. #else /* NLIST_STRUCT */
  360. #include <nlist.h>
  361. #endif /* NLIST_STRUCT */
  362.  
  363. #ifdef SUNOS_5
  364. #include <fcntl.h>
  365. #include <kvm.h>
  366. #endif
  367.  
  368. #ifndef KERNEL_FILE
  369. #define KERNEL_FILE "/vmunix"
  370. #endif /* KERNEL_FILE */
  371.  
  372. #ifndef LDAV_SYMBOL
  373. #define LDAV_SYMBOL "_avenrun"
  374. #endif /* LDAV_SYMBOL */
  375.  
  376. #else /* VMS */
  377.  
  378. #ifndef eunice
  379. #include <iodef.h>
  380. #include <descrip.h>
  381. #else /* eunice */
  382. #include <vms/iodef.h>
  383. #endif /* eunice */
  384. #endif /* VMS */
  385.  
  386. #ifndef LDAV_CVT
  387. #define LDAV_CVT(n) ((double) (n))
  388. #endif /* !LDAV_CVT */
  389.  
  390. #endif /* LOAD_AVE_TYPE */
  391.  
  392. #ifdef NeXT
  393. /* This undef is needed to shut up the NeXT compiler about a stupid
  394.    non-problem. */
  395. #undef index
  396. #ifdef HAVE_MACH_MACH_H
  397. #include <mach/mach.h>
  398. #else
  399. #include <mach.h>
  400. #endif
  401. #endif /* NeXT */
  402.  
  403. #ifdef sgi
  404. #include <sys/sysmp.h>
  405. #endif /* sgi */
  406.  
  407. #ifdef UMAX
  408. #include <stdio.h>
  409. #include <signal.h>
  410. #include <sys/time.h>
  411. #include <sys/wait.h>
  412. #include <sys/syscall.h>
  413.  
  414. #ifdef UMAX_43
  415. #include <machine/cpu.h>
  416. #include <inq_stats/statistics.h>
  417. #include <inq_stats/sysstats.h>
  418. #include <inq_stats/cpustats.h>
  419. #include <inq_stats/procstats.h>
  420. #else /* Not UMAX_43.  */
  421. #include <sys/sysdefs.h>
  422. #include <sys/statistics.h>
  423. #include <sys/sysstats.h>
  424. #include <sys/cpudefs.h>
  425. #include <sys/cpustats.h>
  426. #include <sys/procstats.h>
  427. #endif /* Not UMAX_43.  */
  428. #endif /* UMAX */
  429.  
  430. #ifdef DGUX
  431. #include <sys/dg_sys_info.h>
  432. #endif
  433.  
  434. /* XEmacs addition */
  435. #if !defined(LDAV_DONE) && defined(HPUX) && !defined (HPUX_PRE_8_0)
  436. #include <sys/pstat.h>
  437. #endif
  438.  
  439. #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
  440. #include <fcntl.h>
  441. #else
  442. #include <sys/file.h>
  443. #endif
  444.  
  445. /* Avoid static vars inside a function since in HPUX they dump as pure.  */
  446.  
  447. #ifdef NeXT
  448. static processor_set_t default_set;
  449. static int getloadavg_initialized;
  450. #endif /* NeXT */
  451.  
  452. #ifdef UMAX
  453. static unsigned int cpus = 0;
  454. static unsigned int samples;
  455. #endif /* UMAX */
  456.  
  457. #ifdef DGUX
  458. static struct dg_sys_info_load_info load_info;    /* what-a-mouthful! */
  459. #endif /* DGUX */
  460.  
  461. #ifdef LOAD_AVE_TYPE
  462. #if defined (VMS) || !defined (SUNOS_5)
  463. /* File descriptor open to /dev/kmem or VMS load ave driver.  */
  464. static int channel;
  465. #endif /* VMS || !SUNOS_5 */
  466. /* Nonzero iff channel is valid.  */
  467. static int getloadavg_initialized;
  468. /* Offset in kmem to seek to read load average, or 0 means invalid.  */
  469. static long offset;
  470.  
  471. #if !defined(VMS) && !defined(sgi)
  472. static struct nlist nl[2];
  473. #endif /* Not VMS or sgi */
  474.  
  475. #ifdef SUNOS_5
  476. static kvm_t *kd;
  477. #endif /* SUNOS_5 */
  478.  
  479. #endif /* LOAD_AVE_TYPE */
  480.  
  481. /* Put the 1 minute, 5 minute and 15 minute load averages
  482.    into the first NELEM elements of LOADAVG.
  483.    Return the number written (never more than 3, but may be less than NELEM),
  484.    or -1 if an error occurred.  */
  485.  
  486. int
  487. getloadavg (double loadavg[], int nelem)
  488. {
  489.   int elem = 0;            /* Return value.  */
  490.  
  491. #ifdef NO_GET_LOAD_AVG
  492. #define LDAV_DONE
  493.   /* Set errno to zero to indicate that there was no particular error;
  494.      this function just can't work at all on this system.  */
  495.   errno = 0;
  496.   elem = -1;
  497. #endif
  498.  
  499. #if !defined (LDAV_DONE) && defined (__linux__)
  500. #define LDAV_DONE
  501. #undef LOAD_AVE_TYPE
  502.  
  503. #ifndef LINUX_LDAV_FILE
  504. #define LINUX_LDAV_FILE "/proc/loadavg"
  505. #endif
  506.  
  507.   char ldavgbuf[40];
  508.   double load_ave[3];
  509.   int fd, count;
  510.  
  511.   fd = open (LINUX_LDAV_FILE, O_RDONLY);
  512.   if (fd == -1)
  513.     return -1;
  514.   count = read (fd, ldavgbuf, 40);
  515.   (void) close (fd);
  516.   if (count <= 0)
  517.     return -1;
  518.  
  519.   count = sscanf (ldavgbuf, "%lf %lf %lf",
  520.           &load_ave[0], &load_ave[1], &load_ave[2]);
  521.   if (count < 1)
  522.     return -1;
  523.  
  524.   for (elem = 0; elem < nelem && elem < count; elem++)
  525.     loadavg[elem] = load_ave[elem];
  526.  
  527.   return elem;
  528.  
  529. #endif /* __linux__ */
  530.  
  531. #if !defined (LDAV_DONE) && defined (__NetBSD__)
  532. #define LDAV_DONE
  533. #undef LOAD_AVE_TYPE
  534.  
  535. #ifndef NETBSD_LDAV_FILE
  536. #define NETBSD_LDAV_FILE "/kern/loadavg"
  537. #endif
  538.  
  539.   unsigned long int load_ave[3], scale;
  540.   int count;
  541.   FILE *fp;
  542.  
  543.   fp = fopen (NETBSD_LDAV_FILE, "r");
  544.   if (fp == NULL)
  545.     return -1;
  546.   count = fscanf (fp, "%lu %lu %lu %lu\n",
  547.           &load_ave[0], &load_ave[1], &load_ave[2],
  548.           &scale);
  549.   (void) fclose (fp);
  550.   if (count != 4)
  551.     return -1;
  552.  
  553.   for (elem = 0; elem < nelem; elem++)
  554.     loadavg[elem] = (double) load_ave[elem] / (double) scale;
  555.  
  556.   return elem;
  557.  
  558. #endif /* __NetBSD__ */
  559.  
  560. #if !defined (LDAV_DONE) && defined (NeXT)
  561. #define LDAV_DONE
  562.   /* The NeXT code was adapted from iscreen 3.2.  */
  563.  
  564.   host_t host;
  565.   struct processor_set_basic_info info;
  566.   unsigned info_count;
  567.  
  568.   if (nelem > 1)
  569.     {
  570.       /* We only know how to get the 1-minute average for this system.  */
  571.       errno = EINVAL;
  572.       return -1;
  573.     }
  574.  
  575.   if (!getloadavg_initialized)
  576.     {
  577.       if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  578.     getloadavg_initialized = 1;
  579.     }
  580.  
  581.   if (getloadavg_initialized)
  582.     {
  583.       info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  584.       if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  585.                  (processor_set_info_t) &info, &info_count)
  586.       != KERN_SUCCESS)
  587.     getloadavg_initialized = 0;
  588.       else
  589.     {
  590.       if (nelem > 0)
  591.         loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  592.     }
  593.     }
  594.  
  595.   if (!getloadavg_initialized)
  596.     return -1;
  597. #endif /* NeXT */
  598.  
  599. #if !defined (LDAV_DONE) && defined (UMAX)
  600. #define LDAV_DONE
  601. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  602.    have a /dev/kmem.  Information about the workings of the running kernel
  603.    can be gathered with inq_stats system calls.
  604.    We only know how to get the 1-minute average for this system.  */
  605.  
  606.   struct proc_summary proc_sum_data;
  607.   struct stat_descr proc_info;
  608.   double load;
  609.   unsigned int i, j;
  610.  
  611.   if (cpus == 0)
  612.     {
  613.       unsigned int c, i;
  614.       struct cpu_config conf;
  615.       struct stat_descr desc;
  616.  
  617.       desc.sd_next = 0;
  618.       desc.sd_subsys = SUBSYS_CPU;
  619.       desc.sd_type = CPUTYPE_CONFIG;
  620.       desc.sd_addr = (char *) &conf;
  621.       desc.sd_size = sizeof conf;
  622.  
  623.       if (inq_stats (1, &desc))
  624.     return -1;
  625.  
  626.       c = 0;
  627.       for (i = 0; i < conf.config_maxclass; ++i)
  628.     {
  629.       struct class_stats stats;
  630.       bzero ((char *) &stats, sizeof stats);
  631.  
  632.       desc.sd_type = CPUTYPE_CLASS;
  633.       desc.sd_objid = i;
  634.       desc.sd_addr = (char *) &stats;
  635.       desc.sd_size = sizeof stats;
  636.  
  637.       if (inq_stats (1, &desc))
  638.         return -1;
  639.  
  640.       c += stats.class_numcpus;
  641.     }
  642.       cpus = c;
  643.       samples = cpus < 2 ? 3 : (2 * cpus / 3);
  644.     }
  645.  
  646.   proc_info.sd_next = 0;
  647.   proc_info.sd_subsys = SUBSYS_PROC;
  648.   proc_info.sd_type = PROCTYPE_SUMMARY;
  649.   proc_info.sd_addr = (char *) &proc_sum_data;
  650.   proc_info.sd_size = sizeof (struct proc_summary);
  651.   proc_info.sd_sizeused = 0;
  652.  
  653.   if (inq_stats (1, &proc_info) != 0)
  654.     return -1;
  655.  
  656.   load = proc_sum_data.ps_nrunnable;
  657.   j = 0;
  658.   for (i = samples - 1; i > 0; --i)
  659.     {
  660.       load += proc_sum_data.ps_nrun[j];
  661.       if (j++ == PS_NRUNSIZE)
  662.     j = 0;
  663.     }
  664.  
  665.   if (nelem > 0)
  666.     loadavg[elem++] = load / samples / cpus;
  667. #endif /* UMAX */
  668.  
  669. #if !defined (LDAV_DONE) && defined (DGUX)
  670. #define LDAV_DONE
  671.   /* This call can return -1 for an error, but with good args
  672.      it's not supposed to fail.  The first argument is for no
  673.      apparent reason of type `long int *'.  */
  674.   dg_sys_info ((long int *) &load_info,
  675.            DG_SYS_INFO_LOAD_INFO_TYPE,
  676.            DG_SYS_INFO_LOAD_VERSION_0);
  677.  
  678.   if (nelem > 0)
  679.     loadavg[elem++] = load_info.one_minute;
  680.   if (nelem > 1)
  681.     loadavg[elem++] = load_info.five_minute;
  682.   if (nelem > 2)
  683.     loadavg[elem++] = load_info.fifteen_minute;
  684. #endif /* DGUX */
  685.  
  686. #if !defined (LDAV_DONE) && defined (apollo)
  687. #define LDAV_DONE
  688. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  689.  
  690.    This system call is not documented.  The load average is obtained as
  691.    three long integers, for the load average over the past minute,
  692.    five minutes, and fifteen minutes.  Each value is a scaled integer,
  693.    with 16 bits of integer part and 16 bits of fraction part.
  694.  
  695.    I'm not sure which operating system first supported this system call,
  696.    but I know that SR10.2 supports it.  */
  697.  
  698.   extern void proc1_$get_loadav ();
  699.   unsigned long load_ave[3];
  700.  
  701.   proc1_$get_loadav (load_ave);
  702.  
  703.   if (nelem > 0)
  704.     loadavg[elem++] = load_ave[0] / 65536.0;
  705.   if (nelem > 1)
  706.     loadavg[elem++] = load_ave[1] / 65536.0;
  707.   if (nelem > 2)
  708.     loadavg[elem++] = load_ave[2] / 65536.0;
  709. #endif /* apollo */
  710.  
  711. #if !defined (LDAV_DONE) && (defined (OSF_MIPS) || defined (OSF_ALPHA))
  712. #define LDAV_DONE
  713.  
  714.   struct tbl_loadavg load_ave;
  715.   table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  716.   while (elem < nelem && elem < 3)
  717.     {
  718.       loadavg[elem++] = (load_ave.tl_lscale == 0
  719.        ? load_ave.tl_avenrun.d[0]
  720.        : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
  721.     }
  722. #endif    /* OSF_MIPS || OSF_ALPHA */
  723.  
  724. #if !defined (LDAV_DONE) && defined (VMS)
  725.   /* VMS specific code -- read from the Load Ave driver.  */
  726.  
  727.   LOAD_AVE_TYPE load_ave[3];
  728.   static int getloadavg_initialized = 0;
  729. #ifdef eunice
  730.   struct
  731.   {
  732.     int dsc$w_length;
  733.     char *dsc$a_pointer;
  734.   } descriptor;
  735. #endif
  736.  
  737.   /* Ensure that there is a channel open to the load ave device.  */
  738.   if (!getloadavg_initialized)
  739.     {
  740.       /* Attempt to open the channel.  */
  741. #ifdef eunice
  742.       descriptor.dsc$w_length = 18;
  743.       descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
  744. #else
  745.       $DESCRIPTOR (descriptor, "LAV0:");
  746. #endif
  747.       if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  748.     getloadavg_initialized = 1;
  749.     }
  750.  
  751.   /* Read the load average vector.  */
  752.   if (getloadavg_initialized
  753.       && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  754.              load_ave, 12, 0, 0, 0, 0) & 1))
  755.     {
  756.       sys$dassgn (channel);
  757.       getloadavg_initialized = 0;
  758.     }
  759.  
  760.   if (!getloadavg_initialized)
  761.     return -1;
  762. #endif /* VMS */
  763.  
  764. #if !defined (LDAV_DONE) && defined (HPUX) && !defined (HPUX_PRE_8_0)
  765. #define LDAV_DONE
  766.   /*
  767.    * This is totally undocumented, and is not guaranteed to work, but
  768.    * mayhap it might ....  If it does work, it will work only on HP-UX
  769.    * 8.0 or later.  -- Darryl Okahata <darrylo@sr.hp.com>
  770.    */
  771. #undef LOAD_AVE_TYPE        /* Make sure these don't exist. */
  772. #undef LOAD_AVE_CVT
  773. #undef LDAV_SYMBOL
  774.   struct pst_dynamic    procinfo;
  775.   union pstun        statbuf;
  776.  
  777.   statbuf.pst_dynamic = &procinfo;
  778.   if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
  779.     return (-1);
  780.   loadavg[elem++] = procinfo.psd_avg_1_min;
  781.   loadavg[elem++] = procinfo.psd_avg_5_min;
  782.   loadavg[elem++] = procinfo.psd_avg_15_min;
  783. #endif    /* HPUX */
  784.  
  785. #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
  786.  
  787.   /* UNIX-specific code -- read the average from /dev/kmem.  */
  788.  
  789. #define LDAV_PRIVILEGED        /* This code requires special installation.  */
  790.  
  791.   LOAD_AVE_TYPE load_ave[3];
  792.  
  793.   /* Get the address of LDAV_SYMBOL.  */
  794.   if (offset == 0)
  795.     {
  796. #ifndef sgi
  797. #ifndef NLIST_STRUCT
  798.       strcpy (nl[0].n_name, LDAV_SYMBOL);
  799.       strcpy (nl[1].n_name, "");
  800. #else /* NLIST_STRUCT */
  801. #ifdef NLIST_NAME_UNION
  802.       nl[0].n_un.n_name = LDAV_SYMBOL;
  803.       nl[1].n_un.n_name = 0;
  804. #else /* not NLIST_NAME_UNION */
  805.       nl[0].n_name = LDAV_SYMBOL;
  806.       nl[1].n_name = 0;
  807. #endif /* not NLIST_NAME_UNION */
  808. #endif /* NLIST_STRUCT */
  809.  
  810. #ifndef SUNOS_5
  811.       if (nlist (KERNEL_FILE, nl) >= 0)
  812.     /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i.  */
  813.     {
  814. #ifdef FIXUP_KERNEL_SYMBOL_ADDR
  815.       FIXUP_KERNEL_SYMBOL_ADDR (nl);
  816. #endif
  817.       offset = nl[0].n_value;
  818.     }
  819. #endif  /* !SUNOS_5 */
  820. #else /* sgi */
  821.       int ldav_off;
  822.  
  823.       ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  824.       if (ldav_off != -1)
  825.     offset = (long) ldav_off & 0x7fffffff;
  826. #endif /* sgi */
  827.     }
  828.  
  829.   /* Make sure we have /dev/kmem open.  */
  830.   if (!getloadavg_initialized)
  831.     {
  832. #ifndef SUNOS_5
  833.       channel = open ("/dev/kmem", 0);
  834.       if (channel >= 0)
  835.     getloadavg_initialized = 1;
  836. #else /* SUNOS_5 */
  837.       /* We pass 0 for the kernel, corefile, and swapfile names
  838.      to use the currently running kernel.  */
  839.       kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  840.       if (kd != 0) 
  841.     {
  842.       /* nlist the currently running kernel.  */
  843.       kvm_nlist (kd, nl);
  844.       offset = nl[0].n_value;
  845.       getloadavg_initialized = 1;
  846.     }
  847. #endif /* SUNOS_5 */
  848.     }
  849.  
  850.   /* If we can, get the load average values.  */
  851.   if (offset && getloadavg_initialized)
  852.     {
  853.       /* Try to read the load.  */
  854. #ifndef SUNOS_5
  855.       if (lseek (channel, offset, 0) == -1L
  856.       || read (channel, (char *) load_ave, sizeof (load_ave))
  857.       != sizeof (load_ave))
  858.     {
  859.       close (channel);
  860.       getloadavg_initialized = 0;
  861.     }
  862. #else  /* SUNOS_5 */
  863.       if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  864.       != sizeof (load_ave))
  865.         {
  866.           kvm_close (kd);
  867.           getloadavg_initialized = 0;
  868.     }
  869. #endif /* SUNOS_5 */
  870.     }
  871.  
  872.   if (offset == 0 || !getloadavg_initialized)
  873.     return -1;
  874. #endif /* LOAD_AVE_TYPE and not VMS */
  875.  
  876. #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS.  */
  877.   if (nelem > 0)
  878.     loadavg[elem++] = LDAV_CVT (load_ave[0]);
  879.   if (nelem > 1)
  880.     loadavg[elem++] = LDAV_CVT (load_ave[1]);
  881.   if (nelem > 2)
  882.     loadavg[elem++] = LDAV_CVT (load_ave[2]);
  883.  
  884. #define LDAV_DONE
  885. #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
  886.  
  887. #ifdef LDAV_DONE
  888.   return elem;
  889. #else
  890.   /* Set errno to zero to indicate that there was no particular error;
  891.      this function just can't work at all on this system.  */
  892.   errno = 0;
  893.   return -1;
  894. #endif
  895. }
  896.  
  897. #endif /* ! HAVE_GETLOADAVG */
  898.  
  899. #ifdef TEST
  900. void
  901. main (int argc, char **argv)
  902. {
  903.   int naptime = 0;
  904.  
  905.   if (argc > 1)
  906.     naptime = atoi (argv[1]);
  907.  
  908.   while (1)
  909.     {
  910.       double avg[3];
  911.       int loads;
  912.  
  913.       errno = 0;        /* Don't be misled if it doesn't set errno.  */
  914.       loads = getloadavg (avg, 3);
  915.       if (loads == -1)
  916.     {
  917.       perror ("Error getting load average");
  918.       exit (1);
  919.     }
  920.       if (loads > 0)
  921.     printf ("1-minute: %f  ", avg[0]);
  922.       if (loads > 1)
  923.     printf ("5-minute: %f  ", avg[1]);
  924.       if (loads > 2)
  925.     printf ("15-minute: %f  ", avg[2]);
  926.       if (loads > 0)
  927.     putchar ('\n');
  928.  
  929.       if (naptime == 0)
  930.     break;
  931.       sleep (naptime);
  932.     }
  933.  
  934.   exit (0);
  935. }
  936. #endif /* TEST */
  937.