Timing queries in the 21st century (with LD_PRELOAD and sed)

So… Baron blogged about wanting higher precision timers from the mysql binary and that running sed on the binary wasn’t cutting it. However… I am not one to give up that easily!

This is what LD_PRELOAD was made for! Evil nasty hacks to make your life easier!

By looking at the mysql.cc source code, I can easily work out how this works… I just have to override two calls! They being sysconf() (we fake how many ticks per second there are) and times() (let’s return a much higher precision number).

Combined with the sed hack on the binary to change the sprintf call to print out the higher precision number, we have:

mysql> select count(*) from t1;
| count(*) |
|   710720 |
1 row in set (1.080110 sec)

Get it from my junkcode: http://www.flamingspork.com/junkcode/high_precision_mysql_timer.c

(or you can get it from http://paste.drizzle.org/show/364/)

9 thoughts on “Timing queries in the 21st century (with LD_PRELOAD and sed)

  1. Pingback: Tweets that mention Timing queries in the 21st century (with LD_PRELOAD and sed) | Ramblings -- Topsy.com

  2. Great and … nasty! :-)

    I was jut thinking about something similar the other day; is it possible, to hijack all mysql c/c++ functions this way?

    For example (file)sort?

    BTW how leagal is this? :-)

  3. perfectly legal. You can override a lot of things using LD_PRELOAD, but really, with software you have the source for – it’s best to fix it.

  4. Awesome.

    Add the following to your instructions, there are a lot of people out there that will need the help.

    chmod +x ./mysql-fixed

  5. true, the better sed expression probably works better.

    and let me be just a tiny bit scared that people may end up using this…

  6. Pingback: Book Review - Effective MySQL | iHeavy blog

  7. Pingback: Effective MySQL: Optimizing SQL Statements - iheavy

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.