Skip to content
Advertisement

How to slow all time measurements of a process?

I’d like to make an application believe that time is going faster/slower than real time. I.e. I need to make all the time measurement APIs return t0+dt*s with user-defined s when t0+dt is real time. This would affect anything like gettimeofday() as well as timer_gettime() and all related functions and mechanisms (including actual trigger times of timers).

I think of putting a hook somewhere which would change the visible time for the app.

In a Linux system, what is the best place to put such a hook, so that I didn’t have to create too many hooks? Is there any central place for this?

Advertisement

Answer

You can use the LD_PRELOAD trick described in this question.
Libfaketime seams to do exactly what you need (see the README under “Advanced time specification options”). The following example shows time going 2x slower for “sleep”:

$ time FAKETIME="x0.5" LD_PRELOAD=/usr/lib/x86_64-linux-gnu/faketime/libfaketime.so.1 sleep 1
real    0m2.002s
user    0m0.000s
sys 0m0.000s

(on a debian system, therefore /usr/lib/)

If it doesn’t actually do what you want, instead of rolling out your own solution, I’d suggest taking its source and implementing what you need from there.

For completeness’ sake, another possible starting point is datefudge.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement