Skip to content

Commit a77fbf8

Browse files
authored
Merge pull request #241 from intel-innersource/agorneanu/239-help-should-suppress-stderr
Suppress stderr messages when --help/-h called
2 parents a17df42 + 5798de1 commit a77fbf8

13 files changed

+424
-336
lines changed

src/pcm-core.cpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -112,26 +112,27 @@ extern "C" {
112112

113113
void print_usage(const string progname)
114114
{
115-
cerr << "\n Usage: \n " << progname
116-
<< " --help | [delay] [options] [-- external_program [external_program_options]]\n";
117-
cerr << " <delay> => time interval to sample performance counters.\n";
118-
cerr << " If not specified, or 0, with external program given\n";
119-
cerr << " will read counters only after external program finishes\n";
120-
cerr << " Supported <options> are: \n";
121-
cerr << " -h | --help | /h => print this help and exit\n";
122-
cerr << " -c | /c => print CPU Model name and exit (used for pmu-query.py)\n";
123-
cerr << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
115+
cout << "\n Usage: \n " << progname
116+
<< " --help | [delay] [options] [-- external_program [external_program_options]]\n";
117+
cout << " <delay> => time interval to sample performance counters.\n";
118+
cout << " If not specified, or 0, with external program given\n";
119+
cout << " will read counters only after external program finishes\n";
120+
cout << " Supported <options> are: \n";
121+
cout << " -h | --help | /h => print this help and exit\n";
122+
cout << " -silent => silence information output and print only measurements\n";
123+
cout << " -c | /c => print CPU Model name and exit (used for pmu-query.py)\n";
124+
cout << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
124125
<< " to a file, in case filename is provided\n";
125-
cerr << " [-e event1] [-e event2] [-e event3] .. => optional list of custom events to monitor\n";
126-
cerr << " event description example: cpu/umask=0x01,event=0x05,name=MISALIGN_MEM_REF.LOADS/ \n";
127-
cerr << " -yc | --yescores | /yc => enable specific cores to output\n";
128-
cerr << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
126+
cout << " [-e event1] [-e event2] [-e event3] .. => optional list of custom events to monitor\n";
127+
cout << " event description example: cpu/umask=0x01,event=0x05,name=MISALIGN_MEM_REF.LOADS/ \n";
128+
cout << " -yc | --yescores | /yc => enable specific cores to output\n";
129+
cout << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
129130
print_help_force_rtm_abort_mode(41);
130-
cerr << " Examples:\n";
131-
cerr << " " << progname << " 1 => print counters every second without core and socket output\n";
132-
cerr << " " << progname << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
133-
cerr << " " << progname << " /csv 5 2>/dev/null => one sampe every 5 seconds, and discard all diagnostic output\n";
134-
cerr << "\n";
131+
cout << " Examples:\n";
132+
cout << " " << progname << " 1 => print counters every second without core and socket output\n";
133+
cout << " " << progname << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
134+
cout << " " << progname << " /csv 5 2>/dev/null => one sampe every 5 seconds, and discard all diagnostic output\n";
135+
cout << "\n";
135136
}
136137

137138
template <class StateType>
@@ -258,14 +259,17 @@ void build_event(const char * argv, EventSelectRegister *reg, int idx)
258259

259260
int main(int argc, char * argv[])
260261
{
261-
set_signal_handlers();
262-
262+
null_stream nullStream2;
263263
#ifdef PCM_FORCE_SILENT
264-
null_stream nullStream1, nullStream2;
264+
null_stream nullStream1;
265265
std::cout.rdbuf(&nullStream1);
266266
std::cerr.rdbuf(&nullStream2);
267+
#else
268+
check_and_set_silent(argc, argv, nullStream2);
267269
#endif
268270

271+
set_signal_handlers();
272+
269273
cerr << "\n";
270274
cerr << " Processor Counter Monitor: Core Monitoring Utility \n";
271275
cerr << "\n";
@@ -294,13 +298,16 @@ int main(int argc, char * argv[])
294298
{
295299
argv++;
296300
argc--;
297-
if (strncmp(*argv, "--help", 6) == 0 ||
298-
strncmp(*argv, "-h", 2) == 0 ||
299-
strncmp(*argv, "/h", 2) == 0)
301+
if (check_argument_equals(*argv, {"--help", "-h", "/h"}))
300302
{
301303
print_usage(program);
302304
exit(EXIT_FAILURE);
303305
}
306+
else if (check_argument_equals(*argv, {"-silent", "/silent"}))
307+
{
308+
// handled in check_and_set_silent
309+
continue;
310+
}
304311
else if (strncmp(*argv, "-csv",4) == 0 ||
305312
strncmp(*argv, "/csv",4) == 0)
306313
{

src/pcm-iio.cpp

Lines changed: 26 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,65 +1123,35 @@ void print_PCIeMapping(const std::vector<struct iio_stacks_on_socket>& iios, con
11231123
}
11241124
}
11251125

1126-
bool check_argument_equals(const char* arg, std::initializer_list<const char*> arg_names)
1127-
{
1128-
const auto arg_len = strlen(arg);
1129-
for (const auto& arg_name: arg_names) {
1130-
if (arg_len == strlen(arg_name) && strncmp(arg, arg_name, arg_len) == 0) {
1131-
return true;
1132-
}
1133-
}
1134-
1135-
return false;
1136-
}
1137-
1138-
bool extract_argument_value(const char* arg, std::initializer_list<const char*> arg_names, std::string& value)
1139-
{
1140-
const auto arg_len = strlen(arg);
1141-
for (const auto& arg_name: arg_names) {
1142-
const auto arg_name_len = strlen(arg_name);
1143-
if (arg_len > arg_name_len && strncmp(arg, arg_name, arg_name_len) == 0 && arg[arg_name_len] == '=') {
1144-
value = arg + arg_name_len + 1;
1145-
const auto last_pos = value.find_last_not_of("\"");
1146-
if (last_pos != string::npos) {
1147-
value.erase(last_pos + 1);
1148-
}
1149-
const auto first_pos = value.find_first_not_of("\"");
1150-
if (first_pos != string::npos) {
1151-
value.erase(0, first_pos);
1152-
}
1153-
1154-
return true;
1155-
}
1156-
}
1157-
1158-
return false;
1159-
}
1160-
11611126
void print_usage(const string& progname)
11621127
{
1163-
cerr << "\n Usage: \n " << progname << " --help | [interval] [options] \n";
1164-
cerr << " <interval> => time interval in seconds (floating point number is accepted)\n";
1165-
cerr << " to sample performance counters.\n";
1166-
cerr << " If not specified - 3.0 is used\n";
1167-
cerr << " Supported <options> are: \n";
1168-
cerr << " -h | --help | /h => print this help and exit\n";
1169-
cerr << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
1128+
cout << "\n Usage: \n " << progname << " --help | [interval] [options] \n";
1129+
cout << " <interval> => time interval in seconds (floating point number is accepted)\n";
1130+
cout << " to sample performance counters.\n";
1131+
cout << " If not specified - 3.0 is used\n";
1132+
cout << " Supported <options> are: \n";
1133+
cout << " -h | --help | /h => print this help and exit\n";
1134+
cout << " -silent => silence information output and print only measurements\n";
1135+
cout << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
11701136
<< " to a file, in case filename is provided\n";
1171-
cerr << " -csv-delimiter=<value> | /csv-delimiter=<value> => set custom csv delimiter\n";
1172-
cerr << " -human-readable | /human-readable => use human readable format for output (for csv only)\n";
1173-
cerr << " -root-port | /root-port => add root port devices to output (for csv only)\n";
1174-
cerr << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
1175-
cerr << " Examples:\n";
1176-
cerr << " " << progname << " 1.0 -i=10 => print counters every second 10 times and exit\n";
1177-
cerr << " " << progname << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
1178-
cerr << " " << progname << " -csv -human-readable => every 3 second print counters in human-readable CSV format\n";
1179-
cerr << "\n";
1137+
cout << " -csv-delimiter=<value> | /csv-delimiter=<value> => set custom csv delimiter\n";
1138+
cout << " -human-readable | /human-readable => use human readable format for output (for csv only)\n";
1139+
cout << " -root-port | /root-port => add root port devices to output (for csv only)\n";
1140+
cout << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
1141+
cout << " Examples:\n";
1142+
cout << " " << progname << " 1.0 -i=10 => print counters every second 10 times and exit\n";
1143+
cout << " " << progname << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
1144+
cout << " " << progname << " -csv -human-readable => every 3 second print counters in human-readable CSV format\n";
1145+
cout << "\n";
11801146
}
11811147

11821148
int main(int argc, char * argv[])
1183-
{
1149+
{
1150+
null_stream nullStream;
1151+
check_and_set_silent(argc, argv, nullStream);
1152+
11841153
set_signal_handlers();
1154+
11851155
std::cout << "\n Processor Counter Monitor " << PCM_VERSION << "\n";
11861156
std::cout << "\n This utility measures Skylake-SP IIO information\n\n";
11871157

@@ -1207,6 +1177,10 @@ int main(int argc, char * argv[])
12071177
print_usage(program);
12081178
exit(EXIT_FAILURE);
12091179
}
1180+
else if (check_argument_equals(*argv, {"-silent", "/silent"})) {
1181+
// handled in check_and_set_silent
1182+
continue;
1183+
}
12101184
else if (extract_argument_value(*argv, {"-csv-delimiter", "/csv-delimiter"}, arg_value)) {
12111185
csv_delimiter = std::move(arg_value);
12121186
}

src/pcm-latency.cpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -450,17 +450,22 @@ void collect_data(PCM *m, bool enable_pmm, bool enable_verbose, int delay_ms, Ma
450450

451451
void print_usage()
452452
{
453-
cerr << "\nUsage: \n";
454-
cerr << " -h | --help | /h => print this help and exit\n";
455-
cerr << " --PMM | -pmm => to enable PMM (Default DDR uncore latency)\n";
456-
cerr << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
457-
cerr << " -v | --verbose => verbose Output\n";
458-
cerr << "\n";
453+
cout << "\nUsage: \n";
454+
cout << " -h | --help | /h => print this help and exit\n";
455+
cout << " --PMM | -pmm => to enable PMM (Default DDR uncore latency)\n";
456+
cout << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
457+
cout << " -silent => silence information output and print only measurements\n";
458+
cout << " -v | --verbose => verbose Output\n";
459+
cout << "\n";
459460
}
460461

461462
int main(int argc, char * argv[])
462-
{
463+
{
464+
null_stream nullStream;
465+
check_and_set_silent(argc, argv, nullStream);
466+
463467
set_signal_handlers();
468+
464469
std::cout << "\n Processor Counter Monitor " << PCM_VERSION << "\n";
465470
std::cout << "\n This utility measures Latency information\n\n";
466471
bool enable_pmm = false;
@@ -472,13 +477,16 @@ int main(int argc, char * argv[])
472477
argv++;
473478
argc--;
474479

475-
if (strncmp(*argv, "--help", 6) == 0 ||
476-
strncmp(*argv, "-h", 2) == 0 ||
477-
strncmp(*argv, "/h", 2) == 0)
480+
if (check_argument_equals(*argv, {"--help", "-h", "/h"}))
478481
{
479482
print_usage();
480483
exit(EXIT_FAILURE);
481484
}
485+
else if (check_argument_equals(*argv, {"-silent", "/silent"}))
486+
{
487+
// handled in check_and_set_silent
488+
continue;
489+
}
482490
else if (mainLoop.parseArg(*argv))
483491
{
484492
continue;

src/pcm-memory.cpp

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -72,34 +72,34 @@ bool skipInactiveChannels = true;
7272

7373
void print_help(const string prog_name)
7474
{
75-
cerr << "\n Usage: \n " << prog_name
75+
cout << "\n Usage: \n " << prog_name
7676
<< " --help | [delay] [options] [-- external_program [external_program_options]]\n";
77-
cerr << " <delay> => time interval to sample performance counters.\n";
78-
cerr << " If not specified, or 0, with external program given\n";
79-
cerr << " will read counters only after external program finishes\n";
80-
cerr << " Supported <options> are: \n";
81-
cerr << " -h | --help | /h => print this help and exit\n";
82-
cerr << " -rank=X | /rank=X => monitor DIMM rank X. At most 2 out of 8 total ranks can be monitored simultaneously.\n";
83-
cerr << " -pmm | /pmm | -pmem | /pmem => monitor PMM memory bandwidth and DRAM cache hit rate in Memory Mode (default on systems with PMM support).\n";
84-
cerr << " -mm => monitor detailed PMM Memory Mode metrics per-socket.\n";
85-
cerr << " -mixed => monitor PMM mixed mode (AppDirect + Memory Mode).\n";
86-
cerr << " -partial => monitor partial writes instead of PMM (default on systems without PMM support).\n";
87-
cerr << " -nc | --nochannel | /nc => suppress output for individual channels.\n";
88-
cerr << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
77+
cout << " <delay> => time interval to sample performance counters.\n";
78+
cout << " If not specified, or 0, with external program given\n";
79+
cout << " will read counters only after external program finishes\n";
80+
cout << " Supported <options> are: \n";
81+
cout << " -h | --help | /h => print this help and exit\n";
82+
cout << " -rank=X | /rank=X => monitor DIMM rank X. At most 2 out of 8 total ranks can be monitored simultaneously.\n";
83+
cout << " -pmm | /pmm | -pmem | /pmem => monitor PMM memory bandwidth and DRAM cache hit rate in Memory Mode (default on systems with PMM support).\n";
84+
cout << " -mm => monitor detailed PMM Memory Mode metrics per-socket.\n";
85+
cout << " -mixed => monitor PMM mixed mode (AppDirect + Memory Mode).\n";
86+
cout << " -partial => monitor partial writes instead of PMM (default on systems without PMM support).\n";
87+
cout << " -nc | --nochannel | /nc => suppress output for individual channels.\n";
88+
cout << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
8989
<< " to a file, in case filename is provided\n";
90-
cerr << " -columns=X | /columns=X => Number of columns to display the NUMA Nodes, defaults to 2.\n";
91-
cerr << " -all | /all => Display all channels (even with no traffic)\n";
92-
cerr << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
93-
cerr << " -s => silence information output and print only measurements\n";
94-
cerr << " -u => update measurements instead of printing new ones\n";
90+
cout << " -columns=X | /columns=X => Number of columns to display the NUMA Nodes, defaults to 2.\n";
91+
cout << " -all | /all => Display all channels (even with no traffic)\n";
92+
cout << " -i[=number] | /i[=number] => allow to determine number of iterations\n";
93+
cout << " -silent => silence information output and print only measurements\n";
94+
cout << " -u => update measurements instead of printing new ones\n";
9595
#ifdef _MSC_VER
96-
cerr << " --uninstallDriver | --installDriver=> (un)install driver\n";
96+
cout << " --uninstallDriver | --installDriver=> (un)install driver\n";
9797
#endif
98-
cerr << " Examples:\n";
99-
cerr << " " << prog_name << " 1 => print counters every second without core and socket output\n";
100-
cerr << " " << prog_name << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
101-
cerr << " " << prog_name << " /csv 5 2>/dev/null => one sampe every 5 seconds, and discard all diagnostic output\n";
102-
cerr << "\n";
98+
cout << " Examples:\n";
99+
cout << " " << prog_name << " 1 => print counters every second without core and socket output\n";
100+
cout << " " << prog_name << " 0.5 -csv=test.log => twice a second save counter values to test.log in CSV format\n";
101+
cout << " " << prog_name << " /csv 5 2>/dev/null => one sampe every 5 seconds, and discard all diagnostic output\n";
102+
cout << "\n";
103103
}
104104

105105
void printSocketBWHeader(uint32 no_columns, uint32 skt, const bool show_channel_output)
@@ -966,8 +966,6 @@ void calculate_bandwidth_rank(PCM *m, const ServerUncoreCounterState uncState1[]
966966

967967
int main(int argc, char * argv[])
968968
{
969-
set_signal_handlers();
970-
971969
null_stream nullStream2;
972970
#ifdef PCM_FORCE_SILENT
973971
null_stream nullStream1;
@@ -977,6 +975,8 @@ int main(int argc, char * argv[])
977975
check_and_set_silent(argc, argv, nullStream2);
978976
#endif
979977

978+
set_signal_handlers();
979+
980980
cerr << "\n";
981981
cerr << " Processor Counter Monitor: Memory Bandwidth Monitoring Utility " << PCM_VERSION << "\n";
982982
cerr << "\n";
@@ -1002,13 +1002,16 @@ int main(int argc, char * argv[])
10021002
{
10031003
argv++;
10041004
argc--;
1005-
if (strncmp(*argv, "--help", 6) == 0 ||
1006-
strncmp(*argv, "-h", 2) == 0 ||
1007-
strncmp(*argv, "/h", 2) == 0)
1005+
if (check_argument_equals(*argv, {"--help", "-h", "/h"}))
10081006
{
10091007
print_help(program);
10101008
exit(EXIT_FAILURE);
10111009
}
1010+
else if (check_argument_equals(*argv, {"-silent", "/silent"}))
1011+
{
1012+
// handled in check_and_set_silent
1013+
continue;
1014+
}
10121015
else if (strncmp(*argv, "-csv",4) == 0 ||
10131016
strncmp(*argv, "/csv",4) == 0)
10141017
{
@@ -1105,12 +1108,6 @@ int main(int argc, char * argv[])
11051108
metrics = PartialWrites;
11061109
continue;
11071110
}
1108-
else if (strncmp(*argv, "-s", 2) == 0 ||
1109-
strncmp(*argv, "/s", 2) == 0)
1110-
{
1111-
//already checked by check_and_set_silent()
1112-
continue;
1113-
}
11141111
else if (strncmp(*argv, "-u", 2) == 0 ||
11151112
strncmp(*argv, "/u", 2) == 0)
11161113
{

0 commit comments

Comments
 (0)