This commit is contained in:
aristocratos 2021-05-26 16:23:29 +02:00
parent 342f0af6a8
commit e7cbc28960
5 changed files with 37 additions and 19 deletions

View file

@ -1,12 +1,14 @@
PREFIX ?= /usr/local
DOCDIR ?= $(PREFIX)/share/btop/doc
CXX = g++
override CXXFLAGS += -std=c++20 -O3 -pthread -Wall -Wextra -pedantic
CPP = g++
CPPFLAGS = -std=c++20 -pthread
OPTFLAG = -O3
INFOFLAGS += -Wall -Wextra -Wno-stringop-overread -pedantic
INCLUDES = -I./src
btop: btop.cpp
@mkdir -p bin
$(CXX) $(CXXFLAGS) $(INCLUDES) -o bin/btop btop.cpp
$(CPP) $(CPPFLAGS) $(INCLUDES) $(OPTFLAG) $(INFOFLAGS) -o bin/btop btop.cpp
install:
@mkdir -p $(DESTDIR)$(PREFIX)/bin

View file

@ -329,7 +329,7 @@ int main(int argc, char **argv){
greyscale.push_back(Theme::dec_to_color(xc, xc, xc));
}
string pbox = Box::draw(Box::Conf(0, 10, Term::width, Term::height - 16, Theme::c("proc_box"), "testbox", "below", true, 7));
string pbox = Draw::createBox({.x = 0, .y = 10, .width = Term::width, .height = Term::height - 16, .line_color = Theme::c("proc_box"), .title = "testbox", .title2 = "below", .num = 7});
pbox += rjust("Pid:", 8) + " " + ljust("Program:", 16) + " " + ljust("Command:", Term::width - 69) + " Threads: " +
ljust("User:", 10) + " " + rjust("MemB", 5) + " " + rjust("Cpu%", 14) + "\n";
@ -356,8 +356,10 @@ int main(int argc, char **argv){
avgtimes.push_front(timestamp);
if (avgtimes.size() > 100) avgtimes.pop_back();
cout << pbox << ostring << Fx::reset << "\n" << endl;
cout << Mv::to(Term::height - 4, 1) << "Processes call took: " << rjust(to_string(timestamp), 4) << "ms. Average: " << rjust(to_string(accumulate(avgtimes.begin(), avgtimes.end(), 0) / avgtimes.size()), 3) <<
"ms of " << avgtimes.size() << " samples. Drawing took: " << time_ms() - timestamp2 << "ms. Number of processes: " << Proc::numpids << ". Run count: " << ++rcount << ". Time: " << strf_time("%X ") << endl;
cout << Mv::to(Term::height - 4, 1) << "Processes call took: " << rjust(to_string(timestamp), 4) << "ms. Average: " <<
rjust(to_string(accumulate(avgtimes.begin(), avgtimes.end(), 0) / avgtimes.size()), 3) << "ms of " << avgtimes.size() <<
" samples. Drawing took: " << time_ms() - timestamp2 << "ms.\nNumber of processes: " << Proc::numpids << ". Run count: " <<
++rcount << ". Time: " << strf_time("%X ") << endl;
while (time_ms() < tsl) {
if (Input::poll(tsl - time_ms())) key = Input::get();

View file

@ -31,9 +31,9 @@ tab-size = 4
using std::string, std::vector, std::map, std::round, std::views::iota;
namespace Box {
namespace Draw {
struct Conf {
struct BoxConf {
uint x=0, y=0;
uint width=0, height=0;
string line_color = "", title = "", title2 = "";
@ -41,8 +41,7 @@ namespace Box {
uint num=0;
};
string draw(Conf c){
string createBox(BoxConf c){
string out;
string lcolor = (c.line_color.empty()) ? Theme::c("div_line") : c.line_color;
string numbering = (c.num == 0) ? "" : Theme::c("hi_fg") + Symbols::superscript[c.num];
@ -80,11 +79,23 @@ namespace Box {
return out + Fx::reset + Mv::to(c.y + 1, c.x + 2);
}
class Meter {
string out, color_gradient, color_inactive;
};
}
namespace Box {
}
namespace Proc {
// Box::Conf box;
// Draw::BoxConf box;
}

View file

@ -23,9 +23,13 @@ tab-size = 4
#include <vector>
#include <deque>
#include <array>
#include <map>
#include <unordered_map>
#include <atomic>
#include <future>
#include <thread>
#include <fstream>
#include <streambuf>
#include <filesystem>
#include <ranges>
#include <list>
@ -37,7 +41,7 @@ tab-size = 4
#include <btop_tools.h>
using std::string, std::vector, std::array, std::ifstream, std::atomic, std::numeric_limits, std::streamsize, std::unordered_map, std::deque, std::list;
using std::string, std::vector, std::array, std::ifstream, std::atomic, std::numeric_limits, std::streamsize, std::map, std::unordered_map, std::deque, std::list;
using std::cout, std::flush, std::endl;
namespace fs = std::filesystem;
using namespace Tools;
@ -200,7 +204,7 @@ namespace Proc {
user = (!uid.empty() && uid_user.contains(uid)) ? uid_user.at(uid) : uid;
}
else continue;
cache[pid] = p_cache(name, cmd, user);
cache[pid] = {name, cmd, user};
}
//* Match filter if defined
@ -221,6 +225,7 @@ namespace Proc {
//? Skip pid and comm field and find comm fields closing ')'
s_pos = instr.find_last_of(')') + 2;
if (s_pos == string::npos) continue;
do {
c_pos = instr.find(' ', s_pos);
@ -266,7 +271,7 @@ namespace Proc {
s_pos = c_pos + 1;
} while (s_count++ < 36);
if (s_count < 20) continue;
if (s_count < 19) continue;
//? Process cpu usage since last update, 100'000 because (100 percent * 1000 milliseconds) for correct conversion
cpu = static_cast<double>(100000 * (cpu_t - cache[pid].cpu_t) / since_last) / clk_tck;
@ -289,7 +294,7 @@ namespace Proc {
}
//* Create proc_info
procs.push_back(proc_info(pid, cache[pid].name, cache[pid].cmd, threads, cache[pid].user, rss_mem, cpu, cpu_s, state, cpu_n, p_nice, ppid));
procs.push_back({pid, cache[pid].name, cache[pid].cmd, threads, cache[pid].user, rss_mem, cpu, cpu_s, state, cpu_n, p_nice, ppid});
}
}
@ -331,7 +336,6 @@ namespace Proc {
}
else cache.clear();
}
tstamp = time_ms();
running.store(false);
return procs;

View file

@ -470,8 +470,7 @@ namespace Logger {
void log_write(uint level, string& msg){
if (logfile.empty() || loglevel < level) return;
while (busy.load()) Tools::sleep_ms(1);
busy.store(true);
busy.wait(true); busy.store(true);
if (fs::exists(logfile) && fs::file_size(logfile) > 1024 << 10) {
auto old_log = logfile;
old_log += ".1";
@ -479,7 +478,7 @@ namespace Logger {
fs::rename(logfile, old_log);
}
std::ofstream lwrite(logfile, std::ios::app);
if (first) { first = false; lwrite << "\n" << Tools::strf_time(tdf) << "----> btop++ v." << Global::Version << "\n";}
if (first) { first = false; lwrite << "\n" << Tools::strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";}
lwrite << Tools::strf_time(tdf) << log_levels[level] << ": " << msg << "\n";
lwrite.close();
busy.store(false);