Interim Update

In my head I’m trying to maintain a weekly posting schedule. I’ve got several posts in the work-in-progress category, each being incrementally updated until I’m ready to publish. The HTML5 work has taken a backseat while I practice my writing – a bit shameful really since I’ve been seeing some hits on my Getting started with HTML5 canvas page and I know I can do better.

Wrapping up Very Sleepy fork

I’m currently trying to package up a nice set of easy to build source for my fork of Very Sleepy. I’m inspired by Yuriy‘s  Sleepy-ex project so I want to prepackage all of the required wxWidgets dependencies to make life easier for downloaders. That’s taken a lot longer than finding somewhere to upload it for sharing and writing a blog post about it!

Converting to Visual Studio 2010

I’ve spent quite a bit of time already trying to summarise my recent experience of converting a suite of Visual Studio 2008 projects to Visual Studio 2010. It’s not big or clever, and the whole conversion system is poorly documented on the internet and I want to get it all written down before it all escapes me. It’s going to take a while thanks to a lot of the intricacies involved and will probably end up being my longest post – I typically aim for 1000-ish words, but 2000 seems easily possible and there’s a nerdy bit of me that wants to aim for 2010 words.

2011 Games

I want to continue writing up the games I played last year. The first part came out a lot longer while I was writing it, so I’ve broken it into parts that I aim to get out in the next few months.

Other stuff!

I’ve got a few more posts in the list, a few more blogs (it’s going to be hard to beat Old New Thing), some short tools posts about tools I can’t live without, and some follow-ups to some blog posts I’ve read.

Happy New Year

Pretty much every site on my Google Reader feed is doing their review of the year. Having just started blogging, I’m only at 9 posts and I’ve just managed to reach triple figures on views! (Well… I reached 100 on the 1st of January which I’m still pretty proud of).

Last few weeks

There’s been a couple of things on my mind for the last few weeks that have been holding up my posting.

  • Still reading Raymond Chen. My next Blog’s I’ve Read entry was going to be The Old New Thing blog from Raymond Chen. With a post every day from him for the last 9 years, I started at the beginning of 343 odd pages with 10 posts a page and have only about 60 left. The quality of the content I’ve found there makes me want to make that the next post for the category, but I want to get up to date first in case something exciting happens.
  • I’ve been considering the best way to post my updated version of Very Sleepy. After writing about my changes, I wanted to publish a version that people could use to see if it fixes their issues and if they like the more responsive interface. My first idea was to speak to Yuriy O’Donnell and piggy-back on his Sleepy-ex project on bitbucket.org. Thinking further (and after having spoken to Yuriy) I realized that it would be better to look into creating my own project for sharing. With Git and Mercurial both being something outside of my comfort zone, using a DVCS is something that I’ve only read about in depth without having actually used it yet. It feels a bit overkill to use a DVCS just to share some code and an exe.
  • My HTML5 work has stalled based on a loss of motivation. My work-in-progress game has been in-progress for the last few months. I’ve got to several points where I’ve had something showable as simple example of using some of the HTML5 features I depended on, but I wanted to take it to the point where I could show off a basic game. Of course, by saying game, I need to make sure I include all of the auxiliary bits that you expect with games, such as menus, score, timer etc. I’ve always been a big fan of the “find the fun” philosophy when creating games, but looking at my bodged together interface makes me want to add some more polish before testing it some more and looking for the fun.
  • Finding time to work on my JavaScript code. Actually getting to the code to write some more is the biggest challenge since dropbox isn’t the kind of thing you can install on a corporate network for giggles.
  • Wanting to update the blog. While travelling during the Christmas break, I left the laptop at home but still wanted to keep working on some posts. As this list of random things is proof, there’s been several ideas I’ve wanted to post about, but not in much depth. The problem is, when I have an idea, I want to note it somewhere. I did look at WordPress for iOS as a possibility, but then I was too busy to try it.
  • Mentally tossing up the value of Twitter. Again, with these random ideas I want to post about, I was thinking I could squeeze it down to 140 characters and dip my toe in the water. However, I realised that once I got started, most of my tweets would be either retweets of other’s work or me expressing anger at SouthEastern trains, delayed ferries and other tidbits that would interest few if any.
  • And then my iPhone broke. I managed to get a bent pin in the connector port. First problem, I only discovered this when I got home and wanted to charge it since the battery was nearly dead – the pin was stopping it from charging. Second problem, I hadn’t backed up since the iOS 5 update which was about 6 weeks out of date, and the phone wouldn’t talk to iTunes. I spoke to one of the Apple Geniuses at the Genius Bar and he suggested a wifi sync when I got home, hoping that I could get enough copied off in however long 17% of battery lasts. However, before I left work, one of my colleagues was able to apply finesse and fine copper wire to bend the pin back up so that a cable could be attached. That got me to 100% battery and leaving the cable in until I got home meant that I could connect to iTunes and backup. (For reference, wifi sync appears to need to be enabled in iTunes with the phone connected so would not have worked 😦 ) The next day I went back and paid for an out of warranty replacement phone, fortunately 1/4 of the price of a new one and installed it straight from the previous night’s backup.
  • Investigating SkyDrive. During a cloud discussion, we had a who’s got the biggest free storage contest and SkyDrive won with 25GB for starters whipping my iCloud‘s 5GB (personally I think that the iCloud storage should scale on the size of device and accumulate for multiple devices). I’ve been looking at how easy it is to get things in and out with something like SDExplorer and soon will go crazy by putting all my photos on there.

The rest of the year

Looking forward to the rest of the year, there’s several things I want to post about:

  • The first thing is a status update for my HTML5 stuff (or rather publishing that post-in-progress with a link to the work-in-progress). I’d then like to polish off what I was working on and write something more in-depth. WebGL is still sitting in the back of my mind, but I just can’t use it on the iPad yet.
  • A review of the games I enjoyed in 2011 – it’s been a great year for PS3 and I’ve played some really enjoyable games.
  • Looking at some upcoming games or writing about my experiences. I’m already looking forward to SSX and Borderlands 2 this year. Now if I could just finish off the other games in the queue.
  • More blogs for the Blogs I’ve Read category.
  • Maybe an iPhone app or two that I’ve enjoyed.

Very Sleepy Profiler

This post has been sitting in the backlog for  a while, but with the release of version 0.8 of Very Sleepy, now seems like as good a time as any.

Introduction

I can’t remember where I first found Very Sleepy (originally known as Sleepy). I’m guessing that I needed a profiler and thought I’d get results quicker with Google rather than trying to scavenge a VTune license code. Another possibility could be that I had already read Kayamon’s blog, found a post about the profiler and wanted to give it a go.

Getting started

The first thing I wanted to do was run one of our apps through it to get a starting point for optimization. Although the typical model was to profile an already in-flight application, I wanted to launch the application directly, run it for a bit and then stop it and review the results – to be honest, I can’t remember if it allowed you to do this or not. However I do remember that at the time, Very Sleepy would crash when the profiled app closed which would have been a kicker … except for the fact that a source package is available so you can build your own copy and fix it. A little compile, link and run and I could reproduce and then fix the crash. This meant I could send a mail to the author with my fix which he rolled into the next release. To me, that was the best example I had ever seen of the GPL in action. I’ve always understood how the GPL was supposed to work while still understanding the problems of trying to integrate GPL code with commercial projects – it’s good to see it in action! Looking back at the email conversation from that time, that was Very Sleepy 0.5 and that was two and a half years ago.

Getting better and faster

With Very Sleepy 0.6 a couple of months later, there were a couple of minor tweaks I wanted to make. The first was the launch dialog – I wanted to specify a working directory but at that point I could update the code to derive the working directory from the application path as a short term workaround.  The other thing I wanted was to profile the threads allocated after the application launched since we spawn worker threads once we’re running, something slightly more complex than I could achieve at the time, but there’s always the option to dig deeper into that if I wanted (or alternatively profile once running).

With Very Sleepy 0.7 came my first attempt to really use the profiler for a long run of one of our apps. I immediately found issues with the interactivity of Very Sleepy when inspecting profiles with a large number of samples. For me, the two main operations once a profile has been captured are:

  1. Randomly clicking on functions.
  2. Moving up and down callers and callees based on the percentage of calls between each.

My first step was to profile Very Sleepy using another instance of Very Sleepy – Inception style! Loading and inspecting my uber-profile allowed me to create another profile of the inner workings. The resulting profile was very interesting since  a lot of the hits within Very Sleepy outside of wxWidgets were in STL, more specifically * and -> operators for iterators as well as iterator destructors.

Looking at the disassembly, it was obvious that redundant STL calls were not being optimized out by the compiler – it was somewhat scary to see what the compiler hadn’t decided to inline those methods that you’d assume it would. I’m assuming one of the reasons for this was not defining those newer magical _SECURE_SCL constants that disable things like Checked Iterators that are already known to affect performance, and then I even started to wonder if _HAS_ITERATOR_DEBUGGING was affecting anything. The first thing to do was dereference the iterator early and then access the object via a reference rather than an iterator – this solved the * and -> operator calls in the disassembly. The other thing I did was to change

for(iterator it = container.begin();
    it != container.end();
    ++it)

loops into:

for(iterator it = container.begin(), end = container.end();
    it != end;
    ++it)

which avoids the destruction of the temporary result of the end() method. Those two fixes pretty much cleared all of the performance lag when randomly picking functions making that a fully interactive process, however, double clicking functions in caller and callee lists still stalled and required further investigation.

Digging deeper, I found that the main function view was being fully rebuilt after each selection was made when all that was actually required was selecting the row for the required function and making sure that it’s visible. With that change in place, the caller and callee lists immediately updated the main view when double clicked – success!

And now 0.8

With the recent release of 0.8,I was ready to grab the code, try the new features and integrate my optimizations. The first thing that happened when I tried running my own application was having to hunt down the working directory so I thought I’d tweak to start at the directory containing the application when providing a directory selector dialog – +1 for having the source.

Once it was running and capturing a profile from one of our applications everything seemed good. However once the capture was complete and gathered, it crashed! Since I’m building my own and starting with F5 in the debugger, it broke into the debugger with a failed buffer security check. Walking up the stack, there was an overflow in SymbolInfo::getProcForAddr() when calling SymFromAddrW() overwriting the stack. Ironically I’d recently been reading Raymond Chen’s Old New Thing blog and had only just read What(‘s) a character! about the issues that arise when going from chars to WCHARS when moving to Unicode and documentation that intermixes chars and bytes which lead to the SYMBOL_INFO page which made me realise there was an issue with the MaxNameLen value being calculated in bytes and not WCHARS. For reference you want to change:

symbol_info->MaxNameLen = sizeof(buffer) – sizeof(SYMBOL_INFOW) + 1;

to:

symbol_info->MaxNameLen =
    ((sizeof(buffer) – sizeof(SYMBOL_INFOW)) / sizeof(WCHAR)) + 1;

Along with this fix and another few UI tweaks, I thought it was worth another try at contacting the author to see if the optimizations could be added for a future version – for me they make the difference between a laggy obstruction and an interactive application. Plan B is to fork off a build like some others have done but I think it’s such a useful app that it’s good to have it in one place for those who want it.