Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gtop for AGX with DLA power usage #10

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
gtop
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
34 changes: 28 additions & 6 deletions display.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,26 @@ 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_power_bars(const int & row, const int & col, const int & current, const int & maximum) {
int val = 100 * (static_cast<float>(current) / static_cast<float>(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/%dmW", 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);
Expand All @@ -41,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();
}
Expand Down Expand Up @@ -108,14 +123,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++;
}
Expand All @@ -126,6 +143,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, "CV Pow");
display_power_bars(row, BAR_OFFSET, ts.dla_power, 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);
Expand All @@ -149,9 +171,9 @@ void display_usage_chart(const int & row, const std::vector<std::vector<int>> 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++;
}
Expand Down
5 changes: 5 additions & 0 deletions display.hh
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -58,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 &);
Expand All @@ -68,7 +71,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<std::vector<int>>);

#endif // DISPLAY_HH_
51 changes: 37 additions & 14 deletions gtop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -62,7 +62,7 @@ int main() {
break;
}

{
{
std::lock_guard<std::mutex> lk(m);
finished = true;
}
Expand Down Expand Up @@ -104,22 +104,27 @@ 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();
}
}
}

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() >= 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));

Expand All @@ -132,11 +137,14 @@ 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));
get_dla_power_stats(ts, stats.at(36));
break;
case TK1: // TODO
break;
}

return ts;
}

void get_cpu_stats_tx1(tegrastats & ts, const std::string & str) {
Expand All @@ -158,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]);
Expand Down Expand Up @@ -189,15 +197,30 @@ 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, '/');

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<std::vector<int>> & usage_buffer,
Expand Down
7 changes: 4 additions & 3 deletions gtop.hh
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,19 @@
#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();
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 &);
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<std::vector<int>> &, const std::vector<int> &);
Expand Down
14 changes: 9 additions & 5 deletions utils.hh
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
#include <sys/stat.h>
#include <wordexp.h>

enum jetson_version {TK1, TX1, TX2};
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<int> cpu_usage;
std::vector<int> cpu_freq;

int gpu_usage;
int gpu_freq;
int gpu_usage = 0;
int gpu_freq = 0;

int dla_power = 0;
int dla_power_avg = 0;
int dla_power_max = 0;

jetson_version version;
};
Expand Down