简体   繁体   English

如何在多线程应用程序中衡量负载平衡

[英]How do I measure load balancing in a multithreaded application

I'm writing a multithreaded C#/.Net application to control an industrial process. 我正在编写一个多线程C#/。Net应用程序来控制工业过程。 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 FWIW我正在使用VS2010专业版

You can measure CPU usage by using PerformanceCounter class. 您可以通过使用PerformanceCounter类来测量CPU使用率。

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). 如果您的负载在多个CPU内核之间得到了很好的平衡,那么如果没有瓶颈(例如磁盘或网络访问),则全局CPU应该以100%的速度运行。 If some thread waits for resources, or is blocked, you should notice it at the CPU usage level. 如果某个线程正在等待资源或被阻塞,则应在CPU使用率级别上注意到它。

But just using taskmgr.exe is really easier during debugging process. 但是在调试过程中,仅使用taskmgr.exe确实很容易。

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. 如果您希望对线程等待进行细粒度的分析,则可以通过注册一个等待线程超时通知的WaitOrTimerCallback来挂接ThreadPool。 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) 但是请记住,线程有时会等待是很正常的,您想要的是最大化CPU使用率,而不是最小化线程等待(是的,在最坏的情况下,您可以使CPU充满线程操作,但是如果您坚持使用相同的数字比逻辑CPU内核的线程数大,就可以了)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM