Révision 9ac74f8d
multicpu1sec-c: implement the "acquire" cmd
| plugins/system/multicpu1sec/multicpu1sec-c.c | ||
|---|---|---|
| 3 | 3 |
*/ |
| 4 | 4 |
#include <stdio.h> |
| 5 | 5 |
#include <string.h> |
| 6 |
#include <unistd.h> |
|
| 7 |
|
|
| 8 |
#include <time.h> |
|
| 6 | 9 |
|
| 7 | 10 |
#define PROC_STAT "/proc/stat" |
| 8 | 11 |
|
| ... | ... | |
| 35 | 38 |
printf( |
| 36 | 39 |
"graph_title multicpu1sec\n" |
| 37 | 40 |
"graph_category system::1sec\n" |
| 38 |
"graph_vlabel average cpu use %\n" |
|
| 41 |
"graph_vlabel average cpu use %%\n"
|
|
| 39 | 42 |
"graph_scale no\n" |
| 40 | 43 |
"graph_total All CPUs\n" |
| 41 | 44 |
"update_rate 1\n" |
| ... | ... | |
| 50 | 53 |
} |
| 51 | 54 |
|
| 52 | 55 |
|
| 56 |
return 0; |
|
| 57 |
} |
|
| 58 |
|
|
| 59 |
char* pid_filename = "./multicpu1sec.pid"; |
|
| 60 |
char* cache_filename = "./multicpu1sec.value"; |
|
| 61 |
|
|
| 62 |
/* Wait until the next second, and return the EPOCH */ |
|
| 63 |
time_t wait_until_next_second() {
|
|
| 64 |
struct timespec tp; |
|
| 65 |
clock_gettime(CLOCK_REALTIME, &tp); |
|
| 66 |
|
|
| 67 |
time_t current_epoch = tp.tv_sec; |
|
| 68 |
long nsec_to_sleep = 1000*1000*1000 - tp.tv_nsec; |
|
| 69 |
|
|
| 70 |
|
|
| 71 |
/* Only sleep if needed */ |
|
| 72 |
if (nsec_to_sleep > 0) {
|
|
| 73 |
tp.tv_sec = 0; |
|
| 74 |
tp.tv_nsec = nsec_to_sleep; |
|
| 75 |
nanosleep(&tp, NULL); |
|
| 76 |
} |
|
| 77 |
|
|
| 78 |
return current_epoch + 1; |
|
| 53 | 79 |
} |
| 54 | 80 |
|
| 55 | 81 |
int acquire() {
|
| 56 | 82 |
printf("acquire()\n");
|
| 83 |
|
|
| 84 |
/* write the pid */ |
|
| 85 |
FILE* pid_file = fopen(pid_filename, "w"); |
|
| 86 |
fprintf(pid_file, "%d\n", getpid()); |
|
| 87 |
fclose(pid_file); |
|
| 88 |
|
|
| 89 |
/* loop each second */ |
|
| 90 |
while (1) {
|
|
| 91 |
/* wait until next second */ |
|
| 92 |
time_t epoch = wait_until_next_second(); |
|
| 93 |
|
|
| 94 |
/* Reading /proc/stat */ |
|
| 95 |
FILE* f = fopen(PROC_STAT, "r"); |
|
| 96 |
// Read and ignore the 1rst line |
|
| 97 |
char buffer[1024]; |
|
| 98 |
fgets(buffer, 1024, f); |
|
| 99 |
|
|
| 100 |
/* open the spoolfile */ |
|
| 101 |
|
|
| 102 |
FILE* cache_file = fopen(cache_filename, "a"); |
|
| 103 |
|
|
| 104 |
while (! feof(f)) {
|
|
| 105 |
if (fgets(buffer, 1024, f) == 0) {
|
|
| 106 |
// EOF |
|
| 107 |
break; |
|
| 108 |
} |
|
| 109 |
|
|
| 110 |
// Not on CPU lines anymore |
|
| 111 |
if (strncmp(buffer, "cpu", 3)) break; |
|
| 112 |
|
|
| 113 |
char cpu_id[64]; |
|
| 114 |
long usr, nice, sys, idle, iowait, irq, softirq; |
|
| 115 |
sscanf(buffer, "%s %ld %ld %ld %ld %ld", cpu_id, &usr, &nice, &sys, &idle, &iowait, &irq, &softirq); |
|
| 116 |
|
|
| 117 |
long used = usr + nice + sys + idle + iowait + irq + softirq; |
|
| 118 |
|
|
| 119 |
fprintf(cache_file, "%s.value %ld:%ld\n", cpu_id, epoch, used); |
|
| 120 |
} |
|
| 121 |
|
|
| 122 |
fclose(cache_file); |
|
| 123 |
fclose(f); |
|
| 124 |
} |
|
| 57 | 125 |
} |
| 58 | 126 |
|
| 59 | 127 |
int fetch() {
|
| 60 | 128 |
printf("fetch()\n");
|
| 129 |
|
|
| 130 |
return 0; |
|
| 61 | 131 |
} |
| 62 | 132 |
|
| 63 | 133 |
int main(int argc, char **argv) {
|
Formats disponibles : Unified diff