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