getloadavg.c 26 KB

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