Use unsigned long long microseconds instead of floating point in get_current_process_creation_time() to avoid rounding errors.

This commit is contained in:
Ion Gaztañaga
2024-10-18 23:15:01 +02:00
parent 75c3c9ef0a
commit 39705606e3
2 changed files with 12 additions and 9 deletions

View File

@@ -47,8 +47,10 @@ inline void get_pid_creation_time_str(std::string &s)
{
std::stringstream stream;
stream << get_current_process_id() << '_';
stream.precision(6);
stream << std::fixed << get_current_process_creation_time();
const unsigned long long total_microsecs = get_current_process_creation_time();
const unsigned long secs = static_cast<unsigned long>(total_microsecs/1000000ul);
const unsigned long usecs = static_cast<unsigned long>(total_microsecs%1000000ul);
stream << secs << '.' << usecs;
s = stream.str();
}

View File

@@ -220,17 +220,18 @@ inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
return get_invalid_thread_id();
}
inline long double get_current_process_creation_time()
inline unsigned long long get_current_process_creation_time()
{
winapi::interprocess_filetime CreationTime, ExitTime, KernelTime, UserTime;
winapi::get_process_times
( winapi::get_current_process(), &CreationTime, &ExitTime, &KernelTime, &UserTime);
typedef long double ldouble_t;
const ldouble_t resolution = (100.0l/1000000000.0l);
return CreationTime.dwHighDateTime*(ldouble_t(1u<<31u)*2.0l*resolution) +
CreationTime.dwLowDateTime*resolution;
unsigned long long microsecs = CreationTime.dwHighDateTime;
microsecs <<= 32u;
microsecs |= CreationTime.dwLowDateTime;
microsecs /= 10u;
return microsecs;
}
inline unsigned int get_num_cores()
@@ -476,8 +477,8 @@ inline OS_systemwide_thread_id_t get_invalid_systemwide_thread_id()
return OS_systemwide_thread_id_t(get_invalid_process_id(), get_invalid_thread_id());
}
inline long double get_current_process_creation_time()
{ return 0.0L; }
inline unsigned long long get_current_process_creation_time()
{ return 0u; }
inline unsigned int get_num_cores()
{