From 8a07d86223e51ce11413dbdd11fcb9c2173e815a Mon Sep 17 00:00:00 2001 From: Georges Da Costa <dacosta@irit.fr> Date: Mon, 8 Mar 2021 00:28:13 +0100 Subject: [PATCH] Improves the network sensor by using pread instead of fopen --- infiniband.c | 13 +++++++------ infiniband.h | 2 +- mojitos.c | 2 +- network.c | 38 ++++++++++++++++++++------------------ network.h | 6 +++--- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/infiniband.c b/infiniband.c index f2321dc..416be9e 100644 --- a/infiniband.c +++ b/infiniband.c @@ -17,14 +17,14 @@ along with Foobar. If not, see <https://www.gnu.org/licenses/>. *******************************************************/ - -#include <stdio.h> #include <stdlib.h> +#include <fcntl.h> +#include <stdio.h> #include <string.h> #include <glob.h> -char **init_infiniband(char* infi_path) { +int *init_infiniband(char* infi_path) { if(infi_path==NULL) return NULL; @@ -42,10 +42,11 @@ char **init_infiniband(char* infi_path) { "%s/port_rcv_data", "%s/port_xmit_packets", "%s/port_xmit_data"}; - char** sources = malloc(sizeof(char*)*4); + int* sources = malloc(sizeof(int)*4); + char buffer[1024]; for(int i=0; i<4; i++) { - sources[i] = malloc(200); - sprintf(sources[i], filenames[i], infi_path); + sprintf(buffer, filenames[i], infi_path); + sources[i] = open(buffer, O_RDONLY); } return sources; diff --git a/infiniband.h b/infiniband.h index 4677d5d..847ad4c 100644 --- a/infiniband.h +++ b/infiniband.h @@ -19,4 +19,4 @@ *******************************************************/ -char **init_infiniband(char* infi_path); +int *init_infiniband(char* infi_path); diff --git a/mojitos.c b/mojitos.c index 9d179a5..d0b175e 100644 --- a/mojitos.c +++ b/mojitos.c @@ -188,7 +188,7 @@ int main(int argc, char **argv) { // Network initialization - char ** network_sources = NULL; + int *network_sources = NULL; if(dev != NULL) network_sources = init_network(dev); long long network_values[4]={0,0,0,0}; diff --git a/network.c b/network.c index 5bab862..c30ae6a 100644 --- a/network.c +++ b/network.c @@ -17,53 +17,55 @@ along with Foobar. If not, see <https://www.gnu.org/licenses/>. *******************************************************/ - -#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> #include <stdlib.h> +#include <stdio.h> #include <string.h> -char **init_network(char* dev) { +int *init_network(char* dev) { if(dev==NULL) return NULL; if(strcmp(dev,"X")==0) { - FILE* f = fopen("/proc/net/route", "r"); + int f = open("/proc/net/route", O_RDONLY); char buffer[1000]; - fgets(buffer, 999, f); - fgets(buffer, 999, f); - char *end_of_dev = index(buffer, '\t'); + read(f, buffer, 999); + char *start_of_dev = index(buffer, '\n')+1; + char *end_of_dev = index(start_of_dev, '\t'); *end_of_dev='\0'; - dev = buffer; - fclose(f); + dev = start_of_dev; + close(f); } char *filenames[] = {"/sys/class/net/%s/statistics/rx_packets", "/sys/class/net/%s/statistics/rx_bytes", "/sys/class/net/%s/statistics/tx_packets", "/sys/class/net/%s/statistics/tx_bytes"}; - char** sources = malloc(sizeof(char*)*4); + int* sources = malloc(sizeof(int)*4); + char buffer2[256]; for(int i=0; i<4; i++) { - sources[i] = malloc(200); - sprintf(sources[i], filenames[i], dev); + sprintf(buffer2, filenames[i], dev); + sources[i] = open(buffer2, O_RDONLY); } return sources; } -void get_network(long long* results, char** sources) { +void get_network(long long* results, int *sources) { if(sources==NULL) return; + char buffer[128]; for(int i=0; i<4; i++){ - FILE* f = fopen(sources[i], "rb"); - fscanf(f, "%lld", &results[i]); - fclose(f); + pread(sources[i], buffer, 127, 0); + results[i] = atoll(buffer); } } -void clean_network(char **sources) { +void clean_network(int *sources) { if(sources==NULL) return; for(int i=0;i<4;i++) - free(sources[i]); + close(sources[i]); free(sources); } diff --git a/network.h b/network.h index cd78a70..2f2eea3 100644 --- a/network.h +++ b/network.h @@ -18,6 +18,6 @@ *******************************************************/ -char **init_network(char* dev); -void get_network(long long* results, char** sources); -void clean_network(char **sources); +int *init_network(char* dev); +void get_network(long long* results, int * sources); +void clean_network(int *sources); -- GitLab