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