Skip to content

Commit

Permalink
Merge pull request #622 from steveschnepp/multicpu1sec-c
Browse files Browse the repository at this point in the history
Multicpu1sec c optimsations
  • Loading branch information
steveschnepp committed May 30, 2015
2 parents f444e60 + 9901e00 commit 066bec8
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 28 deletions.
4 changes: 4 additions & 0 deletions plugins/system/multicpu1sec/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/multicpu1sec-c
/multicpu1sec.o
/multicpu1sec.pid
/multicpu1sec.value
81 changes: 53 additions & 28 deletions plugins/system/multicpu1sec/multicpu1sec-c.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>

#include <time.h>

Expand All @@ -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);
}

// tokenization per-line
char* line;
char* newl = "\n";
for (line = strtok(buffer, newl); line; line = strtok(NULL, newl)) {
if (! strncmp(line, "cpu", 3)) ncpu ++;
}

fclose(f);
close(f);

printf(
"graph_title multicpu1sec\n"
Expand Down Expand Up @@ -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");
/* lock */
flock(fileno(cache_file), LOCK_EX);
const int buffer_size = 64 * 1024;
char buffer[buffer_size];

while (! feof(f)) {
if (fgets(buffer, 1024, f) == 0) {
// EOF
break;
}
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(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() {
Expand Down

0 comments on commit 066bec8

Please sign in to comment.