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