<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title>Rambling Comments</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/" />
    <link rel="self" type="application/atom+xml" href="http://www.lenholgate.com/blog/atom.xml" />
    <id>tag:www.lenholgate.com,2010-12-10:/blog//12</id>
    <updated>2011-07-07T09:23:10Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 5.12</generator>

<entry>
    <title>Another day, another deadlock avoided with no harm done</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2011/07/another-day-another-deadlock-avoided-with-no-harm-done.html" />
    <id>tag:www.lenholgate.com,2011:/blog//12.1127</id>

    <published>2011-07-07T08:43:50Z</published>
    <updated>2011-07-07T09:23:10Z</updated>

    <summary><![CDATA[ I'm currently building a new example server for The&nbsp;Server&nbsp;Framework. This is a variation on one of our proxy server examples for a client that's doing some WebSockets work. The idea is that the server takes an inbound WebSockets connection,...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<div>
I'm currently building a new example server for <a href="http://www.serverframework.com" target="_blank">The&nbsp;Server&nbsp;Framework</a>. This is a variation on one of our proxy server examples for a client that's doing some <a href="http://en.wikipedia.org/wiki/WebSockets" target="_blank">WebSockets</a> work. The idea is that the server takes an inbound WebSockets connection, creates an outbound TCP connection to the target server and routes data to and from the remote server and the WebSockets client. It's fairly simple stuff to put together once you're up to speed on <a href="http://www.serverframework.com" target="_blank">The&nbsp;Server&nbsp;Framework</a> but my client needed a helping hand and it's another nice example of what you can do with the framework.
</div>
<div><br /></div>
<div>
As I've mentioned before, all of these example servers run a set of black box integration tests during the build process. Once the code has compiled the tests kick off automatically, start the server, run a client against it, check the results are as expected, etc. If the test fails then the build fails. 
</div>
<div><br /></div>
This works great at flushing out any latent issues that the unit tests of all the framework code don't happen to catch. It's good for race condition problems and deadlocks and the like. The fact that the integration tests run on all of my build servers (which are a nice mix of power, memory and processors) means that race conditions and whatever get shaken out of the framework pretty quickly.
]]>
        <![CDATA[<div>
As I mentioned a while back, I've now got <a href="http://www.lenholgate.com/blog/2010/11/tangential-testing.html">memory leak analysis</a> and <a href="http://www.lenholgate.com/blog/2010/11/a-lock-inversion-detector-as-part-of-the-build-is-good.html">lock inversion detection</a> built into the integration test runners. The lock inversion detection can find and report on potential deadlocks without the code under test needing to actually deadlock. It detects that the code COULD deadlock and reports why. You can download the lock inversion detector that I use from <a href="http://www.lockexplorer.com/lock-inversion-detector-download-free.html" target="_blank">here</a> on my <a href="http://www.lockexplorer.com/" target="_blank">www.lockexplorer.com</a> site.
</div>
<div><br /></div>
<div>
Anyway, I set up the integration tests for my new server example this morning and ran it. The tests ran fine but <a href="http://www.lockexplorer.com/lock-inversion-detector-download-free.html" target="_blank">LID</a> reported lock inversions. Every lock inversion is a deadlock waiting to happen and although the tests had run through fine and the server hadn't deadlocked the report from LID was showing me that, given the right set of threading circumstances, it could...  
</div>
<div><br /></div>
<div>
By default I run LID during the integration tests as this runs slightly faster than <a href="http://www.lockexplorer.com/lock-inversion-analyser-buy-now.html" target="_blank">LIA</a> the full featured lock inversion analyser. A simple adjustment to the configuration of the integration test lets me run LIA instead and the report was quite clear as to where the lock inversions were.
</div>
<div>
<small>
<pre class="brush: text gutter: false">****************New Log*****************
Target process has terminated
Process "E:\OutboundGatewayServer\Output\VS2010\URelease\OutboundGatewayServer.exe" exit code: 0
Checking 3151 lock acquisition sequences for inversions

Lock inversions detected!

Lock inversion in acquisition of locks 164 and 41
Primary lock acquisition sequence: 164 @ 171, 41 @ 173
Executed on thread: 8 [6164] "IOPool"
Secondary lock acquisition sequence: 41 @ 154, 164 @ 178
Executed on threads: 5 [1064] "IOPool", 17 [1236] "IOPool", 16 [1288] "IOPool", 3 [2028] "IOPool", 13 [3652] "IOPool", 9 [4056] "IOPool", 12 [4676] "IOPool", 10 [4948] "IOPool", 7 [5444] "IOPool", 8 [6164] "IOPool", 4 [6232] "IOPool", 15 [6412] "IOPool", 11 [6704] "IOPool", 6 [6836] "IOPool", 14 [6876] "IOPool", 18 [7080] "IOPool"
Secondary lock acquisition sequence: 41 @ 154, 164 @ 187
Executed on threads: 5 [1064] "IOPool", 17 [1236] "IOPool", 16 [1288] "IOPool", 3 [2028] "IOPool", 13 [3652] "IOPool", 9 [4056] "IOPool", 12 [4676] "IOPool", 10 [4948] "IOPool", 7 [5444] "IOPool", 8 [6164] "IOPool", 4 [6232] "IOPool", 15 [6412] "IOPool", 11 [6704] "IOPool", 6 [6836] "IOPool", 14 [6876] "IOPool", 18 [7080] "IOPool"
Secondary lock acquisition sequence: 41 @ 154, 164 @ 194
Executed on thread: 12 [4676] "IOPool"
Location: 154
e:\jetbytetools\win32tools\criticalsection.cpp(80) : CCriticalSection::Enter
e:\jetbytetools\win32tools\icriticalsection.cpp(44) : ICriticalSection::Owner::Owner
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(260) : CProtocolHandler::OnDataReceived
e:\outboundgatewayserver\socketserver.cpp(206) : CSocketServer::OnReadCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanagerbase.cpp(128) : CStreamSocketConnectionManagerBase::OnReadCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(933) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::HandleOperation
e:\jetbytetools\sockettools\tasyncsocket.h(565) : TAsyncSocket&lt;ipoolablestreamsocket,istreamsocketconnectionmanager,istreamsocketcallback&gt;::HandleOperation
e:\jetbytetools\iotools\iopool.cpp(319) : CIOPool::WorkerThread::Run
e:\jetbytetools\win32tools\thread.cpp(241) : CThread::ThreadFunction
---
Location: 171
e:\jetbytetools\win32tools\criticalsection.cpp(80) : CCriticalSection::Enter
e:\jetbytetools\win32tools\icriticalsection.cpp(44) : ICriticalSection::Owner::Owner
e:\outboundgatewayserver\connection.cpp(128) : CConnection::OnConnectionEstablished
e:\outboundgatewayserver\socketserver.cpp(157) : CSocketServer::OnConnectionEstablished
e:\jetbytetools\sockettools\streamsocketconnectionmanagerbase.cpp(86) : CStreamSocketConnectionManagerBase::OnConnectionEstablished
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(1422) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::ConnectCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(981) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::HandleOperation
e:\jetbytetools\sockettools\tasyncsocket.h(565) : TAsyncSocket&lt;ipoolablestreamsocket,istreamsocketconnectionmanager,istreamsocketcallback&gt;::HandleOperation
e:\jetbytetools\iotools\iopool.cpp(319) : CIOPool::WorkerThread::Run
e:\jetbytetools\win32tools\thread.cpp(241) : CThread::ThreadFunction
---
Location: 173
e:\jetbytetools\win32tools\criticalsection.cpp(80) : CCriticalSection::Enter
e:\jetbytetools\win32tools\icriticalsection.cpp(44) : ICriticalSection::Owner::Owner
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(996) : CProtocolHandler::TryRead
e:\jetbytetools\websockettools\hybi08\websocket.cpp(166) : CWebSocket::TryRead
e:\jetbytetools\websockettools\hybi08\websocket.cpp(145) : CWebSocket::TryRead
e:\outboundgatewayserver\connection.cpp(143) : CConnection::OnConnectionEstablished
e:\outboundgatewayserver\socketserver.cpp(157) : CSocketServer::OnConnectionEstablished
e:\jetbytetools\sockettools\streamsocketconnectionmanagerbase.cpp(86) : CStreamSocketConnectionManagerBase::OnConnectionEstablished
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(1422) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::ConnectCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(981) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::HandleOperation
e:\jetbytetools\sockettools\tasyncsocket.h(565) : TAsyncSocket&lt;ipoolablestreamsocket,istreamsocketconnectionmanager,istreamsocketcallback&gt;::HandleOperation
e:\jetbytetools\iotools\iopool.cpp(319) : CIOPool::WorkerThread::Run
e:\jetbytetools\win32tools\thread.cpp(241) : CThread::ThreadFunction
---
Location: 178
e:\jetbytetools\win32tools\criticalsection.cpp(80) : CCriticalSection::Enter
e:\jetbytetools\win32tools\icriticalsection.cpp(44) : ICriticalSection::Owner::Owner
e:\outboundgatewayserver\connection.cpp(165) : CConnection::OnReadCompleted
e:\outboundgatewayserver\socketserver.cpp(322) : CSocketServer::OnDataFrame
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(917) : CProtocolHandler::HandleData
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(692) : CProtocolHandler::DispatchFrame
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(536) : CProtocolHandler::ProcessDataReceived
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(341) : CProtocolHandler::HandleDataReceived
e:\jetbytetools\websockettools\hybi08\protocolhandler.cpp(284) : CProtocolHandler::OnDataReceived
e:\outboundgatewayserver\socketserver.cpp(206) : CSocketServer::OnReadCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanagerbase.cpp(128) : CStreamSocketConnectionManagerBase::OnReadCompleted
e:\jetbytetools\sockettools\streamsocketconnectionmanager.h(933) : TStreamSocketConnectionManager&lt;cstreamsocketconnectionmanagerbase&gt;::HandleOperation
e:\jetbytetools\sockettools\tasyncsocket.h(565) : TAsyncSocket&lt;ipoolablestreamsocket,istreamsocketconnectionmanager,istreamsocketcallback&gt;::HandleOperation
e:\jetbytetools\iotools\iopool.cpp(319) : CIOPool::WorkerThread::Run
e:\jetbytetools\win32tools\thread.cpp(241) : CThread::ThreadFunction
---
</pre>
</small>
</div>
<div>
Diving into the code for the lock acquisitions in question shows me that in one of the objects concerned I'm holding the lock for longer than I need to. Reducing the scope of the lock means that I don't hold it across the use of the other object which removes the lock inversion. 2 minutes and one recompile from discovery to confirmed fix. A nice start to the day.
</div>]]>
    </content>
</entry>

<entry>
    <title>Lock inversion detector finally fully integrated in my build</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/11/lock-inversion-detector-finally-fully-integrated-in-my-build.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.991</id>

    <published>2010-11-23T09:25:00Z</published>
    <updated>2011-07-07T07:19:36Z</updated>

    <summary><![CDATA[After a week or so of serious dog fooding I've finally got a nice reliable lock inversion detector running as part of my build system for The&nbsp;Server&nbsp;Framework's example servers. Note: the deadlock detector mentioned in this blog post is now...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>After a week or so of serious <a href="http://en.wikipedia.org/wiki/Eating_your_own_dog_food">dog fooding</a> I've finally got a nice reliable lock inversion detector running as part of my build system for <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework's</a> <a href="http://www.serverframework.com/ServerFramework/latest/Docs/socketsamples.html">example servers</a>.</p>

<p><b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.</p>

<p>The build system has always run a set of 'black box' server tests for each server example as part of the build. These start up the example server in question, run a set of connections against it and shut the server down. This proves that the example servers actually work and has also proved a very useful technique for flushing out hard to reproduce bugs; generally race conditions. Since the build system runs so often and on so many different build machines the code gets run enough and on varied enough hardware to flush out these problems.</p>

<p>I've been aiming to get some form of <a href="http://www.lenholgate.com/archives/000643.html">deadlock testing</a> into this build system for a while. The original deadlock detector tool was too slow to run on the build servers and so I developed a cut down version and tuned the injection and monitoring engine. That gave me a detector that COULD run as part of the build system...</p>

<p>This last piece of work has involved <a href="http://www.lenholgate.com/archives/000937.html">integrating that detector</a> into the test scripts so that it gets run and can report failures in a way that will flag the build as failed if it detects any lock inversions. Of course it also detects full blown deadlocks but the lock inversion detection is much more powerful as the code under test need never actually be coaxed into deadlocking it just has to show the possibility of deadlocking for the tests to fail.</p>

<p>I've now run the new build system for the 70+ example servers and I've found one lock inversion in three places of the framework; the inversion was due to a pattern that I repeated for the <a href="http://www.serverframework.com/products---the-ssltls-using-openssl-option.html">OpenSSL</a>, <a href="http://www.serverframework.com/products---the-ssltls-using-schannel-option.html">SChannel</a> and <a href="http://www.serverframework.com/products---the-sspi-negotiate-option.html">SSPI</a> connectors and was easily fixed by removing the lock that was the cause of the inversion and instead using one lock rather than two. This clearly shows what I've known for a long time, object oriented programming and encapsulation in particular are often at odds with concurrency. It's obvious to make an object thread safe by giving it a lock that it can use to protect itself; it's often then more complex to ensure that these objects don't deadlock as they call into each other whilst holding locks that aren't evident at the call site. At present I consider it a post design optimisation to break encapsulation and, potentially, share locks across related objects. The lock inversion detector as part of the build makes it obvious when it's not just an optimisation but a requirement.</p>

The lock inversions mentioned above are removed in <a href="http://www.serverframework.com/asynchronousevents/2010/11/latest-release-of-the-server-framework-632.html">release 6.3.2 of The&nbsp;Server&nbsp;Framework which was released today</a>.]]>
        
    </content>
</entry>

