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