What are the reasons why the CPU usage doesn’t go 100% with C# and APM?
- by Martin
I have an application which is CPU intensive. When the data is processed on a single thread, the CPU usage goes to 100% for many minutes. So the performance of the application appears to be bound by the CPU. I have multithreaded the logic of the application, which result in an increase of the overall performance. However, the CPU usage hardly goes above 30%-50%. I would expect the CPU (and the many cores) to go to 100% since I process many set of data at the same time.
Below is a simplified example of the logic I use to start the threads. When I run this example, the CPU goes to 100% (on an 8/16 cores machine). However, my application which uses the same pattern doesn’t.
public class DataExecutionContext
{
public int Counter { get; set; }
// Arrays of data
}
static void Main(string[] args)
{
// Load data from the database into the context
var contexts = new List<DataExecutionContext>(100);
for (int i = 0; i < 100; i++)
{
contexts.Add(new DataExecutionContext());
}
// Data loaded. Start to process.
var latch = new CountdownEvent(contexts.Count);
var processData = new Action<DataExecutionContext>(c =>
{
// The thread doesn't access data from a DB, file,
// network, etc. It reads and write data in RAM only
// (in its context).
for (int i = 0; i < 100000000; i++)
c.Counter++;
});
foreach (var context in contexts)
{
processData.BeginInvoke(context, new AsyncCallback(ar =>
{
latch.Signal();
}), null);
}
latch.Wait();
}
I have reduced the number of locks to the strict minimum (only the latch is locking). The best way I found was to create a context in which a thread can read/write in memory. Contexts are not shared among other threads. The threads can’t access the database, files or network. In other words, I profiled my application and I didn’t find any bottleneck.
Why the CPU usage of my application doesn’t go about 50%? Is it the pattern I use? Should I create my own thread instead of using the .Net thread pool? Is there any gotchas? Is there any tool that you could recommend me to find my issue?
Thanks!