{"id":2289,"date":"2011-02-08T16:16:16","date_gmt":"2011-02-08T06:16:16","guid":{"rendered":"http:\/\/www.flamingspork.com\/blog\/?p=2289"},"modified":"2013-12-18T08:56:49","modified_gmt":"2013-12-17T22:56:49","slug":"timing-queries-in-the-21st-century-with-ld_preload-and-sed","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2011\/02\/08\/timing-queries-in-the-21st-century-with-ld_preload-and-sed\/","title":{"rendered":"Timing queries in the 21st century (with LD_PRELOAD and sed)"},"content":{"rendered":"<p>So&#8230; <a href=\"http:\/\/www.xaprb.com\/blog\/2011\/02\/07\/timing-queries-in-the-21st-century\/\">Baron blogged about wanting higher precision timers from the mysql binary<\/a> and that running sed on the binary wasn&#8217;t cutting it. However&#8230; I am not one to give up that easily!<\/p>\n<p>This is what LD_PRELOAD was made for! Evil nasty hacks to make your life easier!<\/p>\n<p>By looking at the mysql.cc source code, I can easily work out how this works&#8230; I just have to override two calls! They being sysconf() (we fake how many ticks per second there are) and times() (let&#8217;s return a much higher precision number).<\/p>\n<p>Combined with the sed hack on the binary to change the sprintf call to print out the higher precision number, we have:<\/p>\n<pre>mysql&gt; select count(*) from t1;\r\n+----------+\r\n| count(*) |\r\n+----------+\r\n|   710720 |\r\n+----------+\r\n1 row in set (1.080110 sec)<\/pre>\n<p>Get it from my junkcode: <a href=\"http:\/\/www.flamingspork.com\/junkcode\/high_precision_mysql_timer.c\">http:\/\/www.flamingspork.com\/junkcode\/high_precision_mysql_timer.c<\/a><\/p>\n<p>(or you can get it from http:\/\/paste.drizzle.org\/show\/364\/)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So&#8230; Baron blogged about wanting higher precision timers from the mysql binary and that running sed on the binary wasn&#8217;t cutting it. However&#8230; I am not one to give up that easily! This is what LD_PRELOAD was made for! Evil &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2011\/02\/08\/timing-queries-in-the-21st-century-with-ld_preload-and-sed\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2},"jetpack_post_was_ever_published":false},"categories":[76,14],"tags":[410,154,628,409],"class_list":["post-2289","post","type-post","status-publish","format-standard","hentry","category-code","category-mysql","tag-hack","tag-ld_preload","tag-mysql","tag-timer"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-AV","jetpack-related-posts":[{"id":1253,"url":"https:\/\/www.flamingspork.com\/blog\/2008\/11\/08\/libmallocfail\/","url_meta":{"origin":2289,"position":0},"title":"libmallocfail","author":"Stewart Smith","date":"2008-11-08","format":false,"excerpt":"Bazaar branches of libmallocfail Simple LD_PRELOAD library that will take parameters via environment variables and cause malloc() to occationally fail. Aim was to use this to test bits of MySQL\/Drizzle although since their libtool based stuf, the binary in tree is a libtool shell script, and I haven't found a\u2026","rel":"","context":"In &quot;code&quot;","block_context":{"text":"code","link":"https:\/\/www.flamingspork.com\/blog\/category\/code\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":706,"url":"https:\/\/www.flamingspork.com\/blog\/2006\/05\/31\/davem-on-ingos-smp-lock-validator\/","url_meta":{"origin":2289,"position":1},"title":"DaveM on Ingo&#8217;s SMP lock validator","author":"Stewart Smith","date":"2006-05-31","format":false,"excerpt":"DaveM talks about Ingo's new SMP lock validator for linux kernel A note reminding me to go take a look and see what can be ripped out and placed into various bits of MySQL and NDB. Ideally, of course, it could be turned into a LD_PRELOAD for pthread mutexes. Anybody\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":939,"url":"https:\/\/www.flamingspork.com\/blog\/2007\/11\/27\/libeatmydata\/","url_meta":{"origin":2289,"position":2},"title":"libeatmydata","author":"Stewart Smith","date":"2007-11-27","format":false,"excerpt":"Following my successful linux.conf.au talk \"Eat My Data: How Everybody Gets POSIX File I\/O Wrong\", I started to feel the need to easily be able to have my data eaten. Okay, not quite. However, when you've written your software properly, so it uses fsync() correctly, opening files with O_SYNC or\u2026","rel":"","context":"In &quot;life, the universe and everything&quot;","block_context":{"text":"life, the universe and everything","link":"https:\/\/www.flamingspork.com\/blog\/category\/life-the-universe-and-everything\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1928,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/05\/25\/nocache-ld_preload\/","url_meta":{"origin":2289,"position":3},"title":"nocache LD_PRELOAD","author":"Stewart Smith","date":"2010-05-25","format":false,"excerpt":"Want to do something like \"cp big_file copy_of_big_file\" or \"tar xfz big_tarball.tar.gz\" but without thrashing your cache? Enrico Zini has a nice little LD_PRELOAD called nocache. $ nocache tar xfz foo.tar.gz Goes well with libeatmydata. A pair of tools for compensating for your Operating System casually hating you. I imagine\u2026","rel":"","context":"In &quot;General&quot;","block_context":{"text":"General","link":"https:\/\/www.flamingspork.com\/blog\/category\/general\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1395,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/02\/12\/performance-schema-show-me-the-code\/","url_meta":{"origin":2289,"position":4},"title":"Performance Schema: Show me the code","author":"Stewart Smith","date":"2009-02-12","format":false,"excerpt":"For such a long worked on feature, with such potential - I find the resistence to publishing a source tree curious (my comments on the topic have been moderated away but others have asked too). I could go and grep through the commits list searching for things (hint: look for\u2026","rel":"","context":"In &quot;drizzle&quot;","block_context":{"text":"drizzle","link":"https:\/\/www.flamingspork.com\/blog\/category\/work-et-al\/drizzle-work-et-al\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":682,"url":"https:\/\/www.flamingspork.com\/blog\/2006\/04\/27\/cool-dolphin-sci-interconnect-stuff\/","url_meta":{"origin":2289,"position":5},"title":"Cool Dolphin SCI interconnect stuff","author":"Stewart Smith","date":"2006-04-27","format":false,"excerpt":"http:\/\/www.dolphinics.com\/ make this cool SCI socket hardware that can be used with MySQL Cluster (for example, like their example setup). Their tech provides high performance (350MB\/sec avail to the user) and low latency (worst case is like 2 or 3 micro seconds to send 512bytes to another node). So can\u2026","rel":"","context":"In &quot;cool gadgets&quot;","block_context":{"text":"cool gadgets","link":"https:\/\/www.flamingspork.com\/blog\/category\/cool-gadgets\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/2289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/comments?post=2289"}],"version-history":[{"count":2,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions"}],"predecessor-version":[{"id":3631,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/2289\/revisions\/3631"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=2289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=2289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=2289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}