From 668350321b373b61a7d7ef3a2f61f48398f4fe43 Mon Sep 17 00:00:00 2001 From: UsualSpec <98665326+UsualSpec@users.noreply.github.com> Date: Mon, 21 Oct 2024 15:04:41 +0200 Subject: [PATCH] Add JSON output with -L option --- src/Settings.cpp | 40 +++++++++++++++++++++++++++++++++++++++- src/Settings.h | 1 + 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/Settings.cpp b/src/Settings.cpp index b9aa7a5..37ddd51 100644 --- a/src/Settings.cpp +++ b/src/Settings.cpp @@ -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; @@ -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 '?': @@ -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; @@ -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); diff --git a/src/Settings.h b/src/Settings.h index d5e5ba4..038144c 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -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;