/* get high precision timers out of mysql command line client without
 * recompiling the mysql client!
 *
 *
 * execute this:
 * $ sed -e 's/%\.2f sec/%.6f sec/' /usr/bin/mysql > mysql-fixed
 * build this lib (see below) and then run:
 * LD_PRELOAD=./libhpmysqltimer.so ./mysql-fixed

 * put the following in Makefile and type "make" to build.

CC?=gcc

all: libhpmysqltimer.so

high_precision_mysql_timer.o: high_precision_mysql_timer.c
	$(CC) -g -c $(CFLAGS) -fPIC -o $@ $<

libhpmysqltimer.so: high_precision_mysql_timer.o 
	$(CC) -shared -Wl,-soname,$@ $(LDFLAGS) -o $@ $< -ldl

 */

#define _GNU_SOURCE
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <dlfcn.h>
#include <sys/time.h>
#include <sys/times.h>

static int (*libc_sysconf)(int)= NULL;

#define ASSIGN_DLSYM_OR_DIE(name)				\
  libc_##name = dlsym(RTLD_NEXT, #name);			\
  if (!libc_##name || dlerror())				\
    _exit(1);

void __attribute__ ((constructor)) hpt_init(void)
{
  ASSIGN_DLSYM_OR_DIE(sysconf);
}

long sysconf(int name)
{
  if(!libc_sysconf)
    hpt_init();
  if (name == _SC_CLK_TCK)
    return 100000;

  return (*libc_sysconf)(name);
}

clock_t times(struct tms *buf)
{
  struct timeval tv;
  gettimeofday(&tv, NULL);

  return (tv.tv_sec*100000) + (tv.tv_usec/10);
}
