Benchmarking Linux And Hardware

Modern operating systems provide abstractions of hardware to make programming easier. However, it can be possible to learn some details about the hardware using simple benchmarks running in userspace. I wrote some of these benchmarks in C to measure these things on my computer:

  • The cost of a context switch in Linux
  • The size of the TLB and the cost of a TLB miss
  • The seek time and rotational delay of a hard drive.

Context Switch

For this test, I create several processes and have each one call sched_yield() 100000 times. Each call yields the processor and thus causes a context switch. If you compare this to the time needed to just create the processes, you can get a rough idea of how long a context switch takes. It’s about 1350ns on the test computer.

TLB

Here I iteratively access more and more pages in each test until there is a jump in the average read time. On the test computer, it looks like the TLB has 64 entries and it takes about 4.5ns to handle a TLB miss.

Hard Drive

I found seek time and rotational delay by doing random reads across the disk, and recording the time and distance between reads. My disk is a 4200rpm, so rotational delay is about 14ms. Seek time ranges from 5ms to 20ms, depending on the distance between reads.

If instead you do sequential reads across the disk, but increase the distance between reads by 1 block each time, you get more information.

You can read about the correct way to interpret these disk measurements in Talagala, Arpaci-Dusseau, Patterson.

Links