views:

3793

answers:

8

I have a quad core system with third party application that once in a while spins several processes (always the same executable but several instances of it) and takes 100% of CPU time. I also have a couple of web services running on the same box (IIS and third party).

The problem with the all cores being busy is that it makes this third party web server to timeout (IIS works fine though, just slower than usual). I have no control over third party web server, it's a part of the bigger product and has to be operational. So, I tried to play with processor affinity (via SysInternals Process Explorer) and limit those pesky processes to 3 cores out of 4 and dedicate the 4th core to the third party web server, and it seems to work quite well.

The problem is that it only sets affinity on the running process and not on executable level, so after those processes finish and later respawn as a new processes it's all the same again - they take all 4 cores. So, I've googled about this ImageCfg.exe utility from Microsoft but I can't find it on Microsoft webside for download and I see that some people tried it and now complain that it doesn't really work.

Is there a way to stick the affinity to the executable?

+3  A: 

One feature of Process Lasso is to set the affinity of a process whenever that process is launched.

Jordan Miner
+2  A: 

Use SetProcessAffinityMask(). And beware, Processor Affinity is inherited!

You'll need to use ImageFileExecutionOptions, specifically the "Debugger" option, and write your own small executable that calls SetProcessAffinityMask() on itself, and then spawns a new process, which is the one you want to set affinity for. Set that as the debugger, and you're done.

jeffamaphone
Really read the documentation on this one. You can easily shoot yourself in the foot with this; lots of gotchas.
jeffamaphone
+3  A: 

You may want to look at the /AFFINITY parameter to start.

From the help:

AFFINITY    The new application will have the specified processor
            affinity mask, expressed as a hexadecimal number.

As processor affinity on Windows is a bitmask you may need some experimentation but I'd assume 1 being the first core, therefore 7 being the first three cores and F being all four. Or 8 for only the fourth.

You can then replace scheduled tasks or shortcuts with a call to start with the appropriate parameters.

Joey
+2  A: 

You can use the single-proc affinity application shim to force one processor on the executable level, which will force the process onto one core.

This article, http://msdn.microsoft.com/en-us/library/bb173458.aspx, has a paragraph on enabling the shim towards the bottom.

Michael
A: 

You could try setting the priority of the process so that even if it decides to use 100% of the CPU, something that is higher priority can take over when it needs to do so.

Doing this automatically (rather than having to play in the task manager) is something I asked about a while ago.

+1  A: 

The ImageCfg.exe utility does work. I just used it to solve a company issue today. It is available from http://www.robpol86.com/pages/imagecfg.php

Imagecfg -a 0x3 xxx.exe

limits the .exe to CPU0 and CPU1, for example.

Steve Sims
Don't know why, but it didn't work in my case.
bychkov
A: 

Steve: I cannot find imageCfg.exe utility. The link provided is no longer valid. "It is available from http://www.robpol86.com/pages/imagecfg.php"

Lori Miller
+1  A: 

http://waynes-world-it.blogspot.com/2009/06/processor-affinity-on-windows-server.html

PowerShell

Use PowerShell to set the processor affinity for one or more running processes. There’s an example script below, setting the processor mask of calc.exe to the first 4 processors. I like this method because the script is simple, it would be easy to schedule, works on x86 and x64, supports multiple processes of the same name and at least partly because it highlights just how easy administration with PowerShell is.

Note that if you use factorial of a large number with calc.exe (n!) you’ll generate100% CPU which can be useful for testing. The mask below is 0xf = 1111 – a mask allowing use of only the first four processors:

$calcSet = Get-Process -ProcessName "calc"
foreach ($calc in $calcSet) {$calc.ProcessorAffinity=0xF}
nex2hex