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