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: keep the files open

This commit is contained in:
Steve Schnepp 2015-05-26 20:56:57 +00:00
parent 2db160e268
commit 9901e00140

View file

@ -112,17 +112,25 @@ int acquire() {
fprintf(pid_file, "%d\n", getpid()); fprintf(pid_file, "%d\n", getpid());
fclose(pid_file); 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 */ /* loop each second */
while (1) { while (1) {
/* wait until next second */ /* wait until next second */
time_t epoch = wait_until_next_second(); time_t epoch = wait_until_next_second();
/* Reading /proc/stat */
int f = open(PROC_STAT, O_RDONLY);
const int buffer_size = 64 * 1024; const int buffer_size = 64 * 1024;
char buffer[buffer_size]; 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 // whole /proc/stat can be read in 1 syscall
if (read(f, buffer, buffer_size) <= 0) { if (read(f, buffer, buffer_size) <= 0) {
return fail("cannot read " PROC_STAT); return fail("cannot read " PROC_STAT);
@ -133,9 +141,6 @@ int acquire() {
const char* newl = "\n"; const char* newl = "\n";
line = strtok(buffer, newl); line = strtok(buffer, newl);
/* open the spoolfile */
int cache_file = open(cache_filename, O_CREAT | O_APPEND | O_WRONLY);
/* lock */ /* lock */
flock(cache_file, LOCK_EX); flock(cache_file, LOCK_EX);
@ -155,10 +160,13 @@ int acquire() {
write(cache_file, out_buffer, strlen(out_buffer)); write(cache_file, out_buffer, strlen(out_buffer));
} }
/* unlock */
flock(cache_file, LOCK_UN);
}
close(cache_file); close(cache_file);
close(f); close(f);
} }
}
int fetch() { int fetch() {
FILE* cache_file = fopen(cache_filename, "r+"); FILE* cache_file = fopen(cache_filename, "r+");