added gpu and gpu memory usage
This commit is contained in:
124
system_monitor.c
124
system_monitor.c
@@ -1,7 +1,7 @@
|
|||||||
#define _GNU_SOURCE
|
#define _GNU_SOURCE
|
||||||
#include "system_monitor.h"
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
#include "common.h"
|
#include "common.h"
|
||||||
|
#include "system_monitor.h"
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
#include <signal.h>
|
#include <signal.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
@@ -54,15 +54,18 @@ void recreate_windows(NCursesMonitor *monitor) {
|
|||||||
// Terminal too small, but still create basic windows
|
// Terminal too small, but still create basic windows
|
||||||
monitor->header_win = newwin(1, monitor->terminal_width, 0, 0);
|
monitor->header_win = newwin(1, monitor->terminal_width, 0, 0);
|
||||||
monitor->stats_win = newwin(1, monitor->terminal_width, 1, 0);
|
monitor->stats_win = newwin(1, monitor->terminal_width, 1, 0);
|
||||||
monitor->process_win = newwin(monitor->terminal_height - 4, monitor->terminal_width, 2, 0);
|
monitor->process_win =
|
||||||
monitor->status_win = newwin(2, monitor->terminal_width, monitor->terminal_height - 2, 0);
|
newwin(monitor->terminal_height - 4, monitor->terminal_width, 2, 0);
|
||||||
monitor->search_win = newwin(1, monitor->terminal_width, monitor->terminal_height - 1, 0);
|
monitor->status_win =
|
||||||
|
newwin(2, monitor->terminal_width, monitor->terminal_height - 2, 0);
|
||||||
|
monitor->search_win =
|
||||||
|
newwin(1, monitor->terminal_width, monitor->terminal_height - 1, 0);
|
||||||
} else {
|
} else {
|
||||||
// Normal size layout
|
// Normal size layout
|
||||||
monitor->header_win = newwin(3, monitor->terminal_width, 0, 0);
|
monitor->header_win = newwin(3, monitor->terminal_width, 0, 0);
|
||||||
monitor->stats_win = newwin(8, monitor->terminal_width, 3, 0);
|
monitor->stats_win = newwin(6, monitor->terminal_width, 3, 0);
|
||||||
monitor->process_win =
|
monitor->process_win =
|
||||||
newwin(monitor->terminal_height - 13, monitor->terminal_width, 11, 0);
|
newwin(monitor->terminal_height - 11, monitor->terminal_width, 9, 0);
|
||||||
monitor->status_win =
|
monitor->status_win =
|
||||||
newwin(2, monitor->terminal_width, monitor->terminal_height - 2, 0);
|
newwin(2, monitor->terminal_width, monitor->terminal_height - 2, 0);
|
||||||
monitor->search_win =
|
monitor->search_win =
|
||||||
@@ -150,13 +153,13 @@ int builtin_monitor(char **args) {
|
|||||||
display_ncurses_dashboard(&monitor, &stats, processes, proc_count);
|
display_ncurses_dashboard(&monitor, &stats, processes, proc_count);
|
||||||
|
|
||||||
int ch = getch();
|
int ch = getch();
|
||||||
|
|
||||||
// Check for resize after input
|
// Check for resize after input
|
||||||
if (monitor.resize_flag) {
|
if (monitor.resize_flag) {
|
||||||
recreate_windows(&monitor);
|
recreate_windows(&monitor);
|
||||||
continue; // Skip to next iteration to redraw everything
|
continue; // Skip to next iteration to redraw everything
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ch != ERR) {
|
if (ch != ERR) {
|
||||||
// Check if we're in search mode first - if so, let search handle ALL
|
// Check if we're in search mode first - if so, let search handle ALL
|
||||||
// input
|
// input
|
||||||
@@ -257,7 +260,7 @@ void cleanup_ncurses_monitor(NCursesMonitor *monitor) {
|
|||||||
// Restore default signal handler
|
// Restore default signal handler
|
||||||
signal(SIGWINCH, SIG_DFL);
|
signal(SIGWINCH, SIG_DFL);
|
||||||
global_monitor = NULL;
|
global_monitor = NULL;
|
||||||
|
|
||||||
endwin();
|
endwin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -362,6 +365,7 @@ void display_ncurses_dashboard(NCursesMonitor *monitor, SystemStats *stats,
|
|||||||
char mem_used_str[32], mem_total_str[32];
|
char mem_used_str[32], mem_total_str[32];
|
||||||
char disk_read_str[32], disk_write_str[32];
|
char disk_read_str[32], disk_write_str[32];
|
||||||
char net_rx_str[32], net_tx_str[32];
|
char net_rx_str[32], net_tx_str[32];
|
||||||
|
char gpu_used_str[32], gpu_total_str[32];
|
||||||
|
|
||||||
// Format data
|
// Format data
|
||||||
float mem_percent = (float)stats->memory_used / stats->memory_total * 100;
|
float mem_percent = (float)stats->memory_used / stats->memory_total * 100;
|
||||||
@@ -371,6 +375,12 @@ void display_ncurses_dashboard(NCursesMonitor *monitor, SystemStats *stats,
|
|||||||
format_bytes(stats->disk_write, disk_write_str);
|
format_bytes(stats->disk_write, disk_write_str);
|
||||||
format_bytes(stats->net_rx, net_rx_str);
|
format_bytes(stats->net_rx, net_rx_str);
|
||||||
format_bytes(stats->net_tx, net_tx_str);
|
format_bytes(stats->net_tx, net_tx_str);
|
||||||
|
|
||||||
|
// Format GPU memory if available
|
||||||
|
if (stats->gpu_memory_total > 0) {
|
||||||
|
format_bytes(stats->gpu_memory_used, gpu_used_str);
|
||||||
|
format_bytes(stats->gpu_memory_total, gpu_total_str);
|
||||||
|
}
|
||||||
|
|
||||||
// Clear all windows
|
// Clear all windows
|
||||||
werase(monitor->header_win);
|
werase(monitor->header_win);
|
||||||
@@ -399,62 +409,32 @@ void display_ncurses_dashboard(NCursesMonitor *monitor, SystemStats *stats,
|
|||||||
if (has_colors())
|
if (has_colors())
|
||||||
wattroff(monitor->stats_win, COLOR_PAIR(1));
|
wattroff(monitor->stats_win, COLOR_PAIR(1));
|
||||||
|
|
||||||
// CPU usage with progress bar
|
// horizontal layout with wrapping
|
||||||
mvwprintw(monitor->stats_win, 1, 2, "CPU Usage:");
|
|
||||||
if (has_colors())
|
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
mvwprintw(monitor->stats_win, 1, 15, "%5.1f%%", stats->cpu_percent);
|
|
||||||
if (has_colors())
|
|
||||||
wattroff(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
|
|
||||||
// Draw CPU progress bar
|
int col1_width = monitor->terminal_width / 2 - 2;
|
||||||
int bar_width = 30;
|
int col2_start = col1_width + 1;
|
||||||
int filled = (stats->cpu_percent * bar_width) / 100;
|
|
||||||
mvwprintw(monitor->stats_win, 1, 25, "[");
|
mvwprintw(monitor->stats_win, 1, 2, "CPU: %5.1f%%", stats->cpu_percent);
|
||||||
for (int i = 0; i < bar_width; i++) {
|
if (stats->gpu_percent > 0 || stats->gpu_memory_total > 0) {
|
||||||
if (i < filled) {
|
mvwprintw(monitor->stats_win, 1, col2_start, "GPU: %5.1f%%",
|
||||||
if (has_colors() && stats->cpu_percent > 80)
|
stats->gpu_percent);
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(4));
|
|
||||||
else if (has_colors() && stats->cpu_percent > 60)
|
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(3));
|
|
||||||
else if (has_colors())
|
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(1));
|
|
||||||
mvwaddch(monitor->stats_win, 1, 26 + i, '#');
|
|
||||||
if (has_colors())
|
|
||||||
wattroff(monitor->stats_win,
|
|
||||||
COLOR_PAIR(1) | COLOR_PAIR(3) | COLOR_PAIR(4));
|
|
||||||
} else {
|
|
||||||
mvwaddch(monitor->stats_win, 1, 26 + i, ' ');
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
mvwprintw(monitor->stats_win, 1, 56, "]");
|
|
||||||
|
|
||||||
// Memory usage
|
// second row: Memory and GPU memory
|
||||||
mvwprintw(monitor->stats_win, 2, 2, "Memory:");
|
|
||||||
if (has_colors())
|
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
mvwprintw(monitor->stats_win, 2, 15, "%5.1f%%", mem_percent);
|
|
||||||
mvwprintw(monitor->stats_win, 3, 15, "%s / %s", mem_used_str, mem_total_str);
|
|
||||||
if (has_colors())
|
|
||||||
wattroff(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
|
|
||||||
// Disk I/O
|
mvwprintw(monitor->stats_win, 2, 2, "MEM: %5.1f%% (%s/%s)", mem_percent,
|
||||||
mvwprintw(monitor->stats_win, 4, 2, "Disk I/O:");
|
mem_used_str, mem_total_str);
|
||||||
if (has_colors())
|
if (stats->gpu_memory_total > 0) {
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(2));
|
float gpu_mem_percent = (float)stats->gpu_memory_used / stats->gpu_memory_total * 100;
|
||||||
mvwprintw(monitor->stats_win, 4, 15, "Read: %s", disk_read_str);
|
mvwprintw(monitor->stats_win, 2, col2_start, "GPU MEM: %5.1f%% (%s/%s)",
|
||||||
mvwprintw(monitor->stats_win, 5, 15, "Write: %s", disk_write_str);
|
gpu_mem_percent, gpu_used_str, gpu_total_str);
|
||||||
if (has_colors())
|
}
|
||||||
wattroff(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
|
// Third row: Disk I/O
|
||||||
// Network I/O
|
mvwprintw(monitor->stats_win, 3, 2, "DISK: R:%s W:%s", disk_read_str, disk_write_str);
|
||||||
mvwprintw(monitor->stats_win, 6, 2, "Network:");
|
|
||||||
if (has_colors())
|
// Fourth row: Network I/O
|
||||||
wattron(monitor->stats_win, COLOR_PAIR(2));
|
mvwprintw(monitor->stats_win, 4, 2, "NET: RX:%s TX:%s", net_rx_str, net_tx_str);
|
||||||
mvwprintw(monitor->stats_win, 6, 15, "RX: %s", net_rx_str);
|
|
||||||
mvwprintw(monitor->stats_win, 7, 15, "TX: %s", net_tx_str);
|
|
||||||
if (has_colors())
|
|
||||||
wattroff(monitor->stats_win, COLOR_PAIR(2));
|
|
||||||
|
|
||||||
// Filter processes based on search in real-time
|
// Filter processes based on search in real-time
|
||||||
ProcessInfo *display_processes = processes;
|
ProcessInfo *display_processes = processes;
|
||||||
@@ -727,6 +707,28 @@ void get_system_stats(SystemStats *stats) {
|
|||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gpu usage
|
||||||
|
|
||||||
|
stats->gpu_percent = 0.0;
|
||||||
|
stats->gpu_memory_used = 0;
|
||||||
|
stats->gpu_memory_total = 0;
|
||||||
|
|
||||||
|
FILE *gpu_file =
|
||||||
|
popen("nvidia-smi --query-gpu=utilization.gpu,memory.used,memory.total "
|
||||||
|
"--format=csv,noheader,nounits 2>/dev/null",
|
||||||
|
"r");
|
||||||
|
|
||||||
|
if (gpu_file) {
|
||||||
|
char line[256];
|
||||||
|
if (fgets(line, sizeof(line), gpu_file)) {
|
||||||
|
sscanf(line, "%f, %lu, %lu", &stats->gpu_percent, &stats->gpu_memory_used,
|
||||||
|
&stats->gpu_memory_total);
|
||||||
|
stats->gpu_memory_used *= 1024 * 1024; // Convert MB to bytes
|
||||||
|
stats->gpu_memory_total *= 1024 * 1024; // Convert MB to bytes
|
||||||
|
}
|
||||||
|
pclose(gpu_file);
|
||||||
|
}
|
||||||
|
|
||||||
// Memory Usage - Read from /proc/meminfo for accuracy
|
// Memory Usage - Read from /proc/meminfo for accuracy
|
||||||
fp = fopen("/proc/meminfo", "r");
|
fp = fopen("/proc/meminfo", "r");
|
||||||
if (fp) {
|
if (fp) {
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
#ifndef SYSTEM_MONITOR_H
|
#ifndef SYSTEM_MONITOR_H
|
||||||
#define SYSTEM_MONITOR_H
|
#define SYSTEM_MONITOR_H
|
||||||
|
|
||||||
#include <signal.h>
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <dirent.h>
|
#include <dirent.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <ncurses.h>
|
#include <ncurses.h>
|
||||||
|
#include <signal.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -17,6 +17,9 @@
|
|||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
float cpu_percent;
|
float cpu_percent;
|
||||||
|
float gpu_percent;
|
||||||
|
unsigned long gpu_memory_used;
|
||||||
|
unsigned long gpu_memory_total;
|
||||||
unsigned long memory_used;
|
unsigned long memory_used;
|
||||||
unsigned long memory_total;
|
unsigned long memory_total;
|
||||||
unsigned long disk_read;
|
unsigned long disk_read;
|
||||||
|
|||||||
BIN
system_monitor.o
BIN
system_monitor.o
Binary file not shown.
Reference in New Issue
Block a user