<?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>2012-03-10T18:16:02Z</updated>
    
    <generator uri="http://www.sixapart.com/movabletype/">Movable Type Pro 5.12</generator>

<entry>
    <title>Visual Studio 11, the UI changes don&apos;t matter...</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2012/03/visual-studio-11-the-ui-changes-dont-matter.html" />
    <id>tag:www.lenholgate.com,2012:/blog//12.1173</id>

    <published>2012-03-09T21:50:40Z</published>
    <updated>2012-03-10T18:16:02Z</updated>

    <summary> The best thing about Visual Studio 11 is that it doesn&apos;t matter if you like the new style IDE or not. The project files are, at last, backwards compatible, so you can load them in Visual Studio 2010 and...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<div>
The best thing about Visual Studio 11 is that it doesn't matter if you like the new style IDE or not. The project files are, at last, backwards compatible, so you can load them in Visual Studio 2010 and build with the new tool chain even though you ignore the new IDE - if that's what you want to do.  
</div> 
<div><br /></div>
<div>
I don't <i>like</i> the new icons, but I find I can work fine in the IDE as long as I don't think about it too much... Probably pretty much like how I felt about all previous versions when they were at the beta stage...
</div> ]]>
        
    </content>
</entry>

<entry>
    <title>RFC 6455: The WebSocket protocol</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2011/12/rfc-6455-the-websocket-protocol.html" />
    <id>tag:www.lenholgate.com,2011:/blog//12.1161</id>

    <published>2011-12-14T08:46:47Z</published>
    <updated>2011-12-14T09:03:46Z</updated>

    <summary> I know I&apos;ve said this before, but now it&apos;s really done... The WebSocket protocol is now an official RFC. There are a small number of changes between RFC 6455 and the draft WebSocket protocol version 17; the only important...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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 know <a href="http://www.lenholgate.com/blog/2011/09/the-websocket-protocol-is-done.html">I've said this before</a>, but now it's really done...
</div>
<div><br/></div>
<div>
The WebSocket protocol is now an official RFC. There are a small number of changes between RFC 6455 and the draft WebSocket protocol version 17; the only important ones being he addition of two new close status codes. The rest is just a case of tidying up the draft.
</div>
<div><br/></div>
<div>
There will be a 6.5.3 release of <a href="http://www.serverframework.com/products---the-websockets-option.html" target="_blank">The Server Framework</a> to include these changes.
</div>]]>
        
    </content>
</entry>

<entry>
    <title>I guess this is a geek thing...</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2011/08/i-guess-this-is-a-geek-thing.html" />
    <id>tag:www.lenholgate.com,2011:/blog//12.1129</id>

    <published>2011-08-21T18:59:49Z</published>
    <updated>2011-08-21T19:05:25Z</updated>

    <summary>I&apos;m just back from a wonderfully relaxing holiday in Italy. This time we had internet connectivity all the time, so I&apos;m up to date on email, etc. The first thing that I do after all the &apos;we&apos;re back, how&apos;s the...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[I'm just back from a wonderfully relaxing holiday in Italy. This time we had internet connectivity all the time, so I'm up to date on email, etc. The first thing that I do after all the 'we're back, how's the house, and you need to go to bed even though you're&nbsp;excited&nbsp;to see all of the toys you've missed' stuff is to fire up all my machines and make sure that they do their windows update stuff... Then the NAS devices need to be started and allowed to settle in, then the VPN needs to be checked, dyndns kicked, etc. I expect my wife should really know to do the 'you need to go to bed even though you're exicited to see all of the toys you've missed' stuff on me too...]]>
        
    </content>
</entry>

<entry>
    <title>The WebSocket protocol, design by committee and requirements tracing</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2011/07/the-websocket-protocol-design-by-committee-and-requirements-tracing.html" />
    <id>tag:www.lenholgate.com,2011:/blog//12.1122</id>

    <published>2011-07-06T07:37:14Z</published>
    <updated>2011-07-08T15:33:12Z</updated>

    <summary><![CDATA[ I've been working with the WebSocket protocol recently, updating the code that implements the protocol in The&nbsp;Server&nbsp;Framework to the latest version of the draft standard (the HyBi 09 version). Whilst this looks like it's almost a real standard, there...]]></summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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've been working with the <a href="http://en.wikipedia.org/wiki/WebSockets" target="_blank">WebSocket protocol</a> recently, updating the code that implements the protocol in <a href="http://www.serverframework.com">The&nbsp;Server&nbsp;Framework</a> to the latest version of the draft standard (the <a href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-09" target="_blank">HyBi 09 version</a>). Whilst this looks like it's almost a real standard, there are still lots of potentially open issues as can be seen from the <a href="http://www.ietf.org/mail-archive/web/hybi/current/maillist.html" target="_blank">HyBi discussion mailing list</a>.
</div>
<div><br /></div>
<div>
It's quite clear from some of the less cohesive parts of the draft spec (and more so from the mailing list) that the protocol is very much a design by committee effort. This is both good and bad: It's good that the protocol includes support for a wide range of use cases and, well, it's bad that the protocol is more complex because it includes support for a wide range of use cases...
</div>
<div><br /></div>
<div>
It would be fascinating (and time consuming) to read through the discussion lists from the start and track the reasons behind the various changes and evolutionary steps that the protocol has gone through. Unfortunately, at present some of the elements of the protocol require you to do that if you want to understand why they exist. Without reading all of the discussion list there's a lack of traceability from the various elements of the protocol to the various use case requirements that caused them to be included. Given the evolution of the protocol it's likely that some areas of the protocol have become detached from the original requirements (as other areas have been modified and now no longer support the original requirements, for example, the idea that WebSockets is a message based protocol is undermined by defining a message in terms of an infinite number of fragments without any form of message length indication).
</div>
<div><br /></div>
<div>
All of this means that implementing the protocol from the current RFC is harder than it needs to be and often the wording of the RFC itself will lead you towards designs that don't work well with the actual protocol. Ideally all of these loose ends will get cleaned up before the standard is ratified but the protocol has been in draft form for a long time and I'm sure <a href="http://www.ietf.org/mail-archive/web/hybi/current/msg07647.html" target="_blank">some would prefer</a> it to be ratified as is rather than go through yet another round of changes... 
</div>
<div><br /></div>
<div>
I've been accumulating several rants about the protocol and I'll be presenting them as stand alone blog entries (mainly because they've each grown too large to be joined together in one entry). Often the solution to my frustrations would simply be a modification of the descriptive portions of the RFC text to give a little more explanation as to the thoughts behind the decisions.
</div>
<div><br /></div>
<div>
<ul>
<li><a href="http://www.lenholgate.com/blog/2011/07/websockets-is-a-stream-not-a-message-based-protocol.html">WebSockets is a stream, not a message based protocol...</a></li>
<li><a href="http://www.lenholgate.com/blog/2011/07/websockets---why-differentiate-between-text-and-binary-frames.html">Why differentiate between text and binary frames?</a></li>
<li><a href="http://www.lenholgate.com/blog/2011/07/websockets---why-do-we-need-to-mask-data-from-client-to-server.html">Why do we need to mask data from client to server?</a></li>
<li><a href="http://www.lenholgate.com/blog/2011/07/websockets---the-deflate-stream-extension-is-broken-and-badly-designed.html">How the deflate-stream extension is broken and badly designed</a></li>
<li><a href="http://www.lenholgate.com/blog/2011/07/websockets---i-miss-the-tcp-half-close.html">I miss the TCP half-close</a></li>
</ul></div>]]>
        
    </content>
</entry>

<entry>
    <title>GetExtendedTcpTable(), MIB_TCPTABLE_OWNER_PID and TIME_WAIT</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2011/02/getextendedtcptable-mib-tcptable-owner-pid-and-time-wait.html" />
    <id>tag:www.lenholgate.com,2011:/blog//12.1068</id>

    <published>2011-02-07T16:49:28Z</published>
    <updated>2011-06-23T10:46:25Z</updated>

    <summary> I have a client who is possibly suffering from TIME_WAIT accumulation issues and I thought that the best way to find out for sure was to get them to add the TIME_WAIT perfmon counter to their normal counter logs...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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 have a client who is possibly <a href="http://www.serverframework.com/asynchronousevents/2011/01/time-wait-and-its-design-implications-for-protocols-and-scalable-servers.html">suffering from <code>TIME_WAIT</code> accumulation issues</a> and I thought that the best way to find out for sure was to get them to add the <code>TIME_WAIT</code> perfmon counter to their normal counter logs so that we could see how and when sockets in <code>TIME_WAIT</code> accumulate on the machine.
