15

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.

  • 1
    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
23

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.

| 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? – likejudo 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? – likejudo 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! – likejudo Jan 25 '11 at 23:37
7

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.

| 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
3
htop

This command works good in both ubuntu and centos and shows graphically how many CPUs and how are they being used.

for centos:

yum install htop

for ubuntu:

apt-get install htop
| improve this answer | |
2

Try the following:

cat /proc/cpuinfo

Here's a link to an Android Java example.

| improve this answer | |
  • 1
    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
0

You can use cat /sys/devices/system/cpu/possible or cat /sys/devices/system/cpu/online. Possible is mainly incase you have isolated a cpu to run some particular program.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.