Sheevaplug: end_request: I/O error, dev mtdblock0

When booting my Sheevaplug the dmesg output shows a number of these errors:

end_request: I/O error, dev mtdblock0, sector 64
uncorrectable error :
uncorrectable error :

As it turns out, nothing is wrong. It’s just Linux telling you that it cannot auto-mount a partition that you specified in /etc/fstab. Solutions:

  • Check if all USB/ESATA drives are connected
  • Check if you specified the right UUID or device
  • Add a ‘noauto’ specifier to the offending partition if you still can’t find the error

Implementing a thread event loop using boost::bind and boost::function

Recently I was thinking about how to build a thread that has an event loop internally. If another thread wants to send us a message and have some function executed inside the event loop, how would I solve that?

Poor man’s solution

The poor man’s solution would be to have the public functions (called from another thread context) construct some kind of message struct/class that contains a ‘type’ field indicating which message should be called and some storage for the parameters. This object will then be pushed into the event queue for the event loop to read. The event loop can switch on the ‘type’ field and execute the proper method in its own context.

The problem is that this approach requires a lot of administrative overhead, if we ever change or add methods we need to update the ‘type’ list and update the switch() statement in the event loop. Cumbersome and error-prone!

Nicer solution using boost::bind and boost::function

So that brings me to my solution: what if there was some way to have the public function construct a function-pointer like object that also stores it’s arguments in the same object? We could put these objects in a thread safe queue and have the event loop just blocking read on that queue. When the event loop notices a new event, it takes the function pointer and executes it in its own thread context.

Enter boost::function! It’s a very clever function pointer that can store arbitrary arguments as well. To create such an object, we use boost::bind to store the parameters and point it to the right object to execute the member function on. Sample code (scroll to the right if text disappears):

#include <iostream>
#include <queue>
#include <string>
#include <boost/function.hpp>
#include <boost/bind.hpp>

 * This is the function pointer we will store and call.
 * It just means it's a function with void return type and no params.
 * Slightly confusing perhaps, but see below that we can actually still
 * bind method parameters and store their values.
 * This definition is only about how we should call the pointer from
 * the event loop, bound parameters do the rest.
typedef boost::function<void(void)> FunctionPointer;

 * Our stupid class
class EventProcessor
    * Pretend these methods will be called from another thread
    * NOTE: no locking is implemented here for simplicity

   void dostuff1(int x)
      /* All C++ member functions secretly have a 'this' pointer
       * as first param, we need to bind that to the object where
       * we want to execute our member function. That's what the first
       * two bind params are about. In this case we just specify the
       * 'this' value of the current object but it might well
       * be another object
       * First argument: bind to this member function
       * Second argument: the member function will be called on this object
       * Third argument: store the value of x
       * NOTE: We're actually storing a pointer to a private member
       * function here, probably because of magic trickery done by
       * boost::bind/boost::function we get around that :)
      FunctionPointer f = boost::bind(
            &EventProcessor::dostuff1_impl, this, x);

   void dostuff2(int y)
      FunctionPointer f = boost::bind(
            &EventProcessor::dostuff2_impl, this, y);

   void dostuff3(std::string &text)
      /* Hey look at this trick, we have a reference to string
       * but still a complete copy is stored
      FunctionPointer f = boost::bind(
            &EventProcessor::dostuff3_impl, this, text);

    * This is normally running inside a thread internal to our EventProcessor
    * But we keep it simple so we call it from our main function as well
   void eventloop()
      while (!events.empty())
         FunctionPointer f = events.front();
   /* Actual implementation functions called from event loop,
    * all methods run on the internal thread */
   void dostuff1_impl(int x)
      std::cout << "dostuff1 " << x << std::endl;
   void dostuff2_impl(int x)
      std::cout << "dostuff2 " << x << std::endl;
   void dostuff3_impl(std::string &text)
      std::cout << "dostuff3 " << text << std::endl;
   std::queue<FunctionPointer> events;

int main()
   EventProcessor p1;

   // In this part of the code no methods are executed yet

   // a copy is performed as you will see later on
   std::string payload = "lama";

   /* if it was still a reference, when we execute the loop
    * we would be seeing "test123" as dostuff3 text */
   payload = "test123";

   // Now iterate over the queue and execute each pointer

Please note that there are no real threads in this example, I left them out for clarity. Output:

dostuff1 100
dostuff2 2001
dostuff3 lama

For more info on boost::bind see my earlier post at Graphical explanation of boost::bind

Profiling FreeBSD system usage

This is a very nice guide to determining where the system load on a FreeBSD server comes from:

Wish I had something like this on linux :)

SIGSEGV tracing

Suppose your dmesg says this:

[832542.638297] XXX[3140]: segfault at 87 ip 483495 sp 7fffffffb920 error 4 in XXX[400000+26e000]

How to make sense of that? Easy! Check and execute this:

addr2line -e /path/of/XXX 483495

The ip value will tell you where the crash occurred!

Also, when your program is not allowed to write in the current directory execute this as root beforehand to change the location of core files:

echo 1 > /proc/sys/kernel/core_uses_pid
echo /tmp/core > /proc/sys/kernel/core_pattern

Beavering away..

Blatantly stolen from a nice picture about a busy beaver :)


Real-time and Embedded Systems, Call Flows and Object Oriented Design

For lots (and I mean lots) of telecom and TCP/IP sequence diagrams check this site:

Also a lot of OO design and patterns there, so if I ever get bored….;-)

Abstracted list of tips from “The Pragmatic Programmer”

Don’t want to read the whole book? :)

Here is a list describing the most important points:

Development Environment Tips

Nice article about setting up a good workspace for us software developers:

Profiling STL added in gcc 4.5

New gcc 4.5 adds a special profile mode to indicate problems with your program’s usage of the STL library:

Can’t wait to try it :)

Graphical explanation of boost::bind

Chris Kohlhoff has a nice explanation about the often misunderstood functionality of boost::bind (or std::bind in the upcoming standard). Binding is something that is used quite often so it’s well worth the time to try and understand it :)

Dealing with file descriptor leak in Eclipse + CDT

Current eclipse builds have the very nasty tendency to leak file descriptors to /usr/share/mime/globs (especially when using CDT). After a while you hit the max fd limit and eclipse will refuse to save your file (and workbench state).

Luckily, under linux you can save the day using ‘gdb’:

  1. Find offending fd’s:
cd /proc/`pidof java`/fd
ls -la|grep globs

Write down about 5 fd numbers

  1. Attach with GDB:
gdb -p `pidof java`

For each fd enter:

p close(XXX)

where XXX is the fd number. Press enter after each line

  1. now type ‘c’ to continue and ‘quit’ to exit
  2. Quickly save your stuff in eclipse and exit/restart


Removing elements that match a specified criterium from a vector

Deleting items that match a specific criterium from a C++ vector is not as straightforward as it seems. The solution? First define a function that determines if the element should be deleted:

bool bla(child_t &child)
        return child.first == -1;

And then use our friends erase and remove_if:

std::vector<child_t> children;
children.erase(remove_if(children.begin(), children.end(), bla), children.end());

Easy right….:)

Popular mechanics

Really cool stuff, all issues of the US magazine ‘Popular Mechanics’ going back to 1926 are available for free on google books:

The scans are complete and of really good quality, also nice looking at ads from the 1950’s :)

Mocking about

Lately I’ve been fascinated by the opportunities a mock framework gives me to speed up unit test development (see wikipedia for a more detailed explanation).

So far, I’ve tried to write my own using the C++ preprocessor and I almost succeeded. The following works just fine for simple virtual members:

/** Define a mock function with 1 parameter */
#define MOCK1(returntype, name, type1, parm1, retval)\
      DefaultZeroInt _##name##_callcount;\
      type1 _##name##_##parm1;\
      /* Function definition*/\
      virtual returntype name(type1 parm1)\
         _##name##_##parm1 = parm1;\
         return retval;\
      /* Mock utility functions*/\
      void name##_reset()\
         _##name##_callcount = 0;\
         _##name##_##parm1 = type1();\
      bool name##_called_once(type1 parm1)\
         return ((_##name##_callcount == 1) && (_##name##_##parm1 == parm1));\
      bool name##_last_params_match(type1 parm1)\
         return ((_##name##_callcount > 0) && (_##name##_##parm1 == parm1));\
      int name##_call_count()\
         return _##name##_callcount;\

class DefaultZeroInt
   DefaultZeroInt() : val(0) {}
   operator int() { return val; }

   DefaultZeroInt& operator= (const int f)
      val = f;
      return *this;

   DefaultZeroInt& operator++ (void)
       return *this;

   int val;

And of course there are variations for multiple parameters (just expanded versions of this 1-argument example). This works fine for arguments like string, int, etc. But not for pointer parameters or reference parameters!

So after some fun time trying to make this work, a little voice in my head started telling me to look around for an existing library. I don’t consider it wasted time to have tried myself, it will help me evaluate other libs better :)

As far as I’m concerned, there are two serious candiates:

  1. Google mock objects (new BSD license)
  2. Hippomocks (LGPL)

Both are actively maintained. My evaluation:

  • Gmock requires linking to their library code, Hippomocks is header-only and therefore very easy to include.
  • Gmock includes gtest lib and that defines ‘TEST’. This rules out a combination with UnitTest++ because of symbol conflicts. The trick is to include gmock first, then #undef TEST and then include UnitTest++. Not nice!
  • Gmock by default doesn’t require the expectations to be met in the order they are specified. It’s possible but requires extra configuration. Hippomocks does do this by default which is nicer in my opinion.
  • Gmock uses macros to define mock method implementations, Eclipse can expand and autocomplete them just fine. Hippomocks is a bit trickier so no autocomplete there.
  • Gmock is tightly coupled with gtest, some pre-main() handholding is necessary to make it work with my current unit test framework of choice (UnitTest++). It’s documented quite well so this is not a big problem./
  • GMock documentation is extensive and includes a big tutorial and a big cookbook for almost any scenario you might want to implement. Hippomocks documentation also includes a very nice, short and to-the-point tutorial. I was up and running much quicker with Hippomocks.
  • Specification of mock method behaviour is more versatile in Gmock but also a little bit more complex than Hippomocks. Somehow the Hippomocks way feels a little bit more natural.

Conclusion: I wil try to use Hippomocks, it seems like an excellent fit for my usage scenario’s at the moment. If I need something more advanced, a migration to Gmock could be an option