getloadavg.c 28 KB

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