{"id":3741,"date":"2014-05-25T12:23:58","date_gmt":"2014-05-25T02:23:58","guid":{"rendered":"https:\/\/www.flamingspork.com\/blog\/?p=3741"},"modified":"2014-05-24T13:08:18","modified_gmt":"2014-05-24T03:08:18","slug":"caring-about-stack-usage","status":"publish","type":"post","link":"https:\/\/www.flamingspork.com\/blog\/2014\/05\/25\/caring-about-stack-usage\/","title":{"rendered":"Caring about stack usage"},"content":{"rendered":"<p>It may not be surprising that there&#8217;s been a few projects over the years that I&#8217;ve worked on where we&#8217;ve had to care about stack usage (to varying degrees).<\/p>\n<p>For threaded userspace applications (e.g. MySQL, Drizzle) you get a certain amount of stack per thread &#8211; and you really don&#8217;t want to bust that. For a great many years now, there&#8217;s been both a configuration parameter in MySQL to set how much stack each thread (connection) gets as well as various checks in the source code to ensure there&#8217;s enough free stack to do a particular operation (IIRC open_table is the most hairy one of this in MySQL).<\/p>\n<p>For the Linux Kernel, stack usage is a relatively (in)famous problem&#8230; although by now just about every real problem has been fixed and merely mentioning it is probably just the influence of the odd grey beard hairs I&#8217;m pretending not to notice.<\/p>\n<p>In a current project I&#8217;m working on, it&#8217;s also something we have to care about.<\/p>\n<p>It turns out that GCC has a few nice things to help you prevent unbounded stack usage or runaway stack usage. There&#8217;s two warnings you can enable.<\/p>\n<p>There&#8217;s -Wstack-usage=len which will throw warnings on unbounded stack usage (e.g. array on stack sized based on an argument to the function), where stack usage is greater than len and when stack usage may exceed len.<\/p>\n<p>There&#8217;s also -Wframe-larger-than=len which is based on calculation for a particular stack frame, as opposed to -Wstack-usage=len, which could be based on several stack frames.<\/p>\n<p>Odds are, you may get some warnings in your project if you set this to what you would consider &#8220;conservative&#8221; values. Now, if this is every going to explode at runtime is something that&#8217;s left as an exercise for the reader, but enabling these warnings is pretty easy and a simple way to help find and prevent some issues.<\/p>\n<p>After all, having your software explode for running off the end of the stack is just a tad embarrassing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It may not be surprising that there&#8217;s been a few projects over the years that I&#8217;ve worked on where we&#8217;ve had to care about stack usage (to varying degrees). For threaded userspace applications (e.g. MySQL, Drizzle) you get a certain &hellip; <a href=\"https:\/\/www.flamingspork.com\/blog\/2014\/05\/25\/caring-about-stack-usage\/\">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_post_was_ever_published":false,"_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}},"categories":[76,75,14],"tags":[354,70,86,628,386],"class_list":["post-3741","post","type-post","status-publish","format-standard","hentry","category-code","category-drizzle-work-et-al","category-mysql","tag-compiler","tag-drizzle","tag-gcc","tag-mysql","tag-stack"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p5a6n8-Yl","jetpack-related-posts":[{"id":2196,"url":"https:\/\/www.flamingspork.com\/blog\/2010\/11\/15\/limiting-functions-to-32k-stack-in-drizzle-and-scoped_ptr\/","url_meta":{"origin":3741,"position":0},"title":"Limiting functions to 32k stack in Drizzle (and scoped_ptr)","author":"Stewart Smith","date":"2010-11-15","format":false,"excerpt":"I wonder if this comes under \"Code Style\" or not... Anyway, Monty and I finished getting Drizzle ready for adding \"\u00ef\u00bb\u00bf\u00ef\u00bb\u00bf\u00ef\u00bb\u00bf-Wframe-larger-than=32768\" as a standard compiler flag. This means that no function within the Drizzle source tree can use greater than 32kb stack - it's a compiler warning - and with\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":2353,"url":"https:\/\/www.flamingspork.com\/blog\/2011\/04\/12\/innodb-and-memcached\/","url_meta":{"origin":3741,"position":1},"title":"innodb and memcached","author":"Stewart Smith","date":"2011-04-12","format":false,"excerpt":"I had a quick look at the source tree (I haven't compiled it, just read the source - that's what I do. I challenge any C\/C++ compiler to keep up with my brain!) that's got a tarball up on labs.mysql.com for the memcached interface to innodb. A few quick thoughts:\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":1738,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/10\/14\/return-of-the-top-5-mysql-wishlist-and-looking-at-drizzle\/","url_meta":{"origin":3741,"position":2},"title":"Return of the &#8220;Top 5 MySQL Wishlist&#8221; and looking at Drizzle","author":"Stewart Smith","date":"2009-10-14","format":false,"excerpt":"It's coming up on a year since I started working full time on Drizzle. So, I got a bit reflective... Have we done things that I (and others) really wanted done? Back in 2007, I wrote my top 5 wishlist for the MySQL Server. I am not going to pretend\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":2303,"url":"https:\/\/www.flamingspork.com\/blog\/2011\/03\/16\/fixed-in-drizzle-no-more-gotchas\/","url_meta":{"origin":3741,"position":3},"title":"Fixed in Drizzle: No more &#8220;GOTCHA&#8217;s&#8221;","author":"Stewart Smith","date":"2011-03-16","format":false,"excerpt":"\u00a0 At the upcoming MySQL Conference and Expo, I'm going to give a Thursday afternoon (2pm) session entitled Fixed in Drizzle: No more \"GOTCHA's\". I plan to have a lot of fun with this session.. If you go back to the very start of when I started submitting code to\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":1395,"url":"https:\/\/www.flamingspork.com\/blog\/2009\/02\/12\/performance-schema-show-me-the-code\/","url_meta":{"origin":3741,"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":3345,"url":"https:\/\/www.flamingspork.com\/blog\/2013\/05\/23\/mysql-vs-drizzle-plugin-api\/","url_meta":{"origin":3741,"position":5},"title":"MySQL vs Drizzle plugin APIs","author":"Stewart Smith","date":"2013-05-23","format":false,"excerpt":"There's a big difference in how plugins are treated in MySQL and how they are treated in Drizzle. The MySQL way has been to create a C API in front of the C++-like (I call it C- as it manages to take the worst of both worlds) internal \"API\". The\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":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/3741","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=3741"}],"version-history":[{"count":1,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/3741\/revisions"}],"predecessor-version":[{"id":3742,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/posts\/3741\/revisions\/3742"}],"wp:attachment":[{"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/media?parent=3741"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/categories?post=3741"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.flamingspork.com\/blog\/wp-json\/wp\/v2\/tags?post=3741"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}