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 | ||
144 | /* VAX C can't handle multi-line #ifs. */ | |
145 | #if (defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(_SEQUENT_) || defined(sgi) || defined(SVR4) || defined(sony_news) || defined(sequent) || defined (OSF_ALPHA) || (defined (ardent) && defined (titan)) || defined (tek4300)) | |
146 | #define LOAD_AVE_TYPE long | |
147 | #endif | |
148 | ||
149 | ||
150 | #ifndef FSCALE | |
151 | ||
152 | /* SunOS and some others define FSCALE in sys/param.h. */ | |
153 | ||
154 | #ifdef MORE_BSD | |
155 | #define FSCALE 2048.0 | |
156 | #endif | |
157 | ||
158 | #if defined(MIPS) || defined(SVR4) || defined(decstation) | |
159 | #define FSCALE 256 | |
160 | #endif | |
161 | ||
162 | #if defined (sgi) || defined (sequent) | |
163 | #define FSCALE 1000.0 | |
164 | #endif | |
165 | ||
166 | #if defined (ardent) && defined (titan) | |
167 | #define FSCALE 65536.0 | |
168 | #endif | |
169 | ||
170 | #ifdef tek4300 | |
171 | #define FSCALE 100.0 | |
172 | #endif | |
173 | ||
174 | #endif /* Not FSCALE. */ | |
175 | ||
176 | #if !defined (LDAV_CVT) && defined (FSCALE) | |
177 | #define LDAV_CVT(n) (((double) (n)) / FSCALE) | |
178 | #endif | |
179 | ||
180 | /* VAX C can't handle multi-line #ifs. */ | |
181 | #if !defined(NLIST_STRUCT) && (defined(MORE_BSD) || defined(sun) || defined(decstation) || defined(hpux) || defined(_SEQUENT_) || defined(sequent) || defined(sgi) || defined(SVR4) || defined(sony_news) || defined (OSF_ALPHA) || (defined (ardent) && defined (titan)) || defined (tek4300) || defined (butterfly)) | |
182 | #define NLIST_STRUCT | |
183 | #endif | |
184 | ||
185 | ||
186 | #if defined(sgi) || (defined(mips) && !defined(BSD)) | |
187 | #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31)) | |
188 | #endif | |
189 | ||
190 | ||
191 | #if !defined (KERNEL_FILE) && defined (sequent) | |
192 | #define KERNEL_FILE "/dynix" | |
193 | #endif | |
194 | ||
195 | #if !defined (KERNEL_FILE) && defined (hpux) | |
196 | #define KERNEL_FILE "/hp-ux" | |
197 | #endif | |
198 | ||
199 | #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan))) | |
200 | #define KERNEL_FILE "/unix" | |
201 | #endif | |
202 | ||
203 | ||
204 | #if !defined (LDAV_SYMBOL) && defined (alliant) | |
205 | #define LDAV_SYMBOL "_Loadavg" | |
206 | #endif | |
207 | ||
208 | #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan))) | |
209 | #define LDAV_SYMBOL "avenrun" | |
210 | #endif | |
211 | ||
212 | #ifdef HAVE_UNISTD_H | |
213 | #include <unistd.h> | |
214 | #endif | |
215 | ||
216 | #include <stdio.h> | |
217 | #include <errno.h> | |
218 | ||
219 | #ifndef errno | |
220 | extern int errno; | |
221 | #endif | |
222 | ||
223 | /* LOAD_AVE_TYPE should only get defined if we're going to use the | |
224 | nlist method. */ | |
225 | #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) | |
226 | #define LOAD_AVE_TYPE double | |
227 | #endif | |
228 | ||
229 | #ifdef LOAD_AVE_TYPE | |
230 | ||
231 | #ifndef VMS | |
232 | #ifndef NLIST_STRUCT | |
233 | #include <a.out.h> | |
234 | #else /* NLIST_STRUCT */ | |
235 | #include <nlist.h> | |
236 | #endif /* NLIST_STRUCT */ | |
237 | ||
238 | #ifdef SUNOS_5 | |
239 | #include <fcntl.h> | |
240 | #include <kvm.h> | |
241 | #endif | |
242 | ||
243 | #ifndef KERNEL_FILE | |
244 | #define KERNEL_FILE "/vmunix" | |
245 | #endif /* KERNEL_FILE */ | |
246 | ||
247 | #ifndef LDAV_SYMBOL | |
248 | #define LDAV_SYMBOL "_avenrun" | |
249 | #endif /* LDAV_SYMBOL */ | |
250 | ||
251 | #else /* VMS */ | |
252 | ||
253 | #ifndef eunice | |
254 | #include <iodef.h> | |
255 | #include <descrip.h> | |
256 | #else /* eunice */ | |
257 | #include <vms/iodef.h> | |
258 | #endif /* eunice */ | |
259 | #endif /* VMS */ | |
260 | ||
261 | #ifndef LDAV_CVT | |
262 | #define LDAV_CVT(n) ((double) (n)) | |
263 | #endif /* !LDAV_CVT */ | |
264 | ||
265 | #endif /* LOAD_AVE_TYPE */ | |
266 | ||
267 | #ifdef NeXT | |
268 | #ifdef HAVE_MACH_MACH_H | |
269 | #include <mach/mach.h> | |
270 | #else | |
271 | #include <mach.h> | |
272 | #endif | |
273 | #endif /* NeXT */ | |
274 | ||
275 | #ifdef sgi | |
276 | #include <sys/sysmp.h> | |
277 | #endif /* sgi */ | |
278 | ||
279 | #ifdef UMAX | |
280 | #include <stdio.h> | |
281 | #include <signal.h> | |
282 | #include <sys/time.h> | |
283 | #include <sys/wait.h> | |
284 | #include <sys/syscall.h> | |
285 | ||
286 | #ifdef UMAX_43 | |
287 | #include <machine/cpu.h> | |
288 | #include <inq_stats/statistics.h> | |
289 | #include <inq_stats/sysstats.h> | |
290 | #include <inq_stats/cpustats.h> | |
291 | #include <inq_stats/procstats.h> | |
292 | #else /* Not UMAX_43. */ | |
293 | #include <sys/sysdefs.h> | |
294 | #include <sys/statistics.h> | |
295 | #include <sys/sysstats.h> | |
296 | #include <sys/cpudefs.h> | |
297 | #include <sys/cpustats.h> | |
298 | #include <sys/procstats.h> | |
299 | #endif /* Not UMAX_43. */ | |
300 | #endif /* UMAX */ | |
301 | ||
302 | #ifdef DGUX | |
303 | #include <sys/dg_sys_info.h> | |
304 | #endif | |
305 | ||
306 | #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION) | |
307 | #include <fcntl.h> | |
308 | #else | |
309 | #include <sys/file.h> | |
310 | #endif | |
311 | \f | |
312 | /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
313 | ||
314 | #ifdef NeXT | |
315 | static processor_set_t default_set; | |
316 | static int getloadavg_initialized; | |
317 | #endif /* NeXT */ | |
318 | ||
319 | #ifdef UMAX | |
320 | static unsigned int cpus = 0; | |
321 | static unsigned int samples; | |
322 | #endif /* UMAX */ | |
323 | ||
324 | #ifdef DGUX | |
325 | static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */ | |
326 | #endif /* DGUX */ | |
327 | ||
328 | #ifdef LOAD_AVE_TYPE | |
329 | /* File descriptor open to /dev/kmem or VMS load ave driver. */ | |
330 | static int channel; | |
331 | /* Nonzero iff channel is valid. */ | |
332 | static int getloadavg_initialized; | |
333 | /* Offset in kmem to seek to read load average, or 0 means invalid. */ | |
334 | static long offset; | |
335 | ||
336 | #if !defined(VMS) && !defined(sgi) | |
337 | static struct nlist nl[2]; | |
338 | #endif /* Not VMS or sgi */ | |
339 | ||
340 | #ifdef SUNOS_5 | |
341 | static kvm_t *kd; | |
342 | #endif /* SUNOS_5 */ | |
343 | ||
344 | #endif /* LOAD_AVE_TYPE */ | |
345 | \f | |
346 | /* Put the 1 minute, 5 minute and 15 minute load averages | |
347 | into the first NELEM elements of LOADAVG. | |
348 | Return the number written (never more than 3), | |
349 | or -1 if an error occurred. */ | |
350 | ||
351 | int | |
352 | getloadavg (loadavg, nelem) | |
353 | double loadavg[]; | |
354 | int nelem; | |
355 | { | |
356 | int elem = 0; /* Return value. */ | |
357 | ||
358 | #if !defined (LDAV_DONE) && defined (LINUX) | |
359 | #define LDAV_DONE | |
360 | #undef LOAD_AVE_TYPE | |
361 | ||
362 | #ifndef LINUX_LDAV_FILE | |
363 | #define LINUX_LDAV_FILE "/proc/loadavg" | |
364 | #endif | |
365 | ||
366 | char ldavgbuf[40]; | |
367 | double load_ave[3]; | |
368 | int fd, count; | |
369 | ||
370 | fd = open (LINUX_LDAV_FILE, O_RDONLY); | |
371 | if (fd == -1) | |
372 | return -1; | |
373 | count = read (fd, ldavgbuf, 40); | |
374 | (void) close (fd); | |
375 | if (count <= 0) | |
376 | return -1; | |
377 | ||
378 | count = sscanf (ldavgbuf, "%lf %lf %lf", | |
379 | &load_ave[0], &load_ave[1], &load_ave[2]); | |
380 | if (count < 1) | |
381 | return -1; | |
382 | ||
383 | for (elem = 0; elem < nelem && elem < count; elem++) | |
384 | loadavg[elem] = load_ave[elem]; | |
385 | ||
386 | return elem; | |
387 | ||
388 | #endif /* LINUX */ | |
389 | ||
390 | #if !defined (LDAV_DONE) && defined (NeXT) | |
391 | #define LDAV_DONE | |
392 | /* The NeXT code was adapted from iscreen 3.2. */ | |
393 | ||
394 | host_t host; | |
395 | struct processor_set_basic_info info; | |
396 | unsigned info_count; | |
397 | ||
398 | if (nelem > 1) | |
399 | { | |
400 | /* We only know how to get the 1-minute average for this system. */ | |
401 | errno = EINVAL; | |
402 | return -1; | |
403 | } | |
404 | ||
405 | if (!getloadavg_initialized) | |
406 | { | |
407 | if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS) | |
408 | getloadavg_initialized = 1; | |
409 | } | |
410 | ||
411 | if (getloadavg_initialized) | |
412 | { | |
413 | info_count = PROCESSOR_SET_BASIC_INFO_COUNT; | |
414 | if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host, | |
415 | (processor_set_info_t) &info, &info_count) | |
416 | != KERN_SUCCESS) | |
417 | getloadavg_initialized = 0; | |
418 | else | |
419 | { | |
420 | if (nelem > 0) | |
421 | loadavg[elem++] = (double) info.load_average / LOAD_SCALE; | |
422 | } | |
423 | } | |
424 | ||
425 | if (!getloadavg_initialized) | |
426 | return -1; | |
427 | #endif /* NeXT */ | |
428 | ||
429 | #if !defined (LDAV_DONE) && defined (UMAX) | |
430 | #define LDAV_DONE | |
431 | /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not | |
432 | have a /dev/kmem. Information about the workings of the running kernel | |
433 | can be gathered with inq_stats system calls. | |
434 | We only know how to get the 1-minute average for this system. */ | |
435 | ||
436 | struct proc_summary proc_sum_data; | |
437 | struct stat_descr proc_info; | |
438 | double load; | |
439 | register unsigned int i, j; | |
440 | ||
441 | if (cpus == 0) | |
442 | { | |
443 | register unsigned int c, i; | |
444 | struct cpu_config conf; | |
445 | struct stat_descr desc; | |
446 | ||
447 | desc.sd_next = 0; | |
448 | desc.sd_subsys = SUBSYS_CPU; | |
449 | desc.sd_type = CPUTYPE_CONFIG; | |
450 | desc.sd_addr = (char *) &conf; | |
451 | desc.sd_size = sizeof conf; | |
452 | ||
453 | if (inq_stats (1, &desc)) | |
454 | return -1; | |
455 | ||
456 | c = 0; | |
457 | for (i = 0; i < conf.config_maxclass; ++i) | |
458 | { | |
459 | struct class_stats stats; | |
460 | bzero ((char *) &stats, sizeof stats); | |
461 | ||
462 | desc.sd_type = CPUTYPE_CLASS; | |
463 | desc.sd_objid = i; | |
464 | desc.sd_addr = (char *) &stats; | |
465 | desc.sd_size = sizeof stats; | |
466 | ||
467 | if (inq_stats (1, &desc)) | |
468 | return -1; | |
469 | ||
470 | c += stats.class_numcpus; | |
471 | } | |
472 | cpus = c; | |
473 | samples = cpus < 2 ? 3 : (2 * cpus / 3); | |
474 | } | |
475 | ||
476 | proc_info.sd_next = 0; | |
477 | proc_info.sd_subsys = SUBSYS_PROC; | |
478 | proc_info.sd_type = PROCTYPE_SUMMARY; | |
479 | proc_info.sd_addr = (char *) &proc_sum_data; | |
480 | proc_info.sd_size = sizeof (struct proc_summary); | |
481 | proc_info.sd_sizeused = 0; | |
482 | ||
483 | if (inq_stats (1, &proc_info) != 0) | |
484 | return -1; | |
485 | ||
486 | load = proc_sum_data.ps_nrunnable; | |
487 | j = 0; | |
488 | for (i = samples - 1; i > 0; --i) | |
489 | { | |
490 | load += proc_sum_data.ps_nrun[j]; | |
491 | if (j++ == PS_NRUNSIZE) | |
492 | j = 0; | |
493 | } | |
494 | ||
495 | if (nelem > 0) | |
496 | loadavg[elem++] = load / samples / cpus; | |
497 | #endif /* UMAX */ | |
498 | ||
499 | #if !defined (LDAV_DONE) && defined (DGUX) | |
500 | #define LDAV_DONE | |
501 | /* This call can return -1 for an error, but with good args | |
502 | it's not supposed to fail. The first argument is for no | |
503 | apparent reason of type `long int *'. */ | |
504 | dg_sys_info ((long int *) &load_info, | |
505 | DG_SYS_INFO_LOAD_INFO_TYPE, | |
506 | DG_SYS_INFO_LOAD_VERSION_0); | |
507 | ||
508 | if (nelem > 0) | |
509 | loadavg[elem++] = load_info.one_minute; | |
510 | if (nelem > 1) | |
511 | loadavg[elem++] = load_info.five_minute; | |
512 | if (nelem > 2) | |
513 | loadavg[elem++] = load_info.fifteen_minute; | |
514 | #endif /* DGUX */ | |
515 | ||
516 | #if !defined (LDAV_DONE) && defined (apollo) | |
517 | #define LDAV_DONE | |
518 | /* Apollo code from lisch@mentorg.com (Ray Lischner). | |
519 | ||
520 | This system call is not documented. The load average is obtained as | |
521 | three long integers, for the load average over the past minute, | |
522 | five minutes, and fifteen minutes. Each value is a scaled integer, | |
523 | with 16 bits of integer part and 16 bits of fraction part. | |
524 | ||
525 | I'm not sure which operating system first supported this system call, | |
526 | but I know that SR10.2 supports it. */ | |
527 | ||
528 | extern void proc1_$get_loadav (); | |
529 | unsigned long load_ave[3]; | |
530 | ||
531 | proc1_$get_loadav (load_ave); | |
532 | ||
533 | if (nelem > 0) | |
534 | loadavg[elem++] = load_ave[0] / 65536.0; | |
535 | if (nelem > 1) | |
536 | loadavg[elem++] = load_ave[1] / 65536.0; | |
537 | if (nelem > 2) | |
538 | loadavg[elem++] = load_ave[2] / 65536.0; | |
539 | #endif /* apollo */ | |
540 | ||
541 | #if !defined (LDAV_DONE) && defined (OSF_MIPS) | |
542 | #define LDAV_DONE | |
543 | #define LDAV_PRIVILEGED | |
544 | ||
545 | struct tbl_loadavg load_ave; | |
546 | table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave)); | |
547 | loadavg[elem++] = (load_ave.tl_lscale == 0 | |
548 | ? load_ave.tl_avenrun.d[0] | |
549 | : (load_ave.tl_avenrun.l[0] / load_ave.tl_lscale)); | |
550 | #endif /* OSF_MIPS */ | |
551 | ||
552 | #if !defined (LDAV_DONE) && defined (VMS) | |
553 | /* VMS specific code -- read from the Load Ave driver. */ | |
554 | ||
555 | LOAD_AVE_TYPE load_ave[3]; | |
556 | static int getloadavg_initialized = 0; | |
557 | #ifdef eunice | |
558 | struct | |
559 | { | |
560 | int dsc$w_length; | |
561 | char *dsc$a_pointer; | |
562 | } descriptor; | |
563 | #endif | |
564 | ||
565 | /* Ensure that there is a channel open to the load ave device. */ | |
566 | if (!getloadavg_initialized) | |
567 | { | |
568 | /* Attempt to open the channel. */ | |
569 | #ifdef eunice | |
570 | descriptor.dsc$w_length = 18; | |
571 | descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE"; | |
572 | #else | |
573 | $DESCRIPTOR (descriptor, "LAV0:"); | |
574 | #endif | |
575 | if (sys$assign (&descriptor, &channel, 0, 0) & 1) | |
576 | getloadavg_initialized = 1; | |
577 | } | |
578 | ||
579 | /* Read the load average vector. */ | |
580 | if (getloadavg_initialized | |
581 | && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0, | |
582 | load_ave, 12, 0, 0, 0, 0) & 1)) | |
583 | { | |
584 | sys$dassgn (channel); | |
585 | getloadavg_initialized = 0; | |
586 | } | |
587 | ||
588 | if (!getloadavg_initialized) | |
589 | return -1; | |
590 | #endif /* VMS */ | |
591 | ||
592 | #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS) | |
593 | ||
594 | /* UNIX-specific code -- read the average from /dev/kmem. */ | |
595 | ||
596 | #define LDAV_PRIVILEGED /* This code requires special installation. */ | |
597 | ||
598 | LOAD_AVE_TYPE load_ave[3]; | |
599 | ||
600 | /* Get the address of LDAV_SYMBOL. */ | |
601 | if (offset == 0) | |
602 | { | |
603 | #ifndef SUNOS_5 | |
604 | #ifndef sgi | |
605 | #ifndef NLIST_STRUCT | |
606 | strcpy (nl[0].n_name, LDAV_SYMBOL); | |
607 | strcpy (nl[1].n_name, ""); | |
608 | #else /* NLIST_STRUCT */ | |
609 | #ifdef NLIST_NAME_UNION | |
610 | nl[0].n_un.n_name = LDAV_SYMBOL; | |
611 | nl[1].n_un.n_name = 0; | |
612 | #else /* not NLIST_NAME_UNION */ | |
613 | nl[0].n_name = LDAV_SYMBOL; | |
614 | nl[1].n_name = 0; | |
615 | #endif /* not NLIST_NAME_UNION */ | |
616 | #endif /* NLIST_STRUCT */ | |
617 | ||
618 | if (nlist (KERNEL_FILE, nl) >= 0) | |
619 | /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */ | |
620 | { | |
621 | #ifdef FIXUP_KERNEL_SYMBOL_ADDR | |
622 | FIXUP_KERNEL_SYMBOL_ADDR (nl); | |
623 | #endif | |
624 | offset = nl[0].n_value; | |
625 | } | |
626 | #else /* sgi */ | |
627 | int ldav_off; | |
628 | ||
629 | ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); | |
630 | if (ldav_off != -1) | |
631 | offset = (long) ldav_off & 0x7fffffff; | |
632 | #endif /* sgi */ | |
633 | #endif /* !SUNOS_5 */ | |
634 | } | |
635 | ||
636 | /* Make sure we have /dev/kmem open. */ | |
637 | if (!getloadavg_initialized) | |
638 | { | |
639 | #ifndef SUNOS_5 | |
640 | channel = open ("/dev/kmem", 0); | |
641 | if (channel >= 0) | |
642 | getloadavg_initialized = 1; | |
643 | #else /* SUNOS_5 */ | |
644 | kd = kvm_open (0, 0, 0, O_RDONLY, 0); | |
645 | if (kd != 0) | |
646 | { | |
647 | kvm_nlist (kd, nl); | |
648 | getloadavg_initialized = 1; | |
649 | } | |
650 | #endif /* SUNOS_5 */ | |
651 | } | |
652 | ||
653 | /* If we can, get the load average values. */ | |
654 | if (offset && getloadavg_initialized) | |
655 | { | |
656 | /* Try to read the load. */ | |
657 | #ifndef SUNOS_5 | |
658 | if (lseek (channel, offset, 0) == -1L | |
659 | || read (channel, (char *) load_ave, sizeof (load_ave)) | |
660 | != sizeof (load_ave)) | |
661 | { | |
662 | close (channel); | |
663 | getloadavg_initialized = 0; | |
664 | } | |
665 | #else /* SUNOS_5 */ | |
666 | if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) | |
667 | != sizeof (load_ave)) | |
668 | { | |
669 | kvm_close (kd); | |
670 | getloadavg_initialized = 0; | |
671 | } | |
672 | #endif /* SUNOS_5 */ | |
673 | } | |
674 | ||
675 | if (offset == 0 || !getloadavg_initialized) | |
676 | return -1; | |
677 | #endif /* LOAD_AVE_TYPE and not VMS */ | |
678 | ||
679 | #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */ | |
680 | if (nelem > 0) | |
681 | loadavg[elem++] = LDAV_CVT (load_ave[0]); | |
682 | if (nelem > 1) | |
683 | loadavg[elem++] = LDAV_CVT (load_ave[1]); | |
684 | if (nelem > 2) | |
685 | loadavg[elem++] = LDAV_CVT (load_ave[2]); | |
686 | ||
687 | #define LDAV_DONE | |
688 | #endif /* !LDAV_DONE && LOAD_AVE_TYPE */ | |
689 | ||
690 | #ifdef LDAV_DONE | |
691 | return elem; | |
692 | #else | |
693 | /* Set errno to zero to indicate that there was no particular error; | |
694 | this function just can't work at all on this system. */ | |
695 | errno = 0; | |
696 | return -1; | |
697 | #endif | |
698 | } | |
699 | \f | |
700 | #ifdef TEST | |
701 | void | |
702 | main (argc, argv) | |
703 | int argc; | |
704 | char **argv; | |
705 | { | |
706 | int naptime = 0; | |
707 | ||
708 | if (argc > 1) | |
709 | naptime = atoi (argv[1]); | |
710 | ||
711 | if (naptime == 0) | |
712 | naptime = 5; | |
713 | ||
714 | while (1) | |
715 | { | |
716 | double avg[3]; | |
717 | int loads; | |
718 | ||
719 | errno = 0; /* Don't be misled if it doesn't set errno. */ | |
720 | loads = getloadavg (avg, 3); | |
721 | if (loads == -1) | |
722 | { | |
723 | perror ("Error getting load average"); | |
724 | exit (1); | |
725 | } | |
726 | if (loads > 0) | |
727 | printf ("1-minute: %f ", avg[0]); | |
728 | if (loads > 1) | |
729 | printf ("5-minute: %f ", avg[1]); | |
730 | if (loads > 2) | |
731 | printf ("15-minute: %f ", avg[2]); | |
732 | if (loads > 0) | |
733 | putchar ('\n'); | |
734 | sleep (naptime); | |
735 | } | |
736 | } | |
737 | #endif /* TEST */ |