2021-05-11 09:46:41 +12:00
|
|
|
/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
indent = tab
|
|
|
|
tab-size = 4
|
|
|
|
*/
|
|
|
|
|
2021-06-20 00:57:27 +12:00
|
|
|
#pragma once
|
2021-05-11 09:46:41 +12:00
|
|
|
|
2022-10-03 07:52:27 +13:00
|
|
|
#include <array>
|
2021-06-20 10:49:13 +12:00
|
|
|
#include <atomic>
|
2021-07-04 11:18:48 +12:00
|
|
|
#include <deque>
|
2022-10-03 07:52:27 +13:00
|
|
|
#include <filesystem>
|
|
|
|
#include <string>
|
2021-09-18 00:25:54 +12:00
|
|
|
#include <tuple>
|
2022-10-03 07:52:27 +13:00
|
|
|
#include <vector>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <robin_hood.h>
|
2021-11-03 09:10:41 +13:00
|
|
|
#include <unistd.h>
|
2021-05-20 09:21:56 +12:00
|
|
|
|
2022-10-03 07:52:27 +13:00
|
|
|
using robin_hood::unordered_flat_map;
|
|
|
|
using std::array;
|
|
|
|
using std::atomic;
|
|
|
|
using std::deque;
|
|
|
|
using std::string;
|
|
|
|
using std::tuple;
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
using namespace std::literals; // for operator""s
|
2021-07-19 01:44:32 +12:00
|
|
|
|
2021-07-30 09:40:56 +12:00
|
|
|
void term_resize(bool force=false);
|
2021-07-21 13:17:34 +12:00
|
|
|
void banner_gen();
|
2021-05-11 09:46:41 +12:00
|
|
|
|
2021-11-14 11:31:01 +13:00
|
|
|
extern void clean_quit(int sig);
|
|
|
|
|
2021-06-20 00:57:27 +12:00
|
|
|
namespace Global {
|
2021-09-13 01:58:23 +12:00
|
|
|
extern const vector<array<string, 2>> Banner_src;
|
2021-06-21 08:07:04 +12:00
|
|
|
extern const string Version;
|
2021-08-04 09:47:46 +12:00
|
|
|
extern atomic<bool> quitting;
|
2021-07-19 01:44:32 +12:00
|
|
|
extern string exit_error_msg;
|
|
|
|
extern atomic<bool> thread_exception;
|
2021-06-21 08:07:04 +12:00
|
|
|
extern string banner;
|
2021-07-19 04:04:49 +12:00
|
|
|
extern atomic<bool> resized;
|
2021-07-21 13:17:34 +12:00
|
|
|
extern string overlay;
|
2021-07-30 09:40:56 +12:00
|
|
|
extern string clock;
|
2021-11-03 09:10:41 +13:00
|
|
|
extern uid_t real_uid, set_uid;
|
2021-06-20 00:57:27 +12:00
|
|
|
}
|
2021-05-15 23:24:24 +12:00
|
|
|
|
2021-07-05 08:02:31 +12:00
|
|
|
namespace Runner {
|
|
|
|
|
2021-07-16 09:49:16 +12:00
|
|
|
extern atomic<bool> active;
|
2021-07-21 13:17:34 +12:00
|
|
|
extern atomic<bool> reading;
|
2021-07-19 01:44:32 +12:00
|
|
|
extern atomic<bool> stopping;
|
2021-09-18 00:25:54 +12:00
|
|
|
extern atomic<bool> redraw;
|
2022-10-08 07:45:12 +13:00
|
|
|
extern atomic<bool> coreNum_reset;
|
2021-08-18 08:33:21 +12:00
|
|
|
extern pthread_t runner_id;
|
2021-09-13 01:58:23 +12:00
|
|
|
extern bool pause_output;
|
|
|
|
extern string debug_bg;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
2023-07-27 00:38:48 +12:00
|
|
|
void run(const string& box="", bool no_update = false, bool force_redraw = false);
|
2021-07-19 01:44:32 +12:00
|
|
|
void stop();
|
2021-07-05 08:02:31 +12:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-06-05 11:41:24 +12:00
|
|
|
namespace Tools {
|
2021-07-01 08:28:12 +12:00
|
|
|
//* Platform specific function for system_uptime (seconds since last restart)
|
2021-06-20 00:57:27 +12:00
|
|
|
double system_uptime();
|
2021-05-15 23:24:24 +12:00
|
|
|
}
|
|
|
|
|
2021-06-26 09:58:19 +12:00
|
|
|
namespace Shared {
|
|
|
|
//* Initialize platform specific needed variables and check for errors
|
|
|
|
void init();
|
2021-07-26 11:06:34 +12:00
|
|
|
|
|
|
|
extern long coreCount, page_size, clk_tck;
|
2021-06-26 09:58:19 +12:00
|
|
|
}
|
|
|
|
|
2021-07-24 12:13:26 +12:00
|
|
|
|
2021-07-05 08:02:31 +12:00
|
|
|
namespace Cpu {
|
2021-07-30 09:40:56 +12:00
|
|
|
extern string box;
|
2021-08-16 09:20:55 +12:00
|
|
|
extern int x, y, width, height, min_width, min_height;
|
2021-09-18 00:25:54 +12:00
|
|
|
extern bool shown, redraw, got_sensors, cpu_temp_only, has_battery;
|
2021-07-30 09:40:56 +12:00
|
|
|
extern string cpuName, cpuHz;
|
2021-09-13 01:58:23 +12:00
|
|
|
extern vector<string> available_fields;
|
|
|
|
extern vector<string> available_sensors;
|
2021-09-18 00:25:54 +12:00
|
|
|
extern tuple<int, long, string> current_bat;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
struct cpu_info {
|
2021-07-30 09:40:56 +12:00
|
|
|
unordered_flat_map<string, deque<long long>> cpu_percent = {
|
|
|
|
{"total", {}},
|
|
|
|
{"user", {}},
|
|
|
|
{"nice", {}},
|
|
|
|
{"system", {}},
|
|
|
|
{"idle", {}},
|
|
|
|
{"iowait", {}},
|
|
|
|
{"irq", {}},
|
|
|
|
{"softirq", {}},
|
|
|
|
{"steal", {}},
|
|
|
|
{"guest", {}},
|
|
|
|
{"guest_nice", {}}
|
|
|
|
};
|
|
|
|
vector<deque<long long>> core_percent;
|
2021-07-19 01:44:32 +12:00
|
|
|
vector<deque<long long>> temp;
|
2021-08-04 09:47:46 +12:00
|
|
|
long long temp_max = 0;
|
2023-07-05 23:23:11 +12:00
|
|
|
array<double, 3> load_avg;
|
2021-07-19 01:44:32 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
//* Collect cpu stats and temperatures
|
2023-07-27 00:38:48 +12:00
|
|
|
auto collect(bool no_update = false) -> cpu_info&;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
//* Draw contents of cpu box using <cpu> as source
|
2023-07-27 00:38:48 +12:00
|
|
|
string draw(const cpu_info& cpu, bool force_redraw = false, bool data_same = false);
|
2021-09-18 00:25:54 +12:00
|
|
|
|
|
|
|
//* Parse /proc/cpu info for mapping of core ids
|
|
|
|
auto get_core_mapping() -> unordered_flat_map<int, int>;
|
|
|
|
extern unordered_flat_map<int, int> core_mapping;
|
|
|
|
|
|
|
|
//* Get battery info from /sys
|
|
|
|
auto get_battery() -> tuple<int, long, string>;
|
2021-07-05 08:02:31 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Mem {
|
|
|
|
extern string box;
|
2021-08-16 09:20:55 +12:00
|
|
|
extern int x, y, width, height, min_width, min_height;
|
2021-07-05 08:02:31 +12:00
|
|
|
extern bool has_swap, shown, redraw;
|
2023-07-27 00:38:48 +12:00
|
|
|
const array mem_names { "used"s, "available"s, "cached"s, "free"s };
|
|
|
|
const array swap_names { "swap_used"s, "swap_free"s };
|
2021-08-11 06:20:33 +12:00
|
|
|
extern int disk_ios;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
struct disk_info {
|
2021-08-11 06:20:33 +12:00
|
|
|
std::filesystem::path dev;
|
|
|
|
string name;
|
2023-07-27 00:38:48 +12:00
|
|
|
string fstype{}; // defaults to ""
|
|
|
|
std::filesystem::path stat{}; // defaults to ""
|
|
|
|
int64_t total{}; // defaults to 0
|
|
|
|
int64_t used{}; // defaults to 0
|
|
|
|
int64_t free{}; // defaults to 0
|
|
|
|
int used_percent{}; // defaults to 0
|
|
|
|
int free_percent{}; // defaults to 0
|
2022-10-03 07:52:27 +13:00
|
|
|
|
2021-09-02 07:40:13 +12:00
|
|
|
array<int64_t, 3> old_io = {0, 0, 0};
|
2021-08-11 06:20:33 +12:00
|
|
|
deque<long long> io_read = {};
|
|
|
|
deque<long long> io_write = {};
|
2021-09-02 07:40:13 +12:00
|
|
|
deque<long long> io_activity = {};
|
2021-07-19 01:44:32 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mem_info {
|
2021-09-18 00:25:54 +12:00
|
|
|
unordered_flat_map<string, uint64_t> stats =
|
|
|
|
{{"used", 0}, {"available", 0}, {"cached", 0}, {"free", 0},
|
|
|
|
{"swap_total", 0}, {"swap_used", 0}, {"swap_free", 0}};
|
|
|
|
unordered_flat_map<string, deque<long long>> percent =
|
|
|
|
{{"used", {}}, {"available", {}}, {"cached", {}}, {"free", {}},
|
|
|
|
{"swap_total", {}}, {"swap_used", {}}, {"swap_free", {}}};
|
2021-07-19 01:44:32 +12:00
|
|
|
unordered_flat_map<string, disk_info> disks;
|
2021-08-11 06:20:33 +12:00
|
|
|
vector<string> disks_order;
|
2021-07-19 01:44:32 +12:00
|
|
|
};
|
|
|
|
|
2021-10-05 20:18:04 +13:00
|
|
|
//?* Get total system memory
|
|
|
|
uint64_t get_totalMem();
|
|
|
|
|
2021-07-19 01:44:32 +12:00
|
|
|
//* Collect mem & disks stats
|
2023-07-27 00:38:48 +12:00
|
|
|
auto collect(bool no_update = false) -> mem_info&;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
//* Draw contents of mem box using <mem> as source
|
2023-07-27 00:38:48 +12:00
|
|
|
string draw(const mem_info& mem, bool force_redraw = false, bool data_same = false);
|
2022-07-04 06:04:05 +12:00
|
|
|
|
2021-07-05 08:02:31 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
namespace Net {
|
|
|
|
extern string box;
|
2021-08-16 09:20:55 +12:00
|
|
|
extern int x, y, width, height, min_width, min_height;
|
2021-07-05 08:02:31 +12:00
|
|
|
extern bool shown, redraw;
|
2021-08-16 09:20:55 +12:00
|
|
|
extern string selected_iface;
|
|
|
|
extern vector<string> interfaces;
|
|
|
|
extern bool rescale;
|
|
|
|
extern unordered_flat_map<string, uint64_t> graph_max;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
struct net_stat {
|
2023-07-27 00:38:48 +12:00
|
|
|
uint64_t speed{}; // defaults to 0
|
|
|
|
uint64_t top{}; // defaults to 0
|
|
|
|
uint64_t total{}; // defaults to 0
|
|
|
|
uint64_t last{}; // defaults to 0
|
|
|
|
uint64_t offset{}; // defaults to 0
|
|
|
|
uint64_t rollover{}; // defaults to 0
|
2021-07-19 01:44:32 +12:00
|
|
|
};
|
|
|
|
|
|
|
|
struct net_info {
|
2021-08-16 09:20:55 +12:00
|
|
|
unordered_flat_map<string, deque<long long>> bandwidth = { {"download", {}}, {"upload", {}} };
|
|
|
|
unordered_flat_map<string, net_stat> stat = { {"download", {}}, {"upload", {}} };
|
2023-07-27 00:38:48 +12:00
|
|
|
string ipv4{}; // defaults to ""
|
|
|
|
string ipv6{}; // defaults to ""
|
|
|
|
bool connected{}; // defaults to false
|
2021-07-19 01:44:32 +12:00
|
|
|
};
|
|
|
|
|
2021-08-16 09:20:55 +12:00
|
|
|
extern unordered_flat_map<string, net_info> current_net;
|
|
|
|
|
2021-07-19 01:44:32 +12:00
|
|
|
//* Collect net upload/download stats
|
2023-07-27 00:38:48 +12:00
|
|
|
auto collect(bool no_update=false) -> net_info&;
|
2021-07-19 01:44:32 +12:00
|
|
|
|
|
|
|
//* Draw contents of net box using <net> as source
|
2023-07-27 00:38:48 +12:00
|
|
|
string draw(const net_info& net, bool force_redraw = false, bool data_same = false);
|
2021-07-05 08:02:31 +12:00
|
|
|
}
|
|
|
|
|
2021-05-15 04:54:37 +12:00
|
|
|
namespace Proc {
|
2021-07-19 01:44:32 +12:00
|
|
|
extern atomic<int> numpids;
|
2021-07-01 08:28:12 +12:00
|
|
|
|
2021-07-05 08:02:31 +12:00
|
|
|
extern string box;
|
2021-08-16 09:20:55 +12:00
|
|
|
extern int x, y, width, height, min_width, min_height;
|
2021-07-05 08:02:31 +12:00
|
|
|
extern bool shown, redraw;
|
2021-07-23 11:41:00 +12:00
|
|
|
extern int select_max;
|
2021-07-16 09:49:16 +12:00
|
|
|
extern atomic<int> detailed_pid;
|
2022-07-03 22:37:54 +12:00
|
|
|
extern int selected_pid, start, selected, collapse, expand, filter_found, selected_depth;
|
2021-09-02 07:40:13 +12:00
|
|
|
extern string selected_name;
|
2021-07-05 08:02:31 +12:00
|
|
|
|
2021-07-01 08:28:12 +12:00
|
|
|
//? Contains the valid sorting options for processes
|
2021-08-13 08:25:18 +12:00
|
|
|
const vector<string> sort_vector = {
|
|
|
|
"pid",
|
|
|
|
"name",
|
|
|
|
"command",
|
|
|
|
"threads",
|
|
|
|
"user",
|
|
|
|
"memory",
|
|
|
|
"cpu direct",
|
|
|
|
"cpu lazy",
|
|
|
|
};
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-07-04 11:18:48 +12:00
|
|
|
//? Translation from process state char to explanative string
|
2021-08-13 08:25:18 +12:00
|
|
|
const unordered_flat_map<char, string> proc_states = {
|
|
|
|
{'R', "Running"},
|
|
|
|
{'S', "Sleeping"},
|
|
|
|
{'D', "Waiting"},
|
|
|
|
{'Z', "Zombie"},
|
|
|
|
{'T', "Stopped"},
|
|
|
|
{'t', "Tracing"},
|
|
|
|
{'X', "Dead"},
|
|
|
|
{'x', "Dead"},
|
|
|
|
{'K', "Wakekill"},
|
|
|
|
{'W', "Unknown"},
|
|
|
|
{'P', "Parked"}
|
|
|
|
};
|
2021-07-04 11:18:48 +12:00
|
|
|
|
2021-06-13 04:49:27 +12:00
|
|
|
//* Container for process information
|
2021-05-15 12:56:04 +12:00
|
|
|
struct proc_info {
|
2023-07-27 00:38:48 +12:00
|
|
|
size_t pid{}; // defaults to 0
|
|
|
|
string name{}; // defaults to ""
|
|
|
|
string cmd{}; // defaults to ""
|
|
|
|
string short_cmd{}; // defaults to ""
|
|
|
|
size_t threads{}; // defaults to 0
|
|
|
|
int name_offset{}; // defaults to 0
|
|
|
|
string user{}; // defaults to ""
|
|
|
|
uint64_t mem{}; // defaults to 0
|
|
|
|
double cpu_p{}; // defaults to = 0.0
|
|
|
|
double cpu_c{}; // defaults to = 0.0
|
2021-06-05 11:41:24 +12:00
|
|
|
char state = '0';
|
2023-07-27 00:38:48 +12:00
|
|
|
int64_t p_nice{}; // defaults to 0
|
|
|
|
uint64_t ppid{}; // defaults to 0
|
|
|
|
uint64_t cpu_s{}; // defaults to 0
|
|
|
|
uint64_t cpu_t{}; // defaults to 0
|
|
|
|
string prefix{}; // defaults to ""
|
|
|
|
size_t depth{}; // defaults to 0
|
|
|
|
size_t tree_index{}; // defaults to 0
|
|
|
|
bool collapsed{}; // defaults to false
|
|
|
|
bool filtered{}; // defaults to false
|
2021-05-15 12:56:04 +12:00
|
|
|
};
|
2021-05-15 04:54:37 +12:00
|
|
|
|
2021-07-01 08:28:12 +12:00
|
|
|
//* Container for process info box
|
|
|
|
struct detail_container {
|
2023-07-27 00:38:48 +12:00
|
|
|
size_t last_pid{}; // defaults to 0
|
|
|
|
bool skip_smaps{}; // defaults to false
|
2021-07-01 08:28:12 +12:00
|
|
|
proc_info entry;
|
2021-07-04 11:18:48 +12:00
|
|
|
string elapsed, parent, status, io_read, io_write, memory;
|
2021-07-26 11:06:34 +12:00
|
|
|
long long first_mem = -1;
|
2021-07-04 11:18:48 +12:00
|
|
|
deque<long long> cpu_percent;
|
2021-07-26 11:06:34 +12:00
|
|
|
deque<long long> mem_bytes;
|
2021-07-01 08:28:12 +12:00
|
|
|
};
|
|
|
|
|
2021-07-26 11:06:34 +12:00
|
|
|
//? Contains all info for proc detailed box
|
2021-07-01 08:28:12 +12:00
|
|
|
extern detail_container detailed;
|
|
|
|
|
2021-07-19 01:44:32 +12:00
|
|
|
//* Collect and sort process information from /proc
|
2023-07-27 00:38:48 +12:00
|
|
|
auto collect(bool no_update = false) -> vector<proc_info>&;
|
2021-07-05 08:02:31 +12:00
|
|
|
|
2021-07-26 11:06:34 +12:00
|
|
|
//* Update current selection and view, returns -1 if no change otherwise the current selection
|
|
|
|
int selection(const string& cmd_key);
|
2021-07-16 09:49:16 +12:00
|
|
|
|
|
|
|
//* Draw contents of proc box using <plist> as data source
|
2023-07-27 00:38:48 +12:00
|
|
|
string draw(const vector<proc_info>& plist, bool force_redraw = false, bool data_same = false);
|
2022-07-03 22:37:54 +12:00
|
|
|
|
|
|
|
struct tree_proc {
|
|
|
|
std::reference_wrapper<proc_info> entry;
|
|
|
|
vector<tree_proc> children;
|
|
|
|
};
|
|
|
|
|
|
|
|
//* Sort vector of proc_info's
|
2023-07-27 00:38:48 +12:00
|
|
|
void proc_sorter(vector<proc_info>& proc_vec, const string& sorting, bool reverse, bool tree = false);
|
2022-07-03 22:37:54 +12:00
|
|
|
|
|
|
|
//* Recursive sort of process tree
|
2023-07-27 00:38:48 +12:00
|
|
|
void tree_sort(vector<tree_proc>& proc_vec, const string& sorting,
|
|
|
|
bool reverse, int& c_index, const int index_max, bool collapsed = false);
|
2022-07-03 22:37:54 +12:00
|
|
|
|
|
|
|
//* Generate process tree list
|
2023-07-27 00:38:48 +12:00
|
|
|
void _tree_gen(proc_info& cur_proc, vector<proc_info>& in_procs, vector<tree_proc>& out_procs,
|
|
|
|
int cur_depth, bool collapsed, const string& filter,
|
|
|
|
bool found = false, bool no_update = false, bool should_filter = false);
|
2021-07-05 08:02:31 +12:00
|
|
|
}
|