From: Florian Forster Date: Wed, 26 Oct 2016 14:46:29 +0000 (+0200) Subject: src/daemon/utils_time.h: Return structs from CDTIME_T_TO_TIME{VAL,SPEC}. X-Git-Tag: collectd-5.7.0~45^2~4 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=a762b2b4a6d804139c9ab5f47afacc1a88d6c0c1;p=collectd.git src/daemon/utils_time.h: Return structs from CDTIME_T_TO_TIME{VAL,SPEC}. 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. --- diff --git a/src/amqp.c b/src/amqp.c index f9777a9b..882df7c5 100644 --- a/src/amqp.c +++ b/src/amqp.c @@ -677,25 +677,21 @@ static void *camqp_subscribe_thread (void *user_data) /* {{{ */ 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 c9e49f80..4843fc65 100644 --- a/src/daemon/collectd.c +++ b/src/daemon/collectd.c @@ -341,7 +341,6 @@ static int do_loop (void) while (loop == 0) { - struct timespec ts_wait = { 0, 0 }; cdtime_t now; #if HAVE_LIBKSTAT @@ -361,7 +360,7 @@ static int do_loop (void) 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 7446b6fa..aa133786 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -522,12 +522,8 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) && (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'. */ diff --git a/src/daemon/utils_time.c b/src/daemon/utils_time.c index a3a9c33a..b24ceac4 100644 --- a/src/daemon/utils_time.c +++ b/src/daemon/utils_time.c @@ -86,15 +86,12 @@ cdtime_t cdtime (void) /* {{{ */ 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; @@ -106,15 +103,12 @@ static int get_utc_time (cdtime_t t, struct tm *t_tm, long *nsec) /* {{{ */ 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; diff --git a/src/daemon/utils_time.h b/src/daemon/utils_time.h index 78347239..f9973698 100644 --- a/src/daemon/utils_time.h +++ b/src/daemon/utils_time.h @@ -68,16 +68,16 @@ extern cdtime_t cdtime_mock; #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); diff --git a/src/daemon/utils_time_test.c b/src/daemon/utils_time_test.c index 9f043683..e9db187e 100644 --- a/src/daemon/utils_time_test.c +++ b/src/daemon/utils_time_test.c @@ -67,9 +67,6 @@ DEF_TEST(conversion) }; 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)); @@ -77,12 +74,12 @@ DEF_TEST(conversion) 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 04e5a1e6..c8e794eb 100644 --- a/src/dns.c +++ b/src/dns.c @@ -278,30 +278,16 @@ static int dns_run_pcap_loop (void) 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 3c636f7d..7d89596f 100644 --- a/src/dpdkstat.c +++ b/src/dpdkstat.c @@ -447,12 +447,12 @@ static int dpdk_helper_run(void) { 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()" @@ -562,8 +562,7 @@ static void dpdk_submit_xstats(const char *dev_name, 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, '_'); @@ -621,8 +620,7 @@ static void dpdk_submit_xstats(const char *dev_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); } } @@ -700,10 +698,9 @@ static int dpdk_read(user_data_t *ud) { /* 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 d7aa4e9d..5bbd9d53 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -377,7 +377,6 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ struct sockaddr_un sa_unix = { 0 }; - struct timeval stv_timeout; cdtime_t cdt_timeout; sd = socket (PF_UNIX, item->socktype, 0); @@ -423,9 +422,9 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ 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);