Merge remote-tracking branch 'github/main'

v1
Brett 2024-02-04 13:07:31 -05:00
commit b2534baaf0
7 changed files with 696 additions and 21 deletions

View File

@ -210,6 +210,8 @@ namespace blt
void deallocate(pointer p, size_t n) noexcept void deallocate(pointer p, size_t n) noexcept
{ {
if (p == nullptr)
return;
// for (size_t i = 0; i < n; i++) // for (size_t i = 0; i < n; i++)
// p[i].~T(); // p[i].~T();
for (auto*& blk : blocks) for (auto*& blk : blocks)
@ -231,6 +233,7 @@ namespace blt
template<class U> template<class U>
inline void destroy(U* p) inline void destroy(U* p)
{ {
if (p != nullptr)
p->~U(); p->~U();
} }

View File

@ -7,24 +7,47 @@
#ifndef BLT_ASSERT_H #ifndef BLT_ASSERT_H
#define BLT_ASSERT_H #define BLT_ASSERT_H
#include <cstdlib>
namespace blt namespace blt
{ {
void printStacktrace(char** messages, int size, const char* path, int line); void printStacktrace(char** messages, int size, const char* path, int line);
void b_assert_failed(const char* expression, const char* path, int line); void b_assert_failed(const char* expression, const char* msg, const char* path, int line);
void b_throw(const char* what, const char* path, int line); void b_throw(const char* what, const char* path, int line);
} }
// prints error with stack trace if assertion fails. Does not stop execution. /**
#define blt_assert(expr) do {static_cast<bool>(expr) ? void(0) : blt::b_assert_failed(#expr, __FILE__, __LINE__) } while (0) * Prints error with stack trace if assertion fails. Does not stop execution.
// prints error with stack trace then exits with failure. */
#define blt_assert(expr) do {static_cast<bool>(expr) ? void(0) : blt::b_assert_failed(#expr, nullptr, __FILE__, __LINE__) } while (0)
/**
* Prints error with stack trace if assertion fails. Will print fail_message after
* the assertion expression but before the stack trace. Does not stop execution.
*/
#define blt_assert_msg(expr, fail_message) do {static_cast<bool>(expr) ? void(0) : blt::b_assert_failed(#expr, fail_message, __FILE__, __LINE__) } while (0)
/**
* Prints error with stack trace then exits with failure.
*/
#define BLT_ASSERT(expr) do { \ #define BLT_ASSERT(expr) do { \
if (!static_cast<bool>(expr)) { \ if (!static_cast<bool>(expr)) { \
blt::b_assert_failed(#expr, __FILE__, __LINE__); \ blt::b_assert_failed(#expr, nullptr, __FILE__, __LINE__); \
std::exit(EXIT_FAILURE); \ std::exit(EXIT_FAILURE); \
} \ } \
} while (0) } while (0)
/**
* Prints the error with stack trace if the assertion fails and stops execution with EXIT_FAILURE. Will print fail_message after
* the assertion expression but before the stack trace.
*/
#define BLT_ASSERT_MSG(expr, fail_message) do { \
if (!static_cast<bool>(expr)) { \
blt::b_assert_failed(#expr, fail_message, __FILE__, __LINE__); \
std::exit(EXIT_FAILURE); \
} \
} while (0)
// prints as error but does not throw the exception. // prints as error but does not throw the exception.
#define blt_throw(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__);} while (0) #define blt_throw(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__);} while (0)
// prints as error with stack trace and throws the exception. // prints as error with stack trace and throws the exception.

View File

@ -11,20 +11,27 @@
#ifdef _WIN32 #ifdef _WIN32
#include <intrin.h> #include <intrin.h>
#else #else
#include <x86intrin.h> #include <x86intrin.h>
#endif #endif
#else #else
#include <chrono> #include <chrono>
#endif #endif
#include <cstdint>
namespace blt::system { #include <cstdint>
#include <string>
#include <optional>
namespace blt::system
{
//#ifdef __GNUC__ //#ifdef __GNUC__
// #define GNU_INLINE __attribute__((__gnu_inline__, __always_inline__)) // #define GNU_INLINE __attribute__((__gnu_inline__, __always_inline__))
//#else //#else
// #define GNU_INLINE // #define GNU_INLINE
//#endif //#endif
inline std::uint64_t rdtsc(){ inline std::uint64_t rdtsc()
{
#ifdef __EMSCRIPTEN__ #ifdef __EMSCRIPTEN__
return std::chrono::high_resolution_clock::now().time_since_epoch().count(); return std::chrono::high_resolution_clock::now().time_since_epoch().count();
#else #else
@ -33,6 +40,144 @@ namespace blt::system {
} }
// TODO: system memory and current CPU usage. (Linux Only currently) // TODO: system memory and current CPU usage. (Linux Only currently)
struct linux_proc_stat
{
// pid %d
std::int32_t PID;
// comm %s
std::string exec_name;
/*
* R Running
* S Sleeping in an interruptible wait
* D Waiting in uninterruptible disk sleep
* Z Zombie
* T Stopped (on a signal) or (before Linux 2.6.33) trace stopped
* t Tracing stop (Linux 2.6.33 onward)
* W Paging (only before Linux 2.6.0)
* X Dead (from Linux 2.6.0 onward)
* x Dead (Linux 2.6.33 to 3.13 only)
* K Wakekill (Linux 2.6.33 to 3.13 only)
* W Waking (Linux 2.6.33 to 3.13 only)
* P Parked (Linux 3.9 to 3.13 only)
* I Idle (Linux 4.14 onward)
*/
// state %c
char state;
// pid of parent
std::int32_t parent_pid;
// group id of process
std::int32_t group_id;
// session id of process
std::int32_t session_id;
// controlling terminal
std::int32_t tty_nr;
// The ID of the foreground process group of the controlling terminal of the process.
std::int32_t tpgid;
std::uint32_t flags;
std::uint64_t minflt;
std::uint64_t cminflt;
std::uint64_t majflt;
std::uint64_t cmajflt;
// amount of time process has been scheduled in user mode measured in clock ticks (divide by sysconf(_SC_CLK_TCK))
std::uint64_t utime;
// amount of time that this process has been scheduled in kernel mode, measured in clock ticks (divide by sysconf(_SC_CLK_TCK)).
std::uint64_t stime;
// children times
std::int64_t cutime;
std::int64_t cstime;
std::int64_t priority;
std::int64_t nice;
std::int64_t num_threads;
std::int64_t itrealvalue;
// The time the process started after system boot. Since Linux 2.6, the value is expressed in clock ticks (divide by sysconf(_SC_CLK_TCK)).
std::uint64_t starttime;
// Virtual memory size in bytes.
std::uint64_t vsize;
// Resident Set Size: number of pages the process has in real memory.
// This is just the pages which count toward text, data, or stack space. This does not include pages which have not been demand-loaded
// in, or which are swapped out. This value is inaccurate; see /proc/pid/statm below.
std::int64_t rss;
// Current soft limit in bytes on the rss of the process; see the description of RLIMIT_RSS in getrlimit(2).
std::uint64_t rsslim;
std::uint64_t startcode;
std::uint64_t endcode;
std::uint64_t startstack;
std::uint64_t kstkesp;
std::uint64_t signal;
std::uint64_t blocked;
std::uint64_t sigignore;
std::uint64_t sigcatch;
std::uint64_t wchan;
std::uint64_t nswap;
std::uint64_t cnswap;
std::int32_t exit_signal;
std::int32_t processor;
std::uint32_t rt_priority;
std::uint32_t policy;
std::uint64_t delayacct_blkio_ticks;
std::uint64_t guest_time;
std::int64_t cguest_time;
std::uint64_t start_data;
std::uint64_t end_data;
std::uint64_t start_brk;
std::uint64_t arg_start;
std::uint64_t arg_end;
std::uint64_t env_start;
std::uint64_t env_end;
std::int32_t exit_code;
};
struct memory_info_t
{
// total program size (bytes) (same as VmSize in status)
std::uint64_t size;
// size of memory portions (bytes) (same as VmRSS in status)
std::uint64_t resident;
// number of bytes that are shared (i.e. backed by a file, same as RssFile+RssShmem in status)
std::uint64_t shared;
// number of bytes that are 'code' (not including libs; broken, includes data segment)
std::uint64_t text;
// number of pages of library (0)
std::uint64_t lib;
// number of bytes of data/stack (including libs; broken, includes library text)
std::uint64_t data;
// number of dirty pages (0)
std::uint64_t dt;
};
struct timeval {
time_t tv_sec; /* Seconds */
suseconds_t tv_usec; /* Microseconds */
};
struct rusage {
timeval ru_utime; /* user CPU time used */
timeval ru_stime; /* system CPU time used */
long ru_maxrss; /* maximum resident set size */
long ru_ixrss; /* integral shared memory size */
long ru_idrss; /* integral unshared data size */
long ru_isrss; /* integral unshared stack size */
long ru_minflt; /* page reclaims (soft page faults) */
long ru_majflt; /* page faults (hard page faults) */
long ru_nswap; /* swaps */
long ru_inblock; /* block input operations */
long ru_oublock; /* block output operations */
long ru_msgsnd; /* IPC messages sent */
long ru_msgrcv; /* IPC messages received */
long ru_nsignals; /* signals received */
long ru_nvcsw; /* voluntary context switches */
long ru_nivcsw; /* involuntary context switches */
};
std::optional<blt::system::rusage> get_resources_process();
std::optional<blt::system::rusage> get_resources_thread();
memory_info_t get_memory_process();
} }
#endif //BLT_SYSTEM_H #endif //BLT_SYSTEM_H

View File

@ -21,6 +21,8 @@
#include <string> #include <string>
#include <blt/compatibility.h> #include <blt/compatibility.h>
#include <variant>
#include <utility>
#if defined(__GNUC__) #if defined(__GNUC__)
@ -189,6 +191,7 @@ namespace blt
return a.current != b.current; return a.current != b.current;
} }
}; };
private: private:
T _begin; T _begin;
T _end; T _end;
@ -209,12 +212,377 @@ namespace blt
} }
}; };
struct unexpect_t
{
explicit unexpect_t() = default;
};
inline constexpr unexpect_t unexpect{};
template<typename T>
using remove_cvref_t = std::remove_reference_t<std::remove_cv_t<T>>;
template<class E>
class unexpected
{
private:
E e;
public:
constexpr unexpected(const unexpected&) = default;
constexpr unexpected(unexpected&&) = default;
template<class Err = E, std::enable_if_t<
!std::is_same_v<remove_cvref_t<Err>, unexpected> && !std::is_same_v<remove_cvref_t<Err>, std::in_place_t> &&
std::is_constructible_v<E, Err>, bool> = true>
constexpr explicit unexpected(Err&& e): e(std::forward<Err>(e))
{}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args...>, bool> = true>
constexpr explicit unexpected(std::in_place_t, Args&& ... args): e(std::forward<Args>(args)...)
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args...>, bool> = true>
constexpr explicit unexpected(std::in_place_t, std::initializer_list<U> il, Args&& ... args): e(il, std::forward<Args>(args)...)
{}
constexpr const E& error() const& noexcept
{
return e;
}
constexpr E& error()& noexcept
{
return e;
}
constexpr const E&& error() const&& noexcept
{
return e;
}
constexpr E&& error()&& noexcept
{
return e;
}
constexpr void swap(unexpected& other) noexcept(std::is_nothrow_swappable_v<E>)
{
std::swap(error(), other.error());
}
template<typename E2>
inline friend constexpr bool operator==(const unexpected& x, const unexpected <E2>& y)
{
return x.error() == y.error();
}
friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y)))
{}
};
template<class E>
unexpected(E) -> unexpected<E>;
template<class E>
class bad_expected_access : public std::exception
{
private:
E e;
public:
explicit bad_expected_access(E e): e(std::move(e))
{}
const E& error() const& noexcept
{ return e; }
E& error()& noexcept
{ return e; }
const E&& error() const&& noexcept
{ return e; }
E&& error()&& noexcept
{ return e; }
[[nodiscard]] const char* what() const noexcept override
{ return "blt::expected does not contain a value!"; }
};
template<typename T, typename E, bool = std::is_copy_constructible_v<T>>
class expected
{
protected:
std::variant<T, E> v;
template<typename U, typename G>
inline static constexpr bool eight_insanity_v =
std::is_constructible_v<T, expected<U, G>&> || std::is_constructible_v<T, expected<U, G>> ||
std::is_constructible_v<T, const expected<U, G>&> || std::is_constructible_v<T, const expected<U, G>> ||
std::is_convertible_v<expected<U, G>&, T> || std::is_convertible_v<expected<U, G>, T> ||
std::is_convertible_v<const expected<U, G>&, T> || std::is_convertible_v<const expected<U, G>, T>;
template<typename U, typename G>
inline static constexpr bool four_insanity_v =
std::is_constructible_v<unexpected<E>, expected<U, G>&> || std::is_constructible_v<unexpected<E>, expected<U, G>> ||
std::is_constructible_v<unexpected<E>, const expected<U, G>&> || std::is_constructible_v<unexpected<E>, const expected<U, G>>;
public:
template<typename std::enable_if_t<std::is_default_constructible_v<T>, bool> = true>
constexpr expected() noexcept: v(T())
{}
constexpr expected(const expected& copy) = delete;
constexpr expected(expected&& move) noexcept: v(move ? std::move(*move) : std::move(move.error()))
{}
/*
* (4)...(5)
*/
template<class U, class G, class UF = std::add_lvalue_reference_t<const U>, class GF = const G&, std::enable_if_t<
(!std::is_convertible_v<UF, T> || !std::is_convertible_v<GF, E>) && (std::is_constructible_v<T, UF> || std::is_void_v<U>) &&
std::is_constructible_v<E, GF> && !eight_insanity_v < U, G>&& !four_insanity_v<U, G>, bool> = true>
constexpr explicit expected(const expected<U, G>& other):
v(other.has_value() ? std::forward<UF>(*other) : std::forward<GF>(other.error()))
{}
template<class U, class G, class UF = U, class GF = G, std::enable_if_t<
(!std::is_convertible_v<UF, T> || !std::is_convertible_v<GF, E>) && (std::is_constructible_v<T, UF> || std::is_void_v<U>) &&
std::is_constructible_v<E, GF> && !eight_insanity_v < U, G>&& !four_insanity_v<U, G>, bool> = true>
constexpr explicit expected(expected<U, G>&& other):
v(other.has_value() ? std::forward<UF>(*other) : std::forward<GF>(other.error()))
{}
template<class U, class G, class UF = std::add_lvalue_reference_t<const U>, class GF = const G&, std::enable_if_t<
(std::is_convertible_v<UF, T> && std::is_convertible_v<GF, E>) && (std::is_constructible_v<T, UF> || std::is_void_v<U>) &&
std::is_constructible_v<E, GF> && !eight_insanity_v < U, G>&& !four_insanity_v<U, G>, bool> = true>
constexpr expected(const expected<U, G>& other):
v(other.has_value() ? std::forward<UF>(*other) : std::forward<GF>(other.error()))
{}
template<class U, class G, class UF = U, class GF = G, std::enable_if_t<
(std::is_convertible_v<UF, T> && std::is_convertible_v<GF, E>) && (std::is_constructible_v<T, UF> || std::is_void_v<U>) &&
std::is_constructible_v<E, GF> && !eight_insanity_v < U, G>&& !four_insanity_v<U, G>, bool> = true>
constexpr expected(expected<U, G>&& other):
v(other.has_value() ? std::forward<UF>(*other) : std::forward<GF>(other.error()))
{}
/*
* (6)
*/
template<class U = T, std::enable_if_t<!std::is_convertible_v<U, T> &&
!std::is_same_v<remove_cvref_t<T>, void> &&
!std::is_same_v<remove_cvref_t<U>, std::in_place_t> &&
!std::is_same_v<expected, remove_cvref_t<U>> &&
std::is_constructible_v<T, U> &&
!std::is_same_v<remove_cvref_t<U>, unexpected<U>> &&
!std::is_same_v<remove_cvref_t<U>, expected<T, E>>, bool> = true>
constexpr explicit expected(U&& v): v(T(std::forward<U>(v)))
{}
template<class U = T, std::enable_if_t<std::is_convertible_v<U, T> &&
!std::is_same_v<remove_cvref_t<T>, void> &&
!std::is_same_v<remove_cvref_t<U>, std::in_place_t> &&
!std::is_same_v<expected, remove_cvref_t<U>> &&
std::is_constructible_v<T, U> &&
!std::is_same_v<remove_cvref_t<U>, unexpected<U>> &&
!std::is_same_v<remove_cvref_t<U>, expected<T, E>>, bool> = true>
constexpr expected(U&& v): v(T(std::forward<U>(v)))
{}
/*
* (7)
*/
template<class G, class GF = std::add_const_t<std::add_lvalue_reference_t<G>>, std::enable_if_t<
!std::is_convertible_v<const G&, E> && std::is_constructible_v<E, GF>, bool> = true>
constexpr explicit expected(const unexpected<G>& e): v(std::forward<GF>(e.error()))
{}
template<class G, class GF = std::add_const_t<std::add_lvalue_reference_t<G>>, std::enable_if_t<
std::is_convertible_v<const G&, E> && std::is_constructible_v<E, GF>, bool> = true>
constexpr expected(const unexpected<G>& e): v(std::forward<GF>(e.error()))
{}
/*
* (8)
*/
template<class G, class GF = std::add_const_t<std::add_lvalue_reference_t<G>>, std::enable_if_t<
!std::is_convertible_v<G, E> && std::is_constructible_v<E, GF>, bool> = true>
constexpr explicit expected(unexpected<G>&& e): v(std::forward<GF>(e.error()))
{}
template<class G, class GF = std::add_const_t<std::add_lvalue_reference_t<G>>, std::enable_if_t<
std::is_convertible_v<G, E> && std::is_constructible_v<E, GF>, bool> = true>
constexpr expected(unexpected<G>&& e): v(std::forward<GF>(e.error()))
{}
/*
* (9)...(13)
*/
template<class... Args, std::enable_if_t<std::is_constructible_v<T, Args...>, bool> = true>
constexpr explicit expected(std::in_place_t, Args&& ... args): v(T(std::forward<Args>(args)...))
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<T, std::initializer_list<U>&, Args...>, bool> = true>
constexpr explicit expected(std::in_place_t, std::initializer_list<U> il, Args&& ... args): v(T(il, std::forward<Args>(args)...))
{}
// template<class... Args, std::enable_if_t<std::is_same_v<remove_cvref_t<T>, void>, bool> = true>
// constexpr explicit expected(std::in_place_t) noexcept: v(T())
// {}
template<class... Args, std::enable_if_t<std::is_constructible_v<E, Args...>, bool> = true>
constexpr explicit expected(unexpect_t, Args&& ... args): v(E(std::forward<Args>(args)...))
{}
template<class U, class... Args, std::enable_if_t<std::is_constructible_v<E, std::initializer_list<U>&, Args...>, bool> = true>
constexpr explicit expected(unexpect_t, std::initializer_list<U> il, Args&& ... args): v(E(il, std::forward<Args>(args)...))
{}
expected& operator=(const expected& copy) = delete;
expected& operator=(expected&& move) = default;
[[nodiscard]] constexpr explicit operator bool() const noexcept
{
return std::holds_alternative<T>(v);
}
[[nodiscard]] constexpr inline bool has_value() const noexcept
{
return std::holds_alternative<T>(v);
}
constexpr T& value()&
{
if (*this)
return std::get<T>(v);
else
throw bad_expected_access(std::as_const(error()));
}
constexpr const T& value() const&
{
if (*this)
return std::get<T>(v);
else
throw bad_expected_access(std::as_const(error()));
}
constexpr T&& value()&&
{
if (*this)
return std::get<T>(v);
else
throw bad_expected_access(std::move(error()));
}
constexpr const T&& value() const&&
{
if (*this)
return std::get<T>(v);
else
throw bad_expected_access(std::move(error()));
}
constexpr const E& error() const& noexcept
{
return std::get<E>(v);
}
constexpr E& error()& noexcept
{
return std::get<E>(v);
}
constexpr const E&& error() const&& noexcept
{
return std::get<E>(v);
}
constexpr E&& error()&& noexcept
{
return std::get<E>(v);
}
template<class U, std::enable_if_t<std::is_convertible_v<U, T> && std::is_copy_constructible_v<T>, bool> = true>
constexpr T value_or(U&& default_value) const&
{
return bool(*this) ? **this : static_cast<T>(std::forward<U>(default_value));
}
template<class U, std::enable_if_t<std::is_convertible_v<U, T> && std::is_move_constructible_v<T>, bool> = true>
constexpr T value_or(U&& default_value)&&
{
return bool(*this) ? std::move(**this) : static_cast<T>(std::forward<U>(default_value));
}
constexpr inline const T* operator->() const noexcept
{
return &std::get<T>(v);
}
constexpr inline T* operator->() noexcept
{
return &std::get<T>(v);
}
constexpr inline const T& operator*() const& noexcept
{
return std::get<T>(v);
}
constexpr inline T& operator*()& noexcept
{
return std::get<T>(v);
}
constexpr inline const T&& operator*() const&& noexcept
{
return std::move(std::get<T>(v));
}
constexpr inline T&& operator*()&& noexcept
{
return std::move(std::get<T>(v));
}
};
template<typename T, typename E>
class expected<T, E, true> : expected<T, E, false>
{
public:
using expected<T, E, false>::expected;
constexpr expected(const expected& copy): expected<T, E, false>::v(copy ? *copy : copy.error())
{}
expected& operator=(const expected& copy) = default;
};
//#define BLT_LAMBDA(type, var, code) [](const type& var) -> auto { return code; } //#define BLT_LAMBDA(type, var, code) [](const type& var) -> auto { return code; }
//#define BLT_LAMBDA(var, code) [](var) -> auto { return code; } //#define BLT_LAMBDA(var, code) [](var) -> auto { return code; }
/*
* std::visit(blt::lambda_visitor{
* lambdas...
* }, data_variant);
*/
// TODO: WTF // TODO: WTF
template<class... TLambdas> template<class... TLambdas>
struct lambda_visitor : TLambdas... { struct lambda_visitor : TLambdas ...
{
using TLambdas::operator()...; using TLambdas::operator()...;
}; };

