Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I want to play around with threading and parallelism. I know my processor has multiple cores, but do not know how to see what is happening on each of them. What is the right way to access this information?

More concretely, I am planning the following: I imagine writing a little program that launches several threads, these threads should then be spread over the cores and communicate with each other where needed. I would like to be able to observe where which thread is running, any hints?

I am completely new to trying this kind of thing (I would also be quite happy with any information related to this that does not directly address this question).

Best, Bart

(googling only got me to "cat /proc/cpuinfo", which seems to tell me I have 7 processors. This is not a true fact, which tells me I do not know how to read this output.)

share|improve this question
add comment

2 Answers 2

up vote 2 down vote accepted

Number of processors: Please note that numbering starts from 0 so if I have processor 1 I have at least 2 of them

Bounding to threads Threads will not be bound to CPU's (unless you do tricks by using cpulimit & co. - in most cases you shouldn't). Assuming that you have configured kernel with 1000Hz and there is no I/O the context switch is done 1000 times per second. Now in my 2 Processors setup I have around 300 threads running at this moment which means that 6.(6) slots will be allocated per process. It is safe to assume that in any second each process will occupy the both core.

Given - the staircase algorithms, I/O make it not necessary true but each thread at one second may be on both cores - as well as running nowhere as it is waiting (so it is not bounded to any core).

Hyperthreading I guess that you have 4 core Intel iX with hyperthreading (unless you have dual processor machine). Hyperthreading works by emulating a separate core. So if process is waiting on one core for I/O it can switch to executing another - which of course complicate matter further.

share|improve this answer
2  
Threads can be bound to a specific CPU with taskset(1) sched_setaffinity(2) or the pthread functions with similar names. It's something you should do only in very specific circumstances (e.g. on a special-purpose device with particular responsiveness objectives) or for testing. Most of the time, the kernel will decide which CPU to run a thread on better than a human could. –  Gilles Oct 4 '10 at 21:12
    
Yes - I know. That was what I intend to write but I wrote only about cpulimit by accident. Fixed by adding "& co.". –  Maciej Piechotka Oct 4 '10 at 21:23
    
Is the conclusion from this that there is no way to track how my software is running on the hardware? –  kasterma Oct 5 '10 at 16:51
    
To large extend - yes. The threads are not usually bound to cores and most of them will not be runned at any particular moment. –  Maciej Piechotka Oct 5 '10 at 21:29
add comment

Each core gets its own entry in /proc/cpuinfo, so there is a lot of redundant information here.

To get an overview of how many cpus and/or cores you have, do something like this:

# egrep 'processor|core id|physical id' /proc/cpuinfo
processor   : 0
physical id : 0
core id     : 0
processor   : 1
physical id : 0
core id     : 1

This is run on my single-chip, dual-core computer. So I have two cores, numbered 0 and 1, but both of them belong to the physical cpu 0.

Computer guys like to count from 0, remember.

Here's the output from a dual-cpu, single-core system:

$ egrep 'processor|core id|physical id' /proc/cpuinfo
processor       : 0
processor       : 1

So I have two CPUs, but since no cores are specified I know that each processor chip has a single core.

Something a bit more interesting:

$ egrep 'processor|core id|physical id' /proc/cpuinfo
processor       : 0
physical id     : 0
core id         : 0
processor       : 1
physical id     : 0
core id         : 1
processor       : 2
physical id     : 1
core id         : 0
processor       : 3
physical id     : 1
core id         : 1

Here's my dual-cpu dual-core system. I have "processors" 0-3, and each lives on processor 0 or 1, as core 0 or 1.

You get the idea.

All that said, I suspect you have a dual-cpu quad-core system, with processors 0 through 7.

share|improve this answer
    
With that explanation, I have 1 cpu with 8 cores (an Intel(R) Xeon(R) CPU, W3520 @ 2.67GHz). Unfortunately this explains my hardware to me, but not how I can track my software running on it in more detail. –  kasterma Oct 5 '10 at 16:50
    
"Computer guys like to count from 0, remember". I'd say computers themselves like to count from zero ;-) –  Josh Dec 19 '10 at 21:50
2  
That's a quibble, computers do what we tell them to do. –  David Mackintosh Dec 20 '10 at 4:05
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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