mirror of
https://github.com/aristocratos/btop.git
synced 2024-09-29 22:51:35 +02:00
Changed: Removed unnecessary async threads in Runner thread
This commit is contained in:
parent
79a030ca82
commit
832699ffe6
225
src/btop.cpp
225
src/btop.cpp
@ -19,8 +19,6 @@ tab-size = 4
|
|||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
#include <future>
|
|
||||||
#include <bitset>
|
|
||||||
#include <numeric>
|
#include <numeric>
|
||||||
#include <ranges>
|
#include <ranges>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -41,7 +39,7 @@ tab-size = 4
|
|||||||
#include <btop_menu.hpp>
|
#include <btop_menu.hpp>
|
||||||
|
|
||||||
using std::string, std::string_view, std::vector, std::atomic, std::endl, std::cout, std::min, std::flush, std::endl;
|
using std::string, std::string_view, std::vector, std::atomic, std::endl, std::cout, std::min, std::flush, std::endl;
|
||||||
using std::string_literals::operator""s, std::to_string, std::future, std::async, std::bitset, std::future_status, std::mutex, std::lock_guard;
|
using std::string_literals::operator""s, std::to_string, std::mutex, std::lock_guard;
|
||||||
namespace fs = std::filesystem;
|
namespace fs = std::filesystem;
|
||||||
namespace rng = std::ranges;
|
namespace rng = std::ranges;
|
||||||
using namespace Tools;
|
using namespace Tools;
|
||||||
@ -200,8 +198,13 @@ void clean_quit(int sig) {
|
|||||||
if (Global::quitting) return;
|
if (Global::quitting) return;
|
||||||
Global::quitting = true;
|
Global::quitting = true;
|
||||||
Runner::stop();
|
Runner::stop();
|
||||||
if (Global::_runner_started and pthread_join(Runner::runner_id, NULL) != 0) {
|
if (Global::_runner_started) {
|
||||||
Logger::error("Failed to join _runner thread!");
|
struct timespec ts;
|
||||||
|
ts.tv_sec = 5;
|
||||||
|
if (pthread_timedjoin_np(Runner::runner_id, NULL, &ts) != 0) {
|
||||||
|
Logger::error("Failed to join _runner thread!");
|
||||||
|
pthread_cancel(Runner::runner_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Config::write();
|
Config::write();
|
||||||
@ -271,7 +274,6 @@ namespace Runner {
|
|||||||
atomic<bool> stopping (false);
|
atomic<bool> stopping (false);
|
||||||
atomic<bool> waiting (false);
|
atomic<bool> waiting (false);
|
||||||
atomic<bool> redraw (false);
|
atomic<bool> redraw (false);
|
||||||
// mutex runner_mtx;
|
|
||||||
|
|
||||||
//* Setup semaphore for triggering thread to do work
|
//* Setup semaphore for triggering thread to do work
|
||||||
#if __GNUC__ < 11
|
#if __GNUC__ < 11
|
||||||
@ -303,21 +305,6 @@ namespace Runner {
|
|||||||
sigset_t mask;
|
sigset_t mask;
|
||||||
pthread_t runner_id;
|
pthread_t runner_id;
|
||||||
pthread_mutex_t mtx;
|
pthread_mutex_t mtx;
|
||||||
// pthread_mutex_t active_mtx;
|
|
||||||
|
|
||||||
const unordered_flat_map<string, uint_fast8_t> box_bits = {
|
|
||||||
{"proc", 0b0000'0001},
|
|
||||||
{"net", 0b0000'0100},
|
|
||||||
{"mem", 0b0001'0000},
|
|
||||||
{"cpu", 0b0100'0000},
|
|
||||||
};
|
|
||||||
|
|
||||||
enum bit_pos {
|
|
||||||
proc_present, proc_running,
|
|
||||||
net_present, net_running,
|
|
||||||
mem_present, mem_running,
|
|
||||||
cpu_present, cpu_running
|
|
||||||
};
|
|
||||||
|
|
||||||
enum debug_actions {
|
enum debug_actions {
|
||||||
collect_begin,
|
collect_begin,
|
||||||
@ -330,16 +317,11 @@ namespace Runner {
|
|||||||
draw
|
draw
|
||||||
};
|
};
|
||||||
|
|
||||||
const uint_fast8_t proc_done = 0b0000'0011;
|
|
||||||
const uint_fast8_t net_done = 0b0000'1100;
|
|
||||||
const uint_fast8_t mem_done = 0b0011'0000;
|
|
||||||
const uint_fast8_t cpu_done = 0b1100'0000;
|
|
||||||
|
|
||||||
string debug_bg;
|
string debug_bg;
|
||||||
unordered_flat_map<string, array<uint64_t, 2>> debug_times;
|
unordered_flat_map<string, array<uint64_t, 2>> debug_times;
|
||||||
|
|
||||||
struct runner_conf {
|
struct runner_conf {
|
||||||
bitset<8> box_mask;
|
vector<string> boxes;
|
||||||
bool no_update;
|
bool no_update;
|
||||||
bool force_redraw;
|
bool force_redraw;
|
||||||
bool background_update;
|
bool background_update;
|
||||||
@ -415,128 +397,86 @@ namespace Runner {
|
|||||||
|
|
||||||
output.clear();
|
output.clear();
|
||||||
|
|
||||||
//* Start collection functions for all boxes in async threads and draw in this thread when finished
|
//* Run collection and draw functions for all boxes
|
||||||
//? Starting order below based on mean time to finish
|
|
||||||
try {
|
try {
|
||||||
future<Cpu::cpu_info&> cpu;
|
//? PROC
|
||||||
future<Mem::mem_info&> mem;
|
if (v_contains(conf.boxes, "proc")) {
|
||||||
future<Net::net_info&> net;
|
try {
|
||||||
future<vector<Proc::proc_info>&> proc;
|
if (Global::debug) debug_timer("proc", collect_begin);
|
||||||
|
|
||||||
//? Loop until all box flags present in bitmask have been zeroed
|
//? Start collect
|
||||||
while (conf.box_mask.count() > 0) {
|
auto proc = Proc::collect(conf.no_update);
|
||||||
if (stopping) break;
|
|
||||||
|
|
||||||
//? PROC
|
if (Global::debug) debug_timer("proc", draw_begin);
|
||||||
if (conf.box_mask.test(proc_present)) {
|
|
||||||
if (not conf.box_mask.test(proc_running)) {
|
|
||||||
if (Global::debug) debug_timer("proc", collect_begin);
|
|
||||||
|
|
||||||
//? Start async collect
|
//? Draw box
|
||||||
proc = async(Proc::collect, conf.no_update);
|
if (not pause_output) output += Proc::draw(proc, conf.force_redraw, conf.no_update);
|
||||||
conf.box_mask.set(proc_running);
|
|
||||||
}
|
|
||||||
else if (not proc.valid())
|
|
||||||
throw std::runtime_error("Proc::collect() future not valid.");
|
|
||||||
|
|
||||||
else if (proc.wait_for(10us) == future_status::ready) {
|
if (Global::debug) debug_timer("proc", draw_done);
|
||||||
try {
|
|
||||||
if (Global::debug) debug_timer("proc", draw_begin);
|
|
||||||
|
|
||||||
//? Draw box
|
|
||||||
if (not pause_output) output += Proc::draw(proc.get(), conf.force_redraw, conf.no_update);
|
|
||||||
|
|
||||||
if (Global::debug) debug_timer("proc", draw_done);
|
|
||||||
}
|
|
||||||
catch (const std::exception& e) {
|
|
||||||
throw std::runtime_error("Proc:: -> " + (string)e.what());
|
|
||||||
}
|
|
||||||
conf.box_mask ^= proc_done;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
//? NET
|
throw std::runtime_error("Proc:: -> " + (string)e.what());
|
||||||
if (conf.box_mask.test(net_present)) {
|
|
||||||
if (not conf.box_mask.test(net_running)) {
|
|
||||||
if (Global::debug) debug_timer("net", collect_begin);
|
|
||||||
|
|
||||||
//? Start async collect
|
|
||||||
net = async(Net::collect, conf.no_update);
|
|
||||||
conf.box_mask.set(net_running);
|
|
||||||
}
|
|
||||||
else if (not net.valid())
|
|
||||||
throw std::runtime_error("Net::collect() future not valid.");
|
|
||||||
|
|
||||||
else if (net.wait_for(10us) == future_status::ready) {
|
|
||||||
try {
|
|
||||||
if (Global::debug) debug_timer("net", draw_begin);
|
|
||||||
|
|
||||||
//? Draw box
|
|
||||||
if (not pause_output) output += Net::draw(net.get(), conf.force_redraw, conf.no_update);
|
|
||||||
|
|
||||||
if (Global::debug) debug_timer("net", draw_done);
|
|
||||||
}
|
|
||||||
catch (const std::exception& e) {
|
|
||||||
throw std::runtime_error("Net:: -> " + (string)e.what());
|
|
||||||
}
|
|
||||||
conf.box_mask ^= net_done;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//? MEM
|
|
||||||
if (conf.box_mask.test(mem_present)) {
|
|
||||||
if (not conf.box_mask.test(mem_running)) {
|
|
||||||
if (Global::debug) debug_timer("mem", collect_begin);
|
|
||||||
|
|
||||||
//? Start async collect
|
//? NET
|
||||||
mem = async(Mem::collect, conf.no_update);
|
if (v_contains(conf.boxes, "net")) {
|
||||||
conf.box_mask.set(mem_running);
|
try {
|
||||||
}
|
if (Global::debug) debug_timer("net", collect_begin);
|
||||||
else if (not mem.valid())
|
|
||||||
throw std::runtime_error("Mem::collect() future not valid.");
|
|
||||||
|
|
||||||
else if (mem.wait_for(10us) == future_status::ready) {
|
//? Start collect
|
||||||
try {
|
auto net = Net::collect(conf.no_update);
|
||||||
if (Global::debug) debug_timer("mem", draw_begin);
|
|
||||||
|
|
||||||
//? Draw box
|
if (Global::debug) debug_timer("net", draw_begin);
|
||||||
if (not pause_output) output += Mem::draw(mem.get(), conf.force_redraw, conf.no_update);
|
|
||||||
|
|
||||||
if (Global::debug) debug_timer("mem", draw_done);
|
//? Draw box
|
||||||
}
|
if (not pause_output) output += Net::draw(net, conf.force_redraw, conf.no_update);
|
||||||
catch (const std::exception& e) {
|
|
||||||
throw std::runtime_error("Mem:: -> " + (string)e.what());
|
if (Global::debug) debug_timer("net", draw_done);
|
||||||
}
|
|
||||||
conf.box_mask ^= mem_done;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
throw std::runtime_error("Net:: -> " + (string)e.what());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
//? CPU
|
//? MEM
|
||||||
if (conf.box_mask.test(cpu_present)) {
|
if (v_contains(conf.boxes, "mem")) {
|
||||||
if (not conf.box_mask.test(cpu_running)) {
|
try {
|
||||||
if (Global::debug) debug_timer("cpu", collect_begin);
|
if (Global::debug) debug_timer("mem", collect_begin);
|
||||||
|
|
||||||
//? Start async collect
|
//? Start collect
|
||||||
cpu = async(Cpu::collect, conf.no_update);
|
auto mem = Mem::collect(conf.no_update);
|
||||||
conf.box_mask.set(cpu_running);
|
|
||||||
}
|
|
||||||
else if (not cpu.valid())
|
|
||||||
throw std::runtime_error("Cpu::collect() future not valid.");
|
|
||||||
|
|
||||||
else if (cpu.wait_for(10us) == future_status::ready) {
|
if (Global::debug) debug_timer("mem", draw_begin);
|
||||||
try {
|
|
||||||
if (Global::debug) debug_timer("cpu", draw_begin);
|
|
||||||
|
|
||||||
//? Draw box
|
//? Draw box
|
||||||
if (not pause_output) output += Cpu::draw(cpu.get(), conf.force_redraw, conf.no_update);
|
if (not pause_output) output += Mem::draw(mem, conf.force_redraw, conf.no_update);
|
||||||
|
|
||||||
if (Global::debug) debug_timer("cpu", draw_done);
|
if (Global::debug) debug_timer("mem", draw_done);
|
||||||
}
|
}
|
||||||
catch (const std::exception& e) {
|
catch (const std::exception& e) {
|
||||||
throw std::runtime_error("Cpu:: -> " + (string)e.what());
|
throw std::runtime_error("Mem:: -> " + (string)e.what());
|
||||||
}
|
}
|
||||||
conf.box_mask ^= cpu_done;
|
}
|
||||||
}
|
|
||||||
|
//? CPU
|
||||||
|
if (v_contains(conf.boxes, "cpu")) {
|
||||||
|
try {
|
||||||
|
if (Global::debug) debug_timer("cpu", collect_begin);
|
||||||
|
|
||||||
|
//? Start collect
|
||||||
|
auto cpu = Cpu::collect(conf.no_update);
|
||||||
|
|
||||||
|
if (Global::debug) debug_timer("cpu", draw_begin);
|
||||||
|
|
||||||
|
//? Draw box
|
||||||
|
if (not pause_output) output += Cpu::draw(cpu, conf.force_redraw, conf.no_update);
|
||||||
|
|
||||||
|
if (Global::debug) debug_timer("cpu", draw_done);
|
||||||
|
}
|
||||||
|
catch (const std::exception& e) {
|
||||||
|
throw std::runtime_error("Cpu:: -> " + (string)e.what());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -617,13 +557,13 @@ namespace Runner {
|
|||||||
Config::unlock();
|
Config::unlock();
|
||||||
Config::lock();
|
Config::lock();
|
||||||
|
|
||||||
//? Setup bitmask for selected boxes and pass to _runner thread
|
current_conf = {
|
||||||
bitset<8> box_mask;
|
(box == "all" ? Config::current_boxes : vector{box}),
|
||||||
for (const auto& box : (box == "all" ? Config::current_boxes : vector{box})) {
|
no_update, force_redraw,
|
||||||
box_mask |= box_bits.at(box);
|
(not Config::getB("tty_mode") and Config::getB("background_update")),
|
||||||
}
|
Global::overlay,
|
||||||
|
Global::clock
|
||||||
current_conf = {box_mask, no_update, force_redraw, (not Config::getB("tty_mode") and Config::getB("background_update")), Global::overlay, Global::clock};
|
};
|
||||||
|
|
||||||
if (Menu::active and not current_conf.background_update) Global::overlay.clear();
|
if (Menu::active and not current_conf.background_update) Global::overlay.clear();
|
||||||
|
|
||||||
@ -646,9 +586,14 @@ namespace Runner {
|
|||||||
else if (ret == EBUSY) {
|
else if (ret == EBUSY) {
|
||||||
atomic_wait_for(active, true, 5000);
|
atomic_wait_for(active, true, 5000);
|
||||||
if (active) {
|
if (active) {
|
||||||
Global::exit_error_msg = "No response from Runner thread, quitting!";
|
|
||||||
active = false;
|
active = false;
|
||||||
exit(1);
|
if (Global::quitting) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
Global::exit_error_msg = "No response from Runner thread, quitting!";
|
||||||
|
exit(1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
thread_trigger();
|
thread_trigger();
|
||||||
atomic_wait_for(active, false, 100);
|
atomic_wait_for(active, false, 100);
|
||||||
|
Loading…
Reference in New Issue
Block a user