From: Florian Forster Date: Fri, 19 Jul 2013 05:39:47 +0000 (+0200) Subject: rrdtool plugin: Minor performance improvement. X-Git-Tag: collectd-5.4.0~15 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=21ef84928de2910c3b41fad156afbbf3d0075e7c;p=collectd.git rrdtool plugin: Minor performance improvement. Replace the value_list_to_string() function with a version which is optimized for data sets with a single data source. The original function is still used for data sets with multiple data sources. This allows to reduce the number of calls to snprintf() in this (very common) case, resulting in ~2% speedup. --- diff --git a/src/rrdtool.c b/src/rrdtool.c index 80833902..2f28329f 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -198,7 +198,7 @@ static int srrd_update (char *filename, char *template, } /* int srrd_update */ #endif /* !HAVE_THREADSAFE_LIBRRD */ -static int value_list_to_string (char *buffer, int buffer_len, +static int value_list_to_string_multiple (char *buffer, int buffer_len, const data_set_t *ds, const value_list_t *vl) { int offset; @@ -242,6 +242,45 @@ static int value_list_to_string (char *buffer, int buffer_len, } /* for ds->ds_num */ return (0); +} /* int value_list_to_string_multiple */ + +static int value_list_to_string (char *buffer, int buffer_len, + const data_set_t *ds, const value_list_t *vl) +{ + int status; + time_t tt; + + if (ds->ds_num != 1) + return (value_list_to_string_multiple (buffer, buffer_len, + ds, vl)); + + tt = CDTIME_T_TO_TIME_T (vl->time); + switch (ds->ds[0].type) + { + case DS_TYPE_DERIVE: + status = ssnprintf (buffer, buffer_len, "%u:%"PRIi64, + (unsigned) tt, vl->values[0].derive); + break; + case DS_TYPE_GAUGE: + status = ssnprintf (buffer, buffer_len, "%u:%lf", + (unsigned) tt, vl->values[0].gauge); + break; + case DS_TYPE_COUNTER: + status = ssnprintf (buffer, buffer_len, "%u:%llu", + (unsigned) tt, vl->values[0].counter); + break; + case DS_TYPE_ABSOLUTE: + status = ssnprintf (buffer, buffer_len, "%u:%"PRIu64, + (unsigned) tt, vl->values[0].absolute); + break; + default: + return (EINVAL); + } + + if ((status < 1) || (status >= buffer_len)) + return (ENOMEM); + + return (0); } /* int value_list_to_string */ static int value_list_to_filename (char *buffer, size_t buffer_size,