getloadavg.c 26 KB

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