简体   繁体   English

Mutex Lock等待时间和看门狗

[英]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 我需要编写一个适配器来衡量花费的时间

  1. Waiting time for locking the mutex 锁定互斥锁的等待时间
  2. Time spent between mutex.lock -> mutex.unlock : critical section execution time 互斥锁->互斥锁之间花费的时间:关键部分执行时间

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.

相关问题 同时锁定两个互斥锁 - Lock two mutex at same time 是否有可能 2 个线程同时锁定一个互斥锁(使用互斥锁包装器)? - Is it possible for 2 threads lock a mutex at the same time (use mutex wrapper)? 线程锁定互斥锁的速度比std :: conditional_variable :: wait()快 - Threads lock mutex faster than std::conditional_variable::wait() mutable boost :: mutex是否可以分离锁定和等待函数? - mutable boost::mutex is it possible to separate lock and wait functions? 当 pthreads 在 mutex_lock/cond_wait 中等待时会发生什么? - What happens when pthreads wait in mutex_lock/cond_wait? 尝试等待boost :: condition_Variable时出现“ unique_lock没有互斥量:不允许操作”错误 - “unique_lock has no mutex: Operation not permitted” error when attempting to wait on boost::condition_Variable pthread_cond_wait和pthread_mutex_lock无法按预期工作 - pthread_cond_wait and pthread_mutex_lock doesnt work as expected 导致 C++11 std::mutex 将阻塞的线程锁定为被动等待状态? - Leads a C++11 std::mutex lock the blocked thread into a passive wait state? C ++在调用std :: unique_lock wait之前解锁std :: mutex - C++ Unlocking a std::mutex before calling std::unique_lock wait 在互斥锁上使用多个 std::unique_lock,FIFO 中的所有线程等待进程? - Use multiple std::unique_lock on mutex, all threads in FIFO to wait process?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM