diff --git a/plugins/system/multicpu1sec/.gitignore b/plugins/system/multicpu1sec/.gitignore new file mode 100644 index 00000000..4ff4b96f --- /dev/null +++ b/plugins/system/multicpu1sec/.gitignore @@ -0,0 +1,4 @@ +/multicpu1sec-c +/multicpu1sec.o +/multicpu1sec.pid +/multicpu1sec.value diff --git a/plugins/system/multicpu1sec/multicpu1sec-c.c b/plugins/system/multicpu1sec/multicpu1sec-c.c index b66eab79..49b4320a 100644 --- a/plugins/system/multicpu1sec/multicpu1sec-c.c +++ b/plugins/system/multicpu1sec/multicpu1sec-c.c @@ -5,6 +5,7 @@ #include #include #include +#include #include @@ -20,23 +21,30 @@ int fail(char* msg) { int config() { /* Get the number of CPU */ - FILE* f; - if ( !(f=fopen(PROC_STAT, "r")) ) { + int f; + if ( !(f=open(PROC_STAT, O_RDONLY)) ) { return fail("cannot open " PROC_STAT); } // Starting with -1, since the first line is the "global cpu line" 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( "graph_title multicpu1sec\n" @@ -104,43 +112,60 @@ int acquire() { fprintf(pid_file, "%d\n", getpid()); fclose(pid_file); + /* Reading /proc/stat */ + int f = open(PROC_STAT, O_RDONLY); + + /* open the spoolfile */ + int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY); + /* loop each second */ while (1) { /* wait until next second */ time_t epoch = wait_until_next_second(); - /* Reading /proc/stat */ - FILE* f = fopen(PROC_STAT, "r"); - // Read and ignore the 1rst line - char buffer[1024]; - fgets(buffer, 1024, f); - /* open the spoolfile */ - FILE* cache_file = fopen(cache_filename, "a"); + const int buffer_size = 64 * 1024; + char buffer[buffer_size]; + + if (lseek(f, 0, SEEK_SET) < 0) { + return fail("cannot seek " PROC_STAT); + } + + // whole /proc/stat can be read in 1 syscall + if (read(f, buffer, buffer_size) <= 0) { + return fail("cannot read " PROC_STAT); + } + + // ignore the 1rst line + char* line; + const char* newl = "\n"; + line = strtok(buffer, newl); + /* lock */ - flock(fileno(cache_file), LOCK_EX); - - while (! feof(f)) { - if (fgets(buffer, 1024, f) == 0) { - // EOF - break; - } + flock(cache_file, LOCK_EX); + for (line = strtok(NULL, newl); line; line = strtok(NULL, newl)) { // Not on CPU lines anymore - if (strncmp(buffer, "cpu", 3)) break; + if (strncmp(line, "cpu", 3)) break; char cpu_id[64]; long usr, nice, sys, idle, iowait, irq, softirq; - sscanf(buffer, "%s %ld %ld %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq); + sscanf(line, "%s %ld %ld %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq); long used = usr + nice + sys + iowait + irq + softirq; - fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used); + char out_buffer[1024]; + sprintf(out_buffer, "%s.value %ld:%ld\n", cpu_id, epoch, used); + + write(cache_file, out_buffer, strlen(out_buffer)); } - fclose(cache_file); - fclose(f); + /* unlock */ + flock(cache_file, LOCK_UN); } + + close(cache_file); + close(f); } int fetch() {