Removed bad goto and added 100 microseconds sleep in thread manager loop to spare some cpu cycles

This commit is contained in:
aristocratos 2021-08-11 23:21:33 +02:00
parent f9ed675d47
commit ced3d47ebe
4 changed files with 209 additions and 198 deletions

View File

@ -347,10 +347,10 @@ namespace Runner {
//* Start collection functions for all boxes in async threads and draw in this thread when finished
//? Starting order below based on mean time to finish
try {
while (box_mask.count() > 0) {
if (stopping) break;
try {
//* PROC
//? PROC
if (box_mask.test(proc_present)) {
if (not box_mask.test(proc_running)) {
proc = async(Proc::collect, conf->no_update);
@ -367,9 +367,10 @@ namespace Runner {
throw std::runtime_error("Proc:: -> " + (string)e.what());
}
box_mask ^= proc_done;
continue;
}
}
//* MEM
//? MEM
if (box_mask.test(mem_present)) {
if (not box_mask.test(mem_running)) {
mem = async(Mem::collect, conf->no_update);
@ -386,9 +387,10 @@ namespace Runner {
throw std::runtime_error("Mem:: -> " + (string)e.what());
}
box_mask ^= mem_done;
continue;
}
}
//* NET
//? NET
if (box_mask.test(net_present)) {
if (not box_mask.test(net_running)) {
net = async(Net::collect, conf->no_update);
@ -405,9 +407,10 @@ namespace Runner {
throw std::runtime_error("Net:: -> " + (string)e.what());
}
box_mask ^= net_done;
continue;
}
}
//* CPU
//? CPU
if (box_mask.test(cpu_present)) {
if (not box_mask.test(cpu_running)) {
cpu = async(Cpu::collect, conf->no_update);
@ -424,16 +427,17 @@ namespace Runner {
throw std::runtime_error("Cpu:: -> " + (string)e.what());
}
box_mask ^= cpu_done;
continue;
}
}
sleep_micros(100);
}
}
catch (const std::exception& e) {
Global::exit_error_msg = "Exception in runner thread -> " + (string)e.what();
Global::thread_exception = true;
Input::interrupt = true;
stopping = true;
break;
}
}
if (stopping) {

View File

@ -962,8 +962,9 @@ namespace Proc {
if (item_fit >= 4) out += cjust("IO/W:", item_width);
if (item_fit >= 5) out += cjust("Parent:", item_width);
if (item_fit >= 6) out += cjust("User:", item_width);
if (item_fit >= 7) out += cjust("Nice:", item_width);
if (item_fit >= 8) out += cjust("Threads:", item_width);
if (item_fit >= 7) out += cjust("Threads:", item_width);
if (item_fit >= 8) out += cjust("Nice:", item_width);
//? Command line
for (int i = 0; const auto& l : {'C', 'M', 'D'})
@ -1084,8 +1085,9 @@ namespace Proc {
if (item_fit >= 4) out += cjust(detailed.io_write, item_width);
if (item_fit >= 5) out += cjust(detailed.parent, item_width, true);
if (item_fit >= 6) out += cjust(detailed.entry.user, item_width, true);
if (item_fit >= 7) out += cjust(to_string(detailed.entry.p_nice), item_width);
if (item_fit >= 8) out += cjust(to_string(detailed.entry.threads), item_width);
if (item_fit >= 7) out += cjust(to_string(detailed.entry.threads), item_width);
if (item_fit >= 8) out += cjust(to_string(detailed.entry.p_nice), item_width);
const double mem_p = (double)detailed.mem_bytes.back() * 100 / Shared::totalMem;
string mem_str = to_string(mem_p);

View File

@ -1000,12 +1000,14 @@ namespace Proc {
procs.reserve(current_procs.size() + 10);
const int cmult = (per_core) ? Shared::coreCount : 1;
bool got_detailed = false;
//* Use pids from last update if only changing filter, sorting or tree options
if (no_update and not cache.empty()) {
procs = current_procs;
if (show_detailed and detailed_pid != detailed.last_pid) _collect_details(detailed_pid, round(uptime), procs);
goto proc_no_update;
}
//* ---------------------------------------------Collection start----------------------------------------------
else {
//? Update uid_user map if /etc/passwd changed since last run
if (not Shared::passwd_path.empty() and fs::last_write_time(Shared::passwd_path) != passwd_time) {
string r_uid, r_user;
@ -1027,7 +1029,7 @@ namespace Proc {
pread.close();
}
//* Get cpu total times from /proc/stat
//? Get cpu total times from /proc/stat
cputimes = 0;
pread.open(Shared::procPath / "stat");
if (pread.good()) {
@ -1037,7 +1039,7 @@ namespace Proc {
}
else throw std::runtime_error("Failure to read /proc/stat");
//* Iterate over all pids in /proc
//? Iterate over all pids in /proc
for (const auto& d: fs::directory_iterator(Shared::procPath)) {
if (Runner::stopping)
return procs;
@ -1048,7 +1050,7 @@ namespace Proc {
proc_info new_proc (stoul(pid_str));
//* Cache program name, command and username
//? Cache program name, command and username
if (not cache.contains(new_proc.pid)) {
string name, cmd, user;
pread.open(d.path() / "comm");
@ -1088,7 +1090,7 @@ namespace Proc {
new_proc.cmd = cache.at(new_proc.pid).cmd;
new_proc.user = cache.at(new_proc.pid).user;
//* Parse /proc/[pid]/stat
//? Parse /proc/[pid]/stat
pread.open(d.path() / "stat");
if (not pread.good()) continue;
@ -1183,7 +1185,7 @@ namespace Proc {
}
//* Clear dead processes from cache at a regular interval
//? Clear dead processes from cache at a regular interval
if (++counter >= 1000 or (cache.size() > procs.size() + 100)) {
counter = 0;
unordered_flat_map<size_t, p_cache> r_cache;
@ -1195,7 +1197,7 @@ namespace Proc {
cache = std::move(r_cache);
}
//* Update the details info box for process if active
//? Update the details info box for process if active
if (show_detailed and got_detailed) {
_collect_details(detailed_pid, round(uptime), procs);
}
@ -1206,8 +1208,8 @@ namespace Proc {
old_cputimes = cputimes;
current_procs = procs;
proc_no_update:
}
//* ---------------------------------------------Collection done-----------------------------------------------
//* Match filter if defined
if (not tree and not filter.empty()) {

View File

@ -229,6 +229,9 @@ namespace Tools {
//* Put current thread to sleep for <ms> milliseconds
inline void sleep_ms(const size_t& ms) { std::this_thread::sleep_for(std::chrono::milliseconds(ms)); }
//* Put current thread to sleep for <micros> microseconds
inline void sleep_micros(const size_t& micros) { std::this_thread::sleep_for(std::chrono::microseconds(micros)); }
//* Left justify string <str> if <x> is greater than <str> length, limit return size to <x> by default
string ljust(string str, const size_t x, const bool utf=false, const bool wide=false, const bool limit=true);