blob: a9cd5fb274581aefb572cfc51ee59393dbe0f001 [file] [log] [blame]
bart922304f2011-03-13 12:02:44 +00001/* -*- mode: C; c-basic-offset: 3; indent-tabs-mode: nil; -*- */
sewardjaf44c822007-11-25 14:01:38 +00002/*
bart86562bd2009-02-16 19:43:56 +00003 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00004
bart922304f2011-03-13 12:02:44 +00005 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00006
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
11
12 This program is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
20 02111-1307, USA.
21
22 The GNU General Public License is contained in the file COPYING.
23*/
24
25
26#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000027#include "drd_barrier.h"
bartd2c5eae2009-02-21 15:27:04 +000028#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000029#include "drd_cond.h"
30#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000031#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000032#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000033#include "drd_suppression.h"
34#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000035#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_basics.h" // Addr, SizeT
sewardjaf44c822007-11-25 14:01:38 +000037#include "pub_tool_libcassert.h" // tl_assert()
38#include "pub_tool_libcbase.h" // VG_(strlen)()
39#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000040#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000041#include "pub_tool_machine.h"
42#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000043#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000044#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
45
bart32ba2082008-06-05 08:53:42 +000046
sewardjaf44c822007-11-25 14:01:38 +000047
bart324a23b2009-02-15 12:14:52 +000048/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000049
bart86a87df2009-03-04 19:26:47 +000050static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
51static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
52static void thread_compute_conflict_set(struct bitmap** conflict_set,
53 const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +000054static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000055
56
bart324a23b2009-02-15 12:14:52 +000057/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000058
bart86a87df2009-03-04 19:26:47 +000059static ULong s_context_switch_count;
60static ULong s_discard_ordered_segments_count;
bart54803fe2009-06-21 09:26:27 +000061static ULong s_compute_conflict_set_count;
bart86a87df2009-03-04 19:26:47 +000062static ULong s_update_conflict_set_count;
barte5214662009-06-21 11:51:23 +000063static ULong s_update_conflict_set_new_sg_count;
64static ULong s_update_conflict_set_sync_count;
65static ULong s_update_conflict_set_join_count;
bart86a87df2009-03-04 19:26:47 +000066static ULong s_conflict_set_bitmap_creation_count;
67static ULong s_conflict_set_bitmap2_creation_count;
68static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000069DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
70ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
71struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000072static Bool s_trace_context_switches = False;
73static Bool s_trace_conflict_set = False;
bart8f822af2009-06-08 18:20:42 +000074static Bool s_trace_conflict_set_bm = False;
bart86a87df2009-03-04 19:26:47 +000075static Bool s_trace_fork_join = False;
76static Bool s_segment_merging = True;
bart8f822af2009-06-08 18:20:42 +000077static Bool s_new_segments_since_last_merge;
bart6f1d7162009-06-24 18:34:10 +000078static int s_segment_merge_interval = 10;
bart6d956dc2011-07-28 09:54:37 +000079static unsigned s_join_list_vol = 10;
80static unsigned s_deletion_head;
81static unsigned s_deletion_tail;
sewardjaf44c822007-11-25 14:01:38 +000082
83
bart324a23b2009-02-15 12:14:52 +000084/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000085
bart86a87df2009-03-04 19:26:47 +000086/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000087void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000088{
bartbedfd232009-03-26 19:07:15 +000089 tl_assert(t == False || t == True);
90 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000091}
92
bart86a87df2009-03-04 19:26:47 +000093/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000094void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000095{
bartbedfd232009-03-26 19:07:15 +000096 tl_assert(t == False || t == True);
97 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000098}
99
bart8f822af2009-06-08 18:20:42 +0000100/** Enables/disables conflict set bitmap tracing. */
101void DRD_(thread_trace_conflict_set_bm)(const Bool t)
102{
103 tl_assert(t == False || t == True);
104 s_trace_conflict_set_bm = t;
105}
106
bart86a87df2009-03-04 19:26:47 +0000107/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +0000108Bool DRD_(thread_get_trace_fork_join)(void)
109{
bartbedfd232009-03-26 19:07:15 +0000110 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +0000111}
112
bart86a87df2009-03-04 19:26:47 +0000113/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +0000114void DRD_(thread_set_trace_fork_join)(const Bool t)
115{
bartbedfd232009-03-26 19:07:15 +0000116 tl_assert(t == False || t == True);
117 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000118}
119
bart86a87df2009-03-04 19:26:47 +0000120/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000121void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000122{
bartbedfd232009-03-26 19:07:15 +0000123 tl_assert(m == False || m == True);
124 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000125}
126
bart8f822af2009-06-08 18:20:42 +0000127/** Get the segment merging interval. */
128int DRD_(thread_get_segment_merge_interval)(void)
129{
130 return s_segment_merge_interval;
131}
132
133/** Set the segment merging interval. */
134void DRD_(thread_set_segment_merge_interval)(const int i)
135{
136 s_segment_merge_interval = i;
137}
138
bart6d956dc2011-07-28 09:54:37 +0000139void DRD_(thread_set_join_list_vol)(const int jlv)
140{
141 s_join_list_vol = jlv;
142}
143
sewardjaf44c822007-11-25 14:01:38 +0000144/**
bart86a87df2009-03-04 19:26:47 +0000145 * Convert Valgrind's ThreadId into a DrdThreadId.
146 *
147 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
148 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000149 */
bart62a784c2009-02-15 13:11:14 +0000150DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000151{
bartbedfd232009-03-26 19:07:15 +0000152 int i;
sewardjaf44c822007-11-25 14:01:38 +0000153
bartbedfd232009-03-26 19:07:15 +0000154 if (tid == VG_INVALID_THREADID)
155 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000156
bartbedfd232009-03-26 19:07:15 +0000157 for (i = 1; i < DRD_N_THREADS; i++)
158 {
159 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
160 && DRD_(g_threadinfo)[i].vg_threadid == tid)
161 {
162 return i;
163 }
164 }
sewardjaf44c822007-11-25 14:01:38 +0000165
bartbedfd232009-03-26 19:07:15 +0000166 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000167}
168
bart86a87df2009-03-04 19:26:47 +0000169/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000170static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000171{
bartbedfd232009-03-26 19:07:15 +0000172 int i;
sewardjaf44c822007-11-25 14:01:38 +0000173
bartbedfd232009-03-26 19:07:15 +0000174 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000175
bartbedfd232009-03-26 19:07:15 +0000176 for (i = 1; i < DRD_N_THREADS; i++)
177 {
bart6d956dc2011-07-28 09:54:37 +0000178 if (!DRD_(g_threadinfo)[i].valid)
bartbedfd232009-03-26 19:07:15 +0000179 {
180 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000181
bart6d956dc2011-07-28 09:54:37 +0000182 DRD_(g_threadinfo)[i].valid = True;
bartbedfd232009-03-26 19:07:15 +0000183 DRD_(g_threadinfo)[i].vg_thread_exists = True;
184 DRD_(g_threadinfo)[i].vg_threadid = tid;
185 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
186 DRD_(g_threadinfo)[i].stack_min = 0;
187 DRD_(g_threadinfo)[i].stack_min_min = 0;
188 DRD_(g_threadinfo)[i].stack_startup = 0;
189 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000190 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000191 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000192 DRD_(g_threadinfo)[i].is_recording_loads = True;
193 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000194 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000195 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart6d956dc2011-07-28 09:54:37 +0000196 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
bartbedfd232009-03-26 19:07:15 +0000197 tl_assert(DRD_(g_threadinfo)[i].first == 0);
198 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000199
bartbedfd232009-03-26 19:07:15 +0000200 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000201
bartbedfd232009-03-26 19:07:15 +0000202 return i;
203 }
204 }
sewardjaf44c822007-11-25 14:01:38 +0000205
bart3c9afb12009-07-24 11:11:30 +0000206 VG_(printf)(
207"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
208"Aborting.\n");
209
bartbedfd232009-03-26 19:07:15 +0000210 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000211
bartbedfd232009-03-26 19:07:15 +0000212 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000213}
214
bart86a87df2009-03-04 19:26:47 +0000215/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000216DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000217{
bartbedfd232009-03-26 19:07:15 +0000218 int i;
sewardjaf44c822007-11-25 14:01:38 +0000219
bartb48bde22009-07-31 08:26:17 +0000220 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000221 {
bartb48bde22009-07-31 08:26:17 +0000222 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000223 {
bartb48bde22009-07-31 08:26:17 +0000224 if (DRD_(g_threadinfo)[i].posix_thread_exists
225 && DRD_(g_threadinfo)[i].pt_threadid == tid)
226 {
227 return i;
228 }
bartbedfd232009-03-26 19:07:15 +0000229 }
230 }
231 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000232}
233
bart86a87df2009-03-04 19:26:47 +0000234/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000235ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000236{
bartbedfd232009-03-26 19:07:15 +0000237 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
238 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000239
bartbedfd232009-03-26 19:07:15 +0000240 return (DRD_(g_threadinfo)[tid].vg_thread_exists
241 ? DRD_(g_threadinfo)[tid].vg_threadid
242 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000243}
244
bart8f822af2009-06-08 18:20:42 +0000245#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000246/**
247 * Sanity check of the doubly linked list of segments referenced by a
248 * ThreadInfo struct.
249 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000250 */
bart62a784c2009-02-15 13:11:14 +0000251static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000252{
bartbedfd232009-03-26 19:07:15 +0000253 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000254
bartbedfd232009-03-26 19:07:15 +0000255 for (p = ti->first; p; p = p->next) {
256 if (p->next && p->next->prev != p)
257 return False;
258 if (p->next == 0 && p != ti->last)
259 return False;
260 }
261 for (p = ti->last; p; p = p->prev) {
262 if (p->prev && p->prev->next != p)
263 return False;
264 if (p->prev == 0 && p != ti->first)
265 return False;
266 }
267 return True;
sewardjaf44c822007-11-25 14:01:38 +0000268}
bart23d3a4e2008-04-05 12:53:00 +0000269#endif
sewardjaf44c822007-11-25 14:01:38 +0000270
bart439c55f2009-02-15 10:38:37 +0000271/**
272 * Create the first segment for a newly started thread.
273 *
274 * This function is called from the handler installed via
275 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
276 * from the context of the creator thread, before the new thread has been
277 * created.
bart86a87df2009-03-04 19:26:47 +0000278 *
279 * @param[in] creator DRD thread ID of the creator thread.
280 * @param[in] vg_created Valgrind thread ID of the created thread.
281 *
282 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000283 */
bart62a784c2009-02-15 13:11:14 +0000284DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
285 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000286{
bartbedfd232009-03-26 19:07:15 +0000287 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000288
bartbedfd232009-03-26 19:07:15 +0000289 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
290 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
291 tl_assert(0 <= (int)created && created < DRD_N_THREADS
292 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000293
bartbedfd232009-03-26 19:07:15 +0000294 tl_assert(DRD_(g_threadinfo)[created].first == 0);
295 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart8f822af2009-06-08 18:20:42 +0000296 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000297 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000298
bartbedfd232009-03-26 19:07:15 +0000299 return created;
sewardjaf44c822007-11-25 14:01:38 +0000300}
301
bart439c55f2009-02-15 10:38:37 +0000302/**
bart86a87df2009-03-04 19:26:47 +0000303 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
304 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000305 * on the newly created thread, e.g. from the handler installed via
306 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000307 *
308 * @param[in] vg_created Valgrind thread ID of the newly created thread.
309 *
310 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000311 */
bart62a784c2009-02-15 13:11:14 +0000312DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000313{
bartbedfd232009-03-26 19:07:15 +0000314 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000315
bartbedfd232009-03-26 19:07:15 +0000316 tl_assert(0 <= (int)created && created < DRD_N_THREADS
317 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000318
bartbedfd232009-03-26 19:07:15 +0000319 DRD_(g_threadinfo)[created].stack_max
320 = VG_(thread_get_stack_max)(vg_created);
321 DRD_(g_threadinfo)[created].stack_startup
322 = DRD_(g_threadinfo)[created].stack_max;
323 DRD_(g_threadinfo)[created].stack_min
324 = DRD_(g_threadinfo)[created].stack_max;
325 DRD_(g_threadinfo)[created].stack_min_min
326 = DRD_(g_threadinfo)[created].stack_max;
327 DRD_(g_threadinfo)[created].stack_size
328 = VG_(thread_get_stack_size)(vg_created);
329 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000330
bartbedfd232009-03-26 19:07:15 +0000331 return created;
bart439c55f2009-02-15 10:38:37 +0000332}
bart09dc13f2009-02-14 15:13:31 +0000333
bart6d956dc2011-07-28 09:54:37 +0000334static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
335{
336 int j;
337
338 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
339 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
340 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
341#if 0
342 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
343#endif
344 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
345 for (j = 0; j < DRD_N_THREADS; ++j) {
346 if (DRD_(IsValidDrdThreadId)(j)
347 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
348 {
349 s_deletion_tail++;
350#if 0
351 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
352#endif
353 DRD_(thread_delete)(j, False);
354 break;
355 }
356 }
357 }
358}
359
bart324a23b2009-02-15 12:14:52 +0000360/**
361 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
362 * after thread drd_joiner joined thread drd_joinee.
363 */
bart09dc13f2009-02-14 15:13:31 +0000364void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
365{
bartbedfd232009-03-26 19:07:15 +0000366 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
367 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000368
bartbedfd232009-03-26 19:07:15 +0000369 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000370 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000371 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000372
bartbedfd232009-03-26 19:07:15 +0000373 if (s_trace_fork_join)
374 {
375 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000376 const unsigned msg_size = 256;
377 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000378
bartbedfd232009-03-26 19:07:15 +0000379 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
380 tl_assert(msg);
381 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000382 "drd_post_thread_join joiner = %d, joinee = %d",
383 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000384 if (joiner)
385 {
bart8f822af2009-06-08 18:20:42 +0000386 char* vc;
387
388 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000389 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000390 ", new vc: %s", vc);
391 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000392 }
bartb92ff0f2011-10-08 08:29:29 +0000393 DRD_(trace_msg)("%s\n", msg);
bartbedfd232009-03-26 19:07:15 +0000394 VG_(free)(msg);
395 }
bart09dc13f2009-02-14 15:13:31 +0000396
bartbedfd232009-03-26 19:07:15 +0000397 if (! DRD_(get_check_stack_accesses)())
398 {
399 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
400 - DRD_(thread_get_stack_size)(drd_joinee),
401 DRD_(thread_get_stack_max)(drd_joinee));
402 }
403 DRD_(clientobj_delete_thread)(drd_joinee);
bart6d956dc2011-07-28 09:54:37 +0000404 DRD_(thread_delayed_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000405}
406
bart324a23b2009-02-15 12:14:52 +0000407/**
408 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
409 * and accesses this data structure from multiple threads without locking.
410 * Any conflicting accesses in the range stack_startup..stack_max will be
411 * ignored.
412 */
bart62a784c2009-02-15 13:11:14 +0000413void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
414 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000415{
bartbedfd232009-03-26 19:07:15 +0000416 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
417 && tid != DRD_INVALID_THREADID);
418 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
419 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
420 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000421}
422
bart86a87df2009-03-04 19:26:47 +0000423/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000424Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000425{
bartbedfd232009-03-26 19:07:15 +0000426 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
427 && tid != DRD_INVALID_THREADID);
428 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000429}
430
bart86a87df2009-03-04 19:26:47 +0000431/**
432 * Return the lowest value that was ever assigned to the stack pointer
433 * for the specified thread.
434 */
bart62a784c2009-02-15 13:11:14 +0000435Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000436{
bartbedfd232009-03-26 19:07:15 +0000437 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
438 && tid != DRD_INVALID_THREADID);
439 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000440}
441
bart86a87df2009-03-04 19:26:47 +0000442/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000443Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000444{
bartbedfd232009-03-26 19:07:15 +0000445 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
446 && tid != DRD_INVALID_THREADID);
447 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000448}
449
bart86a87df2009-03-04 19:26:47 +0000450/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000451SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000452{
bartbedfd232009-03-26 19:07:15 +0000453 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
454 && tid != DRD_INVALID_THREADID);
455 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000456}
457
bart383d6132010-09-02 14:43:18 +0000458Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
459{
460 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
461 && tid != DRD_INVALID_THREADID);
462 return DRD_(g_threadinfo)[tid].on_alt_stack;
463}
464
465void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
466 const Bool on_alt_stack)
467{
468 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
469 && tid != DRD_INVALID_THREADID);
470 tl_assert(on_alt_stack == !!on_alt_stack);
471 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
472}
473
474Int DRD_(thread_get_threads_on_alt_stack)(void)
475{
476 int i, n = 0;
477
478 for (i = 1; i < DRD_N_THREADS; i++)
479 n += DRD_(g_threadinfo)[i].on_alt_stack;
480 return n;
481}
482
bart09dc13f2009-02-14 15:13:31 +0000483/**
bart6d956dc2011-07-28 09:54:37 +0000484 * Clean up thread-specific data structures.
sewardjaf44c822007-11-25 14:01:38 +0000485 */
bart9194e932011-02-09 11:55:12 +0000486void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
sewardjaf44c822007-11-25 14:01:38 +0000487{
bartbedfd232009-03-26 19:07:15 +0000488 Segment* sg;
489 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000490
bartbedfd232009-03-26 19:07:15 +0000491 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000492
bartbedfd232009-03-26 19:07:15 +0000493 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
494 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
495 {
496 sg_prev = sg->prev;
497 sg->prev = 0;
498 sg->next = 0;
499 DRD_(sg_put)(sg);
500 }
bart6d956dc2011-07-28 09:54:37 +0000501 DRD_(g_threadinfo)[tid].valid = False;
bartbedfd232009-03-26 19:07:15 +0000502 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
503 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart9194e932011-02-09 11:55:12 +0000504 if (detached)
505 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
506 else
507 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
bartbedfd232009-03-26 19:07:15 +0000508 DRD_(g_threadinfo)[tid].first = 0;
509 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000510
bartbedfd232009-03-26 19:07:15 +0000511 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000512}
513
bart324a23b2009-02-15 12:14:52 +0000514/**
515 * Called after a thread performed its last memory access and before
516 * thread_delete() is called. Note: thread_delete() is only called for
517 * joinable threads, not for detached threads.
518 */
bart62a784c2009-02-15 13:11:14 +0000519void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000520{
bartbedfd232009-03-26 19:07:15 +0000521 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
522 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000523
bartbedfd232009-03-26 19:07:15 +0000524 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000525
bartbedfd232009-03-26 19:07:15 +0000526 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
527 {
528 /*
529 * Once a detached thread has finished, its stack is deallocated and
530 * should no longer be taken into account when computing the conflict set.
531 */
532 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000533
bartbedfd232009-03-26 19:07:15 +0000534 /*
535 * For a detached thread, calling pthread_exit() invalidates the
536 * POSIX thread ID associated with the detached thread. For joinable
537 * POSIX threads however, the POSIX thread ID remains live after the
538 * pthread_exit() call until pthread_join() is called.
539 */
540 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
541 }
sewardjaf44c822007-11-25 14:01:38 +0000542}
543
bart5c7e6b62011-02-03 17:47:50 +0000544/** Called just after fork() in the child process. */
545void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
546{
547 unsigned i;
548
549 for (i = 1; i < DRD_N_THREADS; i++)
550 {
551 if (i == tid)
552 continue;
553 if (DRD_(IsValidDrdThreadId(i)))
bart9194e932011-02-09 11:55:12 +0000554 DRD_(thread_delete)(i, True);
bart5c7e6b62011-02-03 17:47:50 +0000555 tl_assert(!DRD_(IsValidDrdThreadId(i)));
556 }
557}
558
bart9b2974a2008-09-27 12:35:31 +0000559/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000560void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000561{
bartbedfd232009-03-26 19:07:15 +0000562 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
563 && tid != DRD_INVALID_THREADID);
564 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000565
bart0c0cd772011-03-03 19:59:20 +0000566 if (DRD_(thread_get_trace_fork_join)())
bartb92ff0f2011-10-08 08:29:29 +0000567 DRD_(trace_msg)("[%d] drd_thread_pre_cancel %d\n",
568 DRD_(g_drd_running_tid), tid);
bartaf0691b2008-09-27 12:26:50 +0000569}
570
barte7dff242009-04-23 17:12:39 +0000571/**
572 * Store the POSIX thread ID for the specified thread.
573 *
574 * @note This function can be called two times for the same thread -- see also
575 * the comment block preceding the pthread_create() wrapper in
576 * drd_pthread_intercepts.c.
577 */
bart62a784c2009-02-15 13:11:14 +0000578void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000579{
bartbedfd232009-03-26 19:07:15 +0000580 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
581 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000582 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
583 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000584 tl_assert(ptid != INVALID_POSIX_THREADID);
585 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
586 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000587}
588
bart86a87df2009-03-04 19:26:47 +0000589/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000590Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000591{
bartbedfd232009-03-26 19:07:15 +0000592 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
593 && tid != DRD_INVALID_THREADID);
594 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000595}
596
bart86a87df2009-03-04 19:26:47 +0000597/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000598void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000599{
bartbedfd232009-03-26 19:07:15 +0000600 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
601 && tid != DRD_INVALID_THREADID);
602 tl_assert(!! joinable == joinable);
603 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000604
bartbedfd232009-03-26 19:07:15 +0000605 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000606}
607
bartdd75cdf2009-07-24 08:20:10 +0000608/** Tells DRD that the calling thread is about to enter pthread_create(). */
609void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
610{
611 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
612 && tid != DRD_INVALID_THREADID);
613 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
614 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
615
616 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
617}
618
619/** Tells DRD that the calling thread has left pthread_create(). */
620void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
621{
622 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
623 && tid != DRD_INVALID_THREADID);
624 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
625 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
626
627 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
628}
629
bartd45d9952009-05-31 18:53:54 +0000630/** Obtain the thread number and the user-assigned thread name. */
631const char* DRD_(thread_get_name)(const DrdThreadId tid)
632{
633 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
634 && tid != DRD_INVALID_THREADID);
635
636 return DRD_(g_threadinfo)[tid].name;
637}
638
639/** Set the name of the specified thread. */
640void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
641{
642 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
643 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000644
bartd45d9952009-05-31 18:53:54 +0000645 if (name == NULL || name[0] == 0)
646 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
647 sizeof(DRD_(g_threadinfo)[tid].name),
648 "Thread %d",
649 tid);
650 else
651 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
652 sizeof(DRD_(g_threadinfo)[tid].name),
653 "Thread %d (%s)",
654 tid, name);
655 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
656}
657
bart86a87df2009-03-04 19:26:47 +0000658/**
659 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
660 * conflict set.
661 */
bart62a784c2009-02-15 13:11:14 +0000662void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000663{
bartbedfd232009-03-26 19:07:15 +0000664 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000665
bartbedfd232009-03-26 19:07:15 +0000666 if (vg_tid != s_vg_running_tid)
667 {
668 DRD_(thread_set_running_tid)(vg_tid,
669 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
670 }
sewardj8b09d4f2007-12-04 21:27:18 +0000671
bartbedfd232009-03-26 19:07:15 +0000672 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
673 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000674}
675
bart86a87df2009-03-04 19:26:47 +0000676/**
677 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
678 * conflict set.
679 */
bart62a784c2009-02-15 13:11:14 +0000680void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
681 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000682{
bartbedfd232009-03-26 19:07:15 +0000683 tl_assert(vg_tid != VG_INVALID_THREADID);
684 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000685
bartbedfd232009-03-26 19:07:15 +0000686 if (vg_tid != s_vg_running_tid)
687 {
688 if (s_trace_context_switches
689 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
690 {
691 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000692 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000693 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000694 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000695 DRD_(sg_get_segments_alive_count)());
696 }
697 s_vg_running_tid = vg_tid;
698 DRD_(g_drd_running_tid) = drd_tid;
699 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
700 s_context_switch_count++;
701 }
sewardj8b09d4f2007-12-04 21:27:18 +0000702
bartbedfd232009-03-26 19:07:15 +0000703 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
704 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000705}
706
bart86a87df2009-03-04 19:26:47 +0000707/**
708 * Increase the synchronization nesting counter. Must be called before the
709 * client calls a synchronization function.
710 */
bart62a784c2009-02-15 13:11:14 +0000711int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000712{
bartbedfd232009-03-26 19:07:15 +0000713 tl_assert(DRD_(IsValidDrdThreadId)(tid));
714 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000715}
716
bart86a87df2009-03-04 19:26:47 +0000717/**
718 * Decrease the synchronization nesting counter. Must be called after the
719 * client left a synchronization function.
720 */
bart62a784c2009-02-15 13:11:14 +0000721int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000722{
bartbedfd232009-03-26 19:07:15 +0000723 tl_assert(DRD_(IsValidDrdThreadId)(tid));
724 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
725 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000726}
727
bart86a87df2009-03-04 19:26:47 +0000728/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000729int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000730{
bartbedfd232009-03-26 19:07:15 +0000731 tl_assert(DRD_(IsValidDrdThreadId)(tid));
732 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000733}
734
bart1a473c72008-03-13 19:03:38 +0000735/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000736static
bart86a87df2009-03-04 19:26:47 +0000737void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000738{
bartbedfd232009-03-26 19:07:15 +0000739 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
740 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000741
742#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
743 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
744#endif
745
bartbedfd232009-03-26 19:07:15 +0000746 sg->prev = DRD_(g_threadinfo)[tid].last;
747 sg->next = 0;
748 if (DRD_(g_threadinfo)[tid].last)
749 DRD_(g_threadinfo)[tid].last->next = sg;
750 DRD_(g_threadinfo)[tid].last = sg;
751 if (DRD_(g_threadinfo)[tid].first == 0)
752 DRD_(g_threadinfo)[tid].first = sg;
bart8f822af2009-06-08 18:20:42 +0000753
754#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
755 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
756#endif
sewardjaf44c822007-11-25 14:01:38 +0000757}
758
bart324a23b2009-02-15 12:14:52 +0000759/**
760 * Remove a segment from the segment list of thread threadid, and free the
761 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000762 */
bart62a784c2009-02-15 13:11:14 +0000763static
bart86a87df2009-03-04 19:26:47 +0000764void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000765{
bartbedfd232009-03-26 19:07:15 +0000766 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
767 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000768
769#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
770 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
771#endif
bart26f73e12008-02-24 18:37:08 +0000772
bartbedfd232009-03-26 19:07:15 +0000773 if (sg->prev)
774 sg->prev->next = sg->next;
775 if (sg->next)
776 sg->next->prev = sg->prev;
777 if (sg == DRD_(g_threadinfo)[tid].first)
778 DRD_(g_threadinfo)[tid].first = sg->next;
779 if (sg == DRD_(g_threadinfo)[tid].last)
780 DRD_(g_threadinfo)[tid].last = sg->prev;
781 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000782
bart8f822af2009-06-08 18:20:42 +0000783#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
784 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
785#endif
sewardjaf44c822007-11-25 14:01:38 +0000786}
787
bart86a87df2009-03-04 19:26:47 +0000788/**
789 * Returns a pointer to the vector clock of the most recent segment associated
790 * with thread 'tid'.
791 */
bart62a784c2009-02-15 13:11:14 +0000792VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000793{
bartbedfd232009-03-26 19:07:15 +0000794 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
795 && tid != DRD_INVALID_THREADID);
796 tl_assert(DRD_(g_threadinfo)[tid].last);
797 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000798}
799
bart324a23b2009-02-15 12:14:52 +0000800/**
801 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000802 */
bart62a784c2009-02-15 13:11:14 +0000803void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000804{
bartbedfd232009-03-26 19:07:15 +0000805 tl_assert(sg);
806 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
807 && tid != DRD_INVALID_THREADID);
808 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000809
bartbedfd232009-03-26 19:07:15 +0000810 DRD_(sg_put)(*sg);
811 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000812}
813
sewardjaf44c822007-11-25 14:01:38 +0000814/**
815 * Compute the minimum of all latest vector clocks of all threads
816 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000817 *
sewardjaf44c822007-11-25 14:01:38 +0000818 * @param vc pointer to a vectorclock, holds result upon return.
819 */
bart62a784c2009-02-15 13:11:14 +0000820static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000821{
bartbedfd232009-03-26 19:07:15 +0000822 unsigned i;
823 Bool first;
824 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000825
bartbedfd232009-03-26 19:07:15 +0000826 first = True;
bart8f822af2009-06-08 18:20:42 +0000827 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000828 {
829 latest_sg = DRD_(g_threadinfo)[i].last;
830 if (latest_sg)
831 {
832 if (first)
833 DRD_(vc_assign)(vc, &latest_sg->vc);
834 else
835 DRD_(vc_min)(vc, &latest_sg->vc);
836 first = False;
837 }
838 }
sewardjaf44c822007-11-25 14:01:38 +0000839}
840
bart86a87df2009-03-04 19:26:47 +0000841/**
842 * Compute the maximum of all latest vector clocks of all threads.
843 *
844 * @param vc pointer to a vectorclock, holds result upon return.
845 */
bart62a784c2009-02-15 13:11:14 +0000846static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000847{
bartbedfd232009-03-26 19:07:15 +0000848 unsigned i;
849 Bool first;
850 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000851
bartbedfd232009-03-26 19:07:15 +0000852 first = True;
bart8f822af2009-06-08 18:20:42 +0000853 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000854 {
855 latest_sg = DRD_(g_threadinfo)[i].last;
856 if (latest_sg)
857 {
858 if (first)
859 DRD_(vc_assign)(vc, &latest_sg->vc);
860 else
861 DRD_(vc_combine)(vc, &latest_sg->vc);
862 first = False;
863 }
864 }
sewardjaf44c822007-11-25 14:01:38 +0000865}
866
867/**
bart5bd9f2d2008-03-03 20:31:58 +0000868 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000869 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000870 * data race.
871 */
bart8f822af2009-06-08 18:20:42 +0000872static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000873{
bartbedfd232009-03-26 19:07:15 +0000874 unsigned i;
875 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000876
bartbedfd232009-03-26 19:07:15 +0000877 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000878
bartbedfd232009-03-26 19:07:15 +0000879 DRD_(vc_init)(&thread_vc_min, 0, 0);
880 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
881 if (DRD_(sg_get_trace)())
882 {
bart8f822af2009-06-08 18:20:42 +0000883 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000884 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000885
bartbedfd232009-03-26 19:07:15 +0000886 DRD_(vc_init)(&thread_vc_max, 0, 0);
887 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000888 vc_min = DRD_(vc_aprint)(&thread_vc_min);
889 vc_max = DRD_(vc_aprint)(&thread_vc_max);
890 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000891 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000892 vc_min, vc_max);
893 VG_(free)(vc_min);
894 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000895 DRD_(vc_cleanup)(&thread_vc_max);
896 }
sewardjaf44c822007-11-25 14:01:38 +0000897
bart8f822af2009-06-08 18:20:42 +0000898 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000899 {
900 Segment* sg;
901 Segment* sg_next;
902 for (sg = DRD_(g_threadinfo)[i].first;
903 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
904 sg = sg_next)
905 {
906 thread_discard_segment(i, sg);
907 }
908 }
909 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000910}
911
bart324a23b2009-02-15 12:14:52 +0000912/**
bart8f822af2009-06-08 18:20:42 +0000913 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
914 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
915 * all segments in the set CS are ordered consistently against both sg1 and
916 * sg2. The set CS is defined as the set of segments that can immediately
917 * precede future segments via inter-thread synchronization operations. In
918 * DRD the set CS consists of the latest segment of each thread combined with
919 * all segments for which the reference count is strictly greater than one.
920 * The code below is an optimized version of the following:
921 *
922 * for (i = 0; i < DRD_N_THREADS; i++)
923 * {
924 * Segment* sg;
925 *
926 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
927 * {
928 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
929 * {
930 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
931 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
932 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
933 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
934 * {
935 * return False;
936 * }
937 * }
938 * }
939 * }
940 */
941static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
942 Segment* const sg1,
943 Segment* const sg2)
944{
945 unsigned i;
946
947 tl_assert(sg1->next);
948 tl_assert(sg2->next);
949 tl_assert(sg1->next == sg2);
950 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
951
952 for (i = 0; i < DRD_N_THREADS; i++)
953 {
954 Segment* sg;
955
956 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
957 {
958 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
959 {
960 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
961 break;
962 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
963 return False;
964 }
965 }
966 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
967 {
968 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
969 {
970 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
971 break;
972 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
973 return False;
974 }
975 }
976 }
977 return True;
978}
979
980/**
bart324a23b2009-02-15 12:14:52 +0000981 * Merge all segments that may be merged without triggering false positives
982 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000983 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
984 * and Koen De Bosschere. Bounding the number of segment histories during
985 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
986 * pp 1221-1238, September 2002. This paper contains a proof that merging
987 * consecutive segments for which the property equiv(s1,s2) holds can be
988 * merged without reducing the accuracy of datarace detection. Furthermore
989 * it is also proven that the total number of all segments will never grow
990 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
991 * every time a new segment is created. The property equiv(s1, s2) is defined
992 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
993 * clocks of segments s and s1 are ordered in the same way as those of segments
994 * s and s2. The set CS is defined as the set of existing segments s that have
995 * the potential to conflict with not yet created segments, either because the
996 * segment s is the latest segment of a thread or because it can become the
997 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000998 */
999static void thread_merge_segments(void)
1000{
bartbedfd232009-03-26 19:07:15 +00001001 unsigned i;
barta9c37392008-03-22 09:38:48 +00001002
bart8f822af2009-06-08 18:20:42 +00001003 s_new_segments_since_last_merge = 0;
1004
1005 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001006 {
1007 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001008
bart8f822af2009-06-08 18:20:42 +00001009#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1010 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1011#endif
barta9c37392008-03-22 09:38:48 +00001012
bartbedfd232009-03-26 19:07:15 +00001013 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +00001014 {
bartbedfd232009-03-26 19:07:15 +00001015 if (DRD_(sg_get_refcnt)(sg) == 1
1016 && sg->next
1017 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +00001018 && sg->next->next
1019 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +00001020 {
1021 /* Merge sg and sg->next into sg. */
1022 DRD_(sg_merge)(sg, sg->next);
1023 thread_discard_segment(i, sg->next);
1024 }
barta9c37392008-03-22 09:38:48 +00001025 }
barta9c37392008-03-22 09:38:48 +00001026
bart8f822af2009-06-08 18:20:42 +00001027#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1028 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1029#endif
bartbedfd232009-03-26 19:07:15 +00001030 }
barta9c37392008-03-22 09:38:48 +00001031}
1032
bart324a23b2009-02-15 12:14:52 +00001033/**
bart324a23b2009-02-15 12:14:52 +00001034 * Create a new segment for the specified thread, and discard any segments
1035 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +00001036 */
bart62a784c2009-02-15 13:11:14 +00001037void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001038{
bart8f822af2009-06-08 18:20:42 +00001039 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001040 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001041
bartbedfd232009-03-26 19:07:15 +00001042 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1043 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +00001044 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001045
bart8f822af2009-06-08 18:20:42 +00001046 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +00001047 new_sg = DRD_(sg_new)(tid, tid);
1048 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001049 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001050 {
bart8f822af2009-06-08 18:20:42 +00001051 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001052 s_update_conflict_set_new_sg_count++;
1053 }
bartd66e3a82008-04-06 15:02:17 +00001054
bart8f822af2009-06-08 18:20:42 +00001055 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001056
bart8f822af2009-06-08 18:20:42 +00001057 if (s_segment_merging
1058 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001059 {
bart8f822af2009-06-08 18:20:42 +00001060 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001061 thread_merge_segments();
1062 }
sewardjaf44c822007-11-25 14:01:38 +00001063}
1064
bart26f73e12008-02-24 18:37:08 +00001065/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001066void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001067{
bartbedfd232009-03-26 19:07:15 +00001068 tl_assert(joiner != joinee);
1069 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1070 && joiner != DRD_INVALID_THREADID);
1071 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1072 && joinee != DRD_INVALID_THREADID);
1073 tl_assert(DRD_(g_threadinfo)[joiner].last);
1074 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +00001075
1076 if (DRD_(sg_get_trace)())
1077 {
1078 char *str1, *str2;
1079 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
1080 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001081 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001082 str1, str2);
1083 VG_(free)(str1);
1084 VG_(free)(str2);
1085 }
barte5214662009-06-21 11:51:23 +00001086 if (joiner == DRD_(g_drd_running_tid))
1087 {
1088 VectorClock old_vc;
1089
1090 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
1091 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001092 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001093 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1094 s_update_conflict_set_join_count++;
1095 DRD_(vc_cleanup)(&old_vc);
1096 }
1097 else
1098 {
1099 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001100 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001101 }
1102
1103 thread_discard_ordered_segments();
1104
bart8f822af2009-06-08 18:20:42 +00001105 if (DRD_(sg_get_trace)())
1106 {
1107 char* str;
1108 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001109 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001110 VG_(free)(str);
1111 }
sewardjaf44c822007-11-25 14:01:38 +00001112}
1113
bart324a23b2009-02-15 12:14:52 +00001114/**
bart8f822af2009-06-08 18:20:42 +00001115 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001116 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001117 */
bartf6ec1fe2009-06-21 18:07:35 +00001118static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001119{
bart8f822af2009-06-08 18:20:42 +00001120 const VectorClock* const vc = &sg->vc;
1121
bartbedfd232009-03-26 19:07:15 +00001122 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1123 && tid != DRD_INVALID_THREADID);
1124 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001125 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001126 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001127
1128 if (tid != sg->tid)
1129 {
1130 VectorClock old_vc;
1131
1132 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1133 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1134 if (DRD_(sg_get_trace)())
1135 {
1136 char *str1, *str2;
1137 str1 = DRD_(vc_aprint)(&old_vc);
1138 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001139 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001140 VG_(free)(str1);
1141 VG_(free)(str2);
1142 }
barte5214662009-06-21 11:51:23 +00001143
bart8f822af2009-06-08 18:20:42 +00001144 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001145
bart8f822af2009-06-08 18:20:42 +00001146 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001147 s_update_conflict_set_sync_count++;
1148
bart8f822af2009-06-08 18:20:42 +00001149 DRD_(vc_cleanup)(&old_vc);
1150 }
1151 else
1152 {
1153 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1154 }
sewardjaf44c822007-11-25 14:01:38 +00001155}
1156
bart324a23b2009-02-15 12:14:52 +00001157/**
bartf6ec1fe2009-06-21 18:07:35 +00001158 * Create a new segment for thread tid and update the vector clock of the last
1159 * segment of this thread with the the vector clock of segment sg. Call this
1160 * function after thread tid had to wait because of thread synchronization
1161 * until the memory accesses in the segment sg finished.
1162 */
1163void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1164{
1165 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1166 && tid != DRD_INVALID_THREADID);
1167 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1168 tl_assert(sg);
1169
1170 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1171
1172 thread_combine_vc_sync(tid, sg);
1173
1174 if (s_segment_merging
1175 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1176 {
1177 thread_discard_ordered_segments();
1178 thread_merge_segments();
1179 }
1180}
1181
1182/**
bart324a23b2009-02-15 12:14:52 +00001183 * Call this function whenever a thread is no longer using the memory
1184 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1185 * increase.
bart26f73e12008-02-24 18:37:08 +00001186 */
bart23ef19d2011-03-12 12:34:44 +00001187void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001188{
bartbedfd232009-03-26 19:07:15 +00001189 unsigned i;
bart178b6862011-07-29 12:30:43 +00001190 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001191
bart8f822af2009-06-08 18:20:42 +00001192 for (i = 0; i < DRD_N_THREADS; i++)
bart178b6862011-07-29 12:30:43 +00001193 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1194 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001195
bart178b6862011-07-29 12:30:43 +00001196 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001197}
1198
bartd45d9952009-05-31 18:53:54 +00001199/** Specify whether memory loads should be recorded. */
1200void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001201{
bartbedfd232009-03-26 19:07:15 +00001202 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1203 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001204 tl_assert(enabled == !! enabled);
1205
1206 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001207}
1208
bartd45d9952009-05-31 18:53:54 +00001209/** Specify whether memory stores should be recorded. */
1210void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001211{
bartbedfd232009-03-26 19:07:15 +00001212 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1213 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001214 tl_assert(enabled == !! enabled);
1215
1216 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001217}
1218
bart86a87df2009-03-04 19:26:47 +00001219/**
1220 * Print the segment information for all threads.
1221 *
1222 * This function is only used for debugging purposes.
1223 */
bart62a784c2009-02-15 13:11:14 +00001224void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001225{
bartbedfd232009-03-26 19:07:15 +00001226 unsigned i;
1227 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001228
bart8f822af2009-06-08 18:20:42 +00001229 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001230 {
1231 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001232 {
bartbedfd232009-03-26 19:07:15 +00001233 VG_(printf)("**************\n"
bart6d956dc2011-07-28 09:54:37 +00001234 "* thread %3d (%d/%d/%d/%d/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001235 "**************\n",
1236 i,
bart6d956dc2011-07-28 09:54:37 +00001237 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001238 DRD_(g_threadinfo)[i].vg_thread_exists,
1239 DRD_(g_threadinfo)[i].vg_threadid,
1240 DRD_(g_threadinfo)[i].posix_thread_exists,
1241 DRD_(g_threadinfo)[i].pt_threadid,
1242 DRD_(g_threadinfo)[i].detached_posix_thread);
1243 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1244 {
1245 DRD_(sg_print)(p);
1246 }
sewardjaf44c822007-11-25 14:01:38 +00001247 }
bartbedfd232009-03-26 19:07:15 +00001248 }
sewardjaf44c822007-11-25 14:01:38 +00001249}
1250
bart86a87df2009-03-04 19:26:47 +00001251/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001252static void show_call_stack(const DrdThreadId tid,
1253 const Char* const msg,
1254 ExeContext* const callstack)
1255{
bartbedfd232009-03-26 19:07:15 +00001256 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001257
bart63c92ea2009-07-19 17:53:56 +00001258 VG_(message)(Vg_UserMsg, "%s (thread %d)\n", msg, tid);
sewardjaf44c822007-11-25 14:01:38 +00001259
bartbedfd232009-03-26 19:07:15 +00001260 if (vg_tid != VG_INVALID_THREADID)
1261 {
1262 if (callstack)
1263 {
1264 VG_(pp_ExeContext)(callstack);
1265 }
1266 else
1267 {
1268 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
1269 }
1270 }
1271 else
1272 {
1273 VG_(message)(Vg_UserMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001274 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001275 }
sewardjaf44c822007-11-25 14:01:38 +00001276}
1277
bart86a87df2009-03-04 19:26:47 +00001278/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001279static void
1280thread_report_conflicting_segments_segment(const DrdThreadId tid,
1281 const Addr addr,
1282 const SizeT size,
1283 const BmAccessTypeT access_type,
1284 const Segment* const p)
1285{
bartbedfd232009-03-26 19:07:15 +00001286 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001287
bartbedfd232009-03-26 19:07:15 +00001288 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1289 && tid != DRD_INVALID_THREADID);
1290 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001291
bart8f822af2009-06-08 18:20:42 +00001292 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001293 {
1294 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001295 {
bartbedfd232009-03-26 19:07:15 +00001296 Segment* q;
1297 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1298 {
1299 /*
bart31b983d2010-02-21 14:52:59 +00001300 * Since q iterates over the segments of thread i in order of
1301 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001302 * q->next->vc <= p->vc will also hold. Hence, break out of the
1303 * loop once this condition is met.
1304 */
1305 if (DRD_(vc_lte)(&q->vc, &p->vc))
1306 break;
1307 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1308 {
bart8f822af2009-06-08 18:20:42 +00001309 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001310 access_type))
1311 {
1312 tl_assert(q->stacktrace);
1313 show_call_stack(i, "Other segment start",
1314 q->stacktrace);
1315 show_call_stack(i, "Other segment end",
1316 q->next ? q->next->stacktrace : 0);
1317 }
1318 }
1319 }
sewardjaf44c822007-11-25 14:01:38 +00001320 }
bartbedfd232009-03-26 19:07:15 +00001321 }
sewardjaf44c822007-11-25 14:01:38 +00001322}
1323
bart86a87df2009-03-04 19:26:47 +00001324/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001325void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1326 const Addr addr,
1327 const SizeT size,
1328 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001329{
bartbedfd232009-03-26 19:07:15 +00001330 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001331
bartbedfd232009-03-26 19:07:15 +00001332 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1333 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001334
bartbedfd232009-03-26 19:07:15 +00001335 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
1336 {
bart8f822af2009-06-08 18:20:42 +00001337 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001338 {
1339 thread_report_conflicting_segments_segment(tid, addr, size,
1340 access_type, p);
1341 }
1342 }
sewardjaf44c822007-11-25 14:01:38 +00001343}
sewardjaf44c822007-11-25 14:01:38 +00001344
bart324a23b2009-02-15 12:14:52 +00001345/**
bart8f822af2009-06-08 18:20:42 +00001346 * Verify whether the conflict set for thread tid is up to date. Only perform
1347 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1348 */
1349static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1350{
1351 static int do_verify_conflict_set = -1;
1352 Bool result;
1353 struct bitmap* computed_conflict_set = 0;
1354
1355 if (do_verify_conflict_set < 0)
1356 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1357
1358 if (do_verify_conflict_set == 0)
1359 return True;
1360
1361 thread_compute_conflict_set(&computed_conflict_set, tid);
1362 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1363 if (! result)
1364 {
1365 VG_(printf)("actual conflict set:\n");
1366 DRD_(bm_print)(DRD_(g_conflict_set));
1367 VG_(printf)("\n");
1368 VG_(printf)("computed conflict set:\n");
1369 DRD_(bm_print)(computed_conflict_set);
1370 VG_(printf)("\n");
1371 }
1372 DRD_(bm_delete)(computed_conflict_set);
1373 return result;
1374}
1375
1376/**
1377 * Compute the conflict set: a bitmap that represents the union of all memory
1378 * accesses of all segments that are unordered to the current segment of the
1379 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001380 */
bart86a87df2009-03-04 19:26:47 +00001381static void thread_compute_conflict_set(struct bitmap** conflict_set,
1382 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001383{
bartbedfd232009-03-26 19:07:15 +00001384 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001385
bartbedfd232009-03-26 19:07:15 +00001386 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1387 && tid != DRD_INVALID_THREADID);
1388 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001389
bart54803fe2009-06-21 09:26:27 +00001390 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001391 s_conflict_set_bitmap_creation_count
1392 -= DRD_(bm_get_bitmap_creation_count)();
1393 s_conflict_set_bitmap2_creation_count
1394 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001395
bartbedfd232009-03-26 19:07:15 +00001396 if (*conflict_set)
1397 {
bartf6ec1fe2009-06-21 18:07:35 +00001398 DRD_(bm_cleanup)(*conflict_set);
1399 DRD_(bm_init)(*conflict_set);
bartbedfd232009-03-26 19:07:15 +00001400 }
bartf6ec1fe2009-06-21 18:07:35 +00001401 else
1402 {
1403 *conflict_set = DRD_(bm_new)();
1404 }
bart26f73e12008-02-24 18:37:08 +00001405
bartbedfd232009-03-26 19:07:15 +00001406 if (s_trace_conflict_set)
1407 {
bart8f822af2009-06-08 18:20:42 +00001408 char* str;
bart26f73e12008-02-24 18:37:08 +00001409
bart8f822af2009-06-08 18:20:42 +00001410 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1411 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001412 "computing conflict set for thread %d with vc %s\n",
1413 tid, str);
bart8f822af2009-06-08 18:20:42 +00001414 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001415 }
sewardjaf44c822007-11-25 14:01:38 +00001416
bartbedfd232009-03-26 19:07:15 +00001417 p = DRD_(g_threadinfo)[tid].last;
1418 {
1419 unsigned j;
1420
1421 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001422 {
bart8f822af2009-06-08 18:20:42 +00001423 char* vc;
bartbedfd232009-03-26 19:07:15 +00001424
bart8f822af2009-06-08 18:20:42 +00001425 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001426 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001427 tid, vc);
1428 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001429 }
sewardjaf44c822007-11-25 14:01:38 +00001430
bart8f822af2009-06-08 18:20:42 +00001431 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001432 {
1433 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1434 {
bart8f822af2009-06-08 18:20:42 +00001435 Segment* q;
bartbedfd232009-03-26 19:07:15 +00001436 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
1437 {
1438 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1439 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1440 {
1441 if (s_trace_conflict_set)
1442 {
bart8f822af2009-06-08 18:20:42 +00001443 char* str;
1444
1445 str = DRD_(vc_aprint)(&q->vc);
1446 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001447 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001448 j, str);
1449 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001450 }
bart8f822af2009-06-08 18:20:42 +00001451 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001452 }
1453 else
1454 {
1455 if (s_trace_conflict_set)
1456 {
bart8f822af2009-06-08 18:20:42 +00001457 char* str;
1458
1459 str = DRD_(vc_aprint)(&q->vc);
1460 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001461 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001462 j, str);
1463 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001464 }
1465 }
1466 }
1467 }
1468 }
1469 }
sewardjaf44c822007-11-25 14:01:38 +00001470
bartbedfd232009-03-26 19:07:15 +00001471 s_conflict_set_bitmap_creation_count
1472 += DRD_(bm_get_bitmap_creation_count)();
1473 s_conflict_set_bitmap2_creation_count
1474 += DRD_(bm_get_bitmap2_creation_count)();
1475
bart8f822af2009-06-08 18:20:42 +00001476 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001477 {
sewardj1e29ebc2009-07-15 14:49:17 +00001478 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001479 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001480 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001481 }
sewardjaf44c822007-11-25 14:01:38 +00001482}
1483
bart8f822af2009-06-08 18:20:42 +00001484/**
1485 * Update the conflict set after the vector clock of thread tid has been
1486 * updated from old_vc to its current value, either because a new segment has
1487 * been created or because of a synchronization operation.
1488 */
1489void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1490 const VectorClock* const old_vc)
1491{
1492 const VectorClock* new_vc;
1493 Segment* p;
1494 unsigned j;
1495
1496 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1497 && tid != DRD_INVALID_THREADID);
1498 tl_assert(old_vc);
1499 tl_assert(tid == DRD_(g_drd_running_tid));
1500 tl_assert(DRD_(g_conflict_set));
1501
1502 if (s_trace_conflict_set)
1503 {
1504 char* str;
1505
1506 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
1507 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001508 "updating conflict set for thread %d with vc %s\n",
1509 tid, str);
bart8f822af2009-06-08 18:20:42 +00001510 VG_(free)(str);
1511 }
1512
1513 new_vc = &DRD_(g_threadinfo)[tid].last->vc;
bartf5fe4b62011-07-03 11:39:30 +00001514 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
bart8f822af2009-06-08 18:20:42 +00001515
1516 DRD_(bm_unmark)(DRD_(g_conflict_set));
1517
1518 for (j = 0; j < DRD_N_THREADS; j++)
1519 {
1520 Segment* q;
1521
1522 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1523 continue;
1524
bart178b6862011-07-29 12:30:43 +00001525 for (q = DRD_(g_threadinfo)[j].last;
1526 q && !DRD_(vc_lte)(&q->vc, new_vc);
1527 q = q->prev) {
bart4b3fdb22011-07-03 11:40:49 +00001528 const Bool included_in_old_conflict_set
bart178b6862011-07-29 12:30:43 +00001529 = !DRD_(vc_lte)(old_vc, &q->vc);
bart4b3fdb22011-07-03 11:40:49 +00001530 const Bool included_in_new_conflict_set
bart178b6862011-07-29 12:30:43 +00001531 = !DRD_(vc_lte)(new_vc, &q->vc);
1532
1533 if (UNLIKELY(s_trace_conflict_set)) {
1534 char* str;
1535
1536 str = DRD_(vc_aprint)(&q->vc);
1537 VG_(message)(Vg_DebugMsg,
1538 "conflict set: [%d] %s segment %s\n", j,
1539 included_in_old_conflict_set
1540 != included_in_new_conflict_set
1541 ? "merging" : "ignoring", str);
1542 VG_(free)(str);
1543 }
1544 if (included_in_old_conflict_set != included_in_new_conflict_set)
1545 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1546 }
1547
1548 for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->prev) {
1549 const Bool included_in_old_conflict_set
1550 = !DRD_(vc_lte)(old_vc, &q->vc);
1551 const Bool included_in_new_conflict_set
1552 = !DRD_(vc_lte)(&q->vc, new_vc)
1553 && !DRD_(vc_lte)(new_vc, &q->vc);
1554
1555 if (UNLIKELY(s_trace_conflict_set)) {
bartac5b95b2011-07-03 11:43:45 +00001556 char* str;
1557
1558 str = DRD_(vc_aprint)(&q->vc);
1559 VG_(message)(Vg_DebugMsg,
1560 "conflict set: [%d] %s segment %s\n", j,
1561 included_in_old_conflict_set
1562 != included_in_new_conflict_set
1563 ? "merging" : "ignoring", str);
1564 VG_(free)(str);
1565 }
bart8f822af2009-06-08 18:20:42 +00001566 if (included_in_old_conflict_set != included_in_new_conflict_set)
bart8f822af2009-06-08 18:20:42 +00001567 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001568 }
1569 }
1570
1571 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1572
1573 p = DRD_(g_threadinfo)[tid].last;
bart4b3fdb22011-07-03 11:40:49 +00001574 for (j = 0; j < DRD_N_THREADS; j++)
bart8f822af2009-06-08 18:20:42 +00001575 {
bart4b3fdb22011-07-03 11:40:49 +00001576 if (j != tid && DRD_(IsValidDrdThreadId)(j))
bart8f822af2009-06-08 18:20:42 +00001577 {
bart4b3fdb22011-07-03 11:40:49 +00001578 Segment* q;
bart178b6862011-07-29 12:30:43 +00001579 for (q = DRD_(g_threadinfo)[j].last;
1580 q && !DRD_(vc_lte)(&q->vc, &p->vc);
1581 q = q->prev) {
1582 if (!DRD_(vc_lte)(&p->vc, &q->vc))
bart4b3fdb22011-07-03 11:40:49 +00001583 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001584 }
1585 }
1586 }
1587
1588 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1589
bart54803fe2009-06-21 09:26:27 +00001590 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001591
1592 if (s_trace_conflict_set_bm)
1593 {
sewardj1e29ebc2009-07-15 14:49:17 +00001594 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001595 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001596 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001597 }
1598
1599 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1600}
1601
bart86a87df2009-03-04 19:26:47 +00001602/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001603ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001604{
bartbedfd232009-03-26 19:07:15 +00001605 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001606}
1607
bart86a87df2009-03-04 19:26:47 +00001608/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001609ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001610{
bartbedfd232009-03-26 19:07:15 +00001611 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001612}
1613
bart54803fe2009-06-21 09:26:27 +00001614/** Return how many times the conflict set has been updated entirely. */
1615ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001616{
bart54803fe2009-06-21 09:26:27 +00001617 return s_compute_conflict_set_count;
1618}
1619
1620/** Return how many times the conflict set has been updated partially. */
1621ULong DRD_(thread_get_update_conflict_set_count)(void)
1622{
bartbedfd232009-03-26 19:07:15 +00001623 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001624}
1625
bart86a87df2009-03-04 19:26:47 +00001626/**
barte5214662009-06-21 11:51:23 +00001627 * Return how many times the conflict set has been updated partially
1628 * because a new segment has been created.
1629 */
1630ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1631{
1632 return s_update_conflict_set_new_sg_count;
1633}
1634
1635/**
1636 * Return how many times the conflict set has been updated partially
1637 * because of combining vector clocks due to synchronization operations
1638 * other than reader/writer lock or barrier operations.
1639 */
1640ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1641{
1642 return s_update_conflict_set_sync_count;
1643}
1644
1645/**
1646 * Return how many times the conflict set has been updated partially
1647 * because of thread joins.
1648 */
1649ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1650{
1651 return s_update_conflict_set_join_count;
1652}
1653
1654/**
bart86a87df2009-03-04 19:26:47 +00001655 * Return the number of first-level bitmaps that have been created during
1656 * conflict set updates.
1657 */
bart62a784c2009-02-15 13:11:14 +00001658ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001659{
bartbedfd232009-03-26 19:07:15 +00001660 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001661}
1662
bart86a87df2009-03-04 19:26:47 +00001663/**
1664 * Return the number of second-level bitmaps that have been created during
1665 * conflict set updates.
1666 */
bart62a784c2009-02-15 13:11:14 +00001667ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001668{
bartbedfd232009-03-26 19:07:15 +00001669 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001670}