Skip to content

Commit 1a5416f

Browse files
Avoid compilation errors in the ResourceMonitor on non-Linux platforms
1 parent 5cb37ee commit 1a5416f

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

library/cpp/include/wavemap/core/utils/profile/resource_monitor.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,10 @@ class ResourceMonitor {
144144
bool running_ = false;
145145

146146
/// @brief Stores the CPU time at the start of the current episode.
147-
timespec episode_start_cpu_time_{};
147+
std::timespec episode_start_cpu_time_{};
148148

149149
/// @brief Stores the wall clock time at the start of the current episode.
150-
timespec episode_start_wall_time_{};
150+
std::timespec episode_start_wall_time_{};
151151

152152
/// @brief Stores the CPU time duration of the last completed episode.
153153
Duration last_episode_cpu_duration_{};
@@ -168,7 +168,8 @@ class ResourceMonitor {
168168
* @param stop The ending POSIX timestamp.
169169
* @return The computed duration as a wavemap::Duration.
170170
*/
171-
static Duration computeDuration(const timespec& start, const timespec& stop);
171+
static Duration computeDuration(const std::timespec& start,
172+
const std::timespec& stop);
172173
};
173174

174175
} // namespace wavemap

library/cpp/src/core/utils/profile/resource_monitor.cc

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,17 @@
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

717
namespace wavemap {
818
void 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

Comments
 (0)