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.
5 Answers
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.
-
So perhaps I should write and run a program that spawns many threads and then run the 'top' command in the console?– likejudoJan 25, 2011 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.– BrianJan 25, 2011 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?– likejudoJan 25, 2011 at 22:15
-
1I don't know - isn't that what your experiment is supposed to figure out?– BrianJan 25, 2011 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!– likejudoJan 25, 2011 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.
-
What if system has 16 cores and nobody is using some of them?– ElalferJan 24, 2011 at 22:44
-
Then it prints 0 because none of them are in use. I think that is what the question is asking for.– MikelJan 24, 2011 at 22:45
-
Didn't see new comments to the question.– ElalferJan 24, 2011 at 22:47
-
1
-
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.– AnilJan 24, 2011 at 22:57
-
2By "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 thancat /proc/cpuinfo
to account for HyperThreading.– MikelJan 24, 2011 at 22:38 -
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
You can use:
cat /sys/devices/system/cpu/possible
or
cat /sys/devices/system/cpu/online
Possible is mainly in case you have isolated a CPU to run some particular program.