getloadavg.c 28 KB

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