From 0f38dc14dd42bee178348a36a01af7751d4c4e59 Mon Sep 17 00:00:00 2001 From: Michael May Date: Tue, 7 Apr 2020 15:49:43 -0700 Subject: [PATCH 1/9] Updated to correctly parse tegrastats messages for AGX --- .gitignore | 1 + display.cc | 6 ++++-- display.hh | 1 + gtop.cc | 11 +++++++++-- gtop.hh | 4 ++-- utils.hh | 2 +- 6 files changed, 18 insertions(+), 7 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ebc7991 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +gtop \ No newline at end of file diff --git a/display.cc b/display.cc index a3da1fe..915221a 100644 --- a/display.cc +++ b/display.cc @@ -108,14 +108,16 @@ void display_cpu_stats(const int & row, const tegrastats & ts) { int idx = 0; for (const auto & u : ts.cpu_usage) { const auto cpu_label = std::string("CPU ") + std::to_string(idx); - attron(COLOR_PAIR(idx+1)); + attron(COLOR_PAIR((idx+1) % COLOR_COUNT)); mvprintw(row+idx, 0, cpu_label.c_str()); - attroff(COLOR_PAIR(idx+1)); + attroff(COLOR_PAIR((idx+1) % COLOR_COUNT)); if (ts.version == TX1) display_bars(row+idx, BAR_OFFSET, u, ts.cpu_freq.at(0)); else if (ts.version == TX2) display_bars(row+idx, BAR_OFFSET, u, ts.cpu_freq.at(idx)); + else if (ts.version == AGX) + display_bars(row+idx, BAR_OFFSET, u, ts.cpu_freq.at(idx)); idx++; } diff --git a/display.hh b/display.hh index 9c68a8d..0c43971 100644 --- a/display.hh +++ b/display.hh @@ -14,6 +14,7 @@ const int BAR_OFFSET = 6; const int MIN_HEIGHT_USAGE_CHART = 30; +const int COLOR_COUNT = 7; enum colors {RED_BLACK=1, GREEN_BLACK, YELLOW_BLACK, diff --git a/gtop.cc b/gtop.cc index d511ebc..4466dc8 100644 --- a/gtop.cc +++ b/gtop.cc @@ -116,10 +116,13 @@ tegrastats parse_tegrastats(const char * buffer) { tegrastats ts; auto stats = tokenize(buffer, ' '); - if (stats.size() >= 15) + if (stats.size() >= 30) { + ts.version = AGX; + } else if (stats.size() >= 15) { ts.version = TX1; - else + } else { ts.version = TX2; + } get_mem_stats(ts, stats.at(1)); @@ -132,6 +135,10 @@ tegrastats parse_tegrastats(const char * buffer) { get_cpu_stats_tx2(ts, stats.at(5)); get_gpu_stats(ts, stats.at(13)); break; + case AGX: + get_cpu_stats_tx2(ts, stats.at(9)); + get_gpu_stats(ts, stats.at(13)); + break; case TK1: // TODO break; } diff --git a/gtop.hh b/gtop.hh index f601067..b7ba6c9 100644 --- a/gtop.hh +++ b/gtop.hh @@ -21,9 +21,9 @@ #include "display.hh" #include "utils.hh" -const int STATS_BUFFER_SIZE = 256; +const int STATS_BUFFER_SIZE = 512; -const std::string TEGRASTATS_PATH = "~/tegrastats"; +const std::string TEGRASTATS_PATH = "/usr/bin/tegrastats"; const std::string TEGRASTATSFAKE_PATH = "./tegrastats_fake"; void read_tegrastats(); diff --git a/utils.hh b/utils.hh index 71e7522..ca56be4 100644 --- a/utils.hh +++ b/utils.hh @@ -10,7 +10,7 @@ #include #include -enum jetson_version {TK1, TX1, TX2}; +enum jetson_version {TK1, TX1, TX2, AGX}; struct tegrastats { int mem_usage; From ac75faec5084f54d8f0d6085a2527de0a6da811d Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 16:13:55 -0700 Subject: [PATCH 2/9] Added DLA power --- display.cc | 9 +++++++-- display.hh | 4 +++- gtop.cc | 29 +++++++++++++++++++++++------ gtop.hh | 1 + utils.hh | 4 ++++ 5 files changed, 38 insertions(+), 9 deletions(-) diff --git a/display.cc b/display.cc index 915221a..547c295 100644 --- a/display.cc +++ b/display.cc @@ -128,6 +128,11 @@ void display_gpu_stats(const int & row, const tegrastats & ts) { display_bars(row, BAR_OFFSET, ts.gpu_usage, ts.gpu_freq); } +void display_dla_power_stats(const int & row, const tegrastats & ts) { + mvprintw(row, 0, "DLA POW"); + display_bars(row, BAR_OFFSET, 100 * (static_cast(ts.dla_power) / static_cast(ts.dla_power_max)), ts.dla_power_max); +} + void display_mem_stats(const int & row, const tegrastats & ts) { mvprintw(row, 0, "Mem"); display_mem_bars(row, BAR_OFFSET, ts.mem_usage, ts.mem_max); @@ -151,9 +156,9 @@ void display_usage_chart(const int & row, const std::vector> cp } int tmp_cpu_usage = int((max_height/100.0)*cpu_usage); - attron(COLOR_PAIR(idx)); + attron(COLOR_PAIR(idx % COLOR_COUNT)); mvprintw(max_height-tmp_cpu_usage+row, col+3, "*"); - attroff(COLOR_PAIR(idx)); + attroff(COLOR_PAIR(idx % COLOR_COUNT)); idx++; } diff --git a/display.hh b/display.hh index 0c43971..49ba20d 100644 --- a/display.hh +++ b/display.hh @@ -11,7 +11,7 @@ #include "utils.hh" -const int BAR_OFFSET = 6; +const int BAR_OFFSET = 8; const int MIN_HEIGHT_USAGE_CHART = 30; const int COLOR_COUNT = 7; @@ -69,7 +69,9 @@ void clear_row(const int &, const int &); void display_cpu_stats(const int &, const tegrastats &); void display_gpu_stats(const int &, const tegrastats &); +void display_dla_power_stats(const int &, const tegrastats &); void display_mem_stats(const int &, const tegrastats &); + void display_usage_chart(const int &, const std::vector>); #endif // DISPLAY_HH_ diff --git a/gtop.cc b/gtop.cc index 4466dc8..731f812 100644 --- a/gtop.cc +++ b/gtop.cc @@ -11,13 +11,13 @@ bool processed = false; bool ready = false; bool finished = false; -int main() { +int main() { if (getuid()) { std::cout << "gtop requires root privileges!" << std::endl; exit(1); } - std::thread t(read_tegrastats); + std::thread t(read_tegrastats); initscr(); noecho(); @@ -51,7 +51,7 @@ int main() { // CPU USAGE CHART update_usage_chart(cpu_usage_buffer, t_stats.cpu_usage); - display_usage_chart(10, cpu_usage_buffer); + display_usage_chart(11, cpu_usage_buffer); lk.unlock(); @@ -62,7 +62,7 @@ int main() { break; } - { + { std::lock_guard lk(m); finished = true; } @@ -138,6 +138,7 @@ tegrastats parse_tegrastats(const char * buffer) { case AGX: get_cpu_stats_tx2(ts, stats.at(9)); get_gpu_stats(ts, stats.at(13)); + get_dla_power_stats(ts, stats.at(36)); break; case TK1: // TODO break; @@ -196,15 +197,31 @@ void get_mem_stats(tegrastats & ts, const std::string & str) { ts.mem_max = std::stoi(mem_max.substr(0, mem_max.size()-2)); } +void get_dla_power_stats(tegrastats & ts, const std::string & str) { + const auto mem_stats = tokenize(str, '/'); + + std::cout << mem_stats.at(0) << std::endl; + ts.dla_power = std::stoi(mem_stats.at(0)); + ts.dla_power_avg = std::stoi(mem_stats.at(1)); + ts.dla_power_max = std::max(ts.dla_power_max, ts.dla_power); +} + void display_stats(const dimensions & d, const tegrastats & ts) { // CPU display_cpu_stats(0, ts); // GPU - display_gpu_stats(ts.cpu_usage.size(), ts); + int pos = ts.cpu_usage.size(); + display_gpu_stats(pos, ts); + + if(ts.version == AGX) { + pos++; + display_dla_power_stats(pos, ts); + } + pos++; // Memory - display_mem_stats(ts.cpu_usage.size()+1, ts); + display_mem_stats(pos, ts); } void update_usage_chart(std::vector> & usage_buffer, diff --git a/gtop.hh b/gtop.hh index b7ba6c9..e2493eb 100644 --- a/gtop.hh +++ b/gtop.hh @@ -33,6 +33,7 @@ void get_cpu_stats_tx1(tegrastats &, const std::string &); void get_cpu_stats_tx2(tegrastats &, const std::string &); void get_gpu_stats(tegrastats &, const std::string &); void get_mem_stats(tegrastats &, const std::string &); +void get_dla_power_stats(tegrastats &, const std::string &); void display_stats(const dimensions &, const tegrastats &); void update_usage_chart(std::vector> &, const std::vector &); diff --git a/utils.hh b/utils.hh index ca56be4..71d2bc0 100644 --- a/utils.hh +++ b/utils.hh @@ -22,6 +22,10 @@ struct tegrastats { int gpu_usage; int gpu_freq; + int dla_power; + int dla_power_avg; + int dla_power_max; + jetson_version version; }; From e9fba13a81ec486199ebb0d2982cabdf6ad29373 Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 16:32:06 -0700 Subject: [PATCH 3/9] Added changed to CV Pow --- display.cc | 2 +- display.hh | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/display.cc b/display.cc index 547c295..2e97b03 100644 --- a/display.cc +++ b/display.cc @@ -129,7 +129,7 @@ void display_gpu_stats(const int & row, const tegrastats & ts) { } void display_dla_power_stats(const int & row, const tegrastats & ts) { - mvprintw(row, 0, "DLA POW"); + mvprintw(row, 0, "CV Pow"); display_bars(row, BAR_OFFSET, 100 * (static_cast(ts.dla_power) / static_cast(ts.dla_power_max)), ts.dla_power_max); } diff --git a/display.hh b/display.hh index 49ba20d..a3f0ea1 100644 --- a/display.hh +++ b/display.hh @@ -11,7 +11,7 @@ #include "utils.hh" -const int BAR_OFFSET = 8; +const int BAR_OFFSET = 6; const int MIN_HEIGHT_USAGE_CHART = 30; const int COLOR_COUNT = 7; From bf087d0620ecf2aab8a64f57523bbfb9202f95a2 Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 16:49:12 -0700 Subject: [PATCH 4/9] Display max power and current power --- display.cc | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/display.cc b/display.cc index 2e97b03..504d4df 100644 --- a/display.cc +++ b/display.cc @@ -30,6 +30,20 @@ void display_bars(const int & row, const int & col, const int & val, const int & refresh(); } +void display_bars(const int & row, const int & col, const int & val, const int & current, const int & maximum) { + auto b = update_bar_dims(val); + clear_row(row, col); + + display_left_bracket(row, col); + display_bars(b.val_bar); + + mvprintw(row, col+b.max_bar+1, "%3d%%", val); + display_right_bracket(); + printw(" %d/%d", current, maximum); + + refresh(); +} + void display_mem_bars(const int & row, const int & col, const int & val, const int & max_val) { auto val_norm = int((float(val) / max_val) * 100); auto b = update_bar_dims(val_norm); @@ -130,7 +144,9 @@ void display_gpu_stats(const int & row, const tegrastats & ts) { void display_dla_power_stats(const int & row, const tegrastats & ts) { mvprintw(row, 0, "CV Pow"); - display_bars(row, BAR_OFFSET, 100 * (static_cast(ts.dla_power) / static_cast(ts.dla_power_max)), ts.dla_power_max); + display_bars(row, BAR_OFFSET, 100 * (static_cast(ts.dla_power) / static_cast(ts.dla_power_max)), + ts.dla_power, + ts.dla_power_max); } void display_mem_stats(const int & row, const tegrastats & ts) { From 4f5f29e5caf0a67bb4aafb7bd4a6b9d91d695376 Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 16:57:06 -0700 Subject: [PATCH 5/9] Fix mem offset and add units to bars --- display.cc | 13 ++++++------- display.hh | 2 ++ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/display.cc b/display.cc index 504d4df..46331b9 100644 --- a/display.cc +++ b/display.cc @@ -25,12 +25,13 @@ void display_bars(const int & row, const int & col, const int & val, const int & mvprintw(row, col+b.max_bar+1, "%3d%%", val); display_right_bracket(); - printw(" %d", freq); + printw(" %dHz", freq); refresh(); } -void display_bars(const int & row, const int & col, const int & val, const int & current, const int & maximum) { +void display_power_bars(const int & row, const int & col, const int & current, const int & maximum) { + int val = 100 * (static_cast(current) / static_cast(maximum)); auto b = update_bar_dims(val); clear_row(row, col); @@ -39,7 +40,7 @@ void display_bars(const int & row, const int & col, const int & val, const int & mvprintw(row, col+b.max_bar+1, "%3d%%", val); display_right_bracket(); - printw(" %d/%d", current, maximum); + printw(" %d/%dmW", current, maximum); refresh(); } @@ -55,7 +56,7 @@ void display_mem_bars(const int & row, const int & col, const int & val, const i char buffer[MEM_BUFFER_SIZE]; sprintf(buffer, "%2.2fG/%2.2fG", mega2giga(val), mega2giga(max_val)); - mvprintw(row, col+b.max_bar-6, buffer); + mvprintw(row, col+b.max_bar-7, buffer); display_right_bracket(); refresh(); } @@ -144,9 +145,7 @@ void display_gpu_stats(const int & row, const tegrastats & ts) { void display_dla_power_stats(const int & row, const tegrastats & ts) { mvprintw(row, 0, "CV Pow"); - display_bars(row, BAR_OFFSET, 100 * (static_cast(ts.dla_power) / static_cast(ts.dla_power_max)), - ts.dla_power, - ts.dla_power_max); + display_power_bars(row, BAR_OFFSET, ts.dla_power, ts.dla_power_max); } void display_mem_stats(const int & row, const tegrastats & ts) { diff --git a/display.hh b/display.hh index a3f0ea1..9aadc41 100644 --- a/display.hh +++ b/display.hh @@ -59,6 +59,8 @@ struct dimensions { void display_bars(const int &, const int &, const int &); void display_bars(const int &, const int &, const int &, const int &); void display_bars(const int &); +void display_power_bars(const int &, const int &, const int &, const int &); + void display_mem_bars(const int &, const int &, const int &, const int &); float mega2giga(const int &); bar update_bar_dims(const int &); From e76d1bc15864281feb50c0d2586cd69e608456fa Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 17:13:20 -0700 Subject: [PATCH 6/9] Initialize data to 0 --- gtop.cc | 10 +++++----- gtop.hh | 2 +- utils.hh | 14 +++++++------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/gtop.cc b/gtop.cc index 731f812..da84d76 100644 --- a/gtop.cc +++ b/gtop.cc @@ -104,7 +104,7 @@ void read_tegrastats() { cv.wait(lk, []{ return ready; }); ready = false; - t_stats = parse_tegrastats(buffer.data()); + parse_tegrastats(buffer.data(), t_stats); processed = true; lk.unlock(); cv.notify_one(); @@ -112,8 +112,10 @@ void read_tegrastats() { } } -tegrastats parse_tegrastats(const char * buffer) { - tegrastats ts; +void parse_tegrastats(const char * buffer, tegrastats &ts) { + + ts.cpu_freq.clear(); + ts.cpu_usage.clear(); auto stats = tokenize(buffer, ' '); if (stats.size() >= 30) { @@ -143,8 +145,6 @@ tegrastats parse_tegrastats(const char * buffer) { case TK1: // TODO break; } - - return ts; } void get_cpu_stats_tx1(tegrastats & ts, const std::string & str) { diff --git a/gtop.hh b/gtop.hh index e2493eb..f075db8 100644 --- a/gtop.hh +++ b/gtop.hh @@ -27,7 +27,7 @@ const std::string TEGRASTATS_PATH = "/usr/bin/tegrastats"; const std::string TEGRASTATSFAKE_PATH = "./tegrastats_fake"; void read_tegrastats(); -tegrastats parse_tegrastats(const char *); +void parse_tegrastats(const char * buffer, tegrastats &ts); void get_cpu_stats_tx1(tegrastats &, const std::string &); void get_cpu_stats_tx2(tegrastats &, const std::string &); diff --git a/utils.hh b/utils.hh index 71d2bc0..dbdef8e 100644 --- a/utils.hh +++ b/utils.hh @@ -13,18 +13,18 @@ enum jetson_version {TK1, TX1, TX2, AGX}; struct tegrastats { - int mem_usage; - int mem_max; + int mem_usage = 0; + int mem_max = 0; std::vector cpu_usage; std::vector cpu_freq; - int gpu_usage; - int gpu_freq; + int gpu_usage = 0; + int gpu_freq = 0; - int dla_power; - int dla_power_avg; - int dla_power_max; + int dla_power = 0; + int dla_power_avg = 0; + int dla_power_max = 0; jetson_version version; }; From 21c262bf9282caebf9ebed11600681f27c5811ee Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 17:23:20 -0700 Subject: [PATCH 7/9] Fix cpu parsing for different modes and mode switching --- gtop.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gtop.cc b/gtop.cc index da84d76..1a9c325 100644 --- a/gtop.cc +++ b/gtop.cc @@ -166,7 +166,7 @@ void get_cpu_stats_tx2(tegrastats & ts, const std::string & str) { const auto at = std::string("@"); for (const auto & u: cpu_stats) { - if (u != "off") { + if (u != "off" && u != "off]" && u != "[off") { std::size_t found = at.find(u); if (found == std::string::npos) { auto cpu_info = tokenize(u, at.c_str()[0]); From 3ffd77b1503953558dd83a06780d06e04fb2c3d4 Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 18:40:19 -0700 Subject: [PATCH 8/9] Add make install and uninstall --- Makefile | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Makefile b/Makefile index 861608c..6771b75 100644 --- a/Makefile +++ b/Makefile @@ -6,3 +6,9 @@ fake: travis: $(CXX) -std=c++14 gtop.cc utils.cc display.cc -o gtop -pedantic -Wall -Wextra -lncurses -lpthread + +install: all + cp gtop /usr/local/bin + +unistall: + rm -f /usr/local/bin/gtop \ No newline at end of file From 24d98fadc0bffbbdca079e83766a14cbe2fd3fb8 Mon Sep 17 00:00:00 2001 From: Michael May Date: Wed, 8 Apr 2020 18:46:46 -0700 Subject: [PATCH 9/9] Remove cout --- gtop.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/gtop.cc b/gtop.cc index 1a9c325..746d1cb 100644 --- a/gtop.cc +++ b/gtop.cc @@ -200,7 +200,6 @@ void get_mem_stats(tegrastats & ts, const std::string & str) { void get_dla_power_stats(tegrastats & ts, const std::string & str) { const auto mem_stats = tokenize(str, '/'); - std::cout << mem_stats.at(0) << std::endl; ts.dla_power = std::stoi(mem_stats.at(0)); ts.dla_power_avg = std::stoi(mem_stats.at(1)); ts.dla_power_max = std::max(ts.dla_power_max, ts.dla_power);