<entry>
    <title>A lock inversion detector as part of the build is good</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/11/a-lock-inversion-detector-as-part-of-the-build-is-good.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.989</id>

    <published>2010-11-12T08:47:34Z</published>
    <updated>2011-07-07T07:19:47Z</updated>

    <summary>As I mentioned, I&apos;ve been adjusting my build system and have finally got to the point where my lock inversion detector is suitable to run on all of my example servers during their test phase on the build machines. I&apos;m...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Testing" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p><a href="http://www.lenholgate.com/archives/000936.html">As I mentioned</a>, I've been adjusting my build system and have finally got to the point where my lock inversion detector is suitable to run on all of my example servers during their test phase on the build machines. I'm working my way through the various example server's test scripts and adjusting them so that they use the lock inversion detector, can be easily configured to run the full blown deadlock detector and also can run the servers under the memory profiling test runner that I put together earlier in the week.</p>

<p><b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.</p>

<p>So far I've found 3 lock inversions that have never caused deadlocks in practice but which could, given the right circumstances and server design, cause problems. The 3 that I've found (and fixed) so far are in the async connectors used by the <a href="http://www.serverframework.com/products---the-ssltls-using-openssl-option.html">OpenSSL</a>, <a href="http://www.serverframework.com/products---the-ssltls-using-schannel-option.html">SChannel</a> and <a href="http://www.serverframework.com/products---the-sspi-negotiate-option.html">SSPI Negotiate</a> stream filters.</p>

So there will be a 6.3.2 next week but I'll delay it until the new build process is being used by all the example servers.]]>
        
    </content>
</entry>

<entry>
    <title>Tangential testing</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/11/tangential-testing.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.988</id>

    <published>2010-11-10T08:15:03Z</published>
    <updated>2011-07-07T08:57:46Z</updated>

    <summary>My theorising about the strange memory related failures that I was experiencing with my distributed testing using WinRS have led me to putting together a test runner that can limit the amount of memory available to a process and terminate...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Testing" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>My <a href="http://www.lenholgate.com/archives/000935.html">theorising about the strange memory related failures</a> that I was experiencing with my distributed testing using WinRS have led me to putting together a test runner that can limit the amount of memory available to a process and terminate it if it exceeds the expected amount. With this in place during my server test runs I can spot the kind of memory leak that slipped through the cracks of my testing and made it into release 6.2 of <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework</a>.</p>

<p>The idea is that rather than just starting the server we start the server using a monitoring process that first creates a job object for the server process and then starts the server process and assigns it to the job. The monitoring process can set some limits to the amount of memory that the processes in the job can allocate and it gets informed when they reach that limit. For the kind of testing I'm currently doing the monitor then simply terminates the server which causes the test to fail. You need to run the target process under the monitor without the memory limiter in place a few times and dump out the memory allocation stats to get an idea of the maximum memory that it will allocate during a normal run and from then on you can limit the memory allocation to that amount and be sure that the tests will fail if there's a leak like the one that made its way into 6.2.</p>

<p>Whilst I was adjusting the test scripts to test the new test runner I decided that it might be a good idea to merge this functionality into my "lock inversion detector" code. The <a href="http://www.lockexplorer.com/lock-inversion-detector-download-free.html"<lock inversion detector</a> is a cut down version of my <a href="http://www.lockexplorer.com/lock-inversion-analyser-buy-now.html">deadlock detection</a> and "lock explorer" tools. The lock inversion detector is considerably faster than the full lock explorer and the idea is that it is used in a similar way to the memory monitoring test runner. Test servers run under the lock inversion detector and if a release introduces the potential to deadlock via a lock inversion then the test will fail. Note that the code under test doesn't actually have to deadlock it just has to have the potential to... This is quite a powerful tool and it's helped my clients out on many occasions but it's still under development (I tend to use it, tweak it if need be to find the problem, and then move on with fixing the problem). Recent tweaks have made it run fast enough to become part of my build process.</p>

<p><b>Note:</b> the lock inversion detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.</p>

<p>If the lock inversion detector finds a lock inversion you need to run the full deadlock detector to get the information you need to fix the problem. This causes the target process to run a little slower than when under the lock inversion detector but the end result is a list of problem lock sequences with the threads concerned and call stacks showing each lock manipulation. With this information it's usually pretty trivial to find and remove the lock inversion. Again the target process doesn't need to actually deadlock for the deadlock detector to show you where it could deadlock.</p>

<p>I need to do some more work to integrate these tests into my build and release process but they're valuable additions. Ideally I'd like to merging the memory monitoring test runner functionality with the lock inversion detector, but for now I may simply run one set of tests with the memory monitoring and one with the lock inversion testing.</p>

These improvements to the build and release process will hopefully be in place for the release of 6.4.]]>
        
    </content>
</entry>

<entry>
    <title>Alternative call stack capturing</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2008/09/alternative-call-stack-capturing.html" />
    <id>tag:www.socketframework.com,2008:/blog//12.864</id>

    <published>2008-09-25T08:34:31Z</published>
    <updated>2011-06-23T10:09:54Z</updated>

    <summary>I&apos;ve just stumbled on these blog posts, by Maciej Sinilo, a game developer. He&apos;s written a memory allocation monitoring tool and mentions that using RtlCaptureStackBackTrace() is a faster (if undocumented) way to capture a call stack. This is interesting to...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I've just stumbled on <a href="http://msinilo.pl/blog/?cat=7">these blog posts</a>, by Maciej Sinilo, a game developer. He's written a memory allocation monitoring tool and <a href="http://msinilo.pl/blog/?p=40 ">mentions</a> that using <code>RtlCaptureStackBackTrace()</code> is a faster (if undocumented) way to capture a call stack. This is interesting to me as the call stack capture code in my debugging tools (<a href="http://www.lenholgate.com/archives/000643.html">deadlock detection</a>, timeshifter, <a href="http://www.lenholgate.com/archives/000648.html">tickshifter</a>, etc.) is pretty slow when using <code>StackWalk64()</code>. It's also interesting that he seems to store and sort stacks by CRC which is similar to what I do in my tools.  </p>

<p>This entry's really just so that I don't forget where I found the link...</p>

<p>Oh, and this <a href="http://www.gamasutra.com/view/feature/1430/monitoring_your_pcs_memory_usage_.php">link</a> is really useful to, though I've already been bitten by and fixed most of the issues that are mentioned.</p>

Updated: Link to the docs for <code>RtlCaptureStackBackTrace()</code> is <a href="http://msdn.microsoft.com/en-us/library/bb204633(VS.85).aspx">here</a>; thanks Barry.]]>
        
    </content>
</entry>

<entry>
    <title>Potential deadlock bug in free socket server framework</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2008/01/potential-deadlock-bug-in-free-socket-server-framework.html" />
    <id>tag:www.lenholgate.com,2008:/blog//12.806</id>

    <published>2008-01-12T13:19:05Z</published>
    <updated>2011-10-19T12:03:06Z</updated>

    <summary>The free version of the socket server framework contained code that could cause a deadlock during connection closure if you also have a lock in your derived class. There&apos;s a lock taken out in CSocketServer::Socket::IsValid() that isn&apos;t really required and...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Socket Servers" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>The <a href="http://www.serverframework.com/products---the-free-framework.html">free version of the socket server framework</a> contained code that could cause a deadlock during connection closure if you also have a lock in your derived class.</p>

<p>There's a lock taken out in <code>CSocketServer::Socket::IsValid()</code> that isn't really required and which can cause a deadlock if you have your own lock in your derived class which you lock in <code>OnConnectionReset()</code> or other server callbacks and which is also locked when you call into the framework via <code>Write()</code> or other calls. The deadlock is due to the lock sequencing; when calling into the framework your lock is locked and then the socket's lock is locked when <code>IsValid()</code> is called, when the framework's calling in to you it does so with the socket's lock locked and so your lock is acquired after the socket's lock. </p>

<p>The fix is to remove the lock acquisition in <code>IsValid()</code> so that the socket's lock is not locked when you call into the framework to <code>Write()</code> or to issue a <code>Read()</code>.</p>

This is now fixed and the updated source can be downloaded from <a href="http://www.serverframework.com/products---the-free-framework.html">here</a>.]]>
        
    </content>
</entry>

<entry>
    <title>CLR Hosting lifetime issues bite again...</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2007/11/clr-hosting-lifetime-issues-bite-again.html" />
    <id>tag:www.socketframework.com,2007:/blog//12.788</id>

    <published>2007-11-12T12:23:04Z</published>
    <updated>2011-10-19T12:06:07Z</updated>

    <summary><![CDATA[I'm looking into adding CLR deadlock detection into the CLR hosting code that's used inside The&nbsp;Server&nbsp;Framework and, once again, the fact that you can't cleanly shutdown the CLR host is causing me problems... Since the CLR can't be stopped by...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="CLR Hosting" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I'm looking into adding CLR deadlock detection into <a href="http://www.serverframework.com/products---the-clr-hosting-option.html">the CLR hosting code</a> that's used inside <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework</a> and, once again, the fact that you <a href="http://www.lenholgate.com/archives/000675.html">can't cleanly shutdown</a> the CLR host is causing me problems...</p>

<p>Since the CLR can't be stopped by a host without terminating the host process (and that's by design...) you need to be aware that any of the code that you have plugged into the CLR by way of the hosting interfaces can be called into during process shutdown. Since most of the code that you plug in will be in the form of COM objects you can rely on the COM reference counting to deal with cleaning up the objects (if the CLR could be relied on to release the references at any point...) but since you're now writing code that out-lives <code>main()</code> you have to be intimately aware of how this code uses other code that might rely on module level static variables, and other nastiness. Sure you wouldn't want to be relying on module level statics and the order of creation and destruction of those statics (which, as we know, is undefined across multiple modules) but if you're using the version of STL that ships with VS2005 in any of your hosting COM objects then you are... </p>

<p>My implementation if <code>IHostTaskManager</code> uses a <code>std::map</code> to keep track of its tasks. During process shutdown the CLR calls into my task manager to put a task to sleep, to do this I need to locate the task in my map and doing that relies on iterating the STL map and that, if <code>_HAS_ITERATOR_DEBUGGING</code> is enabled, seems to manipulate a global lock... Unfortunately the global lock has been destroyed by the time the CLR decides to shutdown ... Fortunately the global lock is kept alive by a reference counting class called <code>_Init_locks</code> which, although apparently undocumented, can be used to keep the locks that you need to use alive until the object that needs them is destroyed... So, by sticking an instance of <code>_Init_locks</code> into my task manager class I can make sure that it can iterate its collection after <code>main()</code> has returned and the process is shutting down...</p>

But it would be much nicer and easier if the <code>Stop()</code> method on <code>ICLRRuntimeHost</code> wasn't broken by design...]]>
        
    </content>
</entry>

<entry>
    <title>Deadlock detection tool design change for Vista?</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2006/07/deadlock-detection-tool-design-change-for-vista.html" />
    <id>tag:www.socketframework.com,2006:/blog//12.710</id>

    <published>2006-07-12T07:54:17Z</published>
    <updated>2011-07-07T07:21:00Z</updated>

    <summary>It seems that Vista contains lots of interesting new Win32 API calls and some of these provide built in support for deadlock detection... I guess my deadlock detection tool can operate differently on Vista then......</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[It seems that Vista contains lots of interesting new Win32 API calls and some of these provide <a href="http://www.bluebytesoftware.com/blog/PermaLink,guid,7a2e54d1-a829-42b7-8059-b3e030da35c1.aspx">built in support for deadlock detection</a>... I guess my <a href="http://www.lockexplorer.com" target="_blank">deadlock detection tool</a> can operate differently on Vista then...]]>
        
    </content>
</entry>

<entry>
    <title>Deadlock detection tool updates</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2006/04/deadlock-detection-tool-updates.html" />
    <id>tag:www.socketframework.com,2006:/blog//12.696</id>

    <published>2006-04-01T23:02:34Z</published>
    <updated>2011-07-07T07:21:43Z</updated>

    <summary>When I came back from skiing in Colorado I had a bug report from a client and it took me a fair while to isolate the problem for them. The report suggested that a server that I&apos;d built for them...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>When I came back from <a href="http://www.megeveski.com/archives/000273.html">skiing in Colorado</a> I had a bug report from a client and it took me a fair while to isolate the problem for them. The report suggested that a server that I'd built for them a while back was deadlocking in certain situations. The server had always had deadlock problems due to a couple of poor design decisions that I made early on and I'd built a <a href="http://www.lenholgate.com/archives/000489.html">tool</a> to help me remove the potential for deadlock from it by analysing its lock acquisition patterns - the code was too complex for me to do this analysis by just looking at the code and although I should rewrite the poorly designed section of code I don't have the time to do so (and it only causes problems when I make changes to the code, which doesn't happen very often, and the problems can be caught by running my tool). My tool monitors lock usage whilst the program is running and then reports on any deadlocks that are possible, even if they don't actually happen.</p>

<p>Anyway, this week I needed to run up the tool to track down this newly reported problem and I found that I didn't have a build that compiled...</p>

<b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.
]]>
        <![CDATA[<p>I'd spent a lot of time working on the deadlock detection tool and getting the engine working correctly. It's basically a custom debugger that uses API hooking (IAT patching) to do the lock monitoring. After getting the deadlock detection tool working nicely I decided to add a GUI to it (working nicely meant that it did the detection I wanted but the reports were a horrible mass of output rather than a nice clicky, drill-downable GUI). Whilst working on the GUI I decided that I needed to step away from the complexity of the deadlock detection tool and instead build the bulk of the GUI for a simpler tool; this allowed me to build various dialogs that I would need for the deadlock tool but also to explore the other issues that would come out of the desire to plug a GUI onto the debug engine.</p>

<p>At that time I had another problem that looked suited to a custom debugger with API hooking and I wrote a new tool to allow me to control the way an application <a href="http://www.lenholgate.com/archives/000576.html">viewed time</a>. Given the way I structure my code, most of it ends up being pushed into libraries for reuse, it was easy for me to build this and other tools from the same codebase. This new time shifting tool was considerably simpler and I slapped a simple GUI onto it. The GUI components were coming on but I still didn't have everything that I needed to put the GUI on the deadlock tool.</p>

<p>I found myself bogged down in the GUI and noticed that I was making less and less progress. I decided to put a halt to the GUI for a while and, instead, work on some of the meatier items from my list of things to do. Top of the list was making the debug engine work with .Net (CLR) processes. At this time my debug engine was simply using <code>CreateProcess()</code> to create a suspended process and then injecting a DLL and hooking the APIs that it wanted to hook and then resuming the process. This worked fine for "normal" exes and failed dismally with .Net exes. For some strange reason <code>CreateProcess()</code> appears to <a href="http://www.lenholgate.com/archives/000494.html">fail to suspend the correct threads</a> in a .Net process and a suspended process simply runs....</p>

<p>The solution was to switch to using the <a href="http://www.lenholgate.com/archives/000577.html">Win32 Debugging API</a> which gave me enough control over the process to be able to work out how to correctly start and safely suspend a .Net process. Once I was using the Win32 Debugging API I decided that I may as well cross another item off of my list by supporting multiple process debugging; my debug engine could now debug a process that started a child process and also debug the child process at the same time.</p>

<p>Then Christmas arrived and I was caught up finishing some work for a client, I integrated the new debug engine <a href="http://www.lenholgate.com/archives/000616.html">into the time shifter tool and updated the GUI</a>, then <a href="http://www.lenholgate.com/archives/000631.html">we skied some</a> and <a href="http://www.lenholgate.com/archives/000638.html">then my world ended</a>.</p>

<p>So there I was, not really in the mood to code and with a sudden need for a working version of the deadlock tool. Due to some sloppiness on my part, I didn't have one (I'd not bothered to tag the code of the last working build) and to get one either meant restoring a backup or knocking another item off of my list by updating the deadlock detection tool to work with the new debugger engine. I decided that I needed to get back into coding so opted for the later and spent two days getting my head back into the code and updating the deadlock detection tool to work in the same way as the time shifting tool, they both now use the new multi-process, CLR friendly, debug engine.</p>

<p>The tool found the problem by mistake. The actual bug that the client was reporting wasn't actually a deadlock but my thrash testing of their server under the deadlock detection tool managed to reproduce the problem and the tool captured some useful information that helped me to track it down.</p>

<p>The problem turned out to be a subtle race condition. The server is a gateway, it accepts incoming connections and routes them to an outbound connection. The race condition was that the "connector" object could be deleted due to a connection closure whilst it was still being used due to data flow. The solution was to reference count the connector so that although the connection closure would release a reference the data flow would still be holding a reference and so destruction would be delayed until the connector was no longer in use.</p>

<p>Locating the problem identified an issue with the deadlock detection tool. It uses masses of memory when running for a long period on processes that perform lots of manipulations on lots of locks. I'm collecting all of the data (in all of the formats) as the manipulation happens. This means storing some of the information multiple times - I track details about which threads do what to each lock, I track the order in which each thread acquires lock sequences, I track everything a particular thread does with any lock, I track all of the operations that occur on any thread in the order that they happen, etc. All of this is useful information but much of it could be worked out after the event. At present we could derive much of this information at the point that we want to create the report, all from a single list of operations... When displaying information on a GUI some of the stuff we currently store needs to be derived for display, but not all of it, and not all of the time, and even if it does then the data structures used for holding the data for display will probably duplicate much of what we're already collecting.</p>

<p>There was no real plan to how we'd collect the data. It just grew as I added more interesting reporting options. I'd decided not <a href="http://www.lenholgate.com/archives/000497.html">to summarise as I went along</a> as this restricted the ways that I could slice and dice the data but now I was doing the opposite and collecting all manner of pre sliced data... Now that I know I'm collecting too much I need to go back and revisit the data collection and work out what is the minimum that I require...</p>

But before I do that, I think I'll explore the changes required to support multiple process deadlock detection; that is, add support for named mutexes, etc. Looks like I'm getting my head back into coding again...]]>
    </content>
</entry>

<entry>
    <title>Interesting article on deadlock detection in DDJ this month</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/12/interesting-article-on-deadlock-detection-in-ddj-this-month.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.651</id>

    <published>2005-12-24T15:49:06Z</published>
    <updated>2011-07-07T07:22:37Z</updated>

    <summary>There&apos;s an interesting article by Tomer Abramson in this month&apos;s Dr Dobb&apos;s Journal about deadlock detection. He provides a compile in tool that works in a similar way to my deadlock detection tool and reports on potential deadlocks in code...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>There's an interesting article by Tomer Abramson in this month's <a href="http://www.ddj.com/documents/s=9938/ddj0601n/0601n.html">Dr Dobb's Journal</a> about deadlock detection. He provides a compile in tool that works in a similar way to <a href="http://www.lockexplorer.com" target="_blank">my deadlock detection tool</a> and reports on potential deadlocks in code even if they never occur during the program run in question.</p>

His architecture is considerably different to mine but the idea is the same. By recording the sequence of lock acquisition and release on all threads in the program you can then examine the sequence that locks are taken out and spot potential deadlocks.]]>
        
    </content>
</entry>

<entry>
    <title>More on locking</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/10/more-on-locking.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.596</id>

    <published>2005-10-07T07:11:28Z</published>
    <updated>2011-07-07T07:23:07Z</updated>

    <summary>Jeff Darcy over at &quot;Canned Platypus&quot; writes about &quot;How to add locking to a program&quot;. He laments the lack of a reasonably priced deadlock detection tool. I assume, from his backgrond, that he&apos;s interested in an Linux tool, so my...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>Jeff Darcy over at "<a href="http://pl.atyp.us/wordpress/">Canned Platypus</a>" writes about "<a href="http://pl.atyp.us/wordpress/?p=888">How to add locking to a program</a>". He laments the lack of a reasonably priced deadlock detection tool. I assume, from his backgrond, that he's interested in an Linux tool, so my deadlock detection tool wont help him much but it's good to know that it's not just me <a href="http://www.lenholgate.com/archives/000489.html">that thinks such a tool would be useful</a>...</p>

<p>Jeff links to some expensive static code analysis tools that do deadlock detection. <a href="http://www.lenholgate.com/archives/000493.html">My tool</a> doesn't rely on static code analysis, so you don't need to have source to run it. It can tell you that you have a potential deadlock present in the application without debug symbols or pdbs and it can <a href="http://www.lenholgate.com/archives/000531.html">show you where it is</a> if you have the symbols. Of course the advantage of static code analysis is that you don't have to run the code and you don't have to get the code to execute the code paths that could have problems. <a href="http://www.lenholgate.com/archives/000491.html">My deadlock detection tool</a> would, ideally, be run whilst you're running your normal "black box" application tests; and hopefully they exercise all your code paths!</p>

<b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.
]]>
        
    </content>
</entry>

<entry>
    <title>Walking the call stack</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/10/walking-the-call-stack.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.595</id>

    <published>2005-10-07T06:23:33Z</published>
    <updated>2011-07-07T07:25:59Z</updated>

    <summary>Ned Batchelder has written about the code he uses to get a call stack out of a windows program (thanks for the link Barry). I&apos;ve added a snippet of the code I use as a comment to his post. Note:...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p><a href="http://www.nedbatchelder.com/">Ned Batchelder</a> has <a href="http://www.nedbatchelder.com/blog/20051006T065335.html">written about the code</a> he uses to get a call stack out of a windows program (thanks for the link <a href="http://lapthorn.net/index.php?id=89">Barry</a>). I've added a snippet of the code I use as a <a href="http://www.nedbatchelder.com/reactor/comment.php?entryid=e20051006T065335">comment to his post</a>. </p>

<p><b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.</p>

I started looking into working with windows call stacks a while ago when I was working on my <a href="http://www.lockexplorer.com" target="_blank">deadlock detection tool</a>. What surprised me was how easy it was to get a call stack once you understood the <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/debug_help_library.asp">DebugHelp</a> API. There are lots of examples on the web of how to use the API, but my requirements were a little different to most as I wanted to collect the call stack in one place and decode it into stack frames somewhere else...]]>
        <![CDATA[<p>The <a href="http://www.lenholgate.com/archives/000493.html">tool</a> spots potential deadlocks in running code by looking at the order in which locks are taken out by different threads in the program. It also reports on <a href="http://www.lenholgate.com/archives/000491.html">concurrency issues and contention</a> and allows you to see exactly how the program under test uses its locks. It's a useful tool and one that has easily earned back its development time for me by allowing me to locate some deadlocks that hadn't yet deadlocked... These bugs were in the code, lurking, and with the aid of the tool I nailed them before they bit.</p>

<p>Most examples of using DebugHelp assume that you want to walk the stack using <a href="http://msdn.microsoft.com/library/default.asp?url=/library/en-us/debug/base/stackwalk64.asp">StackWalk64()</a> and decode the resulting <code>STACKFRAME64</code> structure there and then. I didn't want that, for many reasons, and so ended up using the <a href="http://www.lenholgate.com/archives/000176.html">web samples as a way of understanding the problem</a> before writing my own implementation that used the DebugHelp API in the way that I needed it. My code is more flexible than most of the samples that I found as it can grab the stack frames in one call and then process them and the corresponding pdb files at another time to produce the call stack data for later display.</p>

<p><a href="http://www.lenholgate.com/blog/assets_c/2010/12/CallStack-58.html" onclick="window.open('http://www.lenholgate.com/blog/assets_c/2010/12/CallStack-58.html','popup','width=1074,height=620,scrollbars=no,resizable=no,toolbar=no,directories=no,location=no,menubar=no,status=no,left=0,top=0'); return false"><img src="http://www.lenholgate.com/blog/assets_c/2010/12/CallStack-thumb-500x288-58.png" width="500" height="288" alt="CallStack.png" class="mt-image-none" style="" /></a></p>

<p>There's not a great deal of time between the grabbing and the decoding but there is some and the decoding occurs in a different process. As usual, for me at least, getting the display to work "just right" took longer than grabbing, processing and persisting the call stack data...</p>]]>
    </content>
</entry>

<entry>
    <title>Good stuff</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/09/good-stuff.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.584</id>

    <published>2005-09-24T09:06:16Z</published>
    <updated>2011-10-19T12:07:16Z</updated>

    <summary>I use BlogLines to read my RSS subscriptions. It&apos;s pretty good, and now that the performance issues I had initially seem to have gone away, I like it a lot. It&apos;s very handy to be able to read my feeds...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I use BlogLines to read my RSS subscriptions. It's pretty good, and now that the performance issues <a href="http://www.lenholgate.com/archives/000424.html">I had initially</a> seem to have gone away, I like it a lot. It's very handy to be able to read my feeds from anywhere and always have them up to date and synchronised. One of the features I like is the little "keep new" check box that each item has; check it and the item stays unread. I use this to keep the interesting stuff on top so that I can write about it later.</p>

This week I've been busy with my <a href="http://www.lenholgate.com/archives/000491.html">deadlock tool</a>; lots of coding, not a great deal of time to write long and considered blog entries. So, here's a list of some great blog postings that I tagged for later comment and never got around to commenting on.]]>
        <![CDATA[<p>The ordering is merely the order that they're stacked up in Bloglines...</p>

<p>1) <a href="http://www.journalhome.com/codecraft/11935/">Why project delivery bonuses suck</a> - from Kevin Barnes over at "<a href="http://www.journalhome.com/codecraft">Code Craft</a>". Kevin writes a lot of really good stuff and this one really hits the nail on the head. I've worked in a lot of investment banks over the years and they use these kinds of bonuses a lot; I've never seen a good outcome from their use, the project always gets twisted to achieve the date and the future usability and maintainability is always the thing that gets sacrificed. In the end they're giving bonuses to write shoddier software that will cost more to fix and extend in the future...</p>

<p>2) <a href="http://www.journalhome.com/codecraft/11785/">Three reasons to reinvent the wheel</a> - another excellent piece from Kevin Barnes. Sometimes there <i>are</i> good reasons to build it yourself from scratch.</p>

<p><iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;o=2&amp;p=8&amp;l=as1&amp;asins=0735619670&amp;fc1=000000&amp;=1&amp;lc1=0000ff&amp;bc1=000000&amp;lt1=_blank&amp;IS2=1&amp;bg1=ffffff&amp;f=ifr" style="width:120px;height:280px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>3) <a href="http://www.codinghorror.com/blog/archives/000396.html">In Defense of Verbosity</a> - Jeff Atwood at "<a href="http://www.codinghorror.com/blog/">Coding Horror</a>" on why saving keystokes whilst writing code is a fool's economy. As always Jeff's spot on with his analysis and backs up everything he says with references to Code Complete. Personally, I'm quite happy with the conciseness of <code>}</code> given my style of coding (I don't nest too deeply and I use lots of really small functions for stuff so it's always easy to know what the current <code>}</code> applies to; but his point applies to all aspects of coding. Write for the reader, not the compiler!</p>

<p>4) <a href="http://www.butunclebob.com/ArticleS.DavidChelimsky.ManageDependenciesNotAesthetics">Manage Dependencies not Aesthetics</a> - David Chelimsky over at "<a href="http://www.butunclebob.com/">But Uncle Bob .com</a>". A nice piece on design and the <a href="http://davidhayden.com/blog/dave/archive/2005/05/29/1066.aspx">Single Responsibility Principle</a>.</p>

<p>5) <a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/09/23/132398.aspx">Classic Technical Lead Blunder</a> - Jeremy D. Miller over at "<a href="http://codebetter.com/">CodeBetter.com</a>" (note that you can subscribe to Jeremy's feed directly rather than subscribing to the whole crowd at CodeBetter.com, a much better option IMHO). A good piece for all who are leading other coders; I've probably made all of these mistakes a couple of times. Some are easy to stop yourself from doing, others less so; the "Detailed work assignments" mistake is spot on and an easy mistake to make.</p>

<p>6) <a href="http://codebetter.com/blogs/jeremy.miller/archive/2005/09/20/132290.aspx">What's so great about Inversion of Control?</a> - Jeremy D. Miller again. Excellent piece on designing for testing which could be used to counter the next posting...</p>

<p>7) <a href="http://beust.com/weblog/archives/000319.html">Why unit tests are disappearing</a> - Cedric over at "<a href="http://beust.com/weblog/">Otaku, Cedric's Weblog</a>". Cedric disagrees with <a href="http://www.artima.com/weblogs/viewpost.jsp?thread=126923">Michael Feather's "rules"</a> on what constitutes a unit test. I disagree with Cedric ;) and feel that he's missing the key point of Michael's rules; <i>"Tests that do these things aren't bad. Often they are worth writing, and they can be written in a unit test harness. However, it is important to be able to separate them from true unit tests so that we can keep a set of tests that we can run fast whenever we make our changes."</i> If you try and stick to the rules then you'll end up breaking the file access, or database access, etc, out into quite a small and simple class that does just that. The rest of the system can deal with these classes via interfaces and can therefore be tested without using the database or file system but instead from a mock object. If you disregard the rules you're less likely to do this breaking apart, your classes violate the SRP and you end up mixing business object to file format type code in with file access code which makes it harder for you to switch to alternative persistence methods in the future. So, stick to Michael's rules and read 6) for a much better explanation of why.</p>

<p>8) <a href="http://sleeksoft.co.uk/public/techblog/articles/20050917_1.html">Ferrari versus Land Rover</a> - Mark Pearce (who lives near me, <a href="http://www.lenholgate.com/mt/mt-comments-4.cgi?entry_id=500">apparently</a>) over on "<a href="http://sleeksoft.co.uk/public/techblog/tech.html">Fighting the Plumbing since 1979</a>" (great name for a blog and not just because I spent the first 10 years of my working life doing just that... ;) ) - Although this piece is a response to my Assert is Evil posting from last week, and, whilst I've responded to that aspect of it there's more here that I feel I need to respond to. The whole Ferrari verses Land Rover idea and the suggestion that most C++ behaves like a Ferrari and most VB like a Land Rover... I think, it depends. Some kinds of program <i>can</i> safely act like a Land Rover and some <i>must</i> act like a Ferrari. Often the programs that can be like a Land Rover can be written in VB...</p>

<p><iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?t=ramcom-21&amp;o=2&amp;p=8&amp;l=as1&amp;asins=0321294319&amp;fc1=000000&amp;=1&amp;lc1=0000ff&amp;bc1=000000&amp;lt1=_blank&amp;IS2=1&amp;bg1=ffffff&amp;f=ifr" style="width:120px;height:280px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>9) <a href="http://www.sysinternals.com/blog/2005/09/multi-platform-images.html">Multi-platform Images</a> - Mark Russinovich over at "<a href="http://www.sysinternals.com/">SysInternals</a>". Mark shows us how he packages the SysInternals tools as a single executable file that can run natively on multiple platforms (32bit &amp; 64bit Windows) by embedding the 64bit executable inside the 32bit exe as a resource. I've embedded dll's in exes in the past, we used it for the simple downloadable driver stuff for my <a href="http://web.archive.org/web/20011205122441/http://www.webremote.co.uk/index.html">WebRemote Lego Mindstorms over Netmeeting code</a> (3 dlls embedded in a single exe which you downloaded and ran to unpack and register the dlls). The embedded driver technique that Mark mentions is mentioned in the <a href="http://www.lenholgate.com/archives/000502.html">RootKits</a> book that I'm currently reading. Oh, and don't miss the link to James Finnegan's article; well worth reading if you're starting out with driver development...</p>

<p><a href="http://twasink.net/blog/archives/2005/09/theres_no_feeli.html">10) There's no feeling like releasing software...</a> - Robert over at "<a href="http://twasink.net/blog/">Software is too expensive to build cheaply...</a>" A nice piece on releases and agility; I have to agree that you can tell how likely a project is to succeed by the "pulse" of its production releases. I've worked on systems where the release schedule is so slow that the team almost forgets that's what they're working towards and on projects where we <a href="http://www.lenholgate.com/archives/000220.html">released twice a week every week</a>. I know which one <a href="http://www.lenholgate.com/archives/000040.html">was better</a> and I know which one had the most <a href="http://www.lenholgate.com/archives/000076.html">polished release procedures</a> and dealt best with release problems, rollbacks and hotfixes. The less often you release the harder a release is because you're less used to doing them and you are less likely to have automated all the things you need to have automated. When you're releasing twice a week to a trading desk you make damn sure that everything that can be automated <i>is</i> automated and that you have lots of regression tests in place.</p>]]>
    </content>
</entry>

<entry>
    <title>In summary, don&apos;t summarise too soon</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/09/in-summary-dont-summarise-too-soon.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.566</id>

    <published>2005-09-09T21:54:40Z</published>
    <updated>2011-07-07T07:25:00Z</updated>

    <summary>I&apos;ve been working on my deadlock detection and lock monitoring tool quite a lot this week; that and fixing the issues that it&apos;s been highlighting. Yesterday I decided that I was drowning in data and that I really needed a...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I've been working on my <a href="http://www.lenholgate.com/archives/000493.html">deadlock detection and lock monitoring tool</a> quite a lot this week; that and fixing the issues that it's been highlighting. Yesterday I decided that I was drowning in data and that I really needed a GUI and, as I thought about how the GUI should work, I realised that I really didn't have <i>enough</i> data.</p>

<b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.
]]>
        <![CDATA[<p>My problem was that I was summarising the data as I sampled it. This made it easier to see the information that I wanted to see but made it hard to mix and match the views in the way that I'd like to if I had a nice GUI on top. By summarising at the sampling stage I was throwing away far too much information. Ideally the GUI will allow you to see all of the locks in your process, look at how your threads use the locks, display inappropriate lock sequences and potential deadlocks and allow you to drill down from anywhere to anywhere else. The summarised data that I was collecting earlier in the week made the last part impossible. I'd thrown away too much information whilst getting to a point where I knew the tool worked.</p>

<p>So, for the last day or so I've been adjusting my data structures so that I retain all of the information and only summarise at the last possible moment. This should mean that I can drill down much better in the GUI because I'll have all of the underlying information that I might wish to drill down into. Already the new data structures are showing promise; I can now drill into a lock contention situation and see what happened in both threads either side of the contention; might be useful, perhaps. I can also zoom out from an incident and look at what other threads were doing at around the same time...</p>

<p>I could have built the data structures this way from the start, I guess. But at the start I didn't know that what I was gathering was so valuable. To be honest, I didn't know that it would work at all. Now I know what I want and I know how to get it... Engage.</p>]]>
    </content>
</entry>

<entry>
    <title>Viewing lock lifetimes</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2005/09/viewing-lock-lifetimes.html" />
    <id>tag:www.socketframework.com,2005:/blog//12.562</id>

    <published>2005-09-08T07:34:02Z</published>
    <updated>2011-07-07T07:26:46Z</updated>

    <summary>I added some more monitoring to the deadlock tool. You can now view the entire life cycle of the locks, from initialisation, through usage to destruction. The lock usage figures put the contention figures in context as you can see...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Debugging Tools" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Lock Explorer" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>I added some more monitoring to the deadlock tool. You can now view the entire life cycle of the locks, from initialisation, through usage to destruction. The lock usage figures put the contention figures in context as you can see how often the lock was acquired by each thread and compare that to how often there was contention...</p>

<b>Note:</b> the deadlock detector mentioned in this blog post is now available for download from <a href="http://www.lockexplorer.com" target="_blank">www.lockexplorer.com</a>.]]>
        <![CDATA[<pre class="brush: text gutter: false">Lock usage for: 0x0012fbe8
Initialised
 by thread: 4500 @ 601
Location: 601
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 56 - Win32::CThreadedCallbackTimerQueue::CThreadedCallbackTimerQueue
MTSCSS - I:\MTSCSS\ServerMain.cpp: 302 - ServerMain
MTSCSS - I:\MTSCSS\ServerMain.cpp: 651 - wWinMain
MTSCSS - _wWinMainCRTStartup

Locked
 by thread: 2740 @ 603 41 times
 by thread: 4960 @ 691 17 times
 by thread: 5880 @ 772 13 times
 by thread: 6020 @ 932 13 times

Location: 603
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 152 - Win32::CThreadedCallbackTimerQueue::GetNextTimeout
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 133 - Win32::CThreadedCallbackTimerQueue::Run
MTSCSS - I:\JetByteTools\Win32Tools\Thread.cpp: 149 - Win32::CThread::ThreadFunction
MTSCSS - __beginthreadex

Location: 691
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 77 - Win32::CThreadedCallbackTimerQueue::CreateTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 102 - CProtocolHandler::CProtocolHandler
MTSCSS - Socket::CAsyncSocket::`vftable'
MTSCSS - I:\JetByteTools\SocketTools\AsyncSocket.cpp: 410 - Socket::CAsyncSocket::Read

Location: 772
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 100 - Win32::CThreadedCallbackTimerQueue::CancelTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 109 - CProtocolHandler::~CProtocolHandler
MTSCSS - CProtocolHandler::`vftable'
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 515 - CProtocolHandler::OnConnectionFailed

Location: 932
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 100 - Win32::CThreadedCallbackTimerQueue::CancelTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 179 - CProtocolHandler::OnDataReceived
MTSCSS - I:\MTSCSS\OutgoingSocketServer.cpp: 238 - COutgoingSocketServer::OnDataAvailable

Unlocked
 by thread: 2740 @ 604 41 times
 by thread: 4960 @ 692 17 times
 by thread: 5880 @ 773 13 times
 by thread: 6020 @ 933 13 times

Location: 604
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 104 - Win32::CCriticalSection::Owner::~Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 154 - Win32::CThreadedCallbackTimerQueue::GetNextTimeout
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 133 - Win32::CThreadedCallbackTimerQueue::Run
MTSCSS - I:\JetByteTools\Win32Tools\Thread.cpp: 149 - Win32::CThread::ThreadFunction
MTSCSS - __beginthreadex

Location: 692
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 104 - Win32::CCriticalSection::Owner::~Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 79 - Win32::CThreadedCallbackTimerQueue::CreateTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 102 - CProtocolHandler::CProtocolHandler
MTSCSS - Socket::CAsyncSocket::`vftable'
MTSCSS - I:\JetByteTools\SocketTools\AsyncSocket.cpp: 410 - Socket::CAsyncSocket::Read

Location: 773
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 104 - Win32::CCriticalSection::Owner::~Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 106 - Win32::CThreadedCallbackTimerQueue::CancelTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 109 - CProtocolHandler::~CProtocolHandler
MTSCSS - CProtocolHandler::`vftable'
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 515 - CProtocolHandler::OnConnectionFailed

Location: 933
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 104 - Win32::CCriticalSection::Owner::~Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 106 - Win32::CThreadedCallbackTimerQueue::CancelTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 179 - CProtocolHandler::OnDataReceived
MTSCSS - I:\MTSCSS\OutgoingSocketServer.cpp: 238 - COutgoingSocketServer::OnDataAvailable

Deleted
 by thread: 4500 @ 1026

Location: 1026
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 73 - Win32::CThreadedCallbackTimerQueue::~CThreadedCallbackTimerQueue
MTSCSS - I:\MTSCSS\ServerMain.cpp: 403 - ServerMain
MTSCSS - I:\MTSCSS\ServerMain.cpp: 651 - wWinMain
MTSCSS - _wWinMainCRTStartup

Lock contention for: 0x0012fbe8
Thread: 5880 blocked @ 774 by thread: 2740 @ 603 1 times

Location: 603
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 152 - Win32::CThreadedCallbackTimerQueue::GetNextTimeout
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 133 - Win32::CThreadedCallbackTimerQueue::Run
MTSCSS - I:\JetByteTools\Win32Tools\Thread.cpp: 149 - Win32::CThread::ThreadFunction
MTSCSS - __beginthreadex

Location: 774
MTSCSS - I:\JetByteTools\Win32Tools\CriticalSection.cpp: 99 - Win32::CCriticalSection::Owner::Owner
MTSCSS - I:\JetByteTools\Win32Tools\ThreadedCallbackTimerQueue.cpp: 112 - Win32::CThreadedCallbackTimerQueue::DestroyTimer
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 111 - CProtocolHandler::~CProtocolHandler
MTSCSS - CProtocolHandler::`vftable'
MTSCSS - I:\MTSCSS\ProtocolHandler.cpp: 515 - CProtocolHandler::OnConnectionFailed</pre>

<p>I also added per thread lock operation tracking, which allows you to see what each thread does to which locks, in which order. I'm starting to drown in data now and I think I'm reaching the point where a GUI is required...</p>

<p>One of the interesting things about the lock operation tracking is that I display the tick count when the operation took place. The idea was to display contention time for locks, ie how long a lock operation was blocked for, but unfortunately all this does is clearly illustrates that <code>GetTickCount()</code> isn't anywhere near accurate enough for this kind of thing; but I expected as much. As <a href="http://blogs.msdn.com/oldnewthing/default.aspx">Raymond Chen</a> points out <code>GetTickCount()</code> has high <a href="http://blogs.msdn.com/oldnewthing/archive/2005/09/02/459952.aspx">precision but low accuracy</a>. A link from Raymond's comments led me to the Nvidia developer site which has some timer testing code available <a href="http://developer.nvidia.com/object/timer_function_performance.html">here</a>, I think I'll take a look at the alternatives and see if I can get some results that are more useful...</p>]]>
    </content>
</entry>

</feed>



