Debian Codegraph @

lib/getloadavg.c

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