Commit | Line | Data |
---|---|---|
b6a89d8b | 1 | /*- |
e1cc4bef KM |
2 | * Copyright (c) 1989 The Regents of the University of California. |
3 | * All rights reserved. | |
4 | * | |
1c15e888 C |
5 | * Redistribution and use in source and binary forms are permitted provided |
6 | * that: (1) source distributions retain this entire copyright notice and | |
7 | * comment, and (2) distributions including binaries display the following | |
8 | * acknowledgement: ``This product includes software developed by the | |
9 | * University of California, Berkeley and its contributors'' in the | |
10 | * documentation or other materials provided with the distribution and in | |
11 | * all advertising materials mentioning features or use of this software. | |
12 | * Neither the name of the University nor the names of its contributors may | |
13 | * be used to endorse or promote products derived from this software without | |
14 | * specific prior written permission. | |
15 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED | |
16 | * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF | |
17 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
e1cc4bef KM |
18 | */ |
19 | ||
20 | #if defined(LIBC_SCCS) && !defined(lint) | |
1c15e888 | 21 | static char sccsid[] = "@(#)getloadavg.c 6.2 (Berkeley) 6/29/90"; |
b6a89d8b | 22 | #endif /* LIBC_SCCS and not lint */ |
e1cc4bef KM |
23 | |
24 | #include <sys/param.h> | |
25 | #include <sys/types.h> | |
26 | #include <sys/file.h> | |
e1cc4bef | 27 | #include <nlist.h> |
e1cc4bef KM |
28 | |
29 | static struct nlist nl[] = { | |
30 | { "_averunnable" }, | |
31 | #define X_AVERUNNABLE 0 | |
32 | { "_fscale" }, | |
33 | #define X_FSCALE 1 | |
34 | { "" }, | |
35 | }; | |
36 | ||
37 | /* | |
38 | * getloadavg() -- Get system load averages. | |
39 | * | |
40 | * Put `nelem' samples into `loadavg' array. | |
41 | * Return number of samples retrieved, or -1 on error. | |
42 | */ | |
43 | getloadavg(loadavg, nelem) | |
44 | double loadavg[]; | |
45 | int nelem; | |
46 | { | |
e1cc4bef KM |
47 | static int need_nlist = 1; |
48 | fixpt_t averunnable[3]; | |
49 | int fscale, kmemfd, i; | |
b6a89d8b | 50 | int alreadyopen; |
e1cc4bef | 51 | |
b6a89d8b MT |
52 | if ((alreadyopen = kvm_openfiles(NULL, NULL, NULL)) == -1) |
53 | return (-1); | |
54 | /* | |
55 | * cache nlist | |
56 | */ | |
e1cc4bef | 57 | if (need_nlist) { |
b6a89d8b MT |
58 | if (kvm_nlist(nl) != 0) |
59 | goto bad; | |
e1cc4bef KM |
60 | need_nlist = 0; |
61 | } | |
b6a89d8b MT |
62 | if (kvm_read((off_t)nl[X_AVERUNNABLE].n_value, (char *)averunnable, |
63 | sizeof(averunnable)) != sizeof(averunnable)) | |
e1cc4bef | 64 | goto bad; |
b6a89d8b MT |
65 | if (kvm_read( (off_t)nl[X_FSCALE].n_value, (char *)&fscale, |
66 | sizeof(fscale)) != sizeof(fscale)) | |
e1cc4bef | 67 | goto bad; |
e1cc4bef KM |
68 | nelem = MIN(nelem, sizeof(averunnable) / sizeof(averunnable[0])); |
69 | for (i = 0; i < nelem; i++) | |
70 | loadavg[i] = (double) averunnable[i] / fscale; | |
b6a89d8b MT |
71 | if (!alreadyopen) |
72 | kvm_close(); | |
e1cc4bef KM |
73 | return (nelem); |
74 | ||
75 | bad: | |
b6a89d8b MT |
76 | if (!alreadyopen) |
77 | kvm_close(); | |
e1cc4bef KM |
78 | return (-1); |
79 | } |