Formerly make.texinfo.~117~
[make.git] / getloadavg.c
blobb748c3a5ace1a8deac62ebc589db6e5c9becd0b1
1 /* Get the system load averages.
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93
3 Free Software Foundation, Inc.
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.
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.
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. */
19 /* Compile-time symbols that this file uses:
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__]: File containing load averages.
36 Specific system predefines this file uses, aside from setting
37 default values if not emacs:
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.
55 In addition, to avoid nesting many #ifdefs, we internally set
56 LDAV_DONE to indicate that the load average has been computed.
58 We also #define LDAV_PRIVILEGED if a program will require
59 special installation to be able to call getloadavg. */
61 #include <sys/types.h>
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
71 #ifdef HAVE_CONFIG_H
72 /* We use <config.h> instead of "config.h" so that a compilation
73 using -I. -I$srcdir will use ./config.h rather than $srcdir/config.h
74 (which it would do because getloadavg.c was found in $srcdir). */
75 #include <config.h>
76 #endif
79 /* Exclude all the code except the test program at the end
80 if the system has its own `getloadavg' function. */
82 #ifndef HAVE_GETLOADAVG
85 /* The existing Emacs configuration files define a macro called
86 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
87 returns the load average multiplied by 100. What we actually want
88 is a macro called LDAV_CVT, which returns the load average as an
89 unmultiplied double.
91 For backwards compatibility, we'll define LDAV_CVT in terms of
92 LOAD_AVE_CVT, but future machine config files should just define
93 LDAV_CVT directly. */
95 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
96 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
97 #endif
99 #if !defined (BSD) && defined (ultrix)
100 /* Ultrix behaves like BSD on Vaxen. */
101 #define BSD
102 #endif
104 #ifdef NeXT
105 /* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which
106 conflicts with the definition understood in this file, that this
107 really is BSD. */
108 #undef BSD
110 /* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being
111 defined to mean that the nlist method should be used, which is not true. */
112 #undef FSCALE
113 #endif
115 /* Set values that are different from the defaults, which are
116 set a little farther down with #ifndef. */
119 /* Some shorthands. */
121 #if defined (HPUX) && !defined (hpux)
122 #define hpux
123 #endif
125 #if defined(hp300) && !defined(hpux)
126 #define MORE_BSD
127 #endif
129 #if defined(ultrix) && defined(mips)
130 #define decstation
131 #endif
133 #if defined(sun) && defined(SVR4)
134 #define SUNOS_5
135 #endif
137 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
138 #define OSF_ALPHA
139 #endif
141 #if defined (__osf__) && (defined (mips) || defined (__mips__))
142 #define OSF_MIPS
143 #include <sys/table.h>
144 #endif
146 /* UTek's /bin/cc on the 4300 has no architecture specific cpp define by
147 default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine
148 that with a couple of other things and we'll have a unique match. */
149 #if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)
150 #define tek4300 /* Define by emacs, but not by other users. */
151 #endif
154 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
155 #ifndef LOAD_AVE_TYPE
157 #ifdef MORE_BSD
158 #define LOAD_AVE_TYPE long
159 #endif
161 #ifdef sun
162 #define LOAD_AVE_TYPE long
163 #endif
165 #ifdef decstation
166 #define LOAD_AVE_TYPE long
167 #endif
169 #ifdef _SEQUENT_
170 #define LOAD_AVE_TYPE long
171 #endif
173 #ifdef sgi
174 #define LOAD_AVE_TYPE long
175 #endif
177 #ifdef SVR4
178 #define LOAD_AVE_TYPE long
179 #endif
181 #ifdef sony_news
182 #define LOAD_AVE_TYPE long
183 #endif
185 #ifdef sequent
186 #define LOAD_AVE_TYPE long
187 #endif
189 #ifdef OSF_ALPHA
190 #define LOAD_AVE_TYPE long
191 #endif
193 #if defined (ardent) && defined (titan)
194 #define LOAD_AVE_TYPE long
195 #endif
197 #ifdef tek4300
198 #define LOAD_AVE_TYPE long
199 #endif
201 #endif /* No LOAD_AVE_TYPE. */
203 #ifdef OSF_ALPHA
204 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
205 according to ghazi@noc.rutgers.edu. */
206 #undef FSCALE
207 #define FSCALE 1024.0
208 #endif
211 #ifndef FSCALE
213 /* SunOS and some others define FSCALE in sys/param.h. */
215 #ifdef MORE_BSD
216 #define FSCALE 2048.0
217 #endif
219 #if defined(MIPS) || defined(SVR4) || defined(decstation)
220 #define FSCALE 256
221 #endif
223 #if defined (sgi) || defined (sequent)
224 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
225 above under #ifdef MIPS. But we want the sgi value. */
226 #undef FSCALE
227 #define FSCALE 1000.0
228 #endif
230 #if defined (ardent) && defined (titan)
231 #define FSCALE 65536.0
232 #endif
234 #ifdef tek4300
235 #define FSCALE 100.0
236 #endif
238 #endif /* Not FSCALE. */
240 #if !defined (LDAV_CVT) && defined (FSCALE)
241 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
242 #endif
244 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
245 #ifndef NLIST_STRUCT
247 #ifdef MORE_BSD
248 #define NLIST_STRUCT
249 #endif
251 #ifdef sun
252 #define NLIST_STRUCT
253 #endif
255 #ifdef decstation
256 #define NLIST_STRUCT
257 #endif
259 #ifdef hpux
260 #define NLIST_STRUCT
261 #endif
263 #if defined (_SEQUENT_) || defined (sequent)
264 #define NLIST_STRUCT
265 #endif
267 #ifdef sgi
268 #define NLIST_STRUCT
269 #endif
271 #ifdef SVR4
272 #define NLIST_STRUCT
273 #endif
275 #ifdef sony_news
276 #define NLIST_STRUCT
277 #endif
279 #ifdef OSF_ALPHA
280 #define NLIST_STRUCT
281 #endif
283 #if defined (ardent) && defined (titan)
284 #define NLIST_STRUCT
285 #endif
287 #ifdef tex4300
288 #define NLIST_STRUCT
289 #endif
291 #ifdef butterfly
292 #define NLIST_STRUCT
293 #endif
295 #endif /* defined (NLIST_STRUCT) */
298 #if defined(sgi) || (defined(mips) && !defined(BSD))
299 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
300 #endif
303 #if !defined (KERNEL_FILE) && defined (sequent)
304 #define KERNEL_FILE "/dynix"
305 #endif
307 #if !defined (KERNEL_FILE) && defined (hpux)
308 #define KERNEL_FILE "/hp-ux"
309 #endif
311 #if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
312 #define KERNEL_FILE "/unix"
313 #endif
316 #if !defined (LDAV_SYMBOL) && defined (alliant)
317 #define LDAV_SYMBOL "_Loadavg"
318 #endif
320 #if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)))
321 #define LDAV_SYMBOL "avenrun"
322 #endif
324 #ifdef HAVE_UNISTD_H
325 #include <unistd.h>
326 #endif
328 #include <stdio.h>
329 #include <errno.h>
331 #ifndef errno
332 extern int errno;
333 #endif
335 /* LOAD_AVE_TYPE should only get defined if we're going to use the
336 nlist method. */
337 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
338 #define LOAD_AVE_TYPE double
339 #endif
341 #ifdef LOAD_AVE_TYPE
343 #ifndef VMS
344 #ifndef NLIST_STRUCT
345 #include <a.out.h>
346 #else /* NLIST_STRUCT */
347 #include <nlist.h>
348 #endif /* NLIST_STRUCT */
350 #ifdef SUNOS_5
351 #include <fcntl.h>
352 #include <kvm.h>
353 #endif
355 #ifndef KERNEL_FILE
356 #define KERNEL_FILE "/vmunix"
357 #endif /* KERNEL_FILE */
359 #ifndef LDAV_SYMBOL
360 #define LDAV_SYMBOL "_avenrun"
361 #endif /* LDAV_SYMBOL */
363 #else /* VMS */
365 #ifndef eunice
366 #include <iodef.h>
367 #include <descrip.h>
368 #else /* eunice */
369 #include <vms/iodef.h>
370 #endif /* eunice */
371 #endif /* VMS */
373 #ifndef LDAV_CVT
374 #define LDAV_CVT(n) ((double) (n))
375 #endif /* !LDAV_CVT */
377 #endif /* LOAD_AVE_TYPE */
379 #ifdef NeXT
380 #ifdef HAVE_MACH_MACH_H
381 #include <mach/mach.h>
382 #else
383 #include <mach.h>
384 #endif
385 #endif /* NeXT */
387 #ifdef sgi
388 #include <sys/sysmp.h>
389 #endif /* sgi */
391 #ifdef UMAX
392 #include <stdio.h>
393 #include <signal.h>
394 #include <sys/time.h>
395 #include <sys/wait.h>
396 #include <sys/syscall.h>
398 #ifdef UMAX_43
399 #include <machine/cpu.h>
400 #include <inq_stats/statistics.h>
401 #include <inq_stats/sysstats.h>
402 #include <inq_stats/cpustats.h>
403 #include <inq_stats/procstats.h>
404 #else /* Not UMAX_43. */
405 #include <sys/sysdefs.h>
406 #include <sys/statistics.h>
407 #include <sys/sysstats.h>
408 #include <sys/cpudefs.h>
409 #include <sys/cpustats.h>
410 #include <sys/procstats.h>
411 #endif /* Not UMAX_43. */
412 #endif /* UMAX */
414 #ifdef DGUX
415 #include <sys/dg_sys_info.h>
416 #endif
418 #if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)
419 #include <fcntl.h>
420 #else
421 #include <sys/file.h>
422 #endif
424 /* Avoid static vars inside a function since in HPUX they dump as pure. */
426 #ifdef NeXT
427 static processor_set_t default_set;
428 static int getloadavg_initialized;
429 #endif /* NeXT */
431 #ifdef UMAX
432 static unsigned int cpus = 0;
433 static unsigned int samples;
434 #endif /* UMAX */
436 #ifdef DGUX
437 static struct dg_sys_info_load_info load_info; /* what-a-mouthful! */
438 #endif /* DGUX */
440 #ifdef LOAD_AVE_TYPE
441 /* File descriptor open to /dev/kmem or VMS load ave driver. */
442 static int channel;
443 /* Nonzero iff channel is valid. */
444 static int getloadavg_initialized;
445 /* Offset in kmem to seek to read load average, or 0 means invalid. */
446 static long offset;
448 #if !defined(VMS) && !defined(sgi)
449 static struct nlist nl[2];
450 #endif /* Not VMS or sgi */
452 #ifdef SUNOS_5
453 static kvm_t *kd;
454 #endif /* SUNOS_5 */
456 #endif /* LOAD_AVE_TYPE */
458 /* Put the 1 minute, 5 minute and 15 minute load averages
459 into the first NELEM elements of LOADAVG.
460 Return the number written (never more than 3, but may be less than NELEM),
461 or -1 if an error occurred. */
464 getloadavg (loadavg, nelem)
465 double loadavg[];
466 int nelem;
468 int elem = 0; /* Return value. */
470 #ifdef NO_GET_LOAD_AVG
471 #define LDAV_DONE
472 /* Set errno to zero to indicate that there was no particular error;
473 this function just can't work at all on this system. */
474 errno = 0;
475 elem = -1;
476 #endif
478 #if !defined (LDAV_DONE) && defined (__linux__)
479 #define LDAV_DONE
480 #undef LOAD_AVE_TYPE
482 #ifndef LINUX_LDAV_FILE
483 #define LINUX_LDAV_FILE "/proc/loadavg"
484 #endif
486 char ldavgbuf[40];
487 double load_ave[3];
488 int fd, count;
490 fd = open (LINUX_LDAV_FILE, O_RDONLY);
491 if (fd == -1)
492 return -1;
493 count = read (fd, ldavgbuf, 40);
494 (void) close (fd);
495 if (count <= 0)
496 return -1;
498 count = sscanf (ldavgbuf, "%lf %lf %lf",
499 &load_ave[0], &load_ave[1], &load_ave[2]);
500 if (count < 1)
501 return -1;
503 for (elem = 0; elem < nelem && elem < count; elem++)
504 loadavg[elem] = load_ave[elem];
506 return elem;
508 #endif /* __linux__ */
510 #if !defined (LDAV_DONE) && defined (NeXT)
511 #define LDAV_DONE
512 /* The NeXT code was adapted from iscreen 3.2. */
514 host_t host;
515 struct processor_set_basic_info info;
516 unsigned info_count;
518 /* We only know how to get the 1-minute average for this system,
519 so even if the caller asks for more than 1, we only return 1. */
521 if (!getloadavg_initialized)
523 if (processor_set_default (host_self (), &default_set) == KERN_SUCCESS)
524 getloadavg_initialized = 1;
527 if (getloadavg_initialized)
529 info_count = PROCESSOR_SET_BASIC_INFO_COUNT;
530 if (processor_set_info (default_set, PROCESSOR_SET_BASIC_INFO, &host,
531 (processor_set_info_t) &info, &info_count)
532 != KERN_SUCCESS)
533 getloadavg_initialized = 0;
534 else
536 if (nelem > 0)
537 loadavg[elem++] = (double) info.load_average / LOAD_SCALE;
541 if (!getloadavg_initialized)
542 return -1;
543 #endif /* NeXT */
545 #if !defined (LDAV_DONE) && defined (UMAX)
546 #define LDAV_DONE
547 /* UMAX 4.2, which runs on the Encore Multimax multiprocessor, does not
548 have a /dev/kmem. Information about the workings of the running kernel
549 can be gathered with inq_stats system calls.
550 We only know how to get the 1-minute average for this system. */
552 struct proc_summary proc_sum_data;
553 struct stat_descr proc_info;
554 double load;
555 register unsigned int i, j;
557 if (cpus == 0)
559 register unsigned int c, i;
560 struct cpu_config conf;
561 struct stat_descr desc;
563 desc.sd_next = 0;
564 desc.sd_subsys = SUBSYS_CPU;
565 desc.sd_type = CPUTYPE_CONFIG;
566 desc.sd_addr = (char *) &conf;
567 desc.sd_size = sizeof conf;
569 if (inq_stats (1, &desc))
570 return -1;
572 c = 0;
573 for (i = 0; i < conf.config_maxclass; ++i)
575 struct class_stats stats;
576 bzero ((char *) &stats, sizeof stats);
578 desc.sd_type = CPUTYPE_CLASS;
579 desc.sd_objid = i;
580 desc.sd_addr = (char *) &stats;
581 desc.sd_size = sizeof stats;
583 if (inq_stats (1, &desc))
584 return -1;
586 c += stats.class_numcpus;
588 cpus = c;
589 samples = cpus < 2 ? 3 : (2 * cpus / 3);
592 proc_info.sd_next = 0;
593 proc_info.sd_subsys = SUBSYS_PROC;
594 proc_info.sd_type = PROCTYPE_SUMMARY;
595 proc_info.sd_addr = (char *) &proc_sum_data;
596 proc_info.sd_size = sizeof (struct proc_summary);
597 proc_info.sd_sizeused = 0;
599 if (inq_stats (1, &proc_info) != 0)
600 return -1;
602 load = proc_sum_data.ps_nrunnable;
603 j = 0;
604 for (i = samples - 1; i > 0; --i)
606 load += proc_sum_data.ps_nrun[j];
607 if (j++ == PS_NRUNSIZE)
608 j = 0;
611 if (nelem > 0)
612 loadavg[elem++] = load / samples / cpus;
613 #endif /* UMAX */
615 #if !defined (LDAV_DONE) && defined (DGUX)
616 #define LDAV_DONE
617 /* This call can return -1 for an error, but with good args
618 it's not supposed to fail. The first argument is for no
619 apparent reason of type `long int *'. */
620 dg_sys_info ((long int *) &load_info,
621 DG_SYS_INFO_LOAD_INFO_TYPE,
622 DG_SYS_INFO_LOAD_VERSION_0);
624 if (nelem > 0)
625 loadavg[elem++] = load_info.one_minute;
626 if (nelem > 1)
627 loadavg[elem++] = load_info.five_minute;
628 if (nelem > 2)
629 loadavg[elem++] = load_info.fifteen_minute;
630 #endif /* DGUX */
632 #if !defined (LDAV_DONE) && defined (apollo)
633 #define LDAV_DONE
634 /* Apollo code from lisch@mentorg.com (Ray Lischner).
636 This system call is not documented. The load average is obtained as
637 three long integers, for the load average over the past minute,
638 five minutes, and fifteen minutes. Each value is a scaled integer,
639 with 16 bits of integer part and 16 bits of fraction part.
641 I'm not sure which operating system first supported this system call,
642 but I know that SR10.2 supports it. */
644 extern void proc1_$get_loadav ();
645 unsigned long load_ave[3];
647 proc1_$get_loadav (load_ave);
649 if (nelem > 0)
650 loadavg[elem++] = load_ave[0] / 65536.0;
651 if (nelem > 1)
652 loadavg[elem++] = load_ave[1] / 65536.0;
653 if (nelem > 2)
654 loadavg[elem++] = load_ave[2] / 65536.0;
655 #endif /* apollo */
657 #if !defined (LDAV_DONE) && defined (OSF_MIPS)
658 #define LDAV_DONE
660 struct tbl_loadavg load_ave;
661 table (TBL_LOADAVG, 0, &load_ave, 1, sizeof (load_ave));
662 loadavg[elem++]
663 = (load_ave.tl_lscale == 0
664 ? load_ave.tl_avenrun.d[0]
665 : (load_ave.tl_avenrun.l[0] / (double) load_ave.tl_lscale));
666 #endif /* OSF_MIPS */
668 #if !defined (LDAV_DONE) && defined (VMS)
669 /* VMS specific code -- read from the Load Ave driver. */
671 LOAD_AVE_TYPE load_ave[3];
672 static int getloadavg_initialized = 0;
673 #ifdef eunice
674 struct
676 int dsc$w_length;
677 char *dsc$a_pointer;
678 } descriptor;
679 #endif
681 /* Ensure that there is a channel open to the load ave device. */
682 if (!getloadavg_initialized)
684 /* Attempt to open the channel. */
685 #ifdef eunice
686 descriptor.dsc$w_length = 18;
687 descriptor.dsc$a_pointer = "$$VMS_LOAD_AVERAGE";
688 #else
689 $DESCRIPTOR (descriptor, "LAV0:");
690 #endif
691 if (sys$assign (&descriptor, &channel, 0, 0) & 1)
692 getloadavg_initialized = 1;
695 /* Read the load average vector. */
696 if (getloadavg_initialized
697 && !(sys$qiow (0, channel, IO$_READVBLK, 0, 0, 0,
698 load_ave, 12, 0, 0, 0, 0) & 1))
700 sys$dassgn (channel);
701 getloadavg_initialized = 0;
704 if (!getloadavg_initialized)
705 return -1;
706 #endif /* VMS */
708 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE) && !defined(VMS)
710 /* UNIX-specific code -- read the average from /dev/kmem. */
712 #define LDAV_PRIVILEGED /* This code requires special installation. */
714 LOAD_AVE_TYPE load_ave[3];
716 /* Get the address of LDAV_SYMBOL. */
717 if (offset == 0)
719 #ifndef sgi
720 #ifndef NLIST_STRUCT
721 strcpy (nl[0].n_name, LDAV_SYMBOL);
722 strcpy (nl[1].n_name, "");
723 #else /* NLIST_STRUCT */
724 #ifdef NLIST_NAME_UNION
725 nl[0].n_un.n_name = LDAV_SYMBOL;
726 nl[1].n_un.n_name = 0;
727 #else /* not NLIST_NAME_UNION */
728 nl[0].n_name = LDAV_SYMBOL;
729 nl[1].n_name = 0;
730 #endif /* not NLIST_NAME_UNION */
731 #endif /* NLIST_STRUCT */
733 #ifndef SUNOS_5
734 if (nlist (KERNEL_FILE, nl) >= 0)
735 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
737 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
738 FIXUP_KERNEL_SYMBOL_ADDR (nl);
739 #endif
740 offset = nl[0].n_value;
742 #endif /* !SUNOS_5 */
743 #else /* sgi */
744 int ldav_off;
746 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
747 if (ldav_off != -1)
748 offset = (long) ldav_off & 0x7fffffff;
749 #endif /* sgi */
752 /* Make sure we have /dev/kmem open. */
753 if (!getloadavg_initialized)
755 #ifndef SUNOS_5
756 channel = open ("/dev/kmem", 0);
757 if (channel >= 0)
758 getloadavg_initialized = 1;
759 #else /* SUNOS_5 */
760 /* We pass 0 for the kernel, corefile, and swapfile names
761 to use the currently running kernel. */
762 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
763 if (kd != 0)
765 /* nlist the currently running kernel. */
766 kvm_nlist (kd, nl);
767 offset = nl[0].n_value;
768 getloadavg_initialized = 1;
770 #endif /* SUNOS_5 */
773 /* If we can, get the load average values. */
774 if (offset && getloadavg_initialized)
776 /* Try to read the load. */
777 #ifndef SUNOS_5
778 if (lseek (channel, offset, 0) == -1L
779 || read (channel, (char *) load_ave, sizeof (load_ave))
780 != sizeof (load_ave))
782 close (channel);
783 getloadavg_initialized = 0;
785 #else /* SUNOS_5 */
786 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
787 != sizeof (load_ave))
789 kvm_close (kd);
790 getloadavg_initialized = 0;
792 #endif /* SUNOS_5 */
795 if (offset == 0 || !getloadavg_initialized)
796 return -1;
797 #endif /* LOAD_AVE_TYPE and not VMS */
799 #if !defined (LDAV_DONE) && defined (LOAD_AVE_TYPE) /* Including VMS. */
800 if (nelem > 0)
801 loadavg[elem++] = LDAV_CVT (load_ave[0]);
802 if (nelem > 1)
803 loadavg[elem++] = LDAV_CVT (load_ave[1]);
804 if (nelem > 2)
805 loadavg[elem++] = LDAV_CVT (load_ave[2]);
807 #define LDAV_DONE
808 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
810 #ifdef LDAV_DONE
811 return elem;
812 #else
813 /* Set errno to zero to indicate that there was no particular error;
814 this function just can't work at all on this system. */
815 errno = 0;
816 return -1;
817 #endif
820 #endif /* ! HAVE_GETLOADAVG */
822 #ifdef TEST
823 void
824 main (argc, argv)
825 int argc;
826 char **argv;
828 int naptime = 0;
830 if (argc > 1)
831 naptime = atoi (argv[1]);
833 while (1)
835 double avg[3];
836 int loads;
838 errno = 0; /* Don't be misled if it doesn't set errno. */
839 loads = getloadavg (avg, 3);
840 if (loads == -1)
842 perror ("Error getting load average");
843 exit (1);
845 if (loads > 0)
846 printf ("1-minute: %f ", avg[0]);
847 if (loads > 1)
848 printf ("5-minute: %f ", avg[1]);
849 if (loads > 2)
850 printf ("15-minute: %f ", avg[2]);
851 if (loads > 0)
852 putchar ('\n');
854 if (naptime == 0)
855 break;
856 sleep (naptime);
859 exit (0);
861 #endif /* TEST */