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