• 其他语言



管理您的数字内容
页面和feed选项
打印
收藏此页
Digg此页 | 添加到您的del.icio.us帐号
目录

Performance Regulator 类
Performance Regulator 类是一个用来协助从微软性能监视类中获取信息的简单类,并能调节特定线程。下面为类定义代码。Performance Regulator 类在 cmperfreg.h 中定义,并可通过英特尔数字内容论坛获得。

/* _____________________________________________________

   PerformanceRegulator
   _____________________________________________________ */

class PerformanceRegulator
   {
public:
   PerformanceRegulator();
   ~PerformanceRegulator();
   int GetCounterNames(vector<string> &CounterNames);
   int Regulate(string RegulationId, string Counter);
   int StopRegulating(string RegulationId, string Counter);
   int RegisterThread(string RegulationId, string Counter, int Throttle);
   int Monitor();
   int AddCounterInstance(string sCounterName);
   };

GetCounterNames 会提供一个计数器列表,Performance Regulator 类能够对该计数器列表进行调节。之所以要在这里提及这个方法,是因为应用程序了解需要选择调节什么内容,以及多久调节一次。该方法能够根据不同的可用资源以及应用程序正在处理的不同内容,执行不同操作。初始的列计数器表相当小,但会随着项目的发展而越来越大:

  • 处理器总闲置时间百分比(Processor total percent idle time)
  • 物理磁盘总使用时间百分比(Physical disk total disk percent time)
  • 虚拟内存文件使用百分比(age file percent usage)

Register Thread 方法提供了一种针对某一应用,注册特定线程并监视该线程特定计数器的方法。throttle(分流)值为一个从 0 到 9 的相对值,0 为最大分流(maximum throttling)值,9 是最小分流(minimal throttling)值。

已经被注册,以便对先前已经注册过的计数器进行实际调节或分流(throttling)的线程,会调用 Regulate 方法。通过使用 Performance Regulator 类中的后台线程监视已注册的性能计数器,对线程进行调节的工作将非常简单。如果达到了分流(throttle)值,布尔值变量就会设置为“true”,正像第一个代码片段所显示的那样。资源的调节可通过调用 Regulate 在应用程序中显示出来。Regulate 方法应由需要调节的线程来调用。

if ( Counter.Value < (10 * (MIN_THROTTLE - Counter.Throttle)) )
   Block = true;

实际调节的过程,可通过为一个对象设置一段经过预先计算的时间来实现。针对某个百分比计数器的等待时间的计算,通过如下所示的方法来实现:

if ( MonitoredCounter.Block == true )
      WaitForSingleObject(MonitoredCounter.Semaphore,
      ((MIN_THROTTLE-1) - MonitoredCounter.Throttle) * 100);

在文件 cperfreg.cpp 中可以找到完整的程序。StopRegulating 方法可用于停止监视计数器,该计数器为面向特定已注册线程的后台计数器。
上一部分1  2  3  4  5  下一部分

第 4 页, 共 9 页