</div>
<div><br /></div>
<div>
The problem is that <a href="http://serverfault.com/questions/232091/is-it-possible-to-monitor-the-number-of-tcp-connections-in-time-wait-using-perfmo" target="_blank">there doesn't seem to be a perfmon counter for this</a>, which is unfortunate, especially since you can easily get the number of <i>established</i> and <i>reset</i> connections from the TCPv4 and TCPv6 performance objects.
</div>
<div><br /></div>
<div>
Since all of this information is available by querying the TCP table with <code>GetTcpTable()</code> or <code>GetExtendedTcpTable()</code> I might put together a utility that polls the TCP table and publishes the a <code>TIME_WAIT</code> counter, however, before doing that I decided to hack up a simple example that polls the TCP table and simply dumps the number of connections in <code>TIME_WAIT</code> to the console. This is a bit like <code>netstat</code> and post processing the output.
</div>
<div><br /></div>
<div>
I got this working quite nicely and then thought it might be useful to switch to using <code>MIB_TCPTABLE_OWNER_PID</code> so that I could group the connections by process and so show which processes were causing the <code>TIME_WAIT</code> connections to accumulate. Unfortunately the <code>dwOwningPid</code> field is set to zero for connections in <code>TIME_WAIT</code>, which is somewhat understandable, as they can outlive the process that generated them, but unfortunate when they haven't...
</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>WinRM/WinRS job memory limits.</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/11/winrmwinrs-job-memory-limits-1.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.990</id>

    <published>2010-11-19T16:11:00Z</published>
    <updated>2011-11-24T12:15:27Z</updated>

    <summary>My tangential testing that began with my problems with commands run via WinRs during some distributed load testing are slowly unravelling back to the start. I now have a better build and test system for the server examples that ship...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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/000936.html">tangential testing</a> that began with my problems with <a href="http://www.lenholgate.com/archives/000935.html">commands run via WinRs</a> during some distributed load testing are slowly unravelling back to the start. I now have a better build and test system for the server examples that ship as part of <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework</a>. I have a test runner that runs the examples with memory limits to help spot memory leak bugs and a test runner that checks for lock inversions. My test scripts are also more flexible and I've found a couple of bugs.</p>

<p>Today I used a variation on my job object based memory limiting test runner to investigate the original problem with commands spawned remotely using WinRS. Given that I had a test runner that could spawn a process and place it into a job object it was simple to adjust this so that the target process was instead broken out of any job that the test runner might be in; just add <code>CREATE_BREAKAWAY_FROM_JOB</code> to the process creation flags and hope... This seemed to work so I then looked into reporting on the job that the test runner found itself in... Some simple calls to <code>IsProcessInJob()</code> and <code>QueryInformationJobObject()</code> with null jobs showed that the WinRS processes <i>were</i> being run inside of a job and that the limits (on my particular box) were as follows:</p>

<pre>Job report for process: 10676
Process IS in a job
Flags: 0x2b08 - 
   JOB_OBJECT_LIMIT_ACTIVE_PROCESS
   JOB_OBJECT_LIMIT_PROCESS_MEMORY
   JOB_OBJECT_LIMIT_JOB_MEMORY
   JOB_OBJECT_LIMIT_BREAKAWAY_OK
   JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE

<p>ActiveProcessLimit: 15
Affinity: 0x0
MaximumWorkingSetSize: 0
MinimumWorkingSetSize: 0
PerJobUserTimeLimit: 0
PerProcessUserTimeLimit: 0
PriorityClass: 32
SchedulingClass: 5
JobMemoryLimit: 157286400
ProcessMemoryLimit: 157286400</p></pre>

<p>The problem limits being the job and process memory limits. Luckily the WinRS job has <code>JOB_OBJECT_LIMIT_BREAKAWAY_OK</code> set so my attempt to break the target process free of these limits works fine.</p>

I still haven't found where any of this is documented, but at least I now don't have to write my own remote process spawning code.]]>
        
    </content>
</entry>

<entry>
    <title>WinRM/WinRS job memory limits?</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/11/winrmwinrs-job-memory-limits.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.987</id>

    <published>2010-11-04T22:46:24Z</published>
    <updated>2011-01-02T17:22:10Z</updated>

    <summary>I&apos;ve had one of those days. In fact I&apos;ve had one of those days for a couple of days this week... It started when I decided to improve the profiling that I was doing on a new memory allocator for...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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 had one of those days. In fact I've had one of those days for a couple of days this week...</p>

<p>It started when I decided to improve the profiling that I was doing on a new memory allocator for <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework</a> by extending the <a href="http://www.lenholgate.com/archives/000903.html">perfmon and WinRS based distributed server testing</a> that I wrote about a while back. This allows me to create a set of perfmon logs, start a server to collect data about and then start a remote client to stress the server. Some simple batch files make it extensible and repeatable and once I get a handle on manipulating the output data automatically it'll be a really useful system. </p>

<p>The allocator that I've been working on is a pooling, per thread, allocator that uses thread local storage to avoid locking and allows other threads to steal from your pools or give to your pools to deal with the fact that sometimes one thread does all the allocation and others do all the deallocation. It works pretty well but could do with some tuning and so I wanted to put it through its paces in a real server with repeatable testing.</p>

<p>I decided to use one of the <a href="http://www.serverframework.com/products---the-ssltls-using-openssl-option.html">OpenSSL server examples</a> as my test server since it did lots of stuff and put the allocator through its paces with a number of threads. The first problem was that I discovered a <a href="http://www.serverframework.com/asynchronousevents/2010/11/bug-in-openssl-stream-socket-filter-which-cases-memory-leaks.html">memory leak in the OpenSSL code</a>. This was located and fixed pretty quickly using the buffer reference tracking debugging code that I built into The&nbsp;Server&nbsp;Framework a while ago to help a customer. A couple of new <code>#define</code> values in <code>Config.h</code> and each of the leaked buffers produced a set of call stacks around each reference count change. The problem was introduced when I redesigned the filtering code for 6.2, it looks like none of the customers that are using this code has put the 6.2 update into production yet as I've had no bug reports on this yet.</p>

<p>The memory leak cost me half a day of profiling as it took me a while to realise that the reason the allocators were not performing how I'd expect was because many buffers were never being released back to the pool. Of course I initially blamed the new allocator for the problem... Once that was sorted I realised that I wasn't loading the server enough to stress the allocators and so I set about investigating why I was only getting a 100mb link from a 1Gb network card; loose cable would you believe! </p>

<p>Finally I adjusted my remote client to create 6000 connections sending data which should have used around 25% of the 1Gb bandwidth and which, I felt, was a good starting point for the stress test. The test ran fine when started manually from the client machine and failed randomly when run from WinRS remotely. At first I thought it was network related, which seemed unlikely but the test client was failing in strange ways and seeming to hang. After debugging the client I discovered that it was failing due to OpenSSL being unable to allocate memory during connection establishment and this was causing my wrapper code to throw an exception which left the internals of the client in a bit of a mess and which caused the client to hang during shutdown - it was waiting for sockets to be released that would never be released due to the exception leaving some references unaccounted for.</p>

<p>Once that problem was fixed (and that fix will find its way into 6.3.1 as well) I could finally run the client from WinRS and watch it fail cleanly when I stepped the number of connections from 2500 to 3000. The program was suffering from memory allocation failures - which it now dealt with correctly and shut down once the connections that had been established completed...</p>

<p>I did some googling and found nothing about any limits that <a href="http://msdn.microsoft.com/en-us/library/aa384426(VS.85).aspx">WinRM/WinRS</a> might impose on the processes that they run. <a href="http://serverfault.com/questions/198197/what-restrictions-does-winrm-winrs-place-on-the-processes-that-it-runs">I asked on ServerFault</a>, but as yet have had no replies. I stopped and played with my son and ate and watched TV...</p>

<p>Job objects, I thought... WinRM must be running the processes that it launches under a job object (as any <a href="http://www.lenholgate.com/archives/000762.html">sensible process launching process should</a>). Job objects can be set to limit the amount of memory that a process can commit. This would give the behaviour that I was seeing.</p>

<p>I haven't continued my investigation yet as I've shut down the machines in question for the night. I expect that <a href="http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx">ProcessExplorer</a> will show me that WinRM is using jobs. I expect a simple "allocate all memory" program will show me that when run using WinRS it fails at a number far far smaller than when run locally on the machine. If I'm lucky I'll find some documentation on how to change the limit in WinRM. If I'm not I guess I need to write a replacement.</p>

Still, the good news is that I've found and fixed a couple of bugs before my customers had problems with them. I've also decided that it would be quite handy to have a simple process launcher that could run a given process under a job with a specific memory limit so that I could check out how things behave in low memory situations and finally I could write another simple process launcher that uses jobs to monitor the maximum memory allocated during runs of my black box server tests, and this would help me catch future memory leaks during the testing that happens during my release process.]]>
        
    </content>
</entry>

<entry>
    <title>More thoughts on invasive/intrusive containers and STL allocators</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/08/more-thoughts-on-invasiveintrusive-containers-and-stl-allocators.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.976</id>

    <published>2010-08-05T15:09:54Z</published>
    <updated>2011-01-02T15:23:01Z</updated>

    <summary>I&apos;m still considering my options with regards to intrusive containers to replace the STL maps that I&apos;m using in my timer queue implementation. I think I may be able to use the boost::intrusive sets in place of a true map...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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 still considering my options with regards to intrusive containers to replace the STL maps that I'm using in my timer queue implementation. I think I may be able to use the <code>boost::intrusive</code> sets in place of a true map in most of the situations that I need (I just have convince myself that a) it will work with all the compilers that I need to support, b) that  adding a dependency to part of boost is a good idea for me and my clients and c) that even though my gut reaction on seeing the code is that it's pointlessly clever and bound to be a bitch to debug it's probably better than rolling my own). </p>

<p>I can't help but think that this whole issue would have never existed if the standard STL allocators had taken the value type as an argument to <code>allocate()</code>, this is likely not possible with things other than <code>std::set</code> and <code>std::map</code> BUT if these took the value type as a parameter then a custom allocator could simply locate the intrusive link structure in the value type and return that to the container. The same underlying map template could then work with either an allocator that allocates its node structure or an allocator that obtains the intrusive structure from the value type... Since it would have been this simple we'd have had the option of intrusive STL containers from day 0...</p>

It's very tempting to hack this kind of change into a copy of the map/set/tree templates used in STLPort rather than writing my own...]]>
        
    </content>
</entry>

<entry>
    <title>Useful Visual Studio retirement matrix</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/08/useful-visual-studio-retirement-matrix.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.974</id>

    <published>2010-08-05T07:26:53Z</published>
    <updated>2011-01-02T15:20:18Z</updated>

    <summary>Here&apos;s a useful matrix which shows when each version of Visual Studio will become unsupported by Microsoft. I&apos;m posting the link here as I&apos;m sure I&apos;ll not be able to find it the next time I need it......</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[Here's a <a href="http://support.microsoft.com/lifecycle/search/?sort=PN&amp;alpha=Visual+Studio">useful matrix</a> which shows when each version of Visual Studio will become unsupported by Microsoft. I'm posting the link here as I'm sure I'll not be able to find it the next time I need it...]]>
        
    </content>
</entry>

<entry>
    <title>Invasive containers</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/08/invasive-containers.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.972</id>

    <published>2010-08-04T08:28:08Z</published>
    <updated>2011-01-02T15:17:33Z</updated>

    <summary>Rather than immediately dive into the fun of writing my own invasive alternative for std::map I decided to take a look at what has been done before, as expected boost contains something that might work in the shape of the...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="C++ Tips" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>Rather than immediately dive into the fun of writing my own <a href="http://www.lenholgate.com/archives/000907.html">invasive alternative</a> for <code>std::map</code> I decided to take a look at what has been done before, as expected <a href="http://www.boost.org/">boost</a> contains something that might work in the shape of the "<a href="http://www.boost.org/doc/libs/1_43_0/doc/html/intrusive.html">intrusive containers library</a>". </p>

<p>Of course, being part of boost I first have to work out exactly how much more of boost it will require me to depend on and then I have to work out how I can use it to replace my current <code>std::map</code> usage. It seems quite clever (no surprise there) and allows for a type to be included in multiple intrusive containers by allowing an object to have multiple links embedded in it and allowing you to specify the link to use for a particular container. It seems slightly over engineered for my needs (again, no surprise there) and it's a pity that it doesn't simply provide obvious drop in replacements for the equivalent STL containers; yes it's nice that there are two kinds of base tree structure (red black and AVL) so you can optimise for your specific usage patterns but it would be nicer if you could simply switch from <code>std::map</code> to <code>boost::intrusive::map</code> and add an appropriate data member to the class you want to store in it... </p>

Still, I guess I should spend some more time learning and understanding... One thing that it doesn't do for me, as far as I can see, is provide a multimap from which I can remove all elements at a specific key in one go...]]>
        
    </content>
</entry>

<entry>
    <title>STL allocators, hmm...</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/08/stl-allocators-hmm.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.971</id>

    <published>2010-08-04T06:17:15Z</published>
    <updated>2011-01-02T15:17:20Z</updated>

    <summary>As I mentioned a while ago, I have some code which needs to perform better than it currently does and one of the areas that could be improved upon is the amount of contention for the heap that&apos;s occurring. The...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="C++ Tips" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[As I mentioned <a href="http://www.lenholgate.com/archives/000907.html">a while ago</a>, I have some code which needs to perform better than it currently does and one of the areas that could be improved upon is the amount of contention for the heap that's occurring. The fact that I'm using an STL <code>map</code> for my collection means that the class has a <a href="http://www.lenholgate.com/archives/000908.html">'big C'</a> contention value of <b>C(n threads using the heap)</b> rather than <b>C(n threads using the object)</b>. Of course, the fact that allocations need to be done at all is an unfortunate feature of <code>std::map</code> but rather than immediately replace the container with an invasive collection I decided to look into replacing the STL allocator that's being used with one that uses a private heap so  that I could reduce the contention value of the allocations to <b>C(n threads using the object)</b>. Doing this has required that I take a look at STL allocators and my initial thought is "hmm..."]]>
        <![CDATA[<p>STL containers can be configured to use custom allocators but by default they use a default allocator which, generally, uses the heap to allocate and free memory in a thread safe way. An allocator is a template parameter to the container and, as pointed out in several references, you don't generally need to mess with them. Still, my requirements are to improve performance and part of that job is to reduce potential contention so the allocators need to be looked at. <iframe align="right" src="http://rcm-uk.amazon.co.uk/e/cm?lt1=_blank&amp;bc1=000000&amp;IS2=1&amp;bg1=FFFFFF&amp;fc1=000000&amp;lc1=0000FF&amp;t=ramcom-21&amp;o=2&amp;p=8&amp;l=as1&amp;m=amazon&amp;f=ifr&amp;md=0M5A6TN3AXP2JHJBWT02&amp;asins=0201309564" style="width:120px;height:240px;" scrolling="no" marginwidth="0" marginheight="0" frameborder="0"></iframe>
My reference books didn't adequately cover allocators, though <a href="http://www.amazon.co.uk/gp/product/0201309564?ie=UTF8&amp;tag=ramcom-21&amp;linkCode=as2&amp;camp=1634&amp;creative=19450&amp;creativeASIN=0201309564">Generic Programming and the STL</a><img src="http://www.assoc-amazon.co.uk/e/ir?t=ramcom-21&amp;l=as2&amp;o=2&amp;a=0201309564" width="1" height="1" border="0" alt="" style="border:none !important; margin:0px !important;" /> did explain it all in a rather terse manner with lots of "you don't need to know this" provisos.</p>
 
<p>A quick Google led me to <a href="http://www.tantalon.com/pete.htm">Pete Isensee's</a> pages where he talks about STL allocators for games programming. He has a nice set of slides and some code and this has helped me get started. </p>

<p>I can't help thinking that allocators weren't really thought through especially well; the whole 'rebind' from <code>allocator&lt;int&gt;</code> to <code>allocator&lt;node&gt;</code> is, IMHO, just pointless 'aren't we clever with templates' crap ;) especially given the fact that at the end of the day all these things are doing is allocating memory of a given size... </p>

Anyway, being able to plug my own allocator into the containers is one small step on the path to improving the performance of the objects in question.]]>
    </content>
</entry>

<entry>
    <title>Tool lag</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/07/tool-lag.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.966</id>

    <published>2010-07-27T10:02:06Z</published>
    <updated>2011-01-02T13:45:36Z</updated>

    <summary>One of the problems of having a collection of tools that interoperate is that there&apos;s often a lag between when a tool will interoperate with the latest version of another tool. I&apos;m hardly a bleeding-edge tool junky, I wait until...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" scheme="http://www.sixapart.com/ns/types#category" />
    
        <category term="Rants" scheme="http://www.sixapart.com/ns/types#category" />
    
    
    <content type="html" xml:lang="en-us" xml:base="http://www.lenholgate.com/blog/">
        <![CDATA[<p>One of the problems of having a collection of tools that interoperate is that there's often a lag between when a tool will interoperate with the latest version of another tool. I'm hardly a bleeding-edge tool junky, I wait until RTM before I start using the latest Visual Studio on a daily basis, and in the case of VC 6 I stuck with it (as did most of my clients) until VS2005 came out and actually improved life for unmanaged C++ development... However, it seems that some tools take a long long time to catch up with Visual Studio. Take DevPartner Studio, for example, it seems that it's always lagging by just enough that I spend more time using it in my "legacy" compiler builds than I do in my day to day builds. It's cool that it works on x64 now but it's less cool that I have to use VS2005 or VS2008 to use it...</p>

<p>The problem with tools that lag is that no matter how useful they are the lag and the lack of daily use affects the usefulness of the tool and that always gets evaluated when I have to pay more money for another year of 'support'... </p>

<p>When a tool isn't in the tool box that you use daily you tend to use it less often than you should... Yes, the reason for this rant is that I've just found a couple of leaks with DevPartner that lived a little longer than they should because I'm developing the code in VS2010 and DevPartner isn't there in the toolbar nagging at me to use it. And, yes I know that I'm the one at fault for not using the tool anyway even though it's easier to ignore right now...</p>

<p>This kind of thing usually isn't actually too much of a problem for me as using these kinds of tools is on my release check-list but as I get clients that only want VS2010 builds it gets more likely that the tool lag will bite.</p>

So, in summary, a version of DevPartner Studio for Visual Studio 2010 would be nice and, even better, if you can't ship with the RTM of the compiler then a roadmap that gives a hint about when you might ship, or even a hand wavey indication of an intention to ship, would be nice...]]>
        
    </content>
</entry>

<entry>
    <title>Amusing bug in GetTempPath()</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/07/amusing-bug-in-gettemppath.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.964</id>

    <published>2010-07-22T09:14:12Z</published>
    <updated>2011-01-02T13:42:40Z</updated>

    <summary>Yesterday I had a bug report from a client who had a user that was getting an exception report from their software which simply said &quot;GetTempPath() - Failed to get temp path&quot;. Now as error messages go this isn&apos;t the...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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>Yesterday I had a bug report from a client who had a user that was getting an exception report from their software which simply said "GetTempPath() - Failed to get temp path". Now as error messages go this isn't the best but it comes from some code that has been in my tools libraries for around 10 years and which has never failed before, it has no tests, we're probably lucky that the message didn't just read "TODO" as I'm pretty sure that it's the first time that anyone has ever seen it apart from by reading my source code or running <a href="http://technet.microsoft.com/en-gb/sysinternals/bb897439.aspx">strings</a> on an exe that includes my source code... Anyway...</p>

<p>The offending code was a wrapper around the operating system function <code><a href="http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx">GetTempPath()</a></code>. I always tend to wrap these kinds of things so that I can convert errors to exceptions (I did a good job of that here didn't I) and so that I can convert types to the kind of types that I prefer to use. In this case I also dealt with the fact that you need to call the API without a buffer to determine how big a buffer it requires and then call it again with the correct sized buffer to obtain the path.</p>

<p><code><a href="http://msdn.microsoft.com/en-us/library/aa364992(VS.85).aspx">GetTempPath()</a></code> is a simple function and the documentation is pretty clear on how it should be used.  My original wrapper was fairly crap.</p>
<pre class="brush: cpp gutter: false">_tstring GetTempPath()
{
   const DWORD spaceRequired = ::GetTempPath(0, 0);      // 1
  
   _tstring directory;
  
   directory.resize(spaceRequired - 1);     // 2
  
   const DWORD spaceUsed = 1 + ::GetTempPath(
      spaceRequired,
      const_cast&lt;tchar*&gt;(directory.c_str()));    // 3
  
   if (spaceRequired != spaceUsed)   // 4
   {
      throw CException(_T("GetTempPath()"),
         _T("Failed to get temp path"));
   }
  
   return directory;
}
</pre>
<p>First I called the function to determine the required size, note that the required "size is the size of the buffer in <code>TCHAR</code>s" needed, which includes the terminating null character. Next I created a string that was the correct size (note that I'm removing 1 because the required size includes space for the null and I'm resizing the data area of the string and the null is added on for me. Thirdly I call the API again with my buffer. I then check that the second call uses all of the space that I allocated in the first call and fail if it doesn't.</p>

<p>There's quite a lot wrong with this code but mostly it will work... The check at stage 4 is overly restrictive but shouldn't pose a problem, except that it does... If your temp path contains an unnecessary double backslash; <code>C:\\temp</code> for example rather that <code>C:\temp</code> then the first call will return the length including the double backslash and the second call will return the path with the unnecessary backslash removed. This means that for the example path shown above, the second call returns <code>spaceRequired - 2</code> rather than the expected <code>spaceRequired - 1</code> and the code fails with the rather useless error message.</p>

<p>The fixed version of the code doesn't suffer from this problem.</p>
<pre class="brush: cpp gutter: false">_tstring GetTempPath()
{
   const DWORD spaceRequired = ::GetTempPath(0, 0);
  
   if (spaceRequired == 0)
   {
      const DWORD lastError = GetLastError();
  
      throw CWin32Exception(_T("GetTempPath()"), lastError);
   }
  
   _tstring directory;
  
   directory.resize(spaceRequired - 1);
  
   const DWORD spaceUsed = ::GetTempPath(
      spaceRequired,
      const_cast&lt;tchar*&gt;(directory.c_str()));
  
   if (spaceUsed == 0)
   {
      const DWORD lastError = GetLastError();
  
      throw CWin32Exception(_T("GetTempPath()"), lastError);
   }
  
   if (spaceUsed &gt;= spaceRequired)
   {
      throw CException(
         _T("GetTempPath()"), 
         _T("Failed to get temp path, second call needed more space ") +
         ToString(spaceUsed + 1) + 
         _T(" than first call allocated ") +
         ToString(spaceRequired));
   }
  
   directory.resize(spaceUsed);
  
   return directory;
}
</pre>
<p>Whilst there are still, no doubt, style issues, this version checks for errors in a better way (I'd first thought that the failure was some kind of permissions thing) and reports these errors in a clearer manner. Hopefully it will be another 10 years at least before I get another error report for this piece of code.</p>

This fix will be included in version 6.3 of <a href="http://www.serverframework.com/">The&nbsp;Server&nbsp;Framework</a> code which currently has no scheduled release date.]]>
        
    </content>
</entry>

<entry>
    <title>More thoughts on Big C</title>
    <link rel="alternate" type="text/html" href="http://www.lenholgate.com/blog/2010/07/more-thoughts-on-big-c.html" />
    <id>tag:www.socketframework.com,2010:/blog//12.960</id>

    <published>2010-07-17T06:21:21Z</published>
    <updated>2011-01-02T13:38:20Z</updated>

    <summary>I&apos;m finding that the thread contention notation that I made up the other day to help me talk about the performance implications of the changes I was making is pretty useful. The definition needs adjusting slightly though... For a given...</summary>
    <author>
        <name>Len</name>
        
    </author>
    
        <category term="Geek Speak" 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 finding that the thread contention notation that I made up <a href="http://www.lenholgate.com/archives/000907.html">the other day</a> to help me talk about the performance implications of the changes I was making is pretty useful. The definition needs adjusting slightly though...</p>

<p>For a given lock the worst case contention is <b>C</b>. For an operation involving a single lock where <b>t</b> threads exist during the lifetime of the process and <b>n</b> threads access the lock the contention for the lock can be expressed as being <b>C(n)</b>. This is simple enough and means that <b>C(1)</b> is no contention on a single lock as there's only one thread that can access it throughout the life of the process. <b>C(0)</b> implies no lock at all. </p>

<p>This gets a little more useful when you have multiple locks and you want to know the contention of a larger piece of code. Take this snippet, for example:</p>
<pre class="brush: cpp gutter: false">CCallbackTimerQueueBase::Handle CCallbackTimerQueueBase::CreateTimer()
{
   TimerData *pData = new TimerData();
  
   MarkTimerUnset(pData);
  
#if (JETBYTE_PERF_TIMER_QUEUE_MONITORING_DISABLED == 0)
  
   m_monitor.OnTimerCreated();
  
#endif
  
   return reinterpret_cast&lt;handle&gt;(pData);
}
  
void CCallbackTimerQueueBase::MarkTimerUnset(
   TimerData *pData)
{
   m_handleMap[pData] = TimerLocation(m_queue.end(), 0);
}</pre>A call to <code>CreateTimer()</code> has a contention factor of <b>C(t-new)+C(t-stl)+C(t-new)</b> and this representation clearly indicates that there are two locks involved and one is entered twice(the lock in <code>new</code> which is also used when the map calls its allocator, and the lock in this version of the STL for its debug iterator support). Since the locks are entered sequentially the contention factor can't be reduced in any way as all thread locks are locked and unlocked in sequence. And this is without the queue having any lock of its own. If we were to look at the thread-safe version then we have a contention factor of <b>C(t-queue)+C(t-new)+C(t-stl)+C(t-new)</b>.<br />
<pre class="brush: cpp gutter: false">CThreadedCallbackTimerQueue::Handle CThreadedCallbackTimerQueue::CreateTimer()
{
#if (JETBYTE_PERF_TIMER_QUEUE_MONITORING_DISABLED == 0)
  
   ICriticalSection::PotentialOwner lock(m_criticalSection);
  
   if (!lock.TryEnter())
   {
      m_monitor.OnTimerProcessingContention(IMonitorThreadedCallbackTimerQueue::CreateTimerContention);
  
      lock.Enter();
   }
  
#else 
  
   ICriticalSection::Owner lock(m_criticalSection);
  
#endif
  
   return m_spTimerQueue-&gt;CreateTimer();
}
</pre>
<p>However, since we hold the queue's lock for the entire period of the call to <code>CreateTimer()</code> it would be more accurate, I think, to represent the contention as <b>C(t-queue)+(C(t-new)+C(t-stl)+C(t-new))</b>, note the brackets. Also, once we have acquired the queue's lock we have <b>t-queue - 1</b> fewer threads in the sets of threads that are <b>t-new</b> and <b>t-stl</b>, thus it should probably be represented as <b>C(tq)+(C(tn-tq+1)+C(ts-tq+1)+C(tn-tq+1))</b>. If we were to use a custom allocator and a custom operator new which both worked on heaps that were private to each instance of the queue then we'd end up with <b>C(tq)+(C(1)+C(ts-tq+1)+C(1))</b> and we could remove the <b>C(1)</b>s as these represent a lock with no contention, giving us <b>C(tq)+(C(ts-tq+1))</b> if debug iterator support is enabled and <b>C(tq)</b> if it isn't.</p>

Obviously the hard part about all of this is actually seeing the locks, but hopefully that's where my <a href="http://www.lenholgate.com/archives/000643.html">deadlock detection tool</a> can help out. It already knows about all the locks and all the threads that access them and the location of the lock acquisitions and releases so it could calculate <b>C</b> for a given function and provide drill down where necessary...]]>
        
    </content>
</entry>

</feed>



