Skip to content

Changing clock speed at runtime without changing offset #198

Closed

Description

@keysmashes

It seems like libfaketime's behaviour when the faketimerc is updated (e.g. from +0 x1 to +0 x10) whilst a program is running is to act as if the program had been started with that faked time – for the x1 to x10 case, this results in a big jump forwards in the time reported to the application if it has been running for a while, or a jump backwards if the multiplier is set from x10 to x1.

I'd like to be able to change just the speed of the clock for an application, without any discontinuities in the reported time, but as far as I can tell this would require me to keep track of the time the application was started, and calculate an offset to compensate for the fact that libfaketime assumes that the multiplier has been in effect for the application's entire lifespan (e.g. if the application has been running for 10 seconds at 1x speed and now I want to move to 2x speed, I would not use +0 x2 but instead -10 x2).

Firstly: am I actually describing libfaketime's behaviour correctly? and secondly: is there a way to do what I want (that is, change the speed of the clock without changing the time) without awkward calculations involving the length of time that the process has been alive?

Activity

  1. wolfcw commented on Aug 13, 2019

    @wolfcw
    Owner

    Your observation is correct. The time that has passed since program start (baseline) is multiplied with the given x factor.

    Unfortunately, there's no easy fix for this; setting the baseline to the most recent change of parameters would likely break other parts, and currently there is no tracking of all changes, i.e., how long the program has been running with which faketime parameters.

  2. wolfcw commented on Aug 21, 2019

    @wolfcw
    Owner

    52108db adds the possibility to set the environment variable FAKETIME_XRESET=1.

    Using this option, transitions between different x modifier values are smoothened be resetting libfaketime internally. It's not perfect, but jumps in time forward are limited to the value of the x modifier times the number of seconds has passed since the previous time-related system call (when increasing the modifier), and it will not jump back in time (when decreasing the modifier).

  3. wolfcw commented on Sep 3, 2019

    @wolfcw
    Owner

    Please reopen if it does not work as expected.

  4. RobertDeRose commented on Aug 5, 2020

    @RobertDeRose

    I wish this FAKETIME_XRESET worked for me. It would solve my issues. But when I set this environment variable to 1, using @ start times seems to fail to change the date.

    So if I use @2020-01-01 01:01:01 x20.0, without FAKETIME_XRESET=1, it will not change the date, but removing that environment variable, it does, but then I get the HUGE jump.

    I've tried using the "Observed" behavior @sersorrel mentioned. To account for it, but that doesn't work either.

    I set the initial time using @ syntax without a modifier. I then keep track of how long the program has run since I changed the time. Then when I need to modify the speed, I've tried doing

    (start_time - current_time).total_seconds * speed_modifier x(speed_modifier) which ends up being something like -1200 x20.0 but that seems to set the speed up, and revert the time to the original time.

    So I then tried changing the format back to the @ format and taking that diff, subtracting it from the current time from faketime and adding the x(speed) to it, but that added the speed, but did not change the time and it was as if I just changed the speed again and got the huge jump in time again.

    UPDATE: After much testing, I finally realized that libfaketime uses the time the application started as the baseline, not the first time FAKETIME variable is set. Once I realized that, I just tracked that, and calculated the difference from that time and now all my mods land about where I expect them. I needed to add a fudge_factor for my speed up

    def fudge_factor(speed):
        return round(0.0002 * speed**2 + 0.0002 * speed + 1, 3) * speed

    This, at least for my application was needed. Can't explain why, solely based on observation data.

    UPDATE 2: Spoke too soon 😞
    Consecutive changes to the time, using the saved initial baseline time, are not working as expected. Perhaps I need to record the first time change as well and that's the diff for all changes going forward? Still testing

  5. wolfcw commented on Aug 6, 2020

    @wolfcw
    Owner

    libfaketime has no way to determine when a particular environment variable was set exactly. It initialises itself with the outside system time (usually the "real" wallclock time) when it intercepts the first time-related system call of the application; this is typically, although not necessarily, quite close to when the application was started.

    I understand what you're trying, but I'm having a hard time figuring out what you want to achieve. Usually there are much easier ways than separately calculating time offsets and manipulating the FAKETIME variable at run-time.

    As a wild guess, using relative FAKETIMEs instead of start-at-timestamps, which the faketime wrapper does automatically for you, might simplify things.

    Other than that, what you described as "baseline" is indeed fixed once set (for this specific process; see FAKETIME_DONT_RESET for child processes). I guess we could add an option to completely re-initialise libfaketime on a specific trigger to also reset this baseline, but this can break some things (e.g., semaphores) real bad for applications and I wonder what a good use case for that would be.

  6. RobertDeRose commented on Aug 6, 2020

    @RobertDeRose

    @wolfcw Thank you for the reply.

    I have finally successfully achieved what I needed to do. For your edification, if you're interested. I am writing a replay-application that takes network traffic for a proprietary platform. I consists of multiple microservices and I need all the services to be insync.

    My initial issues was a misunderstanding on my part. I was using a bash script to launch all the services with the environment variable FAKETIME_DONT_RESET, thinking that all application launched from that script would be considered children. I must have been really tired, but obviously, that's not true, unless the script then calls another program that then calls all the application.

    Anyways. I moved the launch of the depended services into the reply-application's startup so they really are now children.

    The next issues I was having was using the @ syntax with the FAKETIME_TIMESTAMP_FILE along with the x modifier.
    Using the observed behaviour from @sersorrel commented, I thought simply calculating the @ time to include the offset from the time the application started, would work. But it didn't I kept getting different resulting start time upon different application launches.

    As you said relative offsets seemed to work better and were more consistent for me. By still calculating the difference from the Application start time, and the time right before changing the time and multiplying it by the speed modifier, I was able to get very close to the desired start time I was expecting.

    I did notice the skew in the time as the speed up was increased. It seems to be linear in my test cases.

    Requested Time Diff Seconds Speed  
    10:03:44 10:03:55.32 00:00:11 11 1 11.947691408533
      10:04:43.13 00:00:59 59 5 59.7384570426651
    Slope 10:05:42.88 00:01:58 119 10 119.47691408533
    11.947691408533 10:06:42.66 00:02:58 179 15 179.215371127995
      10:07:42.41 00:03:58 238 20 238.95382817066
      10:09:41.93 00:05:57 358 30 358.430742255991
      10:11:41.40 00:07:57 477 40 477.907656341321
      10:13:40.97 00:09:56 597 50 597.384570426651
      10:15:40.49 00:11:56 716 60 716.861484511981

    But adding OFFSET = DESIRE_TIME_in_SECOND - ((CURR_TIME - APP_START_TIME).seconds * SPEED + SPEED * SLOPE) and then inserting that into the FAKETIME_TIMESTAMP_FILE like:

    -OFFSET xSPEED
    

    I assume the skew is in part to the CACHE_DURATION as changing the CACHE_DURATION changes the slope, but disabling the CACHE has too much of a performance hit when using high speed modifiers (greater than 40x) and there was still a small skew in the time. I'm pretty sure at this point it's just all do to code execution delay, because doing something as simple as moving my above calculation into its own method and calling it, caused enough change to the above slope, I had to move it back to being inline.

    I finally got a consistent faked time across multiple launches, at different speeds.

    Perhaps I'm still just missing something, but I got it to work and your amazing Library has made it so I can replay network traffic at speeds WELL above real-time which is a HUGE help in debugging weird issues and proving our application is doing what we expect without having to wait hours to replay data.

    Thanks for the help and the awesome software!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions