From 11fe0e76ecb63f5278fbda1ad34c6acdbce61550 Mon Sep 17 00:00:00 2001 From: "jaehoon.hyun" Date: Wed, 10 Apr 2019 18:11:21 +0900 Subject: [PATCH] bug fix : getNetworkBandwidth change logic Change-Id: I595bbcc0ce6dc80d4b71a3600ce63400e5db170d --- .../resourceservice/resourceservice.c | 104 +++++++++++------- 1 file changed, 63 insertions(+), 41 deletions(-) diff --git a/src/scoringmgr/resourceservice/resourceservice.c b/src/scoringmgr/resourceservice/resourceservice.c index 9a9db0c..1804123 100644 --- a/src/scoringmgr/resourceservice/resourceservice.c +++ b/src/scoringmgr/resourceservice/resourceservice.c @@ -12,6 +12,7 @@ #include #include #include +#include //cpu #include @@ -23,7 +24,7 @@ typedef double (*tGetResource)(const char *); typedef struct { pthread_t t; - int done; + volatile int done; } tThread; tThread cpuUsageThread; @@ -176,7 +177,7 @@ static void *daemonCpuUsage(void *obj) { gCpuUsage = goCpuUsage(); } - printf("daemonNetworkUsage done\n"); + printf("daemonCpuUsage done\n"); return NULL; } @@ -195,52 +196,59 @@ static double getNetworkMbps() static double getNetworkBandwidth() { - int sock; +#ifdef __linux__ + struct ifaddrs *ifap, *ifa; struct ifreq ifr; struct ethtool_cmd edata; - int rc; - - sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); - if (sock < 0) - { - perror("socket"); - exit(1); - } + int sock, rc; + double max_speed = 0; - strncpy(ifr.ifr_name, "enp4s0", sizeof(ifr.ifr_name)); - ifr.ifr_data = (caddr_t)&edata; + getifaddrs(&ifap); - edata.cmd = ETHTOOL_GSET; + double total = 0; + int count = 0; - rc = ioctl(sock, SIOCETHTOOL, &ifr); - if (rc < 0) + for (ifa = ifap; ifa; ifa = ifa->ifa_next) { - perror("ioctl"); - exit(1); + + if (ifa->ifa_addr->sa_family == AF_INET) + { + + sock = socket(PF_INET, SOCK_DGRAM, IPPROTO_IP); + if (sock < 0) + { + perror("socket"); + exit(1); + } + + if (!SSTRNCMP(ifa->ifa_name, "eth") || + !SSTRNCMP(ifa->ifa_name, "enp") || + !SSTRNCMP(ifa->ifa_name, "wl")) + { + strncpy(ifr.ifr_name, ifa->ifa_name, sizeof(ifr.ifr_name)); + ifr.ifr_data = (caddr_t)&edata; + + edata.cmd = ETHTOOL_GSET; + + rc = ioctl(sock, SIOCETHTOOL, &ifr); + if (rc < 0) + { + perror("ioctl"); + exit(1); + } + + total += (double)ethtool_cmd_speed(&edata); + count++; + + } + } } - // switch (ethtool_cmd_speed(&edata)) - // { - // case SPEED_10: - // printf("10Mbps\n"); - // break; - // case SPEED_100: - // printf("100Mbps\n"); - // break; - // case SPEED_1000: - // printf("1Gbps\n"); - // break; - // case SPEED_2500: - // printf("2.5Gbps\n"); - // break; - // case SPEED_10000: - // printf("10Gbps\n"); - // break; - // default: - // printf("Speed returned is %d\n", edata.speed); - // } - - return (double)ethtool_cmd_speed(&edata); + return total / count; +#else +#error "Not Support this architecture" + return 0.0; +#endif } unsigned long networkPackets(char *ifNamePath) @@ -384,6 +392,7 @@ static void *daemonDiskUsage(void *obj) while (!diskUsageThread.done) { } + printf("daemonDiskUsage done\n"); return NULL; @@ -457,6 +466,7 @@ double getResource(const char *resourceName) void startResourceService() { printf("startResourceService\n"); + pthread_create(&cpuUsageThread.t, NULL, daemonCpuUsage, NULL); pthread_create(&networkUsageThread.t, NULL, daemonNetworkUsage, NULL); pthread_create(&diskUsageThread.t, NULL, daemonDiskUsage, NULL); @@ -464,10 +474,22 @@ void startResourceService() void stopResourceService() { - printf("stopResourceService\n"); cpuUsageThread.done = 1; networkUsageThread.done = 1; diskUsageThread.done = 1; + + int status; + + //TODO : pthread_join using + // pthread_join(cpuUsageThread.t,NULL); + // pthread_join(networkUsageThread.t,NULL); + // pthread_join(diskUsageThread.t,NULL); + + pthread_detach(cpuUsageThread.t); + pthread_detach(networkUsageThread.t); + pthread_detach(diskUsageThread.t); + + printf("stopResourceService end\n"); } double -- 2.34.1