getloadavg.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052
  1. /* Get the system load averages.
  2. Copyright (C) 1985, 1986, 1987, 1988, 1989, 1991, 1992, 1993, 1994,
  3. 1995, 1997, 1999, 2000, 2003, 2004, 2005, 2006, 2007, 2008, 2009 Free
  4. Software Foundation, Inc.
  5. NOTE: The canonical source of this file is maintained with gnulib.
  6. Bugs can be reported to bug-gnulib@gnu.org.
  7. This program is free software: you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  17. /* Compile-time symbols that this file uses:
  18. HAVE_PSTAT_GETDYNAMIC Define this if your system has the
  19. pstat_getdynamic function. I think it
  20. is unique to HPUX9. The best way to get the
  21. definition is through the AC_FUNC_GETLOADAVG
  22. macro that comes with autoconf 2.13 or newer.
  23. If that isn't an option, then just put
  24. AC_CHECK_FUNCS(pstat_getdynamic) in your
  25. configure.in file.
  26. HAVE_LIBPERFSTAT Define this if your system has the
  27. perfstat_cpu_total function in libperfstat (AIX).
  28. FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
  29. KERNEL_FILE Name of the kernel file to nlist.
  30. LDAV_CVT() Scale the load average from the kernel.
  31. Returns a double.
  32. LDAV_SYMBOL Name of kernel symbol giving load average.
  33. LOAD_AVE_TYPE Type of the load average array in the kernel.
  34. Must be defined unless one of
  35. apollo, DGUX, NeXT, or UMAX is defined;
  36. or we have libkstat;
  37. otherwise, no load average is available.
  38. HAVE_NLIST_H nlist.h is available. NLIST_STRUCT defaults
  39. to this.
  40. NLIST_STRUCT Include nlist.h, not a.out.h, and
  41. the nlist n_name element is a pointer,
  42. not an array.
  43. HAVE_STRUCT_NLIST_N_UN_N_NAME `n_un.n_name' is member of `struct nlist'.
  44. LINUX_LDAV_FILE [__linux__, __CYGWIN__]: File containing
  45. load averages.
  46. Specific system predefines this file uses, aside from setting
  47. default values if not emacs:
  48. apollo
  49. BSD Real BSD, not just BSD-like.
  50. convex
  51. DGUX
  52. eunice UNIX emulator under VMS.
  53. hpux
  54. __MSDOS__ No-op for MSDOS.
  55. NeXT
  56. sgi
  57. sequent Sequent Dynix 3.x.x (BSD)
  58. _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
  59. sony_news NEWS-OS (works at least for 4.1C)
  60. UMAX
  61. UMAX4_3
  62. VMS
  63. WINDOWS32 No-op for Windows95/NT.
  64. __linux__ Linux: assumes /proc file system mounted.
  65. Support from Michael K. Johnson.
  66. __CYGWIN__ Cygwin emulates linux /proc/loadavg.
  67. __NetBSD__ NetBSD: assumes /kern file system mounted.
  68. In addition, to avoid nesting many #ifdefs, we internally set
  69. LDAV_DONE to indicate that the load average has been computed.
  70. We also #define LDAV_PRIVILEGED if a program will require
  71. special installation to be able to call getloadavg. */
  72. /* "configure" defines CONFIGURING_GETLOADAVG to sidestep problems
  73. with partially-configured source directories. */
  74. #ifndef CONFIGURING_GETLOADAVG
  75. # include <config.h>
  76. # include <stdbool.h>
  77. #endif
  78. /* Specification. */
  79. #include <stdlib.h>
  80. #include <errno.h>
  81. #include <stdio.h>
  82. /* Exclude all the code except the test program at the end
  83. if the system has its own `getloadavg' function. */
  84. #ifndef HAVE_GETLOADAVG
  85. # include <sys/types.h>
  86. /* Both the Emacs and non-Emacs sections want this. Some
  87. configuration files' definitions for the LOAD_AVE_CVT macro (like
  88. sparc.h's) use macros like FSCALE, defined here. */
  89. # if defined (unix) || defined (__unix)
  90. # include <sys/param.h>
  91. # endif
  92. # include "c-strtod.h"
  93. # include "cloexec.h"
  94. # include "intprops.h"
  95. # include "xalloc.h"
  96. /* The existing Emacs configuration files define a macro called
  97. LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  98. returns the load average multiplied by 100. What we actually want
  99. is a macro called LDAV_CVT, which returns the load average as an
  100. unmultiplied double.
  101. For backwards compatibility, we'll define LDAV_CVT in terms of
  102. LOAD_AVE_CVT, but future machine config files should just define
  103. LDAV_CVT directly. */
  104. # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
  105. # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  106. # endif
  107. # if !defined (BSD) && defined (ultrix)
  108. /* Ultrix behaves like BSD on Vaxen. */
  109. # define BSD
  110. # endif
  111. # ifdef NeXT
  112. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  113. conflicts with the definition understood in this file, that this
  114. really is BSD. */
  115. # undef BSD
  116. /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
  117. defined to mean that the nlist method should be used, which is not true. */
  118. # undef FSCALE
  119. # endif
  120. /* Same issues as for NeXT apply to the HURD-based GNU system. */
  121. # ifdef __GNU__
  122. # undef BSD
  123. # undef FSCALE
  124. # endif /* __GNU__ */
  125. /* Set values that are different from the defaults, which are
  126. set a little farther down with #ifndef. */
  127. /* Some shorthands. */
  128. # if defined (HPUX) && !defined (hpux)
  129. # define hpux
  130. # endif
  131. # if defined (__hpux) && !defined (hpux)
  132. # define hpux
  133. # endif
  134. # if defined (__sun) && !defined (sun)
  135. # define sun
  136. # endif
  137. # if defined (hp300) && !defined (hpux)
  138. # define MORE_BSD
  139. # endif
  140. # if defined (ultrix) && defined (mips)
  141. # define decstation
  142. # endif
  143. # if defined (__SVR4) && !defined (SVR4)
  144. # define SVR4
  145. # endif
  146. # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
  147. # define SUNOS_5
  148. # endif
  149. # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
  150. # define OSF_ALPHA
  151. # include <sys/mbuf.h>
  152. # include <sys/socket.h>
  153. # include <net/route.h>
  154. # include <sys/table.h>
  155. /* Tru64 4.0D's table.h redefines sys */
  156. # undef sys
  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. # if defined _AIX && ! defined HAVE_LIBPERFSTAT
  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. # if defined _AIX && !defined HAVE_LIBPERFSTAT
  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) && !defined(HAVE_LIBPERFSTAT)))
  276. # define LDAV_SYMBOL "avenrun"
  277. # endif
  278. # include <unistd.h>
  279. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  280. nlist method. */
  281. # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
  282. # define LOAD_AVE_TYPE double
  283. # endif
  284. # ifdef LOAD_AVE_TYPE
  285. # ifndef __VMS
  286. # ifndef __linux__
  287. # ifndef NLIST_STRUCT
  288. # include <a.out.h>
  289. # else /* NLIST_STRUCT */
  290. # include <nlist.h>
  291. # endif /* NLIST_STRUCT */
  292. # ifdef SUNOS_5
  293. # include <fcntl.h>
  294. # include <kvm.h>
  295. # include <kstat.h>
  296. # endif
  297. # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
  298. # include <sys/pstat.h>
  299. # endif
  300. # ifndef KERNEL_FILE
  301. # define KERNEL_FILE "/vmunix"
  302. # endif /* KERNEL_FILE */
  303. # ifndef LDAV_SYMBOL
  304. # define LDAV_SYMBOL "_avenrun"
  305. # endif /* LDAV_SYMBOL */
  306. # endif /* __linux__ */
  307. # else /* __VMS */
  308. # ifndef eunice
  309. # include <iodef.h>
  310. # include <descrip.h>
  311. # else /* eunice */
  312. # include <vms/iodef.h>
  313. # endif /* eunice */
  314. # endif /* __VMS */
  315. # ifndef LDAV_CVT
  316. # define LDAV_CVT(n) ((double) (n))
  317. # endif /* !LDAV_CVT */
  318. # endif /* LOAD_AVE_TYPE */
  319. # if defined HAVE_LIBPERFSTAT
  320. # include <sys/protosw.h>
  321. # include <libperfstat.h>
  322. # include <sys/proc.h>
  323. # ifndef SBITS
  324. # define SBITS 16
  325. # endif
  326. # endif
  327. # if defined (__GNU__) && !defined (NeXT)
  328. /* Note that NeXT Openstep defines __GNU__ even though it should not. */
  329. /* GNU system acts much like NeXT, for load average purposes,
  330. but not exactly. */
  331. # define NeXT
  332. # define host_self mach_host_self
  333. # endif
  334. # ifdef NeXT
  335. # ifdef HAVE_MACH_MACH_H
  336. # include <mach/mach.h>
  337. # else
  338. # include <mach.h>
  339. # endif
  340. # endif /* NeXT */
  341. # ifdef sgi
  342. # include <sys/sysmp.h>
  343. # endif /* sgi */
  344. # ifdef UMAX
  345. # include <signal.h>
  346. # include <sys/time.h>
  347. # include <sys/wait.h>
  348. # include <sys/syscall.h>
  349. # ifdef UMAX_43
  350. # include <machine/cpu.h>
  351. # include <inq_stats/statistics.h>
  352. # include <inq_stats/sysstats.h>
  353. # include <inq_stats/cpustats.h>
  354. # include <inq_stats/procstats.h>
  355. # else /* Not UMAX_43. */
  356. # include <sys/sysdefs.h>
  357. # include <sys/statistics.h>
  358. # include <sys/sysstats.h>
  359. # include <sys/cpudefs.h>
  360. # include <sys/cpustats.h>
  361. # include <sys/procstats.h>
  362. # endif /* Not UMAX_43. */
  363. # endif /* UMAX */
  364. # ifdef DGUX
  365. # include <sys/dg_sys_info.h>
  366. # endif
  367. # include "fcntl--.h"
  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 bool 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. /* True iff channel is valid. */
  384. static bool 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
  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 HAVE_LIBPERFSTAT
  463. # define LDAV_DONE
  464. # undef LOAD_AVE_TYPE
  465. /* Use perfstat_cpu_total because we don't have to be root. */
  466. {
  467. perfstat_cpu_total_t cpu_stats;
  468. int result = perfstat_cpu_total (NULL, &cpu_stats, sizeof cpu_stats, 1);
  469. if (result == -1)
  470. return result;
  471. loadavg[0] = cpu_stats.loadavg[0] / (double)(1 << SBITS);
  472. loadavg[1] = cpu_stats.loadavg[1] / (double)(1 << SBITS);
  473. loadavg[2] = cpu_stats.loadavg[2] / (double)(1 << SBITS);
  474. elem = 3;
  475. }
  476. # endif
  477. # if !defined (LDAV_DONE) && (defined (__linux__) || defined (__CYGWIN__))
  478. # define LDAV_DONE
  479. # undef LOAD_AVE_TYPE
  480. # ifndef LINUX_LDAV_FILE
  481. # define LINUX_LDAV_FILE "/proc/loadavg"
  482. # endif
  483. char ldavgbuf[3 * (INT_STRLEN_BOUND (int) + sizeof ".00 ")];
  484. char const *ptr = ldavgbuf;
  485. int fd, count;
  486. fd = open (LINUX_LDAV_FILE, O_RDONLY);
  487. if (fd == -1)
  488. return -1;
  489. count = read (fd, ldavgbuf, sizeof ldavgbuf - 1);
  490. (void) close (fd);
  491. if (count <= 0)
  492. return -1;
  493. ldavgbuf[count] = '\0';
  494. for (elem = 0; elem < nelem; elem++)
  495. {
  496. char *endptr;
  497. double d;
  498. errno = 0;
  499. d = c_strtod (ptr, &endptr);
  500. if (ptr == endptr || (d == 0 && errno != 0))
  501. {
  502. if (elem == 0)
  503. return -1;
  504. break;
  505. }
  506. loadavg[elem] = d;
  507. ptr = endptr;
  508. }
  509. return elem;
  510. # endif /* __linux__ || __CYGWIN__ */
  511. # if !defined (LDAV_DONE) && defined (__NetBSD__)
  512. # define LDAV_DONE
  513. # undef LOAD_AVE_TYPE
  514. # ifndef NETBSD_LDAV_FILE
  515. # define NETBSD_LDAV_FILE "/kern/loadavg"
  516. # endif
  517. unsigned long int load_ave[3], scale;
  518. int count;
  519. FILE *fp;
  520. fp = fopen (NETBSD_LDAV_FILE, "r");
  521. if (fp == NULL)
  522. return -1;
  523. count = fscanf (fp, "%lu %lu %lu %lu\n",
  524. &load_ave[0], &load_ave[1], &load_ave[2],
  525. &scale);
  526. (void) fclose (fp);
  527. if (count != 4)
  528. return -1;
  529. for (elem = 0; elem < nelem; elem++)
  530. loadavg[elem] = (double) load_ave[elem] / (double) scale;
  531. return elem;
  532. # endif /* __NetBSD__ */
  533. # if !defined (LDAV_DONE) && defined (NeXT)
  534. # define LDAV_DONE
  535. /* The NeXT code was adapted from iscreen 3.2. */
  536. host_t host;
  537. struct processor_set_basic_info info;
  538. unsigned int info_count;
  539. /* We only know how to get the 1-minute average for this system,
  540. so even if the caller asks for more than 1, we only return 1. */
  541. if (!getloadavg_initialized)
  542. {
  543. if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  544. getloadavg_initialized = true;
  545. }
  546. if (getloadavg_initialized)
  547. {
  548. info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  549. if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  550. (processor_set_info_t) &info, &info_count)
  551. != KERN_SUCCESS)
  552. getloadavg_initialized = false;
  553. else
  554. {
  555. if (nelem > 0)
  556. loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  557. }
  558. }
  559. if (!getloadavg_initialized)
  560. return -1;
  561. # endif /* NeXT */
  562. # if !defined (LDAV_DONE) && defined (UMAX)
  563. # define LDAV_DONE
  564. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  565. have a /dev/kmem. Information about the workings of the running kernel
  566. can be gathered with inq_stats system calls.
  567. We only know how to get the 1-minute average for this system. */
  568. struct proc_summary proc_sum_data;
  569. struct stat_descr proc_info;
  570. double load;
  571. register unsigned int i, j;
  572. if (cpus == 0)
  573. {
  574. register unsigned int c, i;
  575. struct cpu_config conf;
  576. struct stat_descr desc;
  577. desc.sd_next = 0;
  578. desc.sd_subsys = SUBSYS_CPU;
  579. desc.sd_type = CPUTYPE_CONFIG;
  580. desc.sd_addr = (char *) &conf;
  581. desc.sd_size = sizeof conf;
  582. if (inq_stats (1, &desc))
  583. return -1;
  584. c = 0;
  585. for (i = 0; i < conf.config_maxclass; ++i)
  586. {
  587. struct class_stats stats;
  588. bzero ((char *) &stats, sizeof stats);
  589. desc.sd_type = CPUTYPE_CLASS;
  590. desc.sd_objid = i;
  591. desc.sd_addr = (char *) &stats;
  592. desc.sd_size = sizeof stats;
  593. if (inq_stats (1, &desc))
  594. return -1;
  595. c += stats.class_numcpus;
  596. }
  597. cpus = c;
  598. samples = cpus < 2 ? 3 : (2 * cpus / 3);
  599. }
  600. proc_info.sd_next = 0;
  601. proc_info.sd_subsys = SUBSYS_PROC;
  602. proc_info.sd_type = PROCTYPE_SUMMARY;
  603. proc_info.sd_addr = (char *) &proc_sum_data;
  604. proc_info.sd_size = sizeof (struct proc_summary);
  605. proc_info.sd_sizeused = 0;
  606. if (inq_stats (1, &proc_info) != 0)
  607. return -1;
  608. load = proc_sum_data.ps_nrunnable;
  609. j = 0;
  610. for (i = samples - 1; i > 0; --i)
  611. {
  612. load += proc_sum_data.ps_nrun[j];
  613. if (j++ == PS_NRUNSIZE)
  614. j = 0;
  615. }
  616. if (nelem > 0)
  617. loadavg[elem++] = load / samples / cpus;
  618. # endif /* UMAX */
  619. # if !defined (LDAV_DONE) && defined (DGUX)
  620. # define LDAV_DONE
  621. /* This call can return -1 for an error, but with good args
  622. it's not supposed to fail. The first argument is for no
  623. apparent reason of type `long int *'. */
  624. dg_sys_info ((long int *) &load_info,
  625. DG_SYS_INFO_LOAD_INFO_TYPE,
  626. DG_SYS_INFO_LOAD_VERSION_0);
  627. if (nelem > 0)
  628. loadavg[elem++] = load_info.one_minute;
  629. if (nelem > 1)
  630. loadavg[elem++] = load_info.five_minute;
  631. if (nelem > 2)
  632. loadavg[elem++] = load_info.fifteen_minute;
  633. # endif /* DGUX */
  634. # if !defined (LDAV_DONE) && defined (apollo)
  635. # define LDAV_DONE
  636. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  637. This system call is not documented. The load average is obtained as
  638. three long integers, for the load average over the past minute,
  639. five minutes, and fifteen minutes. Each value is a scaled integer,
  640. with 16 bits of integer part and 16 bits of fraction part.
  641. I'm not sure which operating system first supported this system call,
  642. but I know that SR10.2 supports it. */
  643. extern void proc1_$get_loadav ();
  644. unsigned long load_ave[3];
  645. proc1_$get_loadav (load_ave);
  646. if (nelem > 0)
  647. loadavg[elem++] = load_ave[0] / 65536.0;
  648. if (nelem > 1)
  649. loadavg[elem++] = load_ave[1] / 65536.0;
  650. if (nelem > 2)
  651. loadavg[elem++] = load_ave[2] / 65536.0;
  652. # endif /* apollo */
  653. # if !defined (LDAV_DONE) && defined (OSF_MIPS)
  654. # define LDAV_DONE
  655. struct tbl_loadavg load_ave;
  656. table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  657. loadavg[elem++]
  658. = (load_ave.tl_lscale == 0
  659. ? load_ave.tl_avenrun.d[0]
  660. : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
  661. # endif /* OSF_MIPS */
  662. # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
  663. # define LDAV_DONE
  664. /* A faithful emulation is going to have to be saved for a rainy day. */
  665. for ( ; elem < nelem; elem++)
  666. {
  667. loadavg[elem] = 0.0;
  668. }
  669. # endif /* __MSDOS__ || WINDOWS32 */
  670. # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
  671. # define LDAV_DONE
  672. struct tbl_loadavg load_ave;
  673. table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  674. for (elem = 0; elem < nelem; elem++)
  675. loadavg[elem]
  676. = (load_ave.tl_lscale == 0
  677. ? load_ave.tl_avenrun.d[elem]
  678. : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
  679. # endif /* OSF_ALPHA */
  680. # if ! defined LDAV_DONE && defined __VMS
  681. /* VMS specific code -- read from the Load Ave driver. */
  682. LOAD_AVE_TYPE load_ave[3];
  683. static bool getloadavg_initialized;
  684. # ifdef eunice
  685. struct
  686. {
  687. int dsc$w_length;
  688. char *dsc$a_pointer;
  689. } descriptor;
  690. # endif
  691. /* Ensure that there is a channel open to the load ave device. */
  692. if (!getloadavg_initialized)
  693. {
  694. /* Attempt to open the channel. */
  695. # ifdef eunice
  696. descriptor.dsc$w_length = 18;
  697. descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
  698. # else
  699. $DESCRIPTOR (descriptor, "LAV0:");
  700. # endif
  701. if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  702. getloadavg_initialized = true;
  703. }
  704. /* Read the load average vector. */
  705. if (getloadavg_initialized
  706. && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  707. load_ave, 12, 0, 0, 0, 0) & 1))
  708. {
  709. sys$dassgn (channel);
  710. getloadavg_initialized = false;
  711. }
  712. if (!getloadavg_initialized)
  713. return -1;
  714. # endif /* ! defined LDAV_DONE && defined __VMS */
  715. # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
  716. /* UNIX-specific code -- read the average from /dev/kmem. */
  717. # define LDAV_PRIVILEGED /* This code requires special installation. */
  718. LOAD_AVE_TYPE load_ave[3];
  719. /* Get the address of LDAV_SYMBOL. */
  720. if (offset == 0)
  721. {
  722. # ifndef sgi
  723. # ifndef NLIST_STRUCT
  724. strcpy (nl[0].n_name, LDAV_SYMBOL);
  725. strcpy (nl[1].n_name, "");
  726. # else /* NLIST_STRUCT */
  727. # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
  728. nl[0].n_un.n_name = LDAV_SYMBOL;
  729. nl[1].n_un.n_name = 0;
  730. # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
  731. nl[0].n_name = LDAV_SYMBOL;
  732. nl[1].n_name = 0;
  733. # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
  734. # endif /* NLIST_STRUCT */
  735. # ifndef SUNOS_5
  736. if (
  737. # if !(defined (_AIX) && !defined (ps2))
  738. nlist (KERNEL_FILE, nl)
  739. # else /* _AIX */
  740. knlist (nl, 1, sizeof (nl[0]))
  741. # endif
  742. >= 0)
  743. /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
  744. {
  745. # ifdef FIXUP_KERNEL_SYMBOL_ADDR
  746. FIXUP_KERNEL_SYMBOL_ADDR (nl);
  747. # endif
  748. offset = nl[0].n_value;
  749. }
  750. # endif /* !SUNOS_5 */
  751. # else /* sgi */
  752. int ldav_off;
  753. ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  754. if (ldav_off != -1)
  755. offset = (long int) ldav_off & 0x7fffffff;
  756. # endif /* sgi */
  757. }
  758. /* Make sure we have /dev/kmem open. */
  759. if (!getloadavg_initialized)
  760. {
  761. # ifndef SUNOS_5
  762. channel = open ("/dev/kmem", O_RDONLY);
  763. if (channel >= 0)
  764. {
  765. /* Set the channel to close on exec, so it does not
  766. litter any child's descriptor table. */
  767. set_cloexec_flag (channel, true);
  768. getloadavg_initialized = true;
  769. }
  770. # else /* SUNOS_5 */
  771. /* We pass 0 for the kernel, corefile, and swapfile names
  772. to use the currently running kernel. */
  773. kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  774. if (kd != 0)
  775. {
  776. /* nlist the currently running kernel. */
  777. kvm_nlist (kd, nl);
  778. offset = nl[0].n_value;
  779. getloadavg_initialized = true;
  780. }
  781. # endif /* SUNOS_5 */
  782. }
  783. /* If we can, get the load average values. */
  784. if (offset && getloadavg_initialized)
  785. {
  786. /* Try to read the load. */
  787. # ifndef SUNOS_5
  788. if (lseek (channel, offset, 0) == -1L
  789. || read (channel, (char *) load_ave, sizeof (load_ave))
  790. != sizeof (load_ave))
  791. {
  792. close (channel);
  793. getloadavg_initialized = false;
  794. }
  795. # else /* SUNOS_5 */
  796. if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  797. != sizeof (load_ave))
  798. {
  799. kvm_close (kd);
  800. getloadavg_initialized = false;
  801. }
  802. # endif /* SUNOS_5 */
  803. }
  804. if (offset == 0 || !getloadavg_initialized)
  805. return -1;
  806. # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
  807. # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
  808. if (nelem > 0)
  809. loadavg[elem++] = LDAV_CVT (load_ave[0]);
  810. if (nelem > 1)
  811. loadavg[elem++] = LDAV_CVT (load_ave[1]);
  812. if (nelem > 2)
  813. loadavg[elem++] = LDAV_CVT (load_ave[2]);
  814. # define LDAV_DONE
  815. # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
  816. # if !defined LDAV_DONE
  817. /* Set errno to zero to indicate that there was no particular error;
  818. this function just can't work at all on this system. */
  819. errno = 0;
  820. elem = -1;
  821. # endif
  822. return elem;
  823. }
  824. #endif /* ! HAVE_GETLOADAVG */
  825. #ifdef TEST
  826. int
  827. main (int argc, char **argv)
  828. {
  829. int naptime = 0;
  830. if (argc > 1)
  831. naptime = atoi (argv[1]);
  832. while (1)
  833. {
  834. double avg[3];
  835. int loads;
  836. errno = 0; /* Don't be misled if it doesn't set errno. */
  837. loads = getloadavg (avg, 3);
  838. if (loads == -1)
  839. {
  840. perror ("Error getting load average");
  841. return EXIT_FAILURE;
  842. }
  843. if (loads > 0)
  844. printf ("1-minute: %f ", avg[0]);
  845. if (loads > 1)
  846. printf ("5-minute: %f ", avg[1]);
  847. if (loads > 2)
  848. printf ("15-minute: %f ", avg[2]);
  849. if (loads > 0)
  850. putchar ('\n');
  851. if (naptime == 0)
  852. break;
  853. sleep (naptime);
  854. }
  855. return EXIT_SUCCESS;
  856. }
  857. #endif /* TEST */