Skip to content

Commit 1469e02

Browse files
committed
pcm-raw: add collection of core events for process ID
1 parent ba16d76 commit 1469e02

File tree

6 files changed

+30
-11
lines changed

6 files changed

+30
-11
lines changed

src/cpucounters.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4583,7 +4583,7 @@ void PCM::programPCU(uint32* PCUCntConf, const uint64 filter)
45834583
}
45844584
}
45854585

4586-
PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool silent)
4586+
PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool silent, const int pid)
45874587
{
45884588
if (MSR.empty()) return PCM::MSRAccessDenied;
45894589
threadMSRConfig = RawPMUConfig{};
@@ -4638,7 +4638,7 @@ PCM::ErrorCode PCM::program(const RawPMUConfigs& curPMUConfigs_, const bool sile
46384638
}
46394639
conf.defaultUncoreProgramming = false;
46404640

4641-
const auto status = program(PCM::EXT_CUSTOM_CORE_EVENTS, &conf, silent);
4641+
const auto status = program(PCM::EXT_CUSTOM_CORE_EVENTS, &conf, silent, pid);
46424642
if (status != PCM::Success)
46434643
{
46444644
return status;

src/cpucounters.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1187,7 +1187,7 @@ class PCM_API PCM
11871187
FrontendPos = 4
11881188
};
11891189
typedef std::map<std::string, RawPMUConfig> RawPMUConfigs;
1190-
ErrorCode program(const RawPMUConfigs& curPMUConfigs, const bool silent = false);
1190+
ErrorCode program(const RawPMUConfigs& curPMUConfigs, const bool silent = false, const int pid = -1);
11911191

11921192
std::pair<unsigned, unsigned> getOCREventNr(const int event, const unsigned coreID) const
11931193
{

src/pcm-raw.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ void print_usage(const string progname)
5858
cerr << " will read counters only after external program finishes\n";
5959
cerr << " Supported <options> are: \n";
6060
cerr << " -h | --help | /h => print this help and exit\n";
61-
cerr << " -e event1 [-e event2] [-e event3] .. => list of custom events to monitor\n";
61+
cerr << " -e event1 [-e event2] [-e event3] .. => list of custom events to monitor\n";
62+
cerr << " -pid PID | /pid PID => collect core metrics only for specified process ID\n";
6263
cerr << " -r | --reset | /reset => reset PMU configuration (at your own risk)\n";
6364
cerr << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
6465
<< " to a file, in case filename is provided\n";
@@ -1786,6 +1787,7 @@ int main(int argc, char* argv[])
17861787

17871788
std::vector<PCM::RawPMUConfigs> PMUConfigs(1);
17881789
double delay = -1.0;
1790+
int pid{-1};
17891791
char* sysCmd = NULL;
17901792
char** sysArgv = NULL;
17911793
MainLoop mainLoop;
@@ -1794,6 +1796,8 @@ int main(int argc, char* argv[])
17941796
bool reset_pmu = false;
17951797
PCM* m = PCM::getInstance();
17961798

1799+
parseParam(argc, argv, "pid", [&pid](const char* p) { if (p) pid = atoi(p); });
1800+
17971801
#ifdef PCM_SIMDJSON_AVAILABLE
17981802
parseParam(argc, argv, "ep", [](const char* p) { eventFileLocationPrefix = p;});
17991803
#endif
@@ -1826,7 +1830,13 @@ int main(int argc, char* argv[])
18261830
{
18271831
continue;
18281832
}
1829-
if (strncmp(*argv, "-reset", 6) == 0 ||
1833+
else if (strncmp(*argv, "-pid", 4) == 0 || strncmp(*argv, "/pid", 4) == 0)
1834+
{
1835+
argv++;
1836+
argc--;
1837+
continue;
1838+
}
1839+
else if (strncmp(*argv, "-reset", 6) == 0 ||
18301840
strncmp(*argv, "-r", 2) == 0 ||
18311841
strncmp(*argv, "/reset", 6) == 0)
18321842
{
@@ -2031,7 +2041,9 @@ int main(int argc, char* argv[])
20312041
cerr << "Enforcing transposed event output because the number of event groups > 1\n";
20322042
}
20332043

2034-
auto programPMUs = [&m](const PCM::RawPMUConfigs & config)
2044+
print_pid_collection_message(pid);
2045+
2046+
auto programPMUs = [&m, &pid](const PCM::RawPMUConfigs & config)
20352047
{
20362048
if (verbose)
20372049
{
@@ -2047,7 +2059,7 @@ int main(int argc, char* argv[])
20472059
}
20482060
}
20492061
}
2050-
PCM::ErrorCode status = m->program(config, !verbose);
2062+
PCM::ErrorCode status = m->program(config, !verbose, pid);
20512063
m->checkError(status);
20522064
};
20532065

src/pcm.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1376,10 +1376,7 @@ int main(int argc, char * argv[])
13761376
SystemCounterState sstate1, sstate2;
13771377
const auto cpu_model = m->getCPUModel();
13781378

1379-
if (pid != -1)
1380-
{
1381-
cerr << "Collecting core metrics for process ID " << pid << "\n";
1382-
}
1379+
print_pid_collection_message(pid);
13831380

13841381
if ((sysCmd != NULL) && (delay <= 0.0)) {
13851382
// in case external command is provided in command line, and

src/utils.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,14 @@ std::string safe_getenv(const char* env)
671671
}
672672
#endif
673673

674+
void print_pid_collection_message(int pid)
675+
{
676+
if (pid != -1)
677+
{
678+
std::cerr << "Collecting core metrics for process ID " << std::dec << pid << "\n";
679+
}
680+
}
681+
674682
void check_and_set_silent(int argc, char * argv[], null_stream &nullStream2) {
675683
if (argc > 1) do
676684
{

src/utils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,6 @@ inline HANDLE openMSRDriver()
496496
// silence all following err output
497497
void check_and_set_silent(int argc, char * argv[], null_stream &nullStream2);
498498

499+
void print_pid_collection_message(int pid);
500+
499501
} // namespace pcm

0 commit comments

Comments
 (0)