7

I have a sequential user space program (some kind of memory intensive search data structure). The program's performance, measured as number of CPU cycles, depends on memory layout of the underlying data structures and data cache size (LLC).

So far my user space program is tuned to death, now I am wondering if I can get performance gain by moving the user space code into kernel (as a kernel module). I can think of the following factors that improve the performance in kernel space ...

  1. No system call overhead (how many CPU cycles is gained per system call). This is less critical as I am barely using any system call in my program except for allocating memory that too just when the program starts.
  2. Control over scheduling, I can create a kernel thread and make it run on a given core without being thrown away.
  3. I can use kmalloc memory allocation and thus can have more control over memory allocated, may can also control the cache coloring more precisely by controlling allocated memory. Is it worth trying?

My questions to the kernel experts...

  • Have I missed any factors in the above list that can improve performance further?
  • Is it worth trying or it is straight way known that I will NOT get much performance improvement?
  • If performance gain is possible in kernel, is there any estimate how much gain it can be (any theoretical guess)?

Thanks.

1
  • 2
    The performance gains from moving a CPU intensive user-space program are tiny. The bear-traps and complexity both in terms of debugging and the ability to crater the program and the system are significant. IMO, this avenue is just not worthwhile; algorithmic optimisation will always give better paybacks.
    – TerryE
    Jun 30, 2012 at 11:02

2 Answers 2

7

Regarding point 1: kernel threads can still be preempted, so unless you're making lots of syscalls (which you aren't) this won't buy you much.

Regarding point 2: you can pin a thread to a specific core by setting its affinity, using sched_setaffinity() on Linux.

Regarding point 3: What extra control are you expecting? You can already allocate page-aligned memory from user space using mmap(). This already lets you control for the cache's set associativity, and you can use inline assembly or compiler intrinsics for any manual prefetching hints or non-temporal writes. The main difference between memory allocated in the kernel and in user space is that kmalloc() allocates wired (non-pageable) memory. I don't see how this would help.

I suspect you'll see much better ROI on parallelising using SIMD, multithreading or making further algorithmic or memory optimisations.

2
  • Regarding point 3: I am convinced that with mmap() I can control cache's set associativity. With kmalloc() non-swapable memor can be allocated which can be simulated by mmap() and mlock() system calls. But with kmalloc we also get physically contiguous memory--can it help in getting some performance boost (h/w prefetching of memory)? Jul 1, 2012 at 16:40
  • 1
    @NitinKunal prefetching works at the virtual memory level: prefetched memory accesses can and do trigger page faults if the page is not resident. (this applies to both the CPU's internal read-ahead and explicit prefetch instructions) If the page is resident, but the page table entry is not in the TLB, it will cause a lookup of the page table entry and add it to the TLB ahead of time. Whether or not that memory is physically contiguous is irrelevant: the CPU doesn't "know" this, it must check in the TLB or page table anyway. Contiguous memory is only useful for DMA.
    – pmdj
    Jul 2, 2012 at 8:43
5

Create a dedicated cpuset for your program and move all other processes out of it. Then bump your process' priority to realtime with FIFO scheduling policy using something like:

struct sched_param schedparams;
// Be portable - don't just set priority to 99 :)
schedparams.sched_priority = sched_get_priority_max(SCHED_FIFO);
sched_setscheduler(0, SCHED_FIFO, &schedparams);

Don't do that on a single-core system!

Reserve large enough stack space with alloca(3) and touch all of the allocated stack memory, map more than enough heap space and then use mlock(2) or mlockall(2) to pin process memory.

Even if your program is a sequential one, if run on a multisocket Nehalem or post-Nehalem Intel system or an AMD64 system, NUMA effects can slow your program down. Use API functions from numa(3) to allocate and keep memory as close to the NUMA node where your program executes as possible.

Try other compilers - some of them might optimise better than the compiler that you are currently using. Intel's compiler for example is very aggresive on laying out instructions as to benefit from out of order execution, pipelining and branch prediction.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.