📄 getloadavg.c
字号:
/* Get the system load averages. Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995, 1997 Free Software Foundation, Inc. This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *//* Compile-time symbols that this file uses: HAVE_PSTAT_GETDYNAMIC Define this if your system has the pstat_getdynamic function. I think it is unique to HPUX9. The best way to get the definition is through the AC_FUNC_GETLOADAVG macro that comes with autoconf 2.13 or newer. If that isn't an option, then just put AC_CHECK_FUNCS(pstat_getdynamic) in your configure.in file. FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist. KERNEL_FILE Pathname of the kernel to nlist. LDAV_CVT() Scale the load average from the kernel. Returns a double. LDAV_SYMBOL Name of kernel symbol giving load average. LOAD_AVE_TYPE Type of the load average array in the kernel. Must be defined unless one of apollo, DGUX, NeXT, or UMAX is defined; otherwise, no load average is available. NLIST_STRUCT Include nlist.h, not a.out.h, and the nlist n_name element is a pointer, not an array. NLIST_NAME_UNION struct nlist has an n_un member, not n_name. LINUX_LDAV_FILE [__linux__]: File containing load averages. Specific system predefines this file uses, aside from setting default values if not emacs: apollo BSD Real BSD, not just BSD-like. convex DGUX eunice UNIX emulator under VMS. hpux __MSDOS__ No-op for MSDOS. NeXT sgi sequent Sequent Dynix 3.x.x (BSD) _SEQUENT_ Sequent DYNIX/ptx 1.x.x (SYSV) sony_news NEWS-OS (works at least for 4.1C) UMAX UMAX4_3 VMS WINDOWS32 No-op for Windows95/NT. __linux__ Linux: assumes /proc filesystem mounted. Support from Michael K. Johnson. __NetBSD__ NetBSD: assumes /kern filesystem mounted. In addition, to avoid nesting many #ifdefs, we internally set LDAV_DONE to indicate that the load average has been computed. We also #define LDAV_PRIVILEGED if a program will require special installation to be able to call getloadavg. *//* This should always be first. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#ifndef HAVE_GETLOADAVG#include <sys/types.h>/* Both the Emacs and non-Emacs sections want this. Some configuration files' definitions for the LOAD_AVE_CVT macro (like sparc.h's) use macros like FSCALE, defined here. */#ifdef unix# include <sys/param.h>#endif/* Exclude all the code except the test program at the end if the system has its own `getloadavg' function. The declaration of `errno' is needed by the test program as well as the function itself, so it comes first. */#include <errno.h>#ifndef errnoextern int errno;#endif/* The existing Emacs configuration files define a macro called LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and returns the load average multiplied by 100. What we actually want is a macro called LDAV_CVT, which returns the load average as an unmultiplied double. For backwards compatibility, we'll define LDAV_CVT in terms of LOAD_AVE_CVT, but future machine config files should just define LDAV_CVT directly. */# if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)# define LDAV_CVT(n) (LOAD_AVE_CVT(n) / 100.0)# endif# if !defined (BSD) && defined (ultrix)/* Ultrix behaves like BSD on Vaxen. */# define BSD# endif# ifdef NeXT/* NeXT in the 2.{0,1,2} releases defines BSD in <sys/param.h>, which conflicts with the definition understood in this file, that this really is BSD. */# undef BSD/* NeXT defines FSCALE in <sys/param.h>. However, we take FSCALE being defined to mean that the nlist method should be used, which is not true. */# undef FSCALE# endif/* Set values that are different from the defaults, which are set a little farther down with #ifndef. *//* Some shorthands. */# if defined (HPUX) && !defined (hpux)# define hpux# endif# if defined (__hpux) && !defined (hpux)# define hpux# endif# if defined (__sun) && !defined (sun)# define sun# endif# if defined(hp300) && !defined(hpux)# define MORE_BSD# endif# if defined(ultrix) && defined(mips)# define decstation# endif# if defined (__SVR4) && !defined (SVR4)# define SVR4# endif# if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)# define SUNOS_5# endif# if defined (__osf__) && (defined (__alpha) || defined (__alpha__))# define OSF_ALPHA# include <sys/mbuf.h># include <sys/socket.h># include <net/route.h># include <sys/table.h># endif# if defined (__osf__) && (defined (mips) || defined (__mips__))# define OSF_MIPS# include <sys/table.h># endif/* UTek's /bin/cc on the 4300 has no architecture specific cpp define by default, but _MACH_IND_SYS_TYPES is defined in <sys/types.h>. Combine that with a couple of other things and we'll have a unique match. */# if !defined (tek4300) && defined (unix) && defined (m68k) && defined (mc68000) && defined (mc68020) && defined (_MACH_IND_SYS_TYPES)# define tek4300 /* Define by emacs, but not by other users. */# endif/* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */# ifndef LOAD_AVE_TYPE# ifdef MORE_BSD# define LOAD_AVE_TYPE long# endif# ifdef sun# define LOAD_AVE_TYPE long# endif# ifdef decstation# define LOAD_AVE_TYPE long# endif# ifdef _SEQUENT_# define LOAD_AVE_TYPE long# endif# ifdef sgi# define LOAD_AVE_TYPE long# endif# ifdef SVR4# define LOAD_AVE_TYPE long# endif# ifdef sony_news# define LOAD_AVE_TYPE long# endif# ifdef sequent# define LOAD_AVE_TYPE long# endif# ifdef OSF_ALPHA# define LOAD_AVE_TYPE long# endif# if defined (ardent) && defined (titan)# define LOAD_AVE_TYPE long# endif# ifdef tek4300# define LOAD_AVE_TYPE long# endif# if defined(alliant) && defined(i860) /* Alliant FX/2800 */# define LOAD_AVE_TYPE long# endif# ifdef _AIX# define LOAD_AVE_TYPE long# endif# ifdef convex# define LOAD_AVE_TYPE double# ifndef LDAV_CVT# define LDAV_CVT(n) (n)# endif# endif# endif /* No LOAD_AVE_TYPE. */# ifdef OSF_ALPHA/* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1, according to ghazi@noc.rutgers.edu. */# undef FSCALE# define FSCALE 1024.0# endif# if defined(alliant) && defined(i860) /* Alliant FX/2800 *//* <sys/param.h> defines an incorrect value for FSCALE on an Alliant FX/2800 Concentrix 2.2, according to ghazi@noc.rutgers.edu. */# undef FSCALE# define FSCALE 100.0# endif# ifndef FSCALE/* SunOS and some others define FSCALE in sys/param.h. */# ifdef MORE_BSD# define FSCALE 2048.0# endif# if defined(MIPS) || defined(SVR4) || defined(decstation)# define FSCALE 256# endif# if defined (sgi) || defined (sequent)/* Sometimes both MIPS and sgi are defined, so FSCALE was just defined above under #ifdef MIPS. But we want the sgi value. */# undef FSCALE# define FSCALE 1000.0# endif# if defined (ardent) && defined (titan)# define FSCALE 65536.0# endif# ifdef tek4300# define FSCALE 100.0# endif# ifdef _AIX# define FSCALE 65536.0# endif# endif /* Not FSCALE. */# if !defined (LDAV_CVT) && defined (FSCALE)# define LDAV_CVT(n) (((double) (n)) / FSCALE)# endif# ifndef NLIST_STRUCT# if HAVE_NLIST_H# define NLIST_STRUCT# endif# endif/* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */# ifndef NLIST_STRUCT# ifdef MORE_BSD# define NLIST_STRUCT# endif# ifdef sun# define NLIST_STRUCT# endif# ifdef decstation# define NLIST_STRUCT# endif# ifdef hpux# define NLIST_STRUCT# endif# if defined (_SEQUENT_) || defined (sequent)# define NLIST_STRUCT# endif# ifdef sgi# define NLIST_STRUCT# endif# ifdef SVR4# define NLIST_STRUCT# endif# ifdef sony_news# define NLIST_STRUCT# endif# ifdef OSF_ALPHA# define NLIST_STRUCT# endif# if defined (ardent) && defined (titan)# define NLIST_STRUCT# endif# ifdef tek4300# define NLIST_STRUCT# endif# ifdef butterfly# define NLIST_STRUCT# endif# if defined(alliant) && defined(i860) /* Alliant FX/2800 */# define NLIST_STRUCT# endif# ifdef _AIX# define NLIST_STRUCT# endif# endif /* defined (NLIST_STRUCT) */# if defined(sgi) || (defined(mips) && !defined(BSD))# define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))# endif# if !defined (KERNEL_FILE) && defined (sequent)# define KERNEL_FILE "/dynix"# endif# if !defined (KERNEL_FILE) && defined (hpux)# define KERNEL_FILE "/hp-ux"# endif# if !defined(KERNEL_FILE) && (defined(_SEQUENT_) || defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || (defined (ardent) && defined (titan)))# define KERNEL_FILE "/unix"# endif# if !defined (LDAV_SYMBOL) && defined (alliant)# define LDAV_SYMBOL "_Loadavg"# endif# if !defined(LDAV_SYMBOL) && ((defined(hpux) && !defined(hp9000s300)) || defined(_SEQUENT_) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))# define LDAV_SYMBOL "avenrun"# endif# ifdef HAVE_UNISTD_H# include <unistd.h># endif# include <stdio.h>/* LOAD_AVE_TYPE should only get defined if we're going to use the nlist method. */# if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))# define LOAD_AVE_TYPE double# endif# ifdef LOAD_AVE_TYPE# ifndef VMS# ifndef __linux__# ifndef NLIST_STRUCT# include <a.out.h># else /* NLIST_STRUCT */# include <nlist.h># endif /* NLIST_STRUCT */# ifdef SUNOS_5# include <fcntl.h># include <kvm.h># include <kstat.h># endif# if defined (hpux) && defined (HAVE_PSTAT_GETDYNAMIC)# include <sys/pstat.h># endif# ifndef KERNEL_FILE# define KERNEL_FILE "/vmunix"# endif /* KERNEL_FILE */# ifndef LDAV_SYMBOL# define LDAV_SYMBOL "_avenrun"# endif /* LDAV_SYMBOL */# endif /* __linux__ */# else /* VMS */# ifndef eunice# include <iodef.h># include <descrip.h># else /* eunice */# include <vms/iodef.h># endif /* eunice */# endif /* VMS */# ifndef LDAV_CVT# define LDAV_CVT(n) ((double) (n))# endif /* !LDAV_CVT */# endif /* LOAD_AVE_TYPE */# if defined(__GNU__) && !defined (NeXT)/* Note that NeXT Openstep defines __GNU__ even though it should not. *//* GNU system acts much like NeXT, for load average purposes, but not exactly. */# define NeXT# define host_self mach_host_self# endif# ifdef NeXT# ifdef HAVE_MACH_MACH_H# include <mach/mach.h># else# include <mach.h># endif# endif /* NeXT */# ifdef sgi# include <sys/sysmp.h># endif /* sgi */# ifdef UMAX# include <stdio.h># include <signal.h># include <sys/time.h># include <sys/wait.h># include <sys/syscall.h># ifdef UMAX_43# include <machine/cpu.h># include <inq_stats/statistics.h># include <inq_stats/sysstats.h># include <inq_stats/cpustats.h># include <inq_stats/procstats.h># else /* Not UMAX_43. */# include <sys/sysdefs.h># include <sys/statistics.h># include <sys/sysstats.h># include <sys/cpudefs.h># include <sys/cpustats.h># include <sys/procstats.h># endif /* Not UMAX_43. */# endif /* UMAX */# ifdef DGUX# include <sys/dg_sys_info.h># endif# if defined(HAVE_FCNTL_H) || defined(_POSIX_VERSION)# include <fcntl.h># else# include <sys/file.h># endif/* Avoid static vars inside a function since in HPUX they dump as pure. */# ifdef NeXT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -