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