Skip to content

Commit 315ae84

Browse files
authored
Merge pull request #356 from opcm/push-2021-12-16
Push 2021 12 16
2 parents 1ecb288 + 1c7b5e2 commit 315ae84

24 files changed

+585
-217
lines changed

c_example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ int main(int argc, const char *argv[])
7878

7979
if(PCM.pcm_c_init == NULL || PCM.pcm_c_start == NULL || PCM.pcm_c_stop == NULL ||
8080
PCM.pcm_c_get_cycles == NULL || PCM.pcm_c_get_instr == NULL ||
81-
PCM.pcm_c_build_core_event == NULL)
81+
PCM.pcm_c_build_core_event == NULL || PCM.pcm_c_get_core_event == NULL)
8282
return -1;
8383
switch(argc-1)
8484
{

cpuasynchcounter.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class AsynchronCounterState {
8080
~AsynchronCounterState()
8181
{
8282
pthread_cancel(UpdateThread);
83-
pthread_mutex_destroy(&CounterMutex);
83+
if (pthread_mutex_destroy(&CounterMutex) != 0) std::cerr << "pthread_mutex_destroy failed\n";
8484
m->cleanup();
8585
delete[] cstates1;
8686
delete[] cstates2;
@@ -190,7 +190,7 @@ void * UpdateCounters(void * state)
190190
AsynchronCounterState * s = (AsynchronCounterState *)state;
191191

192192
while (true) {
193-
pthread_mutex_lock(&(s->CounterMutex));
193+
if (pthread_mutex_lock(&(s->CounterMutex)) != 0) std::cerr << "pthread_mutex_lock failed\n";
194194
for (uint32 core = 0; core < s->m->getNumCores(); ++core) {
195195
s->cstates1[core] = std::move(s->cstates2[core]);
196196
s->cstates2[core] = s->m->getCoreCounterState(core);
@@ -204,7 +204,7 @@ void * UpdateCounters(void * state)
204204
s->sstate1 = std::move(s->sstate2);
205205
s->sstate2 = s->m->getSystemCounterState();
206206

207-
pthread_mutex_unlock(&(s->CounterMutex));
207+
if (pthread_mutex_unlock(&(s->CounterMutex)) != 0) std::cerr << "pthread_mutex_unlock failed\n";
208208
sleep(1);
209209
}
210210
return NULL;

cpucounters.cpp

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ bool PCM::initWinRing0Lib()
112112

113113
if (result == FALSE)
114114
{
115-
CloseHandle(hOpenLibSys);
115+
DeinitOpenLibSys(&hOpenLibSys);
116116
hOpenLibSys = NULL;
117117
return false;
118118
}
@@ -161,9 +161,9 @@ class InstanceLock
161161
public:
162162
InstanceLock(const bool global_) : globalSemaphoreName(PCM_INSTANCE_LOCK_SEMAPHORE_NAME), globalSemaphore(NULL), global(global_)
163163
{
164-
if(!global)
164+
if (!global)
165165
{
166-
pthread_mutex_lock(&processIntanceMutex);
166+
if (pthread_mutex_lock(&processIntanceMutex) != 0) std::cerr << "pthread_mutex_lock failed\n";
167167
return;
168168
}
169169
umask(0);
@@ -195,9 +195,9 @@ class InstanceLock
195195
}
196196
~InstanceLock()
197197
{
198-
if(!global)
198+
if (!global)
199199
{
200-
pthread_mutex_unlock(&processIntanceMutex);
200+
if (pthread_mutex_unlock(&processIntanceMutex) != 0) std::cerr << "pthread_mutex_unlock failed\n";
201201
return;
202202
}
203203
if (sem_post(globalSemaphore)) {
@@ -220,6 +220,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
220220
public:
221221
TemporalThreadAffinity(uint32 core_id, bool checkStatus = true)
222222
{
223+
assert(core_id < 1024);
223224
pthread_getaffinity_np(pthread_self(), sizeof(cpu_set_t), &old_affinity);
224225

225226
cpu_set_t new_affinity;
@@ -247,6 +248,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
247248
TemporalThreadAffinity(const uint32 core_id, bool checkStatus = true)
248249
: set_size(CPU_ALLOC_SIZE(maxCPUs))
249250
{
251+
assert(core_id < maxCPUs);
250252
old_affinity = CPU_ALLOC(maxCPUs);
251253
assert(old_affinity);
252254
pthread_getaffinity_np(pthread_self(), set_size, old_affinity);
@@ -593,7 +595,7 @@ bool PCM::detectModel()
593595
auto tokens = split(line, ':');
594596
if (tokens.size() >= 2 && tokens[0].find("flags") == 0)
595597
{
596-
for (auto curFlag : split(tokens[1], ' '))
598+
for (const auto & curFlag : split(tokens[1], ' '))
597599
{
598600
if (flag == curFlag)
599601
{
@@ -730,27 +732,27 @@ void PCM::initRDT()
730732
auto env = std::getenv("PCM_USE_RESCTRL");
731733
if (env != nullptr && std::string(env) == std::string("1"))
732734
{
733-
std::cout << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because environment variable PCM_USE_RESCTRL=1\n";
735+
std::cerr << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because environment variable PCM_USE_RESCTRL=1\n";
734736
resctrl.init();
735737
useResctrl = true;
736738
return;
737739
}
738740
if (resctrl.isMounted())
739741
{
740-
std::cout << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because resctrl driver is mounted.\n";
742+
std::cerr << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because resctrl driver is mounted.\n";
741743
resctrl.init();
742744
useResctrl = true;
743745
return;
744746
}
745747
if (isSecureBoot())
746748
{
747-
std::cout << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because Secure Boot mode is enabled.\n";
749+
std::cerr << "INFO: using Linux resctrl driver for RDT metrics (L3OCC, LMB, RMB) because Secure Boot mode is enabled.\n";
748750
resctrl.init();
749751
useResctrl = true;
750752
return;
751753
}
752754
#endif
753-
std::cout << "Initializing RMIDs" << std::endl;
755+
std::cerr << "Initializing RMIDs" << std::endl;
754756
unsigned maxRMID;
755757
/* Calculate maximum number of RMID supported by socket */
756758
maxRMID = getMaxRMID();
@@ -2105,6 +2107,7 @@ class CoreTaskQueue
21052107
std::thread worker;
21062108
CoreTaskQueue() = delete;
21072109
CoreTaskQueue(CoreTaskQueue &) = delete;
2110+
CoreTaskQueue & operator = (CoreTaskQueue &) = delete;
21082111
public:
21092112
CoreTaskQueue(int32 core) :
21102113
worker([=]() {
@@ -3046,18 +3049,21 @@ PCM::ErrorCode PCM::programCoreCounters(const int i /* core */,
30463049
perf_event_attr e = PCM_init_perf_event_attr();
30473050
e.type = PERF_TYPE_RAW;
30483051
e.config = (1ULL << 63ULL) + event_select_reg.value;
3049-
if (event_select_reg.fields.event_select == getOCREventNr(0, i).first && event_select_reg.fields.umask == getOCREventNr(0, i).second)
3050-
e.config1 = pExtDesc->OffcoreResponseMsrValue[0];
3051-
if (event_select_reg.fields.event_select == getOCREventNr(1, i).first && event_select_reg.fields.umask == getOCREventNr(1, i).second)
3052-
e.config1 = pExtDesc->OffcoreResponseMsrValue[1];
3053-
3054-
if (event_select_reg.fields.event_select == LOAD_LATENCY_EVTNR && event_select_reg.fields.umask == LOAD_LATENCY_UMASK)
3055-
{
3056-
e.config1 = pExtDesc->LoadLatencyMsrValue;
3057-
}
3058-
if (event_select_reg.fields.event_select == FRONTEND_EVTNR && event_select_reg.fields.umask == FRONTEND_UMASK)
3052+
if (pExtDesc != nullptr)
30593053
{
3060-
e.config1 = pExtDesc->FrontendMsrValue;
3054+
if (event_select_reg.fields.event_select == getOCREventNr(0, i).first && event_select_reg.fields.umask == getOCREventNr(0, i).second)
3055+
e.config1 = pExtDesc->OffcoreResponseMsrValue[0];
3056+
if (event_select_reg.fields.event_select == getOCREventNr(1, i).first && event_select_reg.fields.umask == getOCREventNr(1, i).second)
3057+
e.config1 = pExtDesc->OffcoreResponseMsrValue[1];
3058+
3059+
if (event_select_reg.fields.event_select == LOAD_LATENCY_EVTNR && event_select_reg.fields.umask == LOAD_LATENCY_UMASK)
3060+
{
3061+
e.config1 = pExtDesc->LoadLatencyMsrValue;
3062+
}
3063+
if (event_select_reg.fields.event_select == FRONTEND_EVTNR && event_select_reg.fields.umask == FRONTEND_UMASK)
3064+
{
3065+
e.config1 = pExtDesc->FrontendMsrValue;
3066+
}
30613067
}
30623068

30633069
if (programPerfEvent(e, PERF_GEN_EVENT_0_POS + j, std::string("generic event #") + std::to_string(i)) == false)
@@ -3113,12 +3119,12 @@ PCM::ErrorCode PCM::programCoreCounters(const int i /* core */,
31133119
std::make_pair(perfRetiringPath, PERF_TOPDOWN_RETIRING_POS)};
31143120
int readPos = core_fixed_counter_num_used + core_gen_counter_num_used;
31153121
leader_counter = -1;
3116-
for (auto event : topDownEvents)
3122+
for (const auto & event : topDownEvents)
31173123
{
31183124
uint64 eventSel = 0, umask = 0;
31193125
const auto eventDesc = readSysFS(event.first);
31203126
const auto tokens = split(eventDesc, ',');
3121-
for (auto token : tokens)
3127+
for (const auto & token : tokens)
31223128
{
31233129
if (match(token, "event=", &eventSel))
31243130
{
@@ -4605,9 +4611,9 @@ PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool sile
46054611
else
46064612
{
46074613
fixedReg.value = 0;
4608-
for (auto cfg : corePMUConfig.fixed)
4614+
for (const auto & cfg : corePMUConfig.fixed)
46094615
{
4610-
fixedReg.value |= cfg.first[0];
4616+
fixedReg.value |= uint64(cfg.first[0]);
46114617
}
46124618
conf.fixedCfg = &fixedReg;
46134619
}
@@ -5428,6 +5434,7 @@ void print_mcfg(const char * path)
54285434
if(read_bytes == 0)
54295435
{
54305436
std::cerr << "PCM Error: Cannot read " << path << "\n";
5437+
::close(mcfg_handle);
54315438
throw std::exception();
54325439
}
54335440

@@ -5442,6 +5449,7 @@ void print_mcfg(const char * path)
54425449
if(read_bytes == 0)
54435450
{
54445451
std::cerr << "PCM Error: Cannot read " << path << " (2)\n";
5452+
::close(mcfg_handle);
54455453
throw std::exception();
54465454
}
54475455
std::cout << "Segment " << std::dec << i << " ";
@@ -5640,16 +5648,21 @@ bool PCM::useLinuxPerfForUncore() const
56405648
bool secureBoot = isSecureBoot();
56415649
#ifdef PCM_USE_PERF
56425650
const auto imcIDs = enumeratePerfPMUs("imc", 100);
5643-
std::cout << "INFO: Linux perf interface to program uncore PMUs is " << (imcIDs.empty()?"NOT ":"") << "present\n";
5651+
std::cerr << "INFO: Linux perf interface to program uncore PMUs is " << (imcIDs.empty()?"NOT ":"") << "present\n";
5652+
if (imcIDs.empty())
5653+
{
5654+
use = 0;
5655+
return 1 == use;
5656+
}
56445657
const char * perf_env = std::getenv("PCM_USE_UNCORE_PERF");
56455658
if (perf_env != NULL && std::string(perf_env) == std::string("1"))
56465659
{
5647-
std::cout << "INFO: using Linux perf interface to program uncore PMUs because env variable PCM_USE_UNCORE_PERF=1\n";
5660+
std::cerr << "INFO: using Linux perf interface to program uncore PMUs because env variable PCM_USE_UNCORE_PERF=1\n";
56485661
use = 1;
56495662
}
56505663
if (secureBoot)
56515664
{
5652-
std::cout << "INFO: Secure Boot detected. Using Linux perf for uncore PMU programming.\n";
5665+
std::cerr << "INFO: Secure Boot detected. Using Linux perf for uncore PMU programming.\n";
56535666
use = 1;
56545667
}
56555668
else
@@ -7209,7 +7222,8 @@ void ServerPCICFGUncore::initMemTest(ServerPCICFGUncore::MemTestParam & param)
72097222
std::cerr << "ERROR: mmap failed\n";
72107223
return;
72117224
}
7212-
unsigned long long maxNode = (unsigned long long)(readMaxFromSysFS("/sys/devices/system/node/online") + 1);
7225+
const int64 onlineNodes = (int64)readMaxFromSysFS("/sys/devices/system/node/online");
7226+
unsigned long long maxNode = (unsigned long long)(onlineNodes + 1);
72137227
if (maxNode == 0)
72147228
{
72157229
std::cerr << "ERROR: max node is 0 \n";
@@ -7682,6 +7696,7 @@ void PCM::programCbo(const uint64 * events, const uint32 opCode, const uint32 nc
76827696

76837697
for(uint32 cbo = 0; cbo < getMaxNumOfCBoxes(); ++cbo)
76847698
{
7699+
assert(cbo < cboPMUs[i].size());
76857700
cboPMUs[i][cbo].initFreeze(UNC_PMON_UNIT_CTL_FRZ_EN);
76867701

76877702
if (ICX != cpu_model && SNOWRIDGE != cpu_model)
@@ -7984,7 +7999,7 @@ void PCM::setupCustomCoreEventsForNuma(PCM::ExtendedCustomCoreEventDescription&
79847999
conf.OffcoreResponseMsrValue[1] = 0x3FC0008FFF | (1 << 27) | (1 << 28) | (1 << 29);
79858000
break;
79868001
case PCM::ICX:
7987-
std::cout << "INFO: Monitored accesses include demand + L2 cache prefetcher, code read and RFO.\n";
8002+
std::cerr << "INFO: Monitored accesses include demand + L2 cache prefetcher, code read and RFO.\n";
79888003
// OCR.READS_TO_CORE.LOCAL_DRAM
79898004
conf.OffcoreResponseMsrValue[0] = 0x0104000477;
79908005
// OCR.READS_TO_CORE.REMOTE_DRAM

cpucounters.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2934,6 +2934,7 @@ class CoreCounterState : public BasicCounterState
29342934
CoreCounterState( const CoreCounterState& ) = default;
29352935
CoreCounterState( CoreCounterState&& ) = default;
29362936
CoreCounterState & operator= ( CoreCounterState&& ) = default;
2937+
virtual ~ CoreCounterState() {}
29372938
};
29382939

29392940
//! \brief Socket-wide counter state
@@ -2972,6 +2973,8 @@ class SocketCounterState : public BasicCounterState, public UncoreCounterState
29722973
UncoreCounterState::operator = ( std::move(ucs) );
29732974
return *this;
29742975
}
2976+
2977+
virtual ~ SocketCounterState() {}
29752978
};
29762979

29772980
//! \brief System-wide counter state
@@ -3028,6 +3031,7 @@ class SystemCounterState : public SocketCounterState
30283031

30293032
return *this;
30303033
}
3034+
virtual ~ SystemCounterState() {}
30313035
};
30323036

30333037
/*! \brief Reads the counter state of the system
@@ -3977,11 +3981,11 @@ inline uint64 getNumberOfEvents(const CounterType & before, const CounterType &
39773981
template <class CounterStateType>
39783982
inline double getLLCReadMissLatency(const CounterStateType & before, const CounterStateType & after)
39793983
{
3980-
if (PCM::getInstance()->LLCReadMissLatencyMetricsAvailable() == false) return -1.;
3984+
auto * m = PCM::getInstance();
3985+
if (m->LLCReadMissLatencyMetricsAvailable() == false) return -1.;
39813986
const double occupancy = double(after.TOROccupancyIAMiss) - double(before.TOROccupancyIAMiss);
39823987
const double inserts = double(after.TORInsertsIAMiss) - double(before.TORInsertsIAMiss);
39833988
const double unc_clocks = double(after.UncClocks) - double(before.UncClocks);
3984-
auto * m = PCM::getInstance();
39853989
const double seconds = double(getInvariantTSC(before, after)) / double(m->getNumOnlineCores()/m->getNumSockets()) / double(m->getNominalFrequency());
39863990
return 1e9*seconds*(occupancy/inserts)/unc_clocks;
39873991
}

daemon/daemon/daemon.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ namespace PCMDaemon {
5555
//Put the poll interval in shared memory so that the client knows
5656
sharedPCMState_->pollMs = pollIntervalMs_;
5757

58-
updatePCMState(&systemStatesBefore_, &socketStatesBefore_, &coreStatesBefore_);
58+
collectionTimeAfter_ = 0;
59+
60+
updatePCMState(&systemStatesBefore_, &socketStatesBefore_, &coreStatesBefore_, collectionTimeBefore_);
5961
systemStatesForQPIBefore_ = SystemCounterState(systemStatesBefore_);
6062

6163
serverUncoreCounterStatesBefore_ = new ServerUncoreCounterState[pcmInstance_->getNumSockets()];
@@ -384,7 +386,7 @@ namespace PCMDaemon {
384386

385387
sharedPCMState_->lastUpdateTscBegin = RDTSC();
386388

387-
updatePCMState(&systemStatesAfter_, &socketStatesAfter_, &coreStatesAfter_);
389+
updatePCMState(&systemStatesAfter_, &socketStatesAfter_, &coreStatesAfter_, collectionTimeAfter_);
388390

389391
getPCMSystem();
390392

@@ -421,7 +423,7 @@ namespace PCMDaemon {
421423
std::swap(collectionTimeBefore_, collectionTimeAfter_);
422424
}
423425

424-
void Daemon::updatePCMState(SystemCounterState* systemStates, std::vector<SocketCounterState>* socketStates, std::vector<CoreCounterState>* coreStates)
426+
void Daemon::updatePCMState(SystemCounterState* systemStates, std::vector<SocketCounterState>* socketStates, std::vector<CoreCounterState>* coreStates, uint64 & t)
425427
{
426428
if(subscribers_.find("core") != subscribers_.end())
427429
{
@@ -434,7 +436,7 @@ namespace PCMDaemon {
434436
pcmInstance_->getUncoreCounterStates(*systemStates, *socketStates);
435437
}
436438
}
437-
collectionTimeAfter_ = pcmInstance_->getTickCount();
439+
t = pcmInstance_->getTickCount();
438440
}
439441

440442
void Daemon::swapPCMBeforeAfterState()

daemon/daemon/daemon.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace PCMDaemon {
4242
void setupSharedMemory();
4343
gid_t resolveGroupName(const std::string& groupName);
4444
void getPCMCounters();
45-
void updatePCMState(SystemCounterState* systemStates, std::vector<SocketCounterState>* socketStates, std::vector<CoreCounterState>* coreStates);
45+
void updatePCMState(SystemCounterState* systemStates, std::vector<SocketCounterState>* socketStates, std::vector<CoreCounterState>* coreStates, uint64 & t);
4646
void swapPCMBeforeAfterState();
4747
void getPCMSystem();
4848
void getPCMCore();
@@ -64,7 +64,7 @@ namespace PCMDaemon {
6464
std::vector<std::string> allowedSubscribers_;
6565

6666
//Data for core, socket and system state
67-
uint64 collectionTimeBefore_, collectionTimeAfter_;
67+
uint64 collectionTimeBefore_{0ULL}, collectionTimeAfter_{0ULL};
6868
std::vector<CoreCounterState> coreStatesBefore_, coreStatesAfter_;
6969
std::vector<SocketCounterState> socketStatesBefore_, socketStatesAfter_;
7070
SystemCounterState systemStatesBefore_, systemStatesForQPIBefore_, systemStatesAfter_;

0 commit comments

Comments
 (0)