linux, C++,pthreads: process uses only 1 CPU core out of 2 available

Started by
4 comments, last by glJack 16 years, 6 months ago
Linux, C++,pthreads: process uses only 1 CPU core out of 2 available. I am doing some multithreading in linux on my dual core CPU, and for some reason the biggest CPU usage I get is 50%. This makes me think that process runs only on one core of two available. I do not do any CPU affinity magic. Any idea what can be the reason for this ? Thanks in advance.
Advertisement
How many threads you running? What tool are you using to read CPU usage? Are you calling pthread_yield() or any kind of sleep function? What are your threads doing?

edit:
Also keep in mind Linux SMP is dealing with a large amount of other processes at the same time yours is running. So you are never guaranteed that two threads will always run at the exact same time on a multiprocessor machine, but with two looping threads the odds a very likely that this will happen 'some' of the time.
Just to get the obvious out of the way, you do have an smp-capable kernel, right?

<hr />
Sander Marechal<small>[Lone Wolves][Hearts for GNOME][E-mail][Forum FAQ]</small>

Wolfdog

>> How many threads you running? What tool are you using to read CPU usage?

I run 3 threads and use "top" command line tool to measure CPU usage. And CPU usage never gets higher than 50%.

Sander

Yes, I am pretty sure that my kernel is SMP-cable. I run on ubuntu feisty and fedora 7. And on both I experience the same problem.
I modified some code to keep two threads running very busy and used top to see the result. On a dual core AMD, running fedora core 7 I see my process at around 197-200%CPU, with 1-2% going to firefox and 0 for every other process.

Perhaps be sure you are not trying to directly interpret the Cpu(s) numbers of %us,%sy,%ni,%ud%wa,%hi,%si,%st, I don't know what they mean, but they do not reflect the CPU usage I see in the process list. Maybe someone else might have an idea what they mean.

From what I can tell if you fully use a dual core processor you should see 200% for %CPU on your process. So 50% means you are only using 1/2 of a single core.

If there is another process that is using a large amount of the CPU this will cause you to get bad results. If not this would indicate maybe that you are either not doing enough work, or your process is ending it's thread quantums early. Perhaps you have a lot of mutex contention?

Just because you have two threads this does not mean that you will have full CPU utilization, this is very dependent on what the threads are doing.
Wolfdog

yeah, you're right. This CPU usage in linux confuses me as well. I don't know how to interpret it. Thanks a lot for help.

I've just found about this function : pthread_setconcurrency(n), and I think that in you call this function passing number of CPU's you have as argument, you'll get your process executed on all the cpu's you have. I will check this tomorrow on my work machine cause I don't linux installed at my home computer.

I have some mutex connection in my source, but I wouldn't say I have lots of it. Anyway, tomorrow I'll check how pthread_setconcurrency works, and will right about my progress here.

Thanks again for help!

This topic is closed to new replies.

Advertisement