Commit 5cde8506 authored by Dominik Charousset's avatar Dominik Charousset

Merge pull request #1317

parents 12bcfbc7 ae4870d8
......@@ -191,8 +191,13 @@
# endif
#elif defined(__FreeBSD__)
# define CAF_BSD
# define CAF_FREE_BSD
#elif defined(__NetBSD__)
# define CAF_BSD
# define CAF_NET_BSD
#elif defined(__OpenBSD__)
# define CAF_BSD
# define CAF_OPEN_BSD
#elif defined(__CYGWIN__)
# define CAF_CYGWIN
#elif defined(WIN32) || defined(_WIN32)
......@@ -201,7 +206,7 @@
# error Platform and/or compiler not supported
#endif
#if defined(CAF_MACOS) || defined(CAF_LINUX) || defined(CAF_BSD) \
|| defined(CAF_CYGWIN)
|| defined(CAF_CYGWIN) || defined(CAF_NET_BSD)
# define CAF_POSIX
#endif
......
......@@ -45,6 +45,32 @@ std::string get_root_uuid() {
} // namespace detail
} // namespace caf
#elif defined(CAF_IOS) || defined(CAF_ANDROID) || defined(CAF_NET_BSD)
// Return a randomly-generated UUID on mobile devices or NetBSD (requires root
// access to get UUID from disk).
# include <random>
namespace caf {
namespace detail {
std::string get_root_uuid() {
std::random_device rd;
std::uniform_int_distribution<int> dist(0, 15);
std::string uuid = uuid_format;
for (auto& c : uuid) {
if (c != '-') {
auto n = dist(rd);
c = static_cast<char>((n < 10) ? n + '0' : (n - 10) + 'A');
}
}
return uuid;
}
} // namespace detail
} // namespace caf
#elif defined(CAF_LINUX) || defined(CAF_BSD) || defined(CAF_CYGWIN)
# include <algorithm>
......@@ -192,29 +218,4 @@ std::string get_root_uuid() {
} // namespace detail
} // namespace caf
#elif defined(CAF_IOS) || defined(CAF_ANDROID)
// return a randomly-generated UUID on mobile devices
# include <random>
namespace caf {
namespace detail {
std::string get_root_uuid() {
std::random_device rd;
std::uniform_int_distribution<int> dist(0, 15);
std::string uuid = uuid_format;
for (auto& c : uuid) {
if (c != '-') {
auto n = dist(rd);
c = static_cast<char>((n < 10) ? n + '0' : (n - 10) + 'A');
}
}
return uuid;
}
} // namespace detail
} // namespace caf
#endif // CAF_WINDOWS
......@@ -6,7 +6,11 @@
#include "caf/config.hpp"
#if defined(CAF_LINUX) || defined(CAF_MACOS)
#if defined(CAF_LINUX) || defined(CAF_MACOS) || defined(CAF_NET_BSD)
# define CAF_HAS_CXX_ABI
#endif
#ifdef CAF_HAS_CXX_ABI
# include <cxxabi.h>
# include <sys/types.h>
# include <unistd.h>
......@@ -43,7 +47,7 @@ void prettify_type_name(std::string& class_name) {
}
void prettify_type_name(std::string& class_name, const char* c_class_name) {
#if defined(CAF_LINUX) || defined(CAF_MACOS)
#ifdef CAF_HAS_CXX_ABI
int stat = 0;
std::unique_ptr<char, decltype(free)*> real_class_name{nullptr, free};
auto tmp = abi::__cxa_demangle(c_class_name, nullptr, nullptr, &stat);
......
......@@ -12,7 +12,7 @@
#if defined(CAF_LINUX)
# include <sys/prctl.h>
#elif defined(CAF_BSD)
#elif defined(CAF_BSD) && !defined(CAF_NET_BSD)
# include <pthread_np.h>
#endif // defined(...)
......@@ -32,6 +32,8 @@ void set_thread_name(const char* name) {
pthread_setname_np(name);
# elif defined(CAF_LINUX)
prctl(PR_SET_NAME, name, 0, 0, 0);
# elif defined(CAF_NET_BSD)
pthread_setname_np(pthread_self(), name, NULL);
# elif defined(CAF_BSD)
pthread_set_name_np(pthread_self(), name);
# endif // defined(...)
......
......@@ -10,7 +10,7 @@
// -- detect supported platforms -----------------------------------------------
#if defined(CAF_MACOS) || defined(CAF_LINUX)
#if defined(CAF_MACOS) || defined(CAF_LINUX) || defined(CAF_NET_BSD)
# define CAF_HAS_PROCESS_METRICS
#endif
......@@ -117,19 +117,17 @@ sys_stats read_sys_stats() {
#endif // CAF_MACOS
// -- Linux-specific scraping logic --------------------------------------------
// -- helper for caching the result of a syscall -------------------------------
#ifdef CAF_LINUX
#if defined(CAF_LINUX) || defined(CAF_NET_BSD)
# include <cstdio>
# include <unistd.h>
namespace {
std::atomic<long> global_ticks_per_second;
std::atomic<long> global_page_size;
/// Caches the result from a `sysconf` call in a cache variable to avoid
/// frequent syscalls. Sets `cache_var` to -1 in case of an error. Initially,
/// `cache_var` must be 0 and we assume a successful syscall would always return
/// some value > 0. If `cache_var` is > 0 then this function simply returns the
/// cached value directly.
bool load_system_setting(std::atomic<long>& cache_var, long& var, int name,
[[maybe_unused]] const char* pretty_name) {
var = cache_var.load();
......@@ -143,9 +141,10 @@ bool load_system_setting(std::atomic<long>& cache_var, long& var, int name,
var = -1;
cache_var = var;
return false;
} else {
cache_var = var;
return true;
}
cache_var = var;
return true;
default:
return true;
}
......@@ -154,6 +153,20 @@ bool load_system_setting(std::atomic<long>& cache_var, long& var, int name,
# define TRY_LOAD(varname, confname) \
load_system_setting(global_##varname, varname, confname, #confname)
#endif
// -- Linux-specific scraping logic --------------------------------------------
#ifdef CAF_LINUX
# include <cstdio>
namespace {
std::atomic<long> global_ticks_per_second;
std::atomic<long> global_page_size;
sys_stats read_sys_stats() {
sys_stats result{0, 0, 0};
long ticks_per_second = 0;
......@@ -210,6 +223,41 @@ sys_stats read_sys_stats() {
} // namespace
// -- NetBSD-specific scraping logic -------------------------------------------
#elif defined(CAF_NET_BSD)
# include <sys/sysctl.h>
namespace {
std::atomic<long> global_page_size;
sys_stats read_sys_stats() {
auto result = sys_stats{0, 0, 0};
auto kip2 = kinfo_proc2{};
auto kip2_size = sizeof(kip2);
int mib[6] = {
CTL_KERN, KERN_PROC2, KERN_PROC_PID, getpid(), static_cast<int>(kip2_size),
1,
};
long page_size = 0;
if (!TRY_LOAD(page_size, _SC_PAGE_SIZE))
return result;
if (sysctl(mib, 6, &kip2, &kip2_size, nullptr, size_t{0}) != 0) {
CAF_LOG_ERROR("failed to call sysctl in read_sys_stats");
global_page_size = -1;
return result;
}
result.rss = static_cast<int64_t>(kip2.p_vm_rssize) * page_size;
result.vms = static_cast<int64_t>(kip2.p_vm_vsize) * page_size;
result.cpu_time = kip2.p_rtime_sec;
result.cpu_time += static_cast<double>(kip2.p_rtime_usec) / 1000000;
return result;
}
} // namespace
#endif // CAF_LINUX
namespace caf::telemetry::importer {
......
......@@ -81,7 +81,7 @@ const int ec_interrupted_syscall = EINTR;
#endif
// Platform-dependent setup for suppressing SIGPIPE.
#if defined(CAF_MACOS) || defined(CAF_IOS) || defined(__FreeBSD__)
#if defined(CAF_MACOS) || defined(CAF_IOS) || defined(CAF_FREE_BSD)
// Set the SO_NOSIGPIPE socket option on macOS, iOS and FreeBSD.
const int no_sigpipe_socket_flag = SO_NOSIGPIPE;
const int no_sigpipe_io_flag = 0;
......@@ -90,7 +90,7 @@ const int no_sigpipe_io_flag = 0;
const int no_sigpipe_socket_flag = 0;
const int no_sigpipe_io_flag = 0;
#else
// Pass MSG_NOSIGNAL to recv/send on Linux/Android/OpenBSD.
// Pass MSG_NOSIGNAL to recv/send on Linux/Android/OpenBSD/NetBSD.
const int no_sigpipe_socket_flag = 0;
const int no_sigpipe_io_flag = MSG_NOSIGNAL;
#endif
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment