From aa9ebc1be8bcf6b544ec2e8b54b611227fbf6420 Mon Sep 17 00:00:00 2001 From: Georges Da Costa <dacosta@irit.fr> Date: Mon, 27 Apr 2020 18:18:32 +0200 Subject: [PATCH] Adds the capability to detect the network device automatically --- mojitos.c | 2 +- network.c | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/mojitos.c b/mojitos.c index d8a888c..0e66a83 100644 --- a/mojitos.c +++ b/mojitos.c @@ -95,7 +95,7 @@ void usage(char** argv) { printf("-p activates performance counters\n"); printf(" perf_list is coma separated list of performance counters without space. Ex: instructions,cache_misses\n"); printf("-l lists the possible performance counters and quits\n"); - printf("-d activates network monitoring\n"); + printf("-d activates network monitoring (if network_device is X tries to detect it automatically)\n"); printf("-s activates statistics of overhead in nanoseconds\n"); printf("-u activates report of system load\n"); exit(EXIT_SUCCESS); diff --git a/network.c b/network.c index 7dfd18b..0c03cb5 100644 --- a/network.c +++ b/network.c @@ -20,10 +20,22 @@ #include <stdio.h> #include <stdlib.h> +#include <string.h> char **init_network(char* dev) { if(dev==NULL) return NULL; + + if(strcmp(dev,"X")==0) { + FILE* f = fopen("/proc/net/route", "r"); + char buffer[1000]; + fgets(buffer, 999, f); + fgets(buffer, 999, f); + char *end_of_dev = index(buffer, '\t'); + *end_of_dev='\0'; + dev = buffer; + } + char *filenames[] = {"/sys/class/net/%s/statistics/rx_packets", "/sys/class/net/%s/statistics/rx_bytes", "/sys/class/net/%s/statistics/tx_packets", -- GitLab