Skip to content

Commit e4b3c95

Browse files
authored
Merge pull request #394 from opcm/push-2022-03-28
Push 2022 03 28
2 parents ac6715b + fba72e2 commit e4b3c95

16 files changed

+162
-35
lines changed

examples/c_example.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ int main(int argc, const char *argv[])
8888
return -2;
8989
}
9090

91-
for (int i = 0; i < numEvents; ++i)
91+
for (i = 0; i < numEvents; ++i)
9292
{
9393
PCM.pcm_c_build_core_event(i, argv[i+1]);
9494
}

scripts/find_field.awk

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/awk -f
2+
3+
{
4+
for(i=1; i<=NF; i++) {
5+
if (index($i, term) > 0) print (i)":"$i;
6+
}
7+
}

scripts/find_field.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
3+
head -1 $1 | awk -F ',' -v term="$2" -f find_field.awk
4+
5+

scripts/pcm.plot

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
2+
set key autotitle columnhead
3+
set datafile separator ","
4+
5+
# change as needed
6+
# set xlabel 'sample # (each is 1000ms)'
7+
8+
set ylabel 'metric value'
9+
10+
set terminal pdf
11+
set output "pcm.pdf"
12+
13+
# change below as needed
14+
# plot metrics 3 .. 37
15+
do for [m=3:37] {
16+
plot "single_header.pcm.csv" using m with dots
17+
}
18+
19+
# plot metrics 84 .. 107
20+
do for [m=84:107] {
21+
plot "single_header.pcm.csv" using m with dots
22+
}
23+
24+

scripts/single_header.awk

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
BEGIN {
2+
line = 0;
3+
}
4+
5+
{
6+
if (line == 0)
7+
{
8+
# print $0;
9+
for(i=1; i<=NF; i++) {
10+
first[i] = $i;
11+
}
12+
}
13+
else if (line == 1)
14+
{
15+
for(i=1; i<=NF; i++) {
16+
if ($i != "") printf first[i]" "$i","
17+
}
18+
print ""
19+
}
20+
else
21+
{
22+
print $0
23+
}
24+
25+
line = line + 1;
26+
}

scripts/single_header.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
cat $1 | awk -F ',' -f single_header.awk > single_header.$1
4+

src/cpucounters.cpp

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
133133
TemporalThreadAffinity(); // forbidden
134134
#if defined(__FreeBSD__) || (defined(__DragonFly__) && __DragonFly_version >= 400707)
135135
cpu_set_t old_affinity;
136-
const bool restore;
136+
bool restore;
137137

138138
public:
139139
TemporalThreadAffinity(uint32 core_id, bool checkStatus = true, const bool restore_ = true)
@@ -152,6 +152,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
152152
// CPU_CMP() returns true if old_affinity is NOT equal to new_affinity
153153
if (!(CPU_CMP(&old_affinity, &new_affinity)))
154154
{
155+
restore = false;
155156
return; // the same affinity => return
156157
}
157158
res = pthread_setaffinity_np(pthread_self(), sizeof(cpu_set_t), &new_affinity);
@@ -171,7 +172,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
171172
cpu_set_t * old_affinity;
172173
static constexpr auto maxCPUs = 8192;
173174
const size_t set_size;
174-
const bool restore;
175+
bool restore;
175176

176177
public:
177178
TemporalThreadAffinity(const uint32 core_id, bool checkStatus = true, const bool restore_ = true)
@@ -193,6 +194,7 @@ class TemporalThreadAffinity // speedup trick for Linux, FreeBSD, DragonFlyBSD,
193194
if (CPU_EQUAL_S(set_size, old_affinity, new_affinity))
194195
{
195196
CPU_FREE(new_affinity);
197+
restore = false;
196198
return;
197199
}
198200
res = pthread_setaffinity_np(pthread_self(), set_size, new_affinity);
@@ -2074,7 +2076,6 @@ PCM::PCM() :
20742076
pkgMinimumPower(-1),
20752077
pkgMaximumPower(-1),
20762078
systemTopology(new SystemRoot(this)),
2077-
programmed_pmu(false),
20782079
joulesPerEnergyUnit(0),
20792080
#ifdef __linux__
20802081
resctrl(*this),
@@ -2411,6 +2412,7 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter
24112412
ExtendedCustomCoreEventDescription * pExtDesc = (ExtendedCustomCoreEventDescription *)parameter_;
24122413

