Commit | Line | Data |
---|---|---|
1e5ba5b5 | 1 | /* Get the system load averages. |
df3dd7d6 | 2 | Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995 |
1e5ba5b5 RM |
3 | Free Software Foundation, Inc. |
4 | ||
5 | This program is free software; you can redistribute it and/or modify | |
6 | it under the terms of the GNU General Public License as published by | |
7 | the Free Software Foundation; either version 2, or (at your option) | |
8 | any later version. | |
9 | ||
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 | ||
15 | You should have received a copy of the GNU General Public License | |
16 | along with this program; if not, write to the Free Software | |
17 | Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
18 | ||
19 | /* Compile-time symbols that this file uses: | |
20 | ||
21 | FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. | |
22 | KERNEL_FILE Pathname of the kernel to nlist. | |
23 | LDAV_CVT() Scale the load average from the kernel. | |
24 | Returns a double. | |
25 | LDAV_SYMBOL Name of kernel symbol giving load average. | |
26 | LOAD_AVE_TYPE Type of the load average array in the kernel. | |
27 | Must be defined unless one of | |
28 | apollo, DGUX, NeXT, or UMAX is defined; | |
29 | otherwise, no load average is available. | |
30 | NLIST_STRUCT Include nlist.h, not a.out.h, and | |
31 | the nlist n_name element is a pointer, | |
32 | not an array. | |
33 | NLIST_NAME_UNION struct nlist has an n_un member, not n_name. | |
22ddf250 | 34 | LINUX_LDAV_FILE [__linux__]: File containing load averages. |
1e5ba5b5 RM |
35 | |
36 | Specific system predefines this file uses, aside from setting | |
37 | default values if not emacs: | |
38 | ||
39 | apollo | |
40 | BSD Real BSD, not just BSD-like. | |
91bf5fad | 41 | convex |
1e5ba5b5 RM |
42 | DGUX |
43 | eunice UNIX emulator under VMS. | |
44 | hpux | |
45 | NeXT | |
46 | sgi | |
47 | sequent Sequent Dynix 3.x.x (BSD) | |
48 | _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV) | |
49 | sony_news NEWS-OS (works at least for 4.1C) | |
50 | UMAX | |
51 | UMAX4_3 | |
52 | VMS | |
22ddf250 | 53 | __linux__ Linux: assumes /proc filesystem mounted. |
1e5ba5b5 | 54 | Support from Michael K. Johnson. |
9eb8034b | 55 | __NetBSD__ NetBSD: assumes /kern filesystem mounted. |
1e5ba5b5 RM |
56 | |
57 | In addition, to avoid nesting many #ifdefs, we internally set | |
58 | LDAV_DONE to indicate that the load average has been computed. | |
59 | ||
60 | We also #define LDAV_PRIVILEGED if a program will require | |
61 | special installation to be able to call getloadavg. */ | |
62 | ||
df3dd7d6 RM |
63 | /* This should always be first. */ |
64 | #ifdef HAVE_CONFIG_H | |
65 | #include <config.h> | |
66 | #endif | |
67 | ||
1e5ba5b5 RM |
68 | #include <sys/types.h> |
69 | ||
70 | /* Both the Emacs and non-Emacs sections want this. Some | |
71 | configuration files' definitions for the LOAD_AVE_CVT macro (like | |
72 | sparc.h's) use macros like FSCALE, defined here. */ | |
73 | #ifdef unix | |
74 | #include <sys/param.h> | |
75 | #endif | |
76 | ||
77 | ||
04402ae3 | 78 | /* Exclude all the code except the test program at the end |
39c5a83f RM |
79 | if the system has its own `getloadavg' function. |
80 | ||
81 | The declaration of `errno' is needed by the test program | |
82 | as well as the function itself, so it comes first. */ | |
83 | ||
84 | #include <errno.h> | |
85 | ||
86 | #ifndef errno | |
87 | extern int errno; | |
88 | #endif | |
04402ae3 RM |
89 | |
90 | #ifndef HAVE_GETLOADAVG | |
91 | ||
92 | ||
1e5ba5b5 RM |
93 | /* The existing Emacs configuration files define a macro called |
94 | LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and | |
95 | returns the load average multiplied by 100. What we actually want | |
96 | is a macro called LDAV_CVT, which returns the load average as an | |
97 | unmultiplied double. | |
98 | ||
99 | For backwards compatibility, we'll define LDAV_CVT in terms of | |
100 | LOAD_AVE_CVT, but future machine config files should just define | |
101 | LDAV_CVT directly. */ | |
102 | ||
103 | #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT) | |
104 | #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0) | |
105 | #endif | |
106 | ||
107 | #if !defined (BSD) && defined (ultrix) | |
108 | /* Ultrix behaves like BSD on Vaxen. */ | |
109 | #define BSD | |
110 | #endif | |
111 | ||
112 | #ifdef NeXT | |
113 | /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which | |
114 | conflicts with the definition understood in this file, that this | |
115 | really is BSD. */ | |
116 | #undef BSD | |
117 | ||
118 | /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being | |
119 | defined to mean that the nlist method should be used, which is not true. */ | |
120 | #undef FSCALE | |
121 | #endif | |
122 | ||
123 | /* Set values that are different from the defaults, which are | |
124 | set a little farther down with #ifndef. */ | |
125 | ||
126 | ||
127 | /* Some shorthands. */ | |
128 | ||
129 | #if defined (HPUX) && !defined (hpux) | |
130 | #define hpux | |
131 | #endif | |
132 | ||
133 | #if defined(hp300) && !defined(hpux) | |
134 | #define MORE_BSD | |
135 | #endif | |
136 | ||
137 | #if defined(ultrix) && defined(mips) | |
138 | #define decstation | |
139 | #endif | |
140 | ||
141 | #if defined(sun) && defined(SVR4) | |
142 | #define SUNOS_5 | |
143 | #endif | |
144 | ||
d2a9653a | 145 | #if defined (__osf__) && (defined (__alpha) || defined (__alpha__)) |
1e5ba5b5 | 146 | #define OSF_ALPHA |
bbd619d7 | 147 | #include <sys/table.h> |
1e5ba5b5 RM |
148 | #endif |
149 | ||
150 | #if defined (__osf__) && (defined (mips) || defined (__mips__)) | |
151 | #define OSF_MIPS | |
152 | #include <sys/table.h> | |
153 | #endif | |
154 | ||
155 | /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by | |
156 | default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine | |
157 | that with a couple of other things and we'll have a unique match. */ | |
158 | #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES) | |
159 | #define tek4300 /* Define by emacs, but not by other users. */ | |
160 | #endif | |
161 | ||
162 | ||
8eda6c51 | 163 | /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */ |
5ac622b2 RM |
164 | #ifndef LOAD_AVE_TYPE |
165 | ||
8eda6c51 JB |
166 | #ifdef MORE_BSD |
167 | #define LOAD_AVE_TYPE long | |
168 | #endif | |
169 | ||
170 | #ifdef sun | |
171 | #define LOAD_AVE_TYPE long | |
172 | #endif | |
173 | ||
174 | #ifdef decstation | |
175 | #define LOAD_AVE_TYPE long | |
176 | #endif | |
177 | ||
178 | #ifdef _SEQUENT_ | |
179 | #define LOAD_AVE_TYPE long | |
180 | #endif | |
181 | ||
182 | #ifdef sgi | |
183 | #define LOAD_AVE_TYPE long | |
184 | #endif | |
185 | ||
186 | #ifdef SVR4 | |
187 | #define LOAD_AVE_TYPE long | |
188 | #endif | |
189 | ||
190 | #ifdef sony_news | |
191 | #define LOAD_AVE_TYPE long | |
192 | #endif | |
193 | ||
194 | #ifdef sequent | |
195 | #define LOAD_AVE_TYPE long | |
196 | #endif | |
197 | ||
198 | #ifdef OSF_ALPHA | |
199 | #define LOAD_AVE_TYPE long | |
200 | #endif | |
201 | ||
5ac622b2 | 202 | #if defined (ardent) && defined (titan) |
8eda6c51 JB |
203 | #define LOAD_AVE_TYPE long |
204 | #endif | |
205 | ||
5ac622b2 | 206 | #ifdef tek4300 |
1e5ba5b5 RM |
207 | #define LOAD_AVE_TYPE long |
208 | #endif | |
209 | ||
59c0e567 RM |
210 | #if defined(alliant) && defined(i860) /* Alliant FX/2800 */ |
211 | #define LOAD_AVE_TYPE long | |
212 | #endif | |
213 | ||
bc77c95c RM |
214 | #ifdef _AIX |
215 | #define LOAD_AVE_TYPE long | |
216 | #endif | |
217 | ||
91bf5fad RM |
218 | #ifdef convex |
219 | #define LOAD_AVE_TYPE double | |
220 | #ifndef LDAV_CVT | |
221 | #define LDAV_CVT(n) (n) | |
222 | #endif | |
223 | #endif | |
224 | ||
5ac622b2 | 225 | #endif /* No LOAD_AVE_TYPE. */ |
1e5ba5b5 | 226 | |
f870f3fe RM |
227 | #ifdef OSF_ALPHA |
228 | /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1, | |
229 | according to ghazi@noc.rutgers.edu. */ | |
230 | #undef FSCALE | |
231 | #define FSCALE 1024.0 | |
232 | #endif | |
233 | ||
bf074c06 RM |
234 | #if defined(alliant) && defined(i860) /* Alliant FX/2800 */ |
235 | /* <sys/param.h> defines an incorrect value for FSCALE on an | |
236 | Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */ | |
237 | #undef FSCALE | |
238 | #define FSCALE 100.0 | |
239 | #endif | |
240 | ||
f870f3fe | 241 | |
1e5ba5b5 RM |
242 | #ifndef FSCALE |
243 | ||
244 | /* SunOS and some others define FSCALE in sys/param.h. */ | |
245 | ||
246 | #ifdef MORE_BSD | |
247 | #define FSCALE 2048.0 | |
248 | #endif | |
249 | ||
250 | #if defined(MIPS) || defined(SVR4) || defined(decstation) | |
251 | #define FSCALE 256 | |
252 | #endif | |
253 | ||
254 | #if defined (sgi) || defined (sequent) | |
934d56b7 RM |
255 | /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined |
256 | above under #ifdef MIPS. But we want the sgi value. */ | |
257 | #undef FSCALE | |
1e5ba5b5 RM |
258 | #define FSCALE 1000.0 |
259 | #endif | |
260 | ||
261 | #if defined (ardent) && defined (titan) | |
262 | #define FSCALE 65536.0 | |
263 | #endif | |
264 | ||
265 | #ifdef tek4300 | |
266 | #define FSCALE 100.0 | |
59c0e567 RM |
267 | #endif |
268 | ||
bc77c95c RM |
269 | #ifdef _AIX |
270 | #define FSCALE 65536.0 | |
271 | #endif | |
272 | ||
1e5ba5b5 RM |
273 | #endif /* Not FSCALE. */ |
274 | ||
275 | #if !defined (LDAV_CVT) && defined (FSCALE) | |
276 | #define LDAV_CVT(n) (((double) (n)) / FSCALE) | |
277 | #endif | |
278 | ||
8eda6c51 JB |
279 | /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */ |
280 | #ifndef NLIST_STRUCT | |
281 | ||
282 | #ifdef MORE_BSD | |
283 | #define NLIST_STRUCT | |
284 | #endif | |
285 | ||
286 | #ifdef sun | |
287 | #define NLIST_STRUCT | |
288 | #endif | |
289 | ||
290 | #ifdef decstation | |
291 | #define NLIST_STRUCT | |
292 | #endif | |
293 | ||
294 | #ifdef hpux | |
295 | #define NLIST_STRUCT | |
296 | #endif | |
297 | ||
298 | #if defined (_SEQUENT_) || defined (sequent) | |
299 | #define NLIST_STRUCT | |
300 | #endif | |
301 | ||
302 | #ifdef sgi | |
303 | #define NLIST_STRUCT | |
304 | #endif | |
305 | ||
306 | #ifdef SVR4 | |
1e5ba5b5 RM |
307 | #define NLIST_STRUCT |
308 | #endif | |
309 | ||
8eda6c51 JB |
310 | #ifdef sony_news |
311 | #define NLIST_STRUCT | |
312 | #endif | |
313 | ||
314 | #ifdef OSF_ALPHA | |
315 | #define NLIST_STRUCT | |
316 | #endif | |
317 | ||
318 | #if defined (ardent) && defined (titan) | |
319 | #define NLIST_STRUCT | |
320 | #endif | |
321 | ||
59c0e567 | 322 | #ifdef tek4300 |
8eda6c51 JB |
323 | #define NLIST_STRUCT |
324 | #endif | |
325 | ||
326 | #ifdef butterfly | |
327 | #define NLIST_STRUCT | |
328 | #endif | |
329 | ||
59c0e567 RM |
330 | #if defined(alliant) && defined(i860) /* Alliant FX/2800 */ |
331 | #define NLIST_STRUCT | |
332 | #endif | |
333 | ||
bc77c95c RM |
334 | #ifdef _AIX |
335 | #define NLIST_STRUCT | |
336 | #endif | |
337 | ||
8eda6c51 JB |
338 | #endif /* defined (NLIST_STRUCT) */ |
339 | ||
1e5ba5b5 RM |
340 | |
341 | #if defined(sgi) || (defined(mips) && !defined(BSD)) | |
342 | #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31)) | |
343 | #endif | |
344 | ||
345 | ||
346 | #if !defined (KERNEL_FILE) && defined (sequent) | |
347 | #define KERNEL_FILE "/dynix" | |
348 | #endif | |
349 | ||
350 | #if !defined (KERNEL_FILE) && defined (hpux) | |
351 | #define KERNEL_FILE "/hp-ux" | |
352 | #endif | |
353 | ||
354 | #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan))) | |
355 | #define KERNEL_FILE "/unix" | |
356 | #endif | |
357 | ||
358 | ||
359 | #if !defined (LDAV_SYMBOL) && defined (alliant) | |
360 | #define LDAV_SYMBOL "_Loadavg" | |
361 | #endif | |
362 | ||
bc77c95c | 363 | #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX)) |
1e5ba5b5 RM |
364 | #define LDAV_SYMBOL "avenrun" |
365 | #endif | |
366 | ||
367 | #ifdef HAVE_UNISTD_H | |
368 | #include <unistd.h> | |
369 | #endif | |
370 | ||
371 | #include <stdio.h> | |
1e5ba5b5 RM |
372 | |
373 | /* LOAD_AVE_TYPE should only get defined if we're going to use the | |
374 | nlist method. */ | |
375 | #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) | |
376 | #define LOAD_AVE_TYPE double | |
377 | #endif | |
378 | ||
379 | #ifdef LOAD_AVE_TYPE | |
380 | ||
381 | #ifndef VMS | |
382 | #ifndef NLIST_STRUCT | |
383 | #include <a.out.h> | |
384 | #else /* NLIST_STRUCT */ | |
385 | #include <nlist.h> | |
386 | #endif /* NLIST_STRUCT */ | |
387 | ||
388 | #ifdef SUNOS_5 | |
389 | #include <fcntl.h> | |
390 | #include <kvm.h> | |
391 | #endif | |
392 | ||
393 | #ifndef KERNEL_FILE | |
394 | #define KERNEL_FILE "/vmunix" | |
395 | #endif /* KERNEL_FILE */ | |
396 | ||
397 | #ifndef LDAV_SYMBOL | |
398 | #define LDAV_SYMBOL "_avenrun" | |
399 | #endif /* LDAV_SYMBOL */ | |
400 | ||
401 | #else /* VMS */ | |
402 | ||
403 | #ifndef eunice | |
404 | #include <iodef.h> | |
405 | #include <descrip.h> | |
406 | #else /* eunice */ | |
407 | #include <vms/iodef.h> | |
408 | #endif /* eunice */ | |
409 | #endif /* VMS */ | |
410 | ||
411 | #ifndef LDAV_CVT | |
412 | #define LDAV_CVT(n) ((double) (n)) | |
413 | #endif /* !LDAV_CVT */ | |
414 | ||
415 | #endif /* LOAD_AVE_TYPE */ | |
416 | ||
417 | #ifdef NeXT | |
418 | #ifdef HAVE_MACH_MACH_H | |
419 | #include <mach/mach.h> | |
420 | #else | |
421 | #include <mach.h> | |
422 | #endif | |
423 | #endif /* NeXT */ | |
424 | ||
425 | #ifdef sgi | |
426 | #include <sys/sysmp.h> | |
427 | #endif /* sgi */ | |
428 | ||
429 | #ifdef UMAX | |
430 | #include <stdio.h> | |
431 | #include <signal.h> | |
432 | #include <sys/time.h> | |
433 | #include <sys/wait.h> | |
434 | #include <sys/syscall.h> | |
435 | ||
436 | #ifdef UMAX_43 | |
437 | #include <machine/cpu.h> | |
438 | #include <inq_stats/statistics.h> | |
439 | #include <inq_stats/sysstats.h> | |
440 | #include <inq_stats/cpustats.h> | |
441 | #include <inq_stats/procstats.h> | |
442 | #else /* Not UMAX_43. */ | |
443 | #include <sys/sysdefs.h> | |
444 | #include <sys/statistics.h> | |
445 | #include <sys/sysstats.h> | |
446 | #include <sys/cpudefs.h> | |
447 | #include <sys/cpustats.h> | |
448 | #include <sys/procstats.h> | |
449 | #endif /* Not UMAX_43. */ | |
450 | #endif /* UMAX */ | |
451 | ||
452 | #ifdef DGUX | |
453 | #include <sys/dg_sys_info.h> | |
454 | #endif | |
455 | ||
456 | #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION) | |
457 | #include <fcntl.h> | |
458 | #else | |
459 | #include <sys/file.h> | |
460 | #endif | |
461 | \f | |
462 | /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
463 | ||
464 | #ifdef NeXT | |
465 | static processor_set_t default_set; | |
466 | static int getloadavg_initialized; | |
467 | #endif /* NeXT */ | |
468 | ||
469 | #ifdef UMAX | |
470 | static unsigned int cpus = 0; | |
471 | static unsigned int samples; | |
472 | #endif /* UMAX */ | |
473 | ||
474 | #ifdef DGUX | |
475 | static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */ | |
476 | #endif /* DGUX */ | |
477 | ||
478 | #ifdef LOAD_AVE_TYPE | |
479 | /* File descriptor open to /dev/kmem or VMS load ave driver. */ | |
480 | static int channel; | |
481 | /* Nonzero iff channel is valid. */ | |
482 | static int getloadavg_initialized; | |
483 | /* Offset in kmem to seek to read load average, or 0 means invalid. */ | |
484 | static long offset; | |
485 | ||
486 | #if !defined(VMS) && !defined(sgi) | |
487 | static struct nlist nl[2]; | |
488 | #endif /* Not VMS or sgi */ | |
489 | ||
490 | #ifdef SUNOS_5 | |
491 | static kvm_t *kd; | |
492 | #endif /* SUNOS_5 */ | |
493 | ||
494 | #endif /* LOAD_AVE_TYPE */ | |
495 | \f | |
496 | /* Put the 1 minute, 5 minute and 15 minute load averages | |
497 | into the first NELEM elements of LOADAVG. | |
039e9c8b | 498 | Return the number written (never more than 3, but may be less than NELEM), |
1e5ba5b5 RM |
499 | or -1 if an error occurred. */ |
500 | ||
501 | int | |
502 | getloadavg (loadavg, nelem) | |
503 | double loadavg[]; | |
504 | int nelem; | |
505 | { | |
506 | int elem = 0; /* Return value. */ | |
507 | ||
7ee260fe RM |
508 | #ifdef NO_GET_LOAD_AVG |
509 | #define LDAV_DONE | |
510 | /* Set errno to zero to indicate that there was no particular error; | |
511 | this function just can't work at all on this system. */ | |
512 | errno = 0; | |
513 | elem = -1; | |
514 | #endif | |
515 | ||
22ddf250 | 516 | #if !defined (LDAV_DONE) && defined (__linux__) |
1e5ba5b5 RM |
517 | #define LDAV_DONE |
518 | #undef LOAD_AVE_TYPE | |
519 | ||
520 | #ifndef LINUX_LDAV_FILE | |
521 | #define LINUX_LDAV_FILE "/proc/loadavg" | |
522 | #endif | |
523 | ||
524 | char ldavgbuf[40]; | |
525 | double load_ave[3]; | |
526 | int fd, count; | |
527 | ||
528 | fd = open (LINUX_LDAV_FILE, O_RDONLY); | |
529 | if (fd == -1) | |
530 | return -1; | |
531 | count = read (fd, ldavgbuf, 40); | |
532 | (void) close (fd); | |
533 | if (count <= 0) | |
534 | return -1; | |
535 | ||
536 | count = sscanf (ldavgbuf, "%lf %lf %lf", | |
537 | &load_ave[0], &load_ave[1], &load_ave[2]); | |
538 | if (count < 1) | |
539 | return -1; | |
540 | ||
541 | for (elem = 0; elem < nelem && elem < count; elem++) | |
542 | loadavg[elem] = load_ave[elem]; | |
543 | ||
544 | return elem; | |
545 | ||
22ddf250 | 546 | #endif /* __linux__ */ |
1e5ba5b5 | 547 | |
9eb8034b RM |
548 | #if !defined (LDAV_DONE) && defined (__NetBSD__) |
549 | #define LDAV_DONE | |
550 | #undef LOAD_AVE_TYPE | |
551 | ||
552 | #ifndef NETBSD_LDAV_FILE | |
553 | #define NETBSD_LDAV_FILE "/kern/loadavg" | |
554 | #endif | |
555 | ||
556 | unsigned long int load_ave[3], scale; | |
557 | int count; | |
558 | FILE *fp; | |
559 | ||
8f090e85 | 560 | fp = fopen (NETBSD_LDAV_FILE, "r"); |
9eb8034b RM |
561 | if (fp == NULL) |
562 | return -1; | |
563 | count = fscanf (fp, "%lu %lu %lu %lu\n", | |
564 | &load_ave[0], &load_ave[1], &load_ave[2], | |
565 | &scale); | |
566 | (void) fclose (fp); | |
567 | if (count != 4) | |
568 | return -1; | |
569 | ||
570 | for (elem = 0; elem < nelem; elem++) | |
571 | loadavg[elem] = (double) load_ave[elem] / (double) scale; | |
572 | ||
573 | return elem; | |
574 | ||
575 | #endif /* __NetBSD__ */ | |
576 | ||
1e5ba5b5 RM |
577 | #if !defined (LDAV_DONE) && defined (NeXT) |
578 | #define LDAV_DONE | |
579 | /* The NeXT code was adapted from iscreen 3.2. */ | |
580 | ||
581 | host_t host; | |
582 | struct processor_set_basic_info info; | |
583 | unsigned info_count; | |
584 | ||
039e9c8b DM |
585 | /* We only know how to get the 1-minute average for this system, |
586 | so even if the caller asks for more than 1, we only return 1. */ | |
1e5ba5b5 RM |
587 | |
588 | if (!getloadavg_initialized) | |
589 | { | |
590 | if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS) | |
591 | getloadavg_initialized = 1; | |
592 | } | |
593 | ||
594 | if (getloadavg_initialized) | |
595 | { | |
596 | info_count = PROCESSOR_SET_BASIC_INFO_COUNT; | |
597 | if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host, | |
598 | (processor_set_info_t) &info, &info_count) | |
599 | != KERN_SUCCESS) | |
600 | getloadavg_initialized = 0; | |
601 | else | |
602 | { | |
603 | if (nelem > 0) | |
604 | loadavg[elem++] = (double) info.load_average / LOAD_SCALE; | |
605 | } | |
606 | } | |
607 | ||
608 | if (!getloadavg_initialized) | |
609 | return -1; | |
610 | #endif /* NeXT */ | |
611 | ||
612 | #if !defined (LDAV_DONE) && defined (UMAX) | |
613 | #define LDAV_DONE | |
614 | /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not | |
615 | have a /dev/kmem. Information about the workings of the running kernel | |
616 | can be gathered with inq_stats system calls. | |
617 | We only know how to get the 1-minute average for this system. */ | |
618 | ||
619 | struct proc_summary proc_sum_data; | |
620 | struct stat_descr proc_info; | |
621 | double load; | |
622 | register unsigned int i, j; | |
623 | ||
624 | if (cpus == 0) | |
625 | { | |
626 | register unsigned int c, i; | |
627 | struct cpu_config conf; | |
628 | struct stat_descr desc; | |
629 | ||
630 | desc.sd_next = 0; | |
631 | desc.sd_subsys = SUBSYS_CPU; | |
632 | desc.sd_type = CPUTYPE_CONFIG; | |
633 | desc.sd_addr = (char *) &conf; | |
634 | desc.sd_size = sizeof conf; | |
635 | ||
636 | if (inq_stats (1, &desc)) | |
637 | return -1; | |
638 | ||
639 | c = 0; | |
640 | for (i = 0; i < conf.config_maxclass; ++i) | |
641 | { | |
642 | struct class_stats stats; | |
643 | bzero ((char *) &stats, sizeof stats); | |
644 | ||
645 | desc.sd_type = CPUTYPE_CLASS; | |
646 | desc.sd_objid = i; | |
647 | desc.sd_addr = (char *) &stats; | |
648 | desc.sd_size = sizeof stats; | |
649 | ||
650 | if (inq_stats (1, &desc)) | |
651 | return -1; | |
652 | ||
653 | c += stats.class_numcpus; | |
654 | } | |
655 | cpus = c; | |
656 | samples = cpus < 2 ? 3 : (2 * cpus / 3); | |
657 | } | |
658 | ||
659 | proc_info.sd_next = 0; | |
660 | proc_info.sd_subsys = SUBSYS_PROC; | |
661 | proc_info.sd_type = PROCTYPE_SUMMARY; | |
662 | proc_info.sd_addr = (char *) &proc_sum_data; | |
663 | proc_info.sd_size = sizeof (struct proc_summary); | |
664 | proc_info.sd_sizeused = 0; | |
665 | ||
666 | if (inq_stats (1, &proc_info) != 0) | |
667 | return -1; | |
668 | ||
669 | load = proc_sum_data.ps_nrunnable; | |
670 | j = 0; | |
671 | for (i = samples - 1; i > 0; --i) | |
672 | { | |
673 | load += proc_sum_data.ps_nrun[j]; | |
674 | if (j++ == PS_NRUNSIZE) | |
675 | j = 0; | |
676 | } | |
677 | ||
678 | if (nelem > 0) | |
679 | loadavg[elem++] = load / samples / cpus; | |
680 | #endif /* UMAX */ | |
681 | ||
682 | #if !defined (LDAV_DONE) && defined (DGUX) | |
683 | #define LDAV_DONE | |
684 | /* This call can return -1 for an error, but with good args | |
685 | it's not supposed to fail. The first argument is for no | |
686 | apparent reason of type `long int *'. */ | |
687 | dg_sys_info ((long int *) &load_info, | |
688 | DG_SYS_INFO_LOAD_INFO_TYPE, | |
689 | DG_SYS_INFO_LOAD_VERSION_0); | |
690 | ||
691 | if (nelem > 0) | |
692 | loadavg[elem++] = load_info.one_minute; | |
693 | if (nelem > 1) | |
694 | loadavg[elem++] = load_info.five_minute; | |
695 | if (nelem > 2) | |
696 | loadavg[elem++] = load_info.fifteen_minute; | |
697 | #endif /* DGUX */ | |
698 | ||
699 | #if !defined (LDAV_DONE) && defined (apollo) | |
700 | #define LDAV_DONE | |
701 | /* Apollo code from lisch@mentorg.com (Ray Lischner). | |
702 | ||
703 | This system call is not documented. The load average is obtained as | |
704 | three long integers, for the load average over the past minute, | |
705 | five minutes, and fifteen minutes. Each value is a scaled integer, | |
706 | with 16 bits of integer part and 16 bits of fraction part. | |
707 | ||
708 | I'm not sure which operating system first supported this system call, | |
709 | but I know that SR10.2 supports it. */ | |
710 | ||
711 | extern void proc1_$get_loadav (); | |
712 | unsigned long load_ave[3]; | |
713 | ||
714 | proc1_$get_loadav (load_ave); | |
715 | ||
716 | if (nelem > 0) | |
717 | loadavg[elem++] = load_ave[0] / 65536.0; | |
718 | if (nelem > 1) | |
719 | loadavg[elem++] = load_ave[1] / 65536.0; | |
720 | if (nelem > 2) | |
721 | loadavg[elem++] = load_ave[2] / 65536.0; | |
722 | #endif /* apollo */ | |
723 | ||
724 | #if !defined (LDAV_DONE) && defined (OSF_MIPS) | |
725 | #define LDAV_DONE | |
1e5ba5b5 RM |
726 | |
727 | struct tbl_loadavg load_ave; | |
728 | table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave)); | |
7ee260fe RM |
729 | loadavg[elem++] |
730 | = (load_ave.tl_lscale == 0 | |
731 | ? load_ave.tl_avenrun.d[0] | |
732 | : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale)); | |
1e5ba5b5 RM |
733 | #endif /* OSF_MIPS */ |
734 | ||
bbd619d7 RS |
735 | #if !defined (LDAV_DONE) && defined (OSF_ALPHA) |
736 | #define LDAV_DONE | |
737 | ||
738 | struct tbl_loadavg load_ave; | |
739 | table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave)); | |
740 | for (elem = 0; elem < nelem; elem++) | |
741 | loadavg[elem] | |
742 | = (load_ave.tl_lscale == 0 | |
743 | ? load_ave.tl_avenrun.d[elem] | |
744 | : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale)); | |
745 | #endif /* OSF_ALPHA */ | |
746 | ||
1e5ba5b5 RM |
747 | #if !defined (LDAV_DONE) && defined (VMS) |
748 | /* VMS specific code -- read from the Load Ave driver. */ | |
749 | ||
750 | LOAD_AVE_TYPE load_ave[3]; | |
751 | static int getloadavg_initialized = 0; | |
752 | #ifdef eunice | |
753 | struct | |
754 | { | |
755 | int dsc$w_length; | |
756 | char *dsc$a_pointer; | |
757 | } descriptor; | |
758 | #endif | |
759 | ||
760 | /* Ensure that there is a channel open to the load ave device. */ | |
761 | if (!getloadavg_initialized) | |
762 | { | |
763 | /* Attempt to open the channel. */ | |
764 | #ifdef eunice | |
765 | descriptor.dsc$w_length = 18; | |
766 | descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE"; | |
767 | #else | |
768 | $DESCRIPTOR (descriptor, "LAV0:"); | |
769 | #endif | |
770 | if (sys$assign (&descriptor, &channel, 0, 0) & 1) | |
771 | getloadavg_initialized = 1; | |
772 | } | |
773 | ||
774 | /* Read the load average vector. */ | |
775 | if (getloadavg_initialized | |
776 | && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0, | |
777 | load_ave, 12, 0, 0, 0, 0) & 1)) | |
778 | { | |
779 | sys$dassgn (channel); | |
780 | getloadavg_initialized = 0; | |
781 | } | |
782 | ||
783 | if (!getloadavg_initialized) | |
784 | return -1; | |
785 | #endif /* VMS */ | |
786 | ||
787 | #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS) | |
788 | ||
789 | /* UNIX-specific code -- read the average from /dev/kmem. */ | |
790 | ||
791 | #define LDAV_PRIVILEGED /* This code requires special installation. */ | |
792 | ||
793 | LOAD_AVE_TYPE load_ave[3]; | |
794 | ||
795 | /* Get the address of LDAV_SYMBOL. */ | |
796 | if (offset == 0) | |
797 | { | |
1e5ba5b5 RM |
798 | #ifndef sgi |
799 | #ifndef NLIST_STRUCT | |
800 | strcpy (nl[0].n_name, LDAV_SYMBOL); | |
801 | strcpy (nl[1].n_name, ""); | |
802 | #else /* NLIST_STRUCT */ | |
803 | #ifdef NLIST_NAME_UNION | |
804 | nl[0].n_un.n_name = LDAV_SYMBOL; | |
805 | nl[1].n_un.n_name = 0; | |
806 | #else /* not NLIST_NAME_UNION */ | |
807 | nl[0].n_name = LDAV_SYMBOL; | |
808 | nl[1].n_name = 0; | |
809 | #endif /* not NLIST_NAME_UNION */ | |
810 | #endif /* NLIST_STRUCT */ | |
811 | ||
fb3ebe6c | 812 | #ifndef SUNOS_5 |
bc77c95c RM |
813 | if ( |
814 | #ifndef _AIX | |
815 | nlist (KERNEL_FILE, nl) | |
816 | #else /* _AIX */ | |
817 | knlist (nl, 1, sizeof (nl[0])) | |
818 | #endif | |
819 | >= 0) | |
820 | /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */ | |
821 | { | |
1e5ba5b5 | 822 | #ifdef FIXUP_KERNEL_SYMBOL_ADDR |
bc77c95c | 823 | FIXUP_KERNEL_SYMBOL_ADDR (nl); |
1e5ba5b5 | 824 | #endif |
bc77c95c RM |
825 | offset = nl[0].n_value; |
826 | } | |
827 | #endif /* !SUNOS_5 */ | |
828 | #else /* sgi */ | |
829 | int ldav_off; | |
1e5ba5b5 | 830 | |
bc77c95c RM |
831 | ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); |
832 | if (ldav_off != -1) | |
833 | offset = (long) ldav_off & 0x7fffffff; | |
1e5ba5b5 | 834 | #endif /* sgi */ |
bc77c95c | 835 | } |
1e5ba5b5 RM |
836 | |
837 | /* Make sure we have /dev/kmem open. */ | |
838 | if (!getloadavg_initialized) | |
839 | { | |
840 | #ifndef SUNOS_5 | |
841 | channel = open ("/dev/kmem", 0); | |
842 | if (channel >= 0) | |
843 | getloadavg_initialized = 1; | |
844 | #else /* SUNOS_5 */ | |
fb3ebe6c DM |
845 | /* We pass 0 for the kernel, corefile, and swapfile names |
846 | to use the currently running kernel. */ | |
1e5ba5b5 RM |
847 | kd = kvm_open (0, 0, 0, O_RDONLY, 0); |
848 | if (kd != 0) | |
849 | { | |
fb3ebe6c | 850 | /* nlist the currently running kernel. */ |
1e5ba5b5 | 851 | kvm_nlist (kd, nl); |
fb3ebe6c | 852 | offset = nl[0].n_value; |
1e5ba5b5 RM |
853 | getloadavg_initialized = 1; |
854 | } | |
855 | #endif /* SUNOS_5 */ | |
856 | } | |
857 | ||
858 | /* If we can, get the load average values. */ | |
859 | if (offset && getloadavg_initialized) | |
860 | { | |
861 | /* Try to read the load. */ | |
862 | #ifndef SUNOS_5 | |
863 | if (lseek (channel, offset, 0) == -1L | |
864 | || read (channel, (char *) load_ave, sizeof (load_ave)) | |
865 | != sizeof (load_ave)) | |
866 | { | |
867 | close (channel); | |
868 | getloadavg_initialized = 0; | |
869 | } | |
870 | #else /* SUNOS_5 */ | |
871 | if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) | |
872 | != sizeof (load_ave)) | |
873 | { | |
874 | kvm_close (kd); | |
875 | getloadavg_initialized = 0; | |
876 | } | |
877 | #endif /* SUNOS_5 */ | |
878 | } | |
879 | ||
880 | if (offset == 0 || !getloadavg_initialized) | |
881 | return -1; | |
882 | #endif /* LOAD_AVE_TYPE and not VMS */ | |
883 | ||
884 | #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */ | |
885 | if (nelem > 0) | |
886 | loadavg[elem++] = LDAV_CVT (load_ave[0]); | |
887 | if (nelem > 1) | |
888 | loadavg[elem++] = LDAV_CVT (load_ave[1]); | |
889 | if (nelem > 2) | |
890 | loadavg[elem++] = LDAV_CVT (load_ave[2]); | |
891 | ||
892 | #define LDAV_DONE | |
893 | #endif /* !LDAV_DONE && LOAD_AVE_TYPE */ | |
894 | ||
895 | #ifdef LDAV_DONE | |
896 | return elem; | |
897 | #else | |
898 | /* Set errno to zero to indicate that there was no particular error; | |
899 | this function just can't work at all on this system. */ | |
900 | errno = 0; | |
901 | return -1; | |
902 | #endif | |
903 | } | |
04402ae3 RM |
904 | |
905 | #endif /* ! HAVE_GETLOADAVG */ | |
1e5ba5b5 RM |
906 | \f |
907 | #ifdef TEST | |
908 | void | |
909 | main (argc, argv) | |
910 | int argc; | |
911 | char **argv; | |
912 | { | |
913 | int naptime = 0; | |
914 | ||
915 | if (argc > 1) | |
916 | naptime = atoi (argv[1]); | |
917 | ||
1e5ba5b5 RM |
918 | while (1) |
919 | { | |
920 | double avg[3]; | |
921 | int loads; | |
922 | ||
923 | errno = 0; /* Don't be misled if it doesn't set errno. */ | |
924 | loads = getloadavg (avg, 3); | |
925 | if (loads == -1) | |
926 | { | |
927 | perror ("Error getting load average"); | |
928 | exit (1); | |
929 | } | |
930 | if (loads > 0) | |
931 | printf ("1-minute: %f ", avg[0]); | |
932 | if (loads > 1) | |
933 | printf ("5-minute: %f ", avg[1]); | |
934 | if (loads > 2) | |
935 | printf ("15-minute: %f ", avg[2]); | |
936 | if (loads > 0) | |
937 | putchar ('\n'); | |
db6730c7 RM |
938 | |
939 | if (naptime == 0) | |
940 | break; | |
1e5ba5b5 RM |
941 | sleep (naptime); |
942 | } | |
db6730c7 RM |
943 | |
944 | exit (0); | |
1e5ba5b5 RM |
945 | } |
946 | #endif /* TEST */ |