summaryrefslogtreecommitdiff
path: root/src/pmdas/linux/proc_stat.c
blob: 6a8a798ade4aea49da65159171d825a645418d7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
 * Linux /proc/stat metrics cluster
 *
 * Copyright (c) 2012-2014 Red Hat.
 * Copyright (c) 2008-2009 Aconex.  All Rights Reserved.
 * Copyright (c) 2000,2004-2008 Silicon Graphics, Inc.  All Rights Reserved.
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "pmapi.h"
#include "impl.h"
#include "pmda.h"
#include "indom.h"
#include <dirent.h>
#include <ctype.h>
#include <sys/stat.h>
#include "proc_cpuinfo.h"
#include "proc_stat.h"

int
refresh_proc_stat(proc_cpuinfo_t *proc_cpuinfo, proc_stat_t *proc_stat)
{
    pmdaIndom *idp = PMDAINDOM(CPU_INDOM);
    char buf[MAXPATHLEN];
    char fmt[64];
    static int fd = -1; /* kept open until exit() */
    static int started;
    static char *statbuf;
    static int maxstatbuf;
    static char **bufindex;
    static int nbufindex;
    static int maxbufindex;
    int size;
    int n;
    int i;
    int j;

    if (fd >= 0) {
	if (lseek(fd, 0, SEEK_SET) < 0)
	    return -oserror();
    } else {
	snprintf(buf, sizeof(buf), "%s/proc/stat", linux_statspath);
	if ((fd = open(buf, O_RDONLY)) < 0)
	    return -oserror();
    }

    for (n=0;;) {
	while (n >= maxstatbuf) {
	    size = maxstatbuf + 512;
	    if ((statbuf = (char *)realloc(statbuf, size)) == NULL)
		return -ENOMEM;
	    maxstatbuf = size;
	}
	size = (statbuf + maxstatbuf) - (statbuf + n);
	if ((i = read(fd, statbuf + n, size)) > 0)
	    n += i;
	else
	    break;
    }
    statbuf[n] = '\0';

    if (bufindex == NULL) {
	size = 4 * sizeof(char *);
	if ((bufindex = (char **)malloc(size)) == NULL)
	    return -ENOMEM;
	maxbufindex = 4;
    }

    nbufindex = 0;
    bufindex[nbufindex] = statbuf;
    for (i=0; i < n; i++) {
	if (statbuf[i] == '\n' || statbuf[i] == '\0') {
	    statbuf[i] = '\0';
	    if (nbufindex + 1 >= maxbufindex) {
		size = (maxbufindex + 4) * sizeof(char *);
		if ((bufindex = (char **)realloc(bufindex, size)) == NULL)
		    return -ENOMEM;
	    	maxbufindex += 4;
	    }
	    bufindex[++nbufindex] = statbuf + i + 1;
	}
    }

    if (!started) {
	started = 1;
	memset(proc_stat, 0, sizeof(*proc_stat));

	/* hz of running kernel */
	proc_stat->hz = sysconf(_SC_CLK_TCK);

	/* scan ncpus */
	for (i=0; i < nbufindex; i++) {
	    if (strncmp("cpu", bufindex[i], 3) == 0 && isdigit((int)bufindex[i][3]))
	    	proc_stat->ncpu++;
	}
	if (proc_stat->ncpu == 0)
	    proc_stat->ncpu = 1; /* non-SMP kernel? */
	proc_stat->cpu_indom = idp;
	proc_stat->cpu_indom->it_numinst = proc_stat->ncpu;
	proc_stat->cpu_indom->it_set = (pmdaInstid *)malloc(
		proc_stat->ncpu * sizeof(pmdaInstid));
	/*
	 * Map out the CPU instance domain.
	 *
	 * The first call to cpu_name() does initialization on the
	 * proc_cpuinfo structure.
	 */
	for (i=0; i < proc_stat->ncpu; i++) {
	    proc_stat->cpu_indom->it_set[i].i_inst = i;
	    proc_stat->cpu_indom->it_set[i].i_name = cpu_name(proc_cpuinfo, i);
	}

	n = proc_stat->ncpu * sizeof(unsigned long long);
	proc_stat->p_user = (unsigned long long *)calloc(1, n);
	proc_stat->p_nice = (unsigned long long *)calloc(1, n);
	proc_stat->p_sys = (unsigned long long *)calloc(1, n);
	proc_stat->p_idle = (unsigned long long *)calloc(1, n);
	proc_stat->p_wait = (unsigned long long *)calloc(1, n);
	proc_stat->p_irq = (unsigned long long *)calloc(1, n);
	proc_stat->p_sirq = (unsigned long long *)calloc(1, n);
	proc_stat->p_steal = (unsigned long long *)calloc(1, n);
	proc_stat->p_guest = (unsigned long long *)calloc(1, n);

	n = proc_cpuinfo->node_indom->it_numinst * sizeof(unsigned long long);
	proc_stat->n_user = calloc(1, n);
	proc_stat->n_nice = calloc(1, n);
	proc_stat->n_sys = calloc(1, n);
	proc_stat->n_idle = calloc(1, n);
	proc_stat->n_wait = calloc(1, n);
	proc_stat->n_irq = calloc(1, n);
	proc_stat->n_sirq = calloc(1, n);
	proc_stat->n_steal = calloc(1, n);
	proc_stat->n_guest = calloc(1, n);
    }
    else {
	/* reset per-node stats */
	n = proc_cpuinfo->node_indom->it_numinst * sizeof(unsigned long long);
	memset(proc_stat->n_user, 0, n);
	memset(proc_stat->n_nice, 0, n);
	memset(proc_stat->n_sys, 0, n);
	memset(proc_stat->n_idle, 0, n);
	memset(proc_stat->n_wait, 0, n);
	memset(proc_stat->n_irq, 0, n);
	memset(proc_stat->n_sirq, 0, n);
	memset(proc_stat->n_steal, 0, n);
	memset(proc_stat->n_guest, 0, n);
    }
    /*
     * cpu  95379 4 20053 6502503
     * 2.6 kernels have 3 additional fields
     * for wait, irq and soft_irq.
     */
    strcpy(fmt, "cpu %llu %llu %llu %llu %llu %llu %llu %llu %llu");
    n = sscanf((const char *)bufindex[0], fmt,
	&proc_stat->user, &proc_stat->nice,
	&proc_stat->sys, &proc_stat->idle,
	&proc_stat->wait, &proc_stat->irq,
	&proc_stat->sirq, &proc_stat->steal,
	&proc_stat->guest);

    /*
     * per-cpu stats
     * e.g. cpu0 95379 4 20053 6502503
     * 2.6 kernels have 3 additional fields for wait, irq and soft_irq.
     * More recent (2008) 2.6 kernels have an extra field for guest.
     */
    if (proc_stat->ncpu == 1) {
	/*
	 * Don't bother scanning - the counters are the same
	 * as for "all" cpus, as already scanned above.
	 * This also handles the non-SMP code where
	 * there is no line starting with "cpu0".
	 */
	proc_stat->p_user[0] = proc_stat->user;
	proc_stat->p_nice[0] = proc_stat->nice;
	proc_stat->p_sys[0] = proc_stat->sys;
	proc_stat->p_idle[0] = proc_stat->idle;
	proc_stat->p_wait[0] = proc_stat->wait;
	proc_stat->p_irq[0] = proc_stat->irq;
	proc_stat->p_sirq[0] = proc_stat->sirq;
	proc_stat->p_steal[0] = proc_stat->steal;
    	proc_stat->p_guest[0] = proc_stat->guest;
    }
    else {
	strcpy(fmt, "cpu%d %llu %llu %llu %llu %llu %llu %llu %llu %llu");
	for (i=0; i < proc_stat->ncpu; i++) {
	    for (j=0; j < nbufindex; j++) {
		if (strncmp("cpu", bufindex[j], 3) == 0 && isdigit((int)bufindex[j][3])) {
		    int c;
		    int cpunum = atoi(&bufindex[j][3]);
		    int node;
		    if (cpunum >= 0 && cpunum < proc_stat->ncpu) {
			n = sscanf(bufindex[j], fmt, &c,
			    &proc_stat->p_user[cpunum],
			    &proc_stat->p_nice[cpunum],
			    &proc_stat->p_sys[cpunum],
			    &proc_stat->p_idle[cpunum],
			    &proc_stat->p_wait[cpunum],
			    &proc_stat->p_irq[cpunum],
			    &proc_stat->p_sirq[cpunum],
			    &proc_stat->p_steal[cpunum],
			    &proc_stat->p_guest[cpunum]);
			if ((node = proc_cpuinfo->cpuinfo[cpunum].node) != -1) {
			    proc_stat->n_user[node] += proc_stat->p_user[cpunum];
			    proc_stat->n_nice[node] += proc_stat->p_nice[cpunum];
			    proc_stat->n_sys[node] += proc_stat->p_sys[cpunum];
			    proc_stat->n_idle[node] += proc_stat->p_idle[cpunum];
			    proc_stat->n_wait[node] += proc_stat->p_wait[cpunum];
			    proc_stat->n_irq[node] += proc_stat->p_irq[cpunum];
			    proc_stat->n_sirq[node] += proc_stat->p_sirq[cpunum];
			    proc_stat->n_steal[node] += proc_stat->p_steal[cpunum];
			    proc_stat->n_guest[node] += proc_stat->p_guest[cpunum];
			}
		    }
		}
	    }
	    if (j == nbufindex)
		break;
	}
    }

    /*
     * page 59739 34786
     * Note: this has moved to /proc/vmstat in 2.6 kernels
     */
    strcpy(fmt, "page %u %u");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 5) == 0) {
	    sscanf((const char *)bufindex[j], fmt,
		&proc_stat->page[0], &proc_stat->page[1]);
	    break;
	}
    }

    /*
     * swap 0 1
     * Note: this has moved to /proc/vmstat in 2.6 kernels
     */
    strcpy(fmt, "swap %u %u");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 5) == 0) {
	    sscanf((const char *)bufindex[j], fmt,
		&proc_stat->swap[0], &proc_stat->swap[1]);
	    break;
	}
    }

    /*
     * intr 32845463 24099228 2049 0 2 ....
     * (just export the first number, which is total interrupts)
     */
    strcpy(fmt, "intr %llu");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 5) == 0) {
	    sscanf((const char *)bufindex[j], fmt, &proc_stat->intr);
	    break;
	}
    }

    /*
     * ctxt 1733480
     */
    strcpy(fmt, "ctxt %llu");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 5) == 0) {
	    sscanf((const char *)bufindex[j], fmt, &proc_stat->ctxt);
	    break;
	}
    }

    /*
     * btime 1733480
     */
    strcpy(fmt, "btime %lu");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 6) == 0) {
	    sscanf((const char *)bufindex[j], fmt, &proc_stat->btime);
	    break;
	}
    }

    /*
     * processes 2213
     */
    strcpy(fmt, "processes %lu");
    for (j=0; j < nbufindex; j++) {
    	if (strncmp(fmt, bufindex[j], 10) == 0) {
	    sscanf((const char *)bufindex[j], fmt, &proc_stat->processes);
	    break;
	}
    }

    /* success */
    return 0;
}