getloadavg.c 26 KB

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