开发者

How can I specify processor affinity?

开发者 https://www.devze.com 2023-01-01 11:23 出处:网络
I have an application that\'s having some trouble handling multi-processor systems.It\'s not an app that I have a particular affection for modifying and would like to avoid it if possible.However, I\'

I have an application that's having some trouble handling multi-processor systems. It's not an app that I have a particular affection for modifying and would like to avoid it if possible. However, I'm not above modifying the code if I have to. The application is written in VBA (and hence my inclination to avoid touching it).

We've noticed that the application seems to run pretty smoothly if we set the processor affinity to a single processor using task manager, only manifesting instability when processor affinity isn't set.

I know that I can specify the processor affinity 开发者_JAVA百科of a task using .NET and as such, there lies a possibility of me writing a shell application that could be used to run legacy applications with a specified processor affinity, does anyone have any experience with this and can throw out some ideas as to headaches I'm likely to run into with this approach?

The other question is: Is it in fact possible to modify the core VBA product to handle its own processor affinity? I've never had to handle this with any of my applications natively so this (at this point in time) is completely outside my realm of expertise.

Thanks in advance


One way you can do this is with the Windows Application Compatibility Toolkit.

You give it a way to identify your application (e.g. by .exe location), and then it allows you to specify a number of tweaks. Processor affinity is one of them. It creates a database that holds all your tweaks.

It's per-machine, so it's not a good solution for a widely-distributed application, but if this application only has to run on one machine, it'll save you having to change the code.


You could use Sysinternal's PsExec to start your application with a certain processor affinity, e.g. to bind Access to CPU 1 call

psexec.exe -a 1 "%ProgramFiles%\Microsoft Office\Office12\access.exe"

To answer your second question: As you tagged the question with access-vba I assume that your VBA code is an Access solution. Your VBA code will then be executed in process, i.e. you will have to modify the processor affinity of the access.exe process. It is not possible to modify the affinity of the VBA engine separately.

However, before you specify a processor affinity make sure that you are fully aware of the consequences. Fiddling around with this setting can have undesired effects. For a good example read Raymond Chen's post:

Psychic debugging: Why your expensive four-processor machine is ignoring three of its processors


Old thread, but here is some VBA. It works, but I didnt write it.

Option Compare Database
Option Explicit

Private Declare Function GetProcessAffinityMask Lib "kernel32.dll" (ByVal hProcess As Long, ByRef dwProcessAffinityMask As Long, ByRef dwSystemAffinityMask As Long) As Boolean

Private Declare Function SetProcessAffinityMask Lib "kernel32.dll" (ByVal hProcess As Long, ByVal dwProcessAffinityMask As Long) As Boolean

Private Declare Function GetCurrentProcess Lib "kernel32.dll" () As Long

Public Function SetAffinity()

    Dim hRet As Long
    Dim dwProcMask As Long
    Dim dwSysMask As Long

    hRet = GetProcessAffinityMask(GetCurrentProcess(), dwProcMask, dwSysMask)

    'Set affinity to the first processor
    hRet = SetProcessAffinityMask(GetCurrentProcess(), &H1)

    If hRet <> 0 Then
        MsgBox "Process Affinity successfully set for this application."
    Else
        MsgBox ("Error : " & Err.LastDllError)
    End If

End Function
0

精彩评论

暂无评论...
验证码 换一张
取 消