fix time returning only extra nanoseconds. function now returns the total nanoseconds sine starting the program

this might be changed in the future
v1
Brett 2023-09-17 16:24:53 -04:00
parent 0b907867a2
commit a438baeca3
2 changed files with 11 additions and 8 deletions

View File

@ -43,23 +43,23 @@ namespace blt::system
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count(); return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch()).count();
} }
static inline auto getCPUThreadTime() static inline int64_t getCPUThreadTime()
{ {
#ifdef unix #ifdef unix
timespec time{}; timespec time{};
clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time); clock_gettime(CLOCK_THREAD_CPUTIME_ID, &time);
return time.tv_nsec; return time.tv_sec * static_cast<int64_t>(1e9) + time.tv_nsec;
#else #else
return std::clock(); return std::clock();
#endif #endif
} }
static inline auto getCPUTime() static inline int64_t getCPUTime()
{ {
#ifdef unix #ifdef unix
timespec time{}; timespec time{};
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time); clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &time);
return time.tv_nsec; return time.tv_sec * static_cast<int64_t>(1e9) + time.tv_nsec;
#else #else
return std::clock(); return std::clock();
#endif #endif
@ -70,7 +70,7 @@ namespace blt::system
#ifdef unix #ifdef unix
timespec res{}; timespec res{};
clock_getres(CLOCK_PROCESS_CPUTIME_ID, &res); clock_getres(CLOCK_PROCESS_CPUTIME_ID, &res);
return res.tv_nsec; return res.tv_sec * static_cast<int64_t>(1e9) + res.tv_nsec;
#else #else
return CLOCKS_PER_SECOND; return CLOCKS_PER_SECOND;
#endif #endif

View File

@ -12,6 +12,8 @@
#include <algorithm> #include <algorithm>
#include <blt/std/format.h> #include <blt/std/format.h>
#define TIME_FUNCTION blt::system::getCPUThreadTime()
namespace blt::profiling { namespace blt::profiling {
// TODO: a better way // TODO: a better way
@ -148,7 +150,7 @@ namespace blt::profiling {
void startInterval(const std::string& profileName, const std::string& intervalName) { void startInterval(const std::string& profileName, const std::string& intervalName) {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
capture_interval interval{}; capture_interval interval{};
interval.start = system::getCPUThreadTime(); interval.start = TIME_FUNCTION;
profiles[profileName].intervals[intervalName] = interval; profiles[profileName].intervals[intervalName] = interval;
} }
@ -157,9 +159,10 @@ namespace blt::profiling {
auto& prof = profiles[profileName]; auto& prof = profiles[profileName];
auto& ref = prof.intervals[intervalName]; auto& ref = prof.intervals[intervalName];
ref.end = system::getCPUThreadTime(); ref.end = TIME_FUNCTION;
auto difference = ref.end - ref.start; auto difference = ref.end - ref.start;
auto& href = prof.intervals_total[intervalName]; auto& href = prof.intervals_total[intervalName];
href.total += difference; href.total += difference;
@ -169,7 +172,7 @@ namespace blt::profiling {
void point(const std::string& profileName, const std::string& pointName) { void point(const std::string& profileName, const std::string& pointName) {
std::scoped_lock lock(profileLock); std::scoped_lock lock(profileLock);
capture_point point{}; capture_point point{};
point.point = system::getCPUThreadTime(); point.point = TIME_FUNCTION;
point.name = pointName; point.name = pointName;
profiles[profileName].points.push(point); profiles[profileName].points.push(point);
} }