c o d i n g f r o g s

croaking about programming, programming languages, software engineering, and the business of software

21Oct/100

How To Ruin Your Own Vacation

You know the feeling.  The feeling you get as you are heading home from work on the last day before your vacation.  That feeling of the weight and stress and pressure of delivering excellent software on time just floating away off your shoulders.  Nothing to look forward to for the next two weeks but fun and relaxation.

Right?

Or are you worried you are going to get a call?

It's the nature of the beast.  We plan to escape, but sometimes that call just can't be avoided.

Sometimes it can, though.  Sometimes you get called because you did something boneheaded, something you could have avoided that would have made your vacation more like, uh, a real vacation.

Some time ago I was working through a bug that was assigned to me at work.  After a bit of digging, I realized the bug was being caused because two methods in a related class had recently been deleted.  Putting those methods back in the class would fix my bug, but was that the right thing to do?

I assumed not.  I work with smart people, so I assumed that there was a good reason for removing those methods.  So I dug into the source code control system to determine which checkin had removed those methods, who had performed the checkin, and why.

I found the commit in question, noticed who had done it, and looked into the commit notes.  Here's essentially what I saw:

Description of problem:  Code was broken.  Description of fix:  Solved problem.

Meat Loaf says "two out of three ain't bad," but that doesn't apply in this case.  Note that our checkin system automatically adds the "Description of problem" and "Description of fix" tags so the commit notes weren't nearly so verbose as it appears.

Obviously this was of no help to me in determining why the change was made, and without a reference to a bug database record I couldn't look up the bug to find out about it either.  So even though the developer in question was on vacation that day, I had to call him:  I couldn't wait for him to get back for the answer.

So, there you have it:  One way to ruin your own vacation is to avoid taking any time at all to provide useful commit notes.

Notice that even typing the notes that were put into this commit was an utter waste of time.  I don't know about you, but I don't make a habit of checking in fixes to code that isn't broken.  So the statement "Code was broken" is a statement of the obvious.  Of course the code is broken; why else would you be checking in a fix?  Saying the code was broken is redundant and unnecessary.  Likewise for "Solved problem"; why would you be checking in if you didn't solve the problem?

The single most effective change that could be made here would be to simply reference the bug database record.  Even if the problem description were simply changed to say "Bug #3165150" that would be enough.  Annoying, but enough.  With that I could at least look up the bug in the bug database and maybe figure out why the change was required.  But seriously, how long does it really take to add a few notes about what changed and, more importantly, why?

I've also seen commit notes like this:

- removed GetLastWidgetStatus() method

- modified signature for UpdateWidget()

- added null check to SyncWidget()

Gosh, really?  You needed to tell me that?  I thought that's what diff tools were for.

Here's my suggestion for creating Awesome and Useful commit notes:

  • Include a description of the problem.  Use the title from the bug record as a guideline.  You might want to be a little more verbose, but often it isn't necessary, as long as you...
  • Include a reference to the bug record.  A clickable link is best, if possible; if not, at least a record ID so the bug can easily be found.
    • At this point some of you may be saying, "What if there is no associated bug record?"  In that case, the answer is, "WHY ON EARTH ARE YOU DOING WORK WITHOUT AN ASSOCIATED BUG RECORD?!?"
      • I mean, seriously.  If for no other reason, you need a record so your boss knows what you are doing!
    • Horrible Example:  "Code was broken."
    • Bad Example:  "WidgetManager class not working."
    • Good Example"  "WidgetManager not correctly updating widgets on DisplayChanged event; see bug #5150316."
  • Include a description of why you made the changes you made, not just THAT you made changes or a simple enumeration of the changes.
    • Horrible Example:  "Fixed code."
    • Bad Example:  "removed GetLastWidgetStatus(), modified signature for UpdateWidget(), added null check to SyncWidget()"
    • Good Example:  "The UpdateWidget() method had no knowledge of the event causing the call so it wasn't handling the DisplayChanged event properly; I added a parameter to UpdateWidget() to provide this information which allows us to handle the event correctly.  Also added a null check to SyncWidget() which would have caught the bug.  Removed GetLastWidgetStatus(); nobody is calling it anymore."

See?  That's not so bad.  Just a few minutes out of your life to show your love and caring to your team members when you are gone.  Just a few minutes out of your life so you can really enjoy that vacation without worrying whether the phone is going to ring.

12Aug/100

Ruby Time

I figure it is time to start learning how to program in Ruby.  It appears that this Ruby fad is not going away.

(That was a joke, Ruby people.)

I'm reading Geoff Moore's "Crossing the Chasm" now, and as a result I seem to be applying that metaphor to just about everything.  Oddly, I find that I'm more in the early-to-late majority camp with a lot of things, even in technology (for example, I still do not own a smart phone - gasp!).  Is this a concern?  Does it mean I am not sufficiently passionate?  Or does it just mean that I prefer to focus my efforts on learning stuff that will be meaningful?

I don't know, but likely this is why I am still not a Ruby programmer.  At any rate, web application development is not exactly a strong point with me, and I figure if I'm going to shore up that part of the fortress, or whatever, then perhaps Ruby is the way to go.

Sadly, this probably does not bode well for nearlyfreespeech.net, my current hosting provider.  I've been happy as a clam with nearlyfreespeech.net, but they only provide PHP.  That's fine for running a WordPress blog like this one or Seeping Matter (my personal blog) for now, but if I start programming in Ruby I'll end up choosing another provider, and I'm likely to only want one.

I'm looking at a few different hosting providers.  I'm open to other suggestions or comments about how these choices are great or not great.  Current short list includes:

  • WebFaction - $8.50/month
  • HostingRails - $8/month
  • RailsPlayground - $9/month

All of them support Ruby/Rails, PHP, and Python; all offer SSH access; all provide Subversion; all offer nightly backups.  HostingRails also offers pay-as-you-go pricing like NFSN so that's a bit of a plus.

Stay tuned.

8Apr/100

Hard Coded Breakpoints

This little line of code totally saved me this week:

__asm int 3;

I'm writing a plugin to an existing Windows application and I could not, for the life of me, figure out if my plugin was even being loaded or if it was executing properly.  All I knew was that I wasn't seeing the behavior from my plugin I expected.  I needed to step through my plugin with a debugger, but since it loads as part of another app I didn't know how to get the debugger to break.  Setting breakpoints in Visual Studio wasn't working.

Fortunately for me I have smart teammates, and one of them taught me this little trick to programmatically insert a hard coded breakpoint into your code.  If I add this line, when my code runs the breakpoint is triggered and the application stops executing, with a dialog giving me the option to debug it in Visual Studio.  When I get in there, behold:  The cursor is pointing at that line of code and I can step through my plugin.

Since then I've done a bit more research and found out that, in truth,

__asm int 3;

is really only meant for x86 architectures.  A more portable way to do this for Windows is:

__debugbreak();

which invokes the correct assembly interrupt for the current architecture.

On Linux, apparently you can do this:

__asm__("int $0x03");

although I haven't tried it. It looks enough like the x86-only version for Windows that it makes me wonder if it would work on x64 architectures; I'd love to hear back if anyone knows for sure.

Also, interested to know how to do this on OS X. Comment or mail if you know.

7Mar/100

Even Simpler WordPress Administration on NFSN

I'm a fan of both WordPress and NearlyFreeSpeech.net, which you may have already figured out if you've done a little digging.  This blog is hosted on NFSN using WordPress, as is my personal blog.

Installing WordPress is a fairly straightforward adventure, but this script makes it a little bit easier:

wget http://wordpress.org/latest.tar.gz
tar xvzf latest.tar.gz
cp -R wordpress/* .
rm -rf wordpress

head -44 wp-config-sample.php > wp-config.php
wget http://api.wordpress.org/secret-key/1.1/
cat index.html >> wp-config.php
rm index.html
tail -28 wp-config-sample.php >> wp-config.php

echo "" >> .htaccess
echo "RewriteEngine On" >> .htaccess
echo "RewriteBase /" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-f" >> .htaccess
echo "RewriteCond %{REQUEST_FILENAME} !-d" >> .htaccess
echo "RewriteRule . /index.php [L]" >> .htaccess
echo "" >> .htaccess

Run this script directly in your document root. You can get a copy of it here.  It grabs the latest version of WordPress and unpacks it, updates your configuration with your own set of unique WordPress keys, and updates your .htaccess file to support the pathname URLs used by WordPress.  Now all you have to do to set up WordPress is set up a database and provide wp-config.php with your database info and you should be good to go.

WordPress updates regularly, which is good news because it means the project is active.  The downside is that this also means upgrading the installation frequently.

To help deal with this I also wrote an upgrade script:

if [ -d "backup" ]
then
        echo "Delete backup directory first."
        exit
fi

mkdir backup
cp -R index.php license.txt readme.html xmlrpc.php wp-* backup

wget http://wordpress.org/latest.tar.gz
tar xvzf latest.tar.gz
cp -R wordpress/* .
rm -rf wordpress

This upgrade is pretty simple but keeps your site intact, including any themes or other content you might have added.  This script is also available here.

I tried this today setting up a new blog as well as upgrading this blog, and the scripts seem to work fine.  Any feedback is welcome.

12Nov/090

Go – Programming Language Nirvana?

The Go Gopher

The Go Gopher

Earlier this week Google announced their new programming language, called Go.

Usually I don't get too worked up about programming languages.  I already feel like I know more languages than I should need to know, and often a new language seems to me like "Hey, check this out!  Here's a more complex or non-intuitive way to accomplish a task you already know how to accomplish, but in a language you will never use professionally!"

I know, I'm disappointing you.

But Go!  Oh my, this seems different.  From the blog post:

Go combines the development speed of working in a dynamic language like Python with the performance and safety of a compiled language like C or C++. Typical...

Wait!  Hold on there — my heart just skipped a beat and it freaked me out.  Can you please repeat that again?

...a dynamic language like Python with the performance and safety of a compiled language like C or C++.

Ooh baby.  Someone help me — I'm shaking with anticipation.  A systems programming language that combines Python and C++?  Can it really be?  It seems too good to be true!  My two favorite programming languages combined in one:  It's like true programming nirvana!

23Jun/091

Versioning Containers and Iterators

Uh, this is a programming post.  Just so you know.

The Problem

Okay, so let's start this discussion with a simple linked list that contains the ingredients for Bill Cosby's Chocolate Cake for Breakfast:

Fig. 1 - Linked list of ingredients for Chocolate Cake for Breakfast

Fig. 1 - Linked list of ingredients for Chocolate Cake for Breakfast

Normally we'd use a linked list in programming when we know we may have to arbitrarily insert items later on.  For example, I just realized that this cake does not contain chocolate, so I should be able to insert that into the list by shuffling a little bit:

Fig. 2 - Chocolate Cake for Breakfast, with Chocolate Added

Fig. 2 - Chocolate Cake for Breakfast, with Chocolate Added

This seems like a much tastier recipe.  But there can be a slight problem with this.

Suppose the linked list is serving as a model for your recipe view.  You have a UI that accessess the model and then displays the results in some sort of view that you can see on your laptop in the kitchen.  Suppose further that you decide to go even crazier and not just stop with adding chocolate, but you are also going to add sugar and frosting.  INSANE!

This is not a problem yet.  Likely your UI offers the ability to add new ingredients through the UI, so you just add them and the model gets updated, and when your view refreshes you see the new list:

Fig. 3 - Chocolate Cake for Breakfast - Dessert Style!

Fig. 3 - Chocolate Cake for Breakfast - Dessert Style!

That sounds like a great chocolate cake.  But now suppose Mr. Cosby is on stage in Winnemucca doing his Chocolate Cake for Breakfast routine, and for some reason that nobody can understand the teleprompter that reminds him of the routine is using your data model to render the view - right at the exact moment in time that you are changing the recipe.

Uh-oh.

I've run into this problem a number of times in my software development career, where I have a data structure that is being accessed by more than one user at the same time.  In our case, we have one read-only accessor and one read/write accessor.  At first glance it doesn't seem like this should be a problem.  But unfortunately, even though the reader isn't making changes to the model, the reader still depends on the model having a constant state.

There's a couple of fairly simple ways to address this problem.

First, we can make a copy of the model for each user.  We can have a resource that provides a copy when a user requests the copy.  Then that user can do whatever they want with the model - read or write.  When they are done, they can either discard their copy, or turn it back to the resource for the changes to be merged into the whole.

This might work for some implementations.  In our trivial example here it would probably be just fine.  But if I have a data structure with two million 50-byte records, this isn't such a great idea.  It isn't just that I don't want to make a copy of a 100 Mbyte (okay, 95 for you technicality folk) data structure - I don't want to double my memory usage from 100 Mbytes to 200 Mbytes (or 95 to 190, sheesh).  So making a copy won't always work.

Another option is to lock the structure.  Using some sort of a mutual exclusion primitive I can lock the data structure so that only one user can access it at a time.  I can't just hold the lock per-access, though - I have to maintain sessions and locks for the entire session.  In other words, I have to make sure that nobody can change the recipe at all throughout the duration of Mr. Cosby's act.  This high level of granularity in the locking of the data structure makes it hard to use.  And it is particularly frustrating for read-only users who are thinking, "Why can't I access the structure?  All I want to know is what is in it!"

Versioning

One way I've successfully solved this problem in production code is to use a versioned container and versioned iterators for that container.  I used these at Volera, for example, for some reason that doesn't matter now, because Volera is DEAD.  Anyway, here's how they work:

Versioned Containers act pretty much like regular containers - vectors, lists, hashtables, etc.  The difference is that each link from one element in the container to the next has an associated version number on the link.  Any given element can have a number of "next" links - even a singly linked list, like in our example.  Each link would have a different version number.  The container itself also has a version number, which corresponds to the highest numbered link in the container.

Versioned Iterators go along with the versioned container.  When you request an iterator from a container, it gives the iterator a version number which corresponds to the current version number of the container at the time of request.  The user requesting the iterator can also request a different version number.

Here's what our data structure might look like if it were versioned:

Fig. 4 - Chocolate Cake for Breakfast with Versioned Containers and Iterators

Fig. 4 - Chocolate Cake for Breakfast with Versioned Containers and Iterators

This ends up working out pretty cool.  Mr. Cosby can request a versioned iterator of the view when his show begins that displays the model as it is when his show starts.  Let's say that his iterator is version 1.  That iterator traverses the list by only following links where the version number is less than or equal to the version number on the iterator.  So it first goes to "eggs", then follows link 1 to "milk", then follows link 1 to "wheat", then ends.  It can be doing this at the same time that another user is changing the data structure, first by inserting "chocolate," which creates the version 2 link from "milk" to "chocolate" and the version 2 link from "chocolate" to "wheat," and later by inserting "sugar" and "frosting" as version 3.  A version two iterator on the structure would start with "eggs" then follow the version 1 link (remember - less than or equal to the iterator version) to "milk."  But then it would follow the version 2 link instead of the version 1 link, so now it goes to "chocolate" instead of going directly to "wheat."

The same goes for the version 3 iterator, which gives us everything in the data structure.

There's a couple of issues you have to deal with when you implement a structure like this.  First off, you need some sort of a scheme to go through the structure and prune it of old traversal versions.  If I remember correctly, a scheme that will work to do this is to have the container itself remember how many users are accessing it and what versions they are at.  When one iterator goes away, the iterator should report this back to the container.  The container then looks to see if that iterator is the lowest-version-number iterator that it knew about.  If it is, the container can go through and prune all of the links that are versioned at that number and below, as well as nodes in the structure that are only included in the structure by links at that version and below.  So there's a little bit of housekeeping that has to go on, but the container should be able to do this after iterators go away.

In other words, once Mr. Cosby's show ends, the data structure that previously looked like Fig. 4 should end up looking more like Fig. 3.

Another problem to consider is this:  If the data structure lives for a long time and has lots of accessors, it is possible for the version numbers to overflow.  The overflowing isn't necessarily a problem, though, as long as you keep track of what is really the oldest version in the data structure.  Another way to do this is, as you prune the table, you can take the liberty of reversioning the table if there are no accessors to the table.  Depending on your situation, you may never have a chance to lock the whole structure in order to reversion it, but the first option should be workable.

Lastly, since you have to version the links themselves, you might not be able to directly use standard containers; for example, if you are using C++ you might have to create your own linked list template with a list of versioned next pointers in each node, instead of implementing your versioned list container in terms of the STL list container.  I never tried this so I'm not sure how it would work.  When I did this, I did it using a tree container that was implemented in STL style, with versioning in the pointers and iterators.  It was pretty cool.

Tagged as: 1 Comment
17Feb/090

Popping Threadsafe Containers

After spending a bit of time last week dealing with thread safety issues and a queue in C++, it only seems appropriate to blog a bit about it.  (Obviously, the information below would also apply to a stack or probably any other container type.)

Suppose you have a very common multiprocessing scenario:  two threads, one of which is producing work to be done, and another which is consuming the work to be done and performing the work.  You can accomplish this in a pretty straightforward fashion with:

  • A queue
  • Mutexes to prevent concurrent queue access
  • A condition variable to tell the consumer when there is work to do

Originally I thought about creating my own container class for this.  Then part of my brain resisted.  "Quiet, engineering part of Matt's brain!" it exclaimed.  "You're just trying to do that because it seems Interesting!"  So I shut down that idea and pushed on trying to use a standard STL queue.  However, after I had to deal with some concurrency issues over and over, I realized that, at least this time, the engineering part of my brain was right.

So I set out to create a threadsafe queue.  The idea was that it would feel a lot like an STL queue, and would implement thread safety under the covers for you - so, for example, a simple call to q.empty() would automatically lock the mutex for you, see if the underlying queue was empty, then unlock and return the result.  So, essentially an STL queue wrapper class with automatic mutex locking, right?  It should be easy!

Well, you'd think so, until you get to an implementation of pop().

To see why this matters, first we should establish what we expect out of a threadsafe queue in C++:

  • It should feel like an STL queue.  This means that it should have similar semantics and API if possible, and also, it should be a template.
  • You get out what you put in.  Not a handle to what you put in; exactly what you put in is what you get out.
  • Thread safety should be handled automatically - the user shouldn't have to deal with thread safety.

An STL queue "pop()" isn't as simple as it sounds.  The concept of "pop" is actually performed like this:

    std::queue q;
    ...
    if (! q.empty()) {
        int v = q.front();
        q.pop();
    }

Popping an empty queue has strange consequences; in fact, on my machine, it segfaults.  You'd think the queue would be resilient to that behavior but it isn't.  Also, you have to get the item off the front in a separate operation from actually removing the item.  This is so you can store anything in the queue - pointers, reference objects, etc. - and obtain the item in the front of the queue and know it will survive long enough for you to do something with it.  You pop() the queue after you are finished with the item.

That's all fine but it makes our threadsafe queue a bit of a problem.  Suppose we decide to follow strictly the STL queue API.  In that case, we'd perform an "if not q.empty(), then q.front() and q.pop()" dance.  We have to assume that each of those three actions is legal on its own though, and we surely can't release the lock after each one.  So that would mean that the user has to obtain a lock on their own, to be used outside of the whole three-step dance, which breaks one of our rules, namely, that the user shouldn't have to deal with thread safety.

Okay, so what if we abandon the need for strict adherence to the STL queue semantics, and instead just implement a pop() method that does everything?  Now we can hide the thread safety management from the user.  But now we have a different problem.  How do we know whether the value we get back from pop() is a legal value?  How do we know whether it came from the queue or whether the queue was empty and there was nothing to pop()?  We need some way to check to see whether the value coming back is valid, i.e., whether there was something in the queue to pop beforehand.

There's a couple of ways to do this, but none I like.

  • If the return value were a pointer (or an iterator), we could check for NULL (or end()).  However, we don't necessarily know that.  If the class is a template, and if we always get back what we put in, we can't know whether the return value is a pointer or a value or a reference.  True, we could cast the return value to a pointer type - but then, not only are we not returning what we put in, but we are getting into a rather scary situation where the user is required to massage the data before it can be used properly.  And we lose type safety.
  • We could return a std::pair<bool, t> where t is the type used to populate the queue.  Not as scary as the casting solution before, but still with the problem where the user has to massage the data, and the return value isn't strictly what was put in (although it is contained in the pair).
  • We could throw an exception if the container is empty, but an empty container isn't exactly an exceptional case and is probably not a good ethical candidate for throwing an exception.
  • We could stop using the STL queue and create our own queue with more reasonable semantics.  However, although I'm not necessarily in love with the STL queue, I'm also not convinced I can do it better, at least not convinced enough that I want to spend the time to do something that already exists.
  • We could do away with making the wrapper class a template and instead make it implementation-specific.

Since I don't know for sure that I will need my threadsafe queue for anything other than a very specific data type I have in mind right now, I chose the last option.  This is the agile way - I'll meet today's requirements today, and trust that I can refactor the code to meet future requirements when they show up.

So now everything works great, except doing the wait on the condition variable, which normally looks something like this:

    mutex m;
    condvar cv;
    m.lock();
    if (q.empty()) cv.wait(m);
    v = q.pop();

Yeah, here I am, managing thread safety outside of the queue again.  I really should put this into my threadsafe queue also; except I would need a method something like waitThenDo(action) where action is a function pointer or some other callable.  I hate how that makes my code look though.  And at this point, with the waiting done as shown above (roughly), my code is working, so I've stopped caring.

But if you have a great solution to this problem, I'd love to hear it.

Tagged as: No Comments