monitor/cpu-dashboard (C): btop-style CPU dashboard app
XCoreGrid + XArcGauge + XHistoryGraph + XLoadBadge + XUsageBar in one 1024x768 window, live /proc/stat + /proc/loadavg sampled every 500 ms. Seeded demo histories are primed out with the first live sample.
This commit is contained in:
1 parent
e474894bae
commit
c6edacbe89
4 files changed
+349
No files matched your search
@@ -0,0 +1,22 @@
|
||||
CXX ?= g++
|
||||
CXXFLAGS ?= -std=c++17 -O2 -Wall -Wextra
|
||||
|
||||
# One -I per widget dir: each widget header includes "core/xframe_core.h"
|
||||
# from its own component directory.
|
||||
INCLUDES := -I../../../widgets/components/XCoreGrid \
|
||||
-I../../../widgets/components/XArcGauge \
|
||||
-I../../../widgets/components/XHistoryGraph \
|
||||
-I../../../widgets/components/XLoadBadge \
|
||||
-I../../../widgets/components/XUsageBar
|
||||
|
||||
LDFLAGS := -lX11
|
||||
|
||||
all: cpu-dashboard
|
||||
|
||||
cpu-dashboard: main.cpp
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) main.cpp -o cpu-dashboard $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
rm -f cpu-dashboard
|
||||
|
||||
.PHONY: all clean
|
||||
@@ -0,0 +1,36 @@
|
||||
# CPU Dashboard — monitor (C)
|
||||
|
||||
A btop-style CPU dashboard in one 1024x768 window, built only from
|
||||
LightGUI XFrame monitor widgets:
|
||||
|
||||
- **XCoreGrid** — per-core mini history graphs (8 cores on hot_dallas)
|
||||
- **XArcGauge** — big total-CPU % gauge (warn 70%, crit 90%)
|
||||
- **XHistoryGraph** — total CPU % over the last 60 s
|
||||
- **XLoadBadge** — load average 1/5/15 min + uptime
|
||||
- **XUsageBar** — total CPU % bar
|
||||
|
||||
Live data comes from `/proc/stat` (per-core busy %) and
|
||||
`/proc/loadavg` (via the badge's own `refresh()`), sampled every
|
||||
500 ms. The seeded demo data each widget preloads is scrolled out at
|
||||
startup by priming the histories with the first live sample.
|
||||
|
||||
## Build
|
||||
|
||||
```sh
|
||||
make
|
||||
```
|
||||
|
||||
## Run
|
||||
|
||||
```sh
|
||||
./cpu-dashboard
|
||||
```
|
||||
|
||||
Esc, `q`, or closing the window quits.
|
||||
|
||||
## Source
|
||||
|
||||
- App: `examples/monitor/cpu-dashboard/main.cpp`
|
||||
- Widgets: `widgets/components/XCoreGrid`, `XArcGauge`,
|
||||
`XHistoryGraph`, `XLoadBadge`, `XUsageBar`
|
||||
- Repo: https://github.com/cnmcdee/lightgui
|
||||
@@ -0,0 +1,291 @@
|
||||
// CPU Dashboard - monitor: a btop-style CPU dashboard built ONLY from
|
||||
// LightGUI XFrame monitor widgets (XCoreGrid, XArcGauge, XHistoryGraph,
|
||||
// XLoadBadge, XUsageBar). Live data from /proc/stat + /proc/loadavg,
|
||||
// refreshed every 500 ms. No raw X11 drawing for UI elements: the app
|
||||
// shell only opens the window and calls each widget's draw().
|
||||
//
|
||||
// Build: make
|
||||
// Run: ./cpu-dashboard (Esc / q / window-close quits)
|
||||
|
||||
#include "XArcGauge.h"
|
||||
#include "XCoreGrid.h"
|
||||
#include "XHistoryGraph.h"
|
||||
#include "XLoadBadge.h"
|
||||
#include "XUsageBar.h"
|
||||
|
||||
#include <X11/Xlib.h>
|
||||
#include <X11/keysym.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <sys/select.h>
|
||||
#include <sys/time.h>
|
||||
#include <unistd.h>
|
||||
|
||||
namespace
|
||||
{
|
||||
|
||||
struct CpuTimes
|
||||
{
|
||||
long long idle = 0;
|
||||
long long total = 0;
|
||||
};
|
||||
|
||||
bool read_cpu_times(std::vector<CpuTimes>& per_core, CpuTimes& total)
|
||||
{
|
||||
FILE* f = std::fopen("/proc/stat", "r");
|
||||
if (!f) return false;
|
||||
char line[1024];
|
||||
per_core.clear();
|
||||
bool ok = false;
|
||||
while (std::fgets(line, sizeof(line), f))
|
||||
{
|
||||
if (std::strncmp(line, "cpu", 3) != 0) continue;
|
||||
char* p = line + 3;
|
||||
bool is_total = (*p == ' ');
|
||||
if (!is_total && (*p < '0' || *p > '9')) continue;
|
||||
long long nums[16] = {0};
|
||||
int n = std::sscanf(p, "%lld %lld %lld %lld %lld %lld %lld %lld "
|
||||
"%lld %lld %lld %lld %lld %lld %lld %lld",
|
||||
&nums[0], &nums[1], &nums[2], &nums[3],
|
||||
&nums[4], &nums[5], &nums[6], &nums[7],
|
||||
&nums[8], &nums[9], &nums[10], &nums[11],
|
||||
&nums[12], &nums[13], &nums[14], &nums[15]);
|
||||
if (n < 4) continue;
|
||||
CpuTimes t;
|
||||
t.total = 0;
|
||||
for (int i = 0; i < n; ++i) t.total += nums[i];
|
||||
t.idle = nums[3] + (n > 4 ? nums[4] : 0);
|
||||
if (is_total) total = t;
|
||||
else per_core.push_back(t);
|
||||
ok = true;
|
||||
}
|
||||
std::fclose(f);
|
||||
return ok && !per_core.empty();
|
||||
}
|
||||
|
||||
double busy_pct(const CpuTimes& prev, const CpuTimes& now)
|
||||
{
|
||||
long long d_total = now.total - prev.total;
|
||||
long long d_idle = now.idle - prev.idle;
|
||||
if (d_total <= 0) return 0.0;
|
||||
double v = 100.0 * (1.0 - (double)d_idle / (double)d_total);
|
||||
if (v < 0.0) v = 0.0;
|
||||
if (v > 100.0) v = 100.0;
|
||||
return v;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main()
|
||||
{
|
||||
Display* display = XOpenDisplay(nullptr);
|
||||
if (!display) return 1;
|
||||
int screen = DefaultScreen(display);
|
||||
Window window = XCreateSimpleWindow(display, RootWindow(display, screen),
|
||||
100, 100, 1024, 768, 1,
|
||||
BlackPixel(display, screen),
|
||||
WhitePixel(display, screen));
|
||||
XStoreName(display, window, "CPU Dashboard - monitor");
|
||||
XSelectInput(display, window,
|
||||
ExposureMask | KeyPressMask | StructureNotifyMask |
|
||||
ButtonPressMask | ButtonReleaseMask | PointerMotionMask);
|
||||
Atom deleteWindowMessage = XInternAtom(display, "WM_DELETE_WINDOW", False);
|
||||
XSetWMProtocols(display, window, &deleteWindowMessage, 1);
|
||||
XMapWindow(display, window);
|
||||
GC gc = DefaultGC(display, screen);
|
||||
SStyle_Manager styles(display, window, screen, gc);
|
||||
styles.add_style_ex("default", Srgb{225, 225, 225}, Srgb{120, 120, 120},
|
||||
Srgb{60, 60, 60}, Srgb{20, 20, 20}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.add_style_ex("dark", Srgb{45, 45, 48}, Srgb{20, 20, 22},
|
||||
Srgb{200, 200, 200}, Srgb{240, 240, 240}, 6, 1,
|
||||
"-misc-fixed-medium-r-normal--13-*");
|
||||
styles.set_active_style_byName("default");
|
||||
|
||||
int ncores = (int)sysconf(_SC_NPROCESSORS_ONLN);
|
||||
if (ncores < 1) ncores = 8;
|
||||
|
||||
XLoadBadge load(display, screen, window, gc, &styles, 16, 16, 320, 84);
|
||||
load.set_text("Load avg");
|
||||
|
||||
XUsageBar total_bar(display, screen, window, gc, &styles, 352, 16, 656, 84);
|
||||
total_bar.set_text("Total CPU");
|
||||
total_bar.set_max(100.0);
|
||||
|
||||
XCoreGrid grid(display, screen, window, gc, &styles, 16, 116, 640, 300);
|
||||
grid.set_text("CPU cores");
|
||||
grid.set_core_count(ncores);
|
||||
|
||||
XArcGauge gauge(display, screen, window, gc, &styles, 672, 116, 336, 300);
|
||||
gauge.set_text("CPU");
|
||||
gauge.set_zones(70.0, 90.0);
|
||||
|
||||
XHistoryGraph history(display, screen, window, gc, &styles, 16, 432, 992, 320);
|
||||
history.set_text("Total CPU history");
|
||||
history.clear_series();
|
||||
history.add_series("CPU", Srgb{0, 170, 70});
|
||||
history.set_y_range(0.0, 100.0);
|
||||
history.set_window_seconds(60);
|
||||
|
||||
auto redraw = [&]() {
|
||||
XClearWindow(display, window);
|
||||
load.draw();
|
||||
total_bar.draw();
|
||||
grid.draw();
|
||||
gauge.draw();
|
||||
history.draw();
|
||||
XFlush(display);
|
||||
};
|
||||
|
||||
// Prime with one live sample so the seeded demo data is fully
|
||||
// scrolled out before the first paint: 60 pushes fill the core
|
||||
// grid (capacity 60), 120 fill the 60 s history window.
|
||||
std::vector<CpuTimes> prev_cores, now_cores;
|
||||
CpuTimes prev_total, now_total;
|
||||
bool have_prev = read_cpu_times(prev_cores, prev_total);
|
||||
if (have_prev)
|
||||
{
|
||||
usleep(100000);
|
||||
if (read_cpu_times(now_cores, now_total) &&
|
||||
now_cores.size() == prev_cores.size())
|
||||
{
|
||||
std::vector<double> per_core;
|
||||
double sum = 0.0;
|
||||
for (size_t i = 0; i < now_cores.size(); ++i)
|
||||
{
|
||||
double v = busy_pct(prev_cores[i], now_cores[i]);
|
||||
per_core.push_back(v);
|
||||
sum += v;
|
||||
}
|
||||
double total = per_core.empty()
|
||||
? 0.0
|
||||
: sum / (double)per_core.size();
|
||||
for (int i = 0; i < 60; ++i) grid.push_all(per_core);
|
||||
for (int i = 0; i < 120; ++i) history.push(0, total);
|
||||
gauge.set_value(total);
|
||||
total_bar.set_value(total);
|
||||
prev_cores = now_cores;
|
||||
prev_total = now_total;
|
||||
}
|
||||
}
|
||||
load.refresh();
|
||||
redraw();
|
||||
|
||||
int fd = ConnectionNumber(display);
|
||||
bool running = true;
|
||||
while (running)
|
||||
{
|
||||
fd_set rfds;
|
||||
FD_ZERO(&rfds);
|
||||
FD_SET(fd, &rfds);
|
||||
struct timeval tv;
|
||||
tv.tv_sec = 0;
|
||||
tv.tv_usec = 500000;
|
||||
int ready = select(fd + 1, &rfds, nullptr, nullptr, &tv);
|
||||
if (ready > 0)
|
||||
{
|
||||
while (XPending(display))
|
||||
{
|
||||
XEvent event;
|
||||
XNextEvent(display, &event);
|
||||
bool handled = false;
|
||||
switch (event.type)
|
||||
{
|
||||
case Expose:
|
||||
handled = true;
|
||||
break;
|
||||
case ButtonPress:
|
||||
handled =
|
||||
grid.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
gauge.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
history.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
load.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
total_bar.on_button_press(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case ButtonRelease:
|
||||
handled =
|
||||
grid.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
gauge.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
history.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
load.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button) ||
|
||||
total_bar.on_button_release(event.xbutton.x, event.xbutton.y,
|
||||
event.xbutton.button);
|
||||
break;
|
||||
case MotionNotify:
|
||||
handled = grid.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
gauge.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
history.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
load.on_motion(event.xmotion.x, event.xmotion.y) ||
|
||||
total_bar.on_motion(event.xmotion.x, event.xmotion.y);
|
||||
break;
|
||||
case KeyPress:
|
||||
{
|
||||
char buffer[32];
|
||||
KeySym key = 0;
|
||||
int n = XLookupString(&event.xkey, buffer,
|
||||
(int)sizeof(buffer) - 1, &key, nullptr);
|
||||
buffer[n] = 0;
|
||||
if (key == XK_Escape || (n == 1 && buffer[0] == 'q'))
|
||||
running = false;
|
||||
handled = grid.on_key(key, buffer) ||
|
||||
gauge.on_key(key, buffer) ||
|
||||
history.on_key(key, buffer) ||
|
||||
load.on_key(key, buffer) ||
|
||||
total_bar.on_key(key, buffer);
|
||||
break;
|
||||
}
|
||||
case ClientMessage:
|
||||
if ((Atom)event.xclient.data.l[0] == deleteWindowMessage)
|
||||
running = false;
|
||||
break;
|
||||
}
|
||||
if (handled) redraw();
|
||||
if (!running) break;
|
||||
}
|
||||
}
|
||||
else if (ready == 0)
|
||||
{
|
||||
// 500 ms tick: live sample from /proc/stat + /proc/loadavg.
|
||||
if (have_prev && read_cpu_times(now_cores, now_total) &&
|
||||
now_cores.size() == prev_cores.size())
|
||||
{
|
||||
std::vector<double> per_core;
|
||||
double sum = 0.0;
|
||||
for (size_t i = 0; i < now_cores.size(); ++i)
|
||||
{
|
||||
double v = busy_pct(prev_cores[i], now_cores[i]);
|
||||
per_core.push_back(v);
|
||||
sum += v;
|
||||
}
|
||||
double total = per_core.empty()
|
||||
? 0.0
|
||||
: sum / (double)per_core.size();
|
||||
grid.push_all(per_core);
|
||||
history.push(0, total);
|
||||
gauge.set_value(total);
|
||||
total_bar.set_value(total);
|
||||
prev_cores = now_cores;
|
||||
prev_total = now_total;
|
||||
}
|
||||
load.refresh();
|
||||
redraw();
|
||||
}
|
||||
}
|
||||
|
||||
XCloseDisplay(display);
|
||||
return 0;
|
||||
}
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
Reference in new issue
Block a user