From 0b907867a2908b747dc9a056a2b3b9a2755a345c Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 17 Sep 2023 15:22:37 -0400 Subject: [PATCH 1/2] argparse get_cast update to work with all is_arithmetic types --- include/blt/parse/argparse.h | 26 +++++++++++++++----------- libraries/parallel-hashmap | 2 +- src/blt/profiling/profiler.cpp | 6 +++--- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/include/blt/parse/argparse.h b/include/blt/parse/argparse.h index 747d3b7..15ea9a7 100755 --- a/include/blt/parse/argparse.h +++ b/include/blt/parse/argparse.h @@ -375,18 +375,22 @@ namespace blt { if constexpr (std::is_same_v) return std::get(v); - else - { - auto t = std::get(v); - // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted - // it is up to the user to deal with the variant if they do not want this behaviour! - if constexpr (std::is_same_v) - { - if (std::holds_alternative(t)) - return std::stoi(std::get(t)); - } + auto t = std::get(v); + // user is requesting an int, but holds a string, we are going to make the assumption the data can be converted + // it is up to the user to deal with the variant if they do not want this behaviour! + if constexpr (!std::is_arithmetic_v) return std::get(t); - } + // ugly! + if (std::holds_alternative(t)) + return static_cast(std::get(t)); + if (std::holds_alternative(t)) + return static_cast(std::get(t)); + auto s = std::get(t); + if constexpr (std::is_floating_point_v) + return static_cast(std::stod(s)); + if constexpr (std::is_signed_v) + return static_cast(std::stoll(s)); + return static_cast(std::stoull(s)); } public: diff --git a/libraries/parallel-hashmap b/libraries/parallel-hashmap index 93201da..77cab81 160000 --- a/libraries/parallel-hashmap +++ b/libraries/parallel-hashmap @@ -1 +1 @@ -Subproject commit 93201da2ba5a6aba0a6e57ada64973555629b3e3 +Subproject commit 77cab8192a879e5d27188f97e8f2080dd7e36ca8 diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index d006c44..5e8f6b7 100755 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -148,7 +148,7 @@ namespace blt::profiling { void startInterval(const std::string& profileName, const std::string& intervalName) { std::scoped_lock lock(profileLock); capture_interval interval{}; - interval.start = system::getCurrentTimeNanoseconds(); + interval.start = system::getCPUThreadTime(); profiles[profileName].intervals[intervalName] = interval; } @@ -157,7 +157,7 @@ namespace blt::profiling { auto& prof = profiles[profileName]; auto& ref = prof.intervals[intervalName]; - ref.end = system::getCurrentTimeNanoseconds(); + ref.end = system::getCPUThreadTime(); auto difference = ref.end - ref.start; auto& href = prof.intervals_total[intervalName]; @@ -169,7 +169,7 @@ namespace blt::profiling { void point(const std::string& profileName, const std::string& pointName) { std::scoped_lock lock(profileLock); capture_point point{}; - point.point = system::getCurrentTimeNanoseconds(); + point.point = system::getCPUThreadTime(); point.name = pointName; profiles[profileName].points.push(point); } From a438baeca3c4bc5ee7d2eb995680334ccf1ab405 Mon Sep 17 00:00:00 2001 From: Brett Date: Sun, 17 Sep 2023 16:24:53 -0400 Subject: [PATCH 2/2] fix time returning only extra nanoseconds. function now returns the total nanoseconds sine starting the program this might be changed in the future --- include/blt/std/time.h | 10 +++++----- src/blt/profiling/profiler.cpp | 9 ++++++--- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/include/blt/std/time.h b/include/blt/std/time.h index 50d6759..970294d 100755 --- a/include/blt/std/time.h +++ b/include/blt/std/time.h @@ -43,23 +43,23 @@ namespace blt::system return std::chrono::duration_cast(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); } - static inline auto getCPUThreadTime() + static inline int64_t getCPUThreadTime() { #ifdef unix timespec time{}; clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time); - return time.tv_nsec; + return time.tv_sec * static_cast(1e9) + time.tv_nsec; #else return std::clock(); #endif } - static inline auto getCPUTime() + static inline int64_t getCPUTime() { #ifdef unix timespec time{}; clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time); - return time.tv_nsec; + return time.tv_sec * static_cast(1e9) + time.tv_nsec; #else return std::clock(); #endif @@ -70,7 +70,7 @@ namespace blt::system #ifdef unix timespec res{}; clock_getres(CLOCK_PROCESS_CPUTIME_ID, &res); - return res.tv_nsec; + return res.tv_sec * static_cast(1e9) + res.tv_nsec; #else return CLOCKS_PER_SECOND; #endif diff --git a/src/blt/profiling/profiler.cpp b/src/blt/profiling/profiler.cpp index 5e8f6b7..5ee5810 100755 --- a/src/blt/profiling/profiler.cpp +++ b/src/blt/profiling/profiler.cpp @@ -12,6 +12,8 @@ #include #include +#define TIME_FUNCTION blt::system::getCPUThreadTime() + namespace blt::profiling { // TODO: a better way @@ -148,7 +150,7 @@ namespace blt::profiling { void startInterval(const std::string& profileName, const std::string& intervalName) { std::scoped_lock lock(profileLock); capture_interval interval{}; - interval.start = system::getCPUThreadTime(); + interval.start = TIME_FUNCTION; profiles[profileName].intervals[intervalName] = interval; } @@ -157,9 +159,10 @@ namespace blt::profiling { auto& prof = profiles[profileName]; auto& ref = prof.intervals[intervalName]; - ref.end = system::getCPUThreadTime(); + ref.end = TIME_FUNCTION; auto difference = ref.end - ref.start; + auto& href = prof.intervals_total[intervalName]; href.total += difference; @@ -169,7 +172,7 @@ namespace blt::profiling { void point(const std::string& profileName, const std::string& pointName) { std::scoped_lock lock(profileLock); capture_point point{}; - point.point = system::getCPUThreadTime(); + point.point = TIME_FUNCTION; point.name = pointName; profiles[profileName].points.push(point); }