0
|
1 /* Get the system load averages.
|
16
|
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995
|
84
|
3 Free Software Foundation, Inc.
|
|
4
|
|
5 This file is part of XEmacs.
|
0
|
6
|
84
|
7 XEmacs is free software; you can redistribute it and/or modify it
|
|
8 under the terms of the GNU General Public License as published by the
|
|
9 Free Software Foundation; either version 2, or (at your option) any
|
|
10 later version.
|
0
|
11
|
84
|
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 for more details.
|
0
|
16
|
84
|
17 You should have received a copy of the GNU General Public License
|
|
18 along with XEmacs; see the file COPYING. If not, write to
|
|
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
20 Boston, MA 02111-1307, USA. */
|
|
21
|
231
|
22 #ifndef __CYGWIN32__
|
0
|
23
|
|
24 /* Compile-time symbols that this file uses:
|
|
25
|
|
26 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
|
|
27 KERNEL_FILE Pathname of the kernel to nlist.
|
|
28 LDAV_CVT() Scale the load average from the kernel.
|
|
29 Returns a double.
|
|
30 LDAV_SYMBOL Name of kernel symbol giving load average.
|
|
31 LOAD_AVE_TYPE Type of the load average array in the kernel.
|
|
32 Must be defined unless one of
|
|
33 apollo, DGUX, NeXT, or UMAX is defined;
|
|
34 otherwise, no load average is available.
|
|
35 NLIST_STRUCT Include nlist.h, not a.out.h, and
|
|
36 the nlist n_name element is a pointer,
|
|
37 not an array.
|
|
38 NLIST_NAME_UNION struct nlist has an n_un member, not n_name.
|
|
39 LINUX_LDAV_FILE [__linux__]: File containing load averages.
|
|
40
|
|
41 Specific system predefines this file uses, aside from setting
|
|
42 default values if not emacs:
|
|
43
|
|
44 apollo
|
|
45 BSD Real BSD, not just BSD-like.
|
|
46 convex
|
|
47 DGUX
|
|
48 hpux
|
|
49 MSDOS No-op for MSDOS.
|
|
50 NeXT
|
|
51 sgi
|
|
52 sequent Sequent Dynix 3.x.x (BSD)
|
|
53 _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV)
|
|
54 sony_news NEWS-OS (works at least for 4.1C)
|
|
55 UMAX
|
|
56 UMAX4_3
|
|
57 WIN32 No-op for Windows95/NT.
|
|
58 __linux__ Linux: assumes /proc filesystem mounted.
|
|
59 Support from Michael K. Johnson.
|
|
60 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
|
|
61
|
|
62 In addition, to avoid nesting many #ifdefs, we internally set
|
|
63 LDAV_DONE to indicate that the load average has been computed.
|
|
64
|
|
65 We also #define LDAV_PRIVILEGED if a program will require
|
|
66 special installation to be able to call getloadavg. */
|
|
67
|
|
68 /* This should always be first. */
|
|
69 #ifdef HAVE_CONFIG_H
|
|
70 #include <config.h>
|
|
71 #endif
|
|
72
|
|
73 #include <sys/types.h>
|
|
74
|
|
75 /* Both the Emacs and non-Emacs sections want this. Some
|
|
76 configuration files' definitions for the LOAD_AVE_CVT macro (like
|
|
77 sparc.h's) use macros like FSCALE, defined here. */
|
|
78 #ifdef unix
|
|
79 #include <sys/param.h>
|
|
80 #endif
|
|
81
|
48
|
82 #ifdef XEMACS
|
272
|
83 #include "lisp.h"
|
|
84 #include "sysfile.h" /* for encapsulated open, close, read, write */
|
48
|
85 #endif /* XEMACS */
|
0
|
86
|
|
87 /* Exclude all the code except the test program at the end
|
|
88 if the system has its own `getloadavg' function.
|
|
89
|
|
90 The declaration of `errno' is needed by the test program
|
|
91 as well as the function itself, so it comes first. */
|
|
92
|
|
93 #include <errno.h>
|
|
94
|
|
95 #ifndef errno
|
|
96 extern int errno;
|
|
97 #endif
|
|
98
|
|
99 #ifndef HAVE_GETLOADAVG
|
|
100
|
|
101 /* The existing Emacs configuration files define a macro called
|
|
102 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
|
|
103 returns the load average multiplied by 100. What we actually want
|
|
104 is a macro called LDAV_CVT, which returns the load average as an
|
|
105 unmultiplied double.
|
|
106
|
|
107 For backwards compatibility, we'll define LDAV_CVT in terms of
|
|
108 LOAD_AVE_CVT, but future machine config files should just define
|
|
109 LDAV_CVT directly. */
|
|
110
|
|
111 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
|
|
112 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
|
|
113 #endif
|
|
114
|
48
|
115 #ifdef XEMACS
|
|
116 #if defined (HAVE_KSTAT_H)
|
|
117 #include <kstat.h>
|
|
118 #endif /* HAVE_KSTAT_H */
|
|
119 #endif /* XEMACS */
|
|
120
|
0
|
121 #if !defined (BSD) && defined (ultrix)
|
|
122 /* Ultrix behaves like BSD on Vaxen. */
|
|
123 #define BSD
|
|
124 #endif
|
|
125
|
|
126 #ifdef NeXT
|
|
127 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
|
|
128 conflicts with the definition understood in this file, that this
|
|
129 really is BSD. */
|
|
130 #undef BSD
|
|
131
|
|
132 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
|
|
133 defined to mean that the nlist method should be used, which is not true. */
|
|
134 #undef FSCALE
|
|
135 #endif
|
|
136
|
|
137 /* Set values that are different from the defaults, which are
|
|
138 set a little farther down with #ifndef. */
|
|
139
|
|
140
|
|
141 /* Some shorthands. */
|
|
142
|
|
143 #if defined (HPUX) && !defined (hpux)
|
|
144 #define hpux
|
|
145 #endif
|
|
146
|
|
147 #if defined(hp300) && !defined(hpux)
|
|
148 #define MORE_BSD
|
|
149 #endif
|
|
150
|
|
151 #if defined(ultrix) && defined(mips)
|
|
152 #define decstation
|
|
153 #endif
|
|
154
|
48
|
155 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
|
0
|
156 #define SUNOS_5
|
|
157 #endif
|
|
158
|
|
159 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
|
|
160 #define OSF_ALPHA
|
151
|
161 #include <netdb.h>
|
|
162 #include <netinet/in.h> /* Needed for Digital UNIX V3 */
|
|
163 #include <net/proto_net.h>
|
0
|
164 #include <sys/table.h>
|
|
165 #endif
|
|
166
|
|
167 #if defined (__osf__) && (defined (mips) || defined (__mips__))
|
|
168 #define OSF_MIPS
|
|
169 #include <sys/table.h>
|
|
170 #endif
|
|
171
|
|
172 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
|
|
173 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
|
|
174 that with a couple of other things and we'll have a unique match. */
|
|
175 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
|
|
176 #define tek4300 /* Define by emacs, but not by other users. */
|
|
177 #endif
|
|
178
|
|
179
|
|
180 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
|
|
181 #ifndef LOAD_AVE_TYPE
|
|
182
|
|
183 #ifdef MORE_BSD
|
|
184 #define LOAD_AVE_TYPE long
|
|
185 #endif
|
|
186
|
|
187 #ifdef sun
|
|
188 #define LOAD_AVE_TYPE long
|
|
189 #endif
|
|
190
|
|
191 #ifdef decstation
|
|
192 #define LOAD_AVE_TYPE long
|
|
193 #endif
|
|
194
|
|
195 #ifdef _SEQUENT_
|
|
196 #define LOAD_AVE_TYPE long
|
|
197 #endif
|
|
198
|
|
199 #ifdef sgi
|
|
200 #define LOAD_AVE_TYPE long
|
|
201 #endif
|
|
202
|
|
203 #ifdef SVR4
|
|
204 #define LOAD_AVE_TYPE long
|
|
205 #endif
|
|
206
|
|
207 #ifdef sony_news
|
|
208 #define LOAD_AVE_TYPE long
|
|
209 #endif
|
|
210
|
|
211 #ifdef sequent
|
|
212 #define LOAD_AVE_TYPE long
|
|
213 #endif
|
|
214
|
|
215 #ifdef OSF_ALPHA
|
|
216 #define LOAD_AVE_TYPE long
|
|
217 #endif
|
|
218
|
|
219 #if defined (ardent) && defined (titan)
|
|
220 #define LOAD_AVE_TYPE long
|
|
221 #endif
|
|
222
|
|
223 #ifdef tek4300
|
|
224 #define LOAD_AVE_TYPE long
|
|
225 #endif
|
|
226
|
|
227 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
228 #define LOAD_AVE_TYPE long
|
|
229 #endif
|
|
230
|
|
231 #ifdef _AIX
|
|
232 #define LOAD_AVE_TYPE long
|
|
233 #endif
|
|
234
|
|
235 #ifdef convex
|
|
236 #define LOAD_AVE_TYPE double
|
|
237 #ifndef LDAV_CVT
|
|
238 #define LDAV_CVT(n) (n)
|
|
239 #endif
|
|
240 #endif
|
|
241
|
|
242 #endif /* No LOAD_AVE_TYPE. */
|
|
243
|
|
244 #ifdef OSF_ALPHA
|
|
245 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
|
|
246 according to ghazi@noc.rutgers.edu. */
|
|
247 #undef FSCALE
|
|
248 #define FSCALE 1024.0
|
|
249 #endif
|
|
250
|
|
251 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
252 /* <sys/param.h> defines an incorrect value for FSCALE on an
|
|
253 Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */
|
|
254 #undef FSCALE
|
|
255 #define FSCALE 100.0
|
|
256 #endif
|
|
257
|
|
258
|
|
259 #ifndef FSCALE
|
|
260
|
|
261 /* SunOS and some others define FSCALE in sys/param.h. */
|
|
262
|
|
263 #ifdef MORE_BSD
|
|
264 #define FSCALE 2048.0
|
|
265 #endif
|
|
266
|
|
267 #if defined(MIPS) || defined(SVR4) || defined(decstation)
|
|
268 #define FSCALE 256
|
|
269 #endif
|
|
270
|
|
271 #if defined (sgi) || defined (sequent)
|
|
272 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
|
|
273 above under #ifdef MIPS. But we want the sgi value. */
|
|
274 #undef FSCALE
|
|
275 #define FSCALE 1000.0
|
|
276 #endif
|
|
277
|
|
278 #if defined (ardent) && defined (titan)
|
|
279 #define FSCALE 65536.0
|
|
280 #endif
|
|
281
|
|
282 #ifdef tek4300
|
|
283 #define FSCALE 100.0
|
|
284 #endif
|
|
285
|
|
286 #ifdef _AIX
|
|
287 #define FSCALE 65536.0
|
|
288 #endif
|
|
289
|
|
290 #endif /* Not FSCALE. */
|
|
291
|
|
292 #if !defined (LDAV_CVT) && defined (FSCALE)
|
|
293 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
|
|
294 #endif
|
|
295
|
|
296 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
|
|
297 #ifndef NLIST_STRUCT
|
|
298
|
|
299 #ifdef MORE_BSD
|
|
300 #define NLIST_STRUCT
|
|
301 #endif
|
|
302
|
|
303 #ifdef sun
|
|
304 #define NLIST_STRUCT
|
|
305 #endif
|
|
306
|
|
307 #ifdef decstation
|
|
308 #define NLIST_STRUCT
|
|
309 #endif
|
|
310
|
|
311 #ifdef hpux
|
|
312 #define NLIST_STRUCT
|
|
313 #endif
|
|
314
|
|
315 #if defined (_SEQUENT_) || defined (sequent)
|
|
316 #define NLIST_STRUCT
|
|
317 #endif
|
|
318
|
|
319 #ifdef sgi
|
|
320 #define NLIST_STRUCT
|
|
321 #endif
|
|
322
|
|
323 #ifdef SVR4
|
|
324 #define NLIST_STRUCT
|
|
325 #endif
|
|
326
|
|
327 #ifdef sony_news
|
|
328 #define NLIST_STRUCT
|
|
329 #endif
|
|
330
|
|
331 #ifdef OSF_ALPHA
|
|
332 #define NLIST_STRUCT
|
|
333 #endif
|
|
334
|
|
335 #if defined (ardent) && defined (titan)
|
|
336 #define NLIST_STRUCT
|
|
337 #endif
|
|
338
|
|
339 #ifdef tek4300
|
|
340 #define NLIST_STRUCT
|
|
341 #endif
|
|
342
|
|
343 #ifdef butterfly
|
|
344 #define NLIST_STRUCT
|
|
345 #endif
|
|
346
|
|
347 #if defined(alliant) && defined(i860) /* Alliant FX/2800 */
|
|
348 #define NLIST_STRUCT
|
|
349 #endif
|
|
350
|
|
351 #ifdef _AIX
|
|
352 #define NLIST_STRUCT
|
|
353 #endif
|
|
354
|
|
355 #endif /* defined (NLIST_STRUCT) */
|
|
356
|
|
357
|
|
358 #if defined(sgi) || (defined(mips) && !defined(BSD))
|
|
359 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
|
|
360 #endif
|
|
361
|
|
362
|
|
363 #if !defined (KERNEL_FILE) && defined (sequent)
|
|
364 #define KERNEL_FILE "/dynix"
|
|
365 #endif
|
|
366
|
|
367 #if !defined (KERNEL_FILE) && defined (hpux)
|
|
368 #define KERNEL_FILE "/hp-ux"
|
|
369 #endif
|
|
370
|
|
371 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
|
|
372 #define KERNEL_FILE "/unix"
|
|
373 #endif
|
|
374
|
|
375
|
|
376 #if !defined (LDAV_SYMBOL) && defined (alliant)
|
|
377 #define LDAV_SYMBOL "_Loadavg"
|
|
378 #endif
|
|
379
|
|
380 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
|
|
381 #define LDAV_SYMBOL "avenrun"
|
|
382 #endif
|
|
383
|
48
|
384 #ifdef HAVE_UNISTD_H
|
0
|
385 #include <unistd.h>
|
48
|
386 #endif
|
0
|
387
|
|
388 #include <stdio.h>
|
|
389
|
|
390 /* LOAD_AVE_TYPE should only get defined if we're going to use the
|
|
391 nlist method. */
|
|
392 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
|
|
393 #define LOAD_AVE_TYPE double
|
|
394 #endif
|
|
395
|
|
396 #ifdef LOAD_AVE_TYPE
|
|
397
|
|
398 #ifndef NLIST_STRUCT
|
|
399 #include <a.out.h>
|
|
400 #else /* NLIST_STRUCT */
|
|
401 #include <nlist.h>
|
|
402 #endif /* NLIST_STRUCT */
|
|
403
|
|
404 #ifdef SUNOS_5
|
|
405 #include <fcntl.h>
|
|
406 #include <kvm.h>
|
|
407 #endif
|
|
408
|
|
409 #ifndef KERNEL_FILE
|
|
410 #define KERNEL_FILE "/vmunix"
|
|
411 #endif /* KERNEL_FILE */
|
|
412
|
|
413 #ifndef LDAV_SYMBOL
|
|
414 #define LDAV_SYMBOL "_avenrun"
|
|
415 #endif /* LDAV_SYMBOL */
|
|
416
|
|
417 #ifndef LDAV_CVT
|
|
418 #define LDAV_CVT(n) ((double) (n))
|
|
419 #endif /* !LDAV_CVT */
|
|
420
|
|
421 #endif /* LOAD_AVE_TYPE */
|
|
422
|
|
423 #ifdef NeXT
|
|
424 #ifdef HAVE_MACH_MACH_H
|
|
425 #include <mach/mach.h>
|
|
426 #else
|
|
427 #include <mach.h>
|
|
428 #endif
|
|
429 #endif /* NeXT */
|
|
430
|
|
431 #ifdef sgi
|
|
432 #include <sys/sysmp.h>
|
|
433 #endif /* sgi */
|
|
434
|
|
435 #ifdef UMAX
|
|
436 #include <stdio.h>
|
|
437 #include <signal.h>
|
|
438 #include <sys/time.h>
|
|
439 #include <sys/wait.h>
|
|
440 #include <sys/syscall.h>
|
|
441
|
|
442 #ifdef UMAX_43
|
|
443 #include <machine/cpu.h>
|
|
444 #include <inq_stats/statistics.h>
|
|
445 #include <inq_stats/sysstats.h>
|
|
446 #include <inq_stats/cpustats.h>
|
|
447 #include <inq_stats/procstats.h>
|
|
448 #else /* Not UMAX_43. */
|
|
449 #include <sys/sysdefs.h>
|
|
450 #include <sys/statistics.h>
|
|
451 #include <sys/sysstats.h>
|
|
452 #include <sys/cpudefs.h>
|
|
453 #include <sys/cpustats.h>
|
|
454 #include <sys/procstats.h>
|
|
455 #endif /* Not UMAX_43. */
|
|
456 #endif /* UMAX */
|
|
457
|
|
458 #ifdef DGUX
|
|
459 #include <sys/dg_sys_info.h>
|
|
460 #endif
|
|
461
|
48
|
462 #ifdef XEMACS
|
|
463 #if defined (HAVE_SYS_PSTAT_H)
|
0
|
464 #include <sys/pstat.h>
|
48
|
465 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */
|
|
466 #endif /* XEMACS */
|
0
|
467
|
|
468 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
|
|
469 #include <fcntl.h>
|
|
470 #else
|
|
471 #include <sys/file.h>
|
|
472 #endif
|
|
473
|
|
474 /* Avoid static vars inside a function since in HPUX they dump as pure. */
|
|
475
|
|
476 #ifdef NeXT
|
|
477 static processor_set_t default_set;
|
|
478 static int getloadavg_initialized;
|
|
479 #endif /* NeXT */
|
|
480
|
|
481 #ifdef UMAX
|
|
482 static unsigned int cpus = 0;
|
|
483 static unsigned int samples;
|
|
484 #endif /* UMAX */
|
|
485
|
|
486 #ifdef DGUX
|
|
487 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
|
|
488 #endif /* DGUX */
|
|
489
|
|
490 #ifdef LOAD_AVE_TYPE
|
209
|
491 /* File descriptor open to /dev/kmem */
|
0
|
492 static int channel;
|
|
493 /* Nonzero iff channel is valid. */
|
|
494 static int getloadavg_initialized;
|
|
495 /* Offset in kmem to seek to read load average, or 0 means invalid. */
|
|
496 static long offset;
|
|
497
|
209
|
498 #ifndef sgi
|
0
|
499 static struct nlist nl[2];
|
209
|
500 #endif /* not sgi */
|
0
|
501
|
|
502 #ifdef SUNOS_5
|
|
503 static kvm_t *kd;
|
|
504 #endif /* SUNOS_5 */
|
|
505
|
278
|
506 #ifndef countof
|
|
507 # define countof(x) (sizeof (x) / sizeof (*(x)))
|
|
508 #endif
|
|
509
|
0
|
510 #endif /* LOAD_AVE_TYPE */
|
|
511
|
|
512 /* Put the 1 minute, 5 minute and 15 minute load averages
|
|
513 into the first NELEM elements of LOADAVG.
|
|
514 Return the number written (never more than 3, but may be less than NELEM),
|
|
515 or -1 if an error occurred. */
|
|
516
|
|
517 int
|
84
|
518 getloadavg (double loadavg[], int nelem)
|
0
|
519 {
|
|
520 int elem = 0; /* Return value. */
|
|
521
|
|
522 #ifdef NO_GET_LOAD_AVG
|
|
523 #define LDAV_DONE
|
|
524 /* Set errno to zero to indicate that there was no particular error;
|
|
525 this function just can't work at all on this system. */
|
|
526 errno = 0;
|
278
|
527 elem = -2;
|
280
|
528 #endif /* NO_GET_LOAD_AVG */
|
0
|
529
|
48
|
530 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
|
|
531 #define LDAV_DONE
|
278
|
532 /* getloadavg is best implemented using kstat (kernel stats), on
|
|
533 systems (like SunOS5) that support it, since you don't need special
|
|
534 privileges to use it.
|
185
|
535
|
278
|
536 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>.
|
|
537 Integrated to XEmacs by Hrvoje Niksic <hniksic@srce.hr>.
|
|
538 Additional cleanup by Hrvoje Niksic, based on code published by
|
|
539 Casper Dik <Casper.Dik@Holland.Sun.Com>. */
|
|
540 kstat_ctl_t *kc;
|
|
541 kstat_t *ksp;
|
|
542 static char *avestrings[] = { "avenrun_1min",
|
|
543 "avenrun_5min",
|
|
544 "avenrun_15min" };
|
48
|
545
|
278
|
546 if (nelem > countof (avestrings))
|
|
547 nelem = countof (avestrings);
|
|
548
|
280
|
549 kc = kstat_open ();
|
278
|
550 if (!kc)
|
|
551 return -1;
|
|
552 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
|
|
553 if (!ksp)
|
48
|
554 {
|
278
|
555 kstat_close (kc);
|
|
556 return -1;
|
48
|
557 }
|
278
|
558 if (kstat_read (kc, ksp, 0) < 0)
|
|
559 {
|
|
560 kstat_close (kc);
|
48
|
561 return -1;
|
278
|
562 }
|
48
|
563 for (elem = 0; elem < nelem; elem++)
|
278
|
564 {
|
|
565 kstat_named_t *kn = kstat_data_lookup (ksp, avestrings[elem]);
|
|
566 if (!kn)
|
|
567 {
|
|
568 kstat_close (kc);
|
|
569 return -1;
|
|
570 }
|
280
|
571 loadavg[elem] = (double)kn->value.ul / FSCALE;
|
278
|
572 }
|
|
573 kstat_close (kc);
|
48
|
574 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
|
82
|
575
|
|
576 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
|
|
577 #define LDAV_DONE
|
|
578 /* This is totally undocumented, and is not guaranteed to work, but
|
|
579 mayhap it might .... If it does work, it will work only on HP-UX
|
|
580 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
|
|
581 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */
|
|
582 #undef LOAD_AVE_CVT
|
|
583 #undef LDAV_SYMBOL
|
|
584 struct pst_dynamic procinfo;
|
|
585 union pstun statbuf;
|
|
586
|
|
587 statbuf.pst_dynamic = &procinfo;
|
|
588 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
|
|
589 return (-1);
|
|
590 loadavg[elem++] = procinfo.psd_avg_1_min;
|
|
591 loadavg[elem++] = procinfo.psd_avg_5_min;
|
|
592 loadavg[elem++] = procinfo.psd_avg_15_min;
|
|
593 #endif /* HPUX */
|
|
594
|
0
|
595 #if !defined (LDAV_DONE) && defined (__linux__)
|
|
596 #define LDAV_DONE
|
|
597 #undef LOAD_AVE_TYPE
|
|
598
|
|
599 #ifndef LINUX_LDAV_FILE
|
|
600 #define LINUX_LDAV_FILE "/proc/loadavg"
|
|
601 #endif
|
|
602
|
|
603 char ldavgbuf[40];
|
|
604 double load_ave[3];
|
|
605 int fd, count;
|
|
606
|
|
607 fd = open (LINUX_LDAV_FILE, O_RDONLY);
|
|
608 if (fd == -1)
|
|
609 return -1;
|
|
610 count = read (fd, ldavgbuf, 40);
|
|
611 (void) close (fd);
|
|
612 if (count <= 0)
|
|
613 return -1;
|
|
614
|
|
615 count = sscanf (ldavgbuf, "%lf %lf %lf",
|
|
616 &load_ave[0], &load_ave[1], &load_ave[2]);
|
|
617 if (count < 1)
|
|
618 return -1;
|
|
619
|
|
620 for (elem = 0; elem < nelem && elem < count; elem++)
|
|
621 loadavg[elem] = load_ave[elem];
|
|
622 #endif /* __linux__ */
|
|
623
|
|
624 #if !defined (LDAV_DONE) && defined (__NetBSD__)
|
|
625 #define LDAV_DONE
|
|
626 #undef LOAD_AVE_TYPE
|
|
627
|
|
628 #ifndef NETBSD_LDAV_FILE
|
|
629 #define NETBSD_LDAV_FILE "/kern/loadavg"
|
|
630 #endif
|
|
631
|
|
632 unsigned long int load_ave[3], scale;
|
|
633 int count;
|
|
634 FILE *fp;
|
|
635
|
|
636 fp = fopen (NETBSD_LDAV_FILE, "r");
|
|
637 if (fp == NULL)
|
|
638 return -1;
|
|
639 count = fscanf (fp, "%lu %lu %lu %lu\n",
|
|
640 &load_ave[0], &load_ave[1], &load_ave[2],
|
|
641 &scale);
|
|
642 (void) fclose (fp);
|
|
643 if (count != 4)
|
|
644 return -1;
|
|
645
|
|
646 for (elem = 0; elem < nelem; elem++)
|
|
647 loadavg[elem] = (double) load_ave[elem] / (double) scale;
|
|
648 #endif /* __NetBSD__ */
|
|
649
|
|
650 #if !defined (LDAV_DONE) && defined (NeXT)
|
|
651 #define LDAV_DONE
|
|
652 /* The NeXT code was adapted from iscreen 3.2. */
|
|
653
|
|
654 host_t host;
|
|
655 struct processor_set_basic_info info;
|
|
656 unsigned info_count;
|
|
657
|
48
|
658 /* We only know how to get the 1-minute average for this system,
|
|
659 so even if the caller asks for more than 1, we only return 1. */
|
0
|
660
|
|
661 if (!getloadavg_initialized)
|
|
662 {
|
|
663 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
|
|
664 getloadavg_initialized = 1;
|
|
665 }
|
|
666
|
|
667 if (getloadavg_initialized)
|
|
668 {
|
|
669 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
|
|
670 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
|
|
671 (processor_set_info_t) &info, &info_count)
|
|
672 != KERN_SUCCESS)
|
|
673 getloadavg_initialized = 0;
|
|
674 else
|
|
675 {
|
|
676 if (nelem > 0)
|
|
677 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
|
|
678 }
|
|
679 }
|
|
680
|
|
681 if (!getloadavg_initialized)
|
|
682 return -1;
|
|
683 #endif /* NeXT */
|
|
684
|
|
685 #if !defined (LDAV_DONE) && defined (UMAX)
|
|
686 #define LDAV_DONE
|
|
687 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
|
|
688 have a /dev/kmem. Information about the workings of the running kernel
|
|
689 can be gathered with inq_stats system calls.
|
|
690 We only know how to get the 1-minute average for this system. */
|
|
691
|
|
692 struct proc_summary proc_sum_data;
|
|
693 struct stat_descr proc_info;
|
|
694 double load;
|
203
|
695 REGISTER unsigned int i, j;
|
0
|
696
|
|
697 if (cpus == 0)
|
|
698 {
|
203
|
699 REGISTER unsigned int c, i;
|
0
|
700 struct cpu_config conf;
|
|
701 struct stat_descr desc;
|
|
702
|
|
703 desc.sd_next = 0;
|
|
704 desc.sd_subsys = SUBSYS_CPU;
|
|
705 desc.sd_type = CPUTYPE_CONFIG;
|
|
706 desc.sd_addr = (char *) &conf;
|
|
707 desc.sd_size = sizeof conf;
|
|
708
|
|
709 if (inq_stats (1, &desc))
|
|
710 return -1;
|
|
711
|
|
712 c = 0;
|
|
713 for (i = 0; i < conf.config_maxclass; ++i)
|
|
714 {
|
|
715 struct class_stats stats;
|
|
716 bzero ((char *) &stats, sizeof stats);
|
|
717
|
|
718 desc.sd_type = CPUTYPE_CLASS;
|
|
719 desc.sd_objid = i;
|
|
720 desc.sd_addr = (char *) &stats;
|
|
721 desc.sd_size = sizeof stats;
|
|
722
|
|
723 if (inq_stats (1, &desc))
|
|
724 return -1;
|
|
725
|
|
726 c += stats.class_numcpus;
|
|
727 }
|
|
728 cpus = c;
|
|
729 samples = cpus < 2 ? 3 : (2 * cpus / 3);
|
|
730 }
|
|
731
|
|
732 proc_info.sd_next = 0;
|
|
733 proc_info.sd_subsys = SUBSYS_PROC;
|
|
734 proc_info.sd_type = PROCTYPE_SUMMARY;
|
|
735 proc_info.sd_addr = (char *) &proc_sum_data;
|
|
736 proc_info.sd_size = sizeof (struct proc_summary);
|
|
737 proc_info.sd_sizeused = 0;
|
|
738
|
|
739 if (inq_stats (1, &proc_info) != 0)
|
|
740 return -1;
|
|
741
|
|
742 load = proc_sum_data.ps_nrunnable;
|
|
743 j = 0;
|
|
744 for (i = samples - 1; i > 0; --i)
|
|
745 {
|
|
746 load += proc_sum_data.ps_nrun[j];
|
|
747 if (j++ == PS_NRUNSIZE)
|
|
748 j = 0;
|
|
749 }
|
|
750
|
|
751 if (nelem > 0)
|
|
752 loadavg[elem++] = load / samples / cpus;
|
|
753 #endif /* UMAX */
|
|
754
|
|
755 #if !defined (LDAV_DONE) && defined (DGUX)
|
|
756 #define LDAV_DONE
|
|
757 /* This call can return -1 for an error, but with good args
|
|
758 it's not supposed to fail. The first argument is for no
|
|
759 apparent reason of type `long int *'. */
|
|
760 dg_sys_info ((long int *) &load_info,
|
|
761 DG_SYS_INFO_LOAD_INFO_TYPE,
|
|
762 DG_SYS_INFO_LOAD_VERSION_0);
|
|
763
|
|
764 if (nelem > 0)
|
|
765 loadavg[elem++] = load_info.one_minute;
|
|
766 if (nelem > 1)
|
|
767 loadavg[elem++] = load_info.five_minute;
|
|
768 if (nelem > 2)
|
|
769 loadavg[elem++] = load_info.fifteen_minute;
|
|
770 #endif /* DGUX */
|
|
771
|
|
772 #if !defined (LDAV_DONE) && defined (apollo)
|
|
773 #define LDAV_DONE
|
|
774 /* Apollo code from lisch@mentorg.com (Ray Lischner).
|
|
775
|
|
776 This system call is not documented. The load average is obtained as
|
|
777 three long integers, for the load average over the past minute,
|
|
778 five minutes, and fifteen minutes. Each value is a scaled integer,
|
|
779 with 16 bits of integer part and 16 bits of fraction part.
|
|
780
|
|
781 I'm not sure which operating system first supported this system call,
|
|
782 but I know that SR10.2 supports it. */
|
|
783
|
|
784 extern void proc1_$get_loadav ();
|
|
785 unsigned long load_ave[3];
|
|
786
|
|
787 proc1_$get_loadav (load_ave);
|
|
788
|
|
789 if (nelem > 0)
|
|
790 loadavg[elem++] = load_ave[0] / 65536.0;
|
|
791 if (nelem > 1)
|
|
792 loadavg[elem++] = load_ave[1] / 65536.0;
|
|
793 if (nelem > 2)
|
|
794 loadavg[elem++] = load_ave[2] / 65536.0;
|
|
795 #endif /* apollo */
|
|
796
|
|
797 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
|
|
798 #define LDAV_DONE
|
|
799
|
|
800 struct tbl_loadavg load_ave;
|
|
801 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
|
48
|
802 loadavg[elem++]
|
|
803 = (load_ave.tl_lscale == 0
|
|
804 ? load_ave.tl_avenrun.d[0]
|
|
805 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
|
0
|
806 #endif /* OSF_MIPS */
|
|
807
|
|
808 #if !defined (LDAV_DONE) && (defined (MSDOS) || defined (WIN32))
|
|
809 #define LDAV_DONE
|
|
810
|
|
811 /* A faithful emulation is going to have to be saved for a rainy day. */
|
|
812 for ( ; elem < nelem; elem++)
|
|
813 {
|
|
814 loadavg[elem] = 0.0;
|
|
815 }
|
|
816 #endif /* MSDOS */
|
|
817
|
|
818 #if !defined (LDAV_DONE) && defined (OSF_ALPHA)
|
|
819 #define LDAV_DONE
|
|
820
|
|
821 struct tbl_loadavg load_ave;
|
|
822 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
|
|
823 for (elem = 0; elem < nelem; elem++)
|
|
824 loadavg[elem]
|
|
825 = (load_ave.tl_lscale == 0
|
|
826 ? load_ave.tl_avenrun.d[elem]
|
|
827 : (load_ave.tl_avenrun.l[elem] / (double) load_ave.tl_lscale));
|
|
828 #endif /* OSF_ALPHA */
|
|
829
|
209
|
830 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
|
0
|
831
|
|
832 /* UNIX-specific code -- read the average from /dev/kmem. */
|
|
833
|
|
834 #define LDAV_PRIVILEGED /* This code requires special installation. */
|
|
835
|
|
836 LOAD_AVE_TYPE load_ave[3];
|
|
837
|
|
838 /* Get the address of LDAV_SYMBOL. */
|
|
839 if (offset == 0)
|
|
840 {
|
|
841 #ifndef sgi
|
|
842 #ifndef NLIST_STRUCT
|
|
843 strcpy (nl[0].n_name, LDAV_SYMBOL);
|
|
844 strcpy (nl[1].n_name, "");
|
|
845 #else /* NLIST_STRUCT */
|
|
846 #ifdef NLIST_NAME_UNION
|
|
847 nl[0].n_un.n_name = LDAV_SYMBOL;
|
|
848 nl[1].n_un.n_name = 0;
|
|
849 #else /* not NLIST_NAME_UNION */
|
84
|
850 nl[0].n_name = (char *) LDAV_SYMBOL;
|
0
|
851 nl[1].n_name = 0;
|
|
852 #endif /* not NLIST_NAME_UNION */
|
|
853 #endif /* NLIST_STRUCT */
|
|
854
|
|
855 #ifndef SUNOS_5
|
|
856 if (
|
|
857 #if !(defined (_AIX) && !defined (ps2))
|
|
858 nlist (KERNEL_FILE, nl)
|
|
859 #else /* _AIX */
|
|
860 knlist (nl, 1, sizeof (nl[0]))
|
|
861 #endif
|
|
862 >= 0)
|
|
863 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
|
|
864 {
|
|
865 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
|
|
866 FIXUP_KERNEL_SYMBOL_ADDR (nl);
|
|
867 #endif
|
|
868 offset = nl[0].n_value;
|
|
869 }
|
|
870 #endif /* !SUNOS_5 */
|
|
871 #else /* sgi */
|
|
872 int ldav_off;
|
|
873
|
|
874 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
|
|
875 if (ldav_off != -1)
|
|
876 offset = (long) ldav_off & 0x7fffffff;
|
|
877 #endif /* sgi */
|
|
878 }
|
|
879
|
|
880 /* Make sure we have /dev/kmem open. */
|
|
881 if (!getloadavg_initialized)
|
|
882 {
|
|
883 #ifndef SUNOS_5
|
|
884 channel = open ("/dev/kmem", 0);
|
|
885 if (channel >= 0)
|
|
886 {
|
|
887 /* Set the channel to close on exec, so it does not
|
|
888 litter any child's descriptor table. */
|
|
889 #ifdef FD_SETFD
|
|
890 #ifndef FD_CLOEXEC
|
|
891 #define FD_CLOEXEC 1
|
|
892 #endif
|
|
893 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
|
|
894 #endif
|
|
895 getloadavg_initialized = 1;
|
|
896 }
|
|
897 #else /* SUNOS_5 */
|
|
898 /* We pass 0 for the kernel, corefile, and swapfile names
|
|
899 to use the currently running kernel. */
|
|
900 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
|
|
901 if (kd != 0)
|
|
902 {
|
|
903 /* nlist the currently running kernel. */
|
|
904 kvm_nlist (kd, nl);
|
|
905 offset = nl[0].n_value;
|
|
906 getloadavg_initialized = 1;
|
|
907 }
|
|
908 #endif /* SUNOS_5 */
|
|
909 }
|
|
910
|
|
911 /* If we can, get the load average values. */
|
|
912 if (offset && getloadavg_initialized)
|
|
913 {
|
|
914 /* Try to read the load. */
|
|
915 #ifndef SUNOS_5
|
|
916 if (lseek (channel, offset, 0) == -1L
|
|
917 || read (channel, (char *) load_ave, sizeof (load_ave))
|
|
918 != sizeof (load_ave))
|
|
919 {
|
|
920 close (channel);
|
|
921 getloadavg_initialized = 0;
|
|
922 }
|
|
923 #else /* SUNOS_5 */
|
|
924 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
|
|
925 != sizeof (load_ave))
|
|
926 {
|
|
927 kvm_close (kd);
|
|
928 getloadavg_initialized = 0;
|
|
929 }
|
|
930 #endif /* SUNOS_5 */
|
|
931 }
|
|
932
|
|
933 if (offset == 0 || !getloadavg_initialized)
|
|
934 return -1;
|
|
935
|
|
936 if (nelem > 0)
|
|
937 loadavg[elem++] = LDAV_CVT (load_ave[0]);
|
|
938 if (nelem > 1)
|
|
939 loadavg[elem++] = LDAV_CVT (load_ave[1]);
|
|
940 if (nelem > 2)
|
|
941 loadavg[elem++] = LDAV_CVT (load_ave[2]);
|
|
942
|
|
943 #define LDAV_DONE
|
|
944 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
|
|
945
|
|
946 return elem;
|
|
947 }
|
|
948
|
|
949 #endif /* ! HAVE_GETLOADAVG */
|
|
950
|
|
951 #ifdef TEST
|
|
952 void
|
84
|
953 main (int argc, char **argv)
|
0
|
954 {
|
|
955 int naptime = 0;
|
|
956
|
|
957 if (argc > 1)
|
|
958 naptime = atoi (argv[1]);
|
|
959
|
|
960 while (1)
|
|
961 {
|
|
962 double avg[3];
|
|
963 int loads;
|
|
964
|
|
965 errno = 0; /* Don't be misled if it doesn't set errno. */
|
|
966 loads = getloadavg (avg, 3);
|
|
967 if (loads == -1)
|
|
968 {
|
|
969 perror ("Error getting load average");
|
|
970 exit (1);
|
|
971 }
|
|
972 if (loads > 0)
|
|
973 printf ("1-minute: %f ", avg[0]);
|
|
974 if (loads > 1)
|
|
975 printf ("5-minute: %f ", avg[1]);
|
|
976 if (loads > 2)
|
|
977 printf ("15-minute: %f ", avg[2]);
|
|
978 if (loads > 0)
|
|
979 putchar ('\n');
|
|
980
|
|
981 if (naptime == 0)
|
|
982 break;
|
|
983 sleep (naptime);
|
|
984 }
|
|
985
|
|
986 exit (0);
|
|
987 }
|
|
988 #endif /* TEST */
|
231
|
989
|
|
990 #else
|
|
991
|
|
992 /* Emulate getloadavg. */
|
|
993 int
|
|
994 getloadavg (double loadavg[], int nelem)
|
|
995 {
|
|
996 int i;
|
|
997
|
|
998 /* A faithful emulation is going to have to be saved for a rainy day. */
|
|
999 for (i = 0; i < nelem; i++)
|
|
1000 {
|
|
1001 loadavg[i] = 0.0;
|
|
1002 }
|
|
1003 return i;
|
|
1004 }
|
|
1005
|
|
1006 #endif /*__GNUWIN32__*/
|
|
1007
|