Skip to content

Commit

Permalink
Add JSON output with -L option
Browse files Browse the repository at this point in the history
  • Loading branch information
UsualSpec committed Oct 21, 2024
1 parent 25ad8d3 commit 6683503
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ bool continuous_header_flag = false;
bool countinous_timestamp_flag = false;
bool energy_delayed_product = false;
bool print_counter_list = false;
bool print_counter_list_json = false;

bool print_total_flag = false;

Expand Down Expand Up @@ -98,7 +99,7 @@ static struct option longopts[] = {
void readProgArgs(int argc, char *argv[])
{
int c;
while ((c = getopt_long (argc, argv, "hlcpne:r:d:i:b:a:o:U:", longopts, NULL)) != -1) {
while ((c = getopt_long (argc, argv, "hlLcpne:r:d:i:b:a:o:U:", longopts, NULL)) != -1) {
switch (c) {
case 'h':
case '?':
Expand Down Expand Up @@ -132,6 +133,9 @@ void readProgArgs(int argc, char *argv[])
case 'b':
before = std::chrono::milliseconds(atoi(optarg));
break;
case 'L':
print_counter_list_json = true;
break;
case 'l':
print_counter_list = true;
break;
Expand Down Expand Up @@ -185,6 +189,40 @@ void validate()
exit(0);
}

if (print_counter_list_json) {
// Print conters and aliasses in JSON format for processing via other applications

// Have a JSON object key available_conters which gets all available counters
std::cout << "{\"available_counters\":[";
bool first = true;
for (const auto & counter: Registry::availableCounters()) {
if (first) {
first=false;
} else {
// for fence post problem on commas
std::cout << ",";
}

std::cout << '"' << counter << '"';
}

// Also print aliasses
first = true;
std::cout << "],\"available_aliasses\":[";
for (const auto & alias: Registry::availableAliases()) {
if (first) {
first=false;
} else {
std::cout << ",";
}

std::cout << "{\"" << alias.first << "\":\"" << alias.second << "\"}";
}

std::cout << "]}";
exit(0);
}

if (no_workload_flag && !continuous_print_flag) {
std::cerr << "-n only works if continous output (-c) is enabled." << std::endl;
exit(1);
Expand Down
1 change: 1 addition & 0 deletions src/Settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern bool no_workload_flag;

extern bool energy_delayed_product;
extern bool print_counter_list;
extern bool print_counter_list_json;

extern bool print_total_flag;

Expand Down

0 comments on commit 6683503

Please sign in to comment.