Jack welch
Jack welch

Reputation: 1707

Calculating the number of processor cores in linux

How can one deduce the amount of processors a linux os has by using elapsed time(time it takes to execute any section of code).I have the clock ticks it took to perform a particular section of code .How do i measure the amount of cores on the machine from that

Upvotes: 0

Views: 245

Answers (2)

Mats Petersson
Mats Petersson

Reputation: 129524

I guess we could do something like this:

Write some code that does some heavy math work, that can be run in parallel - such as a naive method for finding primes. Then run that in multiple threads, starting with one, then going up. At some point, the time it takes to figure out the N first primes will be, the number of threads will be the same as the number of (available) processor cores.

But it's a very clumsy method to find the number of processors, and it almost certainly won't work on a system with high load, or a system that is running on a virtual machine, etc, etc.

Upvotes: 1

akp
akp

Reputation: 1823

You can get the information about CPU(No of processors & no of cores,etc) using

cat /proc/cpuinfo.

but if you are trying to calculate the clock ticks taken in executing particular code inside linux-kernel you can try.

#include <sys/time.h>

unsigned long ini,end;
preempt_disable();
rdtscl(ini);
...your code....
rdtscl(end);
preempt_enable();
printk("time lapse in cpu clics: %lu\n",(end-ini));

for more details http://www.xml.com/ldd/chapter/book/ch06.html or download.intel.com/embedded/software/IA/324264.pdf and if your code is taking more time then you can also use jiffies effectively.

And for user-space application you can use various timing functions which give the time in nanosecond resolution or oprofile(http://oprofile.sourceforge.net/about/) & refer Timer function to provide time in nano seconds using C++

Upvotes: 2

Related Questions