xref: /original-bsd/lib/libc/gen/getloadavg.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getloadavg.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include <sys/param.h>
13 #include <sys/time.h>
14 #include <sys/resource.h>
15 #include <sys/sysctl.h>
16 #include <vm/vm_param.h>
17 
18 #include <stdlib.h>
19 
20 /*
21  * getloadavg() -- Get system load averages.
22  *
23  * Put `nelem' samples into `loadavg' array.
24  * Return number of samples retrieved, or -1 on error.
25  */
26 int
27 getloadavg(loadavg, nelem)
28 	double loadavg[];
29 	int nelem;
30 {
31 	struct loadavg loadinfo;
32 	int i, mib[2];
33 	size_t size;
34 
35 	mib[0] = CTL_VM;
36 	mib[1] = VM_LOADAVG;
37 	size = sizeof(loadinfo);
38 	if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0)
39 		return (-1);
40 
41 	nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t));
42 	for (i = 0; i < nelem; i++)
43 		loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale;
44 	return (nelem);
45 }
46