1 /* Copyright (c) 2008, 2009
2 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
3 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
4 * Micah Cowan (micah@cowan.name)
5 * Sadrul Habib Chowdhury (sadrul@users.sourceforge.net)
6 * Copyright (c) 1993-2002, 2003, 2005, 2006, 2007
7 * Juergen Weigert (jnweiger@immd4.informatik.uni-erlangen.de)
8 * Michael Schroeder (mlschroe@immd4.informatik.uni-erlangen.de)
9 * Copyright (c) 1987 Oliver Laumann
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 3, or (at your option)
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program (see the file COPYING); if not, see
23 * http://www.gnu.org/licenses/, or contact Free Software Foundation, Inc.,
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
26 ****************************************************************
29 #include <sys/types.h>
32 # include <sys/fixpoint.h>
35 /* mach stuff included here to prevent index macro conflict */
37 # include <sys/version.h>
38 # if KERNEL_MAJOR_VERSION > 2
39 # include <mach/mach.h>
52 static int GetLoadav
__P((void));
54 static LOADAV_TYPE loadav
[LOADAV_NUM
];
59 /***************************************************************/
61 #if defined(linux) && !defined(LOADAV_DONE)
64 * This is the easy way. It relies in /proc being mounted.
65 * For the big and ugly way refer to previous screen version.
81 if ((fp
= secfopen("/proc/loadavg", "r")) == NULL
)
84 fgets(buf
, sizeof(buf
), fp
);
86 /* can't use fscanf because the decimal point symbol depends on
87 * the locale but the kernel uses always '.'.
90 for (i
= 0; i
< (LOADAV_NUM
> 3 ? 3 : LOADAV_NUM
); i
++)
101 else if (*s
>= '0' && *s
<= '9')
103 d
= d
* 10 + (*s
- '0');
111 loadav
[i
] = e
? d
/ e
: d
;
117 /***************************************************************/
119 #if defined(LOADAV_GETLOADAVG) && !defined(LOADAV_DONE)
130 return getloadavg(loadav
, LOADAV_NUM
);
134 /***************************************************************/
136 #if defined(apollo) && !defined(LOADAV_DONE)
147 proc1_$
get_loadav(loadav
);
152 /***************************************************************/
154 #if defined(NeXT) && !defined(LOADAV_DONE)
157 static processor_set_t default_set
;
164 error
= processor_set_default(host_self(), &default_set
);
165 if (error
!= KERN_SUCCESS
)
166 mach_error("Error calling processor_set_default", error
);
174 unsigned int info_count
;
175 struct processor_set_basic_info info
;
178 info_count
= PROCESSOR_SET_BASIC_INFO_COUNT
;
179 if (processor_set_info(default_set
, PROCESSOR_SET_BASIC_INFO
, &host
, (processor_set_info_t
)&info
, &info_count
) != KERN_SUCCESS
)
181 loadav
[0] = (float)info
.load_average
/ LOAD_SCALE
;
186 /***************************************************************/
188 #if defined(sun) && defined(SVR4) && !defined(LOADAV_DONE)
193 static kstat_ctl_t
*kc
;
198 loadok
= (kc
= kstat_open()) != 0;
205 kstat_named_t
*avgs
[3];
208 kstat_chain_update(kc
);
209 if ((ks
= kstat_lookup(kc
, "unix", -1, "system_misc")) == 0 || kstat_read(kc
, ks
, (void *)0) == -1)
211 avgs
[0] = kstat_data_lookup(ks
, "avenrun_1min");
212 avgs
[1] = kstat_data_lookup(ks
, "avenrun_5min");
213 avgs
[2] = kstat_data_lookup(ks
, "avenrun_15min");
214 for (i
= 0; i
< 3; i
++)
216 if (avgs
[i
] == 0 || avgs
[i
]->data_type
!= KSTAT_DATA_ULONG
)
218 loadav
[i
] = avgs
[i
]->value
.ul
;
225 /***************************************************************/
227 #if defined(__osf__) && defined(__alpha) && !defined(LOADAV_DONE)
230 struct rtentry
; struct mbuf
; /* shut up gcc on OSF/1 4.0 */
231 #include <sys/table.h>
242 struct tbl_loadavg tbl
;
245 if (table(TBL_LOADAVG
, 0, &tbl
, 1, sizeof(struct tbl_loadavg
)) != 1)
251 for (i
= 0; i
< LOADAV_NUM
; i
++)
252 loadav
[i
] = (double) tbl
.tl_avenrun
.l
[i
] / tbl
.tl_lscale
;
257 for (i
= 0; i
< LOADAV_NUM
; i
++)
258 loadav
[i
] = tbl
.tl_avenrun
.d
[i
];
264 /***************************************************************/
266 #if !defined(LOADAV_DONE)
268 * The old fashion way: open kernel and read avenrun
270 * Header File includes
278 # ifndef NLIST_DECLARED
279 extern int nlist
__P((char *, struct nlist
*));
282 #ifdef LOADAV_USE_NLIST64
283 # define nlist nlist64
286 static struct nlist nl
[2];
290 # define nlist(u,l) knlist(l,1,sizeof(*l))
296 debug("Init Kmem...\n");
297 if ((kmemf
= open("/dev/kmem", O_RDONLY
)) == -1)
299 # if !defined(_AUX_SOURCE) && !defined(AUX)
300 # ifdef NLIST_NAME_UNION
301 nl
[0].n_un
.n_name
= LOADAV_AVENRUN
;
303 nl
[0].n_name
= LOADAV_AVENRUN
;
306 strncpy(nl
[0].n_name
, LOADAV_AVENRUN
, sizeof(nl
[0].n_name
));
308 debug2("Searching in %s for %s\n", LOADAV_UNIX
, nl
[0].n_name
);
309 nlist(LOADAV_UNIX
, nl
);
310 if (nl
[0].n_value
== 0)
315 # if 0 /* no longer needed (Al.Smith@aeschi.ch.eu.org) */
317 nl
[0].n_value
&= (unsigned long)-1 >> 1; /* clear upper bit */
320 debug1("AvenrunSym found (0x%lx)!!\n", nl
[0].n_value
);
327 if (lseek(kmemf
, (off_t
) nl
[0].n_value
, 0) == (off_t
)-1)
329 if (read(kmemf
, (char *) loadav
, sizeof(loadav
)) != sizeof(loadav
))
335 /***************************************************************/
338 #define FIX_TO_DBL(l) ((double)(l) / LOADAV_SCALE)
349 for (i
= 0; i
< j
; i
++)
351 sprintf(p
, " %2.2f" + !i
, FIX_TO_DBL(loadav
[i
]));