3

I know how to get the number of physical, and number of logical processors on my machine, but I want to know how many logical processors my application has access to.

For instance, I develop on a quad core machine, but I have a number of single core users out there, and in many instances I "dumb down" the interface, or run into locking problems that a multi-core system never experiences.

So, to that end I have set up VSTS to build my app in either debug or "Debug Single Core". The goal there is basically to set the processor affinity to core "0", which by looking at the windows task manager is working as expected.

My problem is, I just noticed, ( and hind sight says it should have been obvious ), that throughout my code I have Environment.ProcessorCount >= something, which works great for truly single core machines, but doesn't give me a read on my single "logically available core".

How can I get the number of "available" logical cores?

C# preferred

Eric J.
  • 147,927
  • 63
  • 340
  • 553
Russ
  • 12,312
  • 20
  • 59
  • 78
  • Duplicate - see http://stackoverflow.com/questions/188503/detecting-the-number-of-processors. The upshot is to use `Environment.ProcessorCount`. – Jeff Sternal Jan 25 '10 at 18:32
  • 1
    This is what I am using, I don't want the number of total physical, or number of total logical, I want the "Number of available to me logical processors" – Russ Jan 25 '10 at 18:34
  • Hmmm, in that case I may have jumped the gun: how does 'available logical processors' differ from 'total logical processors'? – Jeff Sternal Jan 25 '10 at 18:37
  • I can set the processor affinity on a quad-core system for an application so that it can only use 1, 2, 3, or 4 of the available cores. By default, and application has access to all 4, but in my case, there are times when I want to limit the application to only a single core, but I still physically, and logically have 4 processors on the machine. Environment.ProcessorCount reads from the environment variables, so it’s not affected by any affinity change to the applications – Russ Jan 25 '10 at 18:43
  • 2
    Well, I worked through more of the answeres in the post you linked to right off the bat. Although the accepted answer is not what I am looking for, if you scroll down to Jesse Slicer's answer, that IS what I am looking for. – Russ Jan 25 '10 at 18:50
  • Ah, terrific! Thanks for the update - I'll go learn something myself. :) – Jeff Sternal Jan 25 '10 at 18:53

1 Answers1

5

A special thanks goes out to Jesse Slicer's answer found here.

Although not the accepted answer to the question asked, it IS the answer I am looking for.

Here is basicly what I ended up with based on Jesse's answer.

#if !DEBUG
                return Environment.ProcessorCount;
#endif
                using (Process currentProcess = Process.GetCurrentProcess())
                {
                    var processAffinityMask =
                        (uint) currentProcess.ProcessorAffinity;
                    const uint BitsPerByte = 8;
                    var loop = BitsPerByte*sizeof (uint);
                    uint result = 0;

                    while (loop > 0)
                    {
                        --loop;
                        result += (processAffinityMask & 1);
                        processAffinityMask >>= 1;
                    }

                    return Convert.ToInt32((result != 0) ? result : 1);
                }
Community
  • 1
  • 1
Russ
  • 12,312
  • 20
  • 59
  • 78
  • A note that this currently doesn't work on Mono, as ProcessorAffinity is still to-do. I'd the same basic idea in some code, and was confused as hell why it worked in .NET and not Mono until I realised that I wasn't doing the above wrong, it just wasn't supported. – Jon Hanna Jan 18 '14 at 19:02