summaryrefslogtreecommitdiffstats
path: root/src/osx/btop_collect.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/osx/btop_collect.cpp')
-rw-r--r--src/osx/btop_collect.cpp103
1 files changed, 51 insertions, 52 deletions
diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp
index d2d0e3b..26a93d3 100644
--- a/src/osx/btop_collect.cpp
+++ b/src/osx/btop_collect.cpp
@@ -4,7 +4,7 @@
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
+ 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,
@@ -45,9 +45,6 @@ tab-size = 4
#include <unistd.h>
#include <stdexcept>
-#include <btop_config.hpp>
-#include <btop_shared.hpp>
-#include <btop_tools.hpp>
#include <cmath>
#include <fstream>
#include <numeric>
@@ -55,6 +52,10 @@ tab-size = 4
#include <regex>
#include <string>
+#include "../btop_config.hpp"
+#include "../btop_shared.hpp"
+#include "../btop_tools.hpp"
+
#include "sensors.hpp"
#include "smc.hpp"
@@ -72,7 +73,6 @@ namespace Cpu {
vector<string> available_fields = {"total"};
vector<string> available_sensors = {"Auto"};
cpu_info current_cpu;
- fs::path freq_path = "/sys/devices/system/cpu/cpufreq/policy0/scaling_cur_freq";
bool got_sensors = false, cpu_temp_only = false;
int core_offset = 0;
@@ -128,7 +128,7 @@ namespace Shared {
}
size_t physicalCoreCountSize = sizeof(physicalCoreCount);
- if (sysctlbyname("hw.physicalcpu", &physicalCoreCount, &physicalCoreCountSize, NULL, 0) < 0) {
+ if (sysctlbyname("hw.physicalcpu", &physicalCoreCount, &physicalCoreCountSize, nullptr, 0) < 0) {
Logger::error("Could not get physical core count");
}
@@ -154,7 +154,7 @@ namespace Shared {
int64_t memsize = 0;
size_t size = sizeof(memsize);
- if (sysctlbyname("hw.memsize", &memsize, &size, NULL, 0) < 0) {
+ if (sysctlbyname("hw.memsize", &memsize, &size, nullptr, 0) < 0) {
Logger::warning("Could not get memory size");
}
totalMem = memsize;
@@ -163,7 +163,6 @@ namespace Shared {
arg_max = sysconf(_SC_ARG_MAX);
//? Init for namespace Cpu
- if (not fs::exists(Cpu::freq_path) or access(Cpu::freq_path.c_str(), R_OK) == -1) Cpu::freq_path.clear();
Cpu::current_cpu.core_percent.insert(Cpu::current_cpu.core_percent.begin(), Shared::coreCount, {});
Cpu::current_cpu.temp.insert(Cpu::current_cpu.temp.begin(), Shared::coreCount + 1, {});
Cpu::core_old_totals.insert(Cpu::core_old_totals.begin(), Shared::coreCount, 0);
@@ -193,19 +192,19 @@ namespace Cpu {
const array<string, 10> time_names = {"user", "nice", "system", "idle"};
unordered_flat_map<string, long long> cpu_old = {
- {"totals", 0},
- {"idles", 0},
- {"user", 0},
- {"nice", 0},
- {"system", 0},
- {"idle", 0}
+ {"totals", 0},
+ {"idles", 0},
+ {"user", 0},
+ {"nice", 0},
+ {"system", 0},
+ {"idle", 0}
};
string get_cpuName() {
string name;
char buffer[1024];
size_t size = sizeof(buffer);
- if (sysctlbyname("machdep.cpu.brand_string", &buffer, &size, NULL, 0) < 0) {
+ if (sysctlbyname("machdep.cpu.brand_string", &buffer, &size, nullptr, 0) < 0) {
Logger::error("Failed to get CPU name");
return name;
}
@@ -323,7 +322,7 @@ namespace Cpu {
int mib[] = {CTL_HW, HW_CPU_FREQ};
- if (sysctl(mib, 2, &freq, &size, NULL, 0) < 0) {
+ if (sysctl(mib, 2, &freq, &size, nullptr, 0) < 0) {
// this fails on Apple Silicon macs. Apparently you're not allowed to know
return "";
}
@@ -442,7 +441,7 @@ namespace Cpu {
return {percent, seconds, status};
}
- auto collect(bool no_update) -> cpu_info & {
+ auto collect(bool no_update) -> cpu_info & {
if (Runner::stopping or (no_update and not current_cpu.cpu_percent.at("total").empty()))
return current_cpu;
auto &cpu = current_cpu;
@@ -454,7 +453,7 @@ namespace Cpu {
natural_t cpu_count;
natural_t i;
kern_return_t error;
- processor_cpu_load_info_data_t *cpu_load_info = NULL;
+ processor_cpu_load_info_data_t *cpu_load_info = nullptr;
MachProcessorInfo info{};
error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, &info.info_array, &info.info_count);
@@ -603,7 +602,7 @@ namespace Mem {
}
/* Get the list of all drive objects. */
if (IOServiceGetMatchingServices(libtop_master_port,
- IOServiceMatching("IOMediaBSDClient"), &drive_list)) {
+ IOServiceMatching("IOMediaBSDClient"), &drive_list)) {
Logger::error("Error in IOServiceGetMatchingServices()");
return;
}
@@ -663,15 +662,15 @@ namespace Mem {
}
}
- auto collect(bool no_update) -> mem_info & {
+ auto collect(bool no_update) -> mem_info & {
if (Runner::stopping or (no_update and not current_mem.percent.at("used").empty()))
return current_mem;
- auto show_swap = Config::getB("show_swap");
- auto show_disks = Config::getB("show_disks");
- auto swap_disk = Config::getB("swap_disk");
+ auto show_swap = Config::getB("show_swap");
+ auto show_disks = Config::getB("show_disks");
+ auto swap_disk = Config::getB("swap_disk");
auto &mem = current_mem;
- static bool snapped = (getenv("BTOP_SNAPPED") != NULL);
+ static bool snapped = (getenv("BTOP_SNAPPED") != nullptr);
vm_statistics64 p;
mach_msg_type_number_t info_size = HOST_VM_INFO64_COUNT;
@@ -686,7 +685,7 @@ namespace Mem {
struct xsw_usage swap;
size_t len = sizeof(struct xsw_usage);
- if (sysctl(mib, 2, &swap, &len, NULL, 0) == 0) {
+ if (sysctl(mib, 2, &swap, &len, nullptr, 0) == 0) {
mem.stats.at("swap_total") = swap.xsu_total;
mem.stats.at("swap_free") = swap.xsu_avail;
mem.stats.at("swap_used") = swap.xsu_used;
@@ -713,7 +712,7 @@ namespace Mem {
double uptime = system_uptime();
auto &disks_filter = Config::getS("disks_filter");
bool filter_exclude = false;
- // auto only_physical = Config::getB("only_physical");
+ // auto only_physical = Config::getB("only_physical");
auto &disks = mem.disks;
vector<string> filter;
if (not disks_filter.empty()) {
@@ -843,11 +842,11 @@ namespace Net {
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 &config_iface = Config::getS("net_iface");
- auto net_sync = Config::getB("net_sync");
- auto net_auto = Config::getB("net_auto");
+ auto net_sync = Config::getB("net_sync");
+ auto net_auto = Config::getB("net_auto");
auto new_timestamp = time_ms();
if (not no_update and errors < 3) {
@@ -867,8 +866,8 @@ namespace Net {
string ipv4, ipv6;
//? Iteration over all items in getifaddrs() list
- for (auto *ifa = if_wrap(); ifa != NULL; ifa = ifa->ifa_next) {
- if (ifa->ifa_addr == NULL) continue;
+ for (auto *ifa = if_wrap(); ifa != nullptr; ifa = ifa->ifa_next) {
+ if (ifa->ifa_addr == nullptr) continue;
family = ifa->ifa_addr->sa_family;
const auto &iface = ifa->ifa_name;
//? Update available interfaces vector and get status of interface
@@ -884,7 +883,7 @@ namespace Net {
//? Get IPv4 address
if (family == AF_INET) {
if (net[iface].ipv4.empty()) {
- if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr)->sin_addr), ip, IPBUFFER_MAXSIZE)) {
+ if (nullptr != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in*>(ifa->ifa_addr)->sin_addr), ip, IPBUFFER_MAXSIZE)) {
net[iface].ipv4 = ip;
} else {
int errsv = errno;
@@ -895,7 +894,7 @@ namespace Net {
//? Get IPv6 address
else if (family == AF_INET6) {
if (net[iface].ipv6.empty()) {
- if (NULL != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr)->sin6_addr), ip, IPBUFFER_MAXSIZE)) {
+ if (nullptr != inet_ntop(family, &(reinterpret_cast<struct sockaddr_in6*>(ifa->ifa_addr)->sin6_addr), ip, IPBUFFER_MAXSIZE)) {
net[iface].ipv6 = ip;
} else {
int errsv = errno;
@@ -908,15 +907,15 @@ namespace Net {
unordered_flat_map<string, std::tuple<uint64_t, uint64_t>> ifstats;
int mib[] = {CTL_NET, PF_ROUTE, 0, 0, NET_RT_IFLIST2, 0};
size_t len;
- if (sysctl(mib, 6, NULL, &len, NULL, 0) < 0) {
+ if (sysctl(mib, 6, nullptr, &len, nullptr, 0) < 0) {
Logger::error("failed getting network interfaces");
} else {
std::unique_ptr<char[]> buf(new char[len]);
- if (sysctl(mib, 6, buf.get(), &len, NULL, 0) < 0) {
+ if (sysctl(mib, 6, buf.get(), &len, nullptr, 0) < 0) {
Logger::error("failed getting network interfaces");
} else {
char *lim = buf.get() + len;
- char *next = NULL;
+ char *next = nullptr;
for (next = buf.get(); next < lim;) {
struct if_msghdr *ifm = (struct if_msghdr *)next;
next += ifm->ifm_msglen;
@@ -1000,7 +999,7 @@ namespace Net {
auto sorted_interfaces = interfaces;
rng::sort(sorted_interfaces, [&](const auto &a, const auto &b) {
return cmp_greater(net.at(a).stat["download"].total + net.at(a).stat["upload"].total,
- net.at(b).stat["download"].total + net.at(b).stat["upload"].total);
+ net.at(b).stat["download"].total + net.at(b).stat["upload"].total);
});
selected_iface.clear();
//? Try to set to a connected interface
@@ -1023,8 +1022,8 @@ namespace Net {
for (const auto &sel : {0, 1}) {
if (rescale or max_count[dir][sel] >= 5) {
const long long avg_speed = (net[selected_iface].bandwidth[dir].size() > 5
- ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5
- : net[selected_iface].stat[dir].speed);
+ ? std::accumulate(net.at(selected_iface).bandwidth.at(dir).rbegin(), net.at(selected_iface).bandwidth.at(dir).rbegin() + 5, 0ll) / 5
+ : net[selected_iface].stat[dir].speed);
graph_max[dir] = max(uint64_t(avg_speed * (sel == 0 ? 1.3 : 3.0)), (uint64_t)10 << 10);
max_count[dir][0] = max_count[dir][1] = 0;
redraw = true;
@@ -1093,7 +1092,7 @@ namespace Proc {
//? Process runtime : current time - start time (both in unix time - seconds since epoch)
struct timeval currentTime;
- gettimeofday(&currentTime, NULL);
+ gettimeofday(&currentTime, nullptr);
detailed.elapsed = sec_to_dhms(currentTime.tv_sec - (detailed.entry.cpu_s / 1'000'000));
if (detailed.elapsed.size() > 8) detailed.elapsed.resize(detailed.elapsed.size() - 3);
@@ -1125,17 +1124,17 @@ namespace Proc {
}
//* Collects and sorts process information from /proc
- auto collect(bool no_update) -> vector<proc_info> & {
+ auto collect(bool no_update) -> vector<proc_info> & {
const auto &sorting = Config::getS("proc_sorting");
- auto reverse = Config::getB("proc_reversed");
+ auto reverse = Config::getB("proc_reversed");
const auto &filter = Config::getS("proc_filter");
- auto per_core = Config::getB("proc_per_core");
- auto tree = Config::getB("proc_tree");
- auto show_detailed = Config::getB("show_detailed");
+ auto per_core = Config::getB("proc_per_core");
+ auto tree = Config::getB("proc_tree");
+ auto show_detailed = Config::getB("show_detailed");
const size_t detailed_pid = Config::getI("detailed_pid");
bool should_filter = current_filter != filter;
if (should_filter) current_filter = filter;
- bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter);
+ bool sorted_change = (sorting != current_sort or reverse != current_rev or should_filter);
if (sorted_change) {
current_sort = sorting;
current_rev = reverse;
@@ -1155,7 +1154,7 @@ namespace Proc {
{ //* Get CPU totals
natural_t cpu_count;
kern_return_t error;
- processor_cpu_load_info_data_t *cpu_load_info = NULL;
+ processor_cpu_load_info_data_t *cpu_load_info = nullptr;
MachProcessorInfo info{};
error = host_processor_info(mach_host_self(), PROCESSOR_CPU_LOAD_INFO, &cpu_count, &info.info_array, &info.info_count);
if (error != KERN_SUCCESS) {
@@ -1177,13 +1176,13 @@ namespace Proc {
size_t size = 0;
const auto timeNow = time_micros();
- if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0 || size == 0) {
+ if (sysctl(mib, 4, nullptr, &size, nullptr, 0) < 0 || size == 0) {
Logger::error("Unable to get size of kproc_infos");
}
uint64_t cpu_t = 0;
std::unique_ptr<kinfo_proc[]> processes(new kinfo_proc[size / sizeof(kinfo_proc)]);
- if (sysctl(mib, 4, processes.get(), &size, NULL, 0) == 0) {
+ if (sysctl(mib, 4, processes.get(), &size, nullptr, 0) == 0) {
size_t count = size / sizeof(struct kinfo_proc);
for (size_t i = 0; i < count; i++) { //* iterate over all processes in kinfo_proc
struct kinfo_proc& kproc = processes.get()[i];
@@ -1214,7 +1213,7 @@ namespace Proc {
std::unique_ptr<char[]> proc_chars(new char[Shared::arg_max]);
int mib[] = {CTL_KERN, KERN_PROCARGS2, (int)pid};
size_t argmax = Shared::arg_max;
- if (sysctl(mib, 3, proc_chars.get(), &argmax, NULL, 0) == 0) {
+ if (sysctl(mib, 3, proc_chars.get(), &argmax, nullptr, 0) == 0) {
int argc = 0;
memcpy(&argc, &proc_chars.get()[0], sizeof(argc));
std::string_view proc_args(proc_chars.get(), argmax);
@@ -1377,8 +1376,8 @@ namespace Tools {
struct timeval ts, currTime;
std::size_t len = sizeof(ts);
int mib[2] = {CTL_KERN, KERN_BOOTTIME};
- if (sysctl(mib, 2, &ts, &len, NULL, 0) != -1) {
- gettimeofday(&currTime, NULL);
+ if (sysctl(mib, 2, &ts, &len, nullptr, 0) != -1) {
+ gettimeofday(&currTime, nullptr);
return currTime.tv_sec - ts.tv_sec;
}
return 0.0;