diff options
author | Balint Reczey <balint@balintreczey.hu> | 2013-08-28 11:49:48 +0200 |
---|---|---|
committer | Balint Reczey <balint@balintreczey.hu> | 2013-08-28 14:41:49 +0200 |
commit | 77eb2531367c6ed974e80e2a42294eac09bf77d5 (patch) | |
tree | 86402c1e10c0f093051c6a4bba8ca367c5b21e1c /src | |
parent | 7931cd180cf29df7c23b30bdaee944cab7446793 (diff) | |
download | libfaketime-77eb2531367c6ed974e80e2a42294eac09bf77d5.tar.gz |
Start system time from the same global clocks in every processes after replaying a file
Diffstat (limited to 'src')
-rw-r--r-- | src/faketime.c | 9 | ||||
-rw-r--r-- | src/faketime_common.h | 12 | ||||
-rw-r--r-- | src/libfaketime.c | 62 |
3 files changed, 49 insertions, 34 deletions
diff --git a/src/faketime.c b/src/faketime.c index 8dfd518..af25edf 100644 --- a/src/faketime.c +++ b/src/faketime.c @@ -207,7 +207,14 @@ int main (int argc, char **argv) /* init elapsed time ticks to zero */ ft_shared->ticks = 0; ft_shared->file_idx = 0; - if (-1 == munmap(ft_shared, (sizeof(uint64_t)))) { + ft_shared->start_time.real.tv_sec = 0; + ft_shared->start_time.real.tv_nsec = -1; + ft_shared->start_time.mon.tv_sec = 0; + ft_shared->start_time.mon.tv_nsec = -1; + ft_shared->start_time.mon_raw.tv_sec = 0; + ft_shared->start_time.mon_raw.tv_nsec = -1; + + if (-1 == munmap(ft_shared, (sizeof(struct ft_shared_s)))) { perror("munmap"); cleanup_shobjs(); exit(EXIT_FAILURE); diff --git a/src/faketime_common.h b/src/faketime_common.h index c8ee3c9..3284c70 100644 --- a/src/faketime_common.h +++ b/src/faketime_common.h @@ -24,6 +24,16 @@ #include <stdint.h> +struct system_time_s { + /** System time according to CLOCK_REALTIME */ + struct timespec real; + /** System time according to CLOCK_MONOTONIC */ + struct timespec mon; + /** System time according to CLOCK_MONOTONIC_RAW */ + struct timespec mon_raw; +}; + + /** Data shared among faketime-spawned processes */ struct ft_shared_s { /** @@ -32,6 +42,8 @@ struct ft_shared_s { uint64_t ticks; /** Index of timstamp to be loaded from file */ uint64_t file_idx; + /** System time Faketime started at */ + struct system_time_s start_time; }; #endif diff --git a/src/libfaketime.c b/src/libfaketime.c index c76633d..1b5f439 100644 --- a/src/libfaketime.c +++ b/src/libfaketime.c @@ -151,22 +151,12 @@ static char ft_spawn_target[1024]; static long ft_spawn_secs = -1; static long ft_spawn_ncalls = -1; -/** - * Static timespec to store our startup time, followed by a load-time library - * initialization declaration. - */ -static struct timespec ftpl_starttime = {0, -1}; /** * Static timespec to store our startup time, followed by a load-time library - * initialization declaration. Saved using CLOCK_MONOTONIC. - */ -static struct timespec ftpl_starttime_mon = {0, -1}; -/** - * Static timespec to store our startup time, followed by a load-time library - * initialization declaration. Saved using CLOCK_MONOTONIC_RAW. + * initialization declaration. */ -static struct timespec ftpl_starttime_mon_raw = {0, -1}; +static struct system_time_s ftpl_starttime = {{0, -1}, {0, -1}, {0, -1}}; static char user_faked_time_fmt[BUFSIZ] = {0}; @@ -315,18 +305,24 @@ static bool load_time(struct timespec *tp) /* we are out of timstamps to replay, return to faking time by rules * using last timestamp from file as the user provided timestamp */ timespec_from_saved(&user_faked_time_timespec, &stss[(infile_size / sizeof(stss[0])) - 1 ]); + + if (ft_shared->ticks == 0) { + /* we set shared memory to stop using infile */ + ft_shared->ticks = 1; #ifdef __APPLE__ - ftpl_starttime = *tp; - ftpl_starttime_mon = *tp; - ftpl_starttime_mon_raw = *tp; + ftpl_starttime.real = *tp; + ftpl_starttime.mon = *tp; + ftpl_starttime.mon_raw = *tp; #else real_clock_gettime(CLOCK_REALTIME, &ftpl_starttime.real); real_clock_gettime(CLOCK_MONOTONIC, &ftpl_starttime.mon); real_clock_gettime(CLOCK_MONOTONIC_RAW, &ftpl_starttime.mon_raw); #endif - if (ft_shared->ticks == 0) { - ft_shared->ticks = 1; + ft_shared->start_time = ftpl_starttime; + } else { + ftpl_starttime = ft_shared->start_time; } + munmap(stss, infile_size); infile_set = false; } else { @@ -687,7 +683,7 @@ static void parse_ft_string(const char *user_faked_time) user_offset.tv_sec = floor(frac_offset); user_offset.tv_nsec = (frac_offset - user_offset.tv_sec) * SEC_TO_nSEC; - timespecadd(&ftpl_starttime, &user_offset, &user_faked_time_timespec); + timespecadd(&ftpl_starttime.real, &user_offset, &user_faked_time_timespec); goto parse_modifiers; break; @@ -823,16 +819,16 @@ void __attribute__ ((constructor)) ftpl_init(void) /* this is not faked */ clock_get_time(cclock, &mts); mach_port_deallocate(mach_task_self(), cclock); - ftpl_starttime.tv_sec = mts.tv_sec; - ftpl_starttime.tv_nsec = mts.tv_nsec; - ftpl_starttime_mon.tv_sec = mts.tv_sec; - ftpl_starttime_mon.tv_nsec = mts.tv_nsec; - ftpl_starttime_mon_raw.tv_sec = mts.tv_sec; - ftpl_starttime_mon_raw.tv_nsec = mts.tv_nsec; + ftpl_starttime.real.tv_sec = mts.tv_sec; + ftpl_starttime.real.tv_nsec = mts.tv_nsec; + ftpl_starttime.mon.tv_sec = mts.tv_sec; + ftpl_starttime.mon.tv_nsec = mts.tv_nsec; + ftpl_starttime.mon_raw.tv_sec = mts.tv_sec; + ftpl_starttime.mon_raw.tv_nsec = mts.tv_nsec; #else - (*real_clock_gettime)(CLOCK_REALTIME, &ftpl_starttime); - (*real_clock_gettime)(CLOCK_MONOTONIC, &ftpl_starttime_mon); - (*real_clock_gettime)(CLOCK_MONOTONIC_RAW, &ftpl_starttime_mon_raw); + (*real_clock_gettime)(CLOCK_REALTIME, &ftpl_starttime.real); + (*real_clock_gettime)(CLOCK_MONOTONIC, &ftpl_starttime.mon); + (*real_clock_gettime)(CLOCK_MONOTONIC_RAW, &ftpl_starttime.mon_raw); #endif /* fake time supplied as environment variable? */ @@ -895,13 +891,13 @@ static pthread_mutex_t time_mutex=PTHREAD_MUTEX_INITIALIZER; /* For debugging, output #seconds and #calls */ switch (clk_id) { case CLOCK_REALTIME: - timespecsub(tp, &ftpl_starttime, &tmp_ts); + timespecsub(tp, &ftpl_starttime.real, &tmp_ts); break; case CLOCK_MONOTONIC: - timespecsub(tp, &ftpl_starttime_mon, &tmp_ts); + timespecsub(tp, &ftpl_starttime.mon, &tmp_ts); break; case CLOCK_MONOTONIC_RAW: - timespecsub(tp, &ftpl_starttime_mon_raw, &tmp_ts); + timespecsub(tp, &ftpl_starttime.mon_raw, &tmp_ts); break; default: printf("Invalid clock_id for clock_gettime: %d", clk_id); @@ -1000,13 +996,13 @@ static pthread_mutex_t time_mutex=PTHREAD_MUTEX_INITIALIZER; struct timespec tdiff, timeadj; switch (clk_id) { case CLOCK_REALTIME: - timespecsub(tp, &ftpl_starttime, &tdiff); + timespecsub(tp, &ftpl_starttime.real, &tdiff); break; case CLOCK_MONOTONIC: - timespecsub(tp, &ftpl_starttime_mon, &tdiff); + timespecsub(tp, &ftpl_starttime.mon, &tdiff); break; case CLOCK_MONOTONIC_RAW: - timespecsub(tp, &ftpl_starttime_mon_raw, &tdiff); + timespecsub(tp, &ftpl_starttime.mon_raw, &tdiff); break; default: printf("Invalid clock_id for clock_gettime: %d", clk_id); |