[英]Mutex Lock wait time and watchdog
我们正在衡量多线程实时应用程序的性能规模。 我需要编写一个适配器来衡量花费的时间
以及locktimer或看门狗定时器之类的组件。 如果线程持有互斥锁的时间超过配置的时间。必须通知错误日志。
有什么最好的方法吗?
在调用互斥锁之前和获取互斥锁之后,您可以花一些时间(使用标准ctime)。 两者之间的差异将为您提供线程等待获取互斥体的大致时间。
可以对过程2执行类似的过程,以找到关键部分的执行时间。
也许RAII惯用语可以帮助您。 例如:
class MutexHolder
{
public:
MutexHolder()
{
//here you should take start time
m_mutex.lock();
//here you should take time and find a differnce between start time
}
~MutexHolder()
{
m_mutex.unlock();
//here you should track the time spent between mutex.lock -> mutex.unlock and do smth else
}
private:
Mutex m_mutex;
};
然后使用该类:
//your code
{//start of critical section
MutexHolder lock;
//code guarded by mutex locking
}//here the destructor of the MutexHolder object will call automatically
您可以轻松完成一件事情,即使用某种统计计数器。 首先定义您需要多少个计数器...说10
int timecounters[10];
然后使用您拥有的任何计时器...更好的粒度和更低的开销当然是最好的...例如clock()/ GetTickCount()/ QueryPerformanceCounter / rdtsc。 最后使用秒表类,如下所示
struct StopWatch
{
int n;
StopWatch(int n) : n(n) { timecounters[n] -= timer(); }
~StopWatch() { timecounters[n] += timer(); }
};
然后对于代码的每个部分,您都需要跟踪编写
{
StopWatch sw(1);
// code to be instrumented
}
在程序执行结束时,您将获得在各个检测部分中花费的总时间,并且开销应该非常低。 在单个执行时间上添加限制检查也很容易...例如:
struct WatchDog
{
int n, limit, start;
WatchDog(int n, int limit) : n(n), limit(limit)
{
start = timer();
}
~WatchDog()
{
int delta = timer() - start;
if (delta > limit)
{
log("WatchDog(%i): Time limit exceeded (%i > %i)",
n, delta, limit);
}
timecounters[n] += delta;
}
};
当然,如果WatchDog
类花费的时间比应该花费的时间长,它将永远不会中断活动……它只会在活动结束时报告问题。 真正的中断常规看门狗类的实现要复杂得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.