getloadavg.c 27 KB

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