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