don't bother with don't need when there is neither madvise not fadvise calls
[rrdtool.git] / src / rrd_create.c
index a8f6a29..32882f2 100644 (file)
@@ -1,9 +1,11 @@
 /*****************************************************************************
- * RRDtool 1.2.99907080300  Copyright by Tobi Oetiker, 1997-2007
+ * RRDtool 1.3rc7  Copyright by Tobi Oetiker, 1997-2008
  *****************************************************************************
  * rrd_create.c  creates new rrds
  *****************************************************************************/
 
+#include <stdlib.h>
+#include <time.h>
 #include <locale.h>
 
 #include "rrd_tool.h"
@@ -22,6 +24,8 @@ void      parseGENERIC_DS(
     const char *def,
     rrd_t *rrd,
     int ds_idx);
+long int  rra_random_row(
+    rra_def_t *);
 
 int rrd_create(
     int argc,
@@ -690,13 +694,13 @@ int rrd_create_fn(
 
     if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) {
         rrd_set_error("allocating pdp_prep");
-        free(rrd->stat_head);       
-        free(rrd->live_head);       
+        free(rrd->stat_head);
+        free(rrd->live_head);
         close(rrd_file);
         return (-1);
     }
 
-    strcpy(rrd->pdp_prep->last_ds, "UNKN");
+    strcpy(rrd->pdp_prep->last_ds, "U");
 
     rrd->pdp_prep->scratch[PDP_val].u_val = 0.0;
     rrd->pdp_prep->scratch[PDP_unkn_sec_cnt].u_cnt =
@@ -707,8 +711,8 @@ int rrd_create_fn(
 
     if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) {
         rrd_set_error("allocating cdp_prep");
-        free(rrd->stat_head);       
-        free(rrd->live_head);       
+        free(rrd->stat_head);
+        free(rrd->live_head);
         close(rrd_file);
         return (-1);
     }
@@ -755,8 +759,8 @@ int rrd_create_fn(
 
     if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) {
         rrd_set_error("allocating rra_ptr");
-        free(rrd->stat_head);       
-        free(rrd->live_head);       
+        free(rrd->stat_head);
+        free(rrd->live_head);
         close(rrd_file);
         return (-1);
     }
@@ -766,15 +770,15 @@ int rrd_create_fn(
      * would occur for cur_row = 1 because rrd_update increments
      * the pointer a priori. */
     for (i = 0; i < rrd->stat_head->rra_cnt; i++) {
-        rrd->rra_ptr->cur_row = rrd->rra_def[i].row_cnt - 1;
+        rrd->rra_ptr->cur_row = rra_random_row(&rrd->rra_def[i]);
         write(rrd_file, rrd->rra_ptr, sizeof(rra_ptr_t));
     }
 
     /* write the empty data area */
     if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) {
         rrd_set_error("allocating unknown");
-        free(rrd->stat_head);       
-        free(rrd->live_head);       
+        free(rrd->stat_head);
+        free(rrd->live_head);
         close(rrd_file);
         return (-1);
     }
@@ -792,8 +796,8 @@ int rrd_create_fn(
     }
     free(unknown);
     fdatasync(rrd_file);
-    free(rrd->stat_head);       
-    free(rrd->live_head);       
+    free(rrd->stat_head);
+    free(rrd->live_head);
     if (close(rrd_file) == -1) {
         rrd_set_error("creating rrd: %s", rrd_strerror(errno));
         return -1;
@@ -805,3 +809,16 @@ int rrd_create_fn(
     rrd_close(rrd_file_dn);
     return (0);
 }
+
+static int rand_init = 0;
+
+long int rra_random_row(
+    rra_def_t *rra)
+{
+    if (!rand_init) {
+        srandom((unsigned int) time(NULL) + (unsigned int) getpid());
+        rand_init++;
+    }
+
+    return random() % rra->row_cnt;
+}