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