|
|
1.1 root 1: /*-
2: * Copyright (c) 1989 The Regents of the University of California.
3: * All rights reserved.
4: *
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.
18: */
19:
20: #if defined(LIBC_SCCS) && !defined(lint)
21: static char sccsid[] = "@(#)getloadavg.c 6.2 (Berkeley) 6/29/90";
22: #endif /* LIBC_SCCS and not lint */
23:
24: #include <sys/param.h>
25: #include <sys/types.h>
26: #include <sys/file.h>
27: #include <nlist.h>
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: {
47: static int need_nlist = 1;
48: fixpt_t averunnable[3];
49: int fscale, kmemfd, i;
50: int alreadyopen;
51:
52: if ((alreadyopen = kvm_openfiles(NULL, NULL, NULL)) == -1)
53: return (-1);
54: /*
55: * cache nlist
56: */
57: if (need_nlist) {
58: if (kvm_nlist(nl) != 0)
59: goto bad;
60: need_nlist = 0;
61: }
62: if (kvm_read((off_t)nl[X_AVERUNNABLE].n_value, (char *)averunnable,
63: sizeof(averunnable)) != sizeof(averunnable))
64: goto bad;
65: if (kvm_read( (off_t)nl[X_FSCALE].n_value, (char *)&fscale,
66: sizeof(fscale)) != sizeof(fscale))
67: goto bad;
68: nelem = MIN(nelem, sizeof(averunnable) / sizeof(averunnable[0]));
69: for (i = 0; i < nelem; i++)
70: loadavg[i] = (double) averunnable[i] / fscale;
71: if (!alreadyopen)
72: kvm_close();
73: return (nelem);
74:
75: bad:
76: if (!alreadyopen)
77: kvm_close();
78: return (-1);
79: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.