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