From 63734fbc33a26dd50d77fb2febc0999c5f099e5a Mon Sep 17 00:00:00 2001 From: thib Date: Sun, 29 Mar 2009 21:31:53 +0100 Subject: [PATCH] Added option jitter --- conf.c | 6 ++++++ database.c | 7 +++++++ doc/en/fcrontab.5.sgml | 10 ++++++++++ doc/fcron-doc.mod.in | 1 + fileconf.c | 8 ++++++++ global.h | 1 + save.c | 3 +++ save.h | 1 + 8 files changed, 37 insertions(+) diff --git a/conf.c b/conf.c index d50ec94..84575e6 100644 --- a/conf.c +++ b/conf.c @@ -661,6 +661,12 @@ read_file(const char *file_name, cf_t *cf) cl->cl_timefreq = (time_t) bufi; break; + case S_JITTER_T: + /* read the jitter (uchar) to use to set next execution time */ + Read(bufi, size, "Error while reading jitter field"); + cl->cl_jitter = (unsigned char) bufi; + break; + case S_MINS_T: Read(cl->cl_mins, size, "Error while reading mins field"); break; diff --git a/database.c b/database.c index 638dddd..65d8afd 100644 --- a/database.c +++ b/database.c @@ -1153,6 +1153,13 @@ set_next_exe(cl_t *line, char option, int info_fd) nextexe += ( (i= int_end_timet - nextexe) > 0) ? (time_t)(((float)i * (float)rand())/(float)RAND_MAX) : 0; } + else if (is_td(line->cl_option) && line->cl_runfreq != 1 && line->cl_jitter > 0){ + /* &-lines only: + * run the command between nextexe and nextexe+jitter seconds, + * as a way not to have 100 jobs all starting exactly at + * the second 0 of the minute they should run */ + nextexe += (time_t)(((float)line->cl_jitter * (float)rand())/(float)RAND_MAX); + } line->cl_nextexe = nextexe + (line->cl_file->cf_tzdiff * 3600); diff --git a/doc/en/fcrontab.5.sgml b/doc/en/fcrontab.5.sgml index 751c8e7..77b7e35 100644 --- a/doc/en/fcrontab.5.sgml +++ b/doc/en/fcrontab.5.sgml @@ -478,6 +478,15 @@ useful when used in conjunction with option &optvolatile;. + + jitter + + integer(0) + Run the task between 0 and jitter seconds later than it should have been run. This options only applies to &-lines and is intended for systems where many jobs are supposed to be started at the same minute: the jitter option will randomly spread the start of all those jobs across the first jitter seconds of the minute instead of starting all of them at the first second of the minute. The argument must be between 0 and 255 (inclusive). + &seealso; option &optrandom;. + + + lavg @@ -611,6 +620,7 @@ execution interval. Besides, you must know that the random scheme may be quite easy to guess for skilled people: thus, you shouldn't rely on this option to make important things secure. However, it shouldn't be a problem for most uses. + &seealso; option &optjitter;. diff --git a/doc/fcron-doc.mod.in b/doc/fcron-doc.mod.in index 40c4fa2..a09b301 100644 --- a/doc/fcron-doc.mod.in +++ b/doc/fcron-doc.mod.in @@ -74,6 +74,7 @@ exesev'> first'> forcemail'> +jitter'> lavg'> lavg1'> lavg5'> diff --git a/fileconf.c b/fileconf.c index cfa3be4..f1489a6 100644 --- a/fileconf.c +++ b/fileconf.c @@ -904,6 +904,14 @@ read_opt(char *ptr, cl_t *cl) fprintf(stderr, " Opt : \"%s\" %d\n", opt_name, i); } + else if( strcmp(opt_name, "jitter") == 0 ) { + if(!in_brackets ||(ptr=get_num(ptr, &i, UCHAR_MAX, 0, NULL))==NULL) + Handle_err; + cl->cl_jitter = i; + if (debug_opt) + fprintf(stderr, " Opt : \"%s\" %d\n", opt_name, i); + } + /* handle %-line : we check if we are really in a %-line (which we do not do * for other options), because writing "&hourly" in a fcrontab results in an * error (hourly ignored) hard to find, and, in any case, annoying. */ diff --git a/global.h b/global.h index cf177f7..fba2b01 100644 --- a/global.h +++ b/global.h @@ -221,6 +221,7 @@ typedef struct cl_t { unsigned char cl_lavg[LAVG_SIZE];/*load averages needed (1,5,15 mins) */ unsigned char cl_numexe; /* entries in queues & running processes */ char cl_nice; /* nice value to control priority */ + unsigned char cl_jitter; /* run randomly late up to jitter seconds */ /* see bitstring(3) man page for more details */ bitstr_t bit_decl(cl_mins, 60); /* 0-59 */ bitstr_t bit_decl(cl_hrs, 24); /* 0-23 */ diff --git a/save.c b/save.c index f50c669..ef2d28e 100644 --- a/save.c +++ b/save.c @@ -263,6 +263,9 @@ write_file_to_disk(int fd, struct cf_t *file, time_t time_date) if ( line->cl_tz != NULL ) { Save_str(fd, S_TZ_T, line->cl_tz, write_buf, &write_buf_used); } + if ( line->cl_jitter > 0 ) { + Save_lint(fd, S_JITTER_T, line->cl_jitter, write_buf, &write_buf_used); + } if ( is_freq(line->cl_option) ) { /* save the frequency to run the line */ diff --git a/save.h b/save.h index 8a8e21b..e6ca454 100644 --- a/save.h +++ b/save.h @@ -81,5 +81,6 @@ extern int save_file_safe(cf_t *file, char *final_path, char *prog_name, uid_t o #define S_OPTION_T 2017 /* options for that line (see option.h) */ #define S_FIRST_T 2018 /* wait time before first execution */ #define S_TZ_T 2019 /* time zone of the line */ +#define S_JITTER_T 2020 /* jitter of the line */ #endif /* __SAVE_H__ */ -- 2.40.0