From a04ffbda508739433df0975328100e33e7986c87 Mon Sep 17 00:00:00 2001 From: Florian Forster Date: Wed, 3 Nov 2010 13:49:58 +0100 Subject: [PATCH] Many plugins and files: Convert "interval_g" and "vl->interval" to cdtime_t. --- bindings/java/org/collectd/api/ValueList.java | 6 ++ src/collectd.c | 68 +++++++++---------- src/collectd.h | 6 +- src/cpu.c | 2 +- src/csv.c | 6 +- src/dbi.c | 2 +- src/dns.c | 2 +- src/exec.c | 5 +- src/gmond.c | 2 +- src/ipmi.c | 3 +- src/java.c | 9 +-- src/memcached.c | 6 +- src/modbus.c | 16 ++--- src/network.c | 5 +- src/onewire.c | 5 +- src/oracle.c | 2 +- src/perl.c | 60 ++++++++-------- src/plugin.c | 29 ++++---- src/plugin.h | 2 +- src/postgresql.c | 40 +++-------- src/powerdns.c | 4 +- src/rrdtool.c | 4 +- src/snmp.c | 7 +- src/target_scale.c | 12 ++-- src/utils_cache.c | 7 +- src/utils_cmd_putval.c | 14 ++-- src/utils_db_query.c | 6 +- src/utils_db_query.h | 2 +- src/utils_format_json.c | 2 +- src/utils_rrdcreate.c | 18 +++-- src/utils_time.c | 2 +- src/utils_time.h | 6 +- src/write_http.c | 6 +- 33 files changed, 180 insertions(+), 186 deletions(-) diff --git a/bindings/java/org/collectd/api/ValueList.java b/bindings/java/org/collectd/api/ValueList.java index 1baeff24..b8d6f40f 100644 --- a/bindings/java/org/collectd/api/ValueList.java +++ b/bindings/java/org/collectd/api/ValueList.java @@ -87,10 +87,16 @@ public class ValueList extends PluginData { _ds = new DataSet (_type, dsrc); } + /** + * Returns the interval (in milliseconds) of the value list. + */ public long getInterval() { return _interval; } + /** + * Sets the interval (in milliseconds) of the value list. + */ public void setInterval(long interval) { _interval = interval; } diff --git a/src/collectd.c b/src/collectd.c index 277d3b0d..4f5fac90 100644 --- a/src/collectd.c +++ b/src/collectd.c @@ -40,7 +40,7 @@ * Global variables */ char hostname_g[DATA_MAX_NAME_LEN]; -int interval_g; +cdtime_t interval_g; int timeout_g; #if HAVE_LIBKSTAT kstat_ctl_t *kc; @@ -139,15 +139,25 @@ static int init_global_variables (void) str = global_option_get ("Interval"); if (str == NULL) - str = "10"; - interval_g = atoi (str); - if (interval_g <= 0) { - fprintf (stderr, "Cannot set the interval to a correct value.\n" - "Please check your settings.\n"); - return (-1); + interval_g = TIME_T_TO_CDTIME_T (10); + } + else + { + double tmp; + + tmp = atof (str); + if (tmp <= 0.0) + { + fprintf (stderr, "Cannot set the interval to a " + "correct value.\n" + "Please check your settings.\n"); + return (-1); + } + + interval_g = DOUBLE_TO_CDTIME_T (tmp); } - DEBUG ("interval_g = %i;", interval_g); + DEBUG ("interval_g = %.3f;", CDTIME_T_TO_DOUBLE (interval_g)); str = global_option_get ("Timeout"); if (str == NULL) @@ -311,22 +321,14 @@ static int do_init (void) static int do_loop (void) { - struct timeval tv_now; - struct timeval tv_next; - struct timeval tv_wait; - struct timespec ts_wait; + cdtime_t wait_until; + + wait_until = cdtime () + interval_g; while (loop == 0) { - if (gettimeofday (&tv_next, NULL) < 0) - { - char errbuf[1024]; - ERROR ("gettimeofday failed: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); - } - tv_next.tv_sec += interval_g; + struct timespec ts_wait = { 0, 0 }; + cdtime_t now; #if HAVE_LIBKSTAT update_kstat (); @@ -335,27 +337,20 @@ static int do_loop (void) /* Issue all plugins */ plugin_read_all (); - if (gettimeofday (&tv_now, NULL) < 0) - { - char errbuf[1024]; - ERROR ("gettimeofday failed: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); - } - - if (timeval_cmp (tv_next, tv_now, &tv_wait) <= 0) + now = cdtime (); + if (now >= wait_until) { WARNING ("Not sleeping because the next interval is " - "%i.%06i seconds in the past!", - (int) tv_wait.tv_sec, (int) tv_wait.tv_usec); + "%.3f seconds in the past!", + CDTIME_T_TO_DOUBLE (now - wait_until)); + wait_until = now + interval_g; continue; } - ts_wait.tv_sec = tv_wait.tv_sec; - ts_wait.tv_nsec = (long) (1000 * tv_wait.tv_usec); + CDTIME_T_TO_TIMESPEC (wait_until - now, &ts_wait); + wait_until = wait_until + interval_g; - while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) == -1)) + while ((loop == 0) && (nanosleep (&ts_wait, &ts_wait) != 0)) { if (errno != EINTR) { @@ -368,7 +363,6 @@ static int do_loop (void) } } /* while (loop == 0) */ - DEBUG ("return (0);"); return (0); } /* int do_loop */ diff --git a/src/collectd.h b/src/collectd.h index 7012f4d1..af0033a5 100644 --- a/src/collectd.h +++ b/src/collectd.h @@ -286,8 +286,8 @@ /* Type for time as used by "utils_time.h" */ typedef uint64_t cdtime_t; -extern char hostname_g[]; -extern int interval_g; -extern int timeout_g; +extern char hostname_g[]; +extern cdtime_t interval_g; +extern int timeout_g; #endif /* COLLECTD_H */ diff --git a/src/cpu.c b/src/cpu.c index 7aa6361b..3dbe80ee 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -163,7 +163,7 @@ static int init (void) DEBUG ("host_processors returned %i %s", (int) cpu_list_len, cpu_list_len == 1 ? "processor" : "processors"); INFO ("cpu plugin: Found %i processor%s.", (int) cpu_list_len, cpu_list_len == 1 ? "" : "s"); - cpu_temp_retry_max = 86400 / interval_g; + cpu_temp_retry_max = 86400 / CDTIME_T_TO_TIME_T (interval_g); /* #endif PROCESSOR_CPU_LOAD_INFO */ #elif defined(HAVE_LIBKSTAT) diff --git a/src/csv.c b/src/csv.c index 889ad62c..87a7b4e3 100644 --- a/src/csv.c +++ b/src/csv.c @@ -299,8 +299,10 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl, } fprintf (use_stdio == 1 ? stdout : stderr, - "PUTVAL %s interval=%i %s\n", - filename, vl->interval, values); + "PUTVAL %s interval=%.3f %s\n", + filename, + CDTIME_T_TO_DOUBLE (vl->interval), + values); return (0); } diff --git a/src/dbi.c b/src/dbi.c index cd9240a6..caa41ef6 100644 --- a/src/dbi.c +++ b/src/dbi.c @@ -566,7 +566,7 @@ static int cdbi_read_database_query (cdbi_database_t *db, /* {{{ */ udb_query_prepare_result (q, prep_area, hostname_g, /* plugin = */ "dbi", db->name, - column_names, column_num, /* interval = */ -1); + column_names, column_num, /* interval = */ 0); /* 0 = error; 1 = success; */ status = dbi_result_first_row (res); /* {{{ */ diff --git a/src/dns.c b/src/dns.c index 6e63fe44..47da4e94 100644 --- a/src/dns.c +++ b/src/dns.c @@ -226,7 +226,7 @@ static void *dns_child_loop (void __attribute__((unused)) *dummy) pcap_obj = pcap_open_live ((pcap_device != NULL) ? pcap_device : "any", PCAP_SNAPLEN, 0 /* Not promiscuous */, - interval_g, + (int) CDTIME_T_TO_MS (interval_g / 2), pcap_error); if (pcap_obj == NULL) { diff --git a/src/exec.c b/src/exec.c index 9aabe9fc..8d5ae9c3 100644 --- a/src/exec.c +++ b/src/exec.c @@ -270,13 +270,14 @@ static void set_environment (void) /* {{{ */ char buffer[1024]; #ifdef HAVE_SETENV - ssnprintf (buffer, sizeof (buffer), "%i", interval_g); + ssnprintf (buffer, sizeof (buffer), "%.3f", CDTIME_T_TO_DOUBLE (interval_g)); setenv ("COLLECTD_INTERVAL", buffer, /* overwrite = */ 1); ssnprintf (buffer, sizeof (buffer), "%s", hostname_g); setenv ("COLLECTD_HOSTNAME", buffer, /* overwrite = */ 1); #else - ssnprintf (buffer, sizeof (buffer), "COLLECTD_INTERVAL=%i", interval_g); + ssnprintf (buffer, sizeof (buffer), "COLLECTD_INTERVAL=%.3f", + CDTIME_T_TO_DOUBLE (interval_g)); putenv (buffer); ssnprintf (buffer, sizeof (buffer), "COLLECTD_HOSTNAME=%s", hostname_g); diff --git a/src/gmond.c b/src/gmond.c index 2ffc42a5..8c774e53 100644 --- a/src/gmond.c +++ b/src/gmond.c @@ -719,7 +719,7 @@ static int mc_handle_metadata_msg (Ganglia_metadata_msg *msg) /* {{{ */ map->type, map->type_instance, ds->ds_num); if (se != NULL) - se->vl.interval = (int) msg_meta.metric.tmax; + se->vl.interval = TIME_T_TO_CDTIME_T (msg_meta.metric.tmax); pthread_mutex_unlock (&staging_lock); if (se == NULL) diff --git a/src/ipmi.c b/src/ipmi.c index c631574d..f341320d 100644 --- a/src/ipmi.c +++ b/src/ipmi.c @@ -664,7 +664,8 @@ static int c_ipmi_init (void) int status; /* Don't send `ADD' notifications during startup (~ 1 minute) */ - c_ipmi_init_in_progress = 1 + (60 / interval_g); + time_t iv = CDTIME_T_TO_TIME_T (interval_g); + c_ipmi_init_in_progress = 1 + (60 / iv); c_ipmi_active = 1; diff --git a/src/java.c b/src/java.c index 4d490eb6..3089a533 100644 --- a/src/java.c +++ b/src/java.c @@ -819,7 +819,8 @@ static jobject ctoj_value_list (JNIEnv *jvm_env, /* {{{ */ } /* Set the `interval' member.. */ - status = ctoj_long (jvm_env, (jlong) vl->interval, + status = ctoj_long (jvm_env, + (jlong) CDTIME_T_TO_MS (vl->interval), c_valuelist, o_valuelist, "setInterval"); if (status != 0) { @@ -914,7 +915,7 @@ static jobject ctoj_notification (JNIEnv *jvm_env, /* {{{ */ return (NULL); } - /* Set the `interval' member.. */ + /* Set the `severity' member.. */ status = ctoj_int (jvm_env, (jint) n->severity, c_notification, o_notification, "setSeverity"); if (status != 0) @@ -1242,7 +1243,7 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */ return (-1); } /* Java measures time in milliseconds. */ - vl->time = (time_t) (tmp_long / ((jlong) 1000)); + vl->time = MS_TO_CDTIME_T (tmp_long); status = jtoc_long (jvm_env, &tmp_long, class_ptr, object_ptr, "getInterval"); @@ -1251,7 +1252,7 @@ static int jtoc_value_list (JNIEnv *jvm_env, value_list_t *vl, /* {{{ */ ERROR ("java plugin: jtoc_value_list: jtoc_long (getInterval) failed."); return (-1); } - vl->interval = (int) tmp_long; + vl->interval = MS_TO_CDTIME_T (tmp_long); status = jtoc_values_array (jvm_env, ds, vl, class_ptr, object_ptr); if (status != 0) diff --git a/src/memcached.c b/src/memcached.c index 8490bf66..8b4a8fc1 100644 --- a/src/memcached.c +++ b/src/memcached.c @@ -176,12 +176,14 @@ static int memcached_query_daemon (char *buffer, int buffer_size) /* {{{ */ p.events = POLLIN | POLLERR | POLLHUP; p.revents = 0; - status = poll (&p, /* nfds = */ 1, /* timeout = */ 1000 * interval_g); + status = poll (&p, /* nfds = */ 1, + /* timeout = */ CDTIME_T_TO_MS (interval_g)); if (status <= 0) { if (status == 0) { - ERROR ("memcached: poll(2) timed out after %i seconds.", interval_g); + ERROR ("memcached: poll(2) timed out after %.3f seconds.", + CDTIME_T_TO_DOUBLE (interval_g)); } else { diff --git a/src/modbus.c b/src/modbus.c index adab0d0f..3824097c 100644 --- a/src/modbus.c +++ b/src/modbus.c @@ -94,7 +94,7 @@ struct mb_host_s /* {{{ */ char node[NI_MAXHOST]; /* char service[NI_MAXSERV]; */ int port; - int interval; + cdtime_t interval; mb_slave_t *slaves; size_t slaves_num; @@ -771,7 +771,7 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ status = -1; } else if (strcasecmp ("Interval", child->key) == 0) - status = cf_util_get_int (child, &host->interval); + status = cf_util_get_cdtime (child, &host->interval); else if (strcasecmp ("Slave", child->key) == 0) /* Don't set status: Gracefully continue if a slave fails. */ mb_config_add_slave (host, child); @@ -797,21 +797,19 @@ static int mb_config_add_host (oconfig_item_t *ci) /* {{{ */ { user_data_t ud; char name[1024]; - struct timespec interval; + struct timespec interval = { 0, 0 }; ud.data = host; ud.free_func = host_free; ssnprintf (name, sizeof (name), "modbus-%s", host->host); - interval.tv_nsec = 0; - if (host->interval > 0) - interval.tv_sec = host->interval; - else - interval.tv_sec = 0; + CDTIME_T_TO_TIMESPEC (host->interval, &interval); plugin_register_complex_read (/* group = */ NULL, name, - mb_read, (interval.tv_sec > 0) ? &interval : NULL, &ud); + /* callback = */ mb_read, + /* interval = */ (host->interval > 0) ? &interval : NULL, + &ud); } else { diff --git a/src/network.c b/src/network.c index 6d53504c..43193cfc 100644 --- a/src/network.c +++ b/src/network.c @@ -1387,7 +1387,7 @@ static int parse_packet (sockent_t *se, /* {{{ */ status = parse_part_number (&buffer, &buffer_size, &tmp); if (status == 0) - vl.interval = (int) tmp; + vl.interval = TIME_T_TO_CDTIME_T (tmp); } else if (pkg_type == TYPE_HOST) { @@ -2592,8 +2592,9 @@ static int add_to_buffer (char *buffer, int buffer_size, /* {{{ */ if (vl_def->interval != vl->interval) { + /* TODO: Create a new type for sub-second intervals. */ if (write_part_number (&buffer, &buffer_size, TYPE_INTERVAL, - (uint64_t) vl->interval)) + (uint64_t) CDTIME_T_TO_TIME_T (vl->interval))) return (-1); vl_def->interval = vl->interval; } diff --git a/src/onewire.c b/src/onewire.c index d308122a..09a6bf09 100644 --- a/src/onewire.c +++ b/src/onewire.c @@ -59,7 +59,7 @@ static ow_family_features_t ow_family_features[] = static int ow_family_features_num = STATIC_ARRAY_SIZE (ow_family_features); static char *device_g = NULL; -static int ow_interval = 0; +static cdtime_t ow_interval = 0; static const char *config_keys[] = { @@ -309,7 +309,8 @@ static int cow_init (void) CDTIME_T_TO_TIMESPEC (ow_interval, &cb_interval); plugin_register_complex_read (/* group = */ NULL, "onewire", cow_read, - &cb_interval, /* user data = */ NULL); + (ow_interval != 0) ? &cb_interval : NULL, + /* user data = */ NULL); plugin_register_shutdown ("onewire", cow_shutdown); return (0); diff --git a/src/oracle.c b/src/oracle.c index 2f218519..03567494 100644 --- a/src/oracle.c +++ b/src/oracle.c @@ -586,7 +586,7 @@ static int o_read_database_query (o_database_t *db, /* {{{ */ status = udb_query_prepare_result (q, prep_area, hostname_g, /* plugin = */ "oracle", db->name, column_names, column_num, - /* interval = */ -1); + /* interval = */ 0); if (status != 0) { ERROR ("oracle plugin: o_read_database_query (%s, %s): " diff --git a/src/perl.c b/src/perl.c index 116c34f3..f8a48227 100644 --- a/src/perl.c +++ b/src/perl.c @@ -235,15 +235,6 @@ struct { { "", NULL } }; -struct { - char name[64]; - int *var; -} g_integers[] = -{ - { "Collectd::interval_g", &interval_g }, - { "", NULL } -}; - /* * Helper functions for data type conversion. */ @@ -403,7 +394,10 @@ static int hv2value_list (pTHX_ HV *hash, value_list_t *vl) } if (NULL != (tmp = hv_fetch (hash, "interval", 8, 0))) - vl->interval = SvIV (*tmp); + { + double t = SvNV (*tmp); + vl->interval = DOUBLE_TO_CDTIME_T (t); + } if (NULL != (tmp = hv_fetch (hash, "host", 4, 0))) sstrncpy (vl->host, SvPV_nolen (*tmp), sizeof (vl->host)); @@ -684,8 +678,11 @@ static int value_list2hv (pTHX_ value_list_t *vl, data_set_t *ds, HV *hash) return -1; } - if (NULL == hv_store (hash, "interval", 8, newSViv (vl->interval), 0)) - return -1; + { + double t = CDTIME_T_TO_DOUBLE (vl->interval); + if (NULL == hv_store (hash, "interval", 8, newSVnv (t), 0)) + return -1; + } if ('\0' != vl->host[0]) if (NULL == hv_store (hash, "host", 4, newSVpv (vl->host, 0), 0)) @@ -2104,19 +2101,27 @@ static int g_pv_set (pTHX_ SV *var, MAGIC *mg) return 0; } /* static int g_pv_set (pTHX_ SV *, MAGIC *) */ -static int g_iv_get (pTHX_ SV *var, MAGIC *mg) +static int g_interval_get (pTHX_ SV *var, MAGIC *mg) { - int *iv = (int *)mg->mg_ptr; - sv_setiv (var, *iv); + cdtime_t *interval = (cdtime_t *)mg->mg_ptr; + double nv; + + nv = CDTIME_T_TO_DOUBLE (*interval); + + sv_setnv (var, nv); return 0; -} /* static int g_iv_get (pTHX_ SV *, MAGIC *) */ +} /* static int g_interval_get (pTHX_ SV *, MAGIC *) */ -static int g_iv_set (pTHX_ SV *var, MAGIC *mg) +static int g_interval_set (pTHX_ SV *var, MAGIC *mg) { - int *iv = (int *)mg->mg_ptr; - *iv = (int)SvIV (var); + cdtime_t *interval = (cdtime_t *)mg->mg_ptr; + double nv; + + nv = (double)SvNV (var); + + *interval = DOUBLE_TO_CDTIME_T (nv); return 0; -} /* static int g_iv_set (pTHX_ SV *, MAGIC *) */ +} /* static int g_interval_set (pTHX_ SV *, MAGIC *) */ static MGVTBL g_pv_vtbl = { g_pv_get, g_pv_set, NULL, NULL, NULL, NULL, NULL @@ -2124,8 +2129,8 @@ static MGVTBL g_pv_vtbl = { , NULL #endif }; -static MGVTBL g_iv_vtbl = { - g_iv_get, g_iv_set, NULL, NULL, NULL, NULL, NULL +static MGVTBL g_interval_vtbl = { + g_interval_get, g_interval_set, NULL, NULL, NULL, NULL, NULL #if HAVE_PERL_STRUCT_MGVTBL_SVT_LOCAL , NULL #endif @@ -2167,12 +2172,11 @@ static void xs_init (pTHX) g_strings[i].var, 0); } - /* global integers */ - for (i = 0; '\0' != g_integers[i].name[0]; ++i) { - tmp = get_sv (g_integers[i].name, 1); - sv_magicext (tmp, NULL, PERL_MAGIC_ext, &g_iv_vtbl, - (char *)g_integers[i].var, 0); - } + tmp = get_sv ("Collectd::interval_g", /* create = */ 1); + sv_magicext (tmp, NULL, /* how = */ PERL_MAGIC_ext, + /* vtbl = */ &g_interval_vtbl, + /* name = */ (char *) &interval_g, /* namelen = */ 0); + return; } /* static void xs_init (pTHX) */ diff --git a/src/plugin.c b/src/plugin.c index 83582763..2e25758e 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -336,7 +336,7 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) while (read_loop != 0) { read_func_t *rf; - struct timeval now; + cdtime_t now; int status; int rf_type; int rc; @@ -347,10 +347,9 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) { struct timespec abstime; - gettimeofday (&now, /* timezone = */ NULL); + now = cdtime (); - abstime.tv_sec = now.tv_sec + interval_g; - abstime.tv_nsec = 1000 * now.tv_usec; + CDTIME_T_TO_TIMESPEC (now + interval_g, &abstime); pthread_mutex_lock (&read_lock); pthread_cond_timedwait (&read_cond, &read_lock, @@ -361,15 +360,13 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) if ((rf->rf_interval.tv_sec == 0) && (rf->rf_interval.tv_nsec == 0)) { - gettimeofday (&now, /* timezone = */ NULL); + now = cdtime (); - rf->rf_interval.tv_sec = interval_g; - rf->rf_interval.tv_nsec = 0; + CDTIME_T_TO_TIMESPEC (interval_g, &rf->rf_interval); rf->rf_effective_interval = rf->rf_interval; - rf->rf_next_read.tv_sec = now.tv_sec; - rf->rf_next_read.tv_nsec = 1000 * now.tv_usec; + CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read); } /* sleep until this entry is due, @@ -459,7 +456,7 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) } /* update the ``next read due'' field */ - gettimeofday (&now, /* timezone = */ NULL); + now = cdtime (); DEBUG ("plugin_read_thread: Effective interval of the " "%s plugin is %i.%09i.", @@ -476,15 +473,12 @@ static void *plugin_read_thread (void __attribute__((unused)) *args) NORMALIZE_TIMESPEC (rf->rf_next_read); /* Check, if `rf_next_read' is in the past. */ - if ((rf->rf_next_read.tv_sec < now.tv_sec) - || ((rf->rf_next_read.tv_sec == now.tv_sec) - && (rf->rf_next_read.tv_nsec < (1000 * now.tv_usec)))) + if (TIMESPEC_TO_CDTIME_T (&rf->rf_next_read) < now) { /* `rf_next_read' is in the past. Insert `now' * so this value doesn't trail off into the * past too much. */ - rf->rf_next_read.tv_sec = now.tv_sec; - rf->rf_next_read.tv_nsec = 1000 * now.tv_usec; + CDTIME_T_TO_TIMESPEC (now, &rf->rf_next_read); } DEBUG ("plugin_read_thread: Next read of the %s plugin at %i.%09i.", @@ -1389,11 +1383,12 @@ int plugin_dispatch_values (value_list_t *vl) if (vl->interval <= 0) vl->interval = interval_g; - DEBUG ("plugin_dispatch_values: time = %.3f; interval = %i; " + DEBUG ("plugin_dispatch_values: time = %.3f; interval = %.3f; " "host = %s; " "plugin = %s; plugin_instance = %s; " "type = %s; type_instance = %s;", - CDTIME_T_TO_DOUBLE (vl->time), vl->interval, + CDTIME_T_TO_DOUBLE (vl->time), + CDTIME_T_TO_DOUBLE (vl->interval), vl->host, vl->plugin, vl->plugin_instance, vl->type, vl->type_instance); diff --git a/src/plugin.h b/src/plugin.h index ada89322..490aed03 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -87,7 +87,7 @@ struct value_list_s value_t *values; int values_len; cdtime_t time; - int interval; + cdtime_t interval; char host[DATA_MAX_NAME_LEN]; char plugin[DATA_MAX_NAME_LEN]; char plugin_instance[DATA_MAX_NAME_LEN]; diff --git a/src/postgresql.c b/src/postgresql.c index 175cc095..a8812e21 100644 --- a/src/postgresql.c +++ b/src/postgresql.c @@ -117,7 +117,7 @@ typedef struct { udb_query_t **queries; size_t queries_num; - int interval; + cdtime_t interval; char *host; char *port; @@ -335,8 +335,9 @@ static PGresult *c_psql_exec_query_params (c_psql_database_t *db, params[i] = db->user; break; case C_PSQL_PARAM_INTERVAL: - ssnprintf (interval, sizeof (interval), "%i", - db->interval > 0 ? db->interval : interval_g); + ssnprintf (interval, sizeof (interval), "%.3f", + (db->interval > 0) + ? CDTIME_T_TO_DOUBLE (db->interval) : interval_g); params[i] = interval; break; default: @@ -535,28 +536,6 @@ static int config_set_s (char *name, char **var, const oconfig_item_t *ci) return 0; } /* config_set_s */ -static int config_set_i (char *name, int *var, - const oconfig_item_t *ci, int min) -{ - int value; - - if ((0 != ci->children_num) || (1 != ci->values_num) - || (OCONFIG_TYPE_NUMBER != ci->values[0].type)) { - log_err ("%s expects a single number argument.", name); - return 1; - } - - value = (int)ci->values[0].value.number; - - if (value < min) { - log_err ("%s expects a number greater or equal to %i.", name, min); - return 1; - } - - *var = value; - return 0; -} /* config_set_s */ - static int config_query_param_add (udb_query_t *q, oconfig_item_t *ci) { c_psql_user_data_t *data; @@ -618,7 +597,7 @@ static int c_psql_config_database (oconfig_item_t *ci) c_psql_database_t *db; char cb_name[DATA_MAX_NAME_LEN]; - struct timespec cb_interval; + struct timespec cb_interval = { 0, 0 }; user_data_t ud; int i; @@ -656,7 +635,7 @@ static int c_psql_config_database (oconfig_item_t *ci) udb_query_pick_from_list (c, queries, queries_num, &db->queries, &db->queries_num); else if (0 == strcasecmp (c->key, "Interval")) - config_set_i ("Interval", &db->interval, c, /* min = */ 1); + cf_util_get_cdtime (c, &db->interval); else log_warn ("Ignoring unknown config key \"%s\".", c->key); } @@ -701,12 +680,11 @@ static int c_psql_config_database (oconfig_item_t *ci) ssnprintf (cb_name, sizeof (cb_name), "postgresql-%s", db->database); - memset (&cb_interval, 0, sizeof (cb_interval)); - if (db->interval > 0) - cb_interval.tv_sec = (time_t)db->interval; + CDTIME_T_TO_TIMESPEC (db->interval, &cb_interval); plugin_register_complex_read ("postgresql", cb_name, c_psql_read, - /* interval = */ &cb_interval, &ud); + /* interval = */ (db->interval > 0) ? &cb_interval : NULL, + &ud); return 0; } /* c_psql_config_database */ diff --git a/src/powerdns.c b/src/powerdns.c index 508ea506..a1b23555 100644 --- a/src/powerdns.c +++ b/src/powerdns.c @@ -364,9 +364,7 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */ break; } - /* TODO: Remove the macro once interval_g is of type cdtime_t. */ - cdt_timeout = TIME_T_TO_CDTIME_T (interval_g); - cdt_timeout = cdt_timeout * 3 / 4; + cdt_timeout = interval_g * 3 / 4; if (cdt_timeout < TIME_T_TO_CDTIME_T (2)) cdt_timeout = TIME_T_TO_CDTIME_T (2); diff --git a/src/rrdtool.c b/src/rrdtool.c index bd7bd77b..c948d1f8 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -1159,12 +1159,12 @@ static int rrd_init (void) rrdcreate_config.heartbeat = 2 * rrdcreate_config.stepsize; if ((rrdcreate_config.heartbeat > 0) - && (rrdcreate_config.heartbeat < interval_g)) + && (rrdcreate_config.heartbeat < CDTIME_T_TO_TIME_T (interval_g))) WARNING ("rrdtool plugin: Your `heartbeat' is " "smaller than your `interval'. This will " "likely cause problems."); else if ((rrdcreate_config.stepsize > 0) - && (rrdcreate_config.stepsize < interval_g)) + && (rrdcreate_config.stepsize < CDTIME_T_TO_TIME_T (interval_g))) WARNING ("rrdtool plugin: Your `stepsize' is " "smaller than your `interval'. This will " "create needlessly big RRD-files."); diff --git a/src/snmp.c b/src/snmp.c index e78ade97..54bcf672 100644 --- a/src/snmp.c +++ b/src/snmp.c @@ -1057,7 +1057,7 @@ static int csnmp_dispatch_table (host_definition_t *host, data_definition_t *dat sstrncpy (vl.host, host->name, sizeof (vl.host)); sstrncpy (vl.plugin, "snmp", sizeof (vl.plugin)); - vl.interval = (int) CDTIME_T_TO_TIME_T (host->interval); + vl.interval = host->interval; subid = 0; have_more = 1; @@ -1445,7 +1445,7 @@ static int csnmp_read_value (host_definition_t *host, data_definition_t *data) sstrncpy (vl.type, data->type, sizeof (vl.type)); sstrncpy (vl.type_instance, data->instance.string, sizeof (vl.type_instance)); - vl.interval = (int) CDTIME_T_TO_TIME_T (host->interval); + vl.interval = host->interval; req = snmp_pdu_create (SNMP_MSG_GET); if (req == NULL) @@ -1518,9 +1518,8 @@ static int csnmp_read_host (user_data_t *ud) host = ud->data; - /* FIXME: Convert "interval_g" to cdtime_t, too. */ if (host->interval == 0) - host->interval = TIME_T_TO_CDTIME_T (interval_g); + host->interval = interval_g; time_start = cdtime (); diff --git a/src/target_scale.c b/src/target_scale.c index 29fecdfd..af224f10 100644 --- a/src/target_scale.c +++ b/src/target_scale.c @@ -96,7 +96,7 @@ static int ts_invoke_counter (const data_set_t *ds, value_list_t *vl, /* {{{ */ { difference = curr_counter - prev_counter; } - rate = ((double) difference) / ((double) vl->interval); + rate = ((double) difference) / CDTIME_T_TO_DOUBLE (vl->interval); /* Modify the rate. */ if (!isnan (data->factor)) @@ -105,7 +105,7 @@ static int ts_invoke_counter (const data_set_t *ds, value_list_t *vl, /* {{{ */ rate += data->offset; /* Calculate the internal counter. */ - int_fraction += (rate * ((double) vl->interval)); + int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval)); difference = (uint64_t) int_fraction; int_fraction -= ((double) difference); int_counter += difference; @@ -199,7 +199,7 @@ static int ts_invoke_derive (const data_set_t *ds, value_list_t *vl, /* {{{ */ /* Calcualte the rate */ difference = curr_derive - prev_derive; - rate = ((double) difference) / ((double) vl->interval); + rate = ((double) difference) / CDTIME_T_TO_DOUBLE (vl->interval); /* Modify the rate. */ if (!isnan (data->factor)) @@ -208,7 +208,7 @@ static int ts_invoke_derive (const data_set_t *ds, value_list_t *vl, /* {{{ */ rate += data->offset; /* Calculate the internal derive. */ - int_fraction += (rate * ((double) vl->interval)); + int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval)); if (int_fraction < 0.0) /* handle negative integer rounding correctly */ difference = ((int64_t) int_fraction) - 1; else @@ -263,7 +263,7 @@ static int ts_invoke_absolute (const data_set_t *ds, value_list_t *vl, /* {{{ */ if (status != 0) int_fraction = 0.0; - rate = ((double) curr_absolute) / ((double) vl->interval); + rate = ((double) curr_absolute) / CDTIME_T_TO_DOUBLE (vl->interval); /* Modify the rate. */ if (!isnan (data->factor)) @@ -272,7 +272,7 @@ static int ts_invoke_absolute (const data_set_t *ds, value_list_t *vl, /* {{{ */ rate += data->offset; /* Calculate the new absolute. */ - int_fraction += (rate * ((double) vl->interval)); + int_fraction += (rate * CDTIME_T_TO_DOUBLE (vl->interval)); curr_absolute = (uint64_t) int_fraction; int_fraction -= ((double) curr_absolute); diff --git a/src/utils_cache.c b/src/utils_cache.c index 05db70c8..10e8a48e 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -44,7 +44,7 @@ typedef struct cache_entry_s cdtime_t last_update; /* Interval in which the data is collected * (for purding old entries) */ - int interval; + cdtime_t interval; int state; int hits; @@ -258,7 +258,7 @@ static int uc_insert (const data_set_t *ds, const value_list_t *vl, ce->values_gauge[i] = NAN; if (vl->interval > 0) ce->values_gauge[i] = ((double) vl->values[i].absolute) - / ((double) vl->interval); + / CDTIME_T_TO_DOUBLE (vl->interval); ce->values_raw[i].absolute = vl->values[i].absolute; break; @@ -319,8 +319,7 @@ int uc_check_timeout (void) while (c_avl_iterator_next (iter, (void *) &key, (void *) &ce) == 0) { /* If entry has not been updated, add to `keys' array */ - /* FIXME: Remove macro once "ce->interval" is of type cdtime_t. */ - if ((now - ce->last_update) >= TIME_T_TO_CDTIME_T (timeout_g * ce->interval)) + if ((now - ce->last_update) >= (ce->interval * timeout_g)) { char **tmp; diff --git a/src/utils_cmd_putval.c b/src/utils_cmd_putval.c index 15cd939b..dd43337e 100644 --- a/src/utils_cmd_putval.c +++ b/src/utils_cmd_putval.c @@ -56,16 +56,16 @@ static int set_option (value_list_t *vl, const char *key, const char *value) if (strcasecmp ("interval", key) == 0) { - int tmp; + double tmp; char *endptr; endptr = NULL; errno = 0; - tmp = strtol (value, &endptr, 0); + tmp = strtod (value, &endptr); if ((errno == 0) && (endptr != NULL) - && (endptr != value) && (tmp > 0)) - vl->interval = tmp; + && (endptr != value) && (tmp > 0.0)) + vl->interval = DOUBLE_TO_CDTIME_T (tmp); } else return (1); @@ -246,9 +246,11 @@ int create_putval (char *ret, size_t ret_len, /* {{{ */ escape_string (buffer_values, sizeof (buffer_values)); ssnprintf (ret, ret_len, - "PUTVAL %s interval=%i %s", + "PUTVAL %s interval=%.3f %s", buffer_ident, - (vl->interval > 0) ? vl->interval : interval_g, + (vl->interval > 0) + ? CDTIME_T_TO_DOUBLE (vl->interval) + : CDTIME_T_TO_DOUBLE (interval_g), buffer_values); return (0); diff --git a/src/utils_db_query.c b/src/utils_db_query.c index 78c8052e..dcac8076 100644 --- a/src/utils_db_query.c +++ b/src/utils_db_query.c @@ -73,7 +73,7 @@ struct udb_query_preparation_area_s /* {{{ */ char *plugin; char *db_name; - int interval; + cdtime_t interval; udb_result_preparation_area_t *result_prep_areas; }; /* }}} */ @@ -839,7 +839,7 @@ void udb_query_finish_result (const udb_query_t const *q, /* {{{ */ sfree (prep_area->plugin); sfree (prep_area->db_name); - prep_area->interval = -1; + prep_area->interval = 0; for (r = q->results, r_area = prep_area->result_prep_areas; r != NULL; r = r->next, r_area = r_area->next) @@ -907,7 +907,7 @@ int udb_query_handle_result (const udb_query_t const *q, /* {{{ */ int udb_query_prepare_result (const udb_query_t const *q, /* {{{ */ udb_query_preparation_area_t *prep_area, const char *host, const char *plugin, const char *db_name, - char **column_names, size_t column_num, int interval) + char **column_names, size_t column_num, cdtime_t interval) { udb_result_preparation_area_t *r_area; udb_result_t *r; diff --git a/src/utils_db_query.h b/src/utils_db_query.h index 846f81c2..727be036 100644 --- a/src/utils_db_query.h +++ b/src/utils_db_query.h @@ -68,7 +68,7 @@ int udb_query_check_version (udb_query_t *q, unsigned int version); int udb_query_prepare_result (const udb_query_t const *q, udb_query_preparation_area_t *prep_area, const char *host, const char *plugin, const char *db_name, - char **column_names, size_t column_num, int interval); + char **column_names, size_t column_num, cdtime_t interval); int udb_query_handle_result (const udb_query_t const *q, udb_query_preparation_area_t *prep_area, char **column_values); void udb_query_finish_result (const udb_query_t const *q, diff --git a/src/utils_format_json.c b/src/utils_format_json.c index ac88c0fa..65c886b6 100644 --- a/src/utils_format_json.c +++ b/src/utils_format_json.c @@ -265,7 +265,7 @@ static int value_list_to_json (char *buffer, size_t buffer_size, /* {{{ */ BUFFER_ADD (",\"dsnames\":%s", temp); BUFFER_ADD (",\"time\":%lu", (unsigned long) vl->time); - BUFFER_ADD (",\"interval\":%i", vl->interval); + BUFFER_ADD (",\"interval\":%.3f", CDTIME_T_TO_DOUBLE (vl->interval)); #define BUFFER_ADD_KEYVAL(key, value) do { \ status = escape_string (temp, sizeof (temp), (value)); \ diff --git a/src/utils_rrdcreate.c b/src/utils_rrdcreate.c index 5f21a17b..7a389f26 100644 --- a/src/utils_rrdcreate.c +++ b/src/utils_rrdcreate.c @@ -103,7 +103,10 @@ static int rra_get (char ***ret, const value_list_t *vl, /* {{{ */ return (-1); } - ss = (cfg->stepsize > 0) ? cfg->stepsize : vl->interval; + if (cfg->stepsize > 0) + ss = cfg->stepsize; + else + ss = (int) CDTIME_T_TO_TIME_T (vl->interval); if (ss <= 0) { *ret = NULL; @@ -241,7 +244,9 @@ static int ds_get (char ***ret, /* {{{ */ status = ssnprintf (buffer, sizeof (buffer), "DS:%s:%s:%i:%s:%s", d->name, type, - (cfg->heartbeat > 0) ? cfg->heartbeat : (2 * vl->interval), + (cfg->heartbeat > 0) + ? cfg->heartbeat + : (int) CDTIME_T_TO_TIME_T (2 * vl->interval), min, max); if ((status < 1) || ((size_t) status >= sizeof (buffer))) break; @@ -369,6 +374,7 @@ int cu_rrd_create_file (const char *filename, /* {{{ */ int ds_num; int status = 0; time_t last_up; + int stepsize; if (check_create_dir (filename)) return (-1); @@ -404,10 +410,14 @@ int cu_rrd_create_file (const char *filename, /* {{{ */ else last_up = CDTIME_T_TO_TIME_T (vl->time) - 10; + if (cfg->stepsize > 0) + stepsize = cfg->stepsize; + else + stepsize = (int) CDTIME_T_TO_TIME_T (vl->interval); + assert (vl->time > 10); status = srrd_create (filename, - (cfg->stepsize > 0) ? cfg->stepsize : vl->interval, - last_up, + stepsize, last_up, argc, (const char **) argv); free (argv); diff --git a/src/utils_time.c b/src/utils_time.c index 31e31090..420b425c 100644 --- a/src/utils_time.c +++ b/src/utils_time.c @@ -38,7 +38,7 @@ cdtime_t cdtime (void) /* {{{ */ return (0); } - return (TIMESPEC_TO_CDTIME_T (ts)); + return (TIMESPEC_TO_CDTIME_T (&ts)); } /* }}} cdtime_t cdtime */ /* vim: set sw=2 sts=2 et fdm=marker : */ diff --git a/src/utils_time.h b/src/utils_time.h index 0c477d8c..da73492c 100644 --- a/src/utils_time.h +++ b/src/utils_time.h @@ -43,8 +43,8 @@ #define CDTIME_T_TO_DOUBLE(t) (((double) (t)) / 1073741824.0) #define DOUBLE_TO_CDTIME_T(d) ((cdtime_t) ((d) * 1073741824.0)) +#define MS_TO_CDTIME_T(ms) ((cdtime_t) (((double) (ms)) * 1073741.824)) #define CDTIME_T_TO_MS(t) ((long) (((double) (t)) / 1073741.824)) - #define US_TO_CDTIME_T(us) ((cdtime_t) (((double) (us)) * 1073.741824)) #define CDTIME_T_TO_US(t) ((suseconds_t) (((double) (t)) / 1073.741824)) #define NS_TO_CDTIME_T(ns) ((cdtime_t) (((double) (ns)) * 1.073741824)) @@ -61,8 +61,8 @@ (tsp)->tv_sec = CDTIME_T_TO_TIME_T (cdt); \ (tsp)->tv_nsec = CDTIME_T_TO_NS ((cdt) % 1073741824); \ } while (0) -#define TIMESPEC_TO_CDTIME_T(ts) (TIME_T_TO_CDTIME_T ((ts).tv_sec) \ - + NS_TO_CDTIME_T ((ts).tv_nsec)) +#define TIMESPEC_TO_CDTIME_T(ts) (TIME_T_TO_CDTIME_T ((ts)->tv_sec) \ + + NS_TO_CDTIME_T ((ts)->tv_nsec)) cdtime_t cdtime (void); diff --git a/src/write_http.c b/src/write_http.c index 9cf9fe11..1a0e4ef0 100644 --- a/src/write_http.c +++ b/src/write_http.c @@ -306,8 +306,10 @@ static int wh_write_command (const data_set_t *ds, const value_list_t *vl, /* {{ } command_len = (size_t) ssnprintf (command, sizeof (command), - "PUTVAL %s interval=%i %s\r\n", - key, vl->interval, values); + "PUTVAL %s interval=%.3f %s\r\n", + key, + CDTIME_T_TO_DOUBLE (vl->interval), + values); if (command_len >= sizeof (command)) { ERROR ("write_http plugin: Command buffer too small: " "Need %zu bytes.", command_len + 1); -- 2.30.2