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