<?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>Coral Blocks &#187; CoralThreads</title>
	<atom:link href="https://www.coralblocks.com/index.php/category/coralthreads/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.coralblocks.com/index.php</link>
	<description>Building amazing software, one piece at a time.</description>
	<lastBuildDate>Fri, 03 Apr 2026 15:31:21 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Getting Started with CoralThreads</title>
		<link>https://www.coralblocks.com/index.php/getting-started-with-coralthreads/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=getting-started-with-coralthreads</link>
		<comments>https://www.coralblocks.com/index.php/getting-started-with-coralthreads/#comments</comments>
		<pubDate>Sat, 26 Apr 2014 20:02:02 +0000</pubDate>
		<dc:creator><![CDATA[cb]]></dc:creator>
				<category><![CDATA[CoralThreads]]></category>
		<category><![CDATA[affinity]]></category>
		<category><![CDATA[context switch]]></category>
		<category><![CDATA[core]]></category>
		<category><![CDATA[hyper-threading]]></category>
		<category><![CDATA[pinning]]></category>
		<category><![CDATA[thread]]></category>

		<guid isPermaLink="false">http://cb.soliveirajr.com/index.php/?p=205</guid>
		<description><![CDATA[ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>In this article we will see how easy it is to use CoralThreads to pin a critical thread to a microprocessor core. <span id="more-205"></span></p>
<h2 class="coral">The microprocessor architecture</h2>
<p>Before we start, let&#8217;s lay down the basic architecture of microprocessors and the terminology used by CoralThreads. Going straight to the point, what you need to know is:</p>
<ul>
<li>Most microprocessors nowadays are multi-core</li>
<li>A core can execute a single process or thread in parallel with the other cores (i.e. true parallelism)</li>
<li>Intel went a step further and introduced hyper-threading which allows a core to execute not only one but two threads concurrently inside a core with extremely fast context switches between them</li>
</ul>
<p>Now to keep things simple, CoralThreads uses the following names:</p>
<ul>
<li><strong>Chip</strong> for the physical multi-core microprocessor you can hold in your hands</li>
<li><strong>Core</strong> for each independent processing unit inside a <em>chip</em></li>
<li><strong>Processor</strong> for each independent processing unit inside a <em>core</em></li>
</ul>
<p>To summarize: a chip can have more than one core. Without hyper-threading, each core will have only one processor. With hyper-threading, each core will have two processors. Because of hyper-threading, it is important to understand that <em>the thread is being executed by a processor inside the core and not by the core itself</em>. Well, without hyper-threading a core will have only one processor, so one could assume that it is the core that is executing the thread. But to keep our terminology consistent with hyper-threading, we separate the core from its processors.</p>
<h2 class="coral">Examining your microprocessor</h2>
<p>Grab your CoralThreads jar and run the <code>Affinity</code> class to see the status of everything we talked above:</p>
<pre>
$ java -cp coralthreads-all.jar com.coralblocks.coralthreads.Affinity

CpuInfo: [<font color="blue">nChips=<b>1</b></font>, <font color="blue">nCoresPerChip=<b>4</b></font>, <font color="blue">hyper-threading=<b>true</b></font>, <font color="blue">nProcessors=<b>8</b></font>, <font color="blue">procIds=<b>0,1,2,3,4,5,6,7</b></font>]

Chip-0:
    Core-0:
        Processor-0: free
        Processor-4: free
    Core-1:
        Processor-1: free
        Processor-5: free
    Core-2:
        Processor-2: free
        Processor-6: free
    Core-3:
        Processor-3: free
        Processor-7: free
</pre>
<p>As you can see from above, the machine has one chip and 4 cores (quad-core). Hyper-threading is turned on so instead of 4 processors we have 8. Their ids are 0-7. You can also see that currently there are no threads assigned to any processor.</p>
<h2 class="coral">Pinning a thread to a processor</h2>
<p>You can see in the example below how easy it is to use CoralThreads:</p>
<pre class="brush: java; highlight: [13,25,35]; title: ; notranslate">
import com.coralblocks.coralthreads.Affinity;

public class Basics {
	
	public static void main(String[] args) throws Exception {
		
		Thread thread = new Thread(new Runnable() {

			@Override
            public void run() {

				// must be the first thing inside the run method
				Affinity.bind();
				
				try {
					
					while(true) {
						
						// do whatever you want here...
					}
					
				} finally {
					
					// must be the last thing inside the run method
					Affinity.unbind(); 
				}
            }
		}, &quot;MyPinnedThread&quot;);
		
		System.out.println();
		Affinity.printSituation(); // nothing done yet...
		
		// assign thread to processor:
		int procToBind = Integer.parseInt(args[0]);
		Affinity.assignToProcessor(procToBind, thread);
		
		Affinity.printSituation(); // now you see it there...
		
		// start the thread!
		thread.start();
		
		Affinity.printSituation(); // now it is running with a pid...
	}
}
</pre>
<p>When you run the program above you get the output below. Note that you can pass the processor id you want to bind through the command line.</p>
<pre>
$ java -cp coralthreads-all.jar com.coralblocks.coralthreads.sample.Basics <font color="blue"><b>2</b></font>

CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]

Chip-0:
    Core-0:
        Processor-0: free
        Processor-4: free
    Core-1:
        Processor-1: free
        Processor-5: free
    Core-2:
        Processor-2: free
        Processor-6: free
    Core-3:
        Processor-3: free
        Processor-7: free

CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]

Chip-0:
    Core-0:
        Processor-0: free
        Processor-4: free
    Core-1:
        Processor-1: free
        Processor-5: free
    Core-2:
        <font color="blue"><b>Processor-2</b>: assigned to MyPinnedThread (not-started)</font>
        Processor-6: free
    Core-3:
        Processor-3: free
        Processor-7: free

CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]

Chip-0:
    Core-0:
        Processor-0: free
        Processor-4: free
    Core-1:
        Processor-1: free
        Processor-5: free
    Core-2:
        <font color="blue"><b>Processor-2</b>: bound to MyPinnedThread (running pid=2180)</font>
        Processor-6: free
    Core-3:
        Processor-3: free
        Processor-7: free
</pre>
<p>You can check that your thread is running in the correct processor by using the <code> top -H </code> shell command. After it is running hit &#8220;1&#8243; to see the cpus at the top. Cpu2 should be at 100%.</p>
<p><a href="/wp-content/uploads/2014/04/cpu2_100.png"><img src="/wp-content/uploads/2014/04/cpu2_100-300x213.png" alt="cpu2_100" width="300" height="213" class="alignnone size-medium wp-image-267" /></a></p>
<h2 class="coral">Isolating processors from kernel and hardware interrupts</h2>
<p>Thread affinity just picks a processor where a thread will always be executed but it does nothing to actually isolate the processor from external interrupts. Fortunately, all modern operating systems provide ways to do that so you can be sure that your critical thread will have the processor for itself at all times. Coral Blocks has the expertise to configure most operating systems to isolate a processor. Below we list some of the tricks that we have done in the past to accomplish that with great success:</p>
<ul>
<li>Removing a processor from the kernel scheduler load balancer so when you pin a thread to a processor you can be sure that the kernel will never try to preempt it and run another user-space thread on that processor. That effectively isolates the processor from any other user-space thread except the ones you are pinning yourself through CoralThreads.</li>
<li>Disallowing a processor to receive hardware interrupts.</li>
<li>Removing all RCU kernel threads (<a href="http://en.wikipedia.org/wiki/Read-copy-update" target="_blank">read-copy-update</a> threads) from latency-sensitive processors.</li>
<li>Removing all bdi-flush kernel threads (<a href="http://lwn.net/Articles/326552/" target="_blank">pdflush</a> threads) from latency-sensitive processors.</li>
<li>Removing timer interrupts (full tickless mode) from latency-sensitive processors.</li>
</ul>
<p>The availability of the tricks above will depend on your flavor of Linux and most importantly on your kernel version. We have found through extensive research and tests that <em>a newer kernel version does not necessarily mean better performance</em>.</p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>https://www.coralblocks.com/index.php/getting-started-with-coralthreads/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>The Pinned Thread Difference</title>
		<link>https://www.coralblocks.com/index.php/the-pinned-thread-difference/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=the-pinned-thread-difference</link>
		<comments>https://www.coralblocks.com/index.php/the-pinned-thread-difference/#comments</comments>
		<pubDate>Wed, 27 Aug 2014 22:19:18 +0000</pubDate>
		<dc:creator><![CDATA[cb]]></dc:creator>
				<category><![CDATA[CoralThreads]]></category>
		<category><![CDATA[cpuset]]></category>
		<category><![CDATA[critical thread]]></category>
		<category><![CDATA[isolated threads]]></category>
		<category><![CDATA[isolcpus]]></category>
		<category><![CDATA[multithreading]]></category>
		<category><![CDATA[pinned threads]]></category>
		<category><![CDATA[thread affinity]]></category>

		<guid isPermaLink="false">http://www.coralblocks.com/index.php/?p=633</guid>
		<description><![CDATA[ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>To illustrate the advantage of a pinned thread, we code a program that launches a bunch of threads to do a simple task and pin one of them to a processor. At the end you can see that the pinned thread is much ahead of the other threads. That&#8217;s because the other threads have to compete for the available processors while the pinned thread has a whole processor for itself. Without context switches and interruptions the pinned thread can perform its job much faster than the other ones. <span id="more-633"></span></p>
<pre class="brush: java; title: ; notranslate">
import com.coralblocks.coralthreads.Affinity;
import com.coralblocks.coralbits.util.SystemUtils;

public class ManyThreads {
	
	public static void main(String[] args) throws Exception {
		
		final int nThreads = SystemUtils.getInt(&quot;nThreads&quot;, 20);
		final int threadToBind = SystemUtils.getInt(&quot;threadToBind&quot;, 10);
		final int procToBind = SystemUtils.getInt(&quot;procToBind&quot;, 2);
		final long maxLong = SystemUtils.getLong(&quot;maxLong&quot;, 1000000000L);
		
		class PinnedThread extends Thread {
			
			long overlaps = 0;
			long count = 0;
			volatile boolean running = true;
			
			PinnedThread(String name) {
				super(name);
			}
			
			void stopMe() {
				running = false;
			}
			
			@Override
            public void run() {

				Affinity.bind();
				
				try {
					while(running) {
						if (count++ == maxLong) {
							overlaps++;
							count = 0;
						}
					}
				} finally {
					Affinity.unbind();
				}
            }
		}
		
		final PinnedThread[] threads = new PinnedThread[nThreads];
		
		for(int i = 0; i &lt; threads.length; i++) {
			
			threads[i] = new PinnedThread(i == threadToBind ? &quot;PinnedThread&quot; : &quot;Thread&quot; + i);
		}	
			
		if (procToBind &gt;= 0) Affinity.assignToProcessor(procToBind, threads[threadToBind]);
		
		for(PinnedThread t : threads) t.start();
		
		System.out.println();
		Affinity.printSituation();
		
		Runtime.getRuntime().addShutdownHook(new Thread() {
			
			@Override
			public void run() {

				for(PinnedThread t : threads) t.stopMe();
				for(PinnedThread t : threads) try { t.join(); } catch(Exception e) { }
				
				System.out.println();
				
				for(PinnedThread t : threads) {
					System.out.println(t.getName() + &quot;: overlaps=&quot; + t.overlaps + &quot; count=&quot; + t.count);
				}
			}
		});
	}
}
</pre>
<p>The output:</p>
<pre>
$ java -cp coralthreads-all.jar com.coralblocks.coralthreads.sample.ManyThreads

CpuInfo: [nChips=1, nCoresPerChip=4, hyper-threading=true, nProcessors=8, procIds=0,1,2,3,4,5,6,7]

Chip-0:
    Core-0:
        Processor-0: free
        Processor-4: free
    Core-1:
        Processor-1: free
        Processor-5: free
    Core-2:
        <font color="blue"><b>Processor-2</b>: bound to PinnedThread (running pid=3406)</font>
        Processor-6: free
    Core-3:
        Processor-3: free
        Processor-7: free

^C
Thread0: overlaps=18 count=219401874
Thread1: overlaps=19 count=261705318
Thread2: overlaps=18 count=610599006
Thread3: overlaps=18 count=134022214
Thread4: overlaps=18 count=706525436
Thread5: overlaps=18 count=53884487
Thread6: overlaps=17 count=614235810
Thread7: overlaps=18 count=288563757
Thread8: overlaps=18 count=437900710
Thread9: overlaps=18 count=913959165
<font color="blue">PinnedThread: overlaps=<b>87</b> count=402690338</font>
Thread11: overlaps=17 count=965738516
Thread12: overlaps=18 count=847900384
Thread13: overlaps=19 count=6207215
Thread14: overlaps=19 count=19490349
Thread15: overlaps=18 count=60562201
Thread16: overlaps=17 count=940538865
Thread17: overlaps=18 count=730871356
Thread18: overlaps=18 count=187664691
Thread19: overlaps=17 count=719411214
</pre>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>https://www.coralblocks.com/index.php/the-pinned-thread-difference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
