commit b629c8b321a6696e6322ee63fe979e4964f5be51
parent 69aac62c0f6a21f0d839994a7058015ba8b0f4c2
Author: Kyle Milz <kyle@0x30.net>
Date: Tue, 26 Jul 2016 21:28:05 -0600
src: term snapshot
Diffstat:
M | src/term_main.cc | | | 92 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++---------------- |
1 file changed, 74 insertions(+), 18 deletions(-)
diff --git a/src/term_main.cc b/src/term_main.cc
@@ -1,9 +1,11 @@
-/*
- * Original idea for this program from Max Zunti.
- */
+//
+// Original idea from Max Zunti.
+//
#include <iostream>
#include <ncurses.h>
+#include <queue>
#include <stdlib.h> // exit
+#include <time.h> // clock_gettime, nanosleep
#include "af_unix.h"
#include "runtime_conn.h"
@@ -22,8 +24,10 @@ main(int argc, char *argv[])
}
start_color();
init_pair(1, COLOR_GREEN, COLOR_BLACK);
+ init_pair(2, COLOR_YELLOW, COLOR_BLACK);
+ init_pair(3, COLOR_RED, COLOR_BLACK);
- printw("Waiting for connection on /tmp/citrun-gl.socket\n");
+ printw("Waiting for connection on /tmp/citrun.socket\n");
refresh();
af_unix *client = listen_sock.accept();
@@ -32,33 +36,85 @@ main(int argc, char *argv[])
RuntimeProcess conn(*client);
+ int fps = 0.;
+ int eps = 0;
+
+ int offset = 0;
+ int size_y, size_x;
+ getmaxyx(stdscr, size_y, size_x);
+
+ struct timespec floating_avg;
+ struct timespec last_frame;
+
+ std::queue<struct timespec> frame_deltas;
+ struct timespec sleep = { 0, 1 * 1000 * 1000 * 1000 / 60 };
+
+ clock_gettime(CLOCK_UPTIME, &last_frame);
+ uint64_t total_executions = 0;
+
while (1) {
erase();
-
conn.read_executions();
auto &t = conn.translation_units[0];
- std::vector<uint32_t>::iterator e = t.execution_counts.begin();
- std::vector<std::string>::iterator l = t.source.begin();
+ for (int i = offset; i < (size_y - 2); i++) {
+ uint32_t e = t.execution_counts[i + 1];
+ std::string l = t.source[i];
+
+ total_executions += e;
- e++;
- while (e != t.execution_counts.end() && l != t.source.end()) {
- if (*e > 10 * 1000)
+ if (e > 10 * 1000)
attron(COLOR_PAIR(1));
- printw("%s\n", l->c_str());
- if (*e > 10 * 1000)
+ else if (e > 1 * 1000)
+ attron(COLOR_PAIR(2));
+ else if (e > 0)
+ attron(COLOR_PAIR(3));
+ printw("%s\n", l.c_str());
+ if (e > 10 * 1000)
attroff(COLOR_PAIR(1));
- e++;
- l++;
+ else if (e > 1 * 1000)
+ attroff(COLOR_PAIR(2));
+ else if (e > 0)
+ attroff(COLOR_PAIR(3));
}
+ move(size_y - 1, 0);
+ clrtoeol();
+
+ printw("%s: [%i tus] [%i fps] [%i execs/s]\n",
+ conn.program_name.c_str(), conn.num_tus, fps, eps);
refresh();
- }
- printw("program name: %s\n", conn.program_name.c_str());
- printw("num tus: %i\n", conn.num_tus);
+ struct timespec tmp, delta;
+ clock_gettime(CLOCK_UPTIME, &tmp);
+
+ // Find out how long last frame took
+ timespecsub(&tmp, &last_frame, &delta);
+ last_frame = tmp;
+
+ timespecadd(&floating_avg, &delta, &floating_avg);
+ frame_deltas.push(delta);
+
+ if (frame_deltas.size() > 60) {
+ tmp = frame_deltas.front();
+ frame_deltas.pop();
+ timespecsub(&floating_avg, &tmp, &floating_avg);
+ }
+
+ fps = 60 * 1000 / (floating_avg.tv_sec * 1000 + floating_avg.tv_nsec / (1000 * 1000));
+ // eps = total_executions * 1000 / delta_ms;
+ // total_executions = 0;
+
+ struct timespec one = { 1, 0 };
+ struct timespec shift = { 0, 1000 * 1000 };
+ if (timespeccmp(&floating_avg, &one, <))
+ timespecadd(&sleep, &shift, &sleep);
+ else
+ timespecsub(&sleep, &shift, &sleep);
+
+ nanosleep(&sleep, NULL);
+ }
- getch();
endwin();
return 0;