Development/Valgrind: Difference between revisions

From bwHPC Wiki
Jump to navigation Jump to search
m (S Richling moved page Valgrind to Development/Valgrind: Umzung in Developmentbereich)
 
(4 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Softwarepage|devel/valgrind}}

{| width=600px class="wikitable"
{| width=600px class="wikitable"
|-
|-
! Description !! Content
! Description !! Content
|-
|-
| module load
| module load devel/valgrind
| devel/valgrind
|-
|-
| Availability
| Availability
Line 24: Line 25:
<br>
<br>
= Introduction =
= Introduction =
'''Valgrind''' is an instrumentation framework for building dynamic analysis tools. There are Valgrind tools that can automatically detect many memory management and threading bugs, and profile your programs in detail. You can also use Valgrind to build new tools.
'''Valgrind''' is an instrumentation framework for building dynamic analysis tools. The main (default) tool <kbd>memcheck</kbd> detects a wide variety of memory management bugs. This page introduces the various tools to fetch threading bugs, and profile your programs in detail. As such, any programmer '''must''' know valgrind's memcheck.
While the Linux distribution provides valgrind, this module comes with a newer version (which may interpret newer instruction sets) and more tools than the system provided one.
<br>
[http://valgrind.org/info/about.html Click here for more infos about Valgrind]
<br>
<br>
<br>


= Commands =
= Overview =
Valgrind ships with various tools, some of them are described in the following table.
Valgrind ships with various tools, some of them are described in the following table.
{| width=600px class="wikitable"
{| width=600px class="wikitable"
Line 38: Line 37:
|-
|-
| memcheck
| memcheck
| Detects heap array overruns, memory leaks and incorrect freeing of heap memory.
| Detects heap array over- and underruns, usage of uninitialized memory, memory leaks and incorrect freeing of heap memory.
|-
| exp-sgcheck
| Detects stack and global array overruns.
|-
|-
| callgrind
| callgrind
| Memory Profiler. Together with kcachegrind allows getting an overview of applications and getting details about memory access and costs.
| Profiler. Helps finding bottlenecks.
|-
|-
| helgrind
| helgrind
Line 51: Line 47:
| drd
| drd
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.
| Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.
|-
| massif
| A heap memory profiler showing the allocation history over time.
|}
|}
<br>
= Versions and Availability =
A list of versions currently available on all bwHPC-C5-Clusters can be obtained from the
<br>
<big>

[https://cis-hpc.uni-konstanz.de/prod.cis/ Cluster Information System CIS]

</big>
{{#widget:Iframe
|url=https://cis-hpc.uni-konstanz.de/prod.cis/bwUniCluster/devel/valgrind
|width=99%
|height=120
}}
On the command line interface of any bwHPC cluster you'll get a list of available versions
by using the command<br>
''''module avail devel/valgrind''''.
<pre>
$ module avail devel/valgrind # bwForCluster (Justus)
------------------------ /opt/bwhpc/common/modulefiles -------------------------
devel/valgrind/3.10.1</pre>
<font color=red>Attention!</font><br>
The default Linux Valgrind debugger may be a different version (if installed).
<br>
Check the <font color=green>default</font> version by: ''''valgrind --version'''' <font color=red>without loaded valgrind-module</font> (if available).
<pre>
$ valgrind --version # bwUniCluster
valgrind-3.8.1</pre>
<br>
<br>


= Documentation =
= Documentation =
== Online ==
== Online ==
* [http://valgrind.org/docs/ Valgrind Documentation]
[http://valgrind.org/docs/ Online Valgrind Documentation]

== Local ==
== Local ==
<pre>$ man valgrind</pre>
<pre>$ man valgrind</pre>
<br>
<br>

= Usage =
= Usage =
To simulate your program and gather information about it using a specific tool, valgrind can be called like this
To simulate your program and gather information about it using a specific tool, valgrind can be called like this
<pre>$ valgrind --tool=<tool> --log-file=<logfile> ./example</pre>
<pre>$ valgrind --tool=<tool> --log-file=<logfile> ./example</pre>

For example to check for race conditions the command would look like this:
For example to check for race conditions the command would look like this:
<pre>$ valgrind --tool=drd --log-file=log.mem ./example</pre>
<pre>$ valgrind --tool=drd --log-file=log.mem ./example</pre>

Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.
Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution.
Furthermore the tools can give false positives, i.e. they report errors even though the are none.
Furthermore the tools can give false positives, i.e. they report errors even though the are none.
<br>
<br>

<br>
= Example for helgrind and drd =
= Example using drd on OpenMP =
The following simple OpenMP-parallel example shows a data race on global data:
This small program demonstrates how to detect data races in parallel programs. Both helgrind and drd support POSIX Threads (Pthreads) but not OpenMP so prepare to get many false positives when using these tools with OpenMP.

<source lang=c>
#include <stdlib.h>
#include <stdio.h>
#include "omp.h"

int global = 4711;

int main (int argc, char * argv[]) {

// The following is an ERROR: This should be a (thread-)local variable.
#pragma omp parallel
global = omp_get_thread_num();

printf("global:%d\n", global);
return EXIT_SUCCESS;
}
</source>
Compile this example using
<pre>$ gcc -Wall -O2 -fopenmp omp_error.c -o omp_error</pre>
One may optionally pass the <kbd>-g</kbd> to allow Valgrind to print source code lines in the erroneous functions.

Running this example shows:
<pre>$ valgrind --tool=drd ./omp_error
...
==1019408== Thread 2:
==1019408== Conflicting store by thread 2 at 0x00601034 size 4
==1019408== at 0x4006D9: main._omp_fn.0 (openmp_error_access.c:13)
...
==1019408== Allocation context: Data section of /pfs/data5/home/es/es_es/es_rakeller/C/openmp_error_access
==1019408== Other segment start (thread 1)
==1019408== at 0x4C3F3CF: pthread_create_intercept (drd_pthread_intercepts.c:625)
==1019408== by 0x4C3F3CF: pthread_create@* (drd_pthread_intercepts.c:631)
==1019408== by 0x4E84A72: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408== by 0x4E7A6A0: GOMP_parallel (in /usr/lib64/libgomp.so.1.0.0)
==1019408== by 0x4005C3: main (openmp_error_access.c:9)
==1019408== Other segment end (thread 1)
==1019408== at 0x4E86FAA: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408== by 0x4E85A1C: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408== by 0x4005C3: main (openmp_error_access.c:9)
==1019408==
==1019408== Thread 1:
==1019408== Conflicting load by thread 1 at 0x00601034 size 4
==1019408== at 0x4005C4: main (openmp_error_access.c:15)
==1019408== Allocation context: Data section of /pfs/data5/home/es/es_es/es_rakeller/C/openmp_error_access
==1019408== Other segment start (thread 2)
...
</pre>


= Example for helgrind for PThreads =
The following PThread-parallel example demonstrates how to detect data races in parallel programs.


<source lang=c>
<source lang=c>
Line 143: Line 170:
}
}
</source>
</source>
The program can be compiled with
Compile the program using
<pre>$ gcc -Wall pthreads.c -o pthreads -pthread -lm -O2</pre>
<pre>$ gcc -Wall -O2 pthreads.c -o pthreads -pthread -lm</pre>
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.
where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library.
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.
This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely.
Line 192: Line 219:
</pre>
</pre>
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).
The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).
<br>
[[Category:debugger software]][[Category:bwUniCluster]][[Category:bwForCluster_Chemistry]]

Latest revision as of 00:47, 9 December 2022

The main documentation is available via module help devel/valgrind on the cluster. Most software modules for applications provide working example batch scripts.


Description Content
module load devel/valgrind
Availability bwUniCluster | BwForCluster_Chemistry
License GPL
Citing n/a
Links Valgrind Homepage
Graphical Interface No


Introduction

Valgrind is an instrumentation framework for building dynamic analysis tools. The main (default) tool memcheck detects a wide variety of memory management bugs. This page introduces the various tools to fetch threading bugs, and profile your programs in detail. As such, any programmer must know valgrind's memcheck. While the Linux distribution provides valgrind, this module comes with a newer version (which may interpret newer instruction sets) and more tools than the system provided one.

Overview

Valgrind ships with various tools, some of them are described in the following table.

Tool Description
memcheck Detects heap array over- and underruns, usage of uninitialized memory, memory leaks and incorrect freeing of heap memory.
callgrind Memory Profiler. Together with kcachegrind allows getting an overview of applications and getting details about memory access and costs.
helgrind Detects race conditions and deadlocks.
drd Detects race conditions. Does not detect deadlocks but needs less memory than helgrind.
massif A heap memory profiler showing the allocation history over time.


Documentation

Online

Online Valgrind Documentation

Local

$ man valgrind


Usage

To simulate your program and gather information about it using a specific tool, valgrind can be called like this

$ valgrind --tool=<tool> --log-file=<logfile> ./example

For example to check for race conditions the command would look like this:

$ valgrind --tool=drd --log-file=log.mem ./example

Beware that the simulation via valgrind can take much longer and can consume much more memory compared to a normal execution. Furthermore the tools can give false positives, i.e. they report errors even though the are none.

Example using drd on OpenMP

The following simple OpenMP-parallel example shows a data race on global data:

#include <stdlib.h>
#include <stdio.h>
#include "omp.h"

int global = 4711;

int main (int argc, char * argv[]) {

    // The following is an ERROR: This should be a (thread-)local variable.
#pragma omp parallel
    global = omp_get_thread_num();

    printf("global:%d\n", global);
    return EXIT_SUCCESS;
}

Compile this example using

$ gcc -Wall -O2 -fopenmp omp_error.c -o omp_error

One may optionally pass the -g to allow Valgrind to print source code lines in the erroneous functions.

Running this example shows:

$ valgrind --tool=drd ./omp_error
...
==1019408== Thread 2:
==1019408== Conflicting store by thread 2 at 0x00601034 size 4
==1019408==    at 0x4006D9: main._omp_fn.0 (openmp_error_access.c:13)
...
==1019408== Allocation context: Data section of /pfs/data5/home/es/es_es/es_rakeller/C/openmp_error_access
==1019408== Other segment start (thread 1)
==1019408==    at 0x4C3F3CF: pthread_create_intercept (drd_pthread_intercepts.c:625)
==1019408==    by 0x4C3F3CF: pthread_create@* (drd_pthread_intercepts.c:631)
==1019408==    by 0x4E84A72: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408==    by 0x4E7A6A0: GOMP_parallel (in /usr/lib64/libgomp.so.1.0.0)
==1019408==    by 0x4005C3: main (openmp_error_access.c:9)
==1019408== Other segment end (thread 1)
==1019408==    at 0x4E86FAA: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408==    by 0x4E85A1C: ??? (in /usr/lib64/libgomp.so.1.0.0)
==1019408==    by 0x4005C3: main (openmp_error_access.c:9)
==1019408== 
==1019408== Thread 1:
==1019408== Conflicting load by thread 1 at 0x00601034 size 4
==1019408==    at 0x4005C4: main (openmp_error_access.c:15)
==1019408== Allocation context: Data section of /pfs/data5/home/es/es_es/es_rakeller/C/openmp_error_access
==1019408== Other segment start (thread 2)
...


Example for helgrind for PThreads

The following PThread-parallel example demonstrates how to detect data races in parallel programs.

/* pthreads.c */
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <math.h>

#define NUM_THREADS 2

int v = 0;

void* f(void* x)
{
    int j = 0;
    int k = *(int*)x;
    
    for(j = 0; j < 100; j++)
        v += sin(j) + 1 + k;

    return NULL;
}

int main(int argc, char *argv[])
{
    pthread_t threads[NUM_THREADS];
    int idx[NUM_THREADS];
    int t;
   
    for(t=0; t<NUM_THREADS; t++) {
        idx[t] = t;
        pthread_create(&threads[t], NULL, f, (void*)&idx[t]);
    }
    
    for(t=0; t<NUM_THREADS; t++)
        pthread_join(threads[t], NULL);

    printf("%i\n", v);

    pthread_exit(NULL);

    return 0;
}

Compile the program using

$ gcc -Wall -O2 pthreads.c -o pthreads -pthread -lm

where -pthread tells the compiler to link against the Pthreads library and -lm is needed for linking against the math library. This program has a race condition, consecutive executions can give different outputs. Such errors are hard to find because they can show up very rarely. Helgrind can tell you the problematic part of your code:

$ valgrind --tool=helgrind ./pthreads
...
==29237== ----------------------------------------------------------------
==29237== 
==29237== Possible data race during read of size 4 at 0x600C78 by thread #3
==29237== Locks held: none
==29237==    at 0x4007BC: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)
==29237== 
==29237== This conflicts with a previous write of size 4 by thread #2
==29237== Locks held: none
==29237==    at 0x4007FB: f(void*) (in /pfs/data2/home/xx/xxxx/xxxx/pthreads)
==29237==    by 0x4A0C0D4: mythread_wrapper (hg_intercepts.c:219)
==29237==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)
==29237==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)
==29237== 
==29237== ----------------------------------------------------------------
...

Now we know that we have to check the function f for a possible data race. A similar output can be achieved using drd with the following command:

$ valgrind --tool=drd ./pthreads

Valgrind can give even better hints when compiled with debug information (helgrind produces similar output):

$ gcc -g -Wall pthreads.c -o pthreads -pthread -lm
$ valgrind --tool=drd ./pthreads
...
==604== Thread 3:
==604== Conflicting load by thread 3 at 0x00600cc0 size 4
==604==    at 0x400749: f(void*) (pthreads.c:17)
==604==    by 0x4A128B4: vgDrd_thread_wrapper (drd_pthread_intercepts.c:355)
==604==    by 0x3EDA6079D0: start_thread (in /lib64/libpthread-2.12.so)
==604==    by 0x3ED9AE8B6C: clone (in /lib64/libc-2.12.so)
==604== Allocation context: BSS section of fs/data2/home/xx/xxxx/xxxx/pthreads
==604== Other segment start (thread 2)
==604==    (thread finished, call stack no longer available)
==604== Other segment end (thread 2)
==604==    (thread finished, call stack no longer available)
...

The execution can take longer but now we know that the problem is most likely in line 17 (which is correct).