diff --git a/src/cpucounters.cpp b/src/cpucounters.cpp index b45b5e78..859bee6d 100644 --- a/src/cpucounters.cpp +++ b/src/cpucounters.cpp @@ -1495,15 +1495,40 @@ bool PCM::discoverSystemTopology() std::cerr << topology[i].socket_id << " " << topology[i].os_id << " " << topology[i].core_id << "\n"; } #endif - if (threads_per_core == 0) + assert(threads_per_core == 0); // make sure it is not initialized yet + // copy the topology array to a temp object + auto sortedTopology = topology; + std::sort(sortedTopology.begin(), sortedTopology.end()); + // find out max number of threads per core given that the topologyCopy is sorted now + assert(sortedTopology.size() > 0); + auto currentCore = sortedTopology.begin(); + while (currentCore != sortedTopology.end()) + { + if (currentCore->os_id == -1 || currentCore->core_id == -1 || currentCore->socket_id == -1) // offlined core + { + ++currentCore; + continue; + } + break; + } + assert(currentCore != sortedTopology.end()); + int current_threads_per_core = 0; + for (auto firstCore = *currentCore; currentCore != sortedTopology.end(); ++currentCore) { - for (int i = 0; i < (int)num_cores; ++i) + DBG(3, "Examining core: os_id=", currentCore->os_id, ", core_id=", currentCore->core_id, ", socket_id=", currentCore->socket_id); + if (currentCore->isSameCore(firstCore)) + { + ++current_threads_per_core; + } + else { - if (topology[i].isSameCore( topology[0] )) - ++threads_per_core; + threads_per_core = (std::max)(threads_per_core, current_threads_per_core); + firstCore = *currentCore; + current_threads_per_core = 1; } - assert(threads_per_core != 0); } + threads_per_core = (std::max)(threads_per_core, current_threads_per_core); + assert(threads_per_core != 0); if(num_phys_cores_per_socket == 0 && num_cores == num_online_cores) num_phys_cores_per_socket = num_cores / num_sockets / threads_per_core; if(num_online_cores == 0) num_online_cores = num_cores; diff --git a/src/pcm-sensor-server.cpp b/src/pcm-sensor-server.cpp index febbf2a1..b060d16b 100644 --- a/src/pcm-sensor-server.cpp +++ b/src/pcm-sensor-server.cpp @@ -1467,7 +1467,6 @@ class Server { int retval = 0; if (useIPv4) { -#ifdef _WIN32 // Use IPv4 struct sockaddr_in serv4; memset(&serv4, 0, sizeof(serv4)); @@ -1479,13 +1478,16 @@ class Server { if ( 1 != ::inet_pton( AF_INET, listenIP_.c_str(), &(serv4.sin_addr) ) ) { DBG( 3, "close clientsocketFD" ); +#ifdef _WIN32 + closesocket(sockfd); +#else ::close(sockfd); +#endif throw std::runtime_error(std::string("Server Constructor: Cannot convert IP string ") + listenIP_ + " to IPv4 address"); } } socklen_t len = sizeof( struct sockaddr_in ); retval = ::bind( sockfd, reinterpret_cast(&serv4), len ); -#endif } else { // Use IPv6 struct sockaddr_in6 serv; diff --git a/src/topologyentry.h b/src/topologyentry.h index 1e7094b9..4c94d4ca 100644 --- a/src/topologyentry.h +++ b/src/topologyentry.h @@ -78,24 +78,40 @@ struct PCM_API TopologyEntry // describes a core } return "unknown"; } - bool isSameSocket( TopologyEntry& te ) { + bool isSameSocket( TopologyEntry& te ) const { return this->socket_id == te.socket_id; } - bool isSameDieGroup( TopologyEntry& te ) { + bool isSameDieGroup( TopologyEntry& te ) const { return this->die_grp_id == te.die_grp_id && isSameSocket(te); } - bool isSameDie( TopologyEntry& te ) { + bool isSameDie( TopologyEntry& te ) const { return this->die_id == te.die_id && isSameDieGroup(te); } - bool isSameTile( TopologyEntry& te ) { + bool isSameTile( TopologyEntry& te ) const { return this->tile_id == te.tile_id && isSameDie(te); } - bool isSameModule( TopologyEntry& te ) { + bool isSameModule( TopologyEntry& te ) const { return this->module_id == te.module_id && isSameTile (te); } - bool isSameCore( TopologyEntry& te ) { + bool isSameCore( TopologyEntry& te ) const { return this->core_id == te.core_id && isSameModule(te); } + bool operator <(const TopologyEntry& other) const + { + if (socket_id != other.socket_id) + return socket_id < other.socket_id; + if (die_grp_id != other.die_grp_id) + return die_grp_id < other.die_grp_id; + if (die_id != other.die_id) + return die_id < other.die_id; + if (tile_id != other.tile_id) + return tile_id < other.tile_id; + if (module_id != other.module_id) + return module_id < other.module_id; + if (core_id != other.core_id) + return core_id < other.core_id; + return thread_id < other.thread_id; + } }; inline void fillEntry(TopologyEntry & entry, const uint32 & smtMaskWidth, const uint32 & coreMaskWidth, const uint32 & l2CacheMaskShift, const int apic_id)