Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added DiskIO Reporter #825

Merged
merged 12 commits into from
Sep 26, 2024
15 changes: 8 additions & 7 deletions metric_providers/cpu/energy/rapl/msr/component/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
#include <string.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <limits.h>


/* AMD Support */
Expand Down Expand Up @@ -91,10 +92,10 @@

static int open_msr(int core) {

char msr_filename[BUFSIZ];
char msr_filename[PATH_MAX];
int fd;

sprintf(msr_filename, "/dev/cpu/%d/msr", core);
snprintf(msr_filename, PATH_MAX, "/dev/cpu/%d/msr", core);
fd = open(msr_filename, O_RDONLY);
if ( fd < 0 ) {
if ( errno == ENXIO ) {
Expand Down Expand Up @@ -174,7 +175,7 @@ static int detect_cpu(void) {

int vendor=-1,family,model=-1;
char buffer[BUFSIZ],*result;
char vendor_string[BUFSIZ];
char vendor_string[1024];

fff=fopen("/proc/cpuinfo","r");
if (fff==NULL) return -1;
Expand Down Expand Up @@ -242,15 +243,15 @@ static int package_map[MAX_PACKAGES];

static int detect_packages(void) {

char filename[BUFSIZ];
char filename[PATH_MAX];
FILE *fff;
int package;
int i;

for(i=0;i<MAX_PACKAGES;i++) package_map[i]=-1;

for(i=0;i<MAX_CPUS;i++) {
sprintf(filename,"/sys/devices/system/cpu/cpu%d/topology/physical_package_id",i);
snprintf(filename, PATH_MAX, "/sys/devices/system/cpu/cpu%d/topology/physical_package_id",i);
fff=fopen(filename,"r");
if (fff==NULL) break;
fscanf(fff,"%d",&package);
Expand Down Expand Up @@ -408,13 +409,13 @@ static int check_system() {
int fd = open_msr(0);
if (fd < 0) {
fprintf(stderr, "Couldn't open MSR 0\n");
exit(127);
exit(1);
}
long long msr_data = read_msr(fd, energy_status);

if(msr_data <= 0) {
fprintf(stderr, "rapl MSR had 0 or negative values: %lld\n", msr_data);
exit(127);
exit(1);
}
close(fd);
return 0;
Expand Down
44 changes: 30 additions & 14 deletions metric_providers/cpu/time/cgroup/container/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,26 @@
#include <time.h>
#include <string.h> // for strtok
#include <getopt.h>
#include <limits.h>

#define DOCKER_CONTAINER_ID_BUFFER 65 // Docker container ID size is 64 + 1 byte for NUL termination

typedef struct container_t { // struct is a specification and this static makes no sense here
char path[BUFSIZ];
char *id;
char path[PATH_MAX];
char id[DOCKER_CONTAINER_ID_BUFFER];
} container_t;

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
// between Threads.
// in any case, none of these variables should change between threads
static int user_id = 0;
static int user_id = -1;
static long int user_hz;
static unsigned int msleep_time=1000;

static long int read_cpu_cgroup(char* filename) {
long int cpu_usage = -1;
FILE* fd = NULL;
fd = fopen(filename, "r");
FILE* fd = fopen(filename, "r");
if ( fd == NULL) {
fprintf(stderr, "Error - Could not open path for reading: %s. Maybe the container is not running anymore? Are you using --rootless mode? Errno: %d\n", filename, errno);
exit(1);
Expand All @@ -33,16 +35,14 @@ static long int read_cpu_cgroup(char* filename) {
return cpu_usage;
}

static int output_stats(container_t *containers, int length) {
static void output_stats(container_t *containers, int length) {
struct timeval now;
gettimeofday(&now, NULL);

for(int i=0; i<length; i++) {
printf("%ld%06ld %ld %s\n", now.tv_sec, now.tv_usec, read_cpu_cgroup(containers[i].path), containers[i].id);
}
usleep(msleep_time*1000);

return 1;
}

static int parse_containers(container_t** containers, char* containers_string, int rootless_mode) {
Expand All @@ -52,20 +52,32 @@ static int parse_containers(container_t** containers, char* containers_string, i
}

*containers = malloc(sizeof(container_t));
if (!containers) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
char *id = strtok(containers_string,",");
int length = 0;

for (; id != NULL; id = strtok(NULL, ",")) {
//printf("Token: %s\n", id);
length++;
*containers = realloc(*containers, length * sizeof(container_t));
(*containers)[length-1].id = id;
if (!containers) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
strncpy((*containers)[length-1].id, id, DOCKER_CONTAINER_ID_BUFFER - 1);
(*containers)[length-1].id[DOCKER_CONTAINER_ID_BUFFER - 1] = '\0';

if(rootless_mode) {
sprintf((*containers)[length-1].path,
snprintf((*containers)[length-1].path,
PATH_MAX,
"/sys/fs/cgroup/user.slice/user-%d.slice/user@%d.service/user.slice/docker-%s.scope/cpu.stat",
user_id, user_id, id);
} else {
sprintf((*containers)[length-1].path,
snprintf((*containers)[length-1].path,
PATH_MAX,
"/sys/fs/cgroup/system.slice/docker-%s.scope/cpu.stat",
id);
}
Expand All @@ -87,12 +99,11 @@ static int check_system(int rootless_mode) {
check_path = "/sys/fs/cgroup/system.slice/cpu.stat";
}

FILE* fd = NULL;
fd = fopen(check_path, "r");
FILE* fd = fopen(check_path, "r");

if (fd == NULL) {
fprintf(stderr, "Couldn't open cpu.stat file at %s\n", check_path);
exit(127);
exit(1);
}
fclose(fd);
return 0;
Expand Down Expand Up @@ -147,7 +158,12 @@ int main(int argc, char **argv) {
break;
case 's':
containers_string = (char *)malloc(strlen(optarg) + 1); // Allocate memory
if (!containers_string) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
strncpy(containers_string, optarg, strlen(optarg));
containers_string[strlen(optarg)] = '\0'; // Ensure NUL termination if max length
break;
case 'c':
check_system_flag = 1;
Expand Down
12 changes: 4 additions & 8 deletions metric_providers/cpu/time/cgroup/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,28 @@ static unsigned int msleep_time=1000;
static long int read_cpu_cgroup() {

long int cpu_usage = -1;
FILE* fd = NULL;
fd = fopen("/sys/fs/cgroup/cpu.stat", "r"); // check for general readability only once
FILE* fd = fopen("/sys/fs/cgroup/cpu.stat", "r"); // check for general readability only once
fscanf(fd, "usage_usec %ld", &cpu_usage);
fclose(fd);
return cpu_usage;
}

static int output_stats() {
static void output_stats() {

struct timeval now;
gettimeofday(&now, NULL);

printf("%ld%06ld %ld\n", now.tv_sec, now.tv_usec, read_cpu_cgroup());
usleep(msleep_time*1000);

return 1;
}

static int check_system() {
const char check_path[] = "/sys/fs/cgroup/cpu.stat";
FILE* fd = NULL;
fd = fopen(check_path, "r");
FILE* fd = fopen(check_path, "r");

if (fd == NULL) {
fprintf(stderr, "Couldn't open cpu.stat file at %s\n", check_path);
exit(127);
exit(1);
}
fclose(fd);
return 0;
Expand Down
15 changes: 4 additions & 11 deletions metric_providers/cpu/time/procfs/system/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ static long int user_hz;
static unsigned int msleep_time=1000;

static long int read_cpu_proc() {
FILE* fd = NULL;
long int user_time, nice_time, system_time, idle_time, iowait_time, irq_time, softirq_time, steal_time;

fd = fopen("/proc/stat", "r");
FILE* fd = fopen("/proc/stat", "r");

fscanf(fd, "cpu %ld %ld %ld %ld %ld %ld %ld %ld", &user_time, &nice_time, &system_time, &idle_time, &iowait_time, &irq_time, &softirq_time, &steal_time);

Expand All @@ -34,28 +32,23 @@ static long int read_cpu_proc() {
return ((user_time+nice_time+system_time+idle_time+iowait_time+irq_time+softirq_time+steal_time)*1000000)/user_hz;
}



static int output_stats() {
static void output_stats() {

struct timeval now;

gettimeofday(&now, NULL);
printf("%ld%06ld %ld\n", now.tv_sec, now.tv_usec, read_cpu_proc());
usleep(msleep_time*1000);

return 1;
}

static int check_system() {
const char check_path[] = "/proc/stat";

FILE* fd = NULL;
fd = fopen(check_path, "r");
FILE* fd = fopen(check_path, "r");

if (fd == NULL) {
fprintf(stderr, "Couldn't open /proc/stat file\n");
exit(127);
exit(1);
}
fclose(fd);
return 0;
Expand Down
46 changes: 30 additions & 16 deletions metric_providers/cpu/utilization/cgroup/container/source.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@
#include <time.h>
#include <string.h> // for strtok
#include <getopt.h>
#include <limits.h>

#define DOCKER_CONTAINER_ID_BUFFER 65 // Docker container ID size is 64 + 1 byte for NUL termination

typedef struct container_t { // struct is a specification and this static makes no sense here
char path[BUFSIZ];
char *id;
char path[PATH_MAX];
char id[DOCKER_CONTAINER_ID_BUFFER];
} container_t;

// All variables are made static, because we believe that this will
// keep them local in scope to the file and not make them persist in state
// between Threads.
// in any case, none of these variables should change between threads
static int user_id = 0;
static int user_id = -1;
static long int user_hz;
static unsigned int msleep_time=1000;

Expand All @@ -41,10 +44,8 @@ static long int read_cpu_cgroup(FILE *fd) {
}

static long int get_cpu_stat(char* filename, int mode) {
FILE* fd = NULL;
long int result=-1;

fd = fopen(filename, "r");
FILE* fd = fopen(filename, "r");

if ( fd == NULL) {
fprintf(stderr, "Error - Could not open path for reading: %s. Maybe the container is not running anymore? Are you using --rootless mode? Errno: %d\n", filename, errno);
Expand All @@ -62,7 +63,7 @@ static long int get_cpu_stat(char* filename, int mode) {
}


static int output_stats(container_t* containers, int length) {
static void output_stats(container_t* containers, int length) {

long int main_cpu_reading_before, main_cpu_reading_after, main_cpu_reading;
long int cpu_readings_before[length];
Expand All @@ -72,7 +73,6 @@ static int output_stats(container_t* containers, int length) {
struct timeval now;
int i;


// Get Energy Readings, set timestamp mark
gettimeofday(&now, NULL);
for(i=0; i<length; i++) {
Expand Down Expand Up @@ -116,7 +116,6 @@ static int output_stats(container_t* containers, int length) {

printf("%ld%06ld %ld %s\n", now.tv_sec, now.tv_usec, reading, containers[i].id);
}
return 1;
}

static int parse_containers(container_t** containers, char* containers_string, int rootless_mode) {
Expand All @@ -126,20 +125,32 @@ static int parse_containers(container_t** containers, char* containers_string, i
}

*containers = malloc(sizeof(container_t));
if (!containers) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
char *id = strtok(containers_string,",");
int length = 0;

for (; id != NULL; id = strtok(NULL, ",")) {
//printf("Token: %s\n", id);
length++;
*containers = realloc(*containers, length * sizeof(container_t));
(*containers)[length-1].id = id;
if (!containers) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
strncpy((*containers)[length-1].id, id, DOCKER_CONTAINER_ID_BUFFER - 1);
(*containers)[length-1].id[DOCKER_CONTAINER_ID_BUFFER - 1] = '\0';

if(rootless_mode) {
sprintf((*containers)[length-1].path,
snprintf((*containers)[length-1].path,
PATH_MAX,
"/sys/fs/cgroup/user.slice/user-%d.slice/user@%d.service/user.slice/docker-%s.scope/cpu.stat",
user_id, user_id, id);
} else {
sprintf((*containers)[length-1].path,
snprintf((*containers)[length-1].path,
PATH_MAX,
"/sys/fs/cgroup/system.slice/docker-%s.scope/cpu.stat",
id);
}
Expand All @@ -164,9 +175,7 @@ static int check_system(int rootless_mode) {
}
file_path_proc_stat = "/proc/stat";

FILE* fd = NULL;

fd = fopen(file_path_cpu_stat, "r");
FILE* fd = fopen(file_path_cpu_stat, "r");
if (fd == NULL) {
fprintf(stderr, "Couldn't open cpu.stat file at %s\n", file_path_cpu_stat);
found_error = 1;
Expand All @@ -183,7 +192,7 @@ static int check_system(int rootless_mode) {
}

if(found_error) {
exit(127);
exit(1);
}

return 0;
Expand Down Expand Up @@ -238,7 +247,12 @@ int main(int argc, char **argv) {
break;
case 's':
containers_string = (char *)malloc(strlen(optarg) + 1); // Allocate memory
if (!containers_string) {
fprintf(stderr, "Could not allocate memory for containers string\n");
exit(1);
}
strncpy(containers_string, optarg, strlen(optarg));
containers_string[strlen(optarg)] = '\0'; // Ensure NUL termination if max length
break;
case 'c':
check_system_flag = 1;
Expand Down
Loading
Loading