1<?xml version="1.0"?> <!-- -*- sgml -*- --> 2<!DOCTYPE chapter PUBLIC "-//OASIS//DTD DocBook XML V4.2//EN" 3 "http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd" 4[ <!ENTITY % vg-entities SYSTEM "../../docs/xml/vg-entities.xml"> %vg-entities; ]> 5 6 7<chapter id="drd-manual" xreflabel="DRD: a thread error detector"> 8 <title>DRD: a thread error detector</title> 9 10<para>To use this tool, you must specify 11<option>--tool=drd</option> 12on the Valgrind command line.</para> 13 14 15<sect1 id="drd-manual.overview" xreflabel="Overview"> 16<title>Overview</title> 17 18<para> 19DRD is a Valgrind tool for detecting errors in multithreaded C and C++ 20programs. The tool works for any program that uses the POSIX threading 21primitives or that uses threading concepts built on top of the POSIX threading 22primitives. 23</para> 24 25<sect2 id="drd-manual.mt-progr-models" xreflabel="MT-progr-models"> 26<title>Multithreaded Programming Paradigms</title> 27 28<para> 29There are two possible reasons for using multithreading in a program: 30<itemizedlist> 31 <listitem> 32 <para> 33 To model concurrent activities. Assigning one thread to each activity 34 can be a great simplification compared to multiplexing the states of 35 multiple activities in a single thread. This is why most server software 36 and embedded software is multithreaded. 37 </para> 38 </listitem> 39 <listitem> 40 <para> 41 To use multiple CPU cores simultaneously for speeding up 42 computations. This is why many High Performance Computing (HPC) 43 applications are multithreaded. 44 </para> 45 </listitem> 46</itemizedlist> 47</para> 48 49<para> 50Multithreaded programs can use one or more of the following programming 51paradigms. Which paradigm is appropriate depends e.g. on the application type. 52Some examples of multithreaded programming paradigms are: 53<itemizedlist> 54 <listitem> 55 <para> 56 Locking. Data that is shared over threads is protected from concurrent 57 accesses via locking. E.g. the POSIX threads library, the Qt library 58 and the Boost.Thread library support this paradigm directly. 59 </para> 60 </listitem> 61 <listitem> 62 <para> 63 Message passing. No data is shared between threads, but threads exchange 64 data by passing messages to each other. Examples of implementations of 65 the message passing paradigm are MPI and CORBA. 66 </para> 67 </listitem> 68 <listitem> 69 <para> 70 Automatic parallelization. A compiler converts a sequential program into 71 a multithreaded program. The original program may or may not contain 72 parallelization hints. One example of such parallelization hints is the 73 OpenMP standard. In this standard a set of directives are defined which 74 tell a compiler how to parallelize a C, C++ or Fortran program. OpenMP 75 is well suited for computational intensive applications. As an example, 76 an open source image processing software package is using OpenMP to 77 maximize performance on systems with multiple CPU 78 cores. GCC supports the 79 OpenMP standard from version 4.2.0 on. 80 </para> 81 </listitem> 82 <listitem> 83 <para> 84 Software Transactional Memory (STM). Any data that is shared between 85 threads is updated via transactions. After each transaction it is 86 verified whether there were any conflicting transactions. If there were 87 conflicts, the transaction is aborted, otherwise it is committed. This 88 is a so-called optimistic approach. There is a prototype of the Intel C++ 89 Compiler available that supports STM. Research about the addition of 90 STM support to GCC is ongoing. 91 </para> 92 </listitem> 93</itemizedlist> 94</para> 95 96<para> 97DRD supports any combination of multithreaded programming paradigms as 98long as the implementation of these paradigms is based on the POSIX 99threads primitives. DRD however does not support programs that use 100e.g. Linux' futexes directly. Attempts to analyze such programs with 101DRD will cause DRD to report many false positives. 102</para> 103 104</sect2> 105 106 107<sect2 id="drd-manual.pthreads-model" xreflabel="Pthreads-model"> 108<title>POSIX Threads Programming Model</title> 109 110<para> 111POSIX threads, also known as Pthreads, is the most widely available 112threading library on Unix systems. 113</para> 114 115<para> 116The POSIX threads programming model is based on the following abstractions: 117<itemizedlist> 118 <listitem> 119 <para> 120 A shared address space. All threads running within the same 121 process share the same address space. All data, whether shared or 122 not, is identified by its address. 123 </para> 124 </listitem> 125 <listitem> 126 <para> 127 Regular load and store operations, which allow to read values 128 from or to write values to the memory shared by all threads 129 running in the same process. 130 </para> 131 </listitem> 132 <listitem> 133 <para> 134 Atomic store and load-modify-store operations. While these are 135 not mentioned in the POSIX threads standard, most 136 microprocessors support atomic memory operations. 137 </para> 138 </listitem> 139 <listitem> 140 <para> 141 Threads. Each thread represents a concurrent activity. 142 </para> 143 </listitem> 144 <listitem> 145 <para> 146 Synchronization objects and operations on these synchronization 147 objects. The following types of synchronization objects have been 148 defined in the POSIX threads standard: mutexes, condition variables, 149 semaphores, reader-writer synchronization objects, barriers and 150 spinlocks. 151 </para> 152 </listitem> 153</itemizedlist> 154</para> 155 156<para> 157Which source code statements generate which memory accesses depends on 158the <emphasis>memory model</emphasis> of the programming language being 159used. There is not yet a definitive memory model for the C and C++ 160languages. For a draft memory model, see also the document 161<ulink url="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2338.html"> 162WG21/N2338: Concurrency memory model compiler consequences</ulink>. 163</para> 164 165<para> 166For more information about POSIX threads, see also the Single UNIX 167Specification version 3, also known as 168<ulink url="http://www.opengroup.org/onlinepubs/000095399/idx/threads.html"> 169IEEE Std 1003.1</ulink>. 170</para> 171 172</sect2> 173 174 175<sect2 id="drd-manual.mt-problems" xreflabel="MT-Problems"> 176<title>Multithreaded Programming Problems</title> 177 178<para> 179Depending on which multithreading paradigm is being used in a program, 180one or more of the following problems can occur: 181<itemizedlist> 182 <listitem> 183 <para> 184 Data races. One or more threads access the same memory location without 185 sufficient locking. Most but not all data races are programming errors 186 and are the cause of subtle and hard-to-find bugs. 187 </para> 188 </listitem> 189 <listitem> 190 <para> 191 Lock contention. One thread blocks the progress of one or more other 192 threads by holding a lock too long. 193 </para> 194 </listitem> 195 <listitem> 196 <para> 197 Improper use of the POSIX threads API. Most implementations of the POSIX 198 threads API have been optimized for runtime speed. Such implementations 199 will not complain on certain errors, e.g. when a mutex is being unlocked 200 by another thread than the thread that obtained a lock on the mutex. 201 </para> 202 </listitem> 203 <listitem> 204 <para> 205 Deadlock. A deadlock occurs when two or more threads wait for 206 each other indefinitely. 207 </para> 208 </listitem> 209 <listitem> 210 <para> 211 False sharing. If threads that run on different processor cores 212 access different variables located in the same cache line 213 frequently, this will slow down the involved threads a lot due 214 to frequent exchange of cache lines. 215 </para> 216 </listitem> 217</itemizedlist> 218</para> 219 220<para> 221Although the likelihood of the occurrence of data races can be reduced 222through a disciplined programming style, a tool for automatic 223detection of data races is a necessity when developing multithreaded 224software. DRD can detect these, as well as lock contention and 225improper use of the POSIX threads API. 226</para> 227 228</sect2> 229 230 231<sect2 id="drd-manual.data-race-detection" xreflabel="data-race-detection"> 232<title>Data Race Detection</title> 233 234<para> 235The result of load and store operations performed by a multithreaded program 236depends on the order in which memory operations are performed. This order is 237determined by: 238<orderedlist> 239 <listitem> 240 <para> 241 All memory operations performed by the same thread are performed in 242 <emphasis>program order</emphasis>, that is, the order determined by the 243 program source code and the results of previous load operations. 244 </para> 245 </listitem> 246 <listitem> 247 <para> 248 Synchronization operations determine certain ordering constraints on 249 memory operations performed by different threads. These ordering 250 constraints are called the <emphasis>synchronization order</emphasis>. 251 </para> 252 </listitem> 253</orderedlist> 254The combination of program order and synchronization order is called the 255<emphasis>happens-before relationship</emphasis>. This concept was first 256defined by S. Adve et al in the paper <emphasis>Detecting data races on weak 257memory systems</emphasis>, ACM SIGARCH Computer Architecture News, v.19 n.3, 258p.234-243, May 1991. 259</para> 260 261<para> 262Two memory operations <emphasis>conflict</emphasis> if both operations are 263performed by different threads, refer to the same memory location and at least 264one of them is a store operation. 265</para> 266 267<para> 268A multithreaded program is <emphasis>data-race free</emphasis> if all 269conflicting memory accesses are ordered by synchronization 270operations. 271</para> 272 273<para> 274A well known way to ensure that a multithreaded program is data-race 275free is to ensure that a locking discipline is followed. It is e.g. 276possible to associate a mutex with each shared data item, and to hold 277a lock on the associated mutex while the shared data is accessed. 278</para> 279 280<para> 281All programs that follow a locking discipline are data-race free, but not all 282data-race free programs follow a locking discipline. There exist multithreaded 283programs where access to shared data is arbitrated via condition variables, 284semaphores or barriers. As an example, a certain class of HPC applications 285consists of a sequence of computation steps separated in time by barriers, and 286where these barriers are the only means of synchronization. Although there are 287many conflicting memory accesses in such applications and although such 288applications do not make use mutexes, most of these applications do not 289contain data races. 290</para> 291 292<para> 293There exist two different approaches for verifying the correctness of 294multithreaded programs at runtime. The approach of the so-called Eraser 295algorithm is to verify whether all shared memory accesses follow a consistent 296locking strategy. And the happens-before data race detectors verify directly 297whether all interthread memory accesses are ordered by synchronization 298operations. While the last approach is more complex to implement, and while it 299is more sensitive to OS scheduling, it is a general approach that works for 300all classes of multithreaded programs. An important advantage of 301happens-before data race detectors is that these do not report any false 302positives. 303</para> 304 305<para> 306DRD is based on the happens-before algorithm. 307</para> 308 309</sect2> 310 311 312</sect1> 313 314 315<sect1 id="drd-manual.using-drd" xreflabel="Using DRD"> 316<title>Using DRD</title> 317 318<sect2 id="drd-manual.options" xreflabel="DRD Command-line Options"> 319<title>DRD Command-line Options</title> 320 321<para>The following command-line options are available for controlling the 322behavior of the DRD tool itself:</para> 323 324<!-- start of xi:include in the manpage --> 325<variablelist id="drd.opts.list"> 326 <varlistentry> 327 <term> 328 <option><![CDATA[--check-stack-var=<yes|no> [default: no]]]></option> 329 </term> 330 <listitem> 331 <para> 332 Controls whether DRD detects data races on stack 333 variables. Verifying stack variables is disabled by default because 334 most programs do not share stack variables over threads. 335 </para> 336 </listitem> 337 </varlistentry> 338 <varlistentry> 339 <term> 340 <option><![CDATA[--exclusive-threshold=<n> [default: off]]]></option> 341 </term> 342 <listitem> 343 <para> 344 Print an error message if any mutex or writer lock has been 345 held longer than the time specified in milliseconds. This 346 option enables the detection of lock contention. 347 </para> 348 </listitem> 349 </varlistentry> 350 <varlistentry> 351 <term> 352 <option><![CDATA[--join-list-vol=<n> [default: 10]]]></option> 353 </term> 354 <listitem> 355 <para> 356 Data races that occur between a statement at the end of one thread 357 and another thread can be missed if memory access information is 358 discarded immediately after a thread has been joined. This option 359 allows to specify for how many joined threads memory access information 360 should be retained. 361 </para> 362 </listitem> 363 </varlistentry> 364 <varlistentry> 365 <term> 366 <option> 367 <![CDATA[--first-race-only=<yes|no> [default: no]]]> 368 </option> 369 </term> 370 <listitem> 371 <para> 372 Whether to report only the first data race that has been detected on a 373 memory location or all data races that have been detected on a memory 374 location. 375 </para> 376 </listitem> 377 </varlistentry> 378 <varlistentry> 379 <term> 380 <option> 381 <![CDATA[--free-is-write=<yes|no> [default: no]]]> 382 </option> 383 </term> 384 <listitem> 385 <para> 386 Whether to report races between accessing memory and freeing 387 memory. Enabling this option may cause DRD to run slightly 388 slower. Notes: 389 <itemizedlist> 390 <listitem> 391 <para> 392 Don't enable this option when using custom memory allocators 393 that use 394 the <computeroutput>VG_USERREQ__MALLOCLIKE_BLOCK</computeroutput> 395 and <computeroutput>VG_USERREQ__FREELIKE_BLOCK</computeroutput> 396 because that would result in false positives. 397 </para> 398 </listitem> 399 <listitem> 400 <para>Don't enable this option when using reference-counted 401 objects because that will result in false positives, even when 402 that code has been annotated properly with 403 <computeroutput>ANNOTATE_HAPPENS_BEFORE</computeroutput> 404 and <computeroutput>ANNOTATE_HAPPENS_AFTER</computeroutput>. See 405 e.g. the output of the following command for an example: 406 <computeroutput>valgrind --tool=drd --free-is-write=yes 407 drd/tests/annotate_smart_pointer</computeroutput>. 408 </para> 409 </listitem> 410 </itemizedlist> 411 </para> 412 </listitem> 413 </varlistentry> 414 <varlistentry> 415 <term> 416 <option> 417 <![CDATA[--report-signal-unlocked=<yes|no> [default: yes]]]> 418 </option> 419 </term> 420 <listitem> 421 <para> 422 Whether to report calls to 423 <function>pthread_cond_signal</function> and 424 <function>pthread_cond_broadcast</function> where the mutex 425 associated with the signal through 426 <function>pthread_cond_wait</function> or 427 <function>pthread_cond_timed_wait</function>is not locked at 428 the time the signal is sent. Sending a signal without holding 429 a lock on the associated mutex is a common programming error 430 which can cause subtle race conditions and unpredictable 431 behavior. There exist some uncommon synchronization patterns 432 however where it is safe to send a signal without holding a 433 lock on the associated mutex. 434 </para> 435 </listitem> 436 </varlistentry> 437 <varlistentry> 438 <term> 439 <option><![CDATA[--segment-merging=<yes|no> [default: yes]]]></option> 440 </term> 441 <listitem> 442 <para> 443 Controls segment merging. Segment merging is an algorithm to 444 limit memory usage of the data race detection 445 algorithm. Disabling segment merging may improve the accuracy 446 of the so-called 'other segments' displayed in race reports 447 but can also trigger an out of memory error. 448 </para> 449 </listitem> 450 </varlistentry> 451 <varlistentry> 452 <term> 453 <option><![CDATA[--segment-merging-interval=<n> [default: 10]]]></option> 454 </term> 455 <listitem> 456 <para> 457 Perform segment merging only after the specified number of new 458 segments have been created. This is an advanced configuration option 459 that allows to choose whether to minimize DRD's memory usage by 460 choosing a low value or to let DRD run faster by choosing a slightly 461 higher value. The optimal value for this parameter depends on the 462 program being analyzed. The default value works well for most programs. 463 </para> 464 </listitem> 465 </varlistentry> 466 <varlistentry> 467 <term> 468 <option><![CDATA[--shared-threshold=<n> [default: off]]]></option> 469 </term> 470 <listitem> 471 <para> 472 Print an error message if a reader lock has been held longer 473 than the specified time (in milliseconds). This option enables 474 the detection of lock contention. 475 </para> 476 </listitem> 477 </varlistentry> 478 <varlistentry> 479 <term> 480 <option><![CDATA[--show-confl-seg=<yes|no> [default: yes]]]></option> 481 </term> 482 <listitem> 483 <para> 484 Show conflicting segments in race reports. Since this 485 information can help to find the cause of a data race, this 486 option is enabled by default. Disabling this option makes the 487 output of DRD more compact. 488 </para> 489 </listitem> 490 </varlistentry> 491 <varlistentry> 492 <term> 493 <option><![CDATA[--show-stack-usage=<yes|no> [default: no]]]></option> 494 </term> 495 <listitem> 496 <para> 497 Print stack usage at thread exit time. When a program creates a large 498 number of threads it becomes important to limit the amount of virtual 499 memory allocated for thread stacks. This option makes it possible to 500 observe how much stack memory has been used by each thread of the the 501 client program. Note: the DRD tool itself allocates some temporary 502 data on the client thread stack. The space necessary for this 503 temporary data must be allocated by the client program when it 504 allocates stack memory, but is not included in stack usage reported by 505 DRD. 506 </para> 507 </listitem> 508 </varlistentry> 509</variablelist> 510<!-- end of xi:include in the manpage --> 511 512<!-- start of xi:include in the manpage --> 513<para> 514The following options are available for monitoring the behavior of the 515client program: 516</para> 517 518<variablelist id="drd.debugopts.list"> 519 <varlistentry> 520 <term> 521 <option><![CDATA[--trace-addr=<address> [default: none]]]></option> 522 </term> 523 <listitem> 524 <para> 525 Trace all load and store activity for the specified 526 address. This option may be specified more than once. 527 </para> 528 </listitem> 529 </varlistentry> 530 <varlistentry> 531 <term> 532 <option><![CDATA[--trace-alloc=<yes|no> [default: no]]]></option> 533 </term> 534 <listitem> 535 <para> 536 Trace all memory allocations and deallocations. May produce a huge 537 amount of output. 538 </para> 539 </listitem> 540 </varlistentry> 541 <varlistentry> 542 <term> 543 <option><![CDATA[--trace-barrier=<yes|no> [default: no]]]></option> 544 </term> 545 <listitem> 546 <para> 547 Trace all barrier activity. 548 </para> 549 </listitem> 550 </varlistentry> 551 <varlistentry> 552 <term> 553 <option><![CDATA[--trace-cond=<yes|no> [default: no]]]></option> 554 </term> 555 <listitem> 556 <para> 557 Trace all condition variable activity. 558 </para> 559 </listitem> 560 </varlistentry> 561 <varlistentry> 562 <term> 563 <option><![CDATA[--trace-fork-join=<yes|no> [default: no]]]></option> 564 </term> 565 <listitem> 566 <para> 567 Trace all thread creation and all thread termination events. 568 </para> 569 </listitem> 570 </varlistentry> 571 <varlistentry> 572 <term> 573 <option><![CDATA[--trace-hb=<yes|no> [default: no]]]></option> 574 </term> 575 <listitem> 576 <para> 577 Trace execution of the <literal>ANNOTATE_HAPPENS_BEFORE()</literal>, 578 <literal>ANNOTATE_HAPPENS_AFTER()</literal> and 579 <literal>ANNOTATE_HAPPENS_DONE()</literal> client requests. 580 </para> 581 </listitem> 582 </varlistentry> 583 <varlistentry> 584 <term> 585 <option><![CDATA[--trace-mutex=<yes|no> [default: no]]]></option> 586 </term> 587 <listitem> 588 <para> 589 Trace all mutex activity. 590 </para> 591 </listitem> 592 </varlistentry> 593 <varlistentry> 594 <term> 595 <option><![CDATA[--trace-rwlock=<yes|no> [default: no]]]></option> 596 </term> 597 <listitem> 598 <para> 599 Trace all reader-writer lock activity. 600 </para> 601 </listitem> 602 </varlistentry> 603 <varlistentry> 604 <term> 605 <option><![CDATA[--trace-semaphore=<yes|no> [default: no]]]></option> 606 </term> 607 <listitem> 608 <para> 609 Trace all semaphore activity. 610 </para> 611 </listitem> 612 </varlistentry> 613</variablelist> 614<!-- end of xi:include in the manpage --> 615 616</sect2> 617 618 619<sect2 id="drd-manual.data-races" xreflabel="Data Races"> 620<title>Detected Errors: Data Races</title> 621 622<para> 623DRD prints a message every time it detects a data race. Please keep 624the following in mind when interpreting DRD's output: 625<itemizedlist> 626 <listitem> 627 <para> 628 Every thread is assigned a <emphasis>thread ID</emphasis> by the DRD 629 tool. A thread ID is a number. Thread ID's start at one and are never 630 recycled. 631 </para> 632 </listitem> 633 <listitem> 634 <para> 635 The term <emphasis>segment</emphasis> refers to a consecutive 636 sequence of load, store and synchronization operations, all 637 issued by the same thread. A segment always starts and ends at a 638 synchronization operation. Data race analysis is performed 639 between segments instead of between individual load and store 640 operations because of performance reasons. 641 </para> 642 </listitem> 643 <listitem> 644 <para> 645 There are always at least two memory accesses involved in a data 646 race. Memory accesses involved in a data race are called 647 <emphasis>conflicting memory accesses</emphasis>. DRD prints a 648 report for each memory access that conflicts with a past memory 649 access. 650 </para> 651 </listitem> 652</itemizedlist> 653</para> 654 655<para> 656Below you can find an example of a message printed by DRD when it 657detects a data race: 658</para> 659<programlisting><![CDATA[ 660$ valgrind --tool=drd --read-var-info=yes drd/tests/rwlock_race 661... 662==9466== Thread 3: 663==9466== Conflicting load by thread 3 at 0x006020b8 size 4 664==9466== at 0x400B6C: thread_func (rwlock_race.c:29) 665==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 666==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 667==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 668==9466== Location 0x6020b8 is 0 bytes inside local var "s_racy" 669==9466== declared at rwlock_race.c:18, in frame #0 of thread 3 670==9466== Other segment start (thread 2) 671==9466== at 0x4C2847D: pthread_rwlock_rdlock* (drd_pthread_intercepts.c:813) 672==9466== by 0x400B6B: thread_func (rwlock_race.c:28) 673==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 674==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 675==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 676==9466== Other segment end (thread 2) 677==9466== at 0x4C28B54: pthread_rwlock_unlock* (drd_pthread_intercepts.c:912) 678==9466== by 0x400B84: thread_func (rwlock_race.c:30) 679==9466== by 0x4C291DF: vg_thread_wrapper (drd_pthread_intercepts.c:186) 680==9466== by 0x4E3403F: start_thread (in /lib64/libpthread-2.8.so) 681==9466== by 0x53250CC: clone (in /lib64/libc-2.8.so) 682... 683]]></programlisting> 684 685<para> 686The above report has the following meaning: 687<itemizedlist> 688 <listitem> 689 <para> 690 The number in the column on the left is the process ID of the 691 process being analyzed by DRD. 692 </para> 693 </listitem> 694 <listitem> 695 <para> 696 The first line ("Thread 3") tells you the thread ID for 697 the thread in which context the data race has been detected. 698 </para> 699 </listitem> 700 <listitem> 701 <para> 702 The next line tells which kind of operation was performed (load or 703 store) and by which thread. On the same line the start address and the 704 number of bytes involved in the conflicting access are also displayed. 705 </para> 706 </listitem> 707 <listitem> 708 <para> 709 Next, the call stack of the conflicting access is displayed. If 710 your program has been compiled with debug information 711 (<option>-g</option>), this call stack will include file names and 712 line numbers. The two 713 bottommost frames in this call stack (<function>clone</function> 714 and <function>start_thread</function>) show how the NPTL starts 715 a thread. The third frame 716 (<function>vg_thread_wrapper</function>) is added by DRD. The 717 fourth frame (<function>thread_func</function>) is the first 718 interesting line because it shows the thread entry point, that 719 is the function that has been passed as the third argument to 720 <function>pthread_create</function>. 721 </para> 722 </listitem> 723 <listitem> 724 <para> 725 Next, the allocation context for the conflicting address is 726 displayed. For dynamically allocated data the allocation call 727 stack is shown. For static variables and stack variables the 728 allocation context is only shown when the option 729 <option>--read-var-info=yes</option> has been 730 specified. Otherwise DRD will print <computeroutput>Allocation 731 context: unknown</computeroutput>. 732 </para> 733 </listitem> 734 <listitem> 735 <para> 736 A conflicting access involves at least two memory accesses. For 737 one of these accesses an exact call stack is displayed, and for 738 the other accesses an approximate call stack is displayed, 739 namely the start and the end of the segments of the other 740 accesses. This information can be interpreted as follows: 741 <orderedlist> 742 <listitem> 743 <para> 744 Start at the bottom of both call stacks, and count the 745 number stack frames with identical function name, file 746 name and line number. In the above example the three 747 bottommost frames are identical 748 (<function>clone</function>, 749 <function>start_thread</function> and 750 <function>vg_thread_wrapper</function>). 751 </para> 752 </listitem> 753 <listitem> 754 <para> 755 The next higher stack frame in both call stacks now tells 756 you between in which source code region the other memory 757 access happened. The above output tells that the other 758 memory access involved in the data race happened between 759 source code lines 28 and 30 in file 760 <computeroutput>rwlock_race.c</computeroutput>. 761 </para> 762 </listitem> 763 </orderedlist> 764 </para> 765 </listitem> 766</itemizedlist> 767</para> 768 769</sect2> 770 771 772<sect2 id="drd-manual.lock-contention" xreflabel="Lock Contention"> 773<title>Detected Errors: Lock Contention</title> 774 775<para> 776Threads must be able to make progress without being blocked for too long by 777other threads. Sometimes a thread has to wait until a mutex or reader-writer 778synchronization object is unlocked by another thread. This is called 779<emphasis>lock contention</emphasis>. 780</para> 781 782<para> 783Lock contention causes delays. Such delays should be as short as 784possible. The two command line options 785<literal>--exclusive-threshold=<n></literal> and 786<literal>--shared-threshold=<n></literal> make it possible to 787detect excessive lock contention by making DRD report any lock that 788has been held longer than the specified threshold. An example: 789</para> 790<programlisting><![CDATA[ 791$ valgrind --tool=drd --exclusive-threshold=10 drd/tests/hold_lock -i 500 792... 793==10668== Acquired at: 794==10668== at 0x4C267C8: pthread_mutex_lock (drd_pthread_intercepts.c:395) 795==10668== by 0x400D92: main (hold_lock.c:51) 796==10668== Lock on mutex 0x7fefffd50 was held during 503 ms (threshold: 10 ms). 797==10668== at 0x4C26ADA: pthread_mutex_unlock (drd_pthread_intercepts.c:441) 798==10668== by 0x400DB5: main (hold_lock.c:55) 799... 800]]></programlisting> 801 802<para> 803The <literal>hold_lock</literal> test program holds a lock as long as 804specified by the <literal>-i</literal> (interval) argument. The DRD 805output reports that the lock acquired at line 51 in source file 806<literal>hold_lock.c</literal> and released at line 55 was held during 807503 ms, while a threshold of 10 ms was specified to DRD. 808</para> 809 810</sect2> 811 812 813<sect2 id="drd-manual.api-checks" xreflabel="API Checks"> 814<title>Detected Errors: Misuse of the POSIX threads API</title> 815 816<para> 817 DRD is able to detect and report the following misuses of the POSIX 818 threads API: 819 <itemizedlist> 820 <listitem> 821 <para> 822 Passing the address of one type of synchronization object 823 (e.g. a mutex) to a POSIX API call that expects a pointer to 824 another type of synchronization object (e.g. a condition 825 variable). 826 </para> 827 </listitem> 828 <listitem> 829 <para> 830 Attempts to unlock a mutex that has not been locked. 831 </para> 832 </listitem> 833 <listitem> 834 <para> 835 Attempts to unlock a mutex that was locked by another thread. 836 </para> 837 </listitem> 838 <listitem> 839 <para> 840 Attempts to lock a mutex of type 841 <literal>PTHREAD_MUTEX_NORMAL</literal> or a spinlock 842 recursively. 843 </para> 844 </listitem> 845 <listitem> 846 <para> 847 Destruction or deallocation of a locked mutex. 848 </para> 849 </listitem> 850 <listitem> 851 <para> 852 Sending a signal to a condition variable while no lock is held 853 on the mutex associated with the condition variable. 854 </para> 855 </listitem> 856 <listitem> 857 <para> 858 Calling <function>pthread_cond_wait</function> on a mutex 859 that is not locked, that is locked by another thread or that 860 has been locked recursively. 861 </para> 862 </listitem> 863 <listitem> 864 <para> 865 Associating two different mutexes with a condition variable 866 through <function>pthread_cond_wait</function>. 867 </para> 868 </listitem> 869 <listitem> 870 <para> 871 Destruction or deallocation of a condition variable that is 872 being waited upon. 873 </para> 874 </listitem> 875 <listitem> 876 <para> 877 Destruction or deallocation of a locked reader-writer synchronization 878 object. 879 </para> 880 </listitem> 881 <listitem> 882 <para> 883 Attempts to unlock a reader-writer synchronization object that was not 884 locked by the calling thread. 885 </para> 886 </listitem> 887 <listitem> 888 <para> 889 Attempts to recursively lock a reader-writer synchronization object 890 exclusively. 891 </para> 892 </listitem> 893 <listitem> 894 <para> 895 Attempts to pass the address of a user-defined reader-writer 896 synchronization object to a POSIX threads function. 897 </para> 898 </listitem> 899 <listitem> 900 <para> 901 Attempts to pass the address of a POSIX reader-writer synchronization 902 object to one of the annotations for user-defined reader-writer 903 synchronization objects. 904 </para> 905 </listitem> 906 <listitem> 907 <para> 908 Reinitialization of a mutex, condition variable, reader-writer 909 lock, semaphore or barrier. 910 </para> 911 </listitem> 912 <listitem> 913 <para> 914 Destruction or deallocation of a semaphore or barrier that is 915 being waited upon. 916 </para> 917 </listitem> 918 <listitem> 919 <para> 920 Missing synchronization between barrier wait and barrier destruction. 921 </para> 922 </listitem> 923 <listitem> 924 <para> 925 Exiting a thread without first unlocking the spinlocks, mutexes or 926 reader-writer synchronization objects that were locked by that thread. 927 </para> 928 </listitem> 929 <listitem> 930 <para> 931 Passing an invalid thread ID to <function>pthread_join</function> 932 or <function>pthread_cancel</function>. 933 </para> 934 </listitem> 935 </itemizedlist> 936</para> 937 938</sect2> 939 940 941<sect2 id="drd-manual.clientreqs" xreflabel="Client requests"> 942<title>Client Requests</title> 943 944<para> 945Just as for other Valgrind tools it is possible to let a client program 946interact with the DRD tool through client requests. In addition to the 947client requests several macros have been defined that allow to use the 948client requests in a convenient way. 949</para> 950 951<para> 952The interface between client programs and the DRD tool is defined in 953the header file <literal><valgrind/drd.h></literal>. The 954available macros and client requests are: 955<itemizedlist> 956 <listitem> 957 <para> 958 The macro <literal>DRD_GET_VALGRIND_THREADID</literal> and the 959 corresponding client 960 request <varname>VG_USERREQ__DRD_GET_VALGRIND_THREAD_ID</varname>. 961 Query the thread ID that has been assigned by the Valgrind core to the 962 thread executing this client request. Valgrind's thread ID's start at 963 one and are recycled in case a thread stops. 964 </para> 965 </listitem> 966 <listitem> 967 <para> 968 The macro <literal>DRD_GET_DRD_THREADID</literal> and the corresponding 969 client request <varname>VG_USERREQ__DRD_GET_DRD_THREAD_ID</varname>. 970 Query the thread ID that has been assigned by DRD to the thread 971 executing this client request. These are the thread ID's reported by DRD 972 in data race reports and in trace messages. DRD's thread ID's start at 973 one and are never recycled. 974 </para> 975 </listitem> 976 <listitem> 977 <para> 978 The macros <literal>DRD_IGNORE_VAR(x)</literal>, 979 <literal>ANNOTATE_TRACE_MEMORY(&x)</literal> and the corresponding 980 client request <varname>VG_USERREQ__DRD_START_SUPPRESSION</varname>. Some 981 applications contain intentional races. There exist e.g. applications 982 where the same value is assigned to a shared variable from two different 983 threads. It may be more convenient to suppress such races than to solve 984 these. This client request allows to suppress such races. 985 </para> 986 </listitem> 987 <listitem> 988 <para> 989 The macro <literal>DRD_STOP_IGNORING_VAR(x)</literal> and the 990 corresponding client request 991 <varname>VG_USERREQ__DRD_FINISH_SUPPRESSION</varname>. Tell DRD 992 to no longer ignore data races for the address range that was suppressed 993 either via the macro <literal>DRD_IGNORE_VAR(x)</literal> or via the 994 client request <varname>VG_USERREQ__DRD_START_SUPPRESSION</varname>. 995 </para> 996 </listitem> 997 <listitem> 998 <para> 999 The macro <literal>DRD_TRACE_VAR(x)</literal>. Trace all load and store 1000 activity for the address range starting at <literal>&x</literal> and 1001 occupying <literal>sizeof(x)</literal> bytes. When DRD reports a data 1002 race on a specified variable, and it's not immediately clear which 1003 source code statements triggered the conflicting accesses, it can be 1004 very helpful to trace all activity on the offending memory location. 1005 </para> 1006 </listitem> 1007 <listitem> 1008 <para> 1009 The macro <literal>ANNOTATE_TRACE_MEMORY(&x)</literal>. Trace all 1010 load and store activity that touches at least the single byte at the 1011 address <literal>&x</literal>. 1012 </para> 1013 </listitem> 1014 <listitem> 1015 <para> 1016 The client request <varname>VG_USERREQ__DRD_START_TRACE_ADDR</varname>, 1017 which allows to trace all load and store activity for the specified 1018 address range. 1019 </para> 1020 </listitem> 1021 <listitem> 1022 <para> 1023 The client 1024 request <varname>VG_USERREQ__DRD_STOP_TRACE_ADDR</varname>. Do no longer 1025 trace load and store activity for the specified address range. 1026 </para> 1027 </listitem> 1028 <listitem> 1029 <para> 1030 The macro <literal>ANNOTATE_HAPPENS_BEFORE(addr)</literal> tells DRD to 1031 insert a mark. Insert this macro just after an access to the variable at 1032 the specified address has been performed. 1033 </para> 1034 </listitem> 1035 <listitem> 1036 <para> 1037 The macro <literal>ANNOTATE_HAPPENS_AFTER(addr)</literal> tells DRD that 1038 the next access to the variable at the specified address should be 1039 considered to have happened after the access just before the latest 1040 <literal>ANNOTATE_HAPPENS_BEFORE(addr)</literal> annotation that 1041 references the same variable. The purpose of these two macros is to tell 1042 DRD about the order of inter-thread memory accesses implemented via 1043 atomic memory operations. See 1044 also <literal>drd/tests/annotate_smart_pointer.cpp</literal> for an 1045 example. 1046 </para> 1047 </listitem> 1048 <listitem> 1049 <para> 1050 The macro <literal>ANNOTATE_RWLOCK_CREATE(rwlock)</literal> tells DRD 1051 that the object at address <literal>rwlock</literal> is a 1052 reader-writer synchronization object that is not a 1053 <literal>pthread_rwlock_t</literal> synchronization object. See 1054 also <literal>drd/tests/annotate_rwlock.c</literal> for an example. 1055 </para> 1056 </listitem> 1057 <listitem> 1058 <para> 1059 The macro <literal>ANNOTATE_RWLOCK_DESTROY(rwlock)</literal> tells DRD 1060 that the reader-writer synchronization object at 1061 address <literal>rwlock</literal> has been destroyed. 1062 </para> 1063 </listitem> 1064 <listitem> 1065 <para> 1066 The macro <literal>ANNOTATE_WRITERLOCK_ACQUIRED(rwlock)</literal> tells 1067 DRD that a writer lock has been acquired on the reader-writer 1068 synchronization object at address <literal>rwlock</literal>. 1069 </para> 1070 </listitem> 1071 <listitem> 1072 <para> 1073 The macro <literal>ANNOTATE_READERLOCK_ACQUIRED(rwlock)</literal> tells 1074 DRD that a reader lock has been acquired on the reader-writer 1075 synchronization object at address <literal>rwlock</literal>. 1076 </para> 1077 </listitem> 1078 <listitem> 1079 <para> 1080 The macro <literal>ANNOTATE_RWLOCK_ACQUIRED(rwlock, is_w)</literal> 1081 tells DRD that a writer lock (when <literal>is_w != 0</literal>) or that 1082 a reader lock (when <literal>is_w == 0</literal>) has been acquired on 1083 the reader-writer synchronization object at 1084 address <literal>rwlock</literal>. 1085 </para> 1086 </listitem> 1087 <listitem> 1088 <para> 1089 The macro <literal>ANNOTATE_WRITERLOCK_RELEASED(rwlock)</literal> tells 1090 DRD that a writer lock has been released on the reader-writer 1091 synchronization object at address <literal>rwlock</literal>. 1092 </para> 1093 </listitem> 1094 <listitem> 1095 <para> 1096 The macro <literal>ANNOTATE_READERLOCK_RELEASED(rwlock)</literal> tells 1097 DRD that a reader lock has been released on the reader-writer 1098 synchronization object at address <literal>rwlock</literal>. 1099 </para> 1100 </listitem> 1101 <listitem> 1102 <para> 1103 The macro <literal>ANNOTATE_RWLOCK_RELEASED(rwlock, is_w)</literal> 1104 tells DRD that a writer lock (when <literal>is_w != 0</literal>) or that 1105 a reader lock (when <literal>is_w == 0</literal>) has been released on 1106 the reader-writer synchronization object at 1107 address <literal>rwlock</literal>. 1108 </para> 1109 </listitem> 1110 <listitem> 1111 <para> 1112 The macro <literal>ANNOTATE_BARRIER_INIT(barrier, count, 1113 reinitialization_allowed)</literal> tells DRD that a new barrier object 1114 at the address <literal>barrier</literal> has been initialized, 1115 that <literal>count</literal> threads participate in each barrier and 1116 also whether or not barrier reinitialization without intervening 1117 destruction should be reported as an error. See 1118 also <literal>drd/tests/annotate_barrier.c</literal> for an example. 1119 </para> 1120 </listitem> 1121 <listitem> 1122 <para> 1123 The macro <literal>ANNOTATE_BARRIER_DESTROY(barrier)</literal> 1124 tells DRD that a barrier object is about to be destroyed. 1125 </para> 1126 </listitem> 1127 <listitem> 1128 <para> 1129 The macro <literal>ANNOTATE_BARRIER_WAIT_BEFORE(barrier)</literal> 1130 tells DRD that waiting for a barrier will start. 1131 </para> 1132 </listitem> 1133 <listitem> 1134 <para> 1135 The macro <literal>ANNOTATE_BARRIER_WAIT_AFTER(barrier)</literal> 1136 tells DRD that waiting for a barrier has finished. 1137 </para> 1138 </listitem> 1139 <listitem> 1140 <para> 1141 The macro <literal>ANNOTATE_BENIGN_RACE_SIZED(addr, size, 1142 descr)</literal> tells DRD that any races detected on the specified 1143 address are benign and hence should not be 1144 reported. The <literal>descr</literal> argument is ignored but can be 1145 used to document why data races on <literal>addr</literal> are benign. 1146 </para> 1147 </listitem> 1148 <listitem> 1149 <para> 1150 The macro <literal>ANNOTATE_BENIGN_RACE_STATIC(var, descr)</literal> 1151 tells DRD that any races detected on the specified static variable are 1152 benign and hence should not be reported. The <literal>descr</literal> 1153 argument is ignored but can be used to document why data races 1154 on <literal>var</literal> are benign. Note: this macro can only be 1155 used in C++ programs and not in C programs. 1156 </para> 1157 </listitem> 1158 <listitem> 1159 <para> 1160 The macro <literal>ANNOTATE_IGNORE_READS_BEGIN</literal> tells 1161 DRD to ignore all memory loads performed by the current thread. 1162 </para> 1163 </listitem> 1164 <listitem> 1165 <para> 1166 The macro <literal>ANNOTATE_IGNORE_READS_END</literal> tells 1167 DRD to stop ignoring the memory loads performed by the current thread. 1168 </para> 1169 </listitem> 1170 <listitem> 1171 <para> 1172 The macro <literal>ANNOTATE_IGNORE_WRITES_BEGIN</literal> tells 1173 DRD to ignore all memory stores performed by the current thread. 1174 </para> 1175 </listitem> 1176 <listitem> 1177 <para> 1178 The macro <literal>ANNOTATE_IGNORE_WRITES_END</literal> tells 1179 DRD to stop ignoring the memory stores performed by the current thread. 1180 </para> 1181 </listitem> 1182 <listitem> 1183 <para> 1184 The macro <literal>ANNOTATE_IGNORE_READS_AND_WRITES_BEGIN</literal> tells 1185 DRD to ignore all memory accesses performed by the current thread. 1186 </para> 1187 </listitem> 1188 <listitem> 1189 <para> 1190 The macro <literal>ANNOTATE_IGNORE_READS_AND_WRITES_END</literal> tells 1191 DRD to stop ignoring the memory accesses performed by the current thread. 1192 </para> 1193 </listitem> 1194 <listitem> 1195 <para> 1196 The macro <literal>ANNOTATE_NEW_MEMORY(addr, size)</literal> tells 1197 DRD that the specified memory range has been allocated by a custom 1198 memory allocator in the client program and that the client program 1199 will start using this memory range. 1200 </para> 1201 </listitem> 1202 <listitem> 1203 <para> 1204 The macro <literal>ANNOTATE_THREAD_NAME(name)</literal> tells DRD to 1205 associate the specified name with the current thread and to include this 1206 name in the error messages printed by DRD. 1207 </para> 1208 </listitem> 1209 <listitem> 1210 <para> 1211 The macros <literal>VALGRIND_MALLOCLIKE_BLOCK</literal> and 1212 <literal>VALGRIND_FREELIKE_BLOCK</literal> from the Valgrind core are 1213 implemented; they are described in 1214 <xref linkend="manual-core-adv.clientreq"/>. 1215 </para> 1216 </listitem> 1217</itemizedlist> 1218</para> 1219 1220<para> 1221Note: if you compiled Valgrind yourself, the header file 1222<literal><valgrind/drd.h></literal> will have been installed in 1223the directory <literal>/usr/include</literal> by the command 1224<literal>make install</literal>. If you obtained Valgrind by 1225installing it as a package however, you will probably have to install 1226another package with a name like <literal>valgrind-devel</literal> 1227before Valgrind's header files are available. 1228</para> 1229 1230</sect2> 1231 1232 1233<sect2 id="drd-manual.gnome" xreflabel="GNOME"> 1234<title>Debugging GNOME Programs</title> 1235 1236<para> 1237GNOME applications use the threading primitives provided by the 1238<computeroutput>glib</computeroutput> and 1239<computeroutput>gthread</computeroutput> libraries. These libraries 1240are built on top of POSIX threads, and hence are directly supported by 1241DRD. Please keep in mind that you have to call 1242<function>g_thread_init</function> before creating any threads, or 1243DRD will report several data races on glib functions. See also the 1244<ulink 1245url="http://library.gnome.org/devel/glib/stable/glib-Threads.html">GLib 1246Reference Manual</ulink> for more information about 1247<function>g_thread_init</function>. 1248</para> 1249 1250<para> 1251One of the many facilities provided by the <literal>glib</literal> 1252library is a block allocator, called <literal>g_slice</literal>. You 1253have to disable this block allocator when using DRD by adding the 1254following to the shell environment variables: 1255<literal>G_SLICE=always-malloc</literal>. See also the <ulink 1256url="http://library.gnome.org/devel/glib/stable/glib-Memory-Slices.html">GLib 1257Reference Manual</ulink> for more information. 1258</para> 1259 1260</sect2> 1261 1262 1263<sect2 id="drd-manual.boost.thread" xreflabel="Boost.Thread"> 1264<title>Debugging Boost.Thread Programs</title> 1265 1266<para> 1267The Boost.Thread library is the threading library included with the 1268cross-platform Boost Libraries. This threading library is an early 1269implementation of the upcoming C++0x threading library. 1270</para> 1271 1272<para> 1273Applications that use the Boost.Thread library should run fine under DRD. 1274</para> 1275 1276<para> 1277More information about Boost.Thread can be found here: 1278<itemizedlist> 1279 <listitem> 1280 <para> 1281 Anthony Williams, <ulink 1282 url="http://www.boost.org/doc/libs/1_37_0/doc/html/thread.html">Boost.Thread</ulink> 1283 Library Documentation, Boost website, 2007. 1284 </para> 1285 </listitem> 1286 <listitem> 1287 <para> 1288 Anthony Williams, <ulink 1289 url="http://www.ddj.com/cpp/211600441">What's New in Boost 1290 Threads?</ulink>, Recent changes to the Boost Thread library, 1291 Dr. Dobbs Magazine, October 2008. 1292 </para> 1293 </listitem> 1294</itemizedlist> 1295</para> 1296 1297</sect2> 1298 1299 1300<sect2 id="drd-manual.openmp" xreflabel="OpenMP"> 1301<title>Debugging OpenMP Programs</title> 1302 1303<para> 1304OpenMP stands for <emphasis>Open Multi-Processing</emphasis>. The OpenMP 1305standard consists of a set of compiler directives for C, C++ and Fortran 1306programs that allows a compiler to transform a sequential program into a 1307parallel program. OpenMP is well suited for HPC applications and allows to 1308work at a higher level compared to direct use of the POSIX threads API. While 1309OpenMP ensures that the POSIX API is used correctly, OpenMP programs can still 1310contain data races. So it definitely makes sense to verify OpenMP programs 1311with a thread checking tool. 1312</para> 1313 1314<para> 1315DRD supports OpenMP shared-memory programs generated by GCC. GCC 1316supports OpenMP since version 4.2.0. GCC's runtime support 1317for OpenMP programs is provided by a library called 1318<literal>libgomp</literal>. The synchronization primitives implemented 1319in this library use Linux' futex system call directly, unless the 1320library has been configured with the 1321<literal>--disable-linux-futex</literal> option. DRD only supports 1322libgomp libraries that have been configured with this option and in 1323which symbol information is present. For most Linux distributions this 1324means that you will have to recompile GCC. See also the script 1325<literal>drd/scripts/download-and-build-gcc</literal> in the 1326Valgrind source tree for an example of how to compile GCC. You will 1327also have to make sure that the newly compiled 1328<literal>libgomp.so</literal> library is loaded when OpenMP programs 1329are started. This is possible by adding a line similar to the 1330following to your shell startup script: 1331</para> 1332<programlisting><![CDATA[ 1333export LD_LIBRARY_PATH=~/gcc-4.4.0/lib64:~/gcc-4.4.0/lib: 1334]]></programlisting> 1335 1336<para> 1337As an example, the test OpenMP test program 1338<literal>drd/tests/omp_matinv</literal> triggers a data race 1339when the option -r has been specified on the command line. The data 1340race is triggered by the following code: 1341</para> 1342<programlisting><![CDATA[ 1343#pragma omp parallel for private(j) 1344for (j = 0; j < rows; j++) 1345{ 1346 if (i != j) 1347 { 1348 const elem_t factor = a[j * cols + i]; 1349 for (k = 0; k < cols; k++) 1350 { 1351 a[j * cols + k] -= a[i * cols + k] * factor; 1352 } 1353 } 1354} 1355]]></programlisting> 1356 1357<para> 1358The above code is racy because the variable <literal>k</literal> has 1359not been declared private. DRD will print the following error message 1360for the above code: 1361</para> 1362<programlisting><![CDATA[ 1363$ valgrind --tool=drd --check-stack-var=yes --read-var-info=yes drd/tests/omp_matinv 3 -t 2 -r 1364... 1365Conflicting store by thread 1/1 at 0x7fefffbc4 size 4 1366 at 0x4014A0: gj.omp_fn.0 (omp_matinv.c:203) 1367 by 0x401211: gj (omp_matinv.c:159) 1368 by 0x40166A: invert_matrix (omp_matinv.c:238) 1369 by 0x4019B4: main (omp_matinv.c:316) 1370Location 0x7fefffbc4 is 0 bytes inside local var "k" 1371declared at omp_matinv.c:160, in frame #0 of thread 1 1372... 1373]]></programlisting> 1374<para> 1375In the above output the function name <function>gj.omp_fn.0</function> 1376has been generated by GCC from the function name 1377<function>gj</function>. The allocation context information shows that the 1378data race has been caused by modifying the variable <literal>k</literal>. 1379</para> 1380 1381<para> 1382Note: for GCC versions before 4.4.0, no allocation context information is 1383shown. With these GCC versions the most usable information in the above output 1384is the source file name and the line number where the data race has been 1385detected (<literal>omp_matinv.c:203</literal>). 1386</para> 1387 1388<para> 1389For more information about OpenMP, see also 1390<ulink url="http://openmp.org/">openmp.org</ulink>. 1391</para> 1392 1393</sect2> 1394 1395 1396<sect2 id="drd-manual.cust-mem-alloc" xreflabel="Custom Memory Allocators"> 1397<title>DRD and Custom Memory Allocators</title> 1398 1399<para> 1400DRD tracks all memory allocation events that happen via the 1401standard memory allocation and deallocation functions 1402(<function>malloc</function>, <function>free</function>, 1403<function>new</function> and <function>delete</function>), via entry 1404and exit of stack frames or that have been annotated with Valgrind's 1405memory pool client requests. DRD uses memory allocation and deallocation 1406information for two purposes: 1407<itemizedlist> 1408 <listitem> 1409 <para> 1410 To know where the scope ends of POSIX objects that have not been 1411 destroyed explicitly. It is e.g. not required by the POSIX 1412 threads standard to call 1413 <function>pthread_mutex_destroy</function> before freeing the 1414 memory in which a mutex object resides. 1415 </para> 1416 </listitem> 1417 <listitem> 1418 <para> 1419 To know where the scope of variables ends. If e.g. heap memory 1420 has been used by one thread, that thread frees that memory, and 1421 another thread allocates and starts using that memory, no data 1422 races must be reported for that memory. 1423 </para> 1424 </listitem> 1425</itemizedlist> 1426</para> 1427 1428<para> 1429It is essential for correct operation of DRD that the tool knows about 1430memory allocation and deallocation events. When analyzing a client program 1431with DRD that uses a custom memory allocator, either instrument the custom 1432memory allocator with the <literal>VALGRIND_MALLOCLIKE_BLOCK</literal> 1433and <literal>VALGRIND_FREELIKE_BLOCK</literal> macros or disable the 1434custom memory allocator. 1435</para> 1436 1437<para> 1438As an example, the GNU libstdc++ library can be configured 1439to use standard memory allocation functions instead of memory pools by 1440setting the environment variable 1441<literal>GLIBCXX_FORCE_NEW</literal>. For more information, see also 1442the <ulink 1443url="http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt04ch11.html">libstdc++ 1444manual</ulink>. 1445</para> 1446 1447</sect2> 1448 1449 1450<sect2 id="drd-manual.drd-versus-memcheck" xreflabel="DRD Versus Memcheck"> 1451<title>DRD Versus Memcheck</title> 1452 1453<para> 1454It is essential for correct operation of DRD that there are no memory 1455errors such as dangling pointers in the client program. Which means that 1456it is a good idea to make sure that your program is Memcheck-clean 1457before you analyze it with DRD. It is possible however that some of 1458the Memcheck reports are caused by data races. In this case it makes 1459sense to run DRD before Memcheck. 1460</para> 1461 1462<para> 1463So which tool should be run first? In case both DRD and Memcheck 1464complain about a program, a possible approach is to run both tools 1465alternatingly and to fix as many errors as possible after each run of 1466each tool until none of the two tools prints any more error messages. 1467</para> 1468 1469</sect2> 1470 1471 1472<sect2 id="drd-manual.resource-requirements" xreflabel="Resource Requirements"> 1473<title>Resource Requirements</title> 1474 1475<para> 1476The requirements of DRD with regard to heap and stack memory and the 1477effect on the execution time of client programs are as follows: 1478<itemizedlist> 1479 <listitem> 1480 <para> 1481 When running a program under DRD with default DRD options, 1482 between 1.1 and 3.6 times more memory will be needed compared to 1483 a native run of the client program. More memory will be needed 1484 if loading debug information has been enabled 1485 (<literal>--read-var-info=yes</literal>). 1486 </para> 1487 </listitem> 1488 <listitem> 1489 <para> 1490 DRD allocates some of its temporary data structures on the stack 1491 of the client program threads. This amount of data is limited to 1492 1 - 2 KB. Make sure that thread stacks are sufficiently large. 1493 </para> 1494 </listitem> 1495 <listitem> 1496 <para> 1497 Most applications will run between 20 and 50 times slower under 1498 DRD than a native single-threaded run. The slowdown will be most 1499 noticeable for applications which perform frequent mutex lock / 1500 unlock operations. 1501 </para> 1502 </listitem> 1503</itemizedlist> 1504</para> 1505 1506</sect2> 1507 1508 1509<sect2 id="drd-manual.effective-use" xreflabel="Effective Use"> 1510<title>Hints and Tips for Effective Use of DRD</title> 1511 1512<para> 1513The following information may be helpful when using DRD: 1514<itemizedlist> 1515 <listitem> 1516 <para> 1517 Make sure that debug information is present in the executable 1518 being analyzed, such that DRD can print function name and line 1519 number information in stack traces. Most compilers can be told 1520 to include debug information via compiler option 1521 <option>-g</option>. 1522 </para> 1523 </listitem> 1524 <listitem> 1525 <para> 1526 Compile with option <option>-O1</option> instead of 1527 <option>-O0</option>. This will reduce the amount of generated 1528 code, may reduce the amount of debug info and will speed up 1529 DRD's processing of the client program. For more information, 1530 see also <xref linkend="manual-core.started"/>. 1531 </para> 1532 </listitem> 1533 <listitem> 1534 <para> 1535 If DRD reports any errors on libraries that are part of your 1536 Linux distribution like e.g. <literal>libc.so</literal> or 1537 <literal>libstdc++.so</literal>, installing the debug packages 1538 for these libraries will make the output of DRD a lot more 1539 detailed. 1540 </para> 1541 </listitem> 1542 <listitem> 1543 <para> 1544 When using C++, do not send output from more than one thread to 1545 <literal>std::cout</literal>. Doing so would not only 1546 generate multiple data race reports, it could also result in 1547 output from several threads getting mixed up. Either use 1548 <function>printf</function> or do the following: 1549 <orderedlist> 1550 <listitem> 1551 <para>Derive a class from <literal>std::ostreambuf</literal> 1552 and let that class send output line by line to 1553 <literal>stdout</literal>. This will avoid that individual 1554 lines of text produced by different threads get mixed 1555 up.</para> 1556 </listitem> 1557 <listitem> 1558 <para>Create one instance of <literal>std::ostream</literal> 1559 for each thread. This makes stream formatting settings 1560 thread-local. Pass a per-thread instance of the class 1561 derived from <literal>std::ostreambuf</literal> to the 1562 constructor of each instance. </para> 1563 </listitem> 1564 <listitem> 1565 <para>Let each thread send its output to its own instance of 1566 <literal>std::ostream</literal> instead of 1567 <literal>std::cout</literal>.</para> 1568 </listitem> 1569 </orderedlist> 1570 </para> 1571 </listitem> 1572</itemizedlist> 1573</para> 1574 1575</sect2> 1576 1577 1578</sect1> 1579 1580 1581<sect1 id="drd-manual.Pthreads" xreflabel="Pthreads"> 1582<title>Using the POSIX Threads API Effectively</title> 1583 1584<sect2 id="drd-manual.mutex-types" xreflabel="mutex-types"> 1585<title>Mutex types</title> 1586 1587<para> 1588The Single UNIX Specification version two defines the following four 1589mutex types (see also the documentation of <ulink 1590url="http://www.opengroup.org/onlinepubs/007908799/xsh/pthread_mutexattr_settype.html"><function>pthread_mutexattr_settype</function></ulink>): 1591<itemizedlist> 1592 <listitem> 1593 <para> 1594 <emphasis>normal</emphasis>, which means that no error checking 1595 is performed, and that the mutex is non-recursive. 1596 </para> 1597 </listitem> 1598 <listitem> 1599 <para> 1600 <emphasis>error checking</emphasis>, which means that the mutex 1601 is non-recursive and that error checking is performed. 1602 </para> 1603 </listitem> 1604 <listitem> 1605 <para> 1606 <emphasis>recursive</emphasis>, which means that a mutex may be 1607 locked recursively. 1608 </para> 1609 </listitem> 1610 <listitem> 1611 <para> 1612 <emphasis>default</emphasis>, which means that error checking 1613 behavior is undefined, and that the behavior for recursive 1614 locking is also undefined. Or: portable code must neither 1615 trigger error conditions through the Pthreads API nor attempt to 1616 lock a mutex of default type recursively. 1617 </para> 1618 </listitem> 1619</itemizedlist> 1620</para> 1621 1622<para> 1623In complex applications it is not always clear from beforehand which 1624mutex will be locked recursively and which mutex will not be locked 1625recursively. Attempts lock a non-recursive mutex recursively will 1626result in race conditions that are very hard to find without a thread 1627checking tool. So either use the error checking mutex type and 1628consistently check the return value of Pthread API mutex calls, or use 1629the recursive mutex type. 1630</para> 1631 1632</sect2> 1633 1634<sect2 id="drd-manual.condvar" xreflabel="condition-variables"> 1635<title>Condition variables</title> 1636 1637<para> 1638A condition variable allows one thread to wake up one or more other 1639threads. Condition variables are often used to notify one or more 1640threads about state changes of shared data. Unfortunately it is very 1641easy to introduce race conditions by using condition variables as the 1642only means of state information propagation. A better approach is to 1643let threads poll for changes of a state variable that is protected by 1644a mutex, and to use condition variables only as a thread wakeup 1645mechanism. See also the source file 1646<computeroutput>drd/tests/monitor_example.cpp</computeroutput> for an 1647example of how to implement this concept in C++. The monitor concept 1648used in this example is a well known and very useful concept -- see 1649also Wikipedia for more information about the <ulink 1650url="http://en.wikipedia.org/wiki/Monitor_(synchronization)">monitor</ulink> 1651concept. 1652</para> 1653 1654</sect2> 1655 1656<sect2 id="drd-manual.pctw" xreflabel="pthread_cond_timedwait"> 1657<title>pthread_cond_timedwait and timeouts</title> 1658 1659<para> 1660Historically the function 1661<function>pthread_cond_timedwait</function> only allowed the 1662specification of an absolute timeout, that is a timeout independent of 1663the time when this function was called. However, almost every call to 1664this function expresses a relative timeout. This typically happens by 1665passing the sum of 1666<computeroutput>clock_gettime(CLOCK_REALTIME)</computeroutput> and a 1667relative timeout as the third argument. This approach is incorrect 1668since forward or backward clock adjustments by e.g. ntpd will affect 1669the timeout. A more reliable approach is as follows: 1670<itemizedlist> 1671 <listitem> 1672 <para> 1673 When initializing a condition variable through 1674 <function>pthread_cond_init</function>, specify that the timeout of 1675 <function>pthread_cond_timedwait</function> will use the clock 1676 <literal>CLOCK_MONOTONIC</literal> instead of 1677 <literal>CLOCK_REALTIME</literal>. You can do this via 1678 <computeroutput>pthread_condattr_setclock(..., 1679 CLOCK_MONOTONIC)</computeroutput>. 1680 </para> 1681 </listitem> 1682 <listitem> 1683 <para> 1684 When calling <function>pthread_cond_timedwait</function>, pass 1685 the sum of 1686 <computeroutput>clock_gettime(CLOCK_MONOTONIC)</computeroutput> 1687 and a relative timeout as the third argument. 1688 </para> 1689 </listitem> 1690</itemizedlist> 1691See also 1692<computeroutput>drd/tests/monitor_example.cpp</computeroutput> for an 1693example. 1694</para> 1695 1696</sect2> 1697 1698</sect1> 1699 1700 1701<sect1 id="drd-manual.limitations" xreflabel="Limitations"> 1702<title>Limitations</title> 1703 1704<para>DRD currently has the following limitations:</para> 1705 1706<itemizedlist> 1707 <listitem> 1708 <para> 1709 DRD, just like Memcheck, will refuse to start on Linux 1710 distributions where all symbol information has been removed from 1711 <filename>ld.so</filename>. This is e.g. the case for the PPC editions 1712 of openSUSE and Gentoo. You will have to install the glibc debuginfo 1713 package on these platforms before you can use DRD. See also openSUSE 1714 bug <ulink url="http://bugzilla.novell.com/show_bug.cgi?id=396197"> 1715 396197</ulink> and Gentoo bug <ulink 1716 url="http://bugs.gentoo.org/214065">214065</ulink>. 1717 </para> 1718 </listitem> 1719 <listitem> 1720 <para> 1721 With gcc 4.4.3 and before, DRD may report data races on the C++ 1722 class <literal>std::string</literal> in a multithreaded program. This is 1723 a know <literal>libstdc++</literal> issue -- see also GCC bug 1724 <ulink url="http://gcc.gnu.org/bugzilla/show_bug.cgi?id=40518">40518</ulink> 1725 for more information. 1726 </para> 1727 </listitem> 1728 <listitem> 1729 <para> 1730 When address tracing is enabled, no information on atomic stores 1731 will be displayed. 1732 </para> 1733 </listitem> 1734 <listitem> 1735 <para> 1736 If you compile the DRD source code yourself, you need GCC 3.0 or 1737 later. GCC 2.95 is not supported. 1738 </para> 1739 </listitem> 1740 <listitem> 1741 <para> 1742 Of the two POSIX threads implementations for Linux, only the 1743 NPTL (Native POSIX Thread Library) is supported. The older 1744 LinuxThreads library is not supported. 1745 </para> 1746 </listitem> 1747</itemizedlist> 1748 1749</sect1> 1750 1751 1752<sect1 id="drd-manual.feedback" xreflabel="Feedback"> 1753<title>Feedback</title> 1754 1755<para> 1756If you have any comments, suggestions, feedback or bug reports about 1757DRD, feel free to either post a message on the Valgrind users mailing 1758list or to file a bug report. See also <ulink 1759url="&vg-url;">&vg-url;</ulink> for more information. 1760</para> 1761 1762</sect1> 1763 1764 1765</chapter> 1766