views:

327

answers:

7

When I run a single-threaded program that i have written on my quad core Intel i can see in the Windows Task Manager that actually all four cores of my CPU are more or less active. One core is more active than the other three, but there is also activity on those. There's no other program (besided the OS kernel of course) running that would be plausible for that activitiy. And when I close my program all activity an all cores drops down to nearly zero. All is left is a little "noise" on the cores, so I'm pretty sure all the visible activity comes directly or indirectly (like invoking system routines) from my program.

Is it possible that the OS or the cores themselves try to balance some code or execution on all four cores, even it's not a multithreaded program? Do you have any links that documents this technique?

Some infos to the program: It's a console app written in Qt, the Task Manager states that only one thread is running. Maybe Qt uses threads, but I don't use signals or slots, nor any GUI.

Link to Task Manager screenshot: http://img97.imageshack.us/img97/6403/taskmanager.png

This question is language agnostic and not tied to Qt/C++, i just want to know if Windows or Intel do to balance also single-threaded code on all cores. If they do, how does this technique work?

All I can think of is, that kernel routines like reading from disk etc. is scheduled on all cores, but this won't improve performance significantly since the code still has to run synchronous to the kernel api calls.

EDIT

Do you know any tools to do a better analysis of single and/or multi-threaded programs than the poor Windows Task Manager?

A: 

Yes unless you utilize "processor affinity" threads (one or many) will be scheduled on whatever processor the kernel thread management code assigns. There is no guarantee this will be the same processor from one instruction to the next

ennuikiller
+1  A: 

What version of Windows, XP and before have a terrible thread scheduler that actually will jump a single threaded task from core to core. And under Windows (like every other OS) there are dozens if not hundreds of non-user programs running at one time regardless of what you think YOU are running that can account for all that activity.

fuzzy lollipop
When my programm is not running, there's no significant activity on all cores. I'm sure that all visible activity comes directly or indirectly from my program.
Wolfgang Plaschg
@Wolfgang - your process may be causing activity in other threads. For instance, if you are drawing to the screen you are causing work in the DWM process.
Michael
This is what I meant with "indirectly", like API calls or invoking all sort of system routines.
Wolfgang Plaschg
+5  A: 

A single threaded process will only ever run on a single core at any given moment in time.

But it's possible for the OS to migrate a thread from one core to another. While the OS will try to keep a thread on the same core, if the last core the thread ran on is busy and another core is free, the OS can migrate the thread.

R Samuel Klatchko
yes of course unless you are working with quantum processors, you can only have 1 processor run a certain thread at a given point in time. However, over an interval of time that single thread can be migrated to other cpu!
ennuikiller
older version of Windows (XP and earlier) DO NOT try to keep a process on the same core, they do the exact opposite of this by default.
fuzzy lollipop
It is *possible* for the OS scheduler to migrate a proc (more accurately, a thread) from one core to another, but it is highly unlikely. I don't know Qt, but it seems to me a more likely explanation is that there are other threads behind the scenes in Qt, getting scheduled on other cores. That activity, plus the activity of the kernel and other processes, shows up on the other cores.
Cheeso
A: 

Despite what you may perceive, there is a lot more goign on in the CPU than just your application. The OS, and all its processes and threads are also running. what you're seeing is your process being scheduled on different cores at different times. Your application is not being parallelized - its just one of many tasks that are currently executing. Be assured your app is running sequentially.

cyberconte
regarding *what you're seeing is your process being scheduled on different cores at different times.* unlikely. It's possible to migrate a thread from one core to another, but it shouldn't happen normally. I agree with "there's a lot more going on that just your app."
Cheeso
I beg to differ. I guarantee that at some point the application will get context switched out - either due to IO wait or 4+ processes wanting CPU time at the same time, or any other myriad of reasons a scheduler does a context switch - and when it gets switched back in, it will go to the core the OS deems most appropriate.
cyberconte
+2  A: 

The tool that you want to do deeper analysis than Task Manager can is called perfmon (perfmon.exe) - it's a UI for capturing and graphing the performance counters which instrument Windows. You can use it to monitor all sorts of information on both a system and per-process level; learn to love it - it's one of a developer's best friends.

http://technet.microsoft.com/en-us/library/bb490957.aspx

aalpern
A: 

A better Windows Task Manager is sysinternals process explorer.

In Windows Task Manager (or in sysinternals process explorer), right-click on your process name, and set (choice) the affinity core, you should see now only one core active...

Which Windows do you use ? XP ? Seven ?

If you use XP, try Seven, cores are better managed.

vic
A: 

I use Windows 7 x64 Home Premium and an Intel i7 920 CPU and single threaded programs jump from core to core so I don't see how it is an improvement over XP.

7 does recognize HT (Hyper Threading) and doesn't schedule multiple tasks to the one CPU Core, that is probably the only benefit 7 has over XP. I did not have a multi core processor with XP for long so not sure if XP took into account HT.

Franpa