Skip to content

Commit 029a512

Browse files
committed
implement no-MSR mode
1 parent 3618308 commit 029a512

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

src/cpucounters.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2505,6 +2505,12 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter
25052505
canUsePerf = false;
25062506
if (!silent) std::cerr << "Installed Linux kernel perf does not support hardware top-down level-1 counters. Using direct PMU programming instead.\n";
25072507
}
2508+
2509+
if (canUsePerf == false && noMSRMode())
2510+
{
2511+
std::cerr << "ERROR: can not use perf driver and no-MSR mode is enabled\n" ;
2512+
return PCM::UnknownError;
2513+
}
25082514
#endif
25092515

25102516
if(allow_multiple_instances)
@@ -3692,9 +3698,9 @@ bool PCM::PMUinUse()
36923698

36933699
for (uint32 j = 0; j < core_gen_counter_num_max; ++j)
36943700
{
3695-
MSR[i]->read(IA32_PERFEVTSEL0_ADDR + j, &event_select_reg.value);
3701+
const auto count = MSR[i]->read(IA32_PERFEVTSEL0_ADDR + j, &event_select_reg.value);
36963702

3697-
if (event_select_reg.fields.event_select != 0 || event_select_reg.fields.apic_int != 0)
3703+
if (count && (event_select_reg.fields.event_select != 0 || event_select_reg.fields.apic_int != 0))
36983704
{
36993705
std::cerr << "WARNING: Core " << i <<" IA32_PERFEVTSEL" << j << "_ADDR is not zeroed " << event_select_reg.value << "\n";
37003706

@@ -3710,12 +3716,12 @@ bool PCM::PMUinUse()
37103716
FixedEventControlRegister ctrl_reg;
37113717
ctrl_reg.value = 0xffffffffffffffff;
37123718

3713-
MSR[i]->read(IA32_CR_FIXED_CTR_CTRL, &ctrl_reg.value);
3719+
const auto count = MSR[i]->read(IA32_CR_FIXED_CTR_CTRL, &ctrl_reg.value);
37143720

37153721
// Check if someone has installed pmi handler on counter overflow.
37163722
// If so, that agent might potentially need to change counter value
37173723
// for the "sample after"-mode messing up PCM measurements
3718-
if(ctrl_reg.fields.enable_pmi0 || ctrl_reg.fields.enable_pmi1 || ctrl_reg.fields.enable_pmi2)
3724+
if (count && (ctrl_reg.fields.enable_pmi0 || ctrl_reg.fields.enable_pmi1 || ctrl_reg.fields.enable_pmi2))
37193725
{
37203726
std::cerr << "WARNING: Core " << i << " fixed ctrl:" << ctrl_reg.value << "\n";
37213727
if (needToRestoreNMIWatchdog == false) // if NMI watchdog did not clear the fields, ignore it

src/msr.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,20 @@ int32 MsrHandle::read(uint64 msr_number, uint64 * value)
213213

214214
#else
215215
// here comes a Linux version
216+
217+
bool noMSRMode()
218+
{
219+
static int noMSR = -1;
220+
if (noMSR < 0)
221+
{
222+
noMSR = (safe_getenv("PCM_NO_MSR") == std::string("1")) ? 1 : 0;
223+
}
224+
return 1 == noMSR;
225+
}
226+
216227
MsrHandle::MsrHandle(uint32 cpu) : fd(-1), cpu_id(cpu)
217228
{
229+
if (noMSRMode()) return;
218230
constexpr auto allowWritesPath = "/sys/module/msr/parameters/allow_writes";
219231
static bool writesEnabled = false;
220232
if (writesEnabled == false)
@@ -237,6 +249,7 @@ MsrHandle::MsrHandle(uint32 cpu) : fd(-1), cpu_id(cpu)
237249
if (handle < 0)
238250
{
239251
std::cerr << "PCM Error: can't open MSR handle for core " << cpu << "\n";
252+
std::cerr << "Try no-MSR mode by setting env variable PCM_NO_MSR=1\n";
240253
throw std::exception();
241254
}
242255
fd = handle;
@@ -254,14 +267,21 @@ int32 MsrHandle::write(uint64 msr_number, uint64 value)
254267
std::lock_guard<std::mutex> g(m);
255268
std::cout << "DEBUG: writing MSR 0x" << std::hex << msr_number << " value 0x" << value << " on cpu " << std::dec << cpu_id << std::endl;
256269
#endif
270+
if (fd < 0) return 0;
257271
return ::pwrite(fd, (const void *)&value, sizeof(uint64), msr_number);
258272
}
259273

260274
int32 MsrHandle::read(uint64 msr_number, uint64 * value)
261275
{
276+
if (fd < 0) return 0;
262277
return ::pread(fd, (void *)value, sizeof(uint64), msr_number);
263278
}
264279

265280
#endif
266281

282+
283+
#ifndef __linux__
284+
bool noMSRMode() { return false; }
285+
#endif
286+
267287
} // namespace pcm

src/msr.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
3535

3636
namespace pcm {
3737

38+
bool noMSRMode();
39+
3840
class MsrHandle
3941
{
4042
#ifdef _MSC_VER

0 commit comments

Comments
 (0)