Blog Archive

Monday, June 23, 2008

Measuring context switching speed of different OSs

OpenBSD 4.3

#include <sys/time.h>

double GetAbsTimer() {
struct timespec ts;
int result=clock_gettime(CLOCK_REALTIME,&ts);
if (result!=0) exit(1);
return ts.tv_sec + ts.tv_nsec*1e-9;
}

The time precision is about 1.3 microseconds.

Fedora 9:

The docs say you can use CLOCK_REAL_TIME_HR after including <linux/time.h>, but that header conflicts with <stdlib.h>. Confusingly, there is also and <time.h>. Include <sys/time.h>. When linking, specify -lrt or you'll get a link error.

The time step is also 1.3 microseconds.

Token passing test: parent process opens a pipe, forks a child. In a loop, write a byte to the pipe. The child reads the byte and writes it back. Repeat a few hundred thousand times. Results:

Windows 64

2.6Ghz dual opteron: 120-140K roundtrips/sec.
Note that Windows pipes don't get anywhere close to that number.
QueueUserAPC is the method of choice on Windows, and we're testing switching between two threads in the same process space (which is a valid test, because thread is how you parallelize stuff on Windows, not processes).

OpenBSD 4.3

ThinkPad T60 (2.3mhz Core duo): 130K roundtrips/sec (56.5K switces/mhz)

Supermicro server, dual quad-core 3.0ghz xeons: 66K roundtrips/sec (22,000 switches/mhz)

Linux 2.6.23

ThinkPad T60 (2.3mhz Core duo): 22K roundtrips/sec (9.5 K switches/mhz). *HORRIBLE*

Fedora 9 under Virtual Box 1.6.2 on 2.4 Quad Core 2: 4.5K roundtrips/sec (1.9K switcehs/mhz)
That's right, 1/25th of the speed. Don't let anyone tell you virtualization is efficient. It's cool, but it's not efficient.

AMD Athlon 64 3200+ (2ghz), 2.6.23.8-34 kernel: 171K switches/sec (85K swiches/mhz)

Notes: cat /proc/cpuinfo, uname -a

2.6Ghz dual opteron, 32-bit Slax: 280-310K roundtrips/sec (115K switches/mhz). Very nice! What is so different about Slax? Why does Fedora suck so much? Mysteries about. Virtually the same kernel was being used.

Some conclusions:
1. Linux sucks at context switching on Intel chips
2. Linux rules at context switching on AMD chips, especially Opteron
3. OpenBSD is faster than Linux on intel chips


VirtualBox guest additions on Fedora, Ubuntu

Fedora:

yum -y install binutils gcc make kernel-headers kernel-devel

Ubuntu:

sudo apt-get install build-essential
sudo apt-get install linux-headers-`uname -r`

I haven't run OpenBSD under VirtualBox, but on that OS the package installer is pkg_add. As in,

pkg_add -v ftp://ftp.openbsd.org/pub/OpenBSD/4.3/packages/i386/xemacs-21.4.19p5.tgz