Mercurial > hg > xemacs-beta
annotate src/getloadavg.c @ 5413:7910031dd78a
Add explicit GPLv3 or later to xpm and xbm graphics.
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Sun, 24 Oct 2010 01:10:23 +0200 |
parents | 308d34e9f07d |
children | 1f0b15040456 |
rev | line source |
---|---|
428 | 1 /* Get the system load averages. |
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995 | |
3 Free Software Foundation, Inc. | |
4 | |
5 This file is part of XEmacs. | |
6 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4759
diff
changeset
|
7 XEmacs is free software: you can redistribute it and/or modify it |
428 | 8 under the terms of the GNU General Public License as published by the |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4759
diff
changeset
|
9 Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4759
diff
changeset
|
10 option) any later version. |
428 | 11 |
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. | |
16 | |
17 You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
4759
diff
changeset
|
18 along with XEmacs. If not, see <http://www.gnu.org/licenses/>. */ |
428 | 19 |
20 /* Compile-time symbols that this file uses: | |
21 | |
22 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. | |
23 KERNEL_FILE Pathname of the kernel to nlist. | |
24 LDAV_CVT() Scale the load average from the kernel. | |
25 Returns a double. | |
26 LDAV_SYMBOL Name of kernel symbol giving load average. | |
27 LOAD_AVE_TYPE Type of the load average array in the kernel. | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
28 Must be defined; otherwise, no load average |
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
29 is available. |
428 | 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 LINUX_LDAV_FILE [__linux__]: File containing load averages. | |
34 | |
35 Specific system predefines this file uses, aside from setting | |
36 default values if not emacs: | |
37 | |
38 BSD Real BSD, not just BSD-like. | |
39 hpux | |
40 sgi | |
442 | 41 WIN32_NATIVE No-op for Windows9x/NT. |
42 CYGWIN No-op for Cygwin. | |
428 | 43 __linux__ Linux: assumes /proc filesystem mounted. |
44 Support from Michael K. Johnson. | |
45 __NetBSD__ NetBSD: assumes /kern filesystem mounted. | |
46 __OpenBSD__ OpenBSD: ditto. | |
47 | |
48 In addition, to avoid nesting many #ifdefs, we internally set | |
49 LDAV_DONE to indicate that the load average has been computed. | |
50 | |
51 We also #define LDAV_PRIVILEGED if a program will require | |
52 special installation to be able to call getloadavg. */ | |
53 | |
54 /* This should always be first. */ | |
55 #ifdef HAVE_CONFIG_H | |
56 #include <config.h> | |
57 #endif | |
58 | |
59 #include "lisp.h" | |
60 #include "sysfile.h" /* for encapsulated open, close, read, write */ | |
61 | |
62 #ifndef HAVE_GETLOADAVG | |
63 | |
64 /* The existing Emacs configuration files define a macro called | |
65 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and | |
66 returns the load average multiplied by 100. What we actually want | |
67 is a macro called LDAV_CVT, which returns the load average as an | |
68 unmultiplied double. | |
69 | |
70 For backwards compatibility, we'll define LDAV_CVT in terms of | |
71 LOAD_AVE_CVT, but future machine config files should just define | |
72 LDAV_CVT directly. */ | |
73 | |
74 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT) | |
75 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0) | |
76 #endif | |
77 | |
78 #if defined (HAVE_KSTAT_H) | |
79 #include <kstat.h> | |
80 #endif /* HAVE_KSTAT_H */ | |
81 | |
82 /* Set values that are different from the defaults, which are | |
83 set a little farther down with #ifndef. */ | |
84 | |
85 | |
86 /* Some shorthands. */ | |
87 | |
88 #if defined (HPUX) && !defined (hpux) | |
89 #define hpux | |
90 #endif | |
91 | |
92 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2) | |
93 #define SUNOS_5 | |
94 #endif | |
95 | |
96 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__)) | |
97 #define OSF_ALPHA | |
98 #include <netdb.h> | |
99 #include <netinet/in.h> /* Needed for Digital UNIX V3 */ | |
100 #include <net/proto_net.h> | |
101 #include <sys/table.h> | |
102 #endif | |
103 | |
104 #if defined (__osf__) && (defined (mips) || defined (__mips__)) | |
105 #define OSF_MIPS | |
106 #include <sys/table.h> | |
107 #endif | |
108 | |
109 | |
110 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */ | |
111 #ifndef LOAD_AVE_TYPE | |
112 | |
113 #ifdef sun | |
114 #define LOAD_AVE_TYPE long | |
115 #endif | |
116 | |
117 #ifdef decstation | |
118 #define LOAD_AVE_TYPE long | |
119 #endif | |
120 | |
121 #ifdef sgi | |
122 #define LOAD_AVE_TYPE long | |
123 #endif | |
124 | |
125 #ifdef SVR4 | |
126 #define LOAD_AVE_TYPE long | |
127 #endif | |
128 | |
129 #ifdef sony_news | |
130 #define LOAD_AVE_TYPE long | |
131 #endif | |
132 | |
133 #ifdef OSF_ALPHA | |
134 #define LOAD_AVE_TYPE long | |
135 #endif | |
136 | |
137 #if defined (ardent) && defined (titan) | |
138 #define LOAD_AVE_TYPE long | |
139 #endif | |
140 | |
141 #ifdef _AIX | |
142 #define LOAD_AVE_TYPE long | |
143 #endif | |
144 | |
145 #endif /* No LOAD_AVE_TYPE. */ | |
146 | |
147 #ifdef OSF_ALPHA | |
148 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1, | |
149 according to ghazi@noc.rutgers.edu. */ | |
150 #undef FSCALE | |
151 #define FSCALE 1024.0 | |
152 #endif | |
153 | |
154 #ifndef FSCALE | |
155 | |
156 /* SunOS and some others define FSCALE in sys/param.h. */ | |
157 | |
158 #if defined(MIPS) || defined(SVR4) || defined(decstation) | |
159 #define FSCALE 256 | |
160 #endif | |
161 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
162 #if defined (sgi) |
428 | 163 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined |
164 above under #ifdef MIPS. But we want the sgi value. */ | |
165 #undef FSCALE | |
166 #define FSCALE 1000.0 | |
167 #endif | |
168 | |
169 #if defined (ardent) && defined (titan) | |
170 #define FSCALE 65536.0 | |
171 #endif | |
172 | |
173 #ifdef _AIX | |
174 #define FSCALE 65536.0 | |
175 #endif | |
176 | |
177 #endif /* Not FSCALE. */ | |
178 | |
179 #if !defined (LDAV_CVT) && defined (FSCALE) | |
180 #define LDAV_CVT(n) (((double) (n)) / FSCALE) | |
181 #endif | |
182 | |
183 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */ | |
184 #ifndef NLIST_STRUCT | |
185 | |
186 #ifdef sun | |
187 #define NLIST_STRUCT | |
188 #endif | |
189 | |
190 #ifdef decstation | |
191 #define NLIST_STRUCT | |
192 #endif | |
193 | |
194 #ifdef hpux | |
195 #define NLIST_STRUCT | |
196 #endif | |
197 | |
198 #ifdef sgi | |
199 #define NLIST_STRUCT | |
200 #endif | |
201 | |
202 #ifdef SVR4 | |
203 #define NLIST_STRUCT | |
204 #endif | |
205 | |
206 #ifdef sony_news | |
207 #define NLIST_STRUCT | |
208 #endif | |
209 | |
210 #ifdef OSF_ALPHA | |
211 #define NLIST_STRUCT | |
212 #endif | |
213 | |
214 #if defined (ardent) && defined (titan) | |
215 #define NLIST_STRUCT | |
216 #endif | |
217 | |
218 #ifdef butterfly | |
219 #define NLIST_STRUCT | |
220 #endif | |
221 | |
222 #ifdef _AIX | |
223 #define NLIST_STRUCT | |
224 #endif | |
225 | |
226 #endif /* defined (NLIST_STRUCT) */ | |
227 | |
228 | |
229 #if defined(sgi) || (defined(mips) && !defined(BSD)) | |
230 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31)) | |
231 #endif | |
232 | |
233 #if !defined (KERNEL_FILE) && defined (hpux) | |
234 #define KERNEL_FILE "/hp-ux" | |
235 #endif | |
236 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
237 #if !defined(KERNEL_FILE) && (defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan))) |
428 | 238 #define KERNEL_FILE "/unix" |
239 #endif | |
240 | |
4759
aa5ed11f473b
Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents:
1315
diff
changeset
|
241 #if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX)) |
428 | 242 #define LDAV_SYMBOL "avenrun" |
243 #endif | |
244 | |
245 #ifdef HAVE_UNISTD_H | |
246 #include <unistd.h> | |
247 #endif | |
248 | |
249 #include <stdio.h> | |
250 | |
251 /* LOAD_AVE_TYPE should only get defined if we're going to use the | |
252 nlist method. */ | |
253 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL)) | |
254 #define LOAD_AVE_TYPE double | |
255 #endif | |
256 | |
257 #ifdef LOAD_AVE_TYPE | |
258 | |
259 #ifndef NLIST_STRUCT | |
260 #include <a.out.h> | |
261 #else /* NLIST_STRUCT */ | |
262 #include <nlist.h> | |
263 #endif /* NLIST_STRUCT */ | |
264 | |
265 #ifdef SUNOS_5 | |
266 #include <fcntl.h> | |
267 #include <kvm.h> | |
268 #endif | |
269 | |
270 #ifndef KERNEL_FILE | |
271 #define KERNEL_FILE "/vmunix" | |
272 #endif /* KERNEL_FILE */ | |
273 | |
274 #ifndef LDAV_SYMBOL | |
275 #define LDAV_SYMBOL "_avenrun" | |
276 #endif /* LDAV_SYMBOL */ | |
277 | |
278 #ifndef LDAV_CVT | |
279 #define LDAV_CVT(n) ((double) (n)) | |
280 #endif /* !LDAV_CVT */ | |
281 | |
282 #endif /* LOAD_AVE_TYPE */ | |
283 | |
284 #ifdef sgi | |
285 #include <sys/sysmp.h> | |
286 #endif /* sgi */ | |
287 | |
288 #if defined (HAVE_SYS_PSTAT_H) | |
289 #include <sys/pstat.h> | |
290 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */ | |
291 | |
292 | |
293 /* Avoid static vars inside a function since in HPUX they dump as pure. */ | |
294 | |
295 #ifdef LOAD_AVE_TYPE | |
296 /* File descriptor open to /dev/kmem */ | |
297 static int channel; | |
298 /* Nonzero iff channel is valid. */ | |
299 static int getloadavg_initialized; | |
300 /* Offset in kmem to seek to read load average, or 0 means invalid. */ | |
301 static long offset; | |
302 | |
303 #ifndef sgi | |
304 static struct nlist nl[2]; | |
305 #endif /* not sgi */ | |
306 | |
307 #ifdef SUNOS_5 | |
308 static kvm_t *kd; | |
309 #endif /* SUNOS_5 */ | |
310 | |
311 #ifndef countof | |
312 # define countof(x) (sizeof (x) / sizeof (*(x))) | |
313 #endif | |
314 | |
315 #endif /* LOAD_AVE_TYPE */ | |
316 | |
317 /* Put the 1 minute, 5 minute and 15 minute load averages | |
318 into the first NELEM elements of LOADAVG. | |
319 Return the number written (never more than 3, but may be less than NELEM), | |
320 or -1 if an error occurred. */ | |
321 | |
442 | 322 int getloadavg (double loadavg[], int nelem); |
323 | |
428 | 324 int |
325 getloadavg (double loadavg[], int nelem) | |
326 { | |
327 int elem = 0; /* Return value. */ | |
328 | |
329 #ifdef NO_GET_LOAD_AVG | |
330 #define LDAV_DONE | |
331 /* Set errno to zero to indicate that there was no particular error; | |
332 this function just can't work at all on this system. */ | |
333 errno = 0; | |
334 elem = -2; | |
335 #endif /* NO_GET_LOAD_AVG */ | |
336 | |
337 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT) | |
338 #define LDAV_DONE | |
339 /* getloadavg is best implemented using kstat (kernel stats), on | |
340 systems (like SunOS5) that support it, since you don't need special | |
341 privileges to use it. | |
342 | |
343 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>. | |
344 Integrated to XEmacs by Hrvoje Niksic <hniksic@xemacs.org>. | |
345 Additional cleanup by Hrvoje Niksic, based on code published by | |
346 Casper Dik <Casper.Dik@Holland.Sun.Com>. */ | |
347 kstat_ctl_t *kc; | |
348 kstat_t *ksp; | |
349 static char *avestrings[] = { "avenrun_1min", | |
350 "avenrun_5min", | |
351 "avenrun_15min" }; | |
352 | |
353 if (nelem > countof (avestrings)) | |
354 nelem = countof (avestrings); | |
355 | |
356 kc = kstat_open (); | |
357 if (!kc) | |
358 return -1; | |
359 ksp = kstat_lookup (kc, "unix", 0, "system_misc"); | |
360 if (!ksp) | |
361 { | |
362 kstat_close (kc); | |
363 return -1; | |
364 } | |
365 if (kstat_read (kc, ksp, 0) < 0) | |
366 { | |
367 kstat_close (kc); | |
368 return -1; | |
369 } | |
370 for (elem = 0; elem < nelem; elem++) | |
371 { | |
372 kstat_named_t *kn = | |
373 (kstat_named_t *) kstat_data_lookup (ksp, avestrings[elem]); | |
374 if (!kn) | |
375 { | |
376 kstat_close (kc); | |
377 return -1; | |
378 } | |
379 loadavg[elem] = (double)kn->value.ul / FSCALE; | |
380 } | |
381 kstat_close (kc); | |
382 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */ | |
383 | |
384 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H) | |
385 #define LDAV_DONE | |
386 /* This is totally undocumented, and is not guaranteed to work, but | |
387 mayhap it might .... If it does work, it will work only on HP-UX | |
388 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */ | |
389 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */ | |
390 #undef LOAD_AVE_CVT | |
391 #undef LDAV_SYMBOL | |
392 struct pst_dynamic procinfo; | |
393 union pstun statbuf; | |
394 | |
395 statbuf.pst_dynamic = &procinfo; | |
396 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1) | |
397 return (-1); | |
398 loadavg[elem++] = procinfo.psd_avg_1_min; | |
399 loadavg[elem++] = procinfo.psd_avg_5_min; | |
400 loadavg[elem++] = procinfo.psd_avg_15_min; | |
401 #endif /* HPUX */ | |
402 | |
403 #if !defined (LDAV_DONE) && defined (__linux__) | |
404 #define LDAV_DONE | |
405 #undef LOAD_AVE_TYPE | |
406 | |
407 #ifndef LINUX_LDAV_FILE | |
408 #define LINUX_LDAV_FILE "/proc/loadavg" | |
409 #endif | |
410 | |
411 char ldavgbuf[40]; | |
412 double load_ave[3]; | |
413 int fd, count; | |
414 | |
771 | 415 fd = retry_open (LINUX_LDAV_FILE, O_RDONLY); |
428 | 416 if (fd == -1) |
417 return -1; | |
771 | 418 count = retry_read (fd, ldavgbuf, 40); |
419 (void) retry_close (fd); | |
428 | 420 if (count <= 0) |
421 return -1; | |
422 | |
423 count = sscanf (ldavgbuf, "%lf %lf %lf", | |
424 &load_ave[0], &load_ave[1], &load_ave[2]); | |
425 if (count < 1) | |
426 return -1; | |
427 | |
428 for (elem = 0; elem < nelem && elem < count; elem++) | |
429 loadavg[elem] = load_ave[elem]; | |
430 #endif /* __linux__ */ | |
431 | |
432 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__) | |
433 #define LDAV_DONE | |
434 #undef LOAD_AVE_TYPE | |
435 | |
436 #ifndef NETBSD_LDAV_FILE | |
437 #define NETBSD_LDAV_FILE "/kern/loadavg" | |
438 #endif | |
439 | |
440 unsigned long int load_ave[3], scale; | |
441 int count; | |
442 FILE *fp; | |
443 | |
771 | 444 fp = retry_fopen (NETBSD_LDAV_FILE, "r"); |
428 | 445 if (fp == NULL) |
446 return -1; | |
447 count = fscanf (fp, "%lu %lu %lu %lu\n", | |
448 &load_ave[0], &load_ave[1], &load_ave[2], | |
449 &scale); | |
771 | 450 (void) retry_fclose (fp); |
428 | 451 if (count != 4) |
452 return -1; | |
453 | |
454 for (elem = 0; elem < nelem; elem++) | |
455 loadavg[elem] = (double) load_ave[elem] / (double) scale; | |
456 #endif /* __NetBSD__ or __OpenBSD__ */ | |
457 | |
1315 | 458 #if !defined (LDAV_DONE) && defined (WIN32_ANY) |
428 | 459 #define LDAV_DONE |
460 | |
461 /* A faithful emulation is going to have to be saved for a rainy day. */ | |
462 for ( ; elem < nelem; elem++) | |
463 { | |
464 loadavg[elem] = 0.0; | |
465 } | |
442 | 466 #endif /* WIN32_NATIVE or CYGWIN */ |
428 | 467 |
468 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) | |
469 | |
470 /* UNIX-specific code -- read the average from /dev/kmem. */ | |
471 | |
472 #define LDAV_PRIVILEGED /* This code requires special installation. */ | |
473 | |
474 LOAD_AVE_TYPE load_ave[3]; | |
475 | |
476 /* Get the address of LDAV_SYMBOL. */ | |
477 if (offset == 0) | |
478 { | |
479 #ifndef sgi | |
480 #ifndef NLIST_STRUCT | |
481 strcpy (nl[0].n_name, LDAV_SYMBOL); | |
482 strcpy (nl[1].n_name, ""); | |
483 #else /* NLIST_STRUCT */ | |
484 nl[0].n_name = (char *) LDAV_SYMBOL; | |
485 nl[1].n_name = 0; | |
486 #endif /* NLIST_STRUCT */ | |
487 | |
488 #ifndef SUNOS_5 | |
489 if ( | |
490 #if !(defined (_AIX) && !defined (ps2)) | |
491 nlist (KERNEL_FILE, nl) | |
492 #else /* _AIX */ | |
493 knlist (nl, 1, sizeof (nl[0])) | |
494 #endif | |
495 >= 0) | |
496 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */ | |
497 { | |
498 #ifdef FIXUP_KERNEL_SYMBOL_ADDR | |
499 FIXUP_KERNEL_SYMBOL_ADDR (nl); | |
500 #endif | |
501 offset = nl[0].n_value; | |
502 } | |
503 #endif /* !SUNOS_5 */ | |
504 #else /* sgi */ | |
505 int ldav_off; | |
506 | |
507 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN); | |
508 if (ldav_off != -1) | |
509 offset = (long) ldav_off & 0x7fffffff; | |
510 #endif /* sgi */ | |
511 } | |
512 | |
513 /* Make sure we have /dev/kmem open. */ | |
514 if (!getloadavg_initialized) | |
515 { | |
516 #ifndef SUNOS_5 | |
771 | 517 channel = retry_open ("/dev/kmem", 0); |
428 | 518 if (channel >= 0) |
519 { | |
520 /* Set the channel to close on exec, so it does not | |
521 litter any child's descriptor table. */ | |
522 #ifdef FD_SETFD | |
523 #ifndef FD_CLOEXEC | |
524 #define FD_CLOEXEC 1 | |
525 #endif | |
526 (void) fcntl (channel, F_SETFD, FD_CLOEXEC); | |
527 #endif | |
528 getloadavg_initialized = 1; | |
529 } | |
530 #else /* SUNOS_5 */ | |
531 /* We pass 0 for the kernel, corefile, and swapfile names | |
532 to use the currently running kernel. */ | |
533 kd = kvm_open (0, 0, 0, O_RDONLY, 0); | |
534 if (kd != 0) | |
535 { | |
536 /* nlist the currently running kernel. */ | |
537 kvm_nlist (kd, nl); | |
538 offset = nl[0].n_value; | |
539 getloadavg_initialized = 1; | |
540 } | |
541 #endif /* SUNOS_5 */ | |
542 } | |
543 | |
544 /* If we can, get the load average values. */ | |
545 if (offset && getloadavg_initialized) | |
546 { | |
547 /* Try to read the load. */ | |
548 #ifndef SUNOS_5 | |
549 if (lseek (channel, offset, 0) == -1L | |
771 | 550 || retry_read (channel, (char *) load_ave, sizeof (load_ave)) |
428 | 551 != sizeof (load_ave)) |
552 { | |
771 | 553 retry_close (channel); |
428 | 554 getloadavg_initialized = 0; |
555 } | |
556 #else /* SUNOS_5 */ | |
557 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave)) | |
558 != sizeof (load_ave)) | |
559 { | |
560 kvm_close (kd); | |
561 getloadavg_initialized = 0; | |
562 } | |
563 #endif /* SUNOS_5 */ | |
564 } | |
565 | |
566 if (offset == 0 || !getloadavg_initialized) | |
567 return -1; | |
568 | |
569 if (nelem > 0) | |
570 loadavg[elem++] = LDAV_CVT (load_ave[0]); | |
571 if (nelem > 1) | |
572 loadavg[elem++] = LDAV_CVT (load_ave[1]); | |
573 if (nelem > 2) | |
574 loadavg[elem++] = LDAV_CVT (load_ave[2]); | |
575 | |
576 #define LDAV_DONE | |
577 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */ | |
578 | |
579 return elem; | |
580 } | |
581 | |
582 #endif /* ! HAVE_GETLOADAVG */ | |
583 | |
584 #ifdef TEST | |
585 void | |
586 main (int argc, char **argv) | |
587 { | |
588 int naptime = 0; | |
589 | |
590 if (argc > 1) | |
591 naptime = atoi (argv[1]); | |
592 | |
593 while (1) | |
594 { | |
595 double avg[3]; | |
596 int loads; | |
597 | |
598 errno = 0; /* Don't be misled if it doesn't set errno. */ | |
599 loads = getloadavg (avg, 3); | |
600 if (loads == -1) | |
601 { | |
602 perror ("Error getting load average"); | |
603 exit (1); | |
604 } | |
605 if (loads > 0) | |
606 printf ("1-minute: %f ", avg[0]); | |
607 if (loads > 1) | |
608 printf ("5-minute: %f ", avg[1]); | |
609 if (loads > 2) | |
610 printf ("15-minute: %f ", avg[2]); | |
611 if (loads > 0) | |
612 putchar ('\n'); | |
613 | |
614 if (naptime == 0) | |
615 break; | |
616 sleep (naptime); | |
617 } | |
618 | |
619 exit (0); | |
620 } | |
621 #endif /* TEST */ |