diff --git a/source/mijin/util/os.cpp b/source/mijin/util/os.cpp index 973268e..54add49 100644 --- a/source/mijin/util/os.cpp +++ b/source/mijin/util/os.cpp @@ -10,6 +10,7 @@ #include #include #include + #include #elif MIJIN_TARGET_OS == MIJIN_OS_WINDOWS #include #include @@ -42,7 +43,16 @@ namespace mijin namespace { std::mutex gDlErrorMutex; // dlerror may not be thread-safe -} + +const std::uint64_t gCPUClockResolution = []() +{ + struct timespec time; + [[maybe_unused]] const int result = clock_getres(CLOCK_PROCESS_CPUTIME_ID, &time); + MIJIN_ASSERT(result == 0, "Error getting cputime resolution."); + MIJIN_ASSERT(time.tv_sec == 0, "What kind of cpu clock is this?"); + return static_cast(time.tv_nsec); +}(); +}; #endif // MIJIN_TARGET_OS == MIJIN_OS_LINUX // @@ -178,4 +188,26 @@ void alignedFree(void* ptr) #endif } +std::uint64_t getCPUTicks() MIJIN_NOEXCEPT +{ +#if MIJIN_TARGET_OS == MIJIN_OS_LINUX + struct timespec time; + [[maybe_unused]] const int result = clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time); + MIJIN_ASSERT(result == 0, "Error getting cpu time."); + return (static_cast(time.tv_sec) * 1e9 + time.tv_nsec) / gCPUClockResolution; +#else + MIJIN_ERROR("TODO"); + return 0; +#endif +} + +std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT +{ +#if MIJIN_TARGET_OS == MIJIN_OS_LINUX + return 1e9 / gCPUClockResolution; +#else + MIJIN_ERROR("TODO"); + return 0; +#endif +} } // namespace mijin diff --git a/source/mijin/util/os.hpp b/source/mijin/util/os.hpp index 0d10dff..2512be1 100644 --- a/source/mijin/util/os.hpp +++ b/source/mijin/util/os.hpp @@ -5,6 +5,7 @@ #define MIJIN_UTIL_OS_HPP_INCLUDED 1 #include +#include #include #include #include @@ -85,6 +86,9 @@ void setCurrentThreadName(const char* threadName) MIJIN_NOEXCEPT; [[nodiscard]] void* alignedRealloc(void* ptr, std::size_t alignment, std::size_t size) MIJIN_NOEXCEPT; void alignedFree(void* ptr); +[[nodiscard]] std::uint64_t getCPUTicks() MIJIN_NOEXCEPT; +[[nodiscard]] std::uint64_t getCPUTicksPerSecond() MIJIN_NOEXCEPT; + SharedLibrary::~SharedLibrary() MIJIN_NOEXCEPT { close();