<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://blogs.msdn.com/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Slava Oks's WebLog : Be Aware</title><link>http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx</link><description>Tags: Be Aware</description><dc:language>en-US</dc:language><generator>CommunityServer 2.1 SP1 (Build: 61025.2)</generator><item><title>Be Aware: 4GB of VAS under WOW, does it really worth it?</title><link>http://blogs.msdn.com/slavao/archive/2006/03/12/550096.aspx</link><pubDate>Mon, 13 Mar 2006 03:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:550096</guid><dc:creator>slavao</dc:creator><slash:comments>7</slash:comments><comments>http://blogs.msdn.com/slavao/comments/550096.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=550096</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=550096</wfw:comment><description>By now you have heard a lot about the fact that under WOW a 32 bit process can get 4GB of VAS. I agree this is great, but I would like you to be aware of something that might hit you really hard. As it turns out enabling 4GB VAS can be problematic due...(&lt;a href="http://blogs.msdn.com/slavao/archive/2006/03/12/550096.aspx"&gt;read more&lt;/a&gt;)&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=550096" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/SQL+Server+Memory+Management/default.aspx">SQL Server Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Memory+Management/default.aspx">Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be aware: To Hyper or not to Hyper</title><link>http://blogs.msdn.com/slavao/archive/2005/11/12/492119.aspx</link><pubDate>Sat, 12 Nov 2005 20:48:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:492119</guid><dc:creator>slavao</dc:creator><slash:comments>35</slash:comments><comments>http://blogs.msdn.com/slavao/comments/492119.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=492119</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=492119</wfw:comment><description>Our customers observed very interesting behavior on high end Hyperthreading (HT) enabled hardware. They noticed that in some cases when high load is applied SQL Server CPU usage increases significantly but SQL Server performance degrades. Occasionally they would also see message in errorlog indicating one of the threads can't acquire a spinlock (Spinlock is a lightweight synchronization data structure. Threads keep a spinlock for short period of time. If a thread can't acquire a spinlock it will spin in a tight loop waiting for spinlock to become available). Customers also noticed that when HT is disabled under the same circumstances CPU usage increases slightly and performance is at appropriate level.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;What is going on here? 
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&lt;SPAN style="mso-spacerun: yes"&gt;&lt;/SPAN&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;After some pondering and investigation we came up with the following theory.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Database server has different type of threads: worker threads, threads executing clients request, and system threads that performing system tasks. An example of a system thread is a lazywriter. The responsibility of the lazywriter is to lazily scan through database buffers and push least recently used buffers out of the cache.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Lazywriter usually kicks in whenever data page cache is full and server short of free data page buffers. The interesting behavior of lazywriter is that it scans large amount of memory. With Intel HT technogolgy logical processors share L1 &amp;amp; L2 caches. As you would guess lazywriter's behavior can potentially trash L1 &amp;amp; L2 caches. If a worker thread end up running on logical CPU that shares physical CPU with lazywriter its cache will be constantly trashed.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;It means that most of the memory accesses for the worker thread will be L1 &amp;amp; L2 misses. Moreover whenever the worker attempts to access hot data structure protected by spinlock it potentially might have a less chance of acquiring the spinlock which will cause the worker's spinning. This behavior will translate to high CPU utilization and significant drop in performance.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;To confirm the theory I decided to create an experiment. I wrote a test program which consist of two types of threads: scan threads and lock threads. Scan threads are scanning through large amounts of memory. Lock threads&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;constantly attempt to acquire global lock.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;If the theory is correct a lock thread that shares a physical CPU with a scan thread should acquire lock much less often than other threads in a given period of time. Below is the program's listing. (Disclosure: The program is for illustrative purpose only, it was written to illustrate the observed behavior, it shouldn't be treated as a final-well tested product :-))&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;stdio.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;stdlib.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;windows.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;eh.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;wtypes.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;locale.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#include &amp;lt;process.h&amp;gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;extern "C" void _mm_pause(void);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#pragma intrinsic (_mm_pause)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;inline void&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;SmtPause ()&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#if defined (_X86_) || defined (_AMD64_)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// PAUSE instruction (REP NOP) helps SMT scheduling&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;_mm_pause ();&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;#endif // _X86_&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Statistic information for threads&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;struct Statistics&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;int&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;locked;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;int&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;spinned;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;DWORD_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;affinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;};&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Type of threads the tests using&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;enum ThreadTypes&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;SCAN_THREAD,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;LOCK_THREAD,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;LAST_THREAD&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;};&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Entry Point&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;typedef void( __cdecl *ThreadEntryPoint )( void * );&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Inforamtion per thread - enables us to create threads in a common way&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;struct ThreadInfo&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;ThreadEntryPoint&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;func;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;LONG_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;affinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;};&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Global constants&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Number of CPUs&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;const int&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;MaxNumberOfCPUs = 64;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Block of memory to scan by scan thread&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;char&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;MemoryBlock [1024 *1024];&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Stat information: number of threads is limmited by number of CPU, &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// that is the purpose of the test&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;volatile Statistics&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Stats [MaxNumberOfCPUs];&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Number of threads running&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;volatile long&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;NumberOfThreadsRunning = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// "Spinlock" that all lock threads attempt to acquire&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;volatile long&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Lock = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//-----------------------------------------------------------------------------&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Function: scanThread&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Description:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Attempt to invalidate caches&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Notes:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;1. Function changes 1MB of contiguous vm memory which appear&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;to be enought invalidate L1&amp;amp;L2 caches&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;void __cdecl &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;scanThread (void* param)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;char i = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;ULONG_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadAffinity =(DWORD_PTR) param;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;long&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadId = InterlockedIncrement (&amp;amp;NumberOfThreadsRunning) -1;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;SetThreadAffinityMask (GetCurrentThread (),threadAffinity);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Initialize affinity&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;Stats [threadId].affinity = threadAffinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;while (i &amp;gt;= 0)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;// Memset memory: quick and dirty scan&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;memset (MemoryBlock, 0, sizeof (MemoryBlock));&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;Stats [threadId].spinned++;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;return;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;//-----------------------------------------------------------------------------&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Function: lockThread&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Description:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Acquires and releases a lock in a loop&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Notes:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;void __cdecl &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;lockThread (void* param)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;ULONG_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadAffinity =(DWORD_PTR) param;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;long&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadId = InterlockedIncrement (&amp;amp;NumberOfThreadsRunning)-1;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;SetThreadAffinityMask (GetCurrentThread (), threadAffinity);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Initialize affinity&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;Stats [threadId].affinity = threadAffinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;while (1)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;if (Lock == 0 &amp;amp;&amp;amp; InterlockedExchange (&amp;amp;Lock, 1) == 0)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Increment number of times we acquired lock&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;Stats [threadId].locked ++;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;Lock = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;else&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;{ &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;Stats [threadId].spinned++;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Be HT friendly&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;SmtPause ();&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;continue;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;return;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;//-----------------------------------------------------------------------------&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Function:main&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Description:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;Creates lock and scan threads, reporting stats every 10sec&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;// Notes:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;//&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;int __cdecl &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;main (&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;int argc, &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;char* argv [])&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; LONG_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadId = 0; &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;ThreadInfo&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadInfo [LAST_THREAD];&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;DWORD_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;processAffinity; &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;DWORD_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;threadAffinity; &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;DWORD_PTR&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;systemAffinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;int&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;counter = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;int&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;numberOfThreads = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Retrive process affinity&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;if (GetProcessAffinityMask (GetCurrentProcess (),&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;&amp;amp;processAffinity,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;&amp;amp;systemAffinity) == FALSE)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf ("GetProcessAffinityMask failed - exiting\n");&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;exit (-1);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Initialize threads Information: First default and then check &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// if client passed anything in&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Allocate all but one CPUs for threads running locking code&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;threadInfo [LOCK_THREAD].func = lockThread;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;threadInfo [LOCK_THREAD].affinity = processAffinity &amp;amp; processAffinity-1;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Allocate one CPU for scan thread&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;threadInfo [SCAN_THREAD].func = scanThread;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;threadInfo [SCAN_THREAD].affinity =threadInfo [LOCK_THREAD].affinity ^ processAffinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Read command line arguments: in simple way&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;for (counter = 0 ; counter&amp;lt; argc; counter++) &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;switch (*(argv [counter] + 1))&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;case 's':&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;case 'S':&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;// Retrieve affinity mask for scan threads&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;threadInfo [SCAN_THREAD].affinity = &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 6"&gt;(DWORD_PTR) _atoi64 ((argv [counter] + 2));&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;break;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;case 'l':&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;case 'L':&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;// Retrieve affinity mask for lock threads&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;threadInfo [LOCK_THREAD].affinity =&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 6"&gt;(DWORD_PTR) _atoi64 ((argv [counter] + 2));&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;break;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;case '?':&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;printf ("Usage: test -sn -lm \n"&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 6"&gt;"Example: test.exe -s8 -l7 (runs 1 scanning thread and 3 locking threads)\n"&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 6"&gt;"-sn is affinity of threads performing memory scan \n"&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 6"&gt;"-sm is affinity of threads acquiring lock");&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;exit (0);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;default:&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;break;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Start threads of all types based on supplied affinities&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;for (counter = 0; counter &amp;lt; LAST_THREAD; counter++)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;while (threadInfo [counter].affinity != 0)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Separate affiinity for a thread we are trying to &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// create from all affinity mask&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: Verdana; mso-outline-level: 4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Remember full mask&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;threadAffinity = threadInfo [counter].affinity;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: Verdana; mso-outline-level: 4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Turn off least significant bit&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;threadInfo [counter].affinity &amp;amp;= threadInfo [counter].affinity -1;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: Verdana; mso-outline-level: 4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Calculate the affinity bit for the thread: &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Leave one bit on - affinity bit&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;threadAffinity ^= threadInfo [counter].affinity; &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: Verdana; mso-outline-level: 4"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Use beginthread for simplicity&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;if (_beginthread (threadInfo [counter].func, &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;NULL, &lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;(void*) threadAffinity) == -1)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;printf ("Failed to create thread - exiting\n");&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;exit (-1);&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;numberOfThreads++;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;// Loop and report stats periodically&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;while (1)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;// Report every 10 sec&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;Sleep (10000);&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: Verdana; mso-outline-level: 3"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;for (threadId=0; threadId &amp;lt; numberOfThreads; threadId++)&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;{&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;printf ("Thread Id = %d\t, Thread Affinity = %p\t, Locked = %d\t, Spin = %d\t\n ",&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;threadId,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;Stats [threadId].affinity,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;Stats [threadId].locked,&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: 'Courier New'; mso-outline-level: 5"&gt;Stats [threadId].spinned);&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: Verdana; mso-outline-level: 5"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;// Reset run time data&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;//&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;Stats [threadId].locked = 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 4"&gt;Stats [threadId].spinned =0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 2in; FONT-FAMILY: Verdana; mso-outline-level: 5"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: 'Courier New'; mso-outline-level: 3"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: 'Courier New'; mso-outline-level: 2"&gt;return 0;&lt;/P&gt;
&lt;P style="FONT-SIZE: 8pt; MARGIN: 0in; FONT-FAMILY: 'Courier New'; mso-outline-level: 1"&gt;}&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;As you can see one can specify an affinity for scan and lock threads at start up of the program.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;I ran sever experiments on my dev box which has 2 physical and 4 logical CPUs. CPU 1 (Affinity 0x1) &amp;amp; 3 (Affinity 0x4)&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;and 2 (Affinity 0x2) &amp;amp; 4 (Affinity 0x8) share physical CPUs respectively. The box also has Windows 2003 Server SP1 on it.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;Below are the results:&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;test.exe -s8 -l7 &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 0&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000008&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 0&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 9035&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 1&amp;nbsp; , Thread Affinity = 00000001&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 9290331&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 10373717&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 2&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000002&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 676397&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 40879794&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 3&amp;nbsp; , Thread Affinity = 00000004&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 10274030&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 42433521&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;test.exe -s4 -l11 &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 0&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000004&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 0&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 8943&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 1&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000001&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 741166&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 11421139&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 2&amp;nbsp; , Thread Affinity = 00000002&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 10747824&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 10024252&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 3&amp;nbsp; , Thread Affinity = 00000008&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 10210221&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 13988134&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;test.exe -s1 -l14 &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 0&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000001&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 0&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 8472&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 1&amp;nbsp; , Thread Affinity = 00000002&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 10550011&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 20156206&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 2&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000004&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 722554&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 11211074&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 3&amp;nbsp; , Thread Affinity = 00000008&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 11182506&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 25376166&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;test.exe -s2 -l13 &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 0&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000002&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 0&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 8585&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 1&amp;nbsp; , Thread Affinity = 00000001&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 9900899&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 12267885&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 2&amp;nbsp; , Thread Affinity = 00000004&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 8984095&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 11909080&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Arial; mso-outline-level: 1"&gt;Thread Id = 3&amp;nbsp; , Thread Affinity = &lt;SPAN style="FONT-WEIGHT: bold"&gt;00000008&lt;/SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Locked = 1297138&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; , Spin = 28199769&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Great! The experiment confirms the theory. So does it mean you have to disable HT when using SQL Server? The answer is it really depends on the load and hardware you are using.&lt;SPAN style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;You have to test your application with HT on and off under heavy loads to understand HT's implications. &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Keep in mind that not only lazywriter thread can cause slowdown but any thread that performs large memory scan - for example a worker thread that scans large amount of data might be a culprit as well. &lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;For some customer applications when disabling HT we saw 10% increase in performance. So make sure that you do your home work before you decide to hyper on not to hyper :-)&lt;/P&gt;
&lt;P style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Hope this information was useful. Let me know if you have any questions!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=492119" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/Coding+_2600_+Debugging+tips/default.aspx">Coding &amp; Debugging tips</category><category domain="http://blogs.msdn.com/slavao/archive/tags/SQL+Server+Scheduling/default.aspx">SQL Server Scheduling</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be Aware: VirtualAlloc with MEM_RESERVE can fail even if you have plenty of VAS inside of the process</title><link>http://blogs.msdn.com/slavao/archive/2005/06/03/424905.aspx</link><pubDate>Fri, 03 Jun 2005 19:04:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:424905</guid><dc:creator>slavao</dc:creator><slash:comments>0</slash:comments><comments>http://blogs.msdn.com/slavao/comments/424905.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=424905</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=424905</wfw:comment><description>&lt;P&gt;&amp;nbsp;Many developers assume that if you have plenty of free VAS in your process VirtualAlloc (&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtualalloc.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtualalloc.asp&lt;/A&gt;) and VirtualAllocEx (&lt;A href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtualallocex.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/memory/base/virtualallocex.asp&lt;/A&gt;) calls with MEM_RESERVE parameter can't fail. This is not actually true.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Remember that when allocating VAS region OS creates VAD (Virtual Address Descriptor). VAD manages VAS region, for more info read &lt;a href="http://blogs.msdn.com/slavao/archive/2005/01/27/361678.aspx"&gt;http://blogs.msdn.com/slavao/archive/2005/01/27/361678.aspx&lt;/A&gt;. When system is low on physical memory which includes RAM and swap file, VAD's allocation might fail. The failure will cause VirtualAlloc* call to fail. It means that if system low on physical memory VirtualAlloc* could fail. This becomes very important on 64 bit platforms. When running on 64 bit, developers might assume that reservation never fails - which is wrong.&lt;/P&gt;
&lt;P&gt;Hope it was useful. &lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=424905" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/Coding+_2600_+Debugging+tips/default.aspx">Coding &amp; Debugging tips</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Memory+Management/default.aspx">Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be Aware: Using  AWE, locked pages in memory, on 64 bit</title><link>http://blogs.msdn.com/slavao/archive/2005/04/29/413425.aspx</link><pubDate>Fri, 29 Apr 2005 15:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:413425</guid><dc:creator>slavao</dc:creator><slash:comments>9</slash:comments><comments>http://blogs.msdn.com/slavao/comments/413425.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=413425</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=413425</wfw:comment><description>&lt;P&gt;We have already talked about Windows AWE mechanism on 32 bit and how SQL Server utilizes it. Today I would like to go over AWE &amp;amp; related mechanism on 64 bit platforms.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;To some people it comes as a surprise that AWE mechanism is still present and actually could be useful on 64 bit platforms. As you remember the mechanism consists of two parts allocating physical memory and mapping it to the given process's VAS. The advantage of allocation mechanism is that once physical memory is allocated operating system can't reclaim it until either the process is terminated or the process frees memory back to the OS. This feature allows an application to control and even avoid paging altogether. Advantage of mapping/unmapping mechanism is that the same physical page could be mapped into different VAS's regions. As you imaging unmapping is not necessary on 64 bit platforms since we have enough VAS to accommodate all of existing physical memory. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;From Operating System theory, OS implements a page table entry, PTE, to describe a mapping of a page in VAS to physical page. Internally physical page is described by page frame number, PFN. Given PFN one can derive complete information about physical page it represents. For example PFN shows to which NUMA node the particular page belongs. OS has a database, collection of PFNs that it manages.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;If page in VAS is committed, it has PTE which might or might not point to given PFN.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Conceptually, page that PTE represents can be either in memory or not, for example swapped out to disk. In the former case it is bound to a given PFN and in latter it is not. In its turn, once a physical page is bound to page in VAS, its PFN points back to PTE. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When OS commits, frees, pages out/in a given PTE or needs to derive some information about it, for example NUMA residency, it has to acquire process's working set lock - to guarantee stability of PTE to PFN binding. This lock is a rather expensive and might hurt scalability of the process. Latter versions of Windows made this lock as light as possible but avoiding still will benefit application's scalability..&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;When allocating physical pages utilizing AWE mechanism we are given a set of PFN entries directly from PFN database - remember that you should not manipulate or modify set of entries you get back&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;nor can you rely on values you get back. OS is required to take a PFN database lock when allocating PFN entries. Using AWE map mechanism you can map allocated PFN entries to the process's VAS. When mapping occurs PTEs are allocated, bound to PFNs and marked as locked. In this case OS needs to acquire process's working set lock only ones. When mapping regular pages, OS does it on demand and hence will have to acquire both working set and PFN database lock for every page. Since pages are locked in memory, OS will ignore these PTEs during&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;paging process. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On 64 bit platforms it is better to refer to such pages as locked pages - please don't confuse them with pages locked through VirtualLock API. As described above locked pages have two major properties - they are not considered for paging by OS and during allocation they acquire both working set and PFN database lock only ones.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The first property has implicit implication on high end hardware such as NUMA. It provides explicit memory residency. Remember that OS commits a page on demand. To allocate physical memory, it will use a node on which a thread touching memory is running.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Latter on, the page can be swapped out by OS. Next time it will be brought up into memory, OS will again allocate physical page from the node a thread touching memory is running on. In this case a node could be completely different&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;from original one. Such behavior makes hard for applications to rely on page's NUMA residency. Locked pages solve this problem by removing themselves from paging altogether.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Moreover Windows 2003 SP1 introduced a new API - QueryWorkingSetEx. It allows to query extended&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;information about PTE's PFN.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;In order to find out real page residency you should use this API. When pages are locked you need to it only ones. Otherwise you will have to do it periodically since residency of the page can actually change.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The second property - taking both working set and PFN's database lock only ones enables applications to perform faster and better scalable ramp up. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;On NUMA SQL Server' Buffer Pool marks each allocated page with its node residency. It leverages QueryWorkingSetEx to accomplish it.&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;Once page is allocated it calls the API to find&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;out page residency.&lt;SPAN&gt;&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;It does it only once. Therefore enabling locked pages for SQL server on 64 bit platform&lt;SPAN&gt;&amp;nbsp; &lt;/SPAN&gt;will improve SQL Server ramp up time and will improve performance &amp;amp; scalability over longer period of time. When running SQL Server with locked pages enabled you shouldn't be worried about overall system performance due to memory starvation - SQL Server participates in OS's paging mechanism by listening on OS's memory notification API's and shrinks its working set accordingly. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let us summarize now - on 64 bit platform, locked pages, awe mechanism, enable better application's scalability and performance both during ramp up time and over long period of time. However, keep in mind that an application is still required to implement a way of responding to memory pressure to avoid starving the whole system for memory. &lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Let me know your comments!&lt;/P&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=413425" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/SQL+Server+Memory+Management/default.aspx">SQL Server Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Memory+Management/default.aspx">Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be Aware: IA64 Stack Size</title><link>http://blogs.msdn.com/slavao/archive/2005/03/19/399117.aspx</link><pubDate>Sat, 19 Mar 2005 20:35:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:399117</guid><dc:creator>slavao</dc:creator><slash:comments>11</slash:comments><comments>http://blogs.msdn.com/slavao/comments/399117.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=399117</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=399117</wfw:comment><description>&lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Stack subject in Windows OS is fascinating. There are so many interesting technical problems surrounding it.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The moment you think you fully understand everything about stack you&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&lt;/span&gt;suddenly discover yet another mystery.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;And so it goes. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;In the last couple of weeks I have been approached by our testers and support engineers asking about for how many threads they need to configure SQL Server on IA64 platform with 4GB of RAM.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;For example they asked is it ok to set number of threads to be 1024?&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;You probably know that SQL Server commits its stacks upfront.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;The reason we do it is to avoid unexplained server death - if there is no physical memory and machine is out of swap file, a process can disappear without leaving any traces. This will happen when a process will try to grow its stack.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;As any respectful server product SQL Server can't afford such&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;behavior. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;SQL Server on IA64 is configured to use 2MB of stack size. You can verify it by looking at SQL Server image header. Does it really consume that much of committed memory per thread? The answer is no. On IA64 there are two stacks - regular stack and backing store.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Backing store is a spill space for cpu register engine.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;It is used by cpu to spill registers to memory when it no longer has registers available.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;When a process specifies committed stack size to be 2MB, Windows commits 2MB for regular stack and 2MB for backing store. Consequently each thread will commit 4MB of RAM (If you only specify reservation Windows will reserve that much but not commit). Fig1 shows what the thread stack really looks like on IA64.&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;----------------------0x00000000000200000&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2MB&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Stack&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;/ \&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&lt;span style="mso-spacerun: yes"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; |&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;----------------------0x00000000000400000&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;2MB&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;/span&gt;Backing store&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&lt;span style="mso-spacerun: yes"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&lt;span style="mso-spacerun: yes"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/span&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;\ /&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;|&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;----------------------0x00000000000600000&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Fig 1. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;I would like to emphasize couple of interesting points. First Windows reserves a contiguous VAS region of size of 4MB for both stack and backing store. Second it divides this region in two so that the stack and the backing store grow in different directions. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Now we are in the position to answer the original question of how much physical memory 1024 threads will consume. The answer is 4MB*1024= 4GB. Considering that max server memory in SQL Server only reflects size of Buffer Pool and usually it is configured to be close to amount of physical memory on the box, this configuration might cause SQL server to crawl. This comes as a real surprise to many people.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;Max amount of threads SQL needs to be configured for does depend on the type of application running on top of server but in this case it definitely needs to be below 1024. Even 512 number of threads could be too much.&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;This post will be incomplete if I didn't somehow mention stack overflow. Backing store makes IA64 stack handling to be different from other platforms. Stack overflow is not an exception in this case - OS needs to handle stack overflow both for regular stack and backing store. Surprise! For you it means that you have to be really careful when recovering from stack overflow especially when you diced to restore guard page by hand. But this is a whole different story on its own. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;I hope you learned something from this post. Comments are welcomed!&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Have a great weekend!&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=399117" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/SQL+Server+Memory+Management/default.aspx">SQL Server Memory Management</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Coding+_2600_+Debugging+tips/default.aspx">Coding &amp; Debugging tips</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be aware: FLS-Fiber Local Storage</title><link>http://blogs.msdn.com/slavao/archive/2005/02/28/381649.aspx</link><pubDate>Mon, 28 Feb 2005 19:23:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:381649</guid><dc:creator>slavao</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/slavao/comments/381649.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=381649</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=381649</wfw:comment><description>&lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Problem:&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;In the last several months there were several publications describing usage of fibers.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;When covering SQLOS's scheduling I will go into more details about them. Today I would like to touch on the subject on how to make your dlls/libraries more robust in the fiber environment. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;As you know, when running SQL server in a fiber mode you need to be very careful when using functionality that make heavy usage of TEB, thread environment block, or TLS, thread local storage. During a fiber switch TLS and many other fields in TEB don't get switched.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;You will be surprised how many libraries leverage TLS and don't work correctly with Fibers. Even older versions of MSVCRT, c-runtime, leverage TLS to allocate their per thread local data. The list of broken libraries include:&lt;/p&gt; &lt;ul style="MARGIN-TOP: 0in; MARGIN-BOTTOM: 0in; MARGIN-LEFT: 1.5in; DIRECTION: ltr; unicode-bidi: embed" type="disc"&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;COM&lt;/span&gt;&lt;/li&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;RPC&lt;/span&gt;&lt;/li&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Side by Side&lt;/span&gt;&lt;/li&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Older versions of CRT&lt;/span&gt;&lt;/li&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;Windows synchronization: Critical Section &amp;amp; Mutex&lt;/span&gt;&lt;/li&gt; &lt;li style="MARGIN-TOP: 0px; FONT-SIZE: 10pt; MARGIN-BOTTOM: 0px; VERTICAL-ALIGN: middle; mso-outline-level: 3"&gt;&lt;span style="FONT-FAMILY: Verdana"&gt;...&lt;/span&gt;&lt;/li&gt;&lt;/ul&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 1in; FONT-FAMILY: Verdana; mso-outline-level: 3"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;So how do you protect yourself? Starting with Windows XP/2003, operating system introduced FLS, Fiber local storage: &lt;a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/flssetvalue.asp"&gt;http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/flssetvalue.asp&lt;/a&gt;. It has exactly the same semantics as TLS. However, opposite from TLS, FLS is per a fiber and it does get switched during SwitchToFiber call.&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;This means that if your dll will be used in fiber mode, you might want to consider using FLS instead of TLS. If a thread is not converted to a fiber, FLS is still available and there is no much overhead in using it.&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;If you are running XP, extended stored procedure or dll that will be loaded into SQL Server, you must use FLS. Be careful though FLS is not available on Win2K. Applications that need to run on down level platforms have to use dynamic linking - leveraging GetProcAddress API to make sure that your dll loads correctly there. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;A bit of history:&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;FLS was finally introduced in Windows XP and 2003 because latter version of c-runtime made heavy usage of TLS for exception handling. This turned fiber mode completely unusable. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;How it works:&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in 0in 0in 0.5in; FONT-FAMILY: Verdana; mso-outline-level: 2"&gt;When a thread is created it doesn't have FLS set up. Windows uses lazy initialization for FLS. When FLSAlloc gets called for the first time, OS allocates FLS for this thread. Once thread is converted to fiber, FLS gets switched during SwitchToFiber call.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;You can use FLSCallBack to destroy FLS entries when a fiber/thread exists or FLS gets shrunk. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;I will be glad to hear your comments!&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=381649" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/SQL+Server+Scheduling/default.aspx">SQL Server Scheduling</category><category domain="http://blogs.msdn.com/slavao/archive/tags/SQLOS/default.aspx">SQLOS</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item><item><title>Be aware:  a new section in my blog</title><link>http://blogs.msdn.com/slavao/archive/2005/02/22/377837.aspx</link><pubDate>Tue, 22 Feb 2005 09:12:00 GMT</pubDate><guid isPermaLink="false">91d46819-8472-40ad-a661-2c78acb4018c:377837</guid><dc:creator>slavao</dc:creator><slash:comments>2</slash:comments><comments>http://blogs.msdn.com/slavao/comments/377837.aspx</comments><wfw:commentRss>http://blogs.msdn.com/slavao/commentrss.aspx?PostID=377837</wfw:commentRss><wfw:comment>http://blogs.msdn.com/slavao/rsscomments.aspx?PostID=377837</wfw:comment><description>Periodically, I bump into unusual behavior, features, of compiler, operating system, strange design and coding decisions made by someone else that makes me wonder. After giving it some thought, I often come to the conclusion that behavior or observed specifics are justifiable. However if I knew it upfront, It would have helped me to avoid unnecessary mistakes and complexity. &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Today I would like to open new section in my blog: Be Aware. In this section I would like to spend some time on this exact subject: strange behaviors and unexpected features. Sometimes these type of things are fascinating, sometimes just interesting and sometimes are just naïve.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;My goal is to point them out so that when you make your own decisions you will have more information to rely on.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp;&amp;nbsp; &lt;/span&gt;My goal is not to uncover bugs or start some kind of religious debates - I really don't like them. I strongly believe that problem drives a solution. What I mean by this is that something outrages in one place will be fully applicable in the other. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;So let's begin :-).&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;The topic of variable's placement have been covered to full extend by now. Some developers prefer to declare all variables upfront some in the scope where variables get actually used. Personally, I have heard cons and pros for both sides and kind of agree with both. I think there is no black and white. In some cases you do have to declare a variable inside of the given scope. Recently I have observed interesting debugger's behavior while debugging dumps. As all of you know debugger usually has difficulties to show local variables for retail, optimized build.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;This is&amp;nbsp;kind of expected in many cases. Before you can rely on a value, you have to confirm that the value&amp;nbsp;is really correct. In windbg I usually use dv command to output local variables&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&lt;span style="mso-spacerun: yes"&gt;&lt;/span&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;gt; dv -V&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Using the command with -V lists the actual placement of the variable on the stack.&lt;span style="mso-spacerun: yes"&gt;&amp;nbsp; &lt;/span&gt;What I noticed is that windbg wasn't displaying variables declared in local scopes at all even if instruction pointer was inside of the scope. It just doesn't list them! So I usually end up chasing those variables by disassembling the code and following stack manipulations. (Knowing original stack assignment in this cases would really helped) The process works 100% of the time, but I would have saved hours if the code declared variables in the beginning of the function.&amp;nbsp; At least&amp;nbsp;the debugger would&amp;nbsp;gave me a clue where the variable resides on the stack. &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;Probably the point here is that avoiding unnecessary complexity could enable tools to do better job and make everyone life easier. Remember tools, as any software, well tested for the common case. So next time when declaring a variable in an internal scope just stop for a second and think: Is it really necessary?&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;I have more stories around complexity but I will keep them until next time :-). &lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt; &lt;p style="FONT-SIZE: 10pt; MARGIN: 0in; FONT-FAMILY: Verdana; mso-outline-level: 1"&gt;&amp;nbsp;&lt;/p&gt;&lt;img src="http://blogs.msdn.com/aggbug.aspx?PostID=377837" width="1" height="1"&gt;</description><category domain="http://blogs.msdn.com/slavao/archive/tags/Coding+_2600_+Debugging+tips/default.aspx">Coding &amp; Debugging tips</category><category domain="http://blogs.msdn.com/slavao/archive/tags/Be+Aware/default.aspx">Be Aware</category></item></channel></rss>