1 /*
2 * (c) Copyright 1998-2001 by Rob Braun
3 * All rights reserved. The file named COPYRIGHT specifies the terms
4 * and conditions for redistribution.
5 */
6 /* This file contains OS dependant implementations of xgetloadavg().
7 * xgetloadavg takes no arguments and simply returns the 1 minute
8 * load average of the machine as a double.
9 * Feel free to add implementations here, please update configure.in
10 * to define HAVE_LOADAVG. Defining this macro enables the option
11 * in the rest of the code.
12 * --Rob
13 */
14
15 #include "config.h"
16 #ifdef HAVE_LOADAVG
17
18 #ifdef linux
19 #include <stdio.h>
20 #include "xgetloadavg.h"
21
22 #define LFILE "/proc/loadavg"
23 #define MAX 32
24
xgetloadavg(void)25 double xgetloadavg(void)
26 {
27 FILE *fd;
28 double ret = 0;
29
30 fd = fopen(LFILE, "r");
31 if( fd == NULL ) {
32 return -1;
33 }
34
35 if( fscanf(fd, "%lf", &ret) != 1 ) {
36 perror("fscanf");
37 return -1;
38 }
39
40 fclose(fd);
41
42 return ret;
43 }
44 #endif /* linux */
45
46
47 #ifdef solaris
48 #ifdef HAVE_KSTAT_H
49 #include <kstat.h>
50
xgetloadavg(void)51 double xgetloadavg(void)
52 {
53 kstat_ctl_t *kc = NULL;
54 kstat_t *ks = NULL;
55 kstat_named_t *kn = NULL;
56
57 kc = kstat_open();
58 if( kc == NULL ) {
59 return -1;
60 }
61
62 ks = kstat_lookup(kc, "unix", 0, "system_misc");
63 if( ks == NULL ) {
64 return -1;
65 }
66
67 if( kstat_read(kc, ks, 0) == -1 ) {
68 return -1;
69 }
70
71 kn = kstat_data_lookup(ks, "avenrun_1min");
72 if( kn == NULL ) {
73 return -1;
74 }
75
76 if( ks->ks_type == KSTAT_TYPE_NAMED ) {
77 kn = ks->ks_data;
78 if( kn == NULL ) {
79 return -1;
80 }
81
82 return (double)(kn->value.ui32)/100;
83 }
84
85 kstat_close(kc);
86 }
87 #endif /* HAVE_KSTAT */
88 #endif /* solaris */
89 #ifdef __FreeBSD__
90
91 #include <stdio.h>
92 #include <sys/param.h>
93 #include <sys/fcntl.h>
94
xgetloadavg()95 double xgetloadavg ()
96 {
97 double ret[3];
98
99 getloadavg (ret, sizeof (ret) / sizeof (ret[0]));
100
101 return ret[0];
102 }
103 #endif /* __FreeBSD__ */
104
105
106 #if defined(bsdi) || defined(__APPLE__)
107 #include <stdlib.h>
108
xgetloadavg(void)109 double xgetloadavg(void)
110 {
111 double loadavg[3];
112
113 if (getloadavg(loadavg, 1) == -1)
114 {
115 return -1;
116 }
117 else
118 {
119 return loadavg[0];
120 }
121 }
122 #endif /* bsdi || __APPLE__ */
123
124
125 #ifdef __osf__
126 #include <sys/table.h>
127
xgetloadavg(void)128 double xgetloadavg(void)
129 {
130 struct tbl_loadavg labuf;
131
132 if (table(TBL_LOADAVG, 0, &labuf, 1, sizeof(labuf)) < 0) {
133 perror("TBL_LOADAVG");
134 return (-1);
135 }
136
137 if (labuf.tl_lscale) {
138 return ((double)labuf.tl_avenrun.l[2] /
139 (double)labuf.tl_lscale);
140 }
141
142 return (labuf.tl_avenrun.d[2]);
143 }
144 #endif /* __osf__ */
145
146 #endif /* HAVE_LOADAVG */
147
148