From: Florian Forster Date: Fri, 12 Jul 2013 16:19:29 +0000 (+0200) Subject: rrdtool, rrdcached plugins: Re-implement value_list_to_filename(). X-Git-Tag: collectd-5.3.1~2^2 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=bfd3f06ef4996707036756371c5724ebf2748821;p=collectd.git rrdtool, rrdcached plugins: Re-implement value_list_to_filename(). Faster version without snprintf(). --- diff --git a/src/rrdcached.c b/src/rrdcached.c index cc991816..cf238031 100644 --- a/src/rrdcached.c +++ b/src/rrdcached.c @@ -112,52 +112,26 @@ static int value_list_to_string (char *buffer, int buffer_len, return (0); } /* int value_list_to_string */ -static int value_list_to_filename (char *buffer, int buffer_len, - const data_set_t *ds, const value_list_t *vl) +static int value_list_to_filename (char *buffer, size_t buffer_size, + value_list_t const *vl) { - int offset = 0; + char const suffix[] = ".rrd"; int status; + size_t len; - assert (0 == strcmp (ds->type, vl->type)); - - if (datadir != NULL) - { - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", datadir); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; - } - - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->host); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; - - if (strlen (vl->plugin_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s/", vl->plugin, vl->plugin_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->plugin); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + status = FORMAT_VL (buffer, buffer_size, vl); + if (status != 0) + return (status); - if (strlen (vl->type_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s", vl->type, vl->type_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s", vl->type); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + len = strlen (buffer); + assert (len < buffer_size); + buffer += len; + buffer_size -= len; - strncpy (buffer + offset, ".rrd", buffer_len - offset); - buffer[buffer_len - 1] = 0; + if (buffer_size <= sizeof (suffix)) + return (ENOMEM); + memcpy (buffer, suffix, sizeof (suffix)); return (0); } /* int value_list_to_filename */ @@ -430,7 +404,7 @@ static int rc_write (const data_set_t *ds, const value_list_t *vl, return (-1); } - if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0) + if (value_list_to_filename (filename, sizeof (filename), vl) != 0) { ERROR ("rrdcached plugin: value_list_to_filename failed."); return (-1); diff --git a/src/rrdtool.c b/src/rrdtool.c index b1d13ee3..916ce01d 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -240,47 +240,26 @@ static int value_list_to_string (char *buffer, int buffer_len, return (0); } /* int value_list_to_string */ -static int value_list_to_filename (char *buffer, int buffer_len, - const data_set_t __attribute__((unused)) *ds, const value_list_t *vl) +static int value_list_to_filename (char *buffer, size_t buffer_size, + value_list_t const *vl) { - int offset = 0; + char const suffix[] = ".rrd"; int status; + size_t len; - if (datadir != NULL) - { - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", datadir); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; - } - - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->host); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + status = FORMAT_VL (buffer, buffer_size, vl); + if (status != 0) + return (status); - if (strlen (vl->plugin_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s/", vl->plugin, vl->plugin_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->plugin); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + len = strlen (buffer); + assert (len < buffer_size); + buffer += len; + buffer_size -= len; - if (strlen (vl->type_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s.rrd", vl->type, vl->type_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s.rrd", vl->type); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + if (buffer_size <= sizeof (suffix)) + return (ENOMEM); + memcpy (buffer, suffix, sizeof (suffix)); return (0); } /* int value_list_to_filename */ @@ -896,7 +875,7 @@ static int rrd_write (const data_set_t *ds, const value_list_t *vl, return -1; } - if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0) + if (value_list_to_filename (filename, sizeof (filename), vl) != 0) return (-1); if (value_list_to_string (values, sizeof (values), ds, vl) != 0)