Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

On Linux, how to tell how many cores of the machine are active? I assume a test for this would work for Android too. I need to know if more than one core is ever active. Was wondering to test this by having a process create many threads. Is it possible for a thread to query which processor it is on? that way one can tell if multiple cores will ever be used under heavy load. Not sure if I am on the right track.

share|improve this question

migrated from stackoverflow.com Jan 25 '11 at 3:17

This question came from our site for professional and enthusiast programmers.

    
By "active", I assume you mean how many cores are currently in use? Or do you mean how many cores the system has? – Mikel Jan 24 '11 at 22:40
    
yes, I would like to tell how many are currently in use – Anil Jan 24 '11 at 22:43
up vote 8 down vote accepted

You can use top to list the utilization of each core. Press 1 if necessary to split the CPU row into a separate row for each core.

You can also add a column that shows the last-used core for each process. Press f to bring up the field list, then j to activate the "P" column. Then press space to return to the live view.

share|improve this answer
    
So perhaps I should write and run a program that spawns many threads and then run the 'top' command in the console? – likejiujitsu Jan 25 '11 at 17:36
    
I think that should work. Just press H when you're in top to list threads separately. Or you could write a program with an infinite loop and run it a bunch of times. – Brian Jan 25 '11 at 21:00
    
Say, I write the program to spawn 100 threads, each one performing some long, intensive computation. If I write it in Java (Android), is there a guarantee that the JVM/KVM will run the threads on different cores, and run them on all the cores? – likejiujitsu Jan 25 '11 at 22:15
1  
I don't know - isn't that what your experiment is supposed to figure out? – Brian Jan 25 '11 at 22:28
    
I need to test a program that says it will restrict the number of processor cores being used. I shall search some more. thanks! – likejiujitsu Jan 25 '11 at 23:37

ps has a field called psr to tell you which processor a job is running on.

So you could use something like:

ps -e -o psr= | sort | uniq | wc -l

Note that merely running ps like this will of course make at least one core active.

Probably better is to run this:

tmp=/tmp/ps.$$
ps -e -o psr= > /tmp/ps.$$
sort -u "$tmp" | wc -l
rm "$tmp"

that way the sort and wc do not increase the count.

share|improve this answer
    
What if system has 16 cores and nobody is using some of them? – Elalfer Jan 24 '11 at 22:44
    
Then it prints 0 because none of them are in use. I think that is what the question is asking for. – Mikel Jan 24 '11 at 22:45
    
Didn't see new comments to the question. – Elalfer Jan 24 '11 at 22:47
1  
Of course, by running ps we are making at least 1 core active. ;-) – Mikel Jan 24 '11 at 22:51
    
Thanks for your answer. The disadvantage is I need to know if more than one core is ever active. Was wondering to test this by having a process create many threads. Is it possible for a thread to query which processor it is on? that way one can tell if multiple cores will ever be used under heavy load. Not sure if I am on the right track. – Anil Jan 24 '11 at 22:57

Try the following:

cat /proc/cpuinfo

Here's a link to an Android Java example.

share|improve this answer
    
By "active", I assume Anil means how many cores are currently in use, and how many are idle, i.e. an approximation of how busy the system is. /proc/cpuinfo just tells you how many cores the system has, and even you would have to do more than cat /proc/cpuinfo to account for HyperThreading. – Mikel Jan 24 '11 at 22:38
    
OK - that wasn't clear but you're right. – Amir Afghani Jan 24 '11 at 22:49

You must log in to answer this question.

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