24132414
#ifdef PCM_USE_PERF
2415+
closePerfHandles(silent);
24142416
if (!silent) std::cerr << "Trying to use Linux perf events...\n";
24152417
const char * no_perf_env = std::getenv("PCM_NO_PERF");
24162418
if (no_perf_env != NULL && std::string(no_perf_env) == std::string("1"))
@@ -2461,7 +2463,7 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter
24612463
}
24622464
#endif
24632465

2464-
if (true)
2466+
if (programmed_core_pmu == false)
24652467
{
24662468
if((canUsePerf == false) && PMUinUse())
24672469
{
@@ -2740,8 +2742,6 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter
27402742
}
27412743
#endif
27422744

2743-
programmed_pmu = true;
2744-
27452745
lastProgrammedCustomCounters.clear();
27462746
lastProgrammedCustomCounters.resize(num_cores);
27472747
core_global_ctrl_value = 0ULL;
@@ -2774,6 +2774,8 @@ PCM::ErrorCode PCM::program(const PCM::ProgramMode mode_, const void * parameter
27742774
}
27752775
}
27762776

2777+
programmed_core_pmu = true;
2778+
27772779
if (canUsePerf && !silent)
27782780
{
27792781
std::cerr << "Successfully programmed on-core PMU using Linux perf\n";
@@ -3806,9 +3808,9 @@ const char * PCM::getUArchCodename(const int32 cpu_model_param) const
38063808
return "unknown";
38073809
}
38083810

