Skip to content
Snippets Groups Projects
Commit 8a07d862 authored by Georges Da Costa's avatar Georges Da Costa
Browse files

Improves the network sensor by using pread instead of fopen

parent 46f7e35f
Branches
Tags
No related merge requests found
...@@ -17,14 +17,14 @@ ...@@ -17,14 +17,14 @@
along with Foobar. If not, see <https://www.gnu.org/licenses/>. along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*******************************************************/ *******************************************************/
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <fcntl.h>
#include <stdio.h>
#include <string.h> #include <string.h>
#include <glob.h> #include <glob.h>
char **init_infiniband(char* infi_path) { int *init_infiniband(char* infi_path) {
if(infi_path==NULL) if(infi_path==NULL)
return NULL; return NULL;
...@@ -42,10 +42,11 @@ char **init_infiniband(char* infi_path) { ...@@ -42,10 +42,11 @@ char **init_infiniband(char* infi_path) {
"%s/port_rcv_data", "%s/port_rcv_data",
"%s/port_xmit_packets", "%s/port_xmit_packets",
"%s/port_xmit_data"}; "%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++) { for(int i=0; i<4; i++) {
sources[i] = malloc(200); sprintf(buffer, filenames[i], infi_path);
sprintf(sources[i], filenames[i], infi_path); sources[i] = open(buffer, O_RDONLY);
} }
return sources; return sources;
......
...@@ -19,4 +19,4 @@ ...@@ -19,4 +19,4 @@
*******************************************************/ *******************************************************/
char **init_infiniband(char* infi_path); int *init_infiniband(char* infi_path);
...@@ -188,7 +188,7 @@ int main(int argc, char **argv) { ...@@ -188,7 +188,7 @@ int main(int argc, char **argv) {
// Network initialization // Network initialization
char ** network_sources = NULL; int *network_sources = NULL;
if(dev != NULL) if(dev != NULL)
network_sources = init_network(dev); network_sources = init_network(dev);
long long network_values[4]={0,0,0,0}; long long network_values[4]={0,0,0,0};
......
...@@ -17,53 +17,55 @@ ...@@ -17,53 +17,55 @@
along with Foobar. If not, see <https://www.gnu.org/licenses/>. along with Foobar. If not, see <https://www.gnu.org/licenses/>.
*******************************************************/ *******************************************************/
#include <unistd.h>
#include <stdio.h> #include <fcntl.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h>
#include <string.h> #include <string.h>
char **init_network(char* dev) { int *init_network(char* dev) {
if(dev==NULL) if(dev==NULL)
return NULL; return NULL;
if(strcmp(dev,"X")==0) { if(strcmp(dev,"X")==0) {
FILE* f = fopen("/proc/net/route", "r"); int f = open("/proc/net/route", O_RDONLY);
char buffer[1000]; char buffer[1000];
fgets(buffer, 999, f); read(f, buffer, 999);
fgets(buffer, 999, f); char *start_of_dev = index(buffer, '\n')+1;
char *end_of_dev = index(buffer, '\t'); char *end_of_dev = index(start_of_dev, '\t');
*end_of_dev='\0'; *end_of_dev='\0';
dev = buffer; dev = start_of_dev;
fclose(f); close(f);
} }
char *filenames[] = {"/sys/class/net/%s/statistics/rx_packets", char *filenames[] = {"/sys/class/net/%s/statistics/rx_packets",
"/sys/class/net/%s/statistics/rx_bytes", "/sys/class/net/%s/statistics/rx_bytes",
"/sys/class/net/%s/statistics/tx_packets", "/sys/class/net/%s/statistics/tx_packets",
"/sys/class/net/%s/statistics/tx_bytes"}; "/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++) { for(int i=0; i<4; i++) {
sources[i] = malloc(200); sprintf(buffer2, filenames[i], dev);
sprintf(sources[i], filenames[i], dev); sources[i] = open(buffer2, O_RDONLY);
} }
return sources; return sources;
} }
void get_network(long long* results, char** sources) { void get_network(long long* results, int *sources) {
if(sources==NULL) if(sources==NULL)
return; return;
char buffer[128];
for(int i=0; i<4; i++){ for(int i=0; i<4; i++){
FILE* f = fopen(sources[i], "rb"); pread(sources[i], buffer, 127, 0);
fscanf(f, "%lld", &results[i]); results[i] = atoll(buffer);
fclose(f);
} }
} }
void clean_network(char **sources) { void clean_network(int *sources) {
if(sources==NULL) if(sources==NULL)
return; return;
for(int i=0;i<4;i++) for(int i=0;i<4;i++)
free(sources[i]); close(sources[i]);
free(sources); free(sources);
} }
...@@ -18,6 +18,6 @@ ...@@ -18,6 +18,6 @@
*******************************************************/ *******************************************************/
char **init_network(char* dev); int *init_network(char* dev);
void get_network(long long* results, char** sources); void get_network(long long* results, int * sources);
void clean_network(char **sources); void clean_network(int *sources);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment