getloadavg.c 27 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046
  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 Free Software
  4. 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. #include <errno.h>
  79. #include <stdio.h>
  80. #include <stdlib.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. # include "xalloc.h"
  95. /* The existing Emacs configuration files define a macro called
  96. LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
  97. returns the load average multiplied by 100. What we actually want
  98. is a macro called LDAV_CVT, which returns the load average as an
  99. unmultiplied double.
  100. For backwards compatibility, we'll define LDAV_CVT in terms of
  101. LOAD_AVE_CVT, but future machine config files should just define
  102. LDAV_CVT directly. */
  103. # if !defined (LDAV_CVT) && defined (LOAD_AVE_CVT)
  104. # define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
  105. # endif
  106. # if !defined (BSD) && defined (ultrix)
  107. /* Ultrix behaves like BSD on Vaxen. */
  108. # define BSD
  109. # endif
  110. # ifdef NeXT
  111. /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
  112. conflicts with the definition understood in this file, that this
  113. really is BSD. */
  114. # undef BSD
  115. /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
  116. defined to mean that the nlist method should be used, which is not true. */
  117. # undef FSCALE
  118. # endif
  119. /* Same issues as for NeXT apply to the HURD-based GNU system. */
  120. # ifdef __GNU__
  121. # undef BSD
  122. # undef FSCALE
  123. # endif /* __GNU__ */
  124. /* Set values that are different from the defaults, which are
  125. set a little farther down with #ifndef. */
  126. /* Some shorthands. */
  127. # if defined (HPUX) && !defined (hpux)
  128. # define hpux
  129. # endif
  130. # if defined (__hpux) && !defined (hpux)
  131. # define hpux
  132. # endif
  133. # if defined (__sun) && !defined (sun)
  134. # define sun
  135. # endif
  136. # if defined (hp300) && !defined (hpux)
  137. # define MORE_BSD
  138. # endif
  139. # if defined (ultrix) && defined (mips)
  140. # define decstation
  141. # endif
  142. # if defined (__SVR4) && !defined (SVR4)
  143. # define SVR4
  144. # endif
  145. # if (defined (sun) && defined (SVR4)) || defined (SOLARIS2)
  146. # define SUNOS_5
  147. # endif
  148. # if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
  149. # define OSF_ALPHA
  150. # include <sys/mbuf.h>
  151. # include <sys/socket.h>
  152. # include <net/route.h>
  153. # include <sys/table.h>
  154. /* Tru64 4.0D's table.h redefines sys */
  155. # undef sys
  156. # endif
  157. # if defined (__osf__) && (defined (mips) || defined (__mips__))
  158. # define OSF_MIPS
  159. # include <sys/table.h>
  160. # endif
  161. /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
  162. default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
  163. that with a couple of other things and we'll have a unique match. */
  164. # if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
  165. # define tek4300 /* Define by emacs, but not by other users. */
  166. # endif
  167. /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
  168. # ifndef LOAD_AVE_TYPE
  169. # ifdef MORE_BSD
  170. # define LOAD_AVE_TYPE long
  171. # endif
  172. # ifdef sun
  173. # define LOAD_AVE_TYPE long
  174. # endif
  175. # ifdef decstation
  176. # define LOAD_AVE_TYPE long
  177. # endif
  178. # ifdef _SEQUENT_
  179. # define LOAD_AVE_TYPE long
  180. # endif
  181. # ifdef sgi
  182. # define LOAD_AVE_TYPE long
  183. # endif
  184. # ifdef SVR4
  185. # define LOAD_AVE_TYPE long
  186. # endif
  187. # ifdef sony_news
  188. # define LOAD_AVE_TYPE long
  189. # endif
  190. # ifdef sequent
  191. # define LOAD_AVE_TYPE long
  192. # endif
  193. # ifdef OSF_ALPHA
  194. # define LOAD_AVE_TYPE long
  195. # endif
  196. # if defined (ardent) && defined (titan)
  197. # define LOAD_AVE_TYPE long
  198. # endif
  199. # ifdef tek4300
  200. # define LOAD_AVE_TYPE long
  201. # endif
  202. # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
  203. # define LOAD_AVE_TYPE long
  204. # endif
  205. # if defined _AIX && ! defined HAVE_LIBPERFSTAT
  206. # define LOAD_AVE_TYPE long
  207. # endif
  208. # ifdef convex
  209. # define LOAD_AVE_TYPE double
  210. # ifndef LDAV_CVT
  211. # define LDAV_CVT(n) (n)
  212. # endif
  213. # endif
  214. # endif /* No LOAD_AVE_TYPE. */
  215. # ifdef OSF_ALPHA
  216. /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
  217. according to ghazi@noc.rutgers.edu. */
  218. # undef FSCALE
  219. # define FSCALE 1024.0
  220. # endif
  221. # if defined (alliant) && defined (i860) /* Alliant FX/2800 */
  222. /* <sys/param.h> defines an incorrect value for FSCALE on an
  223. Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
  224. # undef FSCALE
  225. # define FSCALE 100.0
  226. # endif
  227. # ifndef FSCALE
  228. /* SunOS and some others define FSCALE in sys/param.h. */
  229. # ifdef MORE_BSD
  230. # define FSCALE 2048.0
  231. # endif
  232. # if defined (MIPS) || defined (SVR4) || defined (decstation)
  233. # define FSCALE 256
  234. # endif
  235. # if defined (sgi) || defined (sequent)
  236. /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
  237. above under #ifdef MIPS. But we want the sgi value. */
  238. # undef FSCALE
  239. # define FSCALE 1000.0
  240. # endif
  241. # if defined (ardent) && defined (titan)
  242. # define FSCALE 65536.0
  243. # endif
  244. # ifdef tek4300
  245. # define FSCALE 100.0
  246. # endif
  247. # if defined _AIX && !defined HAVE_LIBPERFSTAT
  248. # define FSCALE 65536.0
  249. # endif
  250. # endif /* Not FSCALE. */
  251. # if !defined (LDAV_CVT) && defined (FSCALE)
  252. # define LDAV_CVT(n) (((double) (n)) / FSCALE)
  253. # endif
  254. # ifndef NLIST_STRUCT
  255. # if HAVE_NLIST_H
  256. # define NLIST_STRUCT
  257. # endif
  258. # endif
  259. # if defined (sgi) || (defined (mips) && !defined (BSD))
  260. # define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
  261. # endif
  262. # if !defined (KERNEL_FILE) && defined (sequent)
  263. # define KERNEL_FILE "/dynix"
  264. # endif
  265. # if !defined (KERNEL_FILE) && defined (hpux)
  266. # define KERNEL_FILE "/hp-ux"
  267. # endif
  268. # if !defined (KERNEL_FILE) && (defined (_SEQUENT_) || defined (MIPS) || defined (SVR4) || defined (ISC) || defined (sgi) || (defined (ardent) && defined (titan)))
  269. # define KERNEL_FILE "/unix"
  270. # endif
  271. # if !defined (LDAV_SYMBOL) && defined (alliant)
  272. # define LDAV_SYMBOL "_Loadavg"
  273. # endif
  274. # 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)))
  275. # define LDAV_SYMBOL "avenrun"
  276. # endif
  277. # include <unistd.h>
  278. /* LOAD_AVE_TYPE should only get defined if we're going to use the
  279. nlist method. */
  280. # if !defined (LOAD_AVE_TYPE) && (defined (BSD) || defined (LDAV_CVT) || defined (KERNEL_FILE) || defined (LDAV_SYMBOL))
  281. # define LOAD_AVE_TYPE double
  282. # endif
  283. # ifdef LOAD_AVE_TYPE
  284. # ifndef __VMS
  285. # ifndef __linux__
  286. # ifndef NLIST_STRUCT
  287. # include <a.out.h>
  288. # else /* NLIST_STRUCT */
  289. # include <nlist.h>
  290. # endif /* NLIST_STRUCT */
  291. # ifdef SUNOS_5
  292. # include <fcntl.h>
  293. # include <kvm.h>
  294. # include <kstat.h>
  295. # endif
  296. # if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)
  297. # include <sys/pstat.h>
  298. # endif
  299. # ifndef KERNEL_FILE
  300. # define KERNEL_FILE "/vmunix"
  301. # endif /* KERNEL_FILE */
  302. # ifndef LDAV_SYMBOL
  303. # define LDAV_SYMBOL "_avenrun"
  304. # endif /* LDAV_SYMBOL */
  305. # endif /* __linux__ */
  306. # else /* __VMS */
  307. # ifndef eunice
  308. # include <iodef.h>
  309. # include <descrip.h>
  310. # else /* eunice */
  311. # include <vms/iodef.h>
  312. # endif /* eunice */
  313. # endif /* __VMS */
  314. # ifndef LDAV_CVT
  315. # define LDAV_CVT(n) ((double) (n))
  316. # endif /* !LDAV_CVT */
  317. # endif /* LOAD_AVE_TYPE */
  318. # if defined HAVE_LIBPERFSTAT
  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 = c_strtod (ptr, &endptr);
  496. if (ptr == endptr)
  497. {
  498. if (elem == 0)
  499. return -1;
  500. break;
  501. }
  502. loadavg[elem] = d;
  503. ptr = endptr;
  504. }
  505. return elem;
  506. # endif /* __linux__ || __CYGWIN__ */
  507. # if !defined (LDAV_DONE) && defined (__NetBSD__)
  508. # define LDAV_DONE
  509. # undef LOAD_AVE_TYPE
  510. # ifndef NETBSD_LDAV_FILE
  511. # define NETBSD_LDAV_FILE "/kern/loadavg"
  512. # endif
  513. unsigned long int load_ave[3], scale;
  514. int count;
  515. FILE *fp;
  516. fp = fopen (NETBSD_LDAV_FILE, "r");
  517. if (fp == NULL)
  518. return -1;
  519. count = fscanf (fp, "%lu %lu %lu %lu\n",
  520. &load_ave[0], &load_ave[1], &load_ave[2],
  521. &scale);
  522. (void) fclose (fp);
  523. if (count != 4)
  524. return -1;
  525. for (elem = 0; elem < nelem; elem++)
  526. loadavg[elem] = (double) load_ave[elem] / (double) scale;
  527. return elem;
  528. # endif /* __NetBSD__ */
  529. # if !defined (LDAV_DONE) && defined (NeXT)
  530. # define LDAV_DONE
  531. /* The NeXT code was adapted from iscreen 3.2. */
  532. host_t host;
  533. struct processor_set_basic_info info;
  534. unsigned int info_count;
  535. /* We only know how to get the 1-minute average for this system,
  536. so even if the caller asks for more than 1, we only return 1. */
  537. if (!getloadavg_initialized)
  538. {
  539. if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
  540. getloadavg_initialized = true;
  541. }
  542. if (getloadavg_initialized)
  543. {
  544. info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
  545. if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
  546. (processor_set_info_t) &info, &info_count)
  547. != KERN_SUCCESS)
  548. getloadavg_initialized = false;
  549. else
  550. {
  551. if (nelem > 0)
  552. loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
  553. }
  554. }
  555. if (!getloadavg_initialized)
  556. return -1;
  557. # endif /* NeXT */
  558. # if !defined (LDAV_DONE) && defined (UMAX)
  559. # define LDAV_DONE
  560. /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
  561. have a /dev/kmem. Information about the workings of the running kernel
  562. can be gathered with inq_stats system calls.
  563. We only know how to get the 1-minute average for this system. */
  564. struct proc_summary proc_sum_data;
  565. struct stat_descr proc_info;
  566. double load;
  567. register unsigned int i, j;
  568. if (cpus == 0)
  569. {
  570. register unsigned int c, i;
  571. struct cpu_config conf;
  572. struct stat_descr desc;
  573. desc.sd_next = 0;
  574. desc.sd_subsys = SUBSYS_CPU;
  575. desc.sd_type = CPUTYPE_CONFIG;
  576. desc.sd_addr = (char *) &conf;
  577. desc.sd_size = sizeof conf;
  578. if (inq_stats (1, &desc))
  579. return -1;
  580. c = 0;
  581. for (i = 0; i < conf.config_maxclass; ++i)
  582. {
  583. struct class_stats stats;
  584. bzero ((char *) &stats, sizeof stats);
  585. desc.sd_type = CPUTYPE_CLASS;
  586. desc.sd_objid = i;
  587. desc.sd_addr = (char *) &stats;
  588. desc.sd_size = sizeof stats;
  589. if (inq_stats (1, &desc))
  590. return -1;
  591. c += stats.class_numcpus;
  592. }
  593. cpus = c;
  594. samples = cpus < 2 ? 3 : (2 * cpus / 3);
  595. }
  596. proc_info.sd_next = 0;
  597. proc_info.sd_subsys = SUBSYS_PROC;
  598. proc_info.sd_type = PROCTYPE_SUMMARY;
  599. proc_info.sd_addr = (char *) &proc_sum_data;
  600. proc_info.sd_size = sizeof (struct proc_summary);
  601. proc_info.sd_sizeused = 0;
  602. if (inq_stats (1, &proc_info) != 0)
  603. return -1;
  604. load = proc_sum_data.ps_nrunnable;
  605. j = 0;
  606. for (i = samples - 1; i > 0; --i)
  607. {
  608. load += proc_sum_data.ps_nrun[j];
  609. if (j++ == PS_NRUNSIZE)
  610. j = 0;
  611. }
  612. if (nelem > 0)
  613. loadavg[elem++] = load / samples / cpus;
  614. # endif /* UMAX */
  615. # if !defined (LDAV_DONE) && defined (DGUX)
  616. # define LDAV_DONE
  617. /* This call can return -1 for an error, but with good args
  618. it's not supposed to fail. The first argument is for no
  619. apparent reason of type `long int *'. */
  620. dg_sys_info ((long int *) &load_info,
  621. DG_SYS_INFO_LOAD_INFO_TYPE,
  622. DG_SYS_INFO_LOAD_VERSION_0);
  623. if (nelem > 0)
  624. loadavg[elem++] = load_info.one_minute;
  625. if (nelem > 1)
  626. loadavg[elem++] = load_info.five_minute;
  627. if (nelem > 2)
  628. loadavg[elem++] = load_info.fifteen_minute;
  629. # endif /* DGUX */
  630. # if !defined (LDAV_DONE) && defined (apollo)
  631. # define LDAV_DONE
  632. /* Apollo code from lisch@mentorg.com (Ray Lischner).
  633. This system call is not documented. The load average is obtained as
  634. three long integers, for the load average over the past minute,
  635. five minutes, and fifteen minutes. Each value is a scaled integer,
  636. with 16 bits of integer part and 16 bits of fraction part.
  637. I'm not sure which operating system first supported this system call,
  638. but I know that SR10.2 supports it. */
  639. extern void proc1_$get_loadav ();
  640. unsigned long load_ave[3];
  641. proc1_$get_loadav (load_ave);
  642. if (nelem > 0)
  643. loadavg[elem++] = load_ave[0] / 65536.0;
  644. if (nelem > 1)
  645. loadavg[elem++] = load_ave[1] / 65536.0;
  646. if (nelem > 2)
  647. loadavg[elem++] = load_ave[2] / 65536.0;
  648. # endif /* apollo */
  649. # if !defined (LDAV_DONE) && defined (OSF_MIPS)
  650. # define LDAV_DONE
  651. struct tbl_loadavg load_ave;
  652. table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  653. loadavg[elem++]
  654. = (load_ave.tl_lscale == 0
  655. ? load_ave.tl_avenrun.d[0]
  656. : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
  657. # endif /* OSF_MIPS */
  658. # if !defined (LDAV_DONE) && (defined (__MSDOS__) || defined (WINDOWS32))
  659. # define LDAV_DONE
  660. /* A faithful emulation is going to have to be saved for a rainy day. */
  661. for ( ; elem < nelem; elem++)
  662. {
  663. loadavg[elem] = 0.0;
  664. }
  665. # endif /* __MSDOS__ || WINDOWS32 */
  666. # if !defined (LDAV_DONE) && defined (OSF_ALPHA)
  667. # define LDAV_DONE
  668. struct tbl_loadavg load_ave;
  669. table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
  670. for (elem = 0; elem < nelem; elem++)
  671. loadavg[elem]
  672. = (load_ave.tl_lscale == 0
  673. ? load_ave.tl_avenrun.d[elem]
  674. : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
  675. # endif /* OSF_ALPHA */
  676. # if ! defined LDAV_DONE && defined __VMS
  677. /* VMS specific code -- read from the Load Ave driver. */
  678. LOAD_AVE_TYPE load_ave[3];
  679. static bool getloadavg_initialized;
  680. # ifdef eunice
  681. struct
  682. {
  683. int dsc$w_length;
  684. char *dsc$a_pointer;
  685. } descriptor;
  686. # endif
  687. /* Ensure that there is a channel open to the load ave device. */
  688. if (!getloadavg_initialized)
  689. {
  690. /* Attempt to open the channel. */
  691. # ifdef eunice
  692. descriptor.dsc$w_length = 18;
  693. descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
  694. # else
  695. $DESCRIPTOR (descriptor, "LAV0:");
  696. # endif
  697. if (sys$assign (&descriptor, &channel, 0, 0) & 1)
  698. getloadavg_initialized = true;
  699. }
  700. /* Read the load average vector. */
  701. if (getloadavg_initialized
  702. && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
  703. load_ave, 12, 0, 0, 0, 0) & 1))
  704. {
  705. sys$dassgn (channel);
  706. getloadavg_initialized = false;
  707. }
  708. if (!getloadavg_initialized)
  709. return -1;
  710. # endif /* ! defined LDAV_DONE && defined __VMS */
  711. # if ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS
  712. /* UNIX-specific code -- read the average from /dev/kmem. */
  713. # define LDAV_PRIVILEGED /* This code requires special installation. */
  714. LOAD_AVE_TYPE load_ave[3];
  715. /* Get the address of LDAV_SYMBOL. */
  716. if (offset == 0)
  717. {
  718. # ifndef sgi
  719. # ifndef NLIST_STRUCT
  720. strcpy (nl[0].n_name, LDAV_SYMBOL);
  721. strcpy (nl[1].n_name, "");
  722. # else /* NLIST_STRUCT */
  723. # ifdef HAVE_STRUCT_NLIST_N_UN_N_NAME
  724. nl[0].n_un.n_name = LDAV_SYMBOL;
  725. nl[1].n_un.n_name = 0;
  726. # else /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
  727. nl[0].n_name = LDAV_SYMBOL;
  728. nl[1].n_name = 0;
  729. # endif /* not HAVE_STRUCT_NLIST_N_UN_N_NAME */
  730. # endif /* NLIST_STRUCT */
  731. # ifndef SUNOS_5
  732. if (
  733. # if !(defined (_AIX) && !defined (ps2))
  734. nlist (KERNEL_FILE, nl)
  735. # else /* _AIX */
  736. knlist (nl, 1, sizeof (nl[0]))
  737. # endif
  738. >= 0)
  739. /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
  740. {
  741. # ifdef FIXUP_KERNEL_SYMBOL_ADDR
  742. FIXUP_KERNEL_SYMBOL_ADDR (nl);
  743. # endif
  744. offset = nl[0].n_value;
  745. }
  746. # endif /* !SUNOS_5 */
  747. # else /* sgi */
  748. int ldav_off;
  749. ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
  750. if (ldav_off != -1)
  751. offset = (long int) ldav_off & 0x7fffffff;
  752. # endif /* sgi */
  753. }
  754. /* Make sure we have /dev/kmem open. */
  755. if (!getloadavg_initialized)
  756. {
  757. # ifndef SUNOS_5
  758. channel = open ("/dev/kmem", O_RDONLY);
  759. if (channel >= 0)
  760. {
  761. /* Set the channel to close on exec, so it does not
  762. litter any child's descriptor table. */
  763. set_cloexec_flag (channel, true);
  764. getloadavg_initialized = true;
  765. }
  766. # else /* SUNOS_5 */
  767. /* We pass 0 for the kernel, corefile, and swapfile names
  768. to use the currently running kernel. */
  769. kd = kvm_open (0, 0, 0, O_RDONLY, 0);
  770. if (kd != 0)
  771. {
  772. /* nlist the currently running kernel. */
  773. kvm_nlist (kd, nl);
  774. offset = nl[0].n_value;
  775. getloadavg_initialized = true;
  776. }
  777. # endif /* SUNOS_5 */
  778. }
  779. /* If we can, get the load average values. */
  780. if (offset && getloadavg_initialized)
  781. {
  782. /* Try to read the load. */
  783. # ifndef SUNOS_5
  784. if (lseek (channel, offset, 0) == -1L
  785. || read (channel, (char *) load_ave, sizeof (load_ave))
  786. != sizeof (load_ave))
  787. {
  788. close (channel);
  789. getloadavg_initialized = false;
  790. }
  791. # else /* SUNOS_5 */
  792. if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
  793. != sizeof (load_ave))
  794. {
  795. kvm_close (kd);
  796. getloadavg_initialized = false;
  797. }
  798. # endif /* SUNOS_5 */
  799. }
  800. if (offset == 0 || !getloadavg_initialized)
  801. return -1;
  802. # endif /* ! defined LDAV_DONE && defined LOAD_AVE_TYPE && ! defined __VMS */
  803. # if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
  804. if (nelem > 0)
  805. loadavg[elem++] = LDAV_CVT (load_ave[0]);
  806. if (nelem > 1)
  807. loadavg[elem++] = LDAV_CVT (load_ave[1]);
  808. if (nelem > 2)
  809. loadavg[elem++] = LDAV_CVT (load_ave[2]);
  810. # define LDAV_DONE
  811. # endif /* !LDAV_DONE && LOAD_AVE_TYPE */
  812. # if !defined LDAV_DONE
  813. /* Set errno to zero to indicate that there was no particular error;
  814. this function just can't work at all on this system. */
  815. errno = 0;
  816. elem = -1;
  817. # endif
  818. return elem;
  819. }
  820. #endif /* ! HAVE_GETLOADAVG */
  821. #ifdef TEST
  822. int
  823. main (int argc, char **argv)
  824. {
  825. int naptime = 0;
  826. if (argc > 1)
  827. naptime = atoi (argv[1]);
  828. while (1)
  829. {
  830. double avg[3];
  831. int loads;
  832. errno = 0; /* Don't be misled if it doesn't set errno. */
  833. loads = getloadavg (avg, 3);
  834. if (loads == -1)
  835. {
  836. perror ("Error getting load average");
  837. return EXIT_FAILURE;
  838. }
  839. if (loads > 0)
  840. printf ("1-minute: %f ", avg[0]);
  841. if (loads > 1)
  842. printf ("5-minute: %f ", avg[1]);
  843. if (loads > 2)
  844. printf ("15-minute: %f ", avg[2]);
  845. if (loads > 0)
  846. putchar ('\n');
  847. if (naptime == 0)
  848. break;
  849. sleep (naptime);
  850. }
  851. return EXIT_SUCCESS;
  852. }
  853. #endif /* TEST */