<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
> <channel><title>Straylight Run &#187; disk</title> <atom:link href="http://blog.straylightrun.net/tag/disk/feed/" rel="self" type="application/rss+xml" /><link>http://blog.straylightrun.net</link> <description>Software, Technology, PHP</description> <lastBuildDate>Mon, 07 Nov 2011 19:26:59 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.3.1</generator> <item><title>Clearing The Linux Buffer Cache</title><link>http://blog.straylightrun.net/2009/12/03/clearing-the-linux-buffer-cache/</link> <comments>http://blog.straylightrun.net/2009/12/03/clearing-the-linux-buffer-cache/#comments</comments> <pubDate>Thu, 03 Dec 2009 19:22:45 +0000</pubDate> <dc:creator>gerard</dc:creator> <category><![CDATA[Performance]]></category> <category><![CDATA[Sysadmin]]></category> <category><![CDATA[apache]]></category> <category><![CDATA[buffer cache]]></category> <category><![CDATA[disk]]></category> <category><![CDATA[logs]]></category> <category><![CDATA[mysql]]></category> <guid
isPermaLink="false">http://blog.straylightrun.net/2009/12/03/clearing-the-linux-buffer-cache/</guid> <description><![CDATA[According to these Munin memory graphs, the large orange area is the OS buffer cache – a buffer the OS uses to cache plain ol’ file data on disk.&#160; The graph below shows one of our web servers after we upgraded its memory.&#160; It makes sense that most of the memory not used by apps [...]]]></description> <content:encoded><![CDATA[<p>According to these <a
href="http://munin.projects.linpro.no/">Munin</a> memory graphs, the large orange area is the OS buffer cache – a buffer the OS uses to cache plain ol’ file data on disk.&#160; The graph below shows one of our web servers after we upgraded its memory.&#160;</p><p><img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Web server memory usage" border="0" alt="Web server memory usage" src="http://blog.straylightrun.net/wp-content/uploads/2009/12/zsweb001memorymonth.png" width="495" height="408" /></p><p>It makes sense that most of the memory not used by apps would be used by the OS to improve disk access.&#160; So seeing the memory graphs filled with orange is generally a good thing.&#160; After a few days, I watched the orange area grow and thought, “Great!&#160; LInux is putting all that extra memory to use.”&#160; I thought in my head that maybe it was caching images and CSS files to serve to Apache.&#160; But was that true?</p><p><strong><u>Looking At A Different Server</u></strong></p><p>Here is a memory graph from one of our database servers after the RAM upgrade.</p><p><img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Database server memory usage" border="0" alt="Database server memory usage" src="http://blog.straylightrun.net/wp-content/uploads/2009/12/zsdb001memorymonth.png" width="495" height="408" /></p><p>Again, I first thought that the OS was caching all that juicy database data from disk.&#160; The problem is that we don’t have 12GB of data, and that step pattern growth was suspiciously consistent.</p><p>Looking again at the web server graph, I saw giant downward spikes of blue color, where the buffer cache was emptied.&#160; (The blue is unused memory.)&#160; These occurred every day at 4 am, and on Sundays there’s a huge one.&#160; What happens every day at 4 am?&#160; The logs are rotated.&#160; And on Sundays, the granddaddy log of them all – the Apache log – is rotated.</p><p><strong><u>The Problem</u></strong></p><p>It was starting to make sense.&#160; Log files seem to take up most of the OS buffer cache on the web servers.&#160; Not optimal, I’m sure.&#160; And when they’re rotated, the data in the cache is invalidated and thus freed.</p><p>Here is a memory graph for one of our other database servers.</p><p> <img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Database server memory usage" border="0" alt="Database server memory usage" src="http://blog.straylightrun.net/wp-content/uploads/2009/12/zsdb002memorymonth.png" width="495" height="408" /></p><p>That step pattern growth is missing!&#160; In fact, most of RAM is unused.&#160; What is the difference between the first database server and this one?&#160; The first has the <code>`mysqldump`</code> backup.&#160; It occurs every night at 2:30 am, right when those step changes occur on its memory usage graph.</p><p>It was clear to me that most of the OS buffer cache was wasted on logs and backups and such.&#160; There had to be a way to tell the OS not to cache a file.&#160;</p><p><strong><u>The Solution</u></strong></p><p>Google gave me this page: <a
href="http://insights.oetiker.ch/linux/fadvise.html">Improving Linux performance by preserving Buffer Cache State</a>.&#160; I copied the little C program into a file and ran it on all the <code>`mysqldump`</code> backups.&#160; Here is the what happened to the memory usage.</p><p><img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Database server memory usage" border="0" alt="Database server memory usage" src="http://blog.straylightrun.net/wp-content/uploads/2009/12/zsdb001memoryweek.png" width="495" height="408" /></p><p>Quite a bit of buffer cache was freed.&#160; On that night’s backup, I logged the buffer cache size before the backup and after.</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% cat 2008.08.21.02.30.log
Starting at Thu Aug 21 02:30:03 EDT 2008
=========================================
Cached:        4490232 kB
Cached:        5350908 kB
=========================================
Ending at Thu Aug 21 02:30:55 EDT 2008</pre></div></div><p>Just under a gigabyte increase in buffer cache size.&#160; What was the size of the new backup file?</p><div
class="wp_syntax"><div
class="code"><pre class="sh" style="font-family:monospace;">% ll 2008.08.21.02.30.sql
-rw-r--r-- 1 root root 879727872 Aug 21 02:30 2008.08.21.02.30.sql</pre></div></div><p>About 900MB.</p><p><strong><u>Did It Work?</u></strong></p><p>I used the C program on that page to ensure no database backups were cached by the OS.&#160; I did the same on the web servers in the <code>logrotate</code> config files.&#160; A couple days later, I checked the memory graph on the database server that performed the backup.&#160; Notice how the buffer cache did not fill up.&#160; It looked like the program worked, and the OS was free to cache more important things.</p><p><img
style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="Database server memory usage" border="0" alt="Database server memory usage" src="http://blog.straylightrun.net/wp-content/uploads/2009/12/zsdb001memoryweek2.png" width="495" height="408" /></p> ]]></content:encoded> <wfw:commentRss>http://blog.straylightrun.net/2009/12/03/clearing-the-linux-buffer-cache/feed/</wfw:commentRss> <slash:comments>0</slash:comments> </item> </channel> </rss>
