src/hotspot/os/linux/os_perf_linux.cpp
changeset 50879 d90c3cbf13df
parent 50160 dc18db671651
child 51106 f605c91e5219
child 51360 d2c720caa480
equal deleted inserted replaced
50878:fb7800b66c92 50879:d90c3cbf13df
    42 #include <dirent.h>
    42 #include <dirent.h>
    43 #include <stdlib.h>
    43 #include <stdlib.h>
    44 #include <dlfcn.h>
    44 #include <dlfcn.h>
    45 #include <pthread.h>
    45 #include <pthread.h>
    46 #include <limits.h>
    46 #include <limits.h>
       
    47 #include <ifaddrs.h>
       
    48 #include <fcntl.h>
    47 
    49 
    48 /**
    50 /**
    49    /proc/[number]/stat
    51    /proc/[number]/stat
    50               Status information about the process.  This is used by ps(1).  It is defined in /usr/src/linux/fs/proc/array.c.
    52               Status information about the process.  This is used by ps(1).  It is defined in /usr/src/linux/fs/proc/array.c.
    51 
    53 
  1046   }
  1048   }
  1047 
  1049 
  1048   cpu_info = *_cpu_info; // shallow copy assignment
  1050   cpu_info = *_cpu_info; // shallow copy assignment
  1049   return OS_OK;
  1051   return OS_OK;
  1050 }
  1052 }
       
  1053 
       
  1054 class NetworkPerformanceInterface::NetworkPerformance : public CHeapObj<mtInternal> {
       
  1055   friend class NetworkPerformanceInterface;
       
  1056  private:
       
  1057   NetworkPerformance();
       
  1058   NetworkPerformance(const NetworkPerformance& rhs); // no impl
       
  1059   NetworkPerformance& operator=(const NetworkPerformance& rhs); // no impl
       
  1060   bool initialize();
       
  1061   ~NetworkPerformance();
       
  1062   int64_t read_counter(const char* iface, const char* counter) const;
       
  1063   int network_utilization(NetworkInterface** network_interfaces) const;
       
  1064 };
       
  1065 
       
  1066 NetworkPerformanceInterface::NetworkPerformance::NetworkPerformance() {
       
  1067 
       
  1068 }
       
  1069 
       
  1070 bool NetworkPerformanceInterface::NetworkPerformance::initialize() {
       
  1071   return true;
       
  1072 }
       
  1073 
       
  1074 NetworkPerformanceInterface::NetworkPerformance::~NetworkPerformance() {
       
  1075 }
       
  1076 
       
  1077 int64_t NetworkPerformanceInterface::NetworkPerformance::read_counter(const char* iface, const char* counter) const {
       
  1078   char buf[128];
       
  1079 
       
  1080   snprintf(buf, sizeof(buf), "/sys/class/net/%s/statistics/%s", iface, counter);
       
  1081 
       
  1082   int fd = open(buf, O_RDONLY);
       
  1083   if (fd == -1) {
       
  1084     return -1;
       
  1085   }
       
  1086 
       
  1087   ssize_t num_bytes = read(fd, buf, sizeof(buf));
       
  1088   close(fd);
       
  1089   if ((num_bytes == -1) || (num_bytes >= static_cast<ssize_t>(sizeof(buf))) || (num_bytes < 1)) {
       
  1090     return -1;
       
  1091   }
       
  1092 
       
  1093   buf[num_bytes] = '\0';
       
  1094   int64_t value = strtoll(buf, NULL, 10);
       
  1095 
       
  1096   return value;
       
  1097 }
       
  1098 
       
  1099 int NetworkPerformanceInterface::NetworkPerformance::network_utilization(NetworkInterface** network_interfaces) const
       
  1100 {
       
  1101   ifaddrs* addresses;
       
  1102   ifaddrs* cur_address;
       
  1103 
       
  1104   if (getifaddrs(&addresses) != 0) {
       
  1105     return OS_ERR;
       
  1106   }
       
  1107 
       
  1108   NetworkInterface* ret = NULL;
       
  1109   for (cur_address = addresses; cur_address != NULL; cur_address = cur_address->ifa_next) {
       
  1110     if (cur_address->ifa_addr->sa_family != AF_PACKET) {
       
  1111       continue;
       
  1112     }
       
  1113 
       
  1114     int64_t bytes_in = read_counter(cur_address->ifa_name, "rx_bytes");
       
  1115     int64_t bytes_out = read_counter(cur_address->ifa_name, "tx_bytes");
       
  1116 
       
  1117     NetworkInterface* cur = new NetworkInterface(cur_address->ifa_name, bytes_in, bytes_out, ret);
       
  1118     ret = cur;
       
  1119   }
       
  1120 
       
  1121   *network_interfaces = ret;
       
  1122 
       
  1123   return OS_OK;
       
  1124 }
       
  1125 
       
  1126 NetworkPerformanceInterface::NetworkPerformanceInterface() {
       
  1127   _impl = NULL;
       
  1128 }
       
  1129 
       
  1130 NetworkPerformanceInterface::~NetworkPerformanceInterface() {
       
  1131   if (_impl != NULL) {
       
  1132     delete _impl;
       
  1133   }
       
  1134 }
       
  1135 
       
  1136 bool NetworkPerformanceInterface::initialize() {
       
  1137   _impl = new NetworkPerformanceInterface::NetworkPerformance();
       
  1138   return _impl != NULL && _impl->initialize();
       
  1139 }
       
  1140 
       
  1141 int NetworkPerformanceInterface::network_utilization(NetworkInterface** network_interfaces) const {
       
  1142   return _impl->network_utilization(network_interfaces);
       
  1143 }