开发者

Is it possible to print the CPU and core using pthreads on Linux

I'm struggling with getting a multi-threaded app to run on multiple cores. I've looked into affinity, scheduling, etc. Is there a way to find out the CPU Id that 开发者_如何学Pythonany thread is running on? I'm using sched_getaffinity now - but I think that is related to the process id, not the thread within the process. The mulit-threaded app works great on Windows, but seems to be CPU bound (using only one CPU) on linux

Update:

If my linux app launches 64 threads - I still only have one pid right? It's still my understanding that each thread launched can run on a different CPU/core on the target hardware, right?

A sample app is here : How do I make a multi-threaded app use all the cores on Ubuntu under VMWare?


Your first question

Is there a way to find out the CPU Id that any thread is running on? I'm using sched_getaffinity now

sched_getaffinity doesn't return the CPU, it returns a mask of eligible CPUs. It says:

The affinity mask is actually a per-thread attribute that can be adjusted independently for each of the threads in a thread group.

And then

If you are using the POSIX threads API, then use pthread_setaffinity_np(3) instead of sched_setaffinity().

To simply find out the CPU used, /proc/[pid]/stat has a "processor" field:

processor %d (since Linux 2.2.8) CPU number last executed on.

Your second question:

The mulit-threaded app works great on Windows, but seems to be CPU bound (using only one CPU) on linux

Show the smallest example that exhibits this problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