Blog by a Linux programmer.

Profiling Input/Output performance

There are various nice tools for program profiling when it comes to CPU usage like gprof or oprofile. Those tools will tell you exactly which functions in your code consume most CPU time, where they are called, how often etc. But what if your program is slower than it should be because it waits for I/O disk operations? How can you check if the I/O is the bottleneck and what are the slowest operations?

Complexity of std::list::size() is... O(N)

I want to share my recent "discovery" that was very shocking to me. Reading comments to a slashdot story about the C++0x standard I read something very interesting: std::list::size() has O(N) complexity in the gcc compiler.

Not so obvious multi-thread programming specific bugs.

We all know that when writing multi-threaded programs one should remember about few more details like locking, using thread-safe libraries etc. Here is a list of not-so obvious bugs specific to multi-threaded programs. Many of them are not mentioned in documentation or tutorials for beginners, but I think everybody who is using threads will hit them eventually.

Using dd as a Swiss Army knife

Here are some useful examples of how a programmer (but not only) can use the dd command as a Swiss Army knife. Many of us are used to use a command similar to this one:

  1. dd if=/dev/sda of=sda.img bs=1k

This is a faster version of the cat command because of the bs= option. But dd can do much more, even for a programmer. Here are some examples of dd usage that I find useful as a debugging aid:

Threads and fork(): think twice before mixing them.

When debugging a program I came across a bug that was caused by using fork(2) in a multi-threaded program. I thought it's worth to write some words about mixing POSIX threads with fork(2) because there are non-obvious problems when doing that.

Using Valgrind to debug memory leaks

Valgrind is a wonderful tool useful mainly to debug memory related problems in C/C++ programs. I don't know a better tool to find memory leaks. Although output of this program is often clear and intuitive it's worth to spend some time to get deeper knowledge of how Valgrind works, what exactly its messages mean and what are the problematic cases when tracing a memory leak is harder even with Valgrind.

All about Linux signals

In most cases if you want to handle a signal in your application you write a simple signal handler like:

void handler (int sig)

and use the signal(2) system function to run it when a signal is delivered to the process. This is the simplest case, but signals are more interesting than that! Information contained in this article is useful for example when you are writing a daemon and must handle interrupting your program properly without interrupting the current operation or the whole program.

C++ exception specifications are evil

When I began to program in C++ I was already experienced in C and had written some code in Java. After writing few thousands lines of code in this language I felt comfortable with all of it's goodies like templates, inheritance, operator overloading and (I thought so) exceptions. But I made at least one mistake: I used exception specifications. My intention was to use them as in Java: tell the compiler that this method throws specific exceptions so it can use this information to optimize code and tell me when I'm throwing/catching wrong exceptions. A great surprise was that in practice in most cases writing exception specifications slows the code and introduces bugs!

Pipe in bash can be a trap!

Today a colleague at work tried to debug a script in bash that didn't want to work as he expected. He hit one of traps people get into when writing bash scripts. Let's look at the code that find the largest message that was sent from this computer:

LZMA - better than bzip2

Everyone sometimes needs to compress something: doing backup, sending files over Internet etc. Most of us uses gzip or bzip2. It's known than bzip2 has a bit better compression ratio but it's much slower. I'm one of that people who only used those two programs, often with conjunction with tar using

tar -czf
or
tar -cjf
and wasn't think I need something better. But one day I wandered if exists something else, some alternative and found LZMA.

Syndicate content