X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Frrd_create.c;h=0e2f82c14150e8d39ef28cd0f42dbed1192cdaf2;hb=73c400314f176d4ac458b2ed357c276ee4080c79;hp=35a06b1d10165899ff7c505b5cc7248785b378bb;hpb=7f30ee2ae089520606cbb7e38fab9981618771f5;p=rrdtool.git diff --git a/src/rrd_create.c b/src/rrd_create.c index 35a06b1..0e2f82c 100644 --- a/src/rrd_create.c +++ b/src/rrd_create.c @@ -4,6 +4,8 @@ * rrd_create.c creates new rrds *****************************************************************************/ +#include + #include "rrd_tool.h" #include "rrd_rpncalc.h" #include "rrd_hw.h" @@ -25,6 +27,13 @@ int rrd_create( int argc, char **argv) { + struct option long_options[] = { + {"start", required_argument, 0, 'b'}, + {"step", required_argument, 0, 's'}, + {0, 0, 0, 0} + }; + int option_index = 0; + int opt; time_t last_up = time(NULL) - 10; unsigned long pdp_step = 300; struct rrd_time_value last_up_tv; @@ -36,14 +45,6 @@ int rrd_create( opterr = 0; /* initialize getopt */ while (1) { - static struct option long_options[] = { - {"start", required_argument, 0, 'b'}, - {"step", required_argument, 0, 's'}, - {0, 0, 0, 0} - }; - int option_index = 0; - int opt; - opt = getopt_long(argc, argv, "b:s:", long_options, &option_index); if (opt == EOF) @@ -247,6 +248,7 @@ int rrd_create_r( switch (cf_conv (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) { case CF_HWPREDICT: + case CF_MHWPREDICT: /* initialize some parameters */ rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha]. u_val = 0.1; @@ -293,6 +295,7 @@ int rrd_create_r( switch (cf_conv (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) { case CF_HWPREDICT: + case CF_MHWPREDICT: case CF_DEVSEASONAL: case CF_SEASONAL: case CF_DEVPREDICT: @@ -316,6 +319,7 @@ int rrd_create_r( switch (cf_conv (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) { case CF_HWPREDICT: + case CF_MHWPREDICT: rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_alpha]. u_val = atof(token); if (atof(token) <= 0.0 || atof(token) >= 1.0) @@ -361,6 +365,7 @@ int rrd_create_r( switch (cf_conv (rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam)) { case CF_HWPREDICT: + case CF_MHWPREDICT: rrd.rra_def[rrd.stat_head->rra_cnt].par[RRA_hw_beta]. u_val = atof(token); if (atof(token) < 0.0 || atof(token) > 1.0) @@ -415,6 +420,7 @@ int rrd_create_r( atoi(token) - 1; break; case CF_HWPREDICT: + case CF_MHWPREDICT: /* length of the associated CF_SEASONAL and CF_DEVSEASONAL arrays. */ period = atoi(token); if (period > @@ -463,8 +469,10 @@ int rrd_create_r( par[RRA_dependent_rra_idx].u_cnt, rrd.stat_head->rra_cnt); #endif /* should we create CF_SEASONAL, CF_DEVSEASONAL, and CF_DEVPREDICT? */ - if (cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == - CF_HWPREDICT + if ((cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == + CF_HWPREDICT + || cf_conv(rrd.rra_def[rrd.stat_head->rra_cnt].cf_nam) == + CF_MHWPREDICT) && rrd.rra_def[rrd.stat_head->rra_cnt]. par[RRA_dependent_rra_idx].u_cnt == rrd.stat_head->rra_cnt) { #ifdef DEBUG @@ -506,7 +514,7 @@ void parseGENERIC_DS( int ds_idx) { char minstr[DS_NAM_SIZE], maxstr[DS_NAM_SIZE]; - + char *old_locale; /* int temp; @@ -514,6 +522,7 @@ void parseGENERIC_DS( &(rrd -> ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt), minstr,maxstr); */ + old_locale = setlocale(LC_NUMERIC,"C"); if (sscanf(def, "%lu:%18[^:]:%18[^:]", &(rrd->ds_def[ds_idx].par[DS_mrhb_cnt].u_cnt), minstr, maxstr) == 3) { @@ -532,11 +541,13 @@ void parseGENERIC_DS( rrd->ds_def[ds_idx].par[DS_min_val].u_val >= rrd->ds_def[ds_idx].par[DS_max_val].u_val) { rrd_set_error("min must be less than max in DS definition"); + setlocale(LC_NUMERIC,old_locale); return; } } else { rrd_set_error("failed to parse data source %s", def); } + setlocale(LC_NUMERIC,old_locale); } /* Create the CF_DEVPREDICT, CF_DEVSEASONAL, CF_SEASONAL, and CF_FAILURES RRAs @@ -618,36 +629,31 @@ int rrd_create_fn( rrd_t *rrd) { unsigned long i, ii; - FILE *rrd_file; + int rrd_file; rrd_value_t *unknown; int unkn_cnt; + rrd_file_t *rrd_file_dn; + rrd_t rrd_dn; - long rrd_head_size; - - if ((rrd_file = fopen(file_name, "wb")) == NULL) { + if ((rrd_file = open(file_name, O_WRONLY|O_CREAT|O_TRUNC,0666)) == NULL) { rrd_set_error("creating '%s': %s", file_name, rrd_strerror(errno)); - free(rrd->stat_head); - rrd->stat_head = NULL; - free(rrd->ds_def); - rrd->ds_def = NULL; - free(rrd->rra_def); - rrd->rra_def = NULL; + rrd_free(rrd); return (-1); } - fwrite(rrd->stat_head, sizeof(stat_head_t), 1, rrd_file); + write(rrd_file,rrd->stat_head, sizeof(stat_head_t)); - fwrite(rrd->ds_def, sizeof(ds_def_t), rrd->stat_head->ds_cnt, rrd_file); + write(rrd_file,rrd->ds_def, sizeof(ds_def_t)*rrd->stat_head->ds_cnt); - fwrite(rrd->rra_def, - sizeof(rra_def_t), rrd->stat_head->rra_cnt, rrd_file); + write(rrd_file,rrd->rra_def, + sizeof(rra_def_t)* rrd->stat_head->rra_cnt); - fwrite(rrd->live_head, sizeof(live_head_t), 1, rrd_file); + write(rrd_file,rrd->live_head, sizeof(live_head_t)); if ((rrd->pdp_prep = calloc(1, sizeof(pdp_prep_t))) == NULL) { rrd_set_error("allocating pdp_prep"); rrd_free(rrd); - fclose(rrd_file); + close(rrd_file); return (-1); } @@ -658,12 +664,12 @@ int rrd_create_fn( rrd->live_head->last_up % rrd->stat_head->pdp_step; for (i = 0; i < rrd->stat_head->ds_cnt; i++) - fwrite(rrd->pdp_prep, sizeof(pdp_prep_t), 1, rrd_file); + write(rrd_file,rrd->pdp_prep, sizeof(pdp_prep_t)); if ((rrd->cdp_prep = calloc(1, sizeof(cdp_prep_t))) == NULL) { rrd_set_error("allocating cdp_prep"); rrd_free(rrd); - fclose(rrd_file); + close(rrd_file); return (-1); } @@ -671,6 +677,7 @@ int rrd_create_fn( for (i = 0; i < rrd->stat_head->rra_cnt; i++) { switch (cf_conv(rrd->rra_def[i].cf_nam)) { case CF_HWPREDICT: + case CF_MHWPREDICT: init_hwpredict_cdp(rrd->cdp_prep); break; case CF_SEASONAL: @@ -699,7 +706,7 @@ int rrd_create_fn( } for (ii = 0; ii < rrd->stat_head->ds_cnt; ii++) { - fwrite(rrd->cdp_prep, sizeof(cdp_prep_t), 1, rrd_file); + write(rrd_file,rrd->cdp_prep, sizeof(cdp_prep_t)); } } @@ -709,7 +716,7 @@ int rrd_create_fn( if ((rrd->rra_ptr = calloc(1, sizeof(rra_ptr_t))) == NULL) { rrd_set_error("allocating rra_ptr"); rrd_free(rrd); - fclose(rrd_file); + close(rrd_file); return (-1); } @@ -719,15 +726,14 @@ int rrd_create_fn( * 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; - fwrite(rrd->rra_ptr, sizeof(rra_ptr_t), 1, rrd_file); + write(rrd_file,rrd->rra_ptr, sizeof(rra_ptr_t)); } - rrd_head_size = ftell(rrd_file); /* write the empty data area */ if ((unknown = (rrd_value_t *) malloc(512 * sizeof(rrd_value_t))) == NULL) { rrd_set_error("allocating unknown"); rrd_free(rrd); - fclose(rrd_file); + close(rrd_file); return (-1); } for (i = 0; i < 512; ++i) @@ -738,40 +744,20 @@ int rrd_create_fn( unkn_cnt += rrd->stat_head->ds_cnt * rrd->rra_def[i].row_cnt; while (unkn_cnt > 0) { - fwrite(unknown, sizeof(rrd_value_t), min(unkn_cnt, 512), rrd_file); + write(rrd_file,unknown, sizeof(rrd_value_t) * min(unkn_cnt, 512)); + unkn_cnt -= 512; } free(unknown); - - /* lets see if we had an error */ - if (ferror(rrd_file)) { - rrd_set_error("a file error occurred while creating '%s'", file_name); - fclose(rrd_file); - rrd_free(rrd); - return (-1); - } -#ifdef HAVE_POSIX_FADVISE - /* this file is not going to be read again any time - soon, so we drop everything except the header portion from - the buffer cache. for this to work, we have to fdsync the file - first though. This will not be all that fast, but 'good' data - like other rrdfiles headers will stay in cache. Now this only works if creating - a single rrd file is not too large, but I assume this should not be the case - in general. Otherwhise we would have to sync and release while writing all - the unknown data. */ - fflush(rrd_file); - fdatasync(fileno(rrd_file)); - if (0 != - posix_fadvise(fileno(rrd_file), rrd_head_size, 0, - POSIX_FADV_DONTNEED)) { - rrd_set_error("setting POSIX_FADV_DONTNEED on '%s': %s", file_name, - rrd_strerror(errno)); - fclose(rrd_file); - return (-1); - } -#endif - - fclose(rrd_file); + fdatasync(rrd_file); rrd_free(rrd); + if ( close(rrd_file) == -1 ) { + rrd_set_error("creating rrd: %s", rrd_strerror(errno)); + return -1; + } + /* flush all we don't need out of the cache */ + rrd_file_dn = rrd_open(file_name, &rrd_dn, RRD_READONLY); + rrd_dontneed(rrd_file_dn,&rrd_dn); + rrd_close(rrd_file_dn); return (0); }