3809-
void PCM::cleanupPMU(const bool silent)
3810-
{
38113811
#ifdef PCM_USE_PERF
3812+
void PCM::closePerfHandles(const bool silent)
3813+
{
38123814
if (canUsePerf)
38133815
{
38143816
auto cleanOne = [this](PerfEventHandleContainer & cont)
@@ -3831,6 +3833,17 @@ void PCM::cleanupPMU(const bool silent)
38313833
perfEventTaskHandle.clear();
38323834

38333835
if (!silent) std::cerr << " Closed perf event handles\n";
3836+
}
3837+
}
3838+
#endif
3839+
3840+
void PCM::cleanupPMU(const bool silent)
3841+
{
3842+
programmed_core_pmu = false;
3843+
#ifdef PCM_USE_PERF
3844+
closePerfHandles(silent);
3845+
if (canUsePerf)
3846+
{
38343847
return;
38353848
}
38363849
#endif
@@ -5741,15 +5754,14 @@ ServerPCICFGUncore::ServerPCICFGUncore(uint32 socket_, const PCM * pcm) :
57415754
, cpu_model(pcm->getCPUModel())
57425755
, qpi_speed(0)
57435756
{
5744-
initRegisterLocations(pcm);
5745-
initBuses(socket_, pcm);
5746-
57475757
if (pcm->useLinuxPerfForUncore())
57485758
{
57495759
initPerf(socket_, pcm);
57505760
}
57515761
else
57525762
{
5763+
initRegisterLocations(pcm);
5764+
initBuses(socket_, pcm);
57535765
initDirect(socket_, pcm);
57545766
}
57555767

src/cpucounters.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ class PCM_API PCM
600600
std::string errorMessage;
601601

602602
static PCM * instance;
603-
bool programmed_pmu;
603+
bool programmed_core_pmu{false};
604604
std::vector<std::shared_ptr<SafeMsrHandle> > MSR;
605605
std::vector<std::shared_ptr<ServerPCICFGUncore> > server_pcicfg_uncore;
606606
std::vector<UncorePMU> pcuPMUs;
@@ -863,6 +863,7 @@ class PCM_API PCM
863863
PerfEventHandleContainer perfEventHandle;
864864
std::vector<PerfEventHandleContainer> perfEventTaskHandle;
865865
void readPerfData(uint32 core, std::vector<uint64> & data);
866+
void closePerfHandles(const bool silent = false);
866867

867868
enum {
868869
PERF_INST_RETIRED_POS = 0,

src/pcm-numa.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ int main(int argc, char * argv[])
108108
cerr << "\n";
109109

110110
double delay = -1.0;
111+
int pid{ -1 };
111112
char * sysCmd = NULL;
112113
char ** sysArgv = NULL;
113114
bool csv = false;
@@ -116,10 +117,17 @@ int main(int argc, char * argv[])
116117

117118
PCM * m = PCM::getInstance();
118119

120+
parsePID(argc, argv, pid);
121+
119122
if (argc > 1) do
120123
{
121124
argv++;
122125
argc--;
126+
if (*argv == nullptr)
127+
{
128+
continue;
129+
}
130+
else
123131
if (strncmp(*argv, "--help", 6) == 0 ||
124132
strncmp(*argv, "-h", 2) == 0 ||
125133
strncmp(*argv, "/h", 2) == 0)
@@ -141,6 +149,12 @@ int main(int argc, char * argv[])
141149
}
142150
continue;
143151
}
152+
else if (isPIDOption(argv))
153+
{
154+
argv++;
155+
argc--;
156+
continue;
157+
}
144158
else if (mainLoop.parseArg(*argv))
145159
{
146160
continue;
@@ -198,7 +212,9 @@ int main(int argc, char * argv[])
198212
regs[1].fields.event_select = m->getOCREventNr(1, 0).first; // OFFCORE_RESPONSE 1 event
199213
regs[1].fields.umask = m->getOCREventNr(1, 0).second;
200214

201-
PCM::ErrorCode status = m->program(PCM::EXT_CUSTOM_CORE_EVENTS, &conf);
215+
print_pid_collection_message(pid);
216+
217+
PCM::ErrorCode status = m->program(PCM::EXT_CUSTOM_CORE_EVENTS, &conf, false, pid);
202218
m->checkError(status);
203219

204220
print_cpu_details();

src/pcm-raw.cpp

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ struct PrintOffset {
962962
std::vector<PrintOffset> printOffsets;
963963
std::vector<std::string> printedBlocks;
964964

965-
int getPrintOffsetIdx(const std::vector<PrintOffset> &printOffsets, const std::string &value) {
965+
int getPrintOffsetIdx(const std::string &value) {
966966
for (size_t i = 0 ; i < printOffsets.size() ; i++) {
967967
if (printOffsets[i].entry == value)
968968
return (int)i;
@@ -1064,7 +1064,7 @@ void printTransposed(const PCM::RawPMUConfigs& curPMUConfigs,
10641064
const auto& fixedEvents = typeEvents.second.fixed;
10651065

10661066
PrintOffset printOffset{type, 0, 0};
1067-
const auto print_idx = getPrintOffsetIdx(printOffsets, type);
1067+
const auto print_idx = getPrintOffsetIdx(type);
10681068

10691069
if (outputType == Header1 || outputType == Header21) {
10701070
if (print_idx != -1)
@@ -1810,7 +1810,7 @@ int main(int argc, char* argv[])
18101810
bool reset_pmu = false;
18111811
PCM* m = PCM::getInstance();
18121812

1813-
parseParam(argc, argv, "pid", [&pid](const char* p) { if (p) pid = atoi(p); });
1813+
parsePID(argc, argv, pid);
18141814

18151815
#ifdef PCM_SIMDJSON_AVAILABLE
18161816
parseParam(argc, argv, "ep", [](const char* p) { eventFileLocationPrefix = p;});
@@ -1844,7 +1844,7 @@ int main(int argc, char* argv[])
18441844
{
18451845
continue;
18461846
}
1847-
else if (strncmp(*argv, "-pid", 4) == 0 || strncmp(*argv, "/pid", 4) == 0)
1847+
else if (isPIDOption(argv))
18481848
{
18491849
argv++;
18501850
argc--;
@@ -2147,11 +2147,7 @@ int main(int argc, char* argv[])
21472147
//cout << "Called sleep function for " << dec << fixed << delay_ms << " ms\n";
21482148

21492149
printAll(group, m, BeforeState, AfterState, BeforeUncoreState, AfterUncoreState, BeforeSocketState, AfterSocketState, PMUConfigs, groupNr == nGroups);
2150-
if (nGroups > 1)
2151-
{
2152-
m->cleanup(true);
2153-
}
2154-
else
2150+
if (nGroups == 1)
21552151
{
21562152
std::swap(BeforeState, AfterState);
21572153
std::swap(BeforeSocketState, AfterSocketState);

0 commit comments

Comments
 (0)