<?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"
	>

<channel>
	<title>Rudd-O.com &#187; Performance</title>
	<atom:link href="http://rudd-o.com/archives/category/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://rudd-o.com</link>
	<description>We only do fun stuff.</description>
	<pubDate>Fri, 29 Aug 2008 11:39:17 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6</generator>
	<language>en</language>
			<item>
		<title>Thanks, Michael!</title>
		<link>http://rudd-o.com/archives/2007/11/25/thanks-michael/</link>
		<comments>http://rudd-o.com/archives/2007/11/25/thanks-michael/#comments</comments>
		<pubDate>Sun, 25 Nov 2007 17:50:40 +0000</pubDate>
		<dc:creator>Rudd-O</dc:creator>
		
		<category><![CDATA[Free software]]></category>

		<category><![CDATA[GNOME]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://rudd-o.com/archives/2007/11/25/thanks-michael/</guid>
		<description><![CDATA[Those bug reports were my pleasure.  Keep on doing great stuff like iogrind and your other projects!  The community needs more efforts and people like you are a source of inspiration for newcomers and oldies alike.
]]></description>
			<content:encoded><![CDATA[<p><a href="http://snteam-events.blogspot.com/2007/11/gnome-announce-list-digest-vol-43-issue_22.html">Those bug reports were my pleasure</a>.  Keep on doing great stuff like <code>iogrind</code> and your other projects!  The community needs more efforts and people like you are a source of inspiration for newcomers and oldies alike.</p>
]]></content:encoded>
			<wfw:commentRss>http://rudd-o.com/archives/2007/11/25/thanks-michael/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Speeding Amarok up</title>
		<link>http://rudd-o.com/archives/2007/10/05/speeding-amarok-up/</link>
		<comments>http://rudd-o.com/archives/2007/10/05/speeding-amarok-up/#comments</comments>
		<pubDate>Fri, 05 Oct 2007 15:25:47 +0000</pubDate>
		<dc:creator>Rudd-O</dc:creator>
		
		<category><![CDATA[Amarok]]></category>

		<category><![CDATA[Free software]]></category>

		<category><![CDATA[Hacks]]></category>

		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Performance]]></category>

		<guid isPermaLink="false">http://rudd-o.com/archives/2007/10/05/speeding-amarok-up/</guid>
		<description><![CDATA[Amarok’s collection is a godsend.  The collection scanner isn’t.  It kills my carefully primed cache with lots of memory pages full of MP3 data.  20.000 MP3s will do that to you (not my actual song count).  Therefore, I propose the following patch:



The patch

The patch is against TagLib svn, the library in [...]]]></description>
			<content:encoded><![CDATA[<p>Amarok&#8217;s collection is a godsend.  The collection scanner isn&#8217;t.  It kills my carefully primed cache with lots of memory pages full of MP3 data.  20.000 MP3s will do that to you (not my actual song count).  Therefore, I propose the following patch:</p>

<p><span id="more-1706"></span></p>

<h2>The patch</h2>

<p>The patch is against TagLib <code>svn</code>, the library in charge of reading the MP3 tags and feeding them to Amarok:</p>

<h1><pre style="clear:both">Index: toolkit/tfile.cpp</pre></h1>

<p>--- toolkit/tfile.cpp   (revisión: 721576)
+++ toolkit/tfile.cpp   (copia de trabajo)
@@ -30,7 +30,11 @@
 # include &lt;io.h&gt;
 # define ftruncate _chsize
 #else
+ /* Rudd-O: for fadvise <em>/
+ /</em> I have NO idea how to make this conditional on compile time.  Ideas? */
+ #define _XOPEN_SOURCE 600
  #include &lt;unistd.h&gt;
+ #include &lt;fcntl.h&gt;
 #endif
 #include &lt;stdlib.h&gt;</p>

<p>@@ -41,6 +45,7 @@
 # define W_OK 2
 #endif</p>

<p>+
 using namespace TagLib;</p>

<p>class File::FilePrivate
@@ -73,6 +78,7 @@</p>

<p>File::File(const char *file)
 {
+  int fadvise_retvalue = 0;
   d = new FilePrivate(::strdup(file));</p>

<p>// First try with read/write mode, if that fails, fall back to read only.
@@ -87,12 +93,26 @@</p>

<p>if(!d-&gt;file)
     debug("Could not open file " + String(file));
+
+  /* Rudd-O: we don't want the kernel to cache this file descriptor or perform any readahead -- but only if no one else opens the file.  see posix_fadvise(2) <em>/
+  /</em> FIXME I have NO idea how to make this conditional on compile time.  Ideas? */
+  if(d-&gt;file) {
+    fadvise_retvalue = posix_fadvise(fileno(d-&gt;file),0,0,POSIX_FADV_RANDOM);
+    if (fadvise_retvalue != 0)
+      debug("Could not tell the kernel to not cache the file " + String(file));
+  }
 }</p>

<p>File::~File()
 {
-  if(d-&gt;file)
+  int fadvise_retvalue = 0;
+  if(d-&gt;file) {
+    /* Rudd-O: FIXME Also needs to be conditional to the availability of posix_fadvise */
+    fadvise_retvalue = posix_fadvise(fileno(d-&gt;file),0,0,POSIX_FADV_DONTNEED);
+    if (fadvise_retvalue != 0)
+      debug("Could not tell the kernel to evict from cache the file " + String(file));
     fclose(d-&gt;file);
+  }
   delete d;
 }</p>

<p></p>

<h2>The theory</h2>

<p><code>posix_fadvise()</code>, in theory, on POSIX systems (Linux included) lets you tell the system how to behave with regard to a certain file.  In particular, this patch tells Linux that the MP3 will be accessed randomly.</p>

<p>Linux 2.6 obeys it by simply not performing readahead.  In effect, the patch says &#8220;you shouldn&#8217;t perform readahead whlie reading the tags on the MP3s&#8221;.</p>

<p>When the file is closed, the patch also uses the <code>POSIX_FADV_DONTNEED</code> hint, to make Linux evict any memory used by the recently read file from the page cache (but only if no other process/thread has the file open, so it should be safe).</p>

<h3>Why this should work</h3>

<p>TagLib figures out the tags by reading two key parts of the MP3 file: the very end for ID3v1 (a couple hundred bytes), and the very beginning for ID3v2 (up to 64 K, but usually considerably less).</p>

<p>What TagLib does is:</p>

<ol>
<li>Open the file</li>
<li>Read the data (a total of anywhere between 16K and 64K)</li>
<li>Close the file</li>
</ol>

<p>But the Linux kernel does a few magic performance tricks behind the scenes, which actually cause the following to happen:</p>

<ol>
<li>Open the file</li>
<li>Read the data &#8212; with block device and filesystem readahead, so maybe up to 512K will be read</li>
<li>Put the data in the block memory cache</li>
<li>Close the file</li>
<li>Hold on to the data in the cache for as long as possible, maybe paging out some pages from the current working set</li>
</ol>

<p>Now, under regular conditions, these performance tricks actually work.  But in the context of a collection scan, it&#8217;s brain damaged &#8212; does your computer really need to extra data from thousands of files, and then keep that data in memory?</p>

<p>Since there is no need for readahead (at most eight or nine 8K blocks will be needed, and the read operation is explicit about that), we disable that.  This should considerably reduce the I/O demands.</p>

<p>Since there is no need for caching the contents (what are the odds that you want to play a specific file in 15.000, seconds after it&#8217;s been scanned?) we save memory by telling Linux not to cache the contents of the files.  This should relieve memory pressure considerably, and preserve other things in memory (think application icons or code).</p>

<p>This patch should make scans faster and make your computer more usable while your music is being scanned or when Amarok is starting up, but&#8230;</p>

<p><span style="font-size:xx-large">I need benchmarks!</span></p>

<p>Who volunteers?  I&#8217;ll post them here!</p>

<p><ins datetime="2007-10-08T12:59:17+00:00">Update: here&#8217;s <a href="https://bugs.kde.org/show_bug.cgi?id=150508">the corresponding bug report</a> in the KDE bug tracking system.</ins></p>
]]></content:encoded>
			<wfw:commentRss>http://rudd-o.com/archives/2007/10/05/speeding-amarok-up/feed/</wfw:commentRss>
		</item>
		<item>
		<title>Tales from responsivenessland: why Linux feels slow, and how to fix that</title>
		<link>http://rudd-o.com/archives/2007/10/02/tales-from-responsivenessland-why-linux-feels-slow-and-how-to-fix-that/</link>
		<comments>http://rudd-o.com/archives/2007/10/02/tales-from-responsivenessland-why-linux-feels-slow-and-how-to-fix-that/#comments</comments>
		<pubDate>Wed, 03 Oct 2007 03:21:05 +0000</pubDate>
		<dc:creator>Rudd-O</dc:creator>
		
		<category><![CDATA[Linux]]></category>

		<category><![CDATA[Performance]]></category>

		<category><![CDATA[Tutorials]]></category>

		<guid isPermaLink="false">http://rudd-o.com/archives/2007/10/02/tales-from-responsivenessland/</guid>
		<description><![CDATA[Desktop performance on Linux computers has been a hot-button issue of late, and a source of longstanding fights among the Linux developers.  Today, I want to show you how I boosted (and you can boost) desktop performance dramatically.



The problem

What’s the number one cause of desktop performance problems?

Let me answer that question with another set [...]]]></description>
			<content:encoded><![CDATA[<p>Desktop performance on Linux computers has been a hot-button issue of late, and a source of longstanding fights among the Linux developers.  Today, I want to show you how I boosted (and you can boost) desktop performance dramatically.</p>

<p><span id="more-1703"/></p>

<h2>The problem</h2>

<p>What’s the number one cause of desktop performance problems?</p>

<p>Let me answer that question with another set of questions:</p>

<ul>
<li>Have you noticed how slow your computer is after  after unzipping a big file?</li>
<li>Perhaps you can also notice this effect overnight as well — you return to your computer, and every open application takes a while before they behave snappily again.</li>
<li>Do you use Amarok?  Then you’ll notice this effect after Amarok has finished scanning your music collection.</li>
</ul>

<p>This problem happens all the time.  In fact, I would be willing to bet this is the cause most people find Linux to be slow.</p>

<h2>The source of the problem</h2>

<p>Here’s my chief finding: desktop performance has very little to do with schedulers and real performance.  It has everything to do with disk caching and <em>perceived</em> performance.</p>

<p>Now, bear with me for a second, because what I’m about to explain will let you understand the following.  Based on my limited understanding of the Linux kernel (please correct me if I’m wrong, especially about the terminology), there are two distinct cache types:</p>

<ul>
<li>a block cache: caches block device contents</li>
<li>a inode/dentry cache: one layer above the block cache, caches directory entries and other filesystem-related things that cost more to look up than just block device contents</li>
</ul>

<p>Problem number 1 is that the block cache competes with applications for RAM.  Problem number two is that the block cache competes with the inode/dentry cache.</p>

<p>On a normal desktop Linux computer, when you’re short of RAM, Linux will aggressively try to free memory to enlarge the caches.  It usually does this by paging out unused portions of applications to disk.  This is not good.</p>

<p>Why?  Since disks are slow, the following consequences follow:</p>

<h3>Let’s get technical (skip this heading if you’re uncomfortable with technicalities)</h3>

<p>For desktop performance, where workloads are unpredictable, responsiveness needs dictate that applications’ working sets should be in core as much as possible, because the probability of major page faults in desktop loads is much larger than on server loads.</p>

<p>Let me rephrase that: <span style="font-size: xx-large">Screw data.  Prioritize code.</span></p>

<p>In other words: it’s OK if your application stalls a bit while reading non-cached <em>data</em> from disk, because that isn’t going to make the application seem frozen for a couple of seconds.  It’s <em>not</em> OK if the application freezes because it needs to execute code that was paged out to disk.</p>

<h3>All in all, how Linux uses your RAM matters a lot for desktop performance</h3>

<p>You can see where this is going: if you have 1 GB of RAM (or less) you unzip a 300 MB archive, and the Linux kernel will swap out portions of your applications to disk in an effort to accomodate both the 300 MB file and the blocks of the uncompressed files about to be written on disk.  It will also shrink the inode/dentry cache.</p>

<p>Linux, of course, thinks it’s doing a good thing — the archive uncompresses, in effect, rather quickly because of this.  You, of course, want to kick Linux in the nuts because while (and after) the uncompress took place, starting an application or opening a new browser window suddenly takes ages, and your open applications “blank out” for a couple of seconds before reacting to your clicks and keystrokes.</p>

<p>Think about it: Linux is catering to the needs of the uncompression program, and you are actively telling your computer you want to do other stuff while it happens.  Linux starts to scramble between reading the archive, writing data to disk, and bringing the portions of the applications you’re using “back from the dead” (from swap).  Guess who loses?  If you guessed “the applications” or “you”, you’d have been right.</p>

<p>Even worse: suppose you want to browse your home directory while all of this happens.  In addition to the fact that your file manager is probably half-on-disk by now, you’ve got the problem that Linux needs to re-read your home directory, since the inode/dentry cache has been squashed.</p>

<p>Not good.  I posit that the default settings may be very good for workloads with many repeating tasks, but definitely <em>un</em>good for desktop situations, when applications (and code paths) are diverse and respond only to the whims of the user.</p>

<h3>What we want is perceived performance</h3>

<p>Let’s take a file manager for example. Let’s focus on Konqueror (it’s a nice case study, and it’s a nice file manager).  Suppose I hit the home button on my panel, which shows one of the (usually hidden and prelaunched) Konqueror instances and prompts it to browse my home directory.</p>

<p>If you have lots of RAM, it won’t be a problem — both Konqueror and the contents of your home directory will be in memory, so it’ll be blindingly fast.  But if you’re rather short of memory, it’s a different matter — what happens next determines whether you feel your computer slow or fast.</p>

<p>If Konqueror has been paged out, it will appear to be frozen (or take longer to “start up”) for a couple of seconds, until Linux has paged necessary code paths in.  If, on the contrary, my home directory has been evicted from the RAM cache, Konqueror will show up instantly and be responsive, while the home directory loads.</p>

<p>I’d much rather wait for the directory display than have to wait for Konq to unfreeze because it was paged out.  The difference is that in the first scenario, I can close the window, use the menus, navigate among the window controls, change the URL, abort the operation; in the second case, I’m screwed until Linux decides to fully pagein whatever Konq needs.</p>

<p>That’s what I’m arguing for — perceived performance, not throughput.  It matters to me that I can manipulate my file manager half-a-second after I’ve hit the home button.  It doesn’t matter to me that, because of this preference, the home directory actually takes one second longer to finish displaying.</p>

<p>Variations of this pattern can be found everywhere: in file open dialogs, in multimedia applications with collection managers, basically everywhere an operation requires some sort of progress report.</p>

<h2>The solution</h2>

<p>There are two distinct and complementary measures we’ll take to solve this problem.  Keep reading to find out about them.</p>
]]></content:encoded>
			<wfw:commentRss>http://rudd-o.com/archives/2007/10/02/tales-from-responsivenessland-why-linux-feels-slow-and-how-to-fix-that/feed/</wfw:commentRss>
		</item>
	</channel>
</rss>
