summaryrefslogtreecommitdiffstats
path: root/libnetdata/clocks/clocks.c
diff options
context:
space:
mode:
Diffstat (limited to 'libnetdata/clocks/clocks.c')
-rw-r--r--libnetdata/clocks/clocks.c31
1 files changed, 16 insertions, 15 deletions
diff --git a/libnetdata/clocks/clocks.c b/libnetdata/clocks/clocks.c
index e4dca6c8b..cabc0000e 100644
--- a/libnetdata/clocks/clocks.c
+++ b/libnetdata/clocks/clocks.c
@@ -11,14 +11,14 @@ usec_t clock_monotonic_resolution = 1000;
usec_t clock_realtime_resolution = 1000;
#ifndef HAVE_CLOCK_GETTIME
-inline int clock_gettime(clockid_t clk_id, struct timespec *ts) {
+inline int clock_gettime(clockid_t clk_id __maybe_unused, struct timespec *ts) {
struct timeval tv;
if(unlikely(gettimeofday(&tv, NULL) == -1)) {
error("gettimeofday() failed.");
return -1;
}
ts->tv_sec = tv.tv_sec;
- ts->tv_nsec = (tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC;
+ ts->tv_nsec = (long)((tv.tv_usec % USEC_PER_SEC) * NSEC_PER_USEC);
return 0;
}
#endif
@@ -197,28 +197,28 @@ void sleep_to_absolute_time(usec_t usec) {
if (!einval_printed) {
einval_printed++;
error(
- "Invalid time given to clock_nanosleep(): clockid = %d, tv_sec = %ld, tv_nsec = %ld",
+ "Invalid time given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
clock,
- req.tv_sec,
+ (long long)req.tv_sec,
req.tv_nsec);
}
} else if (ret == ENOTSUP) {
if (!enotsup_printed) {
enotsup_printed++;
error(
- "Invalid clock id given to clock_nanosleep(): clockid = %d, tv_sec = %ld, tv_nsec = %ld",
+ "Invalid clock id given to clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
clock,
- req.tv_sec,
+ (long long)req.tv_sec,
req.tv_nsec);
}
} else {
if (!eunknown_printed) {
eunknown_printed++;
error(
- "Unknown return value %d from clock_nanosleep(): clockid = %d, tv_sec = %ld, tv_nsec = %ld",
+ "Unknown return value %d from clock_nanosleep(): clockid = %d, tv_sec = %lld, tv_nsec = %ld",
ret,
clock,
- req.tv_sec,
+ (long long)req.tv_sec,
req.tv_nsec);
}
}
@@ -341,20 +341,21 @@ usec_t heartbeat_next(heartbeat_t *hb, usec_t tick) {
void sleep_usec(usec_t usec) {
// we expect microseconds (1.000.000 per second)
// but timespec is nanoseconds (1.000.000.000 per second)
- struct timespec rem, req = {
+ struct timespec rem = { 0, 0 }, req = {
.tv_sec = (time_t) (usec / USEC_PER_SEC),
.tv_nsec = (suseconds_t) ((usec % USEC_PER_SEC) * NSEC_PER_USEC)
};
#ifdef __linux__
- while ((errno = clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem)) != 0) {
+ while (clock_nanosleep(CLOCK_REALTIME, 0, &req, &rem) != 0) {
#else
- while ((errno = nanosleep(&req, &rem)) != 0) {
+ while (nanosleep(&req, &rem) != 0) {
#endif
- if (likely(errno == EINTR)) {
- req.tv_sec = rem.tv_sec;
- req.tv_nsec = rem.tv_nsec;
- } else {
+ if (likely(errno == EINTR && (rem.tv_sec || rem.tv_nsec))) {
+ req = rem;
+ rem = (struct timespec){ 0, 0 };
+ }
+ else {
#ifdef __linux__
error("Cannot clock_nanosleep(CLOCK_REALTIME) for %llu microseconds.", usec);
#else