Skip to content

Commit dd44e05

Browse files
committed
add color helper code
1 parent d0ea7f5 commit dd44e05

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/pcm.cpp

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ void print_help(const string & prog_name)
8989
cout << " -yc | --yescores | /yc => enable specific cores to output\n";
9090
cout << " -ns | --nosockets | /ns => hide socket related output\n";
9191
cout << " -nsys | --nosystem | /nsys => hide system related output\n";
92+
cout << " --color => use ASCII colors\n";
9293
cout << " -csv[=file.csv] | /csv[=file.csv] => output compact CSV format to screen or\n"
9394
<< " to a file, in case filename is provided\n"
9495
<< " the format used is documented here: https://www.intel.com/content/www/us/en/developer/articles/technical/intel-pcm-column-names-decoder-ring.html\n";
@@ -169,10 +170,43 @@ void print_output(PCM * m,
169170
const bool show_partial_core_output,
170171
const bool show_socket_output,
171172
const bool show_system_output,
172-
const int metricVersion
173+
const int metricVersion,
174+
const bool color
173175
)
174176
{
175177
cout << "\n";
178+
auto setColor = [&color](const char * colorStr)
179+
{
180+
return color ? colorStr : "";
181+
};
182+
std::vector<const char *> colorTable = {
183+
ASCII_GREEN,
184+
ASCII_YELLOW,
185+
ASCII_MAGENTA,
186+
ASCII_CYAN,
187+
ASCII_BRIGHT_GREEN,
188+
ASCII_BRIGHT_YELLOW,
189+
ASCII_BRIGHT_BLUE,
190+
ASCII_BRIGHT_MAGENTA,
191+
ASCII_BRIGHT_CYAN,
192+
ASCII_BRIGHT_WHITE
193+
};
194+
size_t currentColor = 0;
195+
auto setNextColor = [&setColor,&currentColor,colorTable]()
196+
{
197+
const auto result = setColor(colorTable[currentColor++]);
198+
if (currentColor == colorTable.size())
199+
{
200+
currentColor = 0;
201+
}
202+
return result;
203+
};
204+
auto resetColor = [&setColor, &currentColor]()
205+
{
206+
currentColor = 0;
207+
return setColor(ASCII_RESET_COLOR);
208+
};
209+
176210
switch (metricVersion)
177211
{
178212
case 2:
@@ -1292,6 +1326,7 @@ int mainThrows(int argc, char * argv[])
12921326
bool disable_JKT_workaround = false; // as per http://software.intel.com/en-us/articles/performance-impact-when-sampling-certain-llc-events-on-snb-ep-with-vtune
12931327
bool enforceFlush = false;
12941328
int metricVersion = 2;
1329+
bool color = false;
12951330

12961331
parsePID(argc, argv, pid);
12971332

@@ -1371,6 +1406,11 @@ int mainThrows(int argc, char * argv[])
13711406
show_system_output = false;
13721407
continue;
13731408
}
1409+
else if (check_argument_equals(*argv, {"--color"}))
1410+
{
1411+
color = true;
1412+
continue;
1413+
}
13741414
else if (check_argument_equals(*argv, {"-csv", "/csv"}))
13751415
{
13761416
csv_output = true;
@@ -1524,7 +1564,8 @@ int mainThrows(int argc, char * argv[])
15241564
cpu_model, show_core_output, show_partial_core_output, show_socket_output, show_system_output);
15251565
else
15261566
print_output(m, cstates1, cstates2, sktstate1, sktstate2, ycores, sstate1, sstate2,
1527-
cpu_model, show_core_output, show_partial_core_output, show_socket_output, show_system_output, metricVersion);
1567+
cpu_model, show_core_output, show_partial_core_output, show_socket_output, show_system_output,
1568+
metricVersion, color);
15281569

15291570
std::swap(sstate1, sstate2);
15301571
std::swap(sktstate1, sktstate2);

src/utils.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,24 @@ struct null_stream : public std::streambuf
145145
#pragma clang diagnostic pop
146146
#endif
147147

148+
constexpr const char* ASCII_BLACK = "\033[0;30m";
149+
constexpr const char* ASCII_RED = "\033[0;31m";
150+
constexpr const char* ASCII_GREEN = "\033[0;32m";
151+
constexpr const char* ASCII_YELLOW = "\033[0;33m";
152+
constexpr const char* ASCII_BLUE = "\033[0;34m";
153+
constexpr const char* ASCII_MAGENTA = "\033[0;35m";
154+
constexpr const char* ASCII_CYAN = "\033[0;36m";
155+
constexpr const char* ASCII_WHITE = "\033[0;37m";
156+
constexpr const char* ASCII_BRIGHT_BLACK = "\033[1;30m";
157+
constexpr const char* ASCII_BRIGHT_RED = "\033[1;31m";
158+
constexpr const char* ASCII_BRIGHT_GREEN = "\033[1;32m";
159+
constexpr const char* ASCII_BRIGHT_YELLOW = "\033[1;33m";
160+
constexpr const char* ASCII_BRIGHT_BLUE = "\033[1;34m";
161+
constexpr const char* ASCII_BRIGHT_MAGENTA = "\033[1;35m";
162+
constexpr const char* ASCII_BRIGHT_CYAN = "\033[1;36m";
163+
constexpr const char* ASCII_BRIGHT_WHITE = "\033[1;37m";
164+
constexpr const char* ASCII_RESET_COLOR = "\033[0m";
165+
148166
template <class IntType>
149167
inline std::string unit_format(IntType n)
150168
{

0 commit comments

Comments
 (0)