On which physical cpu/core does a process run?

Started by
15 comments, last by hydroo 16 years, 2 months ago
Is there a way to find that out? I need this for performance evaluation. I found out that it might be task_struct->oncpu, which unfortunately is only accessible inside the kernel :(
Advertisement
Quote:Original post by hydroo
Is there a way to find that out?
I need this for performance evaluation.

I found out that it might be task_struct->oncpu, which unfortunately is only accessible inside the kernel :(


A process may run on any or all cores, at the same time. There is no way to find out outside of the kernel because it requires a context switch to the kernel, so (a) the process may not even be running when the query is made and (2) the answer is just as likely not correct by the time the kernel call returns.

Stephen M. Webb
Professional Free Software Developer

It's not totally important to be 100% in timing correct by the way.
I'd give it a try. And watch how accurate it'd be.

Or can I get an info when a process is rescheduled to another queue?
On windows you can request a thread runs on a particular processor/core (something like SetThreadAffinity() I believe). I assume you can do something similar on the major operating systems. However, in general it is a bad idea as the OS knows a lot better than you how to schedule threads/processes.
I don't want to pin them down. I want to find out where they are at the moment.

thanks for the input guys
Its kind of like trying to pin the exact location and momentum of an electron..
I am pretty sure that you can find out where it is. Because even when a process is not running it's still in the queue of it's cpu.
And as I said its not the goal to be 1ms-correct.

thanks for your replies
Quote:Original post by hydroo
I am pretty sure that you can find out where it is. Because even when a process is not running it's still in the queue of it's cpu.


Is it? or is it in the ready queue, not assigned to a CPU until it's running? If that's the case, no matter how often you look, your process will never appear to be getting any CPU at all on a single-processor system, and chances are it's not running on a multiprocessor system.

Your best bet is to read the Linux kernel code to understand how the scheduler works.

Stephen M. Webb
Professional Free Software Developer

Quote:Original post by Bregma
Quote:Original post by hydroo
I am pretty sure that you can find out where it is. Because even when a process is not running it's still in the queue of it's cpu.


Is it? or is it in the ready queue, not assigned to a CPU until it's running? If that's the case, no matter how often you look, your process will never appear to be getting any CPU at all on a single-processor system, and chances are it's not running on a multiprocessor system.

Your best bet is to read the Linux kernel code to understand how the scheduler works.


I am pretty sure that there is no central queue (I read about the O(1)-Scheduler. Don't know how CFS handles things). Processes will most likely be scheduled to the cpu they were scheduled to before, to for example exploit locality. Only under certain circumstances it will be moved to another cpu.
This wouldn't be possible without indication where a process has been, would it.


So I guess there is no easy way to do it.
Probably one the best questions on this forum in a while.

I don't know of any easy way to do this. You could try and modify the kernel to log the information (which is going to be tremendous BTW and slow everything down).

Doing this from user space in your app would get messy.

How often does a process (thread is a process on linux) switch CPUs. My guess is that it happens more often that you would expect.

You could keep track of the gettimeofday() time of common thread entry/blocking points (after IO blocking or mutex, etc). Won't give you an idea of what CPU you are on, but will give you an idea of what really matters, ie how the threads are interacting and blocking.

Also gettimeofday() is a syscall, so you might want to use RDTSC in x86 chips to simply read the CPU clock cycle counter instead (but keep in mind it wraps around quickly...),

// for 32bit machinestypedef signed long long is8;static inline is8 VCycle(void) { is8 x; asm volatile("rdtsc\n\tmov %%edx, %%ecx\n\t" :"=A" (x)); return x; }// or for when running in 64bit modetypedef unsigned int iu4;typedef signed long is8;static inline is8 VCycle(void) { iu4 aa,dd; asm volatile("rdtsc" : "=a" (aa), "=d" (dd)); return ((is8)aa)|(((is8)dd)<<32); }


BTW, I know the 64bit one works, because I use it all the time, forget if I tested the 32bit version!
_|imothy Farrar :: www.farrarfocus.com/atom

This topic is closed to new replies.

Advertisement