blob: 2a6270a9786781ccec9d27f180e11597fb9c45e0 [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
bart86562bd2009-02-16 19:43:56 +00002 This file is part of drd, a thread error detector.
sewardjaf44c822007-11-25 14:01:38 +00003
bart922304f2011-03-13 12:02:44 +00004 Copyright (C) 2006-2011 Bart Van Assche <bvanassche@acm.org>.
sewardjaf44c822007-11-25 14:01:38 +00005
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19 02111-1307, USA.
20
21 The GNU General Public License is contained in the file COPYING.
22*/
23
24
25#include "drd_error.h"
bart09dc13f2009-02-14 15:13:31 +000026#include "drd_barrier.h"
bartd2c5eae2009-02-21 15:27:04 +000027#include "drd_clientobj.h"
bart09dc13f2009-02-14 15:13:31 +000028#include "drd_cond.h"
29#include "drd_mutex.h"
sewardjaf44c822007-11-25 14:01:38 +000030#include "drd_segment.h"
bart09dc13f2009-02-14 15:13:31 +000031#include "drd_semaphore.h"
sewardjaf44c822007-11-25 14:01:38 +000032#include "drd_suppression.h"
33#include "drd_thread.h"
bart82195c12008-04-13 17:35:08 +000034#include "pub_tool_vki.h"
sewardjaf44c822007-11-25 14:01:38 +000035#include "pub_tool_basics.h" // Addr, SizeT
sewardjaf44c822007-11-25 14:01:38 +000036#include "pub_tool_libcassert.h" // tl_assert()
37#include "pub_tool_libcbase.h" // VG_(strlen)()
38#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000039#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000040#include "pub_tool_machine.h"
41#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000042#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000043#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
44
bart32ba2082008-06-05 08:53:42 +000045
sewardjaf44c822007-11-25 14:01:38 +000046
bart324a23b2009-02-15 12:14:52 +000047/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000048
bart86a87df2009-03-04 19:26:47 +000049static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
50static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
51static void thread_compute_conflict_set(struct bitmap** conflict_set,
52 const DrdThreadId tid);
bart8f822af2009-06-08 18:20:42 +000053static Bool thread_conflict_set_up_to_date(const DrdThreadId tid);
sewardjaf44c822007-11-25 14:01:38 +000054
55
bart324a23b2009-02-15 12:14:52 +000056/* Local variables. */
sewardjaf44c822007-11-25 14:01:38 +000057
bart86a87df2009-03-04 19:26:47 +000058static ULong s_context_switch_count;
59static ULong s_discard_ordered_segments_count;
bart54803fe2009-06-21 09:26:27 +000060static ULong s_compute_conflict_set_count;
bart86a87df2009-03-04 19:26:47 +000061static ULong s_update_conflict_set_count;
barte5214662009-06-21 11:51:23 +000062static ULong s_update_conflict_set_new_sg_count;
63static ULong s_update_conflict_set_sync_count;
64static ULong s_update_conflict_set_join_count;
bart86a87df2009-03-04 19:26:47 +000065static ULong s_conflict_set_bitmap_creation_count;
66static ULong s_conflict_set_bitmap2_creation_count;
67static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000068DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
69ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
70struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000071static Bool s_trace_context_switches = False;
72static Bool s_trace_conflict_set = False;
bart8f822af2009-06-08 18:20:42 +000073static Bool s_trace_conflict_set_bm = False;
bart86a87df2009-03-04 19:26:47 +000074static Bool s_trace_fork_join = False;
75static Bool s_segment_merging = True;
bart8f822af2009-06-08 18:20:42 +000076static Bool s_new_segments_since_last_merge;
bart6f1d7162009-06-24 18:34:10 +000077static int s_segment_merge_interval = 10;
bart6d956dc2011-07-28 09:54:37 +000078static unsigned s_join_list_vol = 10;
79static unsigned s_deletion_head;
80static unsigned s_deletion_tail;
sewardjaf44c822007-11-25 14:01:38 +000081
82
bart324a23b2009-02-15 12:14:52 +000083/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000084
bart86a87df2009-03-04 19:26:47 +000085/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000086void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000087{
bartbedfd232009-03-26 19:07:15 +000088 tl_assert(t == False || t == True);
89 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000090}
91
bart86a87df2009-03-04 19:26:47 +000092/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000093void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000094{
bartbedfd232009-03-26 19:07:15 +000095 tl_assert(t == False || t == True);
96 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000097}
98
bart8f822af2009-06-08 18:20:42 +000099/** Enables/disables conflict set bitmap tracing. */
100void DRD_(thread_trace_conflict_set_bm)(const Bool t)
101{
102 tl_assert(t == False || t == True);
103 s_trace_conflict_set_bm = t;
104}
105
bart86a87df2009-03-04 19:26:47 +0000106/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +0000107Bool DRD_(thread_get_trace_fork_join)(void)
108{
bartbedfd232009-03-26 19:07:15 +0000109 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +0000110}
111
bart86a87df2009-03-04 19:26:47 +0000112/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +0000113void DRD_(thread_set_trace_fork_join)(const Bool t)
114{
bartbedfd232009-03-26 19:07:15 +0000115 tl_assert(t == False || t == True);
116 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000117}
118
bart86a87df2009-03-04 19:26:47 +0000119/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000120void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000121{
bartbedfd232009-03-26 19:07:15 +0000122 tl_assert(m == False || m == True);
123 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000124}
125
bart8f822af2009-06-08 18:20:42 +0000126/** Get the segment merging interval. */
127int DRD_(thread_get_segment_merge_interval)(void)
128{
129 return s_segment_merge_interval;
130}
131
132/** Set the segment merging interval. */
133void DRD_(thread_set_segment_merge_interval)(const int i)
134{
135 s_segment_merge_interval = i;
136}
137
bart6d956dc2011-07-28 09:54:37 +0000138void DRD_(thread_set_join_list_vol)(const int jlv)
139{
140 s_join_list_vol = jlv;
141}
142
sewardjaf44c822007-11-25 14:01:38 +0000143/**
bart86a87df2009-03-04 19:26:47 +0000144 * Convert Valgrind's ThreadId into a DrdThreadId.
145 *
146 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
147 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000148 */
bart62a784c2009-02-15 13:11:14 +0000149DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000150{
bartbedfd232009-03-26 19:07:15 +0000151 int i;
sewardjaf44c822007-11-25 14:01:38 +0000152
bartbedfd232009-03-26 19:07:15 +0000153 if (tid == VG_INVALID_THREADID)
154 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000155
bartbedfd232009-03-26 19:07:15 +0000156 for (i = 1; i < DRD_N_THREADS; i++)
157 {
158 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
159 && DRD_(g_threadinfo)[i].vg_threadid == tid)
160 {
161 return i;
162 }
163 }
sewardjaf44c822007-11-25 14:01:38 +0000164
bartbedfd232009-03-26 19:07:15 +0000165 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000166}
167
bart86a87df2009-03-04 19:26:47 +0000168/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000169static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000170{
bartbedfd232009-03-26 19:07:15 +0000171 int i;
sewardjaf44c822007-11-25 14:01:38 +0000172
bartbedfd232009-03-26 19:07:15 +0000173 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000174
bartbedfd232009-03-26 19:07:15 +0000175 for (i = 1; i < DRD_N_THREADS; i++)
176 {
bart6d956dc2011-07-28 09:54:37 +0000177 if (!DRD_(g_threadinfo)[i].valid)
bartbedfd232009-03-26 19:07:15 +0000178 {
179 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000180
bart6d956dc2011-07-28 09:54:37 +0000181 DRD_(g_threadinfo)[i].valid = True;
bartbedfd232009-03-26 19:07:15 +0000182 DRD_(g_threadinfo)[i].vg_thread_exists = True;
183 DRD_(g_threadinfo)[i].vg_threadid = tid;
184 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
185 DRD_(g_threadinfo)[i].stack_min = 0;
186 DRD_(g_threadinfo)[i].stack_min_min = 0;
187 DRD_(g_threadinfo)[i].stack_startup = 0;
188 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000189 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000190 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000191 DRD_(g_threadinfo)[i].is_recording_loads = True;
192 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000193 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000194 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart6d956dc2011-07-28 09:54:37 +0000195 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
bartbedfd232009-03-26 19:07:15 +0000196 tl_assert(DRD_(g_threadinfo)[i].first == 0);
197 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000198
bartbedfd232009-03-26 19:07:15 +0000199 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000200
bartbedfd232009-03-26 19:07:15 +0000201 return i;
202 }
203 }
sewardjaf44c822007-11-25 14:01:38 +0000204
bart3c9afb12009-07-24 11:11:30 +0000205 VG_(printf)(
206"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
207"Aborting.\n");
208
bartbedfd232009-03-26 19:07:15 +0000209 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000210
bartbedfd232009-03-26 19:07:15 +0000211 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000212}
213
bart86a87df2009-03-04 19:26:47 +0000214/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000215DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000216{
bartbedfd232009-03-26 19:07:15 +0000217 int i;
sewardjaf44c822007-11-25 14:01:38 +0000218
bartb48bde22009-07-31 08:26:17 +0000219 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000220 {
bartb48bde22009-07-31 08:26:17 +0000221 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000222 {
bartb48bde22009-07-31 08:26:17 +0000223 if (DRD_(g_threadinfo)[i].posix_thread_exists
224 && DRD_(g_threadinfo)[i].pt_threadid == tid)
225 {
226 return i;
227 }
bartbedfd232009-03-26 19:07:15 +0000228 }
229 }
230 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000231}
232
bart86a87df2009-03-04 19:26:47 +0000233/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000234ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000235{
bartbedfd232009-03-26 19:07:15 +0000236 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
237 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000238
bartbedfd232009-03-26 19:07:15 +0000239 return (DRD_(g_threadinfo)[tid].vg_thread_exists
240 ? DRD_(g_threadinfo)[tid].vg_threadid
241 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000242}
243
bart8f822af2009-06-08 18:20:42 +0000244#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000245/**
246 * Sanity check of the doubly linked list of segments referenced by a
247 * ThreadInfo struct.
248 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000249 */
bart62a784c2009-02-15 13:11:14 +0000250static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000251{
bartbedfd232009-03-26 19:07:15 +0000252 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000253
bartbedfd232009-03-26 19:07:15 +0000254 for (p = ti->first; p; p = p->next) {
255 if (p->next && p->next->prev != p)
256 return False;
257 if (p->next == 0 && p != ti->last)
258 return False;
259 }
260 for (p = ti->last; p; p = p->prev) {
261 if (p->prev && p->prev->next != p)
262 return False;
263 if (p->prev == 0 && p != ti->first)
264 return False;
265 }
266 return True;
sewardjaf44c822007-11-25 14:01:38 +0000267}
bart23d3a4e2008-04-05 12:53:00 +0000268#endif
sewardjaf44c822007-11-25 14:01:38 +0000269
bart439c55f2009-02-15 10:38:37 +0000270/**
271 * Create the first segment for a newly started thread.
272 *
273 * This function is called from the handler installed via
274 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
275 * from the context of the creator thread, before the new thread has been
276 * created.
bart86a87df2009-03-04 19:26:47 +0000277 *
278 * @param[in] creator DRD thread ID of the creator thread.
279 * @param[in] vg_created Valgrind thread ID of the created thread.
280 *
281 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000282 */
bart62a784c2009-02-15 13:11:14 +0000283DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
284 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000285{
bartbedfd232009-03-26 19:07:15 +0000286 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000287
bartbedfd232009-03-26 19:07:15 +0000288 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
289 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
290 tl_assert(0 <= (int)created && created < DRD_N_THREADS
291 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000292
bartbedfd232009-03-26 19:07:15 +0000293 tl_assert(DRD_(g_threadinfo)[created].first == 0);
294 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart8f822af2009-06-08 18:20:42 +0000295 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000296 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000297
bartbedfd232009-03-26 19:07:15 +0000298 return created;
sewardjaf44c822007-11-25 14:01:38 +0000299}
300
bart439c55f2009-02-15 10:38:37 +0000301/**
bart86a87df2009-03-04 19:26:47 +0000302 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
303 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000304 * on the newly created thread, e.g. from the handler installed via
305 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000306 *
307 * @param[in] vg_created Valgrind thread ID of the newly created thread.
308 *
309 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000310 */
bart62a784c2009-02-15 13:11:14 +0000311DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000312{
bartbedfd232009-03-26 19:07:15 +0000313 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000314
bartbedfd232009-03-26 19:07:15 +0000315 tl_assert(0 <= (int)created && created < DRD_N_THREADS
316 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000317
bartbedfd232009-03-26 19:07:15 +0000318 DRD_(g_threadinfo)[created].stack_max
319 = VG_(thread_get_stack_max)(vg_created);
320 DRD_(g_threadinfo)[created].stack_startup
321 = DRD_(g_threadinfo)[created].stack_max;
322 DRD_(g_threadinfo)[created].stack_min
323 = DRD_(g_threadinfo)[created].stack_max;
324 DRD_(g_threadinfo)[created].stack_min_min
325 = DRD_(g_threadinfo)[created].stack_max;
326 DRD_(g_threadinfo)[created].stack_size
327 = VG_(thread_get_stack_size)(vg_created);
328 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000329
bartbedfd232009-03-26 19:07:15 +0000330 return created;
bart439c55f2009-02-15 10:38:37 +0000331}
bart09dc13f2009-02-14 15:13:31 +0000332
bart6d956dc2011-07-28 09:54:37 +0000333static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
334{
335 int j;
336
337 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
338 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
339 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
340#if 0
341 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
342#endif
343 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
344 for (j = 0; j < DRD_N_THREADS; ++j) {
345 if (DRD_(IsValidDrdThreadId)(j)
346 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
347 {
348 s_deletion_tail++;
349#if 0
350 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
351#endif
352 DRD_(thread_delete)(j, False);
353 break;
354 }
355 }
356 }
357}
358
bart324a23b2009-02-15 12:14:52 +0000359/**
360 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
361 * after thread drd_joiner joined thread drd_joinee.
362 */
bart09dc13f2009-02-14 15:13:31 +0000363void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
364{
bartbedfd232009-03-26 19:07:15 +0000365 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
366 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000367
bartbedfd232009-03-26 19:07:15 +0000368 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000369 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000370 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000371
bartbedfd232009-03-26 19:07:15 +0000372 if (s_trace_fork_join)
373 {
374 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000375 const unsigned msg_size = 256;
376 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000377
bartbedfd232009-03-26 19:07:15 +0000378 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
379 tl_assert(msg);
380 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000381 "drd_post_thread_join joiner = %d, joinee = %d",
382 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000383 if (joiner)
384 {
bart8f822af2009-06-08 18:20:42 +0000385 char* vc;
386
387 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000388 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000389 ", new vc: %s", vc);
390 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000391 }
bartad994e82011-10-13 18:04:30 +0000392 DRD_(trace_msg)("%pS", msg);
bartbedfd232009-03-26 19:07:15 +0000393 VG_(free)(msg);
394 }
bart09dc13f2009-02-14 15:13:31 +0000395
bartbedfd232009-03-26 19:07:15 +0000396 if (! DRD_(get_check_stack_accesses)())
397 {
398 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
399 - DRD_(thread_get_stack_size)(drd_joinee),
400 DRD_(thread_get_stack_max)(drd_joinee));
401 }
402 DRD_(clientobj_delete_thread)(drd_joinee);
bart6d956dc2011-07-28 09:54:37 +0000403 DRD_(thread_delayed_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000404}
405
bart324a23b2009-02-15 12:14:52 +0000406/**
407 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
408 * and accesses this data structure from multiple threads without locking.
409 * Any conflicting accesses in the range stack_startup..stack_max will be
410 * ignored.
411 */
bart62a784c2009-02-15 13:11:14 +0000412void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
413 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000414{
bartbedfd232009-03-26 19:07:15 +0000415 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
416 && tid != DRD_INVALID_THREADID);
417 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
418 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
419 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000420}
421
bart86a87df2009-03-04 19:26:47 +0000422/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000423Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000424{
bartbedfd232009-03-26 19:07:15 +0000425 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
426 && tid != DRD_INVALID_THREADID);
427 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000428}
429
bart86a87df2009-03-04 19:26:47 +0000430/**
431 * Return the lowest value that was ever assigned to the stack pointer
432 * for the specified thread.
433 */
bart62a784c2009-02-15 13:11:14 +0000434Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000435{
bartbedfd232009-03-26 19:07:15 +0000436 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
437 && tid != DRD_INVALID_THREADID);
438 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000439}
440
bart86a87df2009-03-04 19:26:47 +0000441/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000442Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000443{
bartbedfd232009-03-26 19:07:15 +0000444 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
445 && tid != DRD_INVALID_THREADID);
446 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000447}
448
bart86a87df2009-03-04 19:26:47 +0000449/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000450SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000451{
bartbedfd232009-03-26 19:07:15 +0000452 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
453 && tid != DRD_INVALID_THREADID);
454 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000455}
456
bart383d6132010-09-02 14:43:18 +0000457Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
458{
459 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
460 && tid != DRD_INVALID_THREADID);
461 return DRD_(g_threadinfo)[tid].on_alt_stack;
462}
463
464void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
465 const Bool on_alt_stack)
466{
467 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
468 && tid != DRD_INVALID_THREADID);
469 tl_assert(on_alt_stack == !!on_alt_stack);
470 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
471}
472
473Int DRD_(thread_get_threads_on_alt_stack)(void)
474{
475 int i, n = 0;
476
477 for (i = 1; i < DRD_N_THREADS; i++)
478 n += DRD_(g_threadinfo)[i].on_alt_stack;
479 return n;
480}
481
bart09dc13f2009-02-14 15:13:31 +0000482/**
bart6d956dc2011-07-28 09:54:37 +0000483 * Clean up thread-specific data structures.
sewardjaf44c822007-11-25 14:01:38 +0000484 */
bart9194e932011-02-09 11:55:12 +0000485void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
sewardjaf44c822007-11-25 14:01:38 +0000486{
bartbedfd232009-03-26 19:07:15 +0000487 Segment* sg;
488 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000489
bartbedfd232009-03-26 19:07:15 +0000490 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000491
bartbedfd232009-03-26 19:07:15 +0000492 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
493 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
494 {
495 sg_prev = sg->prev;
496 sg->prev = 0;
497 sg->next = 0;
498 DRD_(sg_put)(sg);
499 }
bart6d956dc2011-07-28 09:54:37 +0000500 DRD_(g_threadinfo)[tid].valid = False;
bartbedfd232009-03-26 19:07:15 +0000501 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
502 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart9194e932011-02-09 11:55:12 +0000503 if (detached)
504 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
505 else
506 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
bartbedfd232009-03-26 19:07:15 +0000507 DRD_(g_threadinfo)[tid].first = 0;
508 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000509
bartbedfd232009-03-26 19:07:15 +0000510 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000511}
512
bart324a23b2009-02-15 12:14:52 +0000513/**
514 * Called after a thread performed its last memory access and before
515 * thread_delete() is called. Note: thread_delete() is only called for
516 * joinable threads, not for detached threads.
517 */
bart62a784c2009-02-15 13:11:14 +0000518void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000519{
bartbedfd232009-03-26 19:07:15 +0000520 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
521 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000522
bartbedfd232009-03-26 19:07:15 +0000523 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000524
bartbedfd232009-03-26 19:07:15 +0000525 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
526 {
527 /*
528 * Once a detached thread has finished, its stack is deallocated and
529 * should no longer be taken into account when computing the conflict set.
530 */
531 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000532
bartbedfd232009-03-26 19:07:15 +0000533 /*
534 * For a detached thread, calling pthread_exit() invalidates the
535 * POSIX thread ID associated with the detached thread. For joinable
536 * POSIX threads however, the POSIX thread ID remains live after the
537 * pthread_exit() call until pthread_join() is called.
538 */
539 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
540 }
sewardjaf44c822007-11-25 14:01:38 +0000541}
542
bart5c7e6b62011-02-03 17:47:50 +0000543/** Called just after fork() in the child process. */
544void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
545{
546 unsigned i;
547
548 for (i = 1; i < DRD_N_THREADS; i++)
549 {
550 if (i == tid)
551 continue;
552 if (DRD_(IsValidDrdThreadId(i)))
bart9194e932011-02-09 11:55:12 +0000553 DRD_(thread_delete)(i, True);
bart5c7e6b62011-02-03 17:47:50 +0000554 tl_assert(!DRD_(IsValidDrdThreadId(i)));
555 }
556}
557
bart9b2974a2008-09-27 12:35:31 +0000558/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000559void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000560{
bartbedfd232009-03-26 19:07:15 +0000561 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
562 && tid != DRD_INVALID_THREADID);
563 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000564
bart0c0cd772011-03-03 19:59:20 +0000565 if (DRD_(thread_get_trace_fork_join)())
bartad994e82011-10-13 18:04:30 +0000566 DRD_(trace_msg)("[%d] drd_thread_pre_cancel %d",
bartb92ff0f2011-10-08 08:29:29 +0000567 DRD_(g_drd_running_tid), tid);
bartaf0691b2008-09-27 12:26:50 +0000568}
569
barte7dff242009-04-23 17:12:39 +0000570/**
571 * Store the POSIX thread ID for the specified thread.
572 *
573 * @note This function can be called two times for the same thread -- see also
574 * the comment block preceding the pthread_create() wrapper in
575 * drd_pthread_intercepts.c.
576 */
bart62a784c2009-02-15 13:11:14 +0000577void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000578{
bartbedfd232009-03-26 19:07:15 +0000579 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
580 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000581 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
582 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000583 tl_assert(ptid != INVALID_POSIX_THREADID);
584 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
585 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000586}
587
bart86a87df2009-03-04 19:26:47 +0000588/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000589Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000590{
bartbedfd232009-03-26 19:07:15 +0000591 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
592 && tid != DRD_INVALID_THREADID);
593 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000594}
595
bart86a87df2009-03-04 19:26:47 +0000596/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000597void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000598{
bartbedfd232009-03-26 19:07:15 +0000599 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
600 && tid != DRD_INVALID_THREADID);
601 tl_assert(!! joinable == joinable);
602 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000603
bartbedfd232009-03-26 19:07:15 +0000604 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000605}
606
bartdd75cdf2009-07-24 08:20:10 +0000607/** Tells DRD that the calling thread is about to enter pthread_create(). */
608void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
609{
610 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
611 && tid != DRD_INVALID_THREADID);
612 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
613 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
614
615 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
616}
617
618/** Tells DRD that the calling thread has left pthread_create(). */
619void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
620{
621 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
622 && tid != DRD_INVALID_THREADID);
623 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
624 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
625
626 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
627}
628
bartd45d9952009-05-31 18:53:54 +0000629/** Obtain the thread number and the user-assigned thread name. */
630const char* DRD_(thread_get_name)(const DrdThreadId tid)
631{
632 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
633 && tid != DRD_INVALID_THREADID);
634
635 return DRD_(g_threadinfo)[tid].name;
636}
637
638/** Set the name of the specified thread. */
639void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
640{
641 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
642 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000643
bartd45d9952009-05-31 18:53:54 +0000644 if (name == NULL || name[0] == 0)
645 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
646 sizeof(DRD_(g_threadinfo)[tid].name),
647 "Thread %d",
648 tid);
649 else
650 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
651 sizeof(DRD_(g_threadinfo)[tid].name),
652 "Thread %d (%s)",
653 tid, name);
654 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
655}
656
bart86a87df2009-03-04 19:26:47 +0000657/**
658 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
659 * conflict set.
660 */
bart62a784c2009-02-15 13:11:14 +0000661void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000662{
bartbedfd232009-03-26 19:07:15 +0000663 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000664
bartbedfd232009-03-26 19:07:15 +0000665 if (vg_tid != s_vg_running_tid)
666 {
667 DRD_(thread_set_running_tid)(vg_tid,
668 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
669 }
sewardj8b09d4f2007-12-04 21:27:18 +0000670
bartbedfd232009-03-26 19:07:15 +0000671 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
672 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000673}
674
bart86a87df2009-03-04 19:26:47 +0000675/**
676 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
677 * conflict set.
678 */
bart62a784c2009-02-15 13:11:14 +0000679void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
680 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000681{
bartbedfd232009-03-26 19:07:15 +0000682 tl_assert(vg_tid != VG_INVALID_THREADID);
683 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000684
bartbedfd232009-03-26 19:07:15 +0000685 if (vg_tid != s_vg_running_tid)
686 {
687 if (s_trace_context_switches
688 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
689 {
690 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000691 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000692 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000693 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000694 DRD_(sg_get_segments_alive_count)());
695 }
696 s_vg_running_tid = vg_tid;
697 DRD_(g_drd_running_tid) = drd_tid;
698 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
699 s_context_switch_count++;
700 }
sewardj8b09d4f2007-12-04 21:27:18 +0000701
bartbedfd232009-03-26 19:07:15 +0000702 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
703 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000704}
705
bart86a87df2009-03-04 19:26:47 +0000706/**
707 * Increase the synchronization nesting counter. Must be called before the
708 * client calls a synchronization function.
709 */
bart62a784c2009-02-15 13:11:14 +0000710int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000711{
bartbedfd232009-03-26 19:07:15 +0000712 tl_assert(DRD_(IsValidDrdThreadId)(tid));
713 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000714}
715
bart86a87df2009-03-04 19:26:47 +0000716/**
717 * Decrease the synchronization nesting counter. Must be called after the
718 * client left a synchronization function.
719 */
bart62a784c2009-02-15 13:11:14 +0000720int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000721{
bartbedfd232009-03-26 19:07:15 +0000722 tl_assert(DRD_(IsValidDrdThreadId)(tid));
723 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
724 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000725}
726
bart86a87df2009-03-04 19:26:47 +0000727/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000728int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000729{
bartbedfd232009-03-26 19:07:15 +0000730 tl_assert(DRD_(IsValidDrdThreadId)(tid));
731 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000732}
733
bart1a473c72008-03-13 19:03:38 +0000734/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000735static
bart86a87df2009-03-04 19:26:47 +0000736void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000737{
bartbedfd232009-03-26 19:07:15 +0000738 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
739 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000740
741#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
742 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
743#endif
744
bartbedfd232009-03-26 19:07:15 +0000745 sg->prev = DRD_(g_threadinfo)[tid].last;
746 sg->next = 0;
747 if (DRD_(g_threadinfo)[tid].last)
748 DRD_(g_threadinfo)[tid].last->next = sg;
749 DRD_(g_threadinfo)[tid].last = sg;
750 if (DRD_(g_threadinfo)[tid].first == 0)
751 DRD_(g_threadinfo)[tid].first = sg;
bart8f822af2009-06-08 18:20:42 +0000752
753#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
754 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
755#endif
sewardjaf44c822007-11-25 14:01:38 +0000756}
757
bart324a23b2009-02-15 12:14:52 +0000758/**
759 * Remove a segment from the segment list of thread threadid, and free the
760 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000761 */
bart62a784c2009-02-15 13:11:14 +0000762static
bart86a87df2009-03-04 19:26:47 +0000763void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000764{
bartbedfd232009-03-26 19:07:15 +0000765 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
766 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000767
768#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
769 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
770#endif
bart26f73e12008-02-24 18:37:08 +0000771
bartbedfd232009-03-26 19:07:15 +0000772 if (sg->prev)
773 sg->prev->next = sg->next;
774 if (sg->next)
775 sg->next->prev = sg->prev;
776 if (sg == DRD_(g_threadinfo)[tid].first)
777 DRD_(g_threadinfo)[tid].first = sg->next;
778 if (sg == DRD_(g_threadinfo)[tid].last)
779 DRD_(g_threadinfo)[tid].last = sg->prev;
780 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000781
bart8f822af2009-06-08 18:20:42 +0000782#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
783 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
784#endif
sewardjaf44c822007-11-25 14:01:38 +0000785}
786
bart86a87df2009-03-04 19:26:47 +0000787/**
788 * Returns a pointer to the vector clock of the most recent segment associated
789 * with thread 'tid'.
790 */
bart62a784c2009-02-15 13:11:14 +0000791VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000792{
bartbedfd232009-03-26 19:07:15 +0000793 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
794 && tid != DRD_INVALID_THREADID);
795 tl_assert(DRD_(g_threadinfo)[tid].last);
796 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000797}
798
bart324a23b2009-02-15 12:14:52 +0000799/**
800 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000801 */
bart62a784c2009-02-15 13:11:14 +0000802void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000803{
bartbedfd232009-03-26 19:07:15 +0000804 tl_assert(sg);
805 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
806 && tid != DRD_INVALID_THREADID);
807 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000808
bartbedfd232009-03-26 19:07:15 +0000809 DRD_(sg_put)(*sg);
810 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000811}
812
sewardjaf44c822007-11-25 14:01:38 +0000813/**
814 * Compute the minimum of all latest vector clocks of all threads
815 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000816 *
sewardjaf44c822007-11-25 14:01:38 +0000817 * @param vc pointer to a vectorclock, holds result upon return.
818 */
bart62a784c2009-02-15 13:11:14 +0000819static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000820{
bartbedfd232009-03-26 19:07:15 +0000821 unsigned i;
822 Bool first;
823 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000824
bartbedfd232009-03-26 19:07:15 +0000825 first = True;
bart8f822af2009-06-08 18:20:42 +0000826 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000827 {
828 latest_sg = DRD_(g_threadinfo)[i].last;
829 if (latest_sg)
830 {
831 if (first)
832 DRD_(vc_assign)(vc, &latest_sg->vc);
833 else
834 DRD_(vc_min)(vc, &latest_sg->vc);
835 first = False;
836 }
837 }
sewardjaf44c822007-11-25 14:01:38 +0000838}
839
bart86a87df2009-03-04 19:26:47 +0000840/**
841 * Compute the maximum of all latest vector clocks of all threads.
842 *
843 * @param vc pointer to a vectorclock, holds result upon return.
844 */
bart62a784c2009-02-15 13:11:14 +0000845static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000846{
bartbedfd232009-03-26 19:07:15 +0000847 unsigned i;
848 Bool first;
849 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000850
bartbedfd232009-03-26 19:07:15 +0000851 first = True;
bart8f822af2009-06-08 18:20:42 +0000852 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000853 {
854 latest_sg = DRD_(g_threadinfo)[i].last;
855 if (latest_sg)
856 {
857 if (first)
858 DRD_(vc_assign)(vc, &latest_sg->vc);
859 else
860 DRD_(vc_combine)(vc, &latest_sg->vc);
861 first = False;
862 }
863 }
sewardjaf44c822007-11-25 14:01:38 +0000864}
865
866/**
bart5bd9f2d2008-03-03 20:31:58 +0000867 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000868 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000869 * data race.
870 */
bart8f822af2009-06-08 18:20:42 +0000871static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000872{
bartbedfd232009-03-26 19:07:15 +0000873 unsigned i;
874 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000875
bartbedfd232009-03-26 19:07:15 +0000876 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000877
bartbedfd232009-03-26 19:07:15 +0000878 DRD_(vc_init)(&thread_vc_min, 0, 0);
879 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
880 if (DRD_(sg_get_trace)())
881 {
bart8f822af2009-06-08 18:20:42 +0000882 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000883 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000884
bartbedfd232009-03-26 19:07:15 +0000885 DRD_(vc_init)(&thread_vc_max, 0, 0);
886 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000887 vc_min = DRD_(vc_aprint)(&thread_vc_min);
888 vc_max = DRD_(vc_aprint)(&thread_vc_max);
889 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000890 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000891 vc_min, vc_max);
892 VG_(free)(vc_min);
893 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000894 DRD_(vc_cleanup)(&thread_vc_max);
895 }
sewardjaf44c822007-11-25 14:01:38 +0000896
bart8f822af2009-06-08 18:20:42 +0000897 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000898 {
899 Segment* sg;
900 Segment* sg_next;
901 for (sg = DRD_(g_threadinfo)[i].first;
902 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
903 sg = sg_next)
904 {
905 thread_discard_segment(i, sg);
906 }
907 }
908 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000909}
910
bart324a23b2009-02-15 12:14:52 +0000911/**
bart8f822af2009-06-08 18:20:42 +0000912 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
913 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
914 * all segments in the set CS are ordered consistently against both sg1 and
915 * sg2. The set CS is defined as the set of segments that can immediately
916 * precede future segments via inter-thread synchronization operations. In
917 * DRD the set CS consists of the latest segment of each thread combined with
918 * all segments for which the reference count is strictly greater than one.
919 * The code below is an optimized version of the following:
920 *
921 * for (i = 0; i < DRD_N_THREADS; i++)
922 * {
923 * Segment* sg;
924 *
925 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
926 * {
927 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
928 * {
929 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
930 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
931 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
932 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
933 * {
934 * return False;
935 * }
936 * }
937 * }
938 * }
939 */
940static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
941 Segment* const sg1,
942 Segment* const sg2)
943{
944 unsigned i;
945
946 tl_assert(sg1->next);
947 tl_assert(sg2->next);
948 tl_assert(sg1->next == sg2);
949 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
950
951 for (i = 0; i < DRD_N_THREADS; i++)
952 {
953 Segment* sg;
954
955 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
956 {
957 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
958 {
959 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
960 break;
961 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
962 return False;
963 }
964 }
965 for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev)
966 {
967 if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1)
968 {
969 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
970 break;
971 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
972 return False;
973 }
974 }
975 }
976 return True;
977}
978
979/**
bart324a23b2009-02-15 12:14:52 +0000980 * Merge all segments that may be merged without triggering false positives
981 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000982 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
983 * and Koen De Bosschere. Bounding the number of segment histories during
984 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
985 * pp 1221-1238, September 2002. This paper contains a proof that merging
986 * consecutive segments for which the property equiv(s1,s2) holds can be
987 * merged without reducing the accuracy of datarace detection. Furthermore
988 * it is also proven that the total number of all segments will never grow
989 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
990 * every time a new segment is created. The property equiv(s1, s2) is defined
991 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
992 * clocks of segments s and s1 are ordered in the same way as those of segments
993 * s and s2. The set CS is defined as the set of existing segments s that have
994 * the potential to conflict with not yet created segments, either because the
995 * segment s is the latest segment of a thread or because it can become the
996 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +0000997 */
998static void thread_merge_segments(void)
999{
bartbedfd232009-03-26 19:07:15 +00001000 unsigned i;
barta9c37392008-03-22 09:38:48 +00001001
bart8f822af2009-06-08 18:20:42 +00001002 s_new_segments_since_last_merge = 0;
1003
1004 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001005 {
1006 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001007
bart8f822af2009-06-08 18:20:42 +00001008#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1009 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1010#endif
barta9c37392008-03-22 09:38:48 +00001011
bartbedfd232009-03-26 19:07:15 +00001012 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +00001013 {
bartbedfd232009-03-26 19:07:15 +00001014 if (DRD_(sg_get_refcnt)(sg) == 1
1015 && sg->next
1016 && DRD_(sg_get_refcnt)(sg->next) == 1
bart8f822af2009-06-08 18:20:42 +00001017 && sg->next->next
1018 && thread_consistent_segment_ordering(i, sg, sg->next))
bartbedfd232009-03-26 19:07:15 +00001019 {
1020 /* Merge sg and sg->next into sg. */
1021 DRD_(sg_merge)(sg, sg->next);
1022 thread_discard_segment(i, sg->next);
1023 }
barta9c37392008-03-22 09:38:48 +00001024 }
barta9c37392008-03-22 09:38:48 +00001025
bart8f822af2009-06-08 18:20:42 +00001026#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1027 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1028#endif
bartbedfd232009-03-26 19:07:15 +00001029 }
barta9c37392008-03-22 09:38:48 +00001030}
1031
bart324a23b2009-02-15 12:14:52 +00001032/**
bart324a23b2009-02-15 12:14:52 +00001033 * Create a new segment for the specified thread, and discard any segments
1034 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +00001035 */
bart62a784c2009-02-15 13:11:14 +00001036void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001037{
bart8f822af2009-06-08 18:20:42 +00001038 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001039 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001040
bartbedfd232009-03-26 19:07:15 +00001041 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1042 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +00001043 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001044
bart8f822af2009-06-08 18:20:42 +00001045 last_sg = DRD_(g_threadinfo)[tid].last;
bartbedfd232009-03-26 19:07:15 +00001046 new_sg = DRD_(sg_new)(tid, tid);
1047 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001048 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001049 {
bart8f822af2009-06-08 18:20:42 +00001050 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001051 s_update_conflict_set_new_sg_count++;
1052 }
bartd66e3a82008-04-06 15:02:17 +00001053
bart8f822af2009-06-08 18:20:42 +00001054 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001055
bart8f822af2009-06-08 18:20:42 +00001056 if (s_segment_merging
1057 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001058 {
bart8f822af2009-06-08 18:20:42 +00001059 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001060 thread_merge_segments();
1061 }
sewardjaf44c822007-11-25 14:01:38 +00001062}
1063
bart26f73e12008-02-24 18:37:08 +00001064/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001065void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001066{
bartbedfd232009-03-26 19:07:15 +00001067 tl_assert(joiner != joinee);
1068 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1069 && joiner != DRD_INVALID_THREADID);
1070 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1071 && joinee != DRD_INVALID_THREADID);
1072 tl_assert(DRD_(g_threadinfo)[joiner].last);
1073 tl_assert(DRD_(g_threadinfo)[joinee].last);
bart8f822af2009-06-08 18:20:42 +00001074
1075 if (DRD_(sg_get_trace)())
1076 {
1077 char *str1, *str2;
1078 str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
1079 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001080 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001081 str1, str2);
1082 VG_(free)(str1);
1083 VG_(free)(str2);
1084 }
barte5214662009-06-21 11:51:23 +00001085 if (joiner == DRD_(g_drd_running_tid))
1086 {
1087 VectorClock old_vc;
1088
1089 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc);
1090 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001091 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001092 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1093 s_update_conflict_set_join_count++;
1094 DRD_(vc_cleanup)(&old_vc);
1095 }
1096 else
1097 {
1098 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
bartf6ec1fe2009-06-21 18:07:35 +00001099 &DRD_(g_threadinfo)[joinee].last->vc);
barte5214662009-06-21 11:51:23 +00001100 }
1101
1102 thread_discard_ordered_segments();
1103
bart8f822af2009-06-08 18:20:42 +00001104 if (DRD_(sg_get_trace)())
1105 {
1106 char* str;
1107 str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001108 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001109 VG_(free)(str);
1110 }
sewardjaf44c822007-11-25 14:01:38 +00001111}
1112
bart324a23b2009-02-15 12:14:52 +00001113/**
bart8f822af2009-06-08 18:20:42 +00001114 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001115 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001116 */
bartf6ec1fe2009-06-21 18:07:35 +00001117static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001118{
bart8f822af2009-06-08 18:20:42 +00001119 const VectorClock* const vc = &sg->vc;
1120
bartbedfd232009-03-26 19:07:15 +00001121 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1122 && tid != DRD_INVALID_THREADID);
1123 tl_assert(DRD_(g_threadinfo)[tid].last);
bart8f822af2009-06-08 18:20:42 +00001124 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001125 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001126
1127 if (tid != sg->tid)
1128 {
1129 VectorClock old_vc;
1130
1131 DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc);
1132 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
1133 if (DRD_(sg_get_trace)())
1134 {
1135 char *str1, *str2;
1136 str1 = DRD_(vc_aprint)(&old_vc);
1137 str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001138 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001139 VG_(free)(str1);
1140 VG_(free)(str2);
1141 }
barte5214662009-06-21 11:51:23 +00001142
bart8f822af2009-06-08 18:20:42 +00001143 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001144
bart8f822af2009-06-08 18:20:42 +00001145 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001146 s_update_conflict_set_sync_count++;
1147
bart8f822af2009-06-08 18:20:42 +00001148 DRD_(vc_cleanup)(&old_vc);
1149 }
1150 else
1151 {
1152 tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc));
1153 }
sewardjaf44c822007-11-25 14:01:38 +00001154}
1155
bart324a23b2009-02-15 12:14:52 +00001156/**
bartf6ec1fe2009-06-21 18:07:35 +00001157 * Create a new segment for thread tid and update the vector clock of the last
1158 * segment of this thread with the the vector clock of segment sg. Call this
1159 * function after thread tid had to wait because of thread synchronization
1160 * until the memory accesses in the segment sg finished.
1161 */
1162void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1163{
1164 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1165 && tid != DRD_INVALID_THREADID);
1166 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1167 tl_assert(sg);
1168
1169 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1170
1171 thread_combine_vc_sync(tid, sg);
1172
1173 if (s_segment_merging
1174 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1175 {
1176 thread_discard_ordered_segments();
1177 thread_merge_segments();
1178 }
1179}
1180
1181/**
bart324a23b2009-02-15 12:14:52 +00001182 * Call this function whenever a thread is no longer using the memory
1183 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1184 * increase.
bart26f73e12008-02-24 18:37:08 +00001185 */
bart23ef19d2011-03-12 12:34:44 +00001186void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001187{
bartbedfd232009-03-26 19:07:15 +00001188 unsigned i;
bart178b6862011-07-29 12:30:43 +00001189 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001190
bart8f822af2009-06-08 18:20:42 +00001191 for (i = 0; i < DRD_N_THREADS; i++)
bart178b6862011-07-29 12:30:43 +00001192 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1193 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001194
bart178b6862011-07-29 12:30:43 +00001195 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001196}
1197
bartd45d9952009-05-31 18:53:54 +00001198/** Specify whether memory loads should be recorded. */
1199void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001200{
bartbedfd232009-03-26 19:07:15 +00001201 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1202 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001203 tl_assert(enabled == !! enabled);
1204
1205 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001206}
1207
bartd45d9952009-05-31 18:53:54 +00001208/** Specify whether memory stores should be recorded. */
1209void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001210{
bartbedfd232009-03-26 19:07:15 +00001211 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1212 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001213 tl_assert(enabled == !! enabled);
1214
1215 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001216}
1217
bart86a87df2009-03-04 19:26:47 +00001218/**
1219 * Print the segment information for all threads.
1220 *
1221 * This function is only used for debugging purposes.
1222 */
bart62a784c2009-02-15 13:11:14 +00001223void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001224{
bartbedfd232009-03-26 19:07:15 +00001225 unsigned i;
1226 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001227
bart8f822af2009-06-08 18:20:42 +00001228 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001229 {
1230 if (DRD_(g_threadinfo)[i].first)
sewardjaf44c822007-11-25 14:01:38 +00001231 {
bartbedfd232009-03-26 19:07:15 +00001232 VG_(printf)("**************\n"
bart6d956dc2011-07-28 09:54:37 +00001233 "* thread %3d (%d/%d/%d/%d/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001234 "**************\n",
1235 i,
bart6d956dc2011-07-28 09:54:37 +00001236 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001237 DRD_(g_threadinfo)[i].vg_thread_exists,
1238 DRD_(g_threadinfo)[i].vg_threadid,
1239 DRD_(g_threadinfo)[i].posix_thread_exists,
1240 DRD_(g_threadinfo)[i].pt_threadid,
1241 DRD_(g_threadinfo)[i].detached_posix_thread);
1242 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
1243 {
1244 DRD_(sg_print)(p);
1245 }
sewardjaf44c822007-11-25 14:01:38 +00001246 }
bartbedfd232009-03-26 19:07:15 +00001247 }
sewardjaf44c822007-11-25 14:01:38 +00001248}
1249
bart86a87df2009-03-04 19:26:47 +00001250/** Show a call stack involved in a data race. */
barte7086002011-10-11 19:08:39 +00001251static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
sewardjaf44c822007-11-25 14:01:38 +00001252{
bartbedfd232009-03-26 19:07:15 +00001253 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001254
barte7086002011-10-11 19:08:39 +00001255 if (vg_tid != VG_INVALID_THREADID) {
bartbedfd232009-03-26 19:07:15 +00001256 if (callstack)
bartbedfd232009-03-26 19:07:15 +00001257 VG_(pp_ExeContext)(callstack);
bartbedfd232009-03-26 19:07:15 +00001258 else
bartbedfd232009-03-26 19:07:15 +00001259 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
barte7086002011-10-11 19:08:39 +00001260 } else {
1261 if (!VG_(clo_xml))
1262 VG_(message)(Vg_UserMsg,
1263 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001264 }
sewardjaf44c822007-11-25 14:01:38 +00001265}
1266
bart86a87df2009-03-04 19:26:47 +00001267/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001268static void
1269thread_report_conflicting_segments_segment(const DrdThreadId tid,
1270 const Addr addr,
1271 const SizeT size,
1272 const BmAccessTypeT access_type,
1273 const Segment* const p)
1274{
bartbedfd232009-03-26 19:07:15 +00001275 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001276
bartbedfd232009-03-26 19:07:15 +00001277 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1278 && tid != DRD_INVALID_THREADID);
1279 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001280
bart8f822af2009-06-08 18:20:42 +00001281 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001282 {
1283 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001284 {
bartbedfd232009-03-26 19:07:15 +00001285 Segment* q;
1286 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
1287 {
1288 /*
bart31b983d2010-02-21 14:52:59 +00001289 * Since q iterates over the segments of thread i in order of
1290 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001291 * q->next->vc <= p->vc will also hold. Hence, break out of the
1292 * loop once this condition is met.
1293 */
1294 if (DRD_(vc_lte)(&q->vc, &p->vc))
1295 break;
1296 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1297 {
bart8f822af2009-06-08 18:20:42 +00001298 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001299 access_type))
1300 {
1301 tl_assert(q->stacktrace);
barte7086002011-10-11 19:08:39 +00001302 if (VG_(clo_xml))
1303 VG_(printf_xml)(" <other_segment_start>\n");
1304 else
1305 VG_(message)(Vg_UserMsg,
1306 "Other segment start (thread %d)\n", i);
1307 show_call_stack(i, q->stacktrace);
1308 if (VG_(clo_xml))
1309 VG_(printf_xml)(" </other_segment_start>\n"
1310 " <other_segment_end>\n");
1311 else
1312 VG_(message)(Vg_UserMsg,
1313 "Other segment end (thread %d)\n", i);
1314 show_call_stack(i, q->next ? q->next->stacktrace : 0);
1315 if (VG_(clo_xml))
1316 VG_(printf_xml)(" </other_segment_end>\n");
bartbedfd232009-03-26 19:07:15 +00001317 }
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}