Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Process.ProcessorAffinity on a system with 32+ logical cores

Tags:

c#

.net

I'm trying to set the processor affinity on a process on a machine that has 16 total physical processor cores, 32 logical. Before, we were using an int but that will overflow when you have 32 logical cores.

Will using a long, instead of an int, when setting the processor affinity still work?

See the code below.

 try
 {
    string pathToExe = GetPathToExe( jobType );

    long processorAffinity = DetermineProcessorAffinity();

    Process jobProcess = Process.Start( pathToExe, jobId.ToString() );
    if ( jobProcess != null )
    {
       jobProcess.ProcessorAffinity = new IntPtr( processorAffinity );
    }

 }
like image 246
Randall Avatar asked Sep 18 '25 06:09

Randall


2 Answers

Under the hood the ProcessorAffinity property will eventually call the Win32 method SetProcessAffinityMask.

  • http://msdn.microsoft.com/en-us/library/ms686223(v=vs.85).aspx

This method is intended to work with more than 32 processors and this solution should work.

Do be aware though that this approach doesn't work in a 32 bit process. In a 32 bit process the backing value of an IntPtr will still be 32 bits. The IntPtr constructor will silently truncate the long value into a int value and you`ll never be able to set the affinity more the extra processors.

like image 121
JaredPar Avatar answered Sep 19 '25 20:09

JaredPar


From IntPtr documentation:

The IntPtr type is designed to be an integer whose size is platform-specific. That is, an instance of this type is expected to be 32-bits on 32-bit hardware and operating systems, and 64-bits on 64-bit hardware and operating systems.

In other words: It should work on 64 bits systems, but not on 32 bit systems. I guess that's not a problem in your case.

like image 41
larsmoa Avatar answered Sep 19 '25 20:09

larsmoa