diff --git a/include/blt/std/allocator.h b/include/blt/std/allocator.h index 3b5bfa5..4a2d517 100644 --- a/include/blt/std/allocator.h +++ b/include/blt/std/allocator.h @@ -210,6 +210,8 @@ namespace blt void deallocate(pointer p, size_t n) noexcept { + if (p == nullptr) + return; // for (size_t i = 0; i < n; i++) // p[i].~T(); for (auto*& blk : blocks) @@ -231,7 +233,8 @@ namespace blt template inline void destroy(U* p) { - p->~U(); + if (p != nullptr) + p->~U(); } [[nodiscard]] inline size_t max_size() const diff --git a/include/blt/std/assert.h b/include/blt/std/assert.h index b29db63..be8ff2b 100644 --- a/include/blt/std/assert.h +++ b/include/blt/std/assert.h @@ -7,24 +7,47 @@ #ifndef BLT_ASSERT_H #define BLT_ASSERT_H +#include + namespace blt { 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); } -// prints error with stack trace if assertion fails. Does not stop execution. -#define blt_assert(expr) do {static_cast(expr) ? void(0) : blt::b_assert_failed(#expr, __FILE__, __LINE__) } while (0) -// prints error with stack trace then exits with failure. -#define BLT_ASSERT(expr) do { \ - if (!static_cast(expr)) { \ - blt::b_assert_failed(#expr, __FILE__, __LINE__); \ - std::exit(EXIT_FAILURE); \ - } \ +/** + * Prints error with stack trace if assertion fails. Does not stop execution. + */ +#define blt_assert(expr) do {static_cast(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(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 { \ + if (!static_cast(expr)) { \ + blt::b_assert_failed(#expr, nullptr, __FILE__, __LINE__); \ + std::exit(EXIT_FAILURE); \ + } \ } 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(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. #define blt_throw(throwable) do {blt::b_throw(throwable.what(), __FILE__, __LINE__);} while (0) // prints as error with stack trace and throws the exception. diff --git a/include/blt/std/system.h b/include/blt/std/system.h index 960521e..471220a 100755 --- a/include/blt/std/system.h +++ b/include/blt/std/system.h @@ -9,22 +9,29 @@ #ifndef __EMSCRIPTEN__ #ifdef _WIN32 - #include + #include #else - #include + + #include + #endif #else #include #endif -#include -namespace blt::system { +#include +#include +#include + +namespace blt::system +{ //#ifdef __GNUC__ // #define GNU_INLINE __attribute__((__gnu_inline__, __always_inline__)) //#else // #define GNU_INLINE //#endif - inline std::uint64_t rdtsc(){ + inline std::uint64_t rdtsc() + { #ifdef __EMSCRIPTEN__ return std::chrono::high_resolution_clock::now().time_since_epoch().count(); #else @@ -33,6 +40,144 @@ namespace blt::system { } // 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 get_resources_process(); + std::optional get_resources_thread(); + + memory_info_t get_memory_process(); + } #endif //BLT_SYSTEM_H diff --git a/include/blt/std/utility.h b/include/blt/std/utility.h index b007bb0..e8f8363 100644 --- a/include/blt/std/utility.h +++ b/include/blt/std/utility.h @@ -21,6 +21,8 @@ #include #include +#include +#include #if defined(__GNUC__) @@ -189,6 +191,7 @@ namespace blt return a.current != b.current; } }; + private: T _begin; T _end; @@ -208,16 +211,381 @@ namespace blt return range_itr(_end - offset, offset == 0); } }; + + struct unexpect_t + { + explicit unexpect_t() = default; + }; + + inline constexpr unexpect_t unexpect{}; + + template + using remove_cvref_t = std::remove_reference_t>; + + template + class unexpected + { + private: + E e; + public: + constexpr unexpected(const unexpected&) = default; + + constexpr unexpected(unexpected&&) = default; + + template, unexpected> && !std::is_same_v, std::in_place_t> && + std::is_constructible_v, bool> = true> + constexpr explicit unexpected(Err&& e): e(std::forward(e)) + {} + + template, bool> = true> + constexpr explicit unexpected(std::in_place_t, Args&& ... args): e(std::forward(args)...) + {} + + template&, Args...>, bool> = true> + constexpr explicit unexpected(std::in_place_t, std::initializer_list il, Args&& ... args): e(il, std::forward(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) + { + std::swap(error(), other.error()); + } + + template + inline friend constexpr bool operator==(const unexpected& x, const unexpected & y) + { + return x.error() == y.error(); + } + + friend constexpr void swap(unexpected& x, unexpected& y) noexcept(noexcept(x.swap(y))) + {} + }; + + template + unexpected(E) -> unexpected; + + template + 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> + class expected + { + protected: + std::variant v; + + template + inline static constexpr bool eight_insanity_v = + std::is_constructible_v&> || std::is_constructible_v> || + std::is_constructible_v&> || std::is_constructible_v> || + std::is_convertible_v&, T> || std::is_convertible_v, T> || + std::is_convertible_v&, T> || std::is_convertible_v, T>; + + template + inline static constexpr bool four_insanity_v = + std::is_constructible_v, expected&> || std::is_constructible_v, expected> || + std::is_constructible_v, const expected&> || std::is_constructible_v, const expected>; + + public: + template, 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 GF = const G&, std::enable_if_t< + (!std::is_convertible_v || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr explicit expected(const expected& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + template || !std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr explicit expected(expected&& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + template, class GF = const G&, std::enable_if_t< + (std::is_convertible_v && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr expected(const expected& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + template && std::is_convertible_v) && (std::is_constructible_v || std::is_void_v) && + std::is_constructible_v && !eight_insanity_v < U, G>&& !four_insanity_v, bool> = true> + + constexpr expected(expected&& other): + v(other.has_value() ? std::forward(*other) : std::forward(other.error())) + {} + + + /* + * (6) + */ + + template && + !std::is_same_v, void> && + !std::is_same_v, std::in_place_t> && + !std::is_same_v> && + std::is_constructible_v && + !std::is_same_v, unexpected> && + !std::is_same_v, expected>, bool> = true> + constexpr explicit expected(U&& v): v(T(std::forward(v))) + {} + + template && + !std::is_same_v, void> && + !std::is_same_v, std::in_place_t> && + !std::is_same_v> && + std::is_constructible_v && + !std::is_same_v, unexpected> && + !std::is_same_v, expected>, bool> = true> + constexpr expected(U&& v): v(T(std::forward(v))) + {} + + /* + * (7) + */ + + template>, std::enable_if_t< + !std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr explicit expected(const unexpected& e): v(std::forward(e.error())) + {} + + template>, std::enable_if_t< + std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr expected(const unexpected& e): v(std::forward(e.error())) + {} + + /* + * (8) + */ + + template>, std::enable_if_t< + !std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr explicit expected(unexpected&& e): v(std::forward(e.error())) + {} + + template>, std::enable_if_t< + std::is_convertible_v && std::is_constructible_v, bool> = true> + constexpr expected(unexpected&& e): v(std::forward(e.error())) + {} + + /* + * (9)...(13) + */ + template, bool> = true> + constexpr explicit expected(std::in_place_t, Args&& ... args): v(T(std::forward(args)...)) + {} + + template&, Args...>, bool> = true> + constexpr explicit expected(std::in_place_t, std::initializer_list il, Args&& ... args): v(T(il, std::forward(args)...)) + {} + +// template, void>, bool> = true> +// constexpr explicit expected(std::in_place_t) noexcept: v(T()) +// {} + + template, bool> = true> + constexpr explicit expected(unexpect_t, Args&& ... args): v(E(std::forward(args)...)) + {} + + template&, Args...>, bool> = true> + constexpr explicit expected(unexpect_t, std::initializer_list il, Args&& ... args): v(E(il, std::forward(args)...)) + {} + + expected& operator=(const expected& copy) = delete; + + expected& operator=(expected&& move) = default; + + [[nodiscard]] constexpr explicit operator bool() const noexcept + { + return std::holds_alternative(v); + } + + [[nodiscard]] constexpr inline bool has_value() const noexcept + { + return std::holds_alternative(v); + } + + constexpr T& value()& + { + if (*this) + return std::get(v); + else + throw bad_expected_access(std::as_const(error())); + } + + constexpr const T& value() const& + { + if (*this) + return std::get(v); + else + throw bad_expected_access(std::as_const(error())); + } + + constexpr T&& value()&& + { + if (*this) + return std::get(v); + else + throw bad_expected_access(std::move(error())); + } + + constexpr const T&& value() const&& + { + if (*this) + return std::get(v); + else + throw bad_expected_access(std::move(error())); + } + + constexpr const E& error() const& noexcept + { + return std::get(v); + } + + constexpr E& error()& noexcept + { + return std::get(v); + } + + constexpr const E&& error() const&& noexcept + { + return std::get(v); + } + + constexpr E&& error()&& noexcept + { + return std::get(v); + } + + template && std::is_copy_constructible_v, bool> = true> + constexpr T value_or(U&& default_value) const& + { + return bool(*this) ? **this : static_cast(std::forward(default_value)); + } + + template && std::is_move_constructible_v, bool> = true> + constexpr T value_or(U&& default_value)&& + { + return bool(*this) ? std::move(**this) : static_cast(std::forward(default_value)); + } + + constexpr inline const T* operator->() const noexcept + { + return &std::get(v); + } + + constexpr inline T* operator->() noexcept + { + return &std::get(v); + } + + constexpr inline const T& operator*() const& noexcept + { + return std::get(v); + } + + constexpr inline T& operator*()& noexcept + { + return std::get(v); + } + + constexpr inline const T&& operator*() const&& noexcept + { + return std::move(std::get(v)); + } + + constexpr inline T&& operator*()&& noexcept + { + return std::move(std::get(v)); + } + }; + + template + class expected : expected + { + public: + using expected::expected; + + constexpr expected(const expected& copy): expected::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(var, code) [](var) -> auto { return code; } +/* + * std::visit(blt::lambda_visitor{ + * lambdas... + * }, data_variant); + */ + // TODO: WTF template - struct lambda_visitor : TLambdas... { + struct lambda_visitor : TLambdas ... + { using TLambdas::operator()...; }; - + #if __cplusplus < 202002L // explicit deduction guide (not needed as of C++20) diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 946ebad..65775fa 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 946ebad67a21212d11a0dd4deb7cdedc297d47bc +Subproject commit 65775fa09fecaa65d0b0022ab6bf091c0e509445 diff --git a/src/blt/std/assert.cpp b/src/blt/std/assert.cpp index d41cf29..229f46a 100644 --- a/src/blt/std/assert.cpp +++ b/src/blt/std/assert.cpp @@ -52,12 +52,14 @@ namespace blt { #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__) BLT_STACK_TRACE(50); BLT_ERROR("The assertion '%s' has failed in file '%s:%d'", expression, path, line); + if (msg != nullptr) + BLT_ERROR(msg); BLT_ERROR("Stack Trace:"); printStacktrace(messages, size, path, line); diff --git a/src/blt/std/system.cpp b/src/blt/std/system.cpp index 19083e5..e987d5d 100755 --- a/src/blt/std/system.cpp +++ b/src/blt/std/system.cpp @@ -4,5 +4,139 @@ * See LICENSE file for license detail */ #include -#include -#include \ No newline at end of file +#include + +#include /* for struct timeval */ +#include /* for CLK_TCK */ + +#include +#include + +#ifndef WIN32 + + #include + #include +#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 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::get_resources_process() + { + return get_resources(RUSAGE_SELF); + } + + std::optional 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 + } +} + +