annotate src/getloadavg.c @ 5167:e374ea766cc1

clean up, rearrange allocation statistics code -------------------- ChangeLog entries follow: -------------------- src/ChangeLog addition: 2010-03-21 Ben Wing <ben@xemacs.org> * alloc.c: * alloc.c (assert_proper_sizing): * alloc.c (c_readonly): * alloc.c (malloced_storage_size): * alloc.c (fixed_type_block_overhead): * alloc.c (lisp_object_storage_size): * alloc.c (inc_lrecord_stats): * alloc.c (dec_lrecord_stats): * alloc.c (pluralize_word): * alloc.c (object_memory_usage_stats): * alloc.c (Fobject_memory_usage): * alloc.c (compute_memusage_stats_length): * alloc.c (disksave_object_finalization_1): * alloc.c (Fgarbage_collect): * mc-alloc.c: * mc-alloc.c (mc_alloced_storage_size): * mc-alloc.h: No functionality change here. Collect the allocations-statistics code that was scattered throughout alloc.c into one place. Add remaining section headings so that all sections have headings clearly identifying the start of the section and its purpose. Expose mc_alloced_storage_size() even when not MEMORY_USAGE_STATS; this fixes build problems and is related to the export of lisp_object_storage_size() and malloced_storage_size() when non-MEMORY_USAGE_STATS in the previous change set.
author Ben Wing <ben@xemacs.org>
date Sun, 21 Mar 2010 04:41:49 -0500
parents aa5ed11f473b
children 308d34e9f07d 861f2601a38b
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
1 /* Get the system load averages.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
2 Copyright (C) 1985, 86, 87, 88, 89, 91, 92, 93, 1994, 1995
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
3 Free Software Foundation, Inc.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
5 This file is part of XEmacs.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
6
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
7 XEmacs is free software; you can redistribute it and/or modify it
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
8 under the terms of the GNU General Public License as published by the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
9 Free Software Foundation; either version 2, or (at your option) any
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
10 later version.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
11
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
15 for more details.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
16
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
17 You should have received a copy of the GNU General Public License
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
18 along with XEmacs; see the file COPYING. If not, write to
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
20 Boston, MA 02111-1307, USA. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
21
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
22 /* Compile-time symbols that this file uses:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
23
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
24 FIXUP_KERNEL_SYMBOL_ADDR() Adjust address in returned struct nlist.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
25 KERNEL_FILE Pathname of the kernel to nlist.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
26 LDAV_CVT() Scale the load average from the kernel.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
27 Returns a double.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
28 LDAV_SYMBOL Name of kernel symbol giving load average.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
29 LOAD_AVE_TYPE Type of the load average array in the kernel.
4759
aa5ed11f473b Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents: 1315
diff changeset
30 Must be defined; otherwise, no load average
aa5ed11f473b Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents: 1315
diff changeset
31 is available.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
32 NLIST_STRUCT Include nlist.h, not a.out.h, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
33 the nlist n_name element is a pointer,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
34 not an array.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
35 LINUX_LDAV_FILE [__linux__]: File containing load averages.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
36
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
37 Specific system predefines this file uses, aside from setting
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
38 default values if not emacs:
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
39
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
40 BSD Real BSD, not just BSD-like.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
41 hpux
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
42 sgi
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
43 WIN32_NATIVE No-op for Windows9x/NT.
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
44 CYGWIN No-op for Cygwin.
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
45 __linux__ Linux: assumes /proc filesystem mounted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
46 Support from Michael K. Johnson.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
47 __NetBSD__ NetBSD: assumes /kern filesystem mounted.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
48 __OpenBSD__ OpenBSD: ditto.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
49
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
50 In addition, to avoid nesting many #ifdefs, we internally set
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
51 LDAV_DONE to indicate that the load average has been computed.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
52
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
53 We also #define LDAV_PRIVILEGED if a program will require
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
54 special installation to be able to call getloadavg. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
55
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
56 /* This should always be first. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
57 #ifdef HAVE_CONFIG_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
58 #include <config.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
59 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
60
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
61 #include "lisp.h"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
62 #include "sysfile.h" /* for encapsulated open, close, read, write */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
63
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
64 #ifndef HAVE_GETLOADAVG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
65
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
66 /* The existing Emacs configuration files define a macro called
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
67 LOAD_AVE_CVT, which accepts a value of type LOAD_AVE_TYPE, and
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
68 returns the load average multiplied by 100. What we actually want
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
69 is a macro called LDAV_CVT, which returns the load average as an
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
70 unmultiplied double.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
71
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
72 For backwards compatibility, we'll define LDAV_CVT in terms of
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
73 LOAD_AVE_CVT, but future machine config files should just define
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
74 LDAV_CVT directly. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
75
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
76 #if !defined(LDAV_CVT) && defined(LOAD_AVE_CVT)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
77 #define LDAV_CVT(n) (LOAD_AVE_CVT (n) / 100.0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
78 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
79
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
80 #if defined (HAVE_KSTAT_H)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
81 #include <kstat.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
82 #endif /* HAVE_KSTAT_H */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
83
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
84 /* Set values that are different from the defaults, which are
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
85 set a little farther down with #ifndef. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
86
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
87
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
88 /* Some shorthands. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
89
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
90 #if defined (HPUX) && !defined (hpux)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
91 #define hpux
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
92 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
93
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
94 #if (defined(sun) && defined(SVR4)) || defined (SOLARIS2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
95 #define SUNOS_5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
96 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
97
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
98 #if defined (__osf__) && (defined (__alpha) || defined (__alpha__))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
99 #define OSF_ALPHA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
100 #include <netdb.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
101 #include <netinet/in.h> /* Needed for Digital UNIX V3 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
102 #include <net/proto_net.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
103 #include <sys/table.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
104 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
105
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
106 #if defined (__osf__) && (defined (mips) || defined (__mips__))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
107 #define OSF_MIPS
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
108 #include <sys/table.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
109 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
110
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
111
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
112 /* VAX C can't handle multi-line #ifs, or lines longer than 256 chars. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
113 #ifndef LOAD_AVE_TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
114
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
115 #ifdef sun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
116 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
117 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
118
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
119 #ifdef decstation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
120 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
121 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
122
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
123 #ifdef sgi
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
124 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
125 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
126
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
127 #ifdef SVR4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
128 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
129 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
130
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
131 #ifdef sony_news
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
132 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
133 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
134
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
135 #ifdef OSF_ALPHA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
136 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
137 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
138
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
139 #if defined (ardent) && defined (titan)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
140 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
141 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
142
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
143 #ifdef _AIX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
144 #define LOAD_AVE_TYPE long
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
145 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
146
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
147 #endif /* No LOAD_AVE_TYPE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
148
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
149 #ifdef OSF_ALPHA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
150 /* <sys/param.h> defines an incorrect value for FSCALE on Alpha OSF/1,
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
151 according to ghazi@noc.rutgers.edu. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
152 #undef FSCALE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
153 #define FSCALE 1024.0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
154 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
155
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
156 #ifndef FSCALE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
157
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
158 /* SunOS and some others define FSCALE in sys/param.h. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
159
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
160 #if defined(MIPS) || defined(SVR4) || defined(decstation)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
161 #define FSCALE 256
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
162 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
163
4759
aa5ed11f473b Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents: 1315
diff changeset
164 #if defined (sgi)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
165 /* Sometimes both MIPS and sgi are defined, so FSCALE was just defined
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
166 above under #ifdef MIPS. But we want the sgi value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
167 #undef FSCALE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
168 #define FSCALE 1000.0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
169 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
170
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
171 #if defined (ardent) && defined (titan)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
172 #define FSCALE 65536.0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
173 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
174
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
175 #ifdef _AIX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
176 #define FSCALE 65536.0
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
177 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
178
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
179 #endif /* Not FSCALE. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
180
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
181 #if !defined (LDAV_CVT) && defined (FSCALE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
182 #define LDAV_CVT(n) (((double) (n)) / FSCALE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
183 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
184
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
185 /* VAX C can't handle multi-line #ifs, or lines longer that 256 characters. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
186 #ifndef NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
187
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
188 #ifdef sun
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
189 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
190 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
191
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
192 #ifdef decstation
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
193 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
194 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
195
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
196 #ifdef hpux
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
197 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
198 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
199
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
200 #ifdef sgi
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
201 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
202 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
203
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
204 #ifdef SVR4
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
205 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
206 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
207
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
208 #ifdef sony_news
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
209 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
210 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
211
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
212 #ifdef OSF_ALPHA
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
213 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
214 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
215
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
216 #if defined (ardent) && defined (titan)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
217 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
218 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
219
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
220 #ifdef butterfly
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
221 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
222 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
223
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
224 #ifdef _AIX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
225 #define NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
226 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
227
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
228 #endif /* defined (NLIST_STRUCT) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
229
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
230
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
231 #if defined(sgi) || (defined(mips) && !defined(BSD))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
232 #define FIXUP_KERNEL_SYMBOL_ADDR(nl) ((nl)[0].n_value &= ~(1 << 31))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
233 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
234
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
235 #if !defined (KERNEL_FILE) && defined (hpux)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
236 #define KERNEL_FILE "/hp-ux"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
237 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
238
4759
aa5ed11f473b Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents: 1315
diff changeset
239 #if !defined(KERNEL_FILE) && (defined(MIPS) || defined(SVR4) || defined(ISC) || defined (sgi) || defined(SVR4) || (defined (ardent) && defined (titan)))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
240 #define KERNEL_FILE "/unix"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
241 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
242
4759
aa5ed11f473b Remove support for obsolete systems. See xemacs-patches message with ID
Jerry James <james@xemacs.org>
parents: 1315
diff changeset
243 #if !defined(LDAV_SYMBOL) && (defined(hpux) || defined(SVR4) || defined(ISC) || defined(sgi) || (defined (ardent) && defined (titan)) || defined (_AIX))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
244 #define LDAV_SYMBOL "avenrun"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
245 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
246
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
247 #ifdef HAVE_UNISTD_H
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
248 #include <unistd.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
249 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
250
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
251 #include <stdio.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
252
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
253 /* LOAD_AVE_TYPE should only get defined if we're going to use the
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
254 nlist method. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
255 #if !defined(LOAD_AVE_TYPE) && (defined(BSD) || defined(LDAV_CVT) || defined(KERNEL_FILE) || defined(LDAV_SYMBOL))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
256 #define LOAD_AVE_TYPE double
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
257 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
258
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
259 #ifdef LOAD_AVE_TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
260
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
261 #ifndef NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
262 #include <a.out.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
263 #else /* NLIST_STRUCT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
264 #include <nlist.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
265 #endif /* NLIST_STRUCT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
266
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
267 #ifdef SUNOS_5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
268 #include <fcntl.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
269 #include <kvm.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
270 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
271
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
272 #ifndef KERNEL_FILE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
273 #define KERNEL_FILE "/vmunix"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
274 #endif /* KERNEL_FILE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
275
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
276 #ifndef LDAV_SYMBOL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
277 #define LDAV_SYMBOL "_avenrun"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
278 #endif /* LDAV_SYMBOL */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
279
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
280 #ifndef LDAV_CVT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
281 #define LDAV_CVT(n) ((double) (n))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
282 #endif /* !LDAV_CVT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
283
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
284 #endif /* LOAD_AVE_TYPE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
285
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
286 #ifdef sgi
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
287 #include <sys/sysmp.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
288 #endif /* sgi */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
289
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
290 #if defined (HAVE_SYS_PSTAT_H)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
291 #include <sys/pstat.h>
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
292 #endif /* HAVE_SYS_PSTAT_H (on HPUX) */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
293
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
294
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
295 /* Avoid static vars inside a function since in HPUX they dump as pure. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
296
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
297 #ifdef LOAD_AVE_TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
298 /* File descriptor open to /dev/kmem */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
299 static int channel;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
300 /* Nonzero iff channel is valid. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
301 static int getloadavg_initialized;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
302 /* Offset in kmem to seek to read load average, or 0 means invalid. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
303 static long offset;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
304
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
305 #ifndef sgi
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
306 static struct nlist nl[2];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
307 #endif /* not sgi */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
308
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
309 #ifdef SUNOS_5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
310 static kvm_t *kd;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
311 #endif /* SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
312
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
313 #ifndef countof
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
314 # define countof(x) (sizeof (x) / sizeof (*(x)))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
315 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
316
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
317 #endif /* LOAD_AVE_TYPE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
318
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
319 /* Put the 1 minute, 5 minute and 15 minute load averages
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
320 into the first NELEM elements of LOADAVG.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
321 Return the number written (never more than 3, but may be less than NELEM),
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
322 or -1 if an error occurred. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
323
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
324 int getloadavg (double loadavg[], int nelem);
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
325
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
326 int
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
327 getloadavg (double loadavg[], int nelem)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
328 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
329 int elem = 0; /* Return value. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
330
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
331 #ifdef NO_GET_LOAD_AVG
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
332 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
333 /* Set errno to zero to indicate that there was no particular error;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
334 this function just can't work at all on this system. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
335 errno = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
336 elem = -2;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
337 #endif /* NO_GET_LOAD_AVG */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
338
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
339 #if ! defined (LDAV_DONE) && defined (HAVE_KSTAT_H) && defined (HAVE_LIBKSTAT)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
340 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
341 /* getloadavg is best implemented using kstat (kernel stats), on
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
342 systems (like SunOS5) that support it, since you don't need special
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
343 privileges to use it.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
344
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
345 Initial implementation courtesy Zlatko Calusic <zcalusic@carnet.hr>.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
346 Integrated to XEmacs by Hrvoje Niksic <hniksic@xemacs.org>.
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
347 Additional cleanup by Hrvoje Niksic, based on code published by
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
348 Casper Dik <Casper.Dik@Holland.Sun.Com>. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
349 kstat_ctl_t *kc;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
350 kstat_t *ksp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
351 static char *avestrings[] = { "avenrun_1min",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
352 "avenrun_5min",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
353 "avenrun_15min" };
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
354
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
355 if (nelem > countof (avestrings))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
356 nelem = countof (avestrings);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
357
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
358 kc = kstat_open ();
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
359 if (!kc)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
360 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
361 ksp = kstat_lookup (kc, "unix", 0, "system_misc");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
362 if (!ksp)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
363 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
364 kstat_close (kc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
365 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
366 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
367 if (kstat_read (kc, ksp, 0) < 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
368 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
369 kstat_close (kc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
370 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
371 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
372 for (elem = 0; elem < nelem; elem++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
373 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
374 kstat_named_t *kn =
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
375 (kstat_named_t *) kstat_data_lookup (ksp, avestrings[elem]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
376 if (!kn)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
377 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
378 kstat_close (kc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
379 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
380 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
381 loadavg[elem] = (double)kn->value.ul / FSCALE;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
382 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
383 kstat_close (kc);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
384 #endif /* HAVE_KSTAT_H && HAVE_LIBKSTAT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
385
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
386 #if !defined (LDAV_DONE) && defined (HAVE_SYS_PSTAT_H)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
387 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
388 /* This is totally undocumented, and is not guaranteed to work, but
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
389 mayhap it might .... If it does work, it will work only on HP-UX
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
390 8.0 or later. -- Darryl Okahata <darrylo@sr.hp.com> */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
391 #undef LOAD_AVE_TYPE /* Make sure these don't exist. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
392 #undef LOAD_AVE_CVT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
393 #undef LDAV_SYMBOL
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
394 struct pst_dynamic procinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
395 union pstun statbuf;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
396
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
397 statbuf.pst_dynamic = &procinfo;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
398 if (pstat (PSTAT_DYNAMIC, statbuf, sizeof (struct pst_dynamic), 0, 0) == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
399 return (-1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
400 loadavg[elem++] = procinfo.psd_avg_1_min;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
401 loadavg[elem++] = procinfo.psd_avg_5_min;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
402 loadavg[elem++] = procinfo.psd_avg_15_min;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
403 #endif /* HPUX */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
404
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
405 #if !defined (LDAV_DONE) && defined (__linux__)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
406 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
407 #undef LOAD_AVE_TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
408
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
409 #ifndef LINUX_LDAV_FILE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
410 #define LINUX_LDAV_FILE "/proc/loadavg"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
411 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
412
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
413 char ldavgbuf[40];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
414 double load_ave[3];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
415 int fd, count;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
416
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
417 fd = retry_open (LINUX_LDAV_FILE, O_RDONLY);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
418 if (fd == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
419 return -1;
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
420 count = retry_read (fd, ldavgbuf, 40);
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
421 (void) retry_close (fd);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
422 if (count <= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
423 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
424
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
425 count = sscanf (ldavgbuf, "%lf %lf %lf",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
426 &load_ave[0], &load_ave[1], &load_ave[2]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
427 if (count < 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
428 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
429
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
430 for (elem = 0; elem < nelem && elem < count; elem++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
431 loadavg[elem] = load_ave[elem];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
432 #endif /* __linux__ */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
433
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
434 #if !defined (LDAV_DONE) && defined (__NetBSD__) || defined (__OpenBSD__)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
435 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
436 #undef LOAD_AVE_TYPE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
437
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
438 #ifndef NETBSD_LDAV_FILE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
439 #define NETBSD_LDAV_FILE "/kern/loadavg"
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
440 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
441
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
442 unsigned long int load_ave[3], scale;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
443 int count;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
444 FILE *fp;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
445
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
446 fp = retry_fopen (NETBSD_LDAV_FILE, "r");
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
447 if (fp == NULL)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
448 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
449 count = fscanf (fp, "%lu %lu %lu %lu\n",
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
450 &load_ave[0], &load_ave[1], &load_ave[2],
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
451 &scale);
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
452 (void) retry_fclose (fp);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
453 if (count != 4)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
454 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
455
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
456 for (elem = 0; elem < nelem; elem++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
457 loadavg[elem] = (double) load_ave[elem] / (double) scale;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
458 #endif /* __NetBSD__ or __OpenBSD__ */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
459
1315
70921960b980 [xemacs-hg @ 2003-02-20 08:19:28 by ben]
ben
parents: 978
diff changeset
460 #if !defined (LDAV_DONE) && defined (WIN32_ANY)
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
461 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
462
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
463 /* A faithful emulation is going to have to be saved for a rainy day. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
464 for ( ; elem < nelem; elem++)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
465 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
466 loadavg[elem] = 0.0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
467 }
442
abe6d1db359e Import from CVS: tag r21-2-36
cvs
parents: 440
diff changeset
468 #endif /* WIN32_NATIVE or CYGWIN */
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
469
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
470 #if !defined (LDAV_DONE) && defined(LOAD_AVE_TYPE)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
471
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
472 /* UNIX-specific code -- read the average from /dev/kmem. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
473
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
474 #define LDAV_PRIVILEGED /* This code requires special installation. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
475
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
476 LOAD_AVE_TYPE load_ave[3];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
477
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
478 /* Get the address of LDAV_SYMBOL. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
479 if (offset == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
480 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
481 #ifndef sgi
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
482 #ifndef NLIST_STRUCT
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
483 strcpy (nl[0].n_name, LDAV_SYMBOL);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
484 strcpy (nl[1].n_name, "");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
485 #else /* NLIST_STRUCT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
486 nl[0].n_name = (char *) LDAV_SYMBOL;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
487 nl[1].n_name = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
488 #endif /* NLIST_STRUCT */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
489
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
490 #ifndef SUNOS_5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
491 if (
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
492 #if !(defined (_AIX) && !defined (ps2))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
493 nlist (KERNEL_FILE, nl)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
494 #else /* _AIX */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
495 knlist (nl, 1, sizeof (nl[0]))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
496 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
497 >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
498 /* Omit "&& nl[0].n_type != 0 " -- it breaks on Sun386i. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
499 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
500 #ifdef FIXUP_KERNEL_SYMBOL_ADDR
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
501 FIXUP_KERNEL_SYMBOL_ADDR (nl);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
502 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
503 offset = nl[0].n_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
504 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
505 #endif /* !SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
506 #else /* sgi */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
507 int ldav_off;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
508
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
509 ldav_off = sysmp (MP_KERNADDR, MPKA_AVENRUN);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
510 if (ldav_off != -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
511 offset = (long) ldav_off & 0x7fffffff;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
512 #endif /* sgi */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
513 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
514
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
515 /* Make sure we have /dev/kmem open. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
516 if (!getloadavg_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
517 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
518 #ifndef SUNOS_5
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
519 channel = retry_open ("/dev/kmem", 0);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
520 if (channel >= 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
521 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
522 /* Set the channel to close on exec, so it does not
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
523 litter any child's descriptor table. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
524 #ifdef FD_SETFD
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
525 #ifndef FD_CLOEXEC
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
526 #define FD_CLOEXEC 1
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
527 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
528 (void) fcntl (channel, F_SETFD, FD_CLOEXEC);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
529 #endif
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
530 getloadavg_initialized = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
531 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
532 #else /* SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
533 /* We pass 0 for the kernel, corefile, and swapfile names
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
534 to use the currently running kernel. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
535 kd = kvm_open (0, 0, 0, O_RDONLY, 0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
536 if (kd != 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
537 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
538 /* nlist the currently running kernel. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
539 kvm_nlist (kd, nl);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
540 offset = nl[0].n_value;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
541 getloadavg_initialized = 1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
542 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
543 #endif /* SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
544 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
545
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
546 /* If we can, get the load average values. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
547 if (offset && getloadavg_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
548 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
549 /* Try to read the load. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
550 #ifndef SUNOS_5
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
551 if (lseek (channel, offset, 0) == -1L
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
552 || retry_read (channel, (char *) load_ave, sizeof (load_ave))
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
553 != sizeof (load_ave))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
554 {
771
943eaba38521 [xemacs-hg @ 2002-03-13 08:51:24 by ben]
ben
parents: 647
diff changeset
555 retry_close (channel);
428
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
556 getloadavg_initialized = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
557 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
558 #else /* SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
559 if (kvm_read (kd, offset, (char *) load_ave, sizeof (load_ave))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
560 != sizeof (load_ave))
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
561 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
562 kvm_close (kd);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
563 getloadavg_initialized = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
564 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
565 #endif /* SUNOS_5 */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
566 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
567
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
568 if (offset == 0 || !getloadavg_initialized)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
569 return -1;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
570
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
571 if (nelem > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
572 loadavg[elem++] = LDAV_CVT (load_ave[0]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
573 if (nelem > 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
574 loadavg[elem++] = LDAV_CVT (load_ave[1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
575 if (nelem > 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
576 loadavg[elem++] = LDAV_CVT (load_ave[2]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
577
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
578 #define LDAV_DONE
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
579 #endif /* !LDAV_DONE && LOAD_AVE_TYPE */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
580
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
581 return elem;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
582 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
583
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
584 #endif /* ! HAVE_GETLOADAVG */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
585
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
586 #ifdef TEST
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
587 void
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
588 main (int argc, char **argv)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
589 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
590 int naptime = 0;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
591
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
592 if (argc > 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
593 naptime = atoi (argv[1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
594
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
595 while (1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
596 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
597 double avg[3];
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
598 int loads;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
599
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
600 errno = 0; /* Don't be misled if it doesn't set errno. */
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
601 loads = getloadavg (avg, 3);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
602 if (loads == -1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
603 {
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
604 perror ("Error getting load average");
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
605 exit (1);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
606 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
607 if (loads > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
608 printf ("1-minute: %f ", avg[0]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
609 if (loads > 1)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
610 printf ("5-minute: %f ", avg[1]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
611 if (loads > 2)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
612 printf ("15-minute: %f ", avg[2]);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
613 if (loads > 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
614 putchar ('\n');
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
615
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
616 if (naptime == 0)
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
617 break;
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
618 sleep (naptime);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
619 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
620
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
621 exit (0);
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
622 }
3ecd8885ac67 Import from CVS: tag r21-2-22
cvs
parents:
diff changeset
623 #endif /* TEST */