0

In Linux, in a multi-core processor, ps, top and similar tools can show the CPU logical core id running a specific process. If the process runs for a certain amount of time, it's easy to identify it in the process list.

I have instead a stand-alone program which prints "hello world" and the number of logical core detected from CPU assembly (RDPID instruction):

$ ./hello_world
hello world
1
$

It immediately ends. I would like to compare this number with the one provided by ps, top or similar. So, how to obtain the same information (the CPU logical core id) in this case? How to get the process information while this process is still executing?

3
  • Why is this information important in case of a short-living process? Please make sure there is no XY problem here. What actual problem are you trying to solve? Commented May 9, 2023 at 8:33
  • @KamilMaciorowski This could be useful in general. However, I edited the question adding more details.
    – BowPark
    Commented May 9, 2023 at 8:42
  • I don't see where top shows the CPU "running" a process or thread. If a process is using 10% of a CPU, then 90% of the time no CPU is running it. top can show you the "Last used CPU", but with a warning that even asking is likely to alter the outcome across the whole system. For ps, the thread cumulative CPU time is found from the time between entry and exit of user mode (not continuously updated). CFS deals with variable time-slices, and longest appears to be capped at 0.006 secs, which means any separate process attempting to monitor is hopelessly lagging behind. Commented May 9, 2023 at 11:57

2 Answers 2

2

The assignment of a process to a core id is transient (unless specifically locked), and the tools can only show the instantaneous value.

A thread can be rescheduled onto a different core at any time (generally during a system call, or when it exceeds its time slice). A multi-threaded process can be running on many cores simultaneously.

There may be optimisations which try to maintain some stability (e.g. to improve caching), but they cannot be relied on.

Also, echo is a shell built-in -- it "runs" inside the existing shell process.

1
  • I edited the question mentioning a stand-alone example process. There's no problem if the core id is transient. But is there a way to obtain this transient value while the process is executing?
    – BowPark
    Commented May 9, 2023 at 8:29
1

There is the Linux syscall getcpu, which returns the CPU that the thread was scheduled on at the point it made the syscall.

Read the notes on that man page and realize how right Paul is: making a syscall gives you a relatively high probability that you will be scheduled on a different CPU core the moment it returns. So, getcpu only makes guaranteed statements on your thread's CPU core at a point in time in the past. Not the present.

So, long story short:

How to get the process information while this process is still executing?

You can get that the CPU core running a thread using getcpu, but it's immediately outdated information. Core allocation isn't static.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .