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