Comments (6)How to determine if a computer is running multiple CPUs in a Silverlight, RIA or web page situation

Added by mike | February 9th, 2009 | 13:02
Categories:   code   for developers

Abstract – How can you tell if the computer you’re running on has more than one CPU?

Now that most new computes have multiple processors we are seeing a rapid rise in the usage of parallel programming. Many libraries supply efficient code for computers with multiple cores, but how can you tell if the computer you are running on indeed has more than one CPU?

Why is the need to identify multiple CPUs becoming a pressing issue now?

Most programs today are multithreaded. More and more libraries are written for effective multithreaded code, with the assumption that this code will be run on computers with multiple cores. The need to know whether the computer is multi-core is imperative yet in certain cases, like RIAs and web applications, you can’t simply “ask” the operating system outright how many cores it is using.

How are multiple CPUs identified “normally”?

When running in the .NET environment you can easily get the exact number of processors by using the following code:

System.Environment.ProcessorCount;

The bad news is that this property isn’t available for situations where you don’t have permission to ask the operating system about the number of processors the system is running. This is exactly the situation you are in when you are developing web pages and RIAs based on MS Silverlight or Adobe Flash.

How can multiple CPUs be identified in a Silverlight application, or similar RIA situation?

I wrote the following code for determining whether a computer has multiple cores using only the basic operations available in any language and on any platform. The code runs two threads in tandem and checks if they can really run simultaneously.

SpinWait – The magic key

Roughly defined a thread’s ‘Quanta’ is the minimum time that the operating system will dedicate to a thread without making a context switch. A quanta is usually somewhere between 10 and 15 milliseconds long. The code I wrote keeps the processors busy for the amount of instructions given (like creating an empty loop with an increased index), however unlike ‘Sleep’ this code prevents context switching from the thread unless it spins in excess of the thread’s quanta.

Here is the code I used:

public static bool MoreThanOneCPU()
        {
         // will indicate that the computer has more than one processor
         bool moreThanOne = false;
         // will close the other thread eventually
         bool toContinue = true;
         ManualResetEvent m = new ManualResetEvent(false);
         // open the secondary thread
         Thread t = new Thread(new ThreadStart(delegate
         {
             // make sure it has time to start
             m.Set();
             // set the flag while not signaled to stop
             while (toContinue)
                 moreThanOne = true;
         }));
         t.IsBackground = false;
         t.Start();
         // waiting for the thread to start
         m.WaitOne();
         // forcing context switch
         Thread.Sleep(0);
         // begining of the threads quanta
         moreThanOne = false;
         // spin long enough for the other thread to set the flag
         // but still shorter than the quanta
         Thread.SpinWait(100000);
         // stop the secondary thread
         toContinue = false;
         // return the result
         return moreThanOne;
}

Note: The method described here isn’t deterministic as the OS could demand a context switch due to a system interrupt, or for some other reasons, in which case the code might report erroneously that the computer has more than one core.

What the code does

The code opens a thread dedicated to repeatedly setting a boolean flag, with the idea of having this thread run on a secondary core, if one exists. In order to ensure that the thread has started and is ready to loop a ‘wait’ handle is used. The main thread waits for the secondary thread to start and then calls ‘Thread.Sleep(0)’ to force the context switch. This ensures the next instruction will execute at the beginning of the threads’ quanta. In order to allow the other thread to set the flag on the other core while we are still spinning we ‘SpinWait’ 100000 instructions. Since the ‘SpinWait’ is shorter than the thread’s quanta it won’t force a context switch. Now we can examine our outcome: If the flag has been set during the elapsed time we know it must have been set by another processor that processed the thread while the other process was busy spinning.

Implementation and usage:

I developed the code presented above while I was researching the performance of Headup, our Semantic Web Silverlight application. I discovered we were wasting a lot of time on mandatory locks that, although required, didn’t justify the accrued overhead.

Spinlock – good value, low cost

After some research I decided to boost our application’s performance by using the ‘SpinLock’ implementation, which utilizes the computer’s two cores to make an efficient lock. I was pleasantly surprised to discover the implementati0n required very little effort.

SpinLock’s secret is that instead of using a system call, like ‘lock’ does, it uses ‘SpinWait()’ to let the other core release the lock. Of course if the computer hasn’t got another core, this is useless. In order to know whether I could use ‘SpinLock’ effectively I first needed to validate that the machine I was running had more than one processor. Hence the code above…

Examples and Notes:

Ideas for the future:

  • Because of the risk of false positives it’s advisable to run the method several times and use the most frequent outcome. This will certainly improve results.
  • By using multiple threads it should be possible to discover the exact amount of processors and not merely whether the machine has one or more.

Thanks go to Yogi for writing this post.

6 Responses to “How to determine if a computer is running multiple CPUs in a Silverlight, RIA or web page situation”

  1. Rachel - computer hardware Says:

    Wow! This post is very informative. Nice! I’ve learned another thing – the best ways on how to identify if a computer is running on multiple CPUs. Thanks for sharing!

  2. Silverlight Travel » Multiple CPUs in a Silverlight Says:

    [...] more [...]

  3. Adi R Says:

    I am still unclear as to why would you do this? Especially inside a framework that doesn’t let you natively control how many threads your software is spawning (such as Adobe Flash).

    Also, I really dislike software which “assumes” so many things (OS or CPU of tomorrow, running on 16 cores, may do context switching differently), and still ends up with “false positives”…

  4. mike Says:

    Hi Adi,
    Thanks for commenting!

    In Silverlight I have exact control on the running threads.
    You can only benefit by knowing what OS is running & how many CPUs are available.

    I could ignore this, and assume always that the computer has one CPU, but I’d sacrifice a great deal of performance.

    I like the concept of a software aware of its environment. Similar to how’s .NET’s JIT complies the IL specifically to the processor on the running computer, I think software can and should be optimized in run time.

    Cheers,
    Mike
    “I tweet @headup”

  5. charlie Says:

    i’m struggling!!! i have “1″ processor but when i use programs to detect my comps spec they all say i’m running 2!!! what does that mean? and how do i fix it?

  6. TalM Says:

    Hi Charlie,
    it is possible that you processor supports hyper-threading:
    http://en.wikipedia.org/wiki/Hyper-threading
    if this is the case, the Operating System treats your processor as 2 processors. (nothing to fix, this is a good behavior)

    Cheers,
    Tal

Leave a Reply