@ -1 +1 @@
Subproject commit 946ebad67a21212d11a0dd4deb7cdedc297d47bc Subproject commit 65775fa09fecaa65d0b0022ab6bf091c0e509445

View File

@ -52,12 +52,14 @@ namespace blt {
#endif #endif
} }
void b_assert_failed(const char* expression, const char* path, int line) void b_assert_failed(const char* expression, const char* msg, const char* path, int line)
{ {
#if defined(__GNUC__) && !defined(__EMSCRIPTEN__) #if defined(__GNUC__) && !defined(__EMSCRIPTEN__)
BLT_STACK_TRACE(50); BLT_STACK_TRACE(50);
BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line); BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line);
if (msg != nullptr)
BLT_ERROR(msg);
BLT_ERROR("Stack Trace:"); BLT_ERROR("Stack Trace:");
printStacktrace(messages, size, path, line); printStacktrace(messages, size, path, line);

View File

@ -4,5 +4,139 @@
* See LICENSE file for license detail * See LICENSE file for license detail
*/ */
#include <blt/std/system.h> #include <blt/std/system.h>
#include <blt/std/binary_tree.h> #include <blt/std/logging.h>
#include <blt/math/math.h>
#include <sys/time.h> /* for struct timeval */
#include <climits> /* for CLK_TCK */
#include <sys/resource.h>
#include <cstring>
#ifndef WIN32
#include <unistd.h>
#include <blt/std/loader.h>
#include "blt/std/assert.h"
inline long blt_get_page_size()
{
return sysconf(_SC_PAGESIZE);
}
//struct proc_statm_t
//{
// // total program size (pages) (same as VmSize in status)
// std::uint64_t size;
// // size of memory portions (pages) (same as VmRSS in status)
// std::uint64_t resident;
// // number of pages that are shared (i.e. backed by a file, same as RssFile+RssShmem in status)
// std::uint64_t shared;
// // number of pages that are 'code' (not including libs; broken, includes data segment)
// std::uint64_t text;
// // number of pages of library (0)
// std::uint64_t lib;
// // number of pages of data/stack (including libs; broken, includes library text)
// std::uint64_t data;
// // number of dirty pages (0)
// std::uint64_t dt;
//};
blt::system::memory_info_t process_proc()
{
static auto page_size = blt_get_page_size();
auto str = blt::fs::getFile("/proc/self/statm");
auto data = blt::string::split(str, ' ');
BLT_ASSERT(data.size() == 7 && "Something went wrong when parsing /proc/self/statm! Expected 7 values!");
blt::system::memory_info_t mem {};
mem.size = page_size * std::stoull(data[0]);
mem.resident = page_size * std::stoull(data[1]);
mem.shared = page_size * std::stoull(data[2]);
mem.text = page_size * std::stoull(data[3]);
mem.lib = page_size * std::stoull(data[4]);
mem.data = page_size * std::stoull(data[5]);
mem.dt = page_size * std::stoull(data[6]);
return mem;
}
#endif
namespace blt
{
std::optional<system::rusage> get_resources(int who)
{
system::rusage usage{};
std::memset(&usage, 0, sizeof(system::rusage));
#ifdef WIN32
FILETIME starttime;
FILETIME exittime;
FILETIME kerneltime;
FILETIME usertime;
ULARGE_INTEGER li;
if (who != RUSAGE_SELF)
{
/* Only RUSAGE_SELF is supported in this implementation for now */
BLT_WARN("Only RUSAGE_SELF is supported in this implementation for now");
return {};
}
if (GetProcessTimes(GetCurrentProcess(),
&starttime, &exittime, &kerneltime, &usertime) == 0)
{
BLT_WARN("Unable to get process resource usage, error: %d", GetLastError());
return {};
}
/* Convert FILETIMEs (0.1 us) to struct timeval */
memcpy(&li, &kerneltime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
usage.ru_stime.tv_sec = li.QuadPart / 1000000L;
usage.ru_stime.tv_usec = li.QuadPart % 1000000L;
memcpy(&li, &usertime, sizeof(FILETIME));
li.QuadPart /= 10L; /* Convert to microseconds */
usage.ru_utime.tv_sec = li.QuadPart / 1000000L;
usage.ru_utime.tv_usec = li.QuadPart % 1000000L;
#else
if (getrusage(who, (struct rusage*) &usage) != 0)
{
BLT_ERROR("Failed to get rusage %d", errno);
return {};
}
#endif
return usage;
}
std::optional<system::rusage> system::get_resources_process()
{
return get_resources(RUSAGE_SELF);
}
std::optional<system::rusage> system::get_resources_thread()
{
#ifdef WIN32
return get_resources(RUSAGE_SELF);
#else
return get_resources(RUSAGE_THREAD);
#endif
}
system::memory_info_t system::get_memory_process()
{
#ifdef WIN32
BLT_WARN("Unsupported OS");
return {};
#else
return process_proc();
#endif
}
}