[英]Mutex Lock wait time and watchdog
We are measuring a performance scale of multi threaded real time application. 我们正在衡量多线程实时应用程序的性能规模。 I need to write a adapter to measure the time spent on
我需要编写一个适配器来衡量花费的时间
and also component like locktimer or watchdog timer. 以及locktimer或看门狗定时器之类的组件。 If a thread holding a mutex lock for more than a configured time.. Have to notify to error log..
如果线程持有互斥锁的时间超过配置的时间。必须通知错误日志。
Any best way of doing this..? 有什么最好的方法吗?
You can take time(using standard ctime) before calling your mutex and after you get the mutex. 在调用互斥锁之前和获取互斥锁之后,您可以花一些时间(使用标准ctime)。 Difference between those two will give you approximate time your thread waited to get mutex.
两者之间的差异将为您提供线程等待获取互斥体的大致时间。
Similar process can be done for process 2 to find critical section execution time. 可以对过程2执行类似的过程,以找到关键部分的执行时间。
Maybe the RAII idiom helps you. 也许RAII惯用语可以帮助您。 For example:
例如:
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;
};
Then use the class: 然后使用该类:
//your code
{//start of critical section
MutexHolder lock;
//code guarded by mutex locking
}//here the destructor of the MutexHolder object will call automatically
Something easy you can do is using sort of statistical counters. 您可以轻松完成一件事情,即使用某种统计计数器。 First define how many counters you need... say 10
首先定义您需要多少个计数器...说10
int timecounters[10];
then use any timer you have... finer granularity and lower overhead are of course best... for example clock()/GetTickCount()/QueryPerformanceCounter/rdtsc. 然后使用您拥有的任何计时器...更好的粒度和更低的开销当然是最好的...例如clock()/ GetTickCount()/ QueryPerformanceCounter / rdtsc。 Finally use a stopwatch class like the following one
最后使用秒表类,如下所示
struct StopWatch
{
int n;
StopWatch(int n) : n(n) { timecounters[n] -= timer(); }
~StopWatch() { timecounters[n] += timer(); }
};
then for every section of code you need to trace write 然后对于代码的每个部分,您都需要跟踪编写
{
StopWatch sw(1);
// code to be instrumented
}
At the end of execution of the program you'll have the total time spent in the various instrumented sections and the overhead should be quite low. 在程序执行结束时,您将获得在各个检测部分中花费的总时间,并且开销应该非常低。 It's also easy to add a limit check on single execution time... for example:
在单个执行时间上添加限制检查也很容易...例如:
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;
}
};
Of course the WatchDog
class will never interrupt an activity if it takes longer than it should... it will just report the problem at the end of the activity. 当然,如果
WatchDog
类花费的时间比应该花费的时间长,它将永远不会中断活动……它只会在活动结束时报告问题。 A true interrupting general watchdog class is much more complex to implement. 真正的中断常规看门狗类的实现要复杂得多。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.