<?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>Alex on Linux&#187; System Administrator Articles</title>
	<atom:link href="http://www.alexonlinux.com/category/articles/system-administrator-articles/feed" rel="self" type="application/rss+xml" />
	<link>http://www.alexonlinux.com</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2012 20:42:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Python for bash replacement</title>
		<link>http://www.alexonlinux.com/python-for-bash-replacement</link>
		<comments>http://www.alexonlinux.com/python-for-bash-replacement#comments</comments>
		<pubDate>Sun, 26 Dec 2010 16:25:37 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Opinion]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[Short articles]]></category>
		<category><![CDATA[System Administrator Articles]]></category>
		<category><![CDATA[awk]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[CLI]]></category>
		<category><![CDATA[ipython]]></category>
		<category><![CDATA[piping]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[redirections]]></category>
		<category><![CDATA[sed]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1847</guid>
		<description><![CDATA[When I started learning Python, I was looking for a programming language that would replace BASH, AWK and SED. I am a C/C++ programmer and as such I better invest my time into studying C and C++. Instead, every time I needed some complex script I opened up a book on BASH and refreshed my [...]
Related posts:<ol>
<li><a href='http://www.alexonlinux.com/6-things-that-i-miss-in-bash' rel='bookmark' title='6 things that I miss in bash'>6 things that I miss in bash</a></li>
<li><a href='http://www.alexonlinux.com/pythons-optparse-for-human-beings' rel='bookmark' title='Python&#8217;s optparse for human beings'>Python&#8217;s optparse for human beings</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When I started learning Python, I was looking for a programming language that would replace BASH, AWK and SED. I am a C/C++ programmer and as such I better invest my time into studying C and C++. Instead, every time I needed some complex script I opened up a book on BASH and refreshed my knowledge. And since bumping into boundaries of what BASH can do is relatively easy, I always opened awk/sed book few minutes later.</p>
<p>Actually, this is quiet common. Once in a while I see my colleagues, just like myself, open up a book on BASH. The problem is that because we don&#8217;t actively program BASH, the knowledge and experience that we gain from this experience wear out over time. So next time we approach, so we have to repeatedly study BASH stuff over and over again. And again, this is not only BASH I am talking about, but also AWK and SED.</p>
<p>It is utterly broken state of affairs and I wish there was a solution. Unfortunately there is no solution yet. The good thing is that with some effort the solution may arise. I am talking about Python programming language.</p>
<p><span id="more-1847"></span></p>
<p>Needless to say that Python can do everything that BASH can do. However, it was never designed as shell environment and as such it is hardly convenient.</p>
<p>For instance, running external commands is easy. You should use subprocess module to run the command and there you go, you have both return value and the output. But this is miles away from convenience of simply typing the command and hitting enter.</p>
<p>Another issue is input/output redirections and piping. Regular shell does this with ease. Python obviously does this as well, but not without a hassle of importing the right module and creating the right object, etc.</p>
<p>On the other hand things are not that bad. For instance implementing command line should be somewhat easy in Python because it has native support for completion (which it uses in its own command line interface).</p>
<p>There is a project that makes few steps in the right direction. I am talking about IPython of course. Unfortunately, I doubt that IPython will ever be able to replace BASH. It was never their goal. Even though IPython developers implemented some features that make IPython command line a little down to earth, I am in doubt they will continue moving in this direction. Also, I must say that IPython evolves quiet slowly.</p>
<p>What do you think? Will Python ever be able to replace BASH?</p>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/6-things-that-i-miss-in-bash' rel='bookmark' title='6 things that I miss in bash'>6 things that I miss in bash</a></li>
<li><a href='http://www.alexonlinux.com/pythons-optparse-for-human-beings' rel='bookmark' title='Python&#8217;s optparse for human beings'>Python&#8217;s optparse for human beings</a></li>
<li><a href='http://www.alexonlinux.com/my-next-programming-language' rel='bookmark' title='My next programming language'>My next programming language</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/python-for-bash-replacement/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>MSI-X &#8211; the right way to spread interrupt load</title>
		<link>http://www.alexonlinux.com/msi-x-the-right-way-to-spread-interrupt-load</link>
		<comments>http://www.alexonlinux.com/msi-x-the-right-way-to-spread-interrupt-load#comments</comments>
		<pubDate>Wed, 18 Nov 2009 07:46:11 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Programming Articles]]></category>
		<category><![CDATA[System Administrator Articles]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1632</guid>
		<description><![CDATA[When considering ways to spread interrupts from one device among multiple cores, I can&#8217;t not to mention MSI-X. The thing is that MSI-X is actually the right way to do the job. Interrupt affinity, which I discussed here and here, has a fundamental problem. That is inevitable CPU cache misses. To emphasise this, think about [...]
Related posts:<ol>
<li><a href='http://www.alexonlinux.com/why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing' rel='bookmark' title='Why interrupt affinity with multiple cores is not such a good thing'>Why interrupt affinity with multiple cores is not such a good thing</a></li>
<li><a href='http://www.alexonlinux.com/smp-affinity-and-proper-interrupt-handling-in-linux' rel='bookmark' title='SMP affinity and proper interrupt handling in Linux'>SMP affinity and proper interrupt handling in Linux</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>When considering ways to spread interrupts from one device among multiple cores, I can&#8217;t not to mention MSI-X. The thing is that MSI-X is actually the right way to do the job.</p>
<p>Interrupt affinity, which I discussed <a href="why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing" rel="nofollow" >here</a> and <a href="smp-affinity-and-proper-interrupt-handling-in-linux" rel="nofollow" >here</a>, has a fundamental problem. That is inevitable CPU cache misses. To emphasise this, think about what happens when your computer receives a packet from the network. Packet belongs to some connection. With interrupt affinity the packet would land on core X, while the chances are that previous packet on the same TCP connection has landed on core Y (X ≠ Y).</p>
<p>Handing the packet would require kernel to load TCP connection object into X&#8217;s cache. But, this is so ineffective. After all, the TCP connection object is already in Y&#8217;s cache. Wouldn&#8217;t it be better to handle second packet on core Y as well?</p>
<p><span id="more-1632"></span>This is the  problem with interrupt affinity. From one point of view we want to spread interrupts to even the load on cores. From another point of view, doing simple round robin isn&#8217;t enough. The little fella that decides where each interrupt goes, should be able to look into the packet and depending on what TCP connection it belongs to, send the interrupt to core that handles all packets that belong to this connection.</p>
<p>Ideally, NICs should be able to:</p>
<ol>
<li>Look into packets and identify connections.</li>
<li>Direct interrupt to core that handles the connection.</li>
</ol>
<p>Apparently, this functionality already here. Devices that support MSI-X do exactly this.</p>
<h2>Meet MSI-X</h2>
<p>MSI-X is an extension to MSI. MSI replaces good old pin based interrupt delivery mechanism.</p>
<blockquote><p>Each IO-APIC chip (x86 permits up to 5) has 24 legs, each connected to one or more devices. When IO-APIC receives an interrupt, it redirects the interrupt to one of the local-APICs. Each local-APIC connected to a core that receives an interrupt.</p></blockquote>
<p>MSI provides a kind of protocol for interrupt delivery. Instead of raising signal on pins, PCI cards send a message over MSI and IO-APIC translates the message into right interrupt. Theoretically this means that each device can have number of interrupt vectors. In reality, plain MSI does not support this, but MSI-X does.</p>
<p>Modern high-end network cards that support MSI-X, implement multiple tx-rx queues. Each queue tied up to an interrupt vector and each NIC has plenty of them. I checked Intel&#8217;s 82575 chipset. With igb driver compiled properly, it has up to eight queues, four rx and four tx. Broadcom&#8217;s 5709 chipset provides eight queues (and eight interrupt vectors), each handling both rx and tx.</p>
<p>In kernel 2.6.24, kernel developers introduced new member of struct sk_buff called queue_mapping. This member tells incoming NIC driver what queue to use when transmitting the packet.</p>
<p>Before transmitting the packet, kernel decides what queue to use for this packet (net/core/dev.c:dev_queue_xmit()). It uses two techniques to do so. First, kernel can ask NIC driver to provide a queue number for the packet. This functionality, however, is optional in NIC drivers and at the moment both Intel and Broadcom drivers don&#8217;t provide it. Otherwise, kernel uses a simple hashing algorithm that produces 16 bit number from two ip addresses and (in case of TCP or UDP) two port numbers. All this happens in function named simple_tx_hash() in net/core/dev.c.</p>
<p>When receiving packets, things are even easier because NIC firmware and the driver decide what queue to use to introduce the packet to the kernel.</p>
<p>Using this simple technique kernel and modern NIC&#8217;s can verify that packets that belong to certain connection land on certain queue. Using interrupt affinity binding techniques you can bind certain interrupt vector to certain core (writing to smp_affinity, etc). Thus you can spread interrupts among multiple cores and yet make sure there are no cache misses.</p>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing' rel='bookmark' title='Why interrupt affinity with multiple cores is not such a good thing'>Why interrupt affinity with multiple cores is not such a good thing</a></li>
<li><a href='http://www.alexonlinux.com/smp-affinity-and-proper-interrupt-handling-in-linux' rel='bookmark' title='SMP affinity and proper interrupt handling in Linux'>SMP affinity and proper interrupt handling in Linux</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/msi-x-the-right-way-to-spread-interrupt-load/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Why interrupt affinity with multiple cores is not such a good thing</title>
		<link>http://www.alexonlinux.com/why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing</link>
		<comments>http://www.alexonlinux.com/why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing#comments</comments>
		<pubDate>Thu, 17 Sep 2009 12:44:23 +0000</pubDate>
		<dc:creator>Alexander Sandler</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[System Administrator Articles]]></category>
		<category><![CDATA[affinity]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[CPU]]></category>
		<category><![CDATA[interrupt]]></category>
		<category><![CDATA[motherboard]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[tcp]]></category>

		<guid isPermaLink="false">http://www.alexonlinux.com/?p=1613</guid>
		<description><![CDATA[One of the features of x86 architecture is ability to spread interrupts evenly among multiple cores. Benefits of such configuration seems to be obvious. Interrupts consume CPU time and by spreading them on all cores we avoid bottle-necks. I&#8217;ve written an article explaining this mechanism in greater detail. Yet let me remind you how it [...]
Related posts:<ol>
<li><a href='http://www.alexonlinux.com/smp-affinity-and-proper-interrupt-handling-in-linux' rel='bookmark' title='SMP affinity and proper interrupt handling in Linux'>SMP affinity and proper interrupt handling in Linux</a></li>
<li><a href='http://www.alexonlinux.com/msi-x-the-right-way-to-spread-interrupt-load' rel='bookmark' title='MSI-X &#8211; the right way to spread interrupt load'>MSI-X &#8211; the right way to spread interrupt load</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>One of the features of x86 architecture is ability to spread interrupts evenly among multiple cores. Benefits of such configuration seems to be obvious. Interrupts consume CPU time and by spreading them on all cores we avoid bottle-necks.</p>
<p>I&#8217;ve written an <a href="http://www.alexonlinux.com/smp-affinity-and-proper-interrupt-handling-in-linux"title="SMP affinity and proper interrupt handling"  onclick="return TrackClick('http%3A%2F%2Fwww.alexonlinux.com%2Fsmp-affinity-and-proper-interrupt-handling-in-linux','SMP+affinity+and+proper+interrupt+handling')">article explaining this mechanism in greater detail</a>. Yet let me remind you how it works in two words.</p>
<p><span id="more-1613"></span>Every x86 motherboard has a chip called IO-APIC. This is a device that controls interrupt delivery within your system. It knows how many CPUs are in your system and can direct various interrupts to various CPUs. It uses so called local APIC-ID as an identifier of the processor.</p>
<p>It has two modes of operation. In one mode it sends interrupts from certain device to single, predefined core. This mode of operation called fixed/physical mode. In another mode, it can deliver interrupts from certain device to multiple cores. The later mode called logical/low priority interrupt delivery mode.</p>
<p>When in logical mode, IO-APIC can deliver interrupts to up to eight cores. Source of this limitation is the size of the bitmask register that tells what CPUs should receive the interrupts. The bitmask is only eight bits long.</p>
<p>When considering the round robin-type of interrupt delivery mode (the logical mode), I cannot stop thinking about how it degrades performance.</p>
<p>You see, having burden of interrupt handling spread among multiple cores may solve some bottle-necks, but it creates a problem.</p>
<p>Consider network interface card for example. Lets say we have a TCP connection to some host out there. When packet arrives, the network card issues an interrupt and IO-APIC directs it to one of the cores. Next, the core handing the packet should fetch the TCP connection objects from the memory to its cache.</p>
<p>IO-APIC does not guarantee that next packet that belongs to the connection will be handled by the same core. So, it is likely that two cores will have to work with TCP connection object. Both of them will have to fetch its content into their cache. This will cause <a href="http://en.wikipedia.org/wiki/Cache_coherence" rel="nofollow" title="Link to cache coherency on Wikipedia"  onclick="return TrackClick('http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FCache_coherence','Link+to+cache+coherency+on+Wikipedia')">cache coherency</a> problems (cache misses). And as you can learn from the <a href="aligned-vs-unaligned-memory-access" rel="nofollow" title="Link to aligned vs unaligned memory access article" >article I&#8217;ve written on misaligned memory accesses</a>, accessing memory that is not in cache can take up to 30 times more time than accessing cached RAM.</p>
<p>Moreover, assuming TCP connection object is properly protected using synchronization techniques, one of the cores will inevitably have to wait for the other, adding unnecessary delay to packet processing.</p>
<p>My point is that round-robin style interrupt delivery can be quiet nasty on performance. It is much better to deliver interrupts from certain device to given core.</p>
<p>Luckily, <em>smp_affinity</em> interface, that I mentioned in my <a href="smp-affinity-and-proper-interrupt-handling-in-linux" rel="nofollow" title="Link to smp affinity and proper interrupt handling article" >old article</a>, allows you to bind interrupts from certain device to certain (single) core.</p>
<p>On some computers IO-APIC does not support logical delivery mode. This can be because of buggy BIOS or too many CPUs. On such computers physical interrupt delivery mode is the only thing that works, so binding single interrupt to single core is the only choice and the only thing you can do is switch the core from one to another.</p>
<p>My point is that round-robin style interrupt delivery can:</p>
<ol>
<li>Malfunction</li>
<li>Cause performance degradation.</li>
</ol>
<p>So, when it still might become useful, you may ask? It depends on what you do with your computer. Usually, you don&#8217;t need round-robin style interrupt delivery. You only need it if you know that your computer receives lots of interrupts and you have real-time applications.</p>
<p>In this case, scheduler (which has no idea about interrupts) can schedule thread that requires lots of CPU time to run on core that serves interrupts. Since interrupts has higher priority, the thread will receive less CPU time. In case of real-time application it may result in reduced responsiveness.</p>
<p>Even so, you can still assign all interrupts to one core and use thread affinity techniques to make sure that your application doesn&#8217;t use that core.</p>
<p>Related posts:<ol>
<li><a href='http://www.alexonlinux.com/smp-affinity-and-proper-interrupt-handling-in-linux' rel='bookmark' title='SMP affinity and proper interrupt handling in Linux'>SMP affinity and proper interrupt handling in Linux</a></li>
<li><a href='http://www.alexonlinux.com/msi-x-the-right-way-to-spread-interrupt-load' rel='bookmark' title='MSI-X &#8211; the right way to spread interrupt load'>MSI-X &#8211; the right way to spread interrupt load</a></li>
<li><a href='http://www.alexonlinux.com/a-new-kind-of-virtualization' rel='bookmark' title='A new kind of virtualization'>A new kind of virtualization</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://www.alexonlinux.com/why-interrupt-affinity-with-multiple-cores-is-not-such-a-good-thing/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>

