私はおそらく本当に古風ですが、私はあなたのコードを測り、それを自分で測ると言います。たとえば、プログラムに次のようなコードを一時的に追加し、プログラムのpthread_mutex_lock()呼び出しをinstrumented_pthread_mutex_lock()に変更するための検索と置換を行います。
次に、stdoutをファイルにリダイレクトしてプログラムを実行します。その後、ファイルを見てどのスレッドがどのミューテックスを待っていたかを見ることができます。
はおそらくない
#include <stdio.h>
#include <unistd.h>
#include <sys/times.h>
static unsigned long long GetCurrentClockTimeMicroseconds()
{
static clock_t _ticksPerSecond = 0;
if (_ticksPerSecond <= 0) _ticksPerSecond = sysconf(_SC_CLK_TCK);
struct tms junk; clock_t newTicks = (clock_t) times(&junk);
return ((((unsigned long long)newTicks)*(1000000))/_ticksPerSecond);
}
int instrumented_pthread_mutex_lock(pthread_mutex_t * mtx)
{
unsigned long long beforeTime = GetCurrentClockTimeMicroseconds();
int ret = pthread_mutex_lock(mtx);
unsigned long long afterTime = GetCurrentClockTimeMicroseconds();
unsigned long long elapsedTime = (afterTime-beforeTime);
if (elapsedTime > 1000) // or whatever threshold you like; I'm using 1 millisecond here
{
printf("Thread %li took %llu microseconds to acquire mutex %p\n", (long int) pthread_self(), elapsedTime, mtx);
}
return ret;
}
、しかし、あなたはおそらくそれを行うことができます(のprintf()はややあなたのプログラムのタイミングを変更しますが、この目的のために、私はそれがはるかに重要ではないと思う呼び出すことに注意してください)自分自身 - 待機する前にフラグをtrueに設定し、待機した後にfalseに設定します。 – immibis
「リソース」とは、pthreadロック/ミューテックス、または何か他のものを意味しますか? –
@JeremyFriesnerはい、私が "リソース"と言うとき、私はpthreadミューテックスを意味します。 –