[collectd] [PATCH] src/csv.c: use a bigger buffer

Colin McCabe cmccabe at alumni.cmu.edu
Tue Jul 26 02:00:07 CEST 2011


From: Colin Patrick McCabe <cmccabe at alumni.cmu.edu>

The CSV plugin uses a relatively small output buffer. If you have a large
dataset, you'll always get error -1 because the line length will be too
long.

This patch allocates a larger buffer.

Signed-off-by: Colin McCabe <colin.mccabe at dreamhost.com>
---
 src/csv.c |   31 +++++++++++++++++++------------
 1 files changed, 19 insertions(+), 12 deletions(-)

diff --git a/src/csv.c b/src/csv.c
index 87a7b4e..f626fa7 100644
--- a/src/csv.c
+++ b/src/csv.c
@@ -259,12 +259,14 @@ static int csv_config (const char *key, const char *value)
 	return (0);
 } /* int csv_config */
 
+#define CSV_BUF_LEN 4096
+
 static int csv_write (const data_set_t *ds, const value_list_t *vl,
 		user_data_t __attribute__((unused)) *user_data)
 {
 	struct stat  statbuf;
 	char         filename[512];
-	char         values[512];
+	char         *values = NULL;
 	FILE        *csv;
 	int          csv_fd;
 	struct flock fl;
@@ -272,16 +274,17 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 
 	if (0 != strcmp (ds->type, vl->type)) {
 		ERROR ("csv plugin: DS type does not match value list type");
-		return -1;
+		goto fail;
 	}
 
 	if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
-		return (-1);
+		goto fail;
 
 	DEBUG ("csv plugin: csv_write: filename = %s;", filename);
 
-	if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
-		return (-1);
+	values = malloc(CSV_BUF_LEN);
+	if (value_list_to_string (values, CSV_BUF_LEN, ds, vl) != 0)
+		goto fail;
 
 	if (use_stdio)
 	{
@@ -290,7 +293,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 		escape_string (filename, sizeof (filename));
 
 		/* Replace commas by colons for PUTVAL compatible output. */
-		for (i = 0; i < sizeof (values); i++)
+		for (i = 0; i < CSV_BUF_LEN; i++)
 		{
 			if (values[i] == 0)
 				break;
@@ -303,6 +306,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 			 filename,
 			 CDTIME_T_TO_DOUBLE (vl->interval),
 			 values);
+		sfree(values);
 		return (0);
 	}
 
@@ -311,7 +315,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 		if (errno == ENOENT)
 		{
 			if (csv_create_file (filename, ds))
-				return (-1);
+				goto fail;
 		}
 		else
 		{
@@ -319,14 +323,14 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 			ERROR ("stat(%s) failed: %s", filename,
 					sstrerror (errno, errbuf,
 						sizeof (errbuf)));
-			return (-1);
+			goto fail;
 		}
 	}
 	else if (!S_ISREG (statbuf.st_mode))
 	{
 		ERROR ("stat(%s): Not a regular file!",
 				filename);
-		return (-1);
+		goto fail;
 	}
 
 	csv = fopen (filename, "a");
@@ -335,7 +339,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 		char errbuf[1024];
 		ERROR ("csv plugin: fopen (%s) failed: %s", filename,
 				sstrerror (errno, errbuf, sizeof (errbuf)));
-		return (-1);
+		goto fail;
 	}
 	csv_fd = fileno (csv);
 
@@ -353,7 +357,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 		ERROR ("csv plugin: flock (%s) failed: %s", filename,
 				sstrerror (errno, errbuf, sizeof (errbuf)));
 		fclose (csv);
-		return (-1);
+		goto fail;
 	}
 
 	fprintf (csv, "%s\n", values);
@@ -361,8 +365,11 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl,
 	/* The lock is implicitely released. I we don't release it explicitely
 	 * because the `FILE *' may need to flush a cache first */
 	fclose (csv);
-
+	sfree(values);
 	return (0);
+fail:
+	sfree(values);
+	return (-1);
 } /* int csv_write */
 
 void module_register (void)
-- 
1.7.2.5




More information about the collectd mailing list