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