0

I'm writing a multithreaded C#/.Net application to control an industrial process. There are many compute-intensive tasks that need to be performed and many of them could be done in parallel with very little contention for shared objects. So this is a good opportunity to spawn multiple threads to improve performance.

How do I measure/monitor/test thread load performance to ensure that I'm balancing the work optimally among all the threads I create, so all the threads are fully utilizing the maximum time-slice they get from the scheduler and they don't spend much time stopped or waiting?

FWIW I'm using VS2010 Professional edition

user316117
  • 7,971
  • 20
  • 83
  • 158

1 Answers1

0

You can measure CPU usage by using PerformanceCounter class.

If your load is well balanced across multiples CPU cores, then your global CPU should run at 100%, if there is no bottleneck (disk or network access for example). If some thread waits for resources, or is blocked, you should notice it at the CPU usage level.

But just using taskmgr.exe is really easier during debugging process.

If you want a fine grained analysis of the Thread waits, you can hook on the ThreadPool by registering a WaitOrTimerCallback to be notified when a Thread times out. But keep in mind it's normal for a thread to wait sometimes, what you want is to maximize CPU usage, not minimize Thread waits (Yes, at worst case, you can saturate a CPU with thread operations, but if you stick with the same number of thread than logical CPU cores, it'll be fine)

Community
  • 1
  • 1
rducom
  • 7,072
  • 1
  • 25
  • 39
  • I want to make sure that each thread is getting a relatively equal chunk of the load. Since spawning and stopping threads has some overhead, if I have 10 tasks to perform, say, 1 big one and 9 little ones it might be more efficient to to just use 2 threads, one for the big one and have the second thread do all the little ones rather than respawning a new thread for each little task. So I do need a fairly fine-grained analysis of what each thread is doing. I don't see how I could get this by just watching CPU utilization. – user316117 Jan 30 '15 at 19:30
  • If you have one main Task producing data, and 9 others task consuming this Data, I think it's not a threading problem, but an architecture design one. Just with async Tasks, and without talking about Threads, it's really easy to saturate CPU usage, having 99% of time used by the work the Tasks are made for. I don't think custom Threading is the good way to go, it will be hard to debug and maintain. Tasks are lighter, and easier to manage than Threads. Concurrent collection and Parrallel.For() have a very little overhead, and are well designed for producer-consumer scenarios. – rducom Jan 30 '15 at 19:48