summaryrefslogtreecommitdiff
path: root/agent/mibgroup/hardware/cpu/cpu_linux.c
blob: 8a081cd09f21cd94284c063b535efdfe422ad32e (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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <net-snmp/agent/hardware/cpu.h>

#include <unistd.h>
#include <fcntl.h>

#define CPU_FILE    "/proc/cpuinfo"
#define STAT_FILE   "/proc/stat"
#define VMSTAT_FILE "/proc/vmstat"


    /* Which field(s) describe the type of CPU */
#if defined(__i386__) || defined(__x86_64__)
#define DESCR_FIELD  "vendor_id"
#define DESCR2_FIELD "model name"
#endif
#if defined(__powerpc__) || defined(__powerpc64__)
#define DESCR_FIELD  "cpu\t"
#endif
#if defined(__ia64__)
	/* since vendor is always Intel ... we don't parse vendor */
#define DESCR_FIELD  "family"
#endif


    /*
     * Initialise the list of CPUs on the system
     *   (including descriptions)
     *
     * XXX - Assumes x86-style /proc/cpuinfo format
     *       See CPUinfo database at
     *           http://www.rush3d.com/gcc/
     *                for info on alternative styles
     */
void init_cpu_linux( void ) {
    FILE *fp;
    char buf[1024], *cp;
    int  i, n = 0;
    netsnmp_cpu_info *cpu = netsnmp_cpu_get_byIdx( -1, 1 );
    strcpy(cpu->name, "Overall CPU statistics");

    fp = fopen( CPU_FILE, "r" );
    if (!fp) {
        snmp_log(LOG_ERR, "Can't open procinfo file %s\n", CPU_FILE);
        return;
    }
    while ( fgets( buf, sizeof(buf), fp)) {
        if ( sscanf( buf, "processor : %d", &i ) == 1)  {
            n++;
            cpu = netsnmp_cpu_get_byIdx( i, 1 );
            cpu->status = 2;  /* running */
            sprintf( cpu->name, "cpu%d", i );
#if defined(__s390__) || defined(__s390x__)
            strcat( cpu->descr, "An S/390 CPU" );
#endif
        }
#if defined(__s390__) || defined(__s390x__)
	/* s390 may have different format of CPU_FILE */
        else {
            if (sscanf( buf, "processor %d:", &i ) == 1)  {
                n++;
                cpu = netsnmp_cpu_get_byIdx( i, 1 );
                cpu->status = 2;  /* running */
                sprintf( cpu->name, "cpu%d", i );
                strcat( cpu->descr, "An S/390 CPU" );
            }
        }
#endif

#ifdef DESCR_FIELD
        if (!strncmp( buf, DESCR_FIELD, strlen(DESCR_FIELD))) {
            cp = strchr( buf, ':' );
            strcpy( cpu->descr, cp+2 );
            cp = strchr( cpu->descr, '\n' );
            *cp = 0;
        }
#endif
#ifdef DESCR2_FIELD
        if (!strncmp( buf, DESCR2_FIELD, strlen(DESCR2_FIELD))) {
            cp = strchr( buf, ':' );
            strcat( cpu->descr, cp );
            cp = strchr( cpu->descr, '\n' );
            *cp = 0;
        }
#endif
    }
    fclose(fp);
    cpu_num = n;
}

void _cpu_load_swap_etc( char *buff, netsnmp_cpu_info *cpu );

    /*
     * Load the latest CPU usage statistics
     */
int netsnmp_cpu_arch_load( netsnmp_cache *cache, void *magic ) {
    static char *buff  = NULL;
    static int   bsize = 0;
    static int   first = 1;
    static int   num_cpuline_elem = 0;
    int          bytes_read, statfd, i;
    char        *b1, *b2;
    unsigned long long cusell = 0, cicell = 0, csysll = 0, cidell = 0,
                       ciowll = 0, cirqll = 0, csoftll = 0, cstealll = 0,
                       cguestll = 0, cguest_nicell = 0;
    netsnmp_cpu_info* cpu;

    if ((statfd = open(STAT_FILE, O_RDONLY, 0)) == -1) {
        snmp_log_perror(STAT_FILE);
        return -1;
    }
    if (bsize == 0) {
        bsize = getpagesize()-1;
        buff = (char*)malloc(bsize+1);
    }
    while ((bytes_read = read(statfd, buff, bsize)) == bsize) {
        bsize += BUFSIZ;
        buff = (char*)realloc(buff, bsize+1);
        DEBUGMSGTL(("cpu", "/proc/stat buffer increased to %d\n", bsize));
        close(statfd);
        statfd = open(STAT_FILE, O_RDONLY, 0);
        if (statfd == -1) {
            snmp_log_perror(STAT_FILE);
            return -1;
	}
    }
    close(statfd);

    if ( bytes_read < 0 ) {
        snmp_log_perror(STAT_FILE "read error");
        return -1;
    }
    buff[bytes_read] = '\0';

        /*
         * CPU statistics (overall and per-CPU)
         */
    b1 = buff;
    while ((b2 = strstr( b1, "cpu" ))) {
        if (b2[3] == ' ') {
            cpu = netsnmp_cpu_get_byIdx( -1, 0 );
            if (!cpu) {
                snmp_log_perror("No (overall) CPU info entry");
                return -1;
            }
            b1 = b2+4; /* Skip "cpu " */
        } else {
            sscanf( b2, "cpu%d", &i );
                       /* Create on the fly to support non-x86 systems - see init */
            cpu = netsnmp_cpu_get_byIdx( i, 1 );
            if (!cpu) {
                snmp_log_perror("Missing CPU info entry");
                break;
            }
            b1 = b2+5; /* Skip "cpuN " */
        }

        num_cpuline_elem = sscanf(b1, "%llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
         &cusell, &cicell, &csysll, &cidell, &ciowll, &cirqll, &csoftll, &cstealll, &cguestll, &cguest_nicell);
        DEBUGMSGTL(("cpu", "/proc/stat cpu line number of elements: %i\n", num_cpuline_elem));

        /* kernel 2.6.33 and above */
        if (num_cpuline_elem == 10) {
            cpu->guestnice_ticks = (unsigned long long)cguest_nicell;
        }
        /* kernel 2.6.24 and above */
        if (num_cpuline_elem >= 9) {
            cpu->guest_ticks = (unsigned long long)cguestll;
        }
        /* kernel 2.6.11 and above */
        if (num_cpuline_elem >= 8) {
            cpu->steal_ticks = (unsigned long long)cstealll;
        }
        /* kernel 2.6 */
        if (num_cpuline_elem >= 5) {
            cpu->wait_ticks   = (unsigned long long)ciowll;
            cpu->intrpt_ticks = (unsigned long long)cirqll;
            cpu->sirq_ticks   = (unsigned long long)csoftll;
        }
        /* rest */
        cpu->user_ticks = (unsigned long long)cusell;
        cpu->nice_ticks = (unsigned long long)cicell;
        cpu->sys_ticks  = (unsigned long long)csysll;
        cpu->idle_ticks = (unsigned long long)cidell;
    }
    if ( b1 == buff ) {
	if (first)
	    snmp_log(LOG_ERR, "No cpu line in %s\n", STAT_FILE);
    }

        /*
         * Interrupt/Context Switch statistics
         *   XXX - Do these really belong here ?
         */
    cpu = netsnmp_cpu_get_byIdx( -1, 0 );
    _cpu_load_swap_etc( buff, cpu );

    /*
     * XXX - TODO: extract per-CPU statistics
     *    (Into separate netsnmp_cpu_info data structures)
     */

    first = 0;
    return 0;
}


        /*
         * Interrupt/Context Switch statistics
         *   XXX - Do these really belong here ?
         */
void _cpu_load_swap_etc( char *buff, netsnmp_cpu_info *cpu ) {
    static int   has_vmstat = 1;
    static char *vmbuff  = NULL;
    static int   vmbsize = 0;
    static int   first   = 1;
    int          bytes_read, vmstatfd;
    char        *b;
    unsigned long long pin, pout, swpin, swpout;
    unsigned long long itot, iticks, ctx;

    if (has_vmstat) {
      vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0);
      if (vmstatfd == -1 ) {
            snmp_log(LOG_ERR, "cannot open %s\n", VMSTAT_FILE);
            has_vmstat = 0;
      } else {
        if (vmbsize == 0) {
	    vmbsize = getpagesize()-1;
	    vmbuff = (char*)malloc(vmbsize+1);
        }
        while ((bytes_read = read(vmstatfd, vmbuff, vmbsize)) == vmbsize) {
	    vmbsize += BUFSIZ;
	    vmbuff = (char*)realloc(vmbuff, vmbsize+1);
	    close(vmstatfd);
	    vmstatfd = open(VMSTAT_FILE, O_RDONLY, 0);
	    if (vmstatfd == -1) {
                snmp_log_perror("cannot open " VMSTAT_FILE);
                return;
	    }
        }
        close(vmstatfd);
        if ( bytes_read < 0 ) {
            snmp_log_perror(VMSTAT_FILE "read error");
            return;
        }
        vmbuff[bytes_read] = '\0';
      }
    }

    if (has_vmstat) {
	b = strstr(vmbuff, "pgpgin ");
	if (b) {
	    sscanf(b, "pgpgin %llu", &pin);
            cpu->pageIn  = (unsigned long long)pin*2;  /* ??? */
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No pgpgin line in %s\n", VMSTAT_FILE);
            cpu->pageIn  = 0;
	}
	b = strstr(vmbuff, "pgpgout ");
	if (b) {
	    sscanf(b, "pgpgout %llu", &pout);
            cpu->pageOut = (unsigned long long)pout*2;  /* ??? */
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No pgpgout line in %s\n", VMSTAT_FILE);
            cpu->pageOut = 0;
	}
	b = strstr(vmbuff, "pswpin ");
	if (b) {
	    sscanf(b, "pswpin %llu", &swpin);
            cpu->swapIn  = (unsigned long long)swpin;
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No pswpin line in %s\n", VMSTAT_FILE);
            cpu->swapIn  = 0;
	}
	b = strstr(vmbuff, "pswpout ");
	if (b) {
	    sscanf(b, "pswpout %llu", &swpout);
            cpu->swapOut = (unsigned long long)swpout;
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No pswpout line in %s\n", VMSTAT_FILE);
            cpu->swapOut = 0;
	}
    }
    else {
	b = strstr(buff, "page ");
	if (b) {
	    sscanf(b, "page %llu %llu", &pin, &pout);
            cpu->pageIn  = (unsigned long long)pin;
            cpu->pageOut = (unsigned long long)pout;
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No page line in %s\n", STAT_FILE);
            cpu->pageIn  = cpu->pageOut = 0;
	}
	b = strstr(buff, "swap ");
	if (b) {
	    sscanf(b, "swap %llu %llu", &swpin, &swpout);
            cpu->swapIn  = (unsigned long long)swpin;
            cpu->swapOut = (unsigned long long)swpout;
	} else {
	    if (first)
		snmp_log(LOG_ERR, "No swap line in %s\n", STAT_FILE);
            cpu->swapIn  = cpu->swapOut = 0;
	}
    }

    b = strstr(buff, "intr ");
    if (b) {
	sscanf(b, "intr %llu %llu", &itot, &iticks);
        cpu->nInterrupts = (unsigned long long)itot;
        /* iticks not used? */
    } else {
	if (first)
	    snmp_log(LOG_ERR, "No intr line in %s\n", STAT_FILE);
    }
    b = strstr(buff, "ctxt ");
    if (b) {
	sscanf(b, "ctxt %llu", &ctx);
        cpu->nCtxSwitches = (unsigned long long)ctx;
    } else {
	if (first)
	    snmp_log(LOG_ERR, "No ctxt line in %s\n", STAT_FILE);
    }
    first = 0;
}