summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: bf70b5b)
raw | patch | inline | side by side (parent: bf70b5b)
author | Florian Forster <octo@collectd.org> | |
Wed, 26 Oct 2016 14:46:29 +0000 (16:46 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Wed, 26 Oct 2016 14:46:29 +0000 (16:46 +0200) |
Since these are macros use composite literals, you can even take the
address of these struct, which is very handy for calling nanosleep()
and friends.
address of these struct, which is very handy for calling nanosleep()
and friends.
diff --git a/src/amqp.c b/src/amqp.c
index f9777a9b2a6f390c4ed4eec927f0e01465538f16..882df7c576d0bc513afa54c87c18d58278b880c1 100644 (file)
--- a/src/amqp.c
+++ b/src/amqp.c
status = camqp_connect (conf);
if (status != 0)
{
- struct timespec ts_interval;
ERROR ("amqp plugin: camqp_connect failed. "
"Will sleep for %.3f seconds.",
CDTIME_T_TO_DOUBLE (interval));
- CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
- nanosleep (&ts_interval, /* remaining = */ NULL);
+ nanosleep (&CDTIME_T_TO_TIMESPEC (interval), /* remaining = */ NULL);
continue;
}
status = amqp_simple_wait_frame (conf->connection, &frame);
if (status < 0)
{
- struct timespec ts_interval;
ERROR ("amqp plugin: amqp_simple_wait_frame failed. "
"Will sleep for %.3f seconds.",
CDTIME_T_TO_DOUBLE (interval));
camqp_close_connection (conf);
- CDTIME_T_TO_TIMESPEC (interval, &ts_interval);
- nanosleep (&ts_interval, /* remaining = */ NULL);
+ nanosleep (&CDTIME_T_TO_TIMESPEC (interval), /* remaining = */ NULL);
continue;
}
diff --git a/src/daemon/collectd.c b/src/daemon/collectd.c
index c9e49f8070536e8d1b99044c74fa0352b7c40178..4843fc659bd46d5c14191a26dc5b70f78b815645 100644 (file)
--- a/src/daemon/collectd.c
+++ b/src/daemon/collectd.c
while (loop == 0)
{
- struct timespec ts_wait = { 0, 0 };
cdtime_t now;
#if HAVE_LIBKSTAT
continue;
}
- CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait);
+ struct timespec ts_wait = CDTIME_T_TO_TIMESPEC (wait_until - now);
wait_until = wait_until + interval;
while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0))
diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c
index 7446b6fab84dc300f41a9675d9653bfdfdf69a3c..aa133786e60c40933c87b02b8fdd63c6f52b8cb7 100644 (file)
--- a/src/daemon/plugin.c
+++ b/src/daemon/plugin.c
&& (cdtime () < rf->rf_next_read)
&& rc == 0)
{
- struct timespec ts = { 0 };
-
- CDTIME_T_TO_TIMESPEC (rf->rf_next_read, &ts);
-
rc = pthread_cond_timedwait (&read_cond, &read_lock,
- &ts);
+ &CDTIME_T_TO_TIMESPEC (rf->rf_next_read));
}
/* Must hold `read_lock' when accessing `rf->rf_type'. */
index a3a9c33ae78343a291d07fd5d6cddee572bf3d68..b24ceac4ac749f1831effab1cb8825b0393701e3 100644 (file)
--- a/src/daemon/utils_time.c
+++ b/src/daemon/utils_time.c
static int get_utc_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
{
- struct timespec t_spec;
- int status;
-
- CDTIME_T_TO_TIMESPEC (t, &t_spec);
+ struct timespec t_spec = CDTIME_T_TO_TIMESPEC (t);
NORMALIZE_TIMESPEC (t_spec);
if (gmtime_r (&t_spec.tv_sec, t_tm) == NULL) {
char errbuf[1024];
- status = errno;
+ int status = errno;
ERROR ("get_utc_time: gmtime_r failed: %s",
sstrerror (status, errbuf, sizeof (errbuf)));
return status;
static int get_local_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */
{
- struct timespec t_spec;
- int status;
-
- CDTIME_T_TO_TIMESPEC (t, &t_spec);
+ struct timespec t_spec = CDTIME_T_TO_TIMESPEC (t);
NORMALIZE_TIMESPEC (t_spec);
if (localtime_r (&t_spec.tv_sec, t_tm) == NULL) {
char errbuf[1024];
- status = errno;
+ int status = errno;
ERROR ("get_local_time: localtime_r failed: %s",
sstrerror (status, errbuf, sizeof (errbuf)));
return status;
index 7834723917587a9e1837ef29a72e373f08c90f3d..f9973698ec2a59923d0204b5918370bc57c7d4dd 100644 (file)
--- a/src/daemon/utils_time.h
+++ b/src/daemon/utils_time.h
#define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0)
#define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0))
-#define CDTIME_T_TO_TIMEVAL(cdt,tvp) do { \
- (tvp)->tv_sec = (time_t) ((cdt) >> 30); \
- (tvp)->tv_usec = (suseconds_t) ((((cdt) & 0x3fffffff) * 1000000 + (1 << 29)) >> 30); \
-} while (0)
+#define CDTIME_T_TO_TIMEVAL(t) (struct timeval) { \
+ .tv_sec = (time_t) ((t) >> 30), \
+ .tv_usec = (suseconds_t) ((((t) & 0x3fffffff) * 1000000 + (1 << 29)) >> 30), \
+}
#define TIMEVAL_TO_CDTIME_T(tv) US_TO_CDTIME_T(1000000 * (tv)->tv_sec + (tv)->tv_usec)
-#define CDTIME_T_TO_TIMESPEC(cdt,tsp) do { \
- (tsp)->tv_sec = (time_t) ((cdt) >> 30); \
- (tsp)->tv_nsec = (long) ((((cdt) & 0x3fffffff) * 1000000000 + (1 << 29)) >> 30); \
-} while (0)
+#define CDTIME_T_TO_TIMESPEC(t) (struct timespec) { \
+ .tv_sec = (time_t) ((t) >> 30), \
+ .tv_nsec = (long) ((((t) & 0x3fffffff) * 1000000000 + (1 << 29)) >> 30), \
+}
#define TIMESPEC_TO_CDTIME_T(ts) NS_TO_CDTIME_T(1000000000ULL * (ts)->tv_sec + (ts)->tv_nsec)
cdtime_t cdtime (void);
index 9f043683e701dd772ce1d2433222c79618ab5251..e9db187e77403c5e921ccdc027d8f8dbdd38e264 100644 (file)
};
for (size_t i = 0; i < (sizeof (cases) / sizeof (cases[0])); i++) {
- struct timeval tv;
- struct timespec ts;
-
// cdtime -> s
EXPECT_EQ_UINT64 (cases[i].tt, CDTIME_T_TO_TIME_T (cases[i].t));
EXPECT_EQ_UINT64(cases[i].ms, CDTIME_T_TO_MS (cases[i].t));
// cdtime -> us
- CDTIME_T_TO_TIMEVAL (cases[i].t, &tv);
+ struct timeval tv = CDTIME_T_TO_TIMEVAL (cases[i].t);
EXPECT_EQ_UINT64 (cases[i].tv.tv_sec, tv.tv_sec);
EXPECT_EQ_UINT64 (cases[i].tv.tv_usec, tv.tv_usec);
// cdtime -> ns
- CDTIME_T_TO_TIMESPEC (cases[i].t, &ts);
+ struct timespec ts = CDTIME_T_TO_TIMESPEC (cases[i].t);
EXPECT_EQ_UINT64 (cases[i].ts.tv_sec, ts.tv_sec);
EXPECT_EQ_UINT64 (cases[i].ts.tv_nsec, ts.tv_nsec);
diff --git a/src/dns.c b/src/dns.c
index 04e5a1e6459671d04aab853256616514a3ad5a32..c8e794eb032f83dd786455767819214548c758a3 100644 (file)
--- a/src/dns.c
+++ b/src/dns.c
static int dns_sleep_one_interval (void) /* {{{ */
{
- cdtime_t interval;
- struct timespec ts = { 0, 0 };
- int status = 0;
-
- interval = plugin_get_interval ();
- CDTIME_T_TO_TIMESPEC (interval, &ts);
-
- while (42)
+ struct timespec ts = CDTIME_T_TO_TIMESPEC (plugin_get_interval ());
+ while (nanosleep (&ts, &ts) != 0)
{
- struct timespec rem = { 0, 0 };
-
- status = nanosleep (&ts, &rem);
- if (status == 0)
- break;
- else if ((errno == EINTR) || (errno == EAGAIN))
- {
- ts = rem;
+ if ((errno == EINTR) || (errno == EAGAIN))
continue;
- }
- else
- break;
+
+ return (errno);
}
- return (status);
+ return (0);
} /* }}} int dns_sleep_one_interval */
static void *dns_child_loop (__attribute__((unused)) void *dummy) /* {{{ */
diff --git a/src/dpdkstat.c b/src/dpdkstat.c
index 3c636f7dbbebd0a05872a9fb1b6fd3741179816c..7d89596f176a7b0413131116c41d1572fe6b2d4b 100644 (file)
--- a/src/dpdkstat.c
+++ b/src/dpdkstat.c
while (1) {
/* sem_timedwait() to avoid blocking forever */
- struct timespec ts;
cdtime_t now = cdtime();
cdtime_t safety_period = MS_TO_CDTIME_T(1500);
- CDTIME_T_TO_TIMESPEC(now + safety_period + g_configuration->interval * 2,
- &ts);
- int ret = sem_timedwait(&g_configuration->sema_helper_get_stats, &ts);
+ int ret =
+ sem_timedwait(&g_configuration->sema_helper_get_stats,
+ &CDTIME_T_TO_TIMESPEC(now + safety_period +
+ g_configuration->interval * 2));
if (ret == -1 && errno == ETIMEDOUT) {
ERROR("dpdkstat-helper: sem timedwait()"
vl.values_len = 1; /* Submit stats one at a time */
vl.time = port_read_time;
sstrncpy(vl.plugin, "dpdkstat", sizeof(vl.plugin));
- sstrncpy(vl.plugin_instance, dev_name,
- sizeof(vl.plugin_instance));
+ sstrncpy(vl.plugin_instance, dev_name, sizeof(vl.plugin_instance));
type_end = strrchr(xstats[j].name, '_');
sstrncpy(vl.type, "derive", sizeof(vl.type));
}
- sstrncpy(vl.type_instance, xstats[j].name,
- sizeof(vl.type_instance));
+ sstrncpy(vl.type_instance, xstats[j].name, sizeof(vl.type_instance));
plugin_dispatch_values(&vl);
}
}
/* Kick helper process through SHM */
sem_post(&g_configuration->sema_helper_get_stats);
- struct timespec ts;
cdtime_t now = cdtime();
- CDTIME_T_TO_TIMESPEC(now + g_configuration->interval, &ts);
- ret = sem_timedwait(&g_configuration->sema_stats_in_shm, &ts);
+ ret = sem_timedwait(&g_configuration->sema_stats_in_shm,
+ &CDTIME_T_TO_TIMESPEC(now + g_configuration->interval));
if (ret == -1) {
if (errno == ETIMEDOUT)
DEBUG(
diff --git a/src/powerdns.c b/src/powerdns.c
index d7aa4e9dbe8ea8d94ca739baa2750b1d2026a707..5bbd9d53e915c5f64868e2c3ce77b6b1f929c0aa 100644 (file)
--- a/src/powerdns.c
+++ b/src/powerdns.c
struct sockaddr_un sa_unix = { 0 };
- struct timeval stv_timeout;
cdtime_t cdt_timeout;
sd = socket (PF_UNIX, item->socktype, 0);
if (cdt_timeout < TIME_T_TO_CDTIME_T (2))
cdt_timeout = TIME_T_TO_CDTIME_T (2);
- CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout);
-
- status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout));
+ status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO,
+ &CDTIME_T_TO_TIMEVAL(cdt_timeout),
+ sizeof(struct timeval));
if (status != 0)
{
SOCK_ERROR ("setsockopt", sa_unix.sun_path);