xref: /original-bsd/lib/libc/gen/getloadavg.c (revision 260a368e)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getloadavg.c	6.6 (Berkeley) 04/27/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 size, i, mib[2];
33 
34 	mib[0] = CTL_VM;
35 	mib[1] = VM_LOADAVG;
36 	size = sizeof(loadinfo);
37 	if (sysctl(mib, 2, &loadinfo, &size, NULL, 0) < 0)
38 		return (-1);
39 
40 	nelem = MIN(nelem, sizeof(loadinfo.ldavg) / sizeof(fixpt_t));
41 	for (i = 0; i < nelem; i++)
42 		loadavg[i] = (double) loadinfo.ldavg[i] / loadinfo.fscale;
43 	return (nelem);
44 }
45