Skip to content

Commit

Permalink
bsd: Wrap kvm_t* in a uniq_ptr and share code between BSD's
Browse files Browse the repository at this point in the history
  • Loading branch information
imwints committed Feb 9, 2024
1 parent edcb68c commit ee46dba
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
20 changes: 19 additions & 1 deletion src/btop_shared.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,19 @@ tab-size = 4
#include <string>
#include <tuple>
#include <vector>
#include <ifaddrs.h>
#include <unordered_map>
#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::atomic;
using std::deque;
Expand Down Expand Up @@ -83,6 +92,15 @@ namespace Shared {
void init();

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
}


Expand Down
22 changes: 5 additions & 17 deletions src/freebsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,18 +189,6 @@ namespace Shared {
Logger::debug("Init -> 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 Cpu {
Expand Down Expand Up @@ -668,9 +656,9 @@ namespace Mem {

if (show_swap) {
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];
int nswap = kvm_getswapinfo(kd(), swap, 16, 0);
int nswap = kvm_getswapinfo(kd.get(), swap, 16, 0);
int totalSwap = 0, usedSwap = 0;
for (int i = 0; i < nswap; i++) {
totalSwap += swap[i].ksw_total;
Expand Down Expand Up @@ -1164,8 +1152,8 @@ namespace Proc {

int count = 0;
char buf[_POSIX2_LINE_MAX];
Shared::kvm_openfiles_wrapper kd(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf);
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_PROC, 0, &count);
Shared::KvmPtr kd {kvm_openfiles(nullptr, _PATH_DEVNULL, nullptr, O_RDONLY, buf)};
const struct kinfo_proc* kprocs = kvm_getprocs(kd.get(), KERN_PROC_PROC, 0, &count);

for (int i = 0; i < count; i++) {
const struct kinfo_proc* kproc = &kprocs[i];
Expand All @@ -1192,7 +1180,7 @@ namespace Proc {
continue;
}
new_proc.name = kproc->ki_comm;
char** argv = kvm_getargv(kd(), kproc, 0);
char** argv = kvm_getargv(kd.get(), kproc, 0);
if (argv) {
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
new_proc.cmd += argv[i] + " "s;
Expand Down
18 changes: 3 additions & 15 deletions src/openbsd/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,18 +184,6 @@ namespace Shared {
Mem::old_uptime = system_uptime();
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 Cpu {
Expand Down Expand Up @@ -1102,8 +1090,8 @@ namespace Proc {

int count = 0;
char buf[_POSIX2_LINE_MAX];
Shared::kvm_openfiles_wrapper kd(nullptr, nullptr, nullptr, KVM_NO_FILES, buf);
const struct kinfo_proc* kprocs = kvm_getprocs(kd(), KERN_PROC_ALL, 0, sizeof(struct kinfo_proc), &count);
Shared::KvmPtr kd {kvm_openfiles(nullptr, nullptr, nullptr, KVM_NO_FILES, buf)};
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++) {
const struct kinfo_proc* kproc = &kprocs[i];
Expand All @@ -1130,7 +1118,7 @@ namespace Proc {
continue;
}
new_proc.name = kproc->p_comm;
char** argv = kvm_getargv(kd(), kproc, 0);
char** argv = kvm_getargv(kd.get(), kproc, 0);
if (argv) {
for (int i = 0; argv[i] and cmp_less(new_proc.cmd.size(), 1000); i++) {
new_proc.cmd += argv[i] + " "s;
Expand Down

0 comments on commit ee46dba

Please sign in to comment.