Jump to content
  • Advertisement
Sign in to follow this  
hydroo

On which physical cpu/core does a process run?

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I don't want to pin them down. I want to find out where they are at the moment.

thanks for the input guys

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 machines
typedef 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 mode
typedef 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!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!