diff --git a/cartographer/common/time.cc b/cartographer/common/time.cc index 8c2882d..b94d340 100644 --- a/cartographer/common/time.cc +++ b/cartographer/common/time.cc @@ -16,8 +16,13 @@ #include "cartographer/common/time.h" +#include +#include +#include #include +#include "glog/logging.h" + namespace cartographer { namespace common { @@ -50,5 +55,12 @@ common::Duration FromMilliseconds(const int64 milliseconds) { std::chrono::milliseconds(milliseconds)); } +double GetThreadCpuTimeSeconds() { + struct timespec thread_cpu_time; + CHECK(clock_gettime(CLOCK_THREAD_CPUTIME_ID, &thread_cpu_time) == 0) + << std::strerror(errno); + return thread_cpu_time.tv_sec + 1e-9 * thread_cpu_time.tv_nsec; +} + } // namespace common } // namespace cartographer diff --git a/cartographer/common/time.h b/cartographer/common/time.h index 7fffcb9..39ffe71 100644 --- a/cartographer/common/time.h +++ b/cartographer/common/time.h @@ -60,6 +60,9 @@ int64 ToUniversal(Time time); // For logging and unit tests, outputs the timestamp integer. std::ostream& operator<<(std::ostream& os, Time time); +// CPU time consumed by the thread so far, in seconds. +double GetThreadCpuTimeSeconds(); + } // namespace common } // namespace cartographer