1
0
Fork 0
mirror of https://github.com/munin-monitoring/contrib.git synced 2025-07-22 14:16:00 +00:00

p/multicpu1sec-c: use posix IO instead of stdlib

This commit is contained in:
Steve Schnepp 2015-05-26 20:50:26 +00:00
parent 7d88587fb6
commit 2db160e268

View file

@ -21,23 +21,30 @@ int fail(char* msg) {
int config() { int config() {
/* Get the number of CPU */ /* Get the number of CPU */
FILE* f; int f;
if ( !(f=fopen(PROC_STAT, "r")) ) { if ( !(f=open(PROC_STAT, O_RDONLY)) ) {
return fail("cannot open " PROC_STAT); return fail("cannot open " PROC_STAT);
} }
// Starting with -1, since the first line is the "global cpu line" // Starting with -1, since the first line is the "global cpu line"
int ncpu = -1; int ncpu = -1;
while (! feof(f)) {
char buffer[1024];
if (fgets(buffer, 1024, f) == 0) {
break;
}
if (! strncmp(buffer, "cpu", 3)) ncpu ++; const int buffer_size = 64 * 1024;
char buffer[buffer_size];
// whole /proc/stat can be read in 1 syscall
if (read(f, buffer, buffer_size) <= 0) {
return fail("cannot read " PROC_STAT);
} }
fclose(f); // tokenization per-line
char* line;
char* newl = "\n";
for (line = strtok(buffer, newl); line; line = strtok(NULL, newl)) {
if (! strncmp(line, "cpu", 3)) ncpu ++;
}
close(f);
printf( printf(
"graph_title multicpu1sec\n" "graph_title multicpu1sec\n"