spervez
spervez

Reputation: 33

pinning a pthread to a single core

I am trying to measure the performance of some library calls. My primary measurement tool is the rdtsc call. After doing some reading I realize that I need to disable preemption and interrupts in order to get the most accurate readings. Can someone help me figure out how to do these? I know that pthreads have a 'set affinity' mechanism. Is that enough to get the job done?

I also read somewhere that I can make calls into the kernel of the sort

preempt_disable()
raw_local_irq_save(...)

Is there any benefit to using one approach over the other? I tried the latter approach and got this error.

error: 'preempt_disable' was not declared in this scope

which can be fixed by including linux/preempt.h but the compiler still complains.

linux/preempt.h: No such file or directory

Obviously I have not done any kernel hacking and I could not find this file on my system anywhere. I am really hoping I wont have to install a new linux kernel. :)

Thanks for your input.

Upvotes: 0

Views: 980

Answers (1)

Pavan Manjunath
Pavan Manjunath

Reputation: 28525

Pinning a pthread to a single CPU can be done using pthread_setaffinity_np

But what you want to achieve at the end is not so simple. I'll explain you why.

preempt.h is part of the Linux Kernel source. Its located here. You need to have kernel sources with you. Anyways, you need to write a kernel module to access it, you cannot use it from user space. Learn how to write a kernel module here. Same is the case with functions preempt_disable and other interrupt disabling kernel functions

Now the point is, pthreads are in user space and your preemption disabling function is in kernel space. How to interact?

Either you need to write a new system call of your own where you do your preemption and interrupt disabling and call it from user space. Or you need to resort to other Kernel-User Space Interfaces like procfs, sysfs, ioctl etc

But I am really skeptical as to how all these will help you to benchmark library functions. You may want to have a look at how performance is typically measured using rdtsc

Upvotes: 3

Related Questions