22
33#include < fstream>
44#include < iomanip>
5+ #include < optional>
56#include < sstream>
7+ #include < string>
8+
9+ // Borrowed from BOOST_HAS_CLOCK_GETTIME
10+ // NOTE: This is predicated on _POSIX_TIMERS (also on _XOPEN_REALTIME
11+ // but at least one platform - linux - defines that flag without
12+ // defining clock_gettime):
13+ #if (defined(_POSIX_TIMERS) && (_POSIX_TIMERS + 0 >= 0))
14+ #define HAS_CLOCK_GETTIME
15+ #endif
616
717namespace wavemap {
818void ResourceMonitor::start () {
@@ -11,8 +21,13 @@ void ResourceMonitor::start() {
1121 return ;
1222 }
1323
24+ #ifdef HAS_CLOCK_GETTIME
1425 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &episode_start_cpu_time_);
1526 clock_gettime (CLOCK_MONOTONIC, &episode_start_wall_time_);
27+ #else
28+ LOG (WARNING) << " Measuring CPU time has not yet been implemented for the "
29+ " current platform." ;
30+ #endif
1631 running_ = true ;
1732}
1833
@@ -22,10 +37,15 @@ void ResourceMonitor::stop() {
2237 return ;
2338 }
2439
25- timespec episode_stop_cpu_time{};
26- timespec episode_stop_wall_time{};
40+ std::timespec episode_stop_cpu_time{};
41+ std::timespec episode_stop_wall_time{};
42+ #ifdef HAS_CLOCK_GETTIME
2743 clock_gettime (CLOCK_PROCESS_CPUTIME_ID, &episode_stop_cpu_time);
2844 clock_gettime (CLOCK_MONOTONIC, &episode_stop_wall_time);
45+ #else
46+ LOG (WARNING) << " Measuring CPU time has not yet been implemented for the "
47+ " current platform." ;
48+ #endif
2949 running_ = false ;
3050
3151 last_episode_cpu_duration_ =
@@ -95,8 +115,8 @@ std::string ResourceMonitor::getTotalResourceUsageStats() const {
95115 return oss.str ();
96116}
97117
98- Duration ResourceMonitor::computeDuration (const timespec& start,
99- const timespec& stop) {
118+ Duration ResourceMonitor::computeDuration (const std:: timespec& start,
119+ const std:: timespec& stop) {
100120 return std::chrono::seconds (stop.tv_sec - start.tv_sec ) +
101121 std::chrono::nanoseconds (stop.tv_nsec - start.tv_nsec );
102122}
0 commit comments