Skip to content

Instantly share code, notes, and snippets.

@fujin
Created April 1, 2015 01:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save fujin/db74f71aa7b2a8a461cf to your computer and use it in GitHub Desktop.
Save fujin/db74f71aa7b2a8a461cf to your computer and use it in GitHub Desktop.
LD_PRELOAD gettimeofday linux speedhack
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <dlfcn.h>
#include <stdio.h>
static timeval * timezero = 0;
typedef int (*go)(timeval *tv, timezone *tz);
extern "C" int gettimeofday(timeval *tv, timezone *tz)
{
// Testing purposes:
go gettimeofday_orig;
int val;
gettimeofday_orig=(go)dlsym(RTLD_NEXT,"gettimeofday");
if (!timezero)
{
timezero = new timeval;
val = gettimeofday_orig(timezero,tz);
(*tv) = (*timezero);
return val;
}
// Multiply speed:
int M = 1;
// Divide speed:
int N = 2;
// That means 1/2 speed;
val = gettimeofday_orig(tv,tz);
// Multiply the seconds:
tv->tv_sec = M*tv->tv_sec - M*timezero->tv_sec + N*timezero->tv_sec;
// Multiply the microseconds:
tv->tv_usec = M*tv->tv_usec - M*timezero->tv_usec + N*timezero->tv_usec;
// Add the modulus of seconds to microseconds:
tv->tv_usec += ((tv->tv_sec % N) * 1000000);
tv->tv_sec /= N;
tv->tv_usec /= N;
while(tv->tv_usec >= 1000000)
{
tv->tv_usec -= 1000000;
tv->tv_sec += 1;
}
while(tv->tv_usec < 0)
{
tv->tv_usec += 1000000;
tv->tv_sec -= 1;
}
return val;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment