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) | |
b6a89d8b MT |
9 | static char sccsid[] = "@(#)getloadavg.c 6.2 (Berkeley) %G%"; |
10 | #endif /* LIBC_SCCS and not lint */ | |
e1cc4bef KM |
11 | |
12 | #include <sys/param.h> | |
13 | #include <sys/types.h> | |
14 | #include <sys/file.h> | |
e1cc4bef | 15 | #include <nlist.h> |
e1cc4bef KM |
16 | |
17 | static struct nlist nl[] = { | |
18 | { "_averunnable" }, | |
19 | #define X_AVERUNNABLE 0 | |
20 | { "_fscale" }, | |
21 | #define X_FSCALE 1 | |
22 | { "" }, | |
23 | }; | |
24 | ||
25 | /* | |
26 | * getloadavg() -- Get system load averages. | |
27 | * | |
28 | * Put `nelem' samples into `loadavg' array. | |
29 | * Return number of samples retrieved, or -1 on error. | |
30 | */ | |
31 | getloadavg(loadavg, nelem) | |
32 | double loadavg[]; | |
33 | int nelem; | |
34 | { | |
e1cc4bef KM |
35 | static int need_nlist = 1; |
36 | fixpt_t averunnable[3]; | |
37 | int fscale, kmemfd, i; | |
b6a89d8b | 38 | int alreadyopen; |
e1cc4bef | 39 | |
b6a89d8b MT |
40 | if ((alreadyopen = kvm_openfiles(NULL, NULL, NULL)) == -1) |
41 | return (-1); | |
42 | /* | |
43 | * cache nlist | |
44 | */ | |
e1cc4bef | 45 | if (need_nlist) { |
b6a89d8b MT |
46 | if (kvm_nlist(nl) != 0) |
47 | goto bad; | |
e1cc4bef KM |
48 | need_nlist = 0; |
49 | } | |
b6a89d8b MT |
50 | if (kvm_read((off_t)nl[X_AVERUNNABLE].n_value, (char *)averunnable, |
51 | sizeof(averunnable)) != sizeof(averunnable)) | |
e1cc4bef | 52 | goto bad; |
b6a89d8b MT |
53 | if (kvm_read( (off_t)nl[X_FSCALE].n_value, (char *)&fscale, |
54 | sizeof(fscale)) != sizeof(fscale)) | |
e1cc4bef | 55 | goto bad; |
e1cc4bef KM |
56 | nelem = MIN(nelem, sizeof(averunnable) / sizeof(averunnable[0])); |
57 | for (i = 0; i < nelem; i++) | |
58 | loadavg[i] = (double) averunnable[i] / fscale; | |
b6a89d8b MT |
59 | if (!alreadyopen) |
60 | kvm_close(); | |
e1cc4bef KM |
61 | return (nelem); |
62 | ||
63 | bad: | |
b6a89d8b MT |
64 | if (!alreadyopen) |
65 | kvm_close(); | |
e1cc4bef KM |
66 | return (-1); |
67 | } |