Valgrind – dynamic code analysis tool – part IV – DRD

Valgrind – DRD – while Helgrind is the major tool for concurrency issue detection, some options of DRD are also helpful on debugging concurrency issue, especially –exclusive-threshold, used to report mutex locked for long, and –shared-threshold, used to report shared rwlock locked for long, by certain thread.

# DRD
http://valgrind.org/docs/manual/drd-manual.html
--tool=drd
--read-var-info=yes
--exclusive-threshold=<n>
--shared-threshold=<n>
[root@localhost tests]# cat hold_lock.c
/** Hold several types of synchronization objects locked as long as specified.
 */

#define _GNU_SOURCE 1

#include <assert.h>
#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>


static void delay_ms(const int ms)
{
 struct timespec ts;

 assert(ms >= 0);
 ts.tv_sec = ms / 1000;
 ts.tv_nsec = (ms % 1000) * 1000 * 1000;
 nanosleep(&ts, 0);
}

int main(int argc, char** argv)
{
 int interval = 0;
 int optchar;
 pthread_mutex_t mutex;
 pthread_mutexattr_t mutexattr;
 pthread_rwlock_t rwlock;

 while ((optchar = getopt(argc, argv, "i:")) != EOF)
 {
 switch (optchar)
 {
 case 'i':
 interval = atoi(optarg);
 break;
 default:
 fprintf(stderr, "Usage: %s [-i <interval time in ms>].n", argv[0]);
 break;
 }
 }

 fprintf(stderr, "Locking mutex ...n");

 pthread_mutexattr_init(&mutexattr);
 pthread_mutexattr_settype(&mutexattr, PTHREAD_MUTEX_RECURSIVE);
 pthread_mutex_init(&mutex, &mutexattr);
 pthread_mutexattr_destroy(&mutexattr);
 pthread_mutex_lock(&mutex);
 delay_ms(interval);
 pthread_mutex_lock(&mutex);
 pthread_mutex_unlock(&mutex);
 pthread_mutex_unlock(&mutex);
 pthread_mutex_destroy(&mutex);

 fprintf(stderr, "Locking rwlock exclusively ...n");

 pthread_rwlock_init(&rwlock, 0);
 pthread_rwlock_wrlock(&rwlock);
 delay_ms(interval);
 pthread_rwlock_unlock(&rwlock);
 pthread_rwlock_destroy(&rwlock);

 fprintf(stderr, "Locking rwlock shared ...n");

 pthread_rwlock_init(&rwlock, 0);
 pthread_rwlock_rdlock(&rwlock);
 delay_ms(interval);
 pthread_rwlock_rdlock(&rwlock);
 pthread_rwlock_unlock(&rwlock);
 pthread_rwlock_unlock(&rwlock);
 pthread_rwlock_destroy(&rwlock);

 fprintf(stderr, "Done.n");

 return 0;
}
[root@localhost valgrindTest]# gcc -Wall -g -lpthread -o hold_lock hold_lock.c
[root@localhost valgrindTest]# valgrind --tool=drd --exclusive-threshold=50 --shared-threshold=100 ./hold_lock -i 150
==32519== drd, a thread error detector
==32519== Copyright (C) 2006-2011, and GNU GPL'd, by Bart Van Assche.
==32519== Using Valgrind-3.7.0 and LibVEX; rerun with -h for copyright info
==32519== Command: ./hold_lock -i 150
==32519==
Locking mutex ...
==32519== Acquired at:
==32519== at 0x400D21B: pthread_mutex_lock (drd_pthread_intercepts.c:615)
==32519== by 0x80489F1: main (hold_lock.c:51)
==32519== Lock on mutex 0xbe9b79a4 was held during 158 ms (threshold: 50 ms).
==32519== at 0x400D7C4: pthread_mutex_unlock (drd_pthread_intercepts.c:665)
==32519== by 0x8048A1D: main (hold_lock.c:55)
==32519== mutex 0xbe9b79a4 was first observed at:
==32519== at 0x401133F: pthread_mutex_init (drd_pthread_intercepts.c:576)
==32519== by 0x80489DB: main (hold_lock.c:49)
==32519==
Locking rwlock exclusively ...
==32519== Acquired at:
==32519== at 0x4010846: pthread_rwlock_wrlock (drd_pthread_intercepts.c:1134)
==32519== by 0x8048A6B: main (hold_lock.c:61)
==32519== Lock on rwlock 0xbe9b7980 was held during 153 ms (threshold: 50 ms).
==32519== at 0x401112F: pthread_rwlock_unlock (drd_pthread_intercepts.c:1221)
==32519== by 0x8048A81: main (hold_lock.c:63)
==32519== rwlock 0xbe9b7980 was first observed at:
==32519== at 0x401034C: pthread_rwlock_init (drd_pthread_intercepts.c:1080)
==32519== by 0x8048A60: main (hold_lock.c:60)
==32519==
Locking rwlock shared ...
==32519== Acquired at:
==32519== at 0x4010666: pthread_rwlock_rdlock (drd_pthread_intercepts.c:1116)
==32519== by 0x8048ACF: main (hold_lock.c:69)
==32519== Lock on rwlock 0xbe9b7980 was held during 154 ms (threshold: 100 ms).
==32519== at 0x401112F: pthread_rwlock_unlock (drd_pthread_intercepts.c:1221)
==32519== by 0x8048AFB: main (hold_lock.c:73)
==32519== rwlock 0xbe9b7980 was first observed at:
==32519== at 0x401034C: pthread_rwlock_init (drd_pthread_intercepts.c:1080)
==32519== by 0x8048AC4: main (hold_lock.c:68)
==32519==
Done.
==32519==
==32519== For counts of detected and suppressed errors, rerun with: -v
==32519== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 0 from 0)
[root@localhost valgrindTest]#

About daveti

Interested in kernel hacking, compilers, machine learning and guitars.
This entry was posted in Static Code Analysis and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s