Skip to content

Commit a0a1ea3

Browse files
committed
pcm-raw: implement -out switch to print all stderr/stdout to a file
Change-Id: I1377146c412d9015ba70bbe323c770cc43f988fe
1 parent dbdc796 commit a0a1ea3

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

src/cpucounters.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,6 +2173,10 @@ class CoreTaskQueue
21732173
}
21742174
};
21752175

2176+
std::ofstream* PCM::outfile = nullptr; // output file stream
2177+
std::streambuf* PCM::backup_ofile = nullptr; // backup of original output = cout
2178+
std::streambuf* PCM::backup_ofile_cerr = nullptr; // backup of original output = cerr
2179+
21762180
PCM::PCM() :
21772181
cpu_family(-1),
21782182
cpu_model(-1),
@@ -2229,8 +2233,6 @@ PCM::PCM() :
22292233
mode(INVALID_MODE),
22302234
numInstancesSemaphore(NULL),
22312235
canUsePerf(false),
2232-
outfile(NULL),
2233-
backup_ofile(NULL),
22342236
run_state(1),
22352237
needToRestoreNMIWatchdog(false)
22362238
{
@@ -4044,11 +4046,16 @@ void PCM::cleanupRDT(const bool silent)
40444046
if (!silent) std::cerr << " Freeing up all RMIDs\n";
40454047
}
40464048

4047-
void PCM::setOutput(const std::string filename)
4049+
void PCM::setOutput(const std::string filename, const bool cerrToo)
40484050
{
40494051
outfile = new std::ofstream(filename.c_str());
40504052
backup_ofile = std::cout.rdbuf();
40514053
std::cout.rdbuf(outfile->rdbuf());
4054+
if (cerrToo)
4055+
{
4056+
backup_ofile_cerr = std::cerr.rdbuf();
4057+
std::cerr.rdbuf(outfile->rdbuf());
4058+
}
40524059
}
40534060

40544061
void PCM::restoreOutput()
@@ -4057,6 +4064,9 @@ void PCM::restoreOutput()
40574064
if(backup_ofile)
40584065
std::cout.rdbuf(backup_ofile);
40594066

4067+
if (backup_ofile_cerr)
4068+
std::cerr.rdbuf(backup_ofile_cerr);
4069+
40604070
// close output file
40614071
if(outfile)
40624072
outfile->close();

src/cpucounters.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -675,8 +675,8 @@ class PCM_API PCM
675675
return (pkgCStateMsr != NULL && state <= ((int)MAX_C_STATE) && pkgCStateMsr[state] != 0);
676676
}
677677

678-
//! \brief Redirects output destination to provided file, instead of std::cout
679-
void setOutput(const std::string filename);
678+
//! \brief Redirects output destination to provided file, instead of std::cout and std::cerr (optional)
679+
static void setOutput(const std::string filename, const bool cerrToo = false);
680680

681681
//! \brief Restores output, closes output file if opened
682682
void restoreOutput();
@@ -898,8 +898,9 @@ class PCM_API PCM
898898
PERF_TOPDOWN_GROUP_LEADER_COUNTER = PERF_TOPDOWN_SLOTS_POS
899899
};
900900
#endif
901-
std::ofstream * outfile; // output file stream
902-
std::streambuf * backup_ofile; // backup of original output = cout
901+
static std::ofstream * outfile; // output file stream
902+
static std::streambuf * backup_ofile; // backup of original output = cout
903+
static std::streambuf * backup_ofile_cerr; // backup of original output = cerr
903904
int run_state; // either running (1) or sleeping (0)
904905

905906
bool needToRestoreNMIWatchdog;

src/pcm-raw.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,10 +59,11 @@ void print_usage(const string progname)
5959
cerr << " will read counters only after external program finishes\n";
6060
cerr << " Supported <options> are: \n";
6161
cerr << " -h | --help | /h => print this help and exit\n";
62+
cerr << " -e event1 [-e event2] [-e event3] .. => list of custom events to monitor\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";
65-
cerr << " [-e event1] [-e event2] [-e event3] .. => list of custom events to monitor\n";
66+
cerr << " -out filename | /out filename => write all output (stdout and stderr) to specified file\n";
6667
cerr << " event description example: -e core/config=0x30203,name=LD_BLOCKS.STORE_FORWARD/ -e core/fixed,config=0x333/ \n";
6768
cerr << " -e cha/config=0,name=UNC_CHA_CLOCKTICKS/ -e imc/fixed,name=DRAM_CLOCKS/\n";
6869
#ifdef PCM_SIMDJSON_AVAILABLE
@@ -1732,6 +1733,13 @@ int main(int argc, char* argv[])
17321733
{
17331734
set_signal_handlers();
17341735

1736+
parseParam(argc, argv, "out", [](const char* p) {
1737+
const string filename{ p };
1738+
if (!filename.empty()) {
1739+
PCM::setOutput(filename, true);
1740+
}
1741+
});
1742+
17351743
#ifdef PCM_FORCE_SILENT
17361744
null_stream nullStream1, nullStream2;
17371745
std::cout.rdbuf(&nullStream1);
@@ -1851,6 +1859,12 @@ int main(int argc, char* argv[])
18511859
}
18521860
continue;
18531861
}
1862+
else if (strncmp(*argv, "-out", 4) == 0 || strncmp(*argv, "/out", 4) == 0)
1863+
{
1864+
argv++;
1865+
argc--;
1866+
continue;
1867+
}
18541868
else if (strncmp(*argv, "-ep", 3) == 0 || strncmp(*argv, "/ep", 3) == 0)
18551869
{
18561870
argv++;

0 commit comments

Comments
 (0)