3

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.)

2 Answers 2

4

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.

3
  • 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, 2010 at 16:50
  • "Computer guys like to count from 0, remember". I'd say computers themselves like to count from zero ;-)
    – Josh
    Dec 19, 2010 at 21:50
  • 2
    That's a quibble, computers do what we tell them to do. Dec 20, 2010 at 4:05
2

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.

4
  • 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. Oct 4, 2010 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.". Oct 4, 2010 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, 2010 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. Oct 5, 2010 at 21:29

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.