mirror of
https://github.com/aristocratos/btop.git
synced 2024-06-16 17:35:03 +12:00
Merge pull request #756 from imwints/unix-duplicates
This commit is contained in:
commit
0f224bb0e2
|
@ -25,10 +25,19 @@ tab-size = 4
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <tuple>
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <ifaddrs.h>
|
|
||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
// From `man 3 getifaddrs`: <net/if.h> must be included before <ifaddrs.h>
|
||||||
|
// clang-format off
|
||||||
|
#include <net/if.h>
|
||||||
|
#include <ifaddrs.h>
|
||||||
|
// clang-format on
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||||
|
# include <kvm.h>
|
||||||
|
#endif
|
||||||
|
|
||||||
using std::array;
|
using std::array;
|
||||||
using std::atomic;
|
using std::atomic;
|
||||||
using std::deque;
|
using std::deque;
|
||||||
|
@ -83,6 +92,15 @@ namespace Shared {
|
||||||
void init();
|
void init();
|
||||||
|
|
||||||
extern long coreCount, page_size, clk_tck;
|
extern long coreCount, page_size, clk_tck;
|
||||||
|
|
||||||
|
#if defined(__FreeBSD__) || defined(__OpenBSD__)
|
||||||
|
struct KvmDeleter {
|
||||||
|
void operator()(kvm_t* handle) {
|
||||||
|
kvm_close(handle);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
using KvmPtr = std::unique_ptr<kvm_t, KvmDeleter>;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -289,6 +307,17 @@ namespace Net {
|
||||||
bool connected{};
|
bool connected{};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class IfAddrsPtr {
|
||||||
|
struct ifaddrs* ifaddr;
|
||||||
|
int status;
|
||||||
|
public:
|
||||||
|
IfAddrsPtr() { status = getifaddrs(&ifaddr); }
|
||||||
|
~IfAddrsPtr() { freeifaddrs(ifaddr); }
|
||||||
|
[[nodiscard]] constexpr auto operator()() -> struct ifaddrs* { return ifaddr; }
|
||||||
|
[[nodiscard]] constexpr auto get() -> struct ifaddrs* { return ifaddr; }
|
||||||
|
[[nodiscard]] constexpr auto get_status() const noexcept -> int { return status; };
|
||||||
|
};
|
||||||
|
|
||||||
extern std::unordered_map<string, net_info> current_net;
|
extern std::unordered_map<string, net_info> current_net;
|
||||||
|
|
||||||
//* Collect net upload/download stats
|
//* Collect net upload/download stats
|
||||||
|
|
|
@ -189,18 +189,6 @@ namespace Shared {
|
||||||
Logger::debug("Init -> Mem::get_zpools()");
|
Logger::debug("Init -> Mem::get_zpools()");
|
||||||
Mem::get_zpools();
|
Mem::get_zpools();
|
||||||
}
|
}
|
||||||
|
|
||||||
//* RAII wrapper for kvm_openfiles
|
|
||||||
class kvm_openfiles_wrapper {
|
|
||||||
kvm_t* kd = nullptr;
|
|
||||||
public:
|
|
||||||
kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) {
|
|
||||||
this->kd = kvm_openfiles(execf, coref, swapf, flags, err);
|
|
||||||
}
|
|
||||||
~kvm_openfiles_wrapper() { kvm_close(kd); }
|
|
||||||
auto operator()() -> kvm_t* { return kd; }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Shared
|
} // namespace Shared
|
||||||
|
|
||||||
namespace Cpu {
|
namespace Cpu {
|
||||||
|
@ -673,9 +661,9 @@ namespace Mem {
|
||||||
|
|
||||||
if (show_swap) {
|
if (show_swap) {
|
||||||
char buf[_POSIX2_LINE_MAX];
|
char buf[_POSIX2_LINE_MAX];
|
||||||
Shared::kvm_openfiles_wrapper kd(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf);
|
Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)};
|
||||||
struct kvm_swap swap[16];
|
struct kvm_swap swap[16];
|
||||||
int nswap = kvm_getswapinfo(kd(), swap, 16, 0);
|
int nswap = kvm_getswapinfo(kd.get(), swap, 16, 0);
|
||||||
int totalSwap = 0, usedSwap = 0;
|
int totalSwap = 0, usedSwap = 0;
|
||||||
for (int i = 0; i < nswap; i++) {
|
for (int i = 0; i < nswap; i++) {
|
||||||
totalSwap += swap[i].ksw_total;
|
totalSwap += swap[i].ksw_total;
|
||||||
|
@ -828,17 +816,6 @@ namespace Net {
|
||||||
bool rescale = true;
|
bool rescale = true;
|
||||||
uint64_t timestamp = 0;
|
uint64_t timestamp = 0;
|
||||||
|
|
||||||
//* RAII wrapper for getifaddrs
|
|
||||||
class getifaddr_wrapper {
|
|
||||||
struct ifaddrs *ifaddr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
int status;
|
|
||||||
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
|
|
||||||
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
|
|
||||||
auto operator()() -> struct ifaddrs * { return ifaddr; }
|
|
||||||
};
|
|
||||||
|
|
||||||
auto collect(bool no_update) -> net_info & {
|
auto collect(bool no_update) -> net_info & {
|
||||||
auto &net = current_net;
|
auto &net = current_net;
|
||||||
auto &config_iface = Config::getS("net_iface");
|
auto &config_iface = Config::getS("net_iface");
|
||||||
|
@ -848,10 +825,10 @@ namespace Net {
|
||||||
|
|
||||||
if (not no_update and errors < 3) {
|
if (not no_update and errors < 3) {
|
||||||
//? Get interface list using getifaddrs() wrapper
|
//? Get interface list using getifaddrs() wrapper
|
||||||
getifaddr_wrapper if_wrap{};
|
IfAddrsPtr if_addrs {};
|
||||||
if (if_wrap.status != 0) {
|
if (if_addrs.get_status() != 0) {
|
||||||
errors++;
|
errors++;
|
||||||
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
|
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
|
||||||
redraw = true;
|
redraw = true;
|
||||||
return empty_net;
|
return empty_net;
|
||||||
}
|
}
|
||||||
|
@ -863,7 +840,7 @@ namespace Net {
|
||||||
string ipv4, ipv6;
|
string ipv4, ipv6;
|
||||||
|
|
||||||
//? Iteration over all items in getifaddrs() list
|
//? Iteration over all items in getifaddrs() list
|
||||||
for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
|
for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr == nullptr) continue;
|
if (ifa->ifa_addr == nullptr) continue;
|
||||||
family = ifa->ifa_addr->sa_family;
|
family = ifa->ifa_addr->sa_family;
|
||||||
const auto &iface = ifa->ifa_name;
|
const auto &iface = ifa->ifa_name;
|
||||||
|
@ -1169,8 +1146,8 @@ namespace Proc {
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
char buf[_POSIX2_LINE_MAX];
|
char buf[_POSIX2_LINE_MAX];
|
||||||
Shared::kvm_openfiles_wrapper kd(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf);
|
Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)};
|
||||||
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_PROC, 0, &count);
|
const struct kinfo_proc* kprocs = kvm_getprocs(kd.get(), KERN_PROC_PROC, 0, &count);
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
const struct kinfo_proc* kproc = &kprocs[i];
|
const struct kinfo_proc* kproc = &kprocs[i];
|
||||||
|
@ -1197,7 +1174,7 @@ namespace Proc {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
new_proc.name = kproc->ki_comm;
|
new_proc.name = kproc->ki_comm;
|
||||||
char** argv = kvm_getargv(kd(), kproc, 0);
|
char** argv = kvm_getargv(kd.get(), kproc, 0);
|
||||||
if (argv) {
|
if (argv) {
|
||||||
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
|
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
|
||||||
new_proc.cmd += argv[i] + " "s;
|
new_proc.cmd += argv[i] + " "s;
|
||||||
|
|
|
@ -2197,16 +2197,6 @@ namespace Net {
|
||||||
bool rescale{true};
|
bool rescale{true};
|
||||||
uint64_t timestamp{};
|
uint64_t timestamp{};
|
||||||
|
|
||||||
//* RAII wrapper for getifaddrs
|
|
||||||
class getifaddr_wrapper {
|
|
||||||
struct ifaddrs* ifaddr;
|
|
||||||
public:
|
|
||||||
int status;
|
|
||||||
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
|
|
||||||
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
|
|
||||||
auto operator()() -> struct ifaddrs* { return ifaddr; }
|
|
||||||
};
|
|
||||||
|
|
||||||
auto collect(bool no_update) -> net_info& {
|
auto collect(bool no_update) -> net_info& {
|
||||||
if (Runner::stopping) return empty_net;
|
if (Runner::stopping) return empty_net;
|
||||||
auto& net = current_net;
|
auto& net = current_net;
|
||||||
|
@ -2217,10 +2207,10 @@ namespace Net {
|
||||||
|
|
||||||
if (not no_update and errors < 3) {
|
if (not no_update and errors < 3) {
|
||||||
//? Get interface list using getifaddrs() wrapper
|
//? Get interface list using getifaddrs() wrapper
|
||||||
getifaddr_wrapper if_wrap {};
|
IfAddrsPtr if_addrs {};
|
||||||
if (if_wrap.status != 0) {
|
if (if_addrs.get_status() != 0) {
|
||||||
errors++;
|
errors++;
|
||||||
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
|
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
|
||||||
redraw = true;
|
redraw = true;
|
||||||
return empty_net;
|
return empty_net;
|
||||||
}
|
}
|
||||||
|
@ -2232,7 +2222,7 @@ namespace Net {
|
||||||
string ipv4, ipv6;
|
string ipv4, ipv6;
|
||||||
|
|
||||||
//? Iteration over all items in getifaddrs() list
|
//? Iteration over all items in getifaddrs() list
|
||||||
for (auto* ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
|
for (auto* ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr == nullptr) continue;
|
if (ifa->ifa_addr == nullptr) continue;
|
||||||
family = ifa->ifa_addr->sa_family;
|
family = ifa->ifa_addr->sa_family;
|
||||||
const auto& iface = ifa->ifa_name;
|
const auto& iface = ifa->ifa_name;
|
||||||
|
|
|
@ -184,18 +184,6 @@ namespace Shared {
|
||||||
Mem::old_uptime = system_uptime();
|
Mem::old_uptime = system_uptime();
|
||||||
Mem::collect();
|
Mem::collect();
|
||||||
}
|
}
|
||||||
|
|
||||||
//* RAII wrapper for kvm_openfiles
|
|
||||||
class kvm_openfiles_wrapper {
|
|
||||||
kvm_t* kd = nullptr;
|
|
||||||
public:
|
|
||||||
kvm_openfiles_wrapper(const char* execf, const char* coref, const char* swapf, int flags, char* err) {
|
|
||||||
this->kd = kvm_openfiles(execf, coref, swapf, flags, err);
|
|
||||||
}
|
|
||||||
~kvm_openfiles_wrapper() { kvm_close(kd); }
|
|
||||||
auto operator()() -> kvm_t* { return kd; }
|
|
||||||
};
|
|
||||||
|
|
||||||
} // namespace Shared
|
} // namespace Shared
|
||||||
|
|
||||||
namespace Cpu {
|
namespace Cpu {
|
||||||
|
@ -780,17 +768,6 @@ namespace Net {
|
||||||
bool rescale = true;
|
bool rescale = true;
|
||||||
uint64_t timestamp = 0;
|
uint64_t timestamp = 0;
|
||||||
|
|
||||||
//* RAII wrapper for getifaddrs
|
|
||||||
class getifaddr_wrapper {
|
|
||||||
struct ifaddrs *ifaddr;
|
|
||||||
|
|
||||||
public:
|
|
||||||
int status;
|
|
||||||
getifaddr_wrapper() { status = getifaddrs(&ifaddr); }
|
|
||||||
~getifaddr_wrapper() { freeifaddrs(ifaddr); }
|
|
||||||
auto operator()() -> struct ifaddrs * { return ifaddr; }
|
|
||||||
};
|
|
||||||
|
|
||||||
auto collect(bool no_update) -> net_info & {
|
auto collect(bool no_update) -> net_info & {
|
||||||
auto &net = current_net;
|
auto &net = current_net;
|
||||||
auto &config_iface = Config::getS("net_iface");
|
auto &config_iface = Config::getS("net_iface");
|
||||||
|
@ -800,10 +777,10 @@ namespace Net {
|
||||||
|
|
||||||
if (not no_update and errors < 3) {
|
if (not no_update and errors < 3) {
|
||||||
//? Get interface list using getifaddrs() wrapper
|
//? Get interface list using getifaddrs() wrapper
|
||||||
getifaddr_wrapper if_wrap{};
|
IfAddrsPtr if_addrs {};
|
||||||
if (if_wrap.status != 0) {
|
if (if_addrs.get_status() != 0) {
|
||||||
errors++;
|
errors++;
|
||||||
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status));
|
Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_addrs.get_status()));
|
||||||
redraw = true;
|
redraw = true;
|
||||||
return empty_net;
|
return empty_net;
|
||||||
}
|
}
|
||||||
|
@ -815,7 +792,7 @@ namespace Net {
|
||||||
string ipv4, ipv6;
|
string ipv4, ipv6;
|
||||||
|
|
||||||
//? Iteration over all items in getifaddrs() list
|
//? Iteration over all items in getifaddrs() list
|
||||||
for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
|
for (auto *ifa = if_addrs.get(); ifa != nullptr; ifa = ifa->ifa_next) {
|
||||||
if (ifa->ifa_addr == nullptr) continue;
|
if (ifa->ifa_addr == nullptr) continue;
|
||||||
family = ifa->ifa_addr->sa_family;
|
family = ifa->ifa_addr->sa_family;
|
||||||
const auto &iface = ifa->ifa_name;
|
const auto &iface = ifa->ifa_name;
|
||||||
|
@ -1102,8 +1079,8 @@ namespace Proc {
|
||||||
|
|
||||||
int count = 0;
|
int count = 0;
|
||||||
char buf[_POSIX2_LINE_MAX];
|
char buf[_POSIX2_LINE_MAX];
|
||||||
Shared::kvm_openfiles_wrapper kd(nullptr, nullptr, nullptr, KVM_NO_FILES, buf);
|
Shared::KvmPtr kd {kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, buf)};
|
||||||
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
|
const struct kinfo_proc* kprocs = kvm_getprocs(kd.get() , KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
|
||||||
|
|
||||||
for (int i = 0; i < count; i++) {
|
for (int i = 0; i < count; i++) {
|
||||||
const struct kinfo_proc* kproc = &kprocs[i];
|
const struct kinfo_proc* kproc = &kprocs[i];
|
||||||
|
@ -1130,7 +1107,7 @@ namespace Proc {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
new_proc.name = kproc->p_comm;
|
new_proc.name = kproc->p_comm;
|
||||||
char** argv = kvm_getargv(kd(), kproc, 0);
|
char** argv = kvm_getargv(kd.get(), kproc, 0);
|
||||||
if (argv) {
|
if (argv) {
|
||||||
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
|
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
|
||||||
new_proc.cmd += argv[i] + " "s;
|
new_proc.cmd += argv[i] + " "s;
|
||||||
|
|
Loading…
Reference in a new issue