Is it possible to get the TID of the thread that is currently running on a logical core, such as logical core 0? This is easy to do in kernel space, but I am not sure how to get the thread information from userspace.
CodePudding user response:
You can get information what is running on which core with hwloc-ps
(require the hwloc
package). For example, hwloc-ps -a -t -l -e
gives you which threads of which process runs on which PU (ie. logical core). The provided information are the name of the thread and the SPID (certainly what you call a TID). You can get additional information by looking in /proc/TARGET_PROCESS_PID/task/TARGET_THREAD_SPID/
. This last operation requires additional privileges (for obviously security reasons) though hwloc-ps
apparently do not require advanced privileges.
CodePudding user response:
IDK the answer to that question, but it's unlikely that the answer will be useful to you in any case.
If you can get the information at all (@Jérôme Richard thinks you can get it), then what will you do with it? Something like this?
int core_of_interest;
pid_t thread_of_interest;
...
if (thread_on_core(core_of_interest) == thread_of_interest) {
do_interesting_thing();
}
That code won't reliably work. The problem is, by the time do_interesting_thing()
was called there's no guarantee that the thread running on the core_of_interest
still is the same thread that was running at the moment when you asked. All you really know is which thread was running on that core at some instant in time between when you asked and when the answer was returned. But with a preemptive scheduler running, which thread is running on which core can change at any time.