blob: 0b522284cd111f72fe6c92b1c21b71c297b2a36f [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
barte278ab52012-01-24 18:28:55 +0000143void DRD_(thread_init)(void)
144{
145 int i;
146
147 for (i = 0; i < DRD_N_THREADS; i++)
148 init_list_head(&DRD_(g_threadinfo)[i].sg_list);
149}
150
sewardjaf44c822007-11-25 14:01:38 +0000151/**
bart86a87df2009-03-04 19:26:47 +0000152 * Convert Valgrind's ThreadId into a DrdThreadId.
153 *
154 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
155 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000156 */
bart62a784c2009-02-15 13:11:14 +0000157DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000158{
bartbedfd232009-03-26 19:07:15 +0000159 int i;
sewardjaf44c822007-11-25 14:01:38 +0000160
bartbedfd232009-03-26 19:07:15 +0000161 if (tid == VG_INVALID_THREADID)
162 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000163
bartbedfd232009-03-26 19:07:15 +0000164 for (i = 1; i < DRD_N_THREADS; i++)
165 {
166 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
167 && DRD_(g_threadinfo)[i].vg_threadid == tid)
168 {
169 return i;
170 }
171 }
sewardjaf44c822007-11-25 14:01:38 +0000172
bartbedfd232009-03-26 19:07:15 +0000173 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000174}
175
bart86a87df2009-03-04 19:26:47 +0000176/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000177static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000178{
bartbedfd232009-03-26 19:07:15 +0000179 int i;
sewardjaf44c822007-11-25 14:01:38 +0000180
bartbedfd232009-03-26 19:07:15 +0000181 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000182
bartbedfd232009-03-26 19:07:15 +0000183 for (i = 1; i < DRD_N_THREADS; i++)
184 {
bart6d956dc2011-07-28 09:54:37 +0000185 if (!DRD_(g_threadinfo)[i].valid)
bartbedfd232009-03-26 19:07:15 +0000186 {
187 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000188
bart6d956dc2011-07-28 09:54:37 +0000189 DRD_(g_threadinfo)[i].valid = True;
bartbedfd232009-03-26 19:07:15 +0000190 DRD_(g_threadinfo)[i].vg_thread_exists = True;
191 DRD_(g_threadinfo)[i].vg_threadid = tid;
192 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
193 DRD_(g_threadinfo)[i].stack_min = 0;
194 DRD_(g_threadinfo)[i].stack_min_min = 0;
195 DRD_(g_threadinfo)[i].stack_startup = 0;
196 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000197 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000198 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000199 DRD_(g_threadinfo)[i].is_recording_loads = True;
200 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000201 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000202 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart6d956dc2011-07-28 09:54:37 +0000203 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
barte278ab52012-01-24 18:28:55 +0000204 tl_assert(list_empty(&DRD_(g_threadinfo)[i].sg_list));
bart86a87df2009-03-04 19:26:47 +0000205
bartbedfd232009-03-26 19:07:15 +0000206 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000207
bartbedfd232009-03-26 19:07:15 +0000208 return i;
209 }
210 }
sewardjaf44c822007-11-25 14:01:38 +0000211
bart3c9afb12009-07-24 11:11:30 +0000212 VG_(printf)(
213"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
214"Aborting.\n");
215
bartbedfd232009-03-26 19:07:15 +0000216 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000217
bartbedfd232009-03-26 19:07:15 +0000218 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000219}
220
bart86a87df2009-03-04 19:26:47 +0000221/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000222DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000223{
bartbedfd232009-03-26 19:07:15 +0000224 int i;
sewardjaf44c822007-11-25 14:01:38 +0000225
bartb48bde22009-07-31 08:26:17 +0000226 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000227 {
bartb48bde22009-07-31 08:26:17 +0000228 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000229 {
bartb48bde22009-07-31 08:26:17 +0000230 if (DRD_(g_threadinfo)[i].posix_thread_exists
231 && DRD_(g_threadinfo)[i].pt_threadid == tid)
232 {
233 return i;
234 }
bartbedfd232009-03-26 19:07:15 +0000235 }
236 }
237 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000238}
239
bart86a87df2009-03-04 19:26:47 +0000240/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000241ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000242{
bartbedfd232009-03-26 19:07:15 +0000243 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
244 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000245
bartbedfd232009-03-26 19:07:15 +0000246 return (DRD_(g_threadinfo)[tid].vg_thread_exists
247 ? DRD_(g_threadinfo)[tid].vg_threadid
248 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000249}
250
bart8f822af2009-06-08 18:20:42 +0000251#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000252/**
253 * Sanity check of the doubly linked list of segments referenced by a
254 * ThreadInfo struct.
255 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000256 */
bart62a784c2009-02-15 13:11:14 +0000257static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000258{
bartbedfd232009-03-26 19:07:15 +0000259 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000260
bartbedfd232009-03-26 19:07:15 +0000261 for (p = ti->first; p; p = p->next) {
262 if (p->next && p->next->prev != p)
263 return False;
264 if (p->next == 0 && p != ti->last)
265 return False;
266 }
267 for (p = ti->last; p; p = p->prev) {
268 if (p->prev && p->prev->next != p)
269 return False;
270 if (p->prev == 0 && p != ti->first)
271 return False;
272 }
273 return True;
sewardjaf44c822007-11-25 14:01:38 +0000274}
bart23d3a4e2008-04-05 12:53:00 +0000275#endif
sewardjaf44c822007-11-25 14:01:38 +0000276
bart439c55f2009-02-15 10:38:37 +0000277/**
278 * Create the first segment for a newly started thread.
279 *
280 * This function is called from the handler installed via
281 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
282 * from the context of the creator thread, before the new thread has been
283 * created.
bart86a87df2009-03-04 19:26:47 +0000284 *
285 * @param[in] creator DRD thread ID of the creator thread.
286 * @param[in] vg_created Valgrind thread ID of the created thread.
287 *
288 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000289 */
bart62a784c2009-02-15 13:11:14 +0000290DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
291 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000292{
bartbedfd232009-03-26 19:07:15 +0000293 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000294
bartbedfd232009-03-26 19:07:15 +0000295 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
296 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
297 tl_assert(0 <= (int)created && created < DRD_N_THREADS
298 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000299
barte278ab52012-01-24 18:28:55 +0000300 tl_assert(list_empty(&DRD_(g_threadinfo)[created].sg_list));
bart8f822af2009-06-08 18:20:42 +0000301 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000302 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000303
bartbedfd232009-03-26 19:07:15 +0000304 return created;
sewardjaf44c822007-11-25 14:01:38 +0000305}
306
bart439c55f2009-02-15 10:38:37 +0000307/**
bart86a87df2009-03-04 19:26:47 +0000308 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
309 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000310 * on the newly created thread, e.g. from the handler installed via
311 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000312 *
313 * @param[in] vg_created Valgrind thread ID of the newly created thread.
314 *
315 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000316 */
bart62a784c2009-02-15 13:11:14 +0000317DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000318{
bartbedfd232009-03-26 19:07:15 +0000319 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000320
bartbedfd232009-03-26 19:07:15 +0000321 tl_assert(0 <= (int)created && created < DRD_N_THREADS
322 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000323
bartbedfd232009-03-26 19:07:15 +0000324 DRD_(g_threadinfo)[created].stack_max
325 = VG_(thread_get_stack_max)(vg_created);
326 DRD_(g_threadinfo)[created].stack_startup
327 = DRD_(g_threadinfo)[created].stack_max;
328 DRD_(g_threadinfo)[created].stack_min
329 = DRD_(g_threadinfo)[created].stack_max;
330 DRD_(g_threadinfo)[created].stack_min_min
331 = DRD_(g_threadinfo)[created].stack_max;
332 DRD_(g_threadinfo)[created].stack_size
333 = VG_(thread_get_stack_size)(vg_created);
334 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000335
bartbedfd232009-03-26 19:07:15 +0000336 return created;
bart439c55f2009-02-15 10:38:37 +0000337}
bart09dc13f2009-02-14 15:13:31 +0000338
bart6d956dc2011-07-28 09:54:37 +0000339static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
340{
341 int j;
342
343 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
344 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
345 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
346#if 0
347 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
348#endif
349 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
350 for (j = 0; j < DRD_N_THREADS; ++j) {
351 if (DRD_(IsValidDrdThreadId)(j)
352 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
353 {
354 s_deletion_tail++;
355#if 0
356 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
357#endif
358 DRD_(thread_delete)(j, False);
359 break;
360 }
361 }
362 }
363}
364
bart324a23b2009-02-15 12:14:52 +0000365/**
366 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
367 * after thread drd_joiner joined thread drd_joinee.
368 */
bart09dc13f2009-02-14 15:13:31 +0000369void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
370{
bartbedfd232009-03-26 19:07:15 +0000371 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
372 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000373
bartbedfd232009-03-26 19:07:15 +0000374 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000375 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000376 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000377
bartbedfd232009-03-26 19:07:15 +0000378 if (s_trace_fork_join)
379 {
380 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000381 const unsigned msg_size = 256;
382 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000383
bartbedfd232009-03-26 19:07:15 +0000384 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
385 tl_assert(msg);
386 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000387 "drd_post_thread_join joiner = %d, joinee = %d",
388 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000389 if (joiner)
390 {
bart8f822af2009-06-08 18:20:42 +0000391 char* vc;
392
393 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000394 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000395 ", new vc: %s", vc);
396 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000397 }
bartad994e82011-10-13 18:04:30 +0000398 DRD_(trace_msg)("%pS", msg);
bartbedfd232009-03-26 19:07:15 +0000399 VG_(free)(msg);
400 }
bart09dc13f2009-02-14 15:13:31 +0000401
bartbedfd232009-03-26 19:07:15 +0000402 if (! DRD_(get_check_stack_accesses)())
403 {
404 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
405 - DRD_(thread_get_stack_size)(drd_joinee),
406 DRD_(thread_get_stack_max)(drd_joinee));
407 }
408 DRD_(clientobj_delete_thread)(drd_joinee);
bart6d956dc2011-07-28 09:54:37 +0000409 DRD_(thread_delayed_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000410}
411
bart324a23b2009-02-15 12:14:52 +0000412/**
413 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
414 * and accesses this data structure from multiple threads without locking.
415 * Any conflicting accesses in the range stack_startup..stack_max will be
416 * ignored.
417 */
bart62a784c2009-02-15 13:11:14 +0000418void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
419 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000420{
bartbedfd232009-03-26 19:07:15 +0000421 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
422 && tid != DRD_INVALID_THREADID);
423 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
424 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
425 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000426}
427
bart86a87df2009-03-04 19:26:47 +0000428/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000429Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000430{
bartbedfd232009-03-26 19:07:15 +0000431 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
432 && tid != DRD_INVALID_THREADID);
433 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000434}
435
bart86a87df2009-03-04 19:26:47 +0000436/**
437 * Return the lowest value that was ever assigned to the stack pointer
438 * for the specified thread.
439 */
bart62a784c2009-02-15 13:11:14 +0000440Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000441{
bartbedfd232009-03-26 19:07:15 +0000442 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
443 && tid != DRD_INVALID_THREADID);
444 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000445}
446
bart86a87df2009-03-04 19:26:47 +0000447/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000448Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000449{
bartbedfd232009-03-26 19:07:15 +0000450 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
451 && tid != DRD_INVALID_THREADID);
452 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000453}
454
bart86a87df2009-03-04 19:26:47 +0000455/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000456SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000457{
bartbedfd232009-03-26 19:07:15 +0000458 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
459 && tid != DRD_INVALID_THREADID);
460 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000461}
462
bart383d6132010-09-02 14:43:18 +0000463Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
464{
465 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
466 && tid != DRD_INVALID_THREADID);
467 return DRD_(g_threadinfo)[tid].on_alt_stack;
468}
469
470void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
471 const Bool on_alt_stack)
472{
473 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
474 && tid != DRD_INVALID_THREADID);
475 tl_assert(on_alt_stack == !!on_alt_stack);
476 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
477}
478
479Int DRD_(thread_get_threads_on_alt_stack)(void)
480{
481 int i, n = 0;
482
483 for (i = 1; i < DRD_N_THREADS; i++)
484 n += DRD_(g_threadinfo)[i].on_alt_stack;
485 return n;
486}
487
bart09dc13f2009-02-14 15:13:31 +0000488/**
bart6d956dc2011-07-28 09:54:37 +0000489 * Clean up thread-specific data structures.
sewardjaf44c822007-11-25 14:01:38 +0000490 */
bart9194e932011-02-09 11:55:12 +0000491void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
sewardjaf44c822007-11-25 14:01:38 +0000492{
bartbedfd232009-03-26 19:07:15 +0000493 Segment* sg;
494 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000495
bartbedfd232009-03-26 19:07:15 +0000496 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000497
bartbedfd232009-03-26 19:07:15 +0000498 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
barte278ab52012-01-24 18:28:55 +0000499 list_for_each_entry_safe(sg, sg_prev, &DRD_(g_threadinfo)[tid].sg_list,
500 thr_list)
bartbedfd232009-03-26 19:07:15 +0000501 {
barte278ab52012-01-24 18:28:55 +0000502 list_del(&sg->thr_list);
bartbedfd232009-03-26 19:07:15 +0000503 DRD_(sg_put)(sg);
504 }
bart6d956dc2011-07-28 09:54:37 +0000505 DRD_(g_threadinfo)[tid].valid = False;
bartbedfd232009-03-26 19:07:15 +0000506 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
507 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart9194e932011-02-09 11:55:12 +0000508 if (detached)
509 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
510 else
511 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
barte278ab52012-01-24 18:28:55 +0000512 tl_assert(list_empty(&DRD_(g_threadinfo)[tid].sg_list));
bart86a87df2009-03-04 19:26:47 +0000513
bartbedfd232009-03-26 19:07:15 +0000514 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000515}
516
bart324a23b2009-02-15 12:14:52 +0000517/**
518 * Called after a thread performed its last memory access and before
519 * thread_delete() is called. Note: thread_delete() is only called for
520 * joinable threads, not for detached threads.
521 */
bart62a784c2009-02-15 13:11:14 +0000522void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000523{
bartbedfd232009-03-26 19:07:15 +0000524 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
525 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000526
bartbedfd232009-03-26 19:07:15 +0000527 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000528
bartbedfd232009-03-26 19:07:15 +0000529 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
530 {
531 /*
532 * Once a detached thread has finished, its stack is deallocated and
533 * should no longer be taken into account when computing the conflict set.
534 */
535 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000536
bartbedfd232009-03-26 19:07:15 +0000537 /*
538 * For a detached thread, calling pthread_exit() invalidates the
539 * POSIX thread ID associated with the detached thread. For joinable
540 * POSIX threads however, the POSIX thread ID remains live after the
541 * pthread_exit() call until pthread_join() is called.
542 */
543 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
544 }
sewardjaf44c822007-11-25 14:01:38 +0000545}
546
bart5c7e6b62011-02-03 17:47:50 +0000547/** Called just after fork() in the child process. */
548void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
549{
550 unsigned i;
551
552 for (i = 1; i < DRD_N_THREADS; i++)
553 {
554 if (i == tid)
555 continue;
556 if (DRD_(IsValidDrdThreadId(i)))
bart9194e932011-02-09 11:55:12 +0000557 DRD_(thread_delete)(i, True);
bart5c7e6b62011-02-03 17:47:50 +0000558 tl_assert(!DRD_(IsValidDrdThreadId(i)));
bartf7a5b3f2011-12-11 20:34:03 +0000559 }
bart5c7e6b62011-02-03 17:47:50 +0000560}
561
bart9b2974a2008-09-27 12:35:31 +0000562/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000563void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000564{
bartbedfd232009-03-26 19:07:15 +0000565 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
566 && tid != DRD_INVALID_THREADID);
567 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000568
bart0c0cd772011-03-03 19:59:20 +0000569 if (DRD_(thread_get_trace_fork_join)())
bartad994e82011-10-13 18:04:30 +0000570 DRD_(trace_msg)("[%d] drd_thread_pre_cancel %d",
bartb92ff0f2011-10-08 08:29:29 +0000571 DRD_(g_drd_running_tid), tid);
bartaf0691b2008-09-27 12:26:50 +0000572}
573
barte7dff242009-04-23 17:12:39 +0000574/**
575 * Store the POSIX thread ID for the specified thread.
576 *
577 * @note This function can be called two times for the same thread -- see also
578 * the comment block preceding the pthread_create() wrapper in
579 * drd_pthread_intercepts.c.
580 */
bart62a784c2009-02-15 13:11:14 +0000581void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000582{
bartbedfd232009-03-26 19:07:15 +0000583 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
584 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000585 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
586 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000587 tl_assert(ptid != INVALID_POSIX_THREADID);
588 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
589 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000590}
591
bart86a87df2009-03-04 19:26:47 +0000592/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000593Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000594{
bartbedfd232009-03-26 19:07:15 +0000595 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
596 && tid != DRD_INVALID_THREADID);
597 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000598}
599
bart86a87df2009-03-04 19:26:47 +0000600/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000601void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000602{
bartbedfd232009-03-26 19:07:15 +0000603 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
604 && tid != DRD_INVALID_THREADID);
605 tl_assert(!! joinable == joinable);
606 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000607
bartbedfd232009-03-26 19:07:15 +0000608 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000609}
610
bartdd75cdf2009-07-24 08:20:10 +0000611/** Tells DRD that the calling thread is about to enter pthread_create(). */
612void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
613{
614 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
615 && tid != DRD_INVALID_THREADID);
616 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
617 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
618
619 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
620}
621
622/** Tells DRD that the calling thread has left pthread_create(). */
623void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
624{
625 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
626 && tid != DRD_INVALID_THREADID);
627 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
628 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
629
630 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
631}
632
bartd45d9952009-05-31 18:53:54 +0000633/** Obtain the thread number and the user-assigned thread name. */
634const char* DRD_(thread_get_name)(const DrdThreadId tid)
635{
636 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
637 && tid != DRD_INVALID_THREADID);
638
639 return DRD_(g_threadinfo)[tid].name;
640}
641
642/** Set the name of the specified thread. */
643void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
644{
645 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
646 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000647
bartd45d9952009-05-31 18:53:54 +0000648 if (name == NULL || name[0] == 0)
649 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
650 sizeof(DRD_(g_threadinfo)[tid].name),
651 "Thread %d",
652 tid);
653 else
654 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
655 sizeof(DRD_(g_threadinfo)[tid].name),
656 "Thread %d (%s)",
657 tid, name);
658 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
659}
660
bart86a87df2009-03-04 19:26:47 +0000661/**
662 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
663 * conflict set.
664 */
bart62a784c2009-02-15 13:11:14 +0000665void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000666{
bartbedfd232009-03-26 19:07:15 +0000667 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000668
bartbedfd232009-03-26 19:07:15 +0000669 if (vg_tid != s_vg_running_tid)
670 {
671 DRD_(thread_set_running_tid)(vg_tid,
672 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
673 }
sewardj8b09d4f2007-12-04 21:27:18 +0000674
bartbedfd232009-03-26 19:07:15 +0000675 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
676 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000677}
678
bart86a87df2009-03-04 19:26:47 +0000679/**
680 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
681 * conflict set.
682 */
bart62a784c2009-02-15 13:11:14 +0000683void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
684 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000685{
bartbedfd232009-03-26 19:07:15 +0000686 tl_assert(vg_tid != VG_INVALID_THREADID);
687 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000688
bartbedfd232009-03-26 19:07:15 +0000689 if (vg_tid != s_vg_running_tid)
690 {
691 if (s_trace_context_switches
692 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
693 {
694 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000695 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000696 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000697 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000698 DRD_(sg_get_segments_alive_count)());
699 }
700 s_vg_running_tid = vg_tid;
701 DRD_(g_drd_running_tid) = drd_tid;
702 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
703 s_context_switch_count++;
704 }
sewardj8b09d4f2007-12-04 21:27:18 +0000705
bartbedfd232009-03-26 19:07:15 +0000706 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
707 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000708}
709
bart86a87df2009-03-04 19:26:47 +0000710/**
711 * Increase the synchronization nesting counter. Must be called before the
712 * client calls a synchronization function.
713 */
bart62a784c2009-02-15 13:11:14 +0000714int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000715{
bartbedfd232009-03-26 19:07:15 +0000716 tl_assert(DRD_(IsValidDrdThreadId)(tid));
717 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000718}
719
bart86a87df2009-03-04 19:26:47 +0000720/**
721 * Decrease the synchronization nesting counter. Must be called after the
722 * client left a synchronization function.
723 */
bart62a784c2009-02-15 13:11:14 +0000724int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000725{
bartbedfd232009-03-26 19:07:15 +0000726 tl_assert(DRD_(IsValidDrdThreadId)(tid));
727 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
728 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000729}
730
bart86a87df2009-03-04 19:26:47 +0000731/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000732int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000733{
bartbedfd232009-03-26 19:07:15 +0000734 tl_assert(DRD_(IsValidDrdThreadId)(tid));
735 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000736}
737
bart1a473c72008-03-13 19:03:38 +0000738/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000739static
bart86a87df2009-03-04 19:26:47 +0000740void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000741{
bartbedfd232009-03-26 19:07:15 +0000742 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
743 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000744
745#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
746 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
747#endif
748
barte278ab52012-01-24 18:28:55 +0000749 list_add_tail(&sg->thr_list, &DRD_(g_threadinfo)[tid].sg_list);
bart8f822af2009-06-08 18:20:42 +0000750
751#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
752 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
753#endif
sewardjaf44c822007-11-25 14:01:38 +0000754}
755
bart324a23b2009-02-15 12:14:52 +0000756/**
757 * Remove a segment from the segment list of thread threadid, and free the
758 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000759 */
bart62a784c2009-02-15 13:11:14 +0000760static
bart86a87df2009-03-04 19:26:47 +0000761void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000762{
bartbedfd232009-03-26 19:07:15 +0000763 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
764 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000765
766#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
767 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
768#endif
bart26f73e12008-02-24 18:37:08 +0000769
barte278ab52012-01-24 18:28:55 +0000770 list_del(&sg->thr_list);
bartbedfd232009-03-26 19:07:15 +0000771 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000772
bart8f822af2009-06-08 18:20:42 +0000773#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
774 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
775#endif
sewardjaf44c822007-11-25 14:01:38 +0000776}
777
bart86a87df2009-03-04 19:26:47 +0000778/**
779 * Returns a pointer to the vector clock of the most recent segment associated
780 * with thread 'tid'.
781 */
bart62a784c2009-02-15 13:11:14 +0000782VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000783{
barte278ab52012-01-24 18:28:55 +0000784 struct list_head* sg_list;
785
bartbedfd232009-03-26 19:07:15 +0000786 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
787 && tid != DRD_INVALID_THREADID);
barte278ab52012-01-24 18:28:55 +0000788 sg_list = &DRD_(g_threadinfo)[tid].sg_list;
789 tl_assert(!list_empty(sg_list));
790 return &list_last_entry(sg_list, Segment, thr_list)->vc;
sewardjaf44c822007-11-25 14:01:38 +0000791}
792
bart324a23b2009-02-15 12:14:52 +0000793/**
794 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000795 */
bart62a784c2009-02-15 13:11:14 +0000796void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000797{
barte278ab52012-01-24 18:28:55 +0000798 struct list_head* sg_list;
799
bartbedfd232009-03-26 19:07:15 +0000800 tl_assert(sg);
801 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
802 && tid != DRD_INVALID_THREADID);
barte278ab52012-01-24 18:28:55 +0000803 sg_list = &DRD_(g_threadinfo)[tid].sg_list;
804 tl_assert(!list_empty(sg_list));
barta2b6e1b2008-03-17 18:32:39 +0000805
bartbedfd232009-03-26 19:07:15 +0000806 DRD_(sg_put)(*sg);
barte278ab52012-01-24 18:28:55 +0000807 *sg = DRD_(sg_get)(list_last_entry(sg_list, Segment, thr_list));
barta2b6e1b2008-03-17 18:32:39 +0000808}
809
sewardjaf44c822007-11-25 14:01:38 +0000810/**
811 * Compute the minimum of all latest vector clocks of all threads
812 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000813 *
sewardjaf44c822007-11-25 14:01:38 +0000814 * @param vc pointer to a vectorclock, holds result upon return.
815 */
bart62a784c2009-02-15 13:11:14 +0000816static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000817{
bartbedfd232009-03-26 19:07:15 +0000818 unsigned i;
819 Bool first;
barte278ab52012-01-24 18:28:55 +0000820 struct list_head* sg_list;
bartbedfd232009-03-26 19:07:15 +0000821 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000822
bartbedfd232009-03-26 19:07:15 +0000823 first = True;
bart8f822af2009-06-08 18:20:42 +0000824 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000825 {
barte278ab52012-01-24 18:28:55 +0000826 sg_list = &DRD_(g_threadinfo)[i].sg_list;
827 if (!list_empty(sg_list)) {
828 latest_sg = list_last_entry(sg_list, Segment, thr_list);
bartbedfd232009-03-26 19:07:15 +0000829 if (first)
830 DRD_(vc_assign)(vc, &latest_sg->vc);
831 else
832 DRD_(vc_min)(vc, &latest_sg->vc);
833 first = False;
834 }
835 }
sewardjaf44c822007-11-25 14:01:38 +0000836}
837
bart86a87df2009-03-04 19:26:47 +0000838/**
839 * Compute the maximum of all latest vector clocks of all threads.
840 *
841 * @param vc pointer to a vectorclock, holds result upon return.
842 */
bart62a784c2009-02-15 13:11:14 +0000843static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000844{
bartbedfd232009-03-26 19:07:15 +0000845 unsigned i;
846 Bool first;
barte278ab52012-01-24 18:28:55 +0000847 struct list_head* sg_list;
bartbedfd232009-03-26 19:07:15 +0000848 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000849
bartbedfd232009-03-26 19:07:15 +0000850 first = True;
bart8f822af2009-06-08 18:20:42 +0000851 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000852 {
barte278ab52012-01-24 18:28:55 +0000853 sg_list = &DRD_(g_threadinfo)[i].sg_list;
854 if (!list_empty(sg_list)) {
855 latest_sg = list_last_entry(sg_list, Segment, thr_list);
bartbedfd232009-03-26 19:07:15 +0000856 if (first)
857 DRD_(vc_assign)(vc, &latest_sg->vc);
858 else
859 DRD_(vc_combine)(vc, &latest_sg->vc);
860 first = False;
861 }
862 }
sewardjaf44c822007-11-25 14:01:38 +0000863}
864
865/**
bart5bd9f2d2008-03-03 20:31:58 +0000866 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000867 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000868 * data race.
869 */
bart8f822af2009-06-08 18:20:42 +0000870static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000871{
bartbedfd232009-03-26 19:07:15 +0000872 unsigned i;
873 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000874
bartbedfd232009-03-26 19:07:15 +0000875 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000876
bartbedfd232009-03-26 19:07:15 +0000877 DRD_(vc_init)(&thread_vc_min, 0, 0);
878 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
879 if (DRD_(sg_get_trace)())
880 {
bart8f822af2009-06-08 18:20:42 +0000881 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000882 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000883
bartbedfd232009-03-26 19:07:15 +0000884 DRD_(vc_init)(&thread_vc_max, 0, 0);
885 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000886 vc_min = DRD_(vc_aprint)(&thread_vc_min);
887 vc_max = DRD_(vc_aprint)(&thread_vc_max);
888 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000889 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000890 vc_min, vc_max);
891 VG_(free)(vc_min);
892 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000893 DRD_(vc_cleanup)(&thread_vc_max);
894 }
sewardjaf44c822007-11-25 14:01:38 +0000895
bart8f822af2009-06-08 18:20:42 +0000896 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000897 {
898 Segment* sg;
899 Segment* sg_next;
barte278ab52012-01-24 18:28:55 +0000900 struct list_head* sg_list;
901
902 sg_list = &DRD_(g_threadinfo)[i].sg_list;
903 list_for_each_entry_safe(sg, sg_next, sg_list, thr_list) {
904 if (list_is_last(&sg->thr_list, sg_list)
905 || !DRD_(vc_lte)(&sg->vc, &thread_vc_min))
906 break;
bartbedfd232009-03-26 19:07:15 +0000907 thread_discard_segment(i, sg);
908 }
909 }
910 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000911}
912
bart324a23b2009-02-15 12:14:52 +0000913/**
bart8f822af2009-06-08 18:20:42 +0000914 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
915 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
916 * all segments in the set CS are ordered consistently against both sg1 and
917 * sg2. The set CS is defined as the set of segments that can immediately
918 * precede future segments via inter-thread synchronization operations. In
919 * DRD the set CS consists of the latest segment of each thread combined with
920 * all segments for which the reference count is strictly greater than one.
921 * The code below is an optimized version of the following:
922 *
923 * for (i = 0; i < DRD_N_THREADS; i++)
924 * {
925 * Segment* sg;
926 *
927 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
928 * {
929 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
930 * {
931 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
932 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
933 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
934 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
935 * {
936 * return False;
937 * }
938 * }
939 * }
940 * }
941 */
942static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
943 Segment* const sg1,
944 Segment* const sg2)
945{
946 unsigned i;
barte278ab52012-01-24 18:28:55 +0000947 struct list_head* sg_list;
bart8f822af2009-06-08 18:20:42 +0000948
barte278ab52012-01-24 18:28:55 +0000949 sg_list = &DRD_(g_threadinfo)[tid].sg_list;
950 tl_assert(!list_is_last(&sg1->thr_list, sg_list));
951 tl_assert(!list_is_last(&sg2->thr_list, sg_list));
952 tl_assert(list_next_entry(&sg1->thr_list, Segment, thr_list) == sg2);
bart8f822af2009-06-08 18:20:42 +0000953 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
954
955 for (i = 0; i < DRD_N_THREADS; i++)
956 {
957 Segment* sg;
958
barte278ab52012-01-24 18:28:55 +0000959 sg_list = &DRD_(g_threadinfo)[i].sg_list;
960 list_for_each_entry(sg, sg_list, thr_list)
bart8f822af2009-06-08 18:20:42 +0000961 {
barte278ab52012-01-24 18:28:55 +0000962 if (list_is_last(&sg->thr_list, sg_list)
963 || DRD_(sg_get_refcnt)(sg) > 1)
bart8f822af2009-06-08 18:20:42 +0000964 {
965 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
966 break;
967 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
968 return False;
969 }
970 }
barte278ab52012-01-24 18:28:55 +0000971 list_for_each_entry_reverse(sg, sg_list, thr_list)
bart8f822af2009-06-08 18:20:42 +0000972 {
barte278ab52012-01-24 18:28:55 +0000973 if (list_is_last(&sg->thr_list, sg_list)
974 || DRD_(sg_get_refcnt)(sg) > 1)
bart8f822af2009-06-08 18:20:42 +0000975 {
976 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
977 break;
978 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
979 return False;
980 }
981 }
982 }
983 return True;
984}
985
986/**
bart324a23b2009-02-15 12:14:52 +0000987 * Merge all segments that may be merged without triggering false positives
988 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000989 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
990 * and Koen De Bosschere. Bounding the number of segment histories during
991 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
992 * pp 1221-1238, September 2002. This paper contains a proof that merging
993 * consecutive segments for which the property equiv(s1,s2) holds can be
994 * merged without reducing the accuracy of datarace detection. Furthermore
995 * it is also proven that the total number of all segments will never grow
996 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
997 * every time a new segment is created. The property equiv(s1, s2) is defined
998 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
999 * clocks of segments s and s1 are ordered in the same way as those of segments
1000 * s and s2. The set CS is defined as the set of existing segments s that have
1001 * the potential to conflict with not yet created segments, either because the
1002 * segment s is the latest segment of a thread or because it can become the
1003 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +00001004 */
1005static void thread_merge_segments(void)
1006{
bartbedfd232009-03-26 19:07:15 +00001007 unsigned i;
barta9c37392008-03-22 09:38:48 +00001008
bart8f822af2009-06-08 18:20:42 +00001009 s_new_segments_since_last_merge = 0;
1010
1011 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001012 {
1013 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001014
bart8f822af2009-06-08 18:20:42 +00001015#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1016 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1017#endif
barta9c37392008-03-22 09:38:48 +00001018
barte278ab52012-01-24 18:28:55 +00001019 struct list_head* sg_list = &DRD_(g_threadinfo)[i].sg_list;
1020 list_for_each_entry(sg, sg_list, thr_list)
barta9c37392008-03-22 09:38:48 +00001021 {
bartbedfd232009-03-26 19:07:15 +00001022 if (DRD_(sg_get_refcnt)(sg) == 1
barte278ab52012-01-24 18:28:55 +00001023 && !list_is_last(&sg->thr_list, sg_list)) {
1024 Segment* sg_next = list_next_entry(&sg->thr_list, Segment,
1025 thr_list);
1026 if (DRD_(sg_get_refcnt)(sg_next) == 1
1027 && !list_is_last(&sg_next->thr_list, sg_list)
1028 && thread_consistent_segment_ordering(i, sg, sg_next))
1029 {
1030 /* Merge sg and sg_next into sg. */
1031 DRD_(sg_merge)(sg, sg_next);
1032 thread_discard_segment(i, sg_next);
1033 }
bartbedfd232009-03-26 19:07:15 +00001034 }
barta9c37392008-03-22 09:38:48 +00001035 }
barta9c37392008-03-22 09:38:48 +00001036
bart8f822af2009-06-08 18:20:42 +00001037#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1038 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1039#endif
bartbedfd232009-03-26 19:07:15 +00001040 }
barta9c37392008-03-22 09:38:48 +00001041}
1042
bart324a23b2009-02-15 12:14:52 +00001043/**
bart324a23b2009-02-15 12:14:52 +00001044 * Create a new segment for the specified thread, and discard any segments
1045 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +00001046 */
bart62a784c2009-02-15 13:11:14 +00001047void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001048{
barte278ab52012-01-24 18:28:55 +00001049 struct list_head* sg_list;
bart8f822af2009-06-08 18:20:42 +00001050 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001051 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001052
bartbedfd232009-03-26 19:07:15 +00001053 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1054 && tid != DRD_INVALID_THREADID);
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
barte278ab52012-01-24 18:28:55 +00001057 sg_list = &DRD_(g_threadinfo)[tid].sg_list;
1058 last_sg = list_empty(sg_list) ? NULL
1059 : list_last_entry(sg_list, Segment, thr_list);
bartbedfd232009-03-26 19:07:15 +00001060 new_sg = DRD_(sg_new)(tid, tid);
1061 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001062 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001063 {
bart8f822af2009-06-08 18:20:42 +00001064 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001065 s_update_conflict_set_new_sg_count++;
1066 }
bartd66e3a82008-04-06 15:02:17 +00001067
bart8f822af2009-06-08 18:20:42 +00001068 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001069
bart8f822af2009-06-08 18:20:42 +00001070 if (s_segment_merging
1071 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001072 {
bart8f822af2009-06-08 18:20:42 +00001073 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001074 thread_merge_segments();
1075 }
sewardjaf44c822007-11-25 14:01:38 +00001076}
1077
bart26f73e12008-02-24 18:37:08 +00001078/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001079void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001080{
bartbedfd232009-03-26 19:07:15 +00001081 tl_assert(joiner != joinee);
1082 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1083 && joiner != DRD_INVALID_THREADID);
1084 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1085 && joinee != DRD_INVALID_THREADID);
barte278ab52012-01-24 18:28:55 +00001086 tl_assert(!list_empty(&DRD_(g_threadinfo)[joiner].sg_list));
1087 tl_assert(!list_empty(&DRD_(g_threadinfo)[joinee].sg_list));
bart8f822af2009-06-08 18:20:42 +00001088
1089 if (DRD_(sg_get_trace)())
1090 {
1091 char *str1, *str2;
bartc6bf1842012-01-22 08:40:42 +00001092 str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1093 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee));
sewardj1e29ebc2009-07-15 14:49:17 +00001094 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001095 str1, str2);
1096 VG_(free)(str1);
1097 VG_(free)(str2);
1098 }
bartae37e6d2012-01-22 08:58:31 +00001099 if (joiner == DRD_(g_drd_running_tid)) {
barte5214662009-06-21 11:51:23 +00001100 VectorClock old_vc;
1101
bartc6bf1842012-01-22 08:40:42 +00001102 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner));
1103 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1104 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001105 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1106 s_update_conflict_set_join_count++;
1107 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001108 } else {
bartc6bf1842012-01-22 08:40:42 +00001109 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1110 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001111 }
1112
1113 thread_discard_ordered_segments();
1114
bartae37e6d2012-01-22 08:58:31 +00001115 if (DRD_(sg_get_trace)()) {
bart8f822af2009-06-08 18:20:42 +00001116 char* str;
bartae37e6d2012-01-22 08:58:31 +00001117
bartc6bf1842012-01-22 08:40:42 +00001118 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
sewardj1e29ebc2009-07-15 14:49:17 +00001119 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001120 VG_(free)(str);
1121 }
sewardjaf44c822007-11-25 14:01:38 +00001122}
1123
bart324a23b2009-02-15 12:14:52 +00001124/**
bart8f822af2009-06-08 18:20:42 +00001125 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001126 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001127 */
bartf6ec1fe2009-06-21 18:07:35 +00001128static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001129{
bart8f822af2009-06-08 18:20:42 +00001130 const VectorClock* const vc = &sg->vc;
1131
bartbedfd232009-03-26 19:07:15 +00001132 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1133 && tid != DRD_INVALID_THREADID);
barte278ab52012-01-24 18:28:55 +00001134 tl_assert(!list_empty(&DRD_(g_threadinfo)[tid].sg_list));
bart8f822af2009-06-08 18:20:42 +00001135 tl_assert(sg);
bartbedfd232009-03-26 19:07:15 +00001136 tl_assert(vc);
bart8f822af2009-06-08 18:20:42 +00001137
bartae37e6d2012-01-22 08:58:31 +00001138 if (tid != sg->tid) {
bart8f822af2009-06-08 18:20:42 +00001139 VectorClock old_vc;
1140
bartc6bf1842012-01-22 08:40:42 +00001141 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(tid));
1142 DRD_(vc_combine)(DRD_(thread_get_vc)(tid), vc);
bartae37e6d2012-01-22 08:58:31 +00001143 if (DRD_(sg_get_trace)()) {
bart8f822af2009-06-08 18:20:42 +00001144 char *str1, *str2;
1145 str1 = DRD_(vc_aprint)(&old_vc);
bartc6bf1842012-01-22 08:40:42 +00001146 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
sewardj1e29ebc2009-07-15 14:49:17 +00001147 VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2);
bart8f822af2009-06-08 18:20:42 +00001148 VG_(free)(str1);
1149 VG_(free)(str2);
1150 }
barte5214662009-06-21 11:51:23 +00001151
bart8f822af2009-06-08 18:20:42 +00001152 thread_discard_ordered_segments();
barte5214662009-06-21 11:51:23 +00001153
bart8f822af2009-06-08 18:20:42 +00001154 DRD_(thread_update_conflict_set)(tid, &old_vc);
barte5214662009-06-21 11:51:23 +00001155 s_update_conflict_set_sync_count++;
1156
bart8f822af2009-06-08 18:20:42 +00001157 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001158 } else {
bartc6bf1842012-01-22 08:40:42 +00001159 tl_assert(DRD_(vc_lte)(vc, DRD_(thread_get_vc)(tid)));
bart8f822af2009-06-08 18:20:42 +00001160 }
sewardjaf44c822007-11-25 14:01:38 +00001161}
1162
bart324a23b2009-02-15 12:14:52 +00001163/**
bartf6ec1fe2009-06-21 18:07:35 +00001164 * Create a new segment for thread tid and update the vector clock of the last
1165 * segment of this thread with the the vector clock of segment sg. Call this
1166 * function after thread tid had to wait because of thread synchronization
1167 * until the memory accesses in the segment sg finished.
1168 */
1169void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg)
1170{
1171 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1172 && tid != DRD_INVALID_THREADID);
1173 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1174 tl_assert(sg);
1175
1176 thread_append_segment(tid, DRD_(sg_new)(tid, tid));
1177
1178 thread_combine_vc_sync(tid, sg);
1179
1180 if (s_segment_merging
1181 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
1182 {
1183 thread_discard_ordered_segments();
1184 thread_merge_segments();
1185 }
1186}
1187
1188/**
bart324a23b2009-02-15 12:14:52 +00001189 * Call this function whenever a thread is no longer using the memory
1190 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
1191 * increase.
bart26f73e12008-02-24 18:37:08 +00001192 */
bart23ef19d2011-03-12 12:34:44 +00001193void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +00001194{
bart178b6862011-07-29 12:30:43 +00001195 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001196
barte278ab52012-01-24 18:28:55 +00001197 list_for_each_entry(p, &DRD_(g_sg_list), g_list)
1198 DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001199
bart178b6862011-07-29 12:30:43 +00001200 DRD_(bm_clear)(DRD_(g_conflict_set), a1, a2);
sewardjaf44c822007-11-25 14:01:38 +00001201}
1202
bartd45d9952009-05-31 18:53:54 +00001203/** Specify whether memory loads should be recorded. */
1204void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001205{
bartbedfd232009-03-26 19:07:15 +00001206 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1207 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001208 tl_assert(enabled == !! enabled);
1209
1210 DRD_(g_threadinfo)[tid].is_recording_loads = enabled;
bart0268dfa2008-03-11 20:10:21 +00001211}
1212
bartd45d9952009-05-31 18:53:54 +00001213/** Specify whether memory stores should be recorded. */
1214void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled)
bart0268dfa2008-03-11 20:10:21 +00001215{
bartbedfd232009-03-26 19:07:15 +00001216 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1217 && tid != DRD_INVALID_THREADID);
bartd45d9952009-05-31 18:53:54 +00001218 tl_assert(enabled == !! enabled);
1219
1220 DRD_(g_threadinfo)[tid].is_recording_stores = enabled;
bart0268dfa2008-03-11 20:10:21 +00001221}
1222
bart86a87df2009-03-04 19:26:47 +00001223/**
1224 * Print the segment information for all threads.
1225 *
1226 * This function is only used for debugging purposes.
1227 */
bart62a784c2009-02-15 13:11:14 +00001228void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +00001229{
bartbedfd232009-03-26 19:07:15 +00001230 unsigned i;
barte278ab52012-01-24 18:28:55 +00001231 struct list_head* sg_list;
bartbedfd232009-03-26 19:07:15 +00001232 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001233
bart8f822af2009-06-08 18:20:42 +00001234 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001235 {
barte278ab52012-01-24 18:28:55 +00001236 sg_list = &DRD_(g_threadinfo)[i].sg_list;
1237 if (!list_empty(sg_list))
sewardjaf44c822007-11-25 14:01:38 +00001238 {
bartbedfd232009-03-26 19:07:15 +00001239 VG_(printf)("**************\n"
bart6d956dc2011-07-28 09:54:37 +00001240 "* thread %3d (%d/%d/%d/%d/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001241 "**************\n",
1242 i,
bart6d956dc2011-07-28 09:54:37 +00001243 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001244 DRD_(g_threadinfo)[i].vg_thread_exists,
1245 DRD_(g_threadinfo)[i].vg_threadid,
1246 DRD_(g_threadinfo)[i].posix_thread_exists,
1247 DRD_(g_threadinfo)[i].pt_threadid,
1248 DRD_(g_threadinfo)[i].detached_posix_thread);
barte278ab52012-01-24 18:28:55 +00001249 list_for_each_entry(p, sg_list, thr_list)
bartbedfd232009-03-26 19:07:15 +00001250 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +00001251 }
bartbedfd232009-03-26 19:07:15 +00001252 }
sewardjaf44c822007-11-25 14:01:38 +00001253}
1254
bart86a87df2009-03-04 19:26:47 +00001255/** Show a call stack involved in a data race. */
barte7086002011-10-11 19:08:39 +00001256static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
sewardjaf44c822007-11-25 14:01:38 +00001257{
bartbedfd232009-03-26 19:07:15 +00001258 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001259
barte7086002011-10-11 19:08:39 +00001260 if (vg_tid != VG_INVALID_THREADID) {
bartbedfd232009-03-26 19:07:15 +00001261 if (callstack)
bartbedfd232009-03-26 19:07:15 +00001262 VG_(pp_ExeContext)(callstack);
bartbedfd232009-03-26 19:07:15 +00001263 else
bartbedfd232009-03-26 19:07:15 +00001264 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
barte7086002011-10-11 19:08:39 +00001265 } else {
1266 if (!VG_(clo_xml))
1267 VG_(message)(Vg_UserMsg,
1268 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001269 }
sewardjaf44c822007-11-25 14:01:38 +00001270}
1271
bart86a87df2009-03-04 19:26:47 +00001272/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001273static void
1274thread_report_conflicting_segments_segment(const DrdThreadId tid,
1275 const Addr addr,
1276 const SizeT size,
1277 const BmAccessTypeT access_type,
1278 const Segment* const p)
1279{
bartbedfd232009-03-26 19:07:15 +00001280 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001281
bartbedfd232009-03-26 19:07:15 +00001282 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1283 && tid != DRD_INVALID_THREADID);
1284 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001285
bart8f822af2009-06-08 18:20:42 +00001286 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001287 {
1288 if (i != tid)
sewardjaf44c822007-11-25 14:01:38 +00001289 {
bartbedfd232009-03-26 19:07:15 +00001290 Segment* q;
barte278ab52012-01-24 18:28:55 +00001291 struct list_head *sg_list;
1292
1293 sg_list = &DRD_(g_threadinfo)[i].sg_list;
1294 list_for_each_entry_reverse(q, sg_list, thr_list) {
bartbedfd232009-03-26 19:07:15 +00001295 /*
bart31b983d2010-02-21 14:52:59 +00001296 * Since q iterates over the segments of thread i in order of
1297 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001298 * q->next->vc <= p->vc will also hold. Hence, break out of the
1299 * loop once this condition is met.
1300 */
1301 if (DRD_(vc_lte)(&q->vc, &p->vc))
1302 break;
1303 if (! DRD_(vc_lte)(&p->vc, &q->vc))
1304 {
bart8f822af2009-06-08 18:20:42 +00001305 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bartbedfd232009-03-26 19:07:15 +00001306 access_type))
1307 {
barte278ab52012-01-24 18:28:55 +00001308 Segment* q_next;
1309
bartbedfd232009-03-26 19:07:15 +00001310 tl_assert(q->stacktrace);
barte7086002011-10-11 19:08:39 +00001311 if (VG_(clo_xml))
1312 VG_(printf_xml)(" <other_segment_start>\n");
1313 else
1314 VG_(message)(Vg_UserMsg,
1315 "Other segment start (thread %d)\n", i);
1316 show_call_stack(i, q->stacktrace);
1317 if (VG_(clo_xml))
1318 VG_(printf_xml)(" </other_segment_start>\n"
1319 " <other_segment_end>\n");
1320 else
1321 VG_(message)(Vg_UserMsg,
1322 "Other segment end (thread %d)\n", i);
barte278ab52012-01-24 18:28:55 +00001323 q_next = list_is_last(&q->thr_list, sg_list)
1324 ? NULL : list_next_entry(&q->thr_list, Segment, thr_list);
1325 show_call_stack(i, q_next ? q_next->stacktrace : 0);
barte7086002011-10-11 19:08:39 +00001326 if (VG_(clo_xml))
1327 VG_(printf_xml)(" </other_segment_end>\n");
bartbedfd232009-03-26 19:07:15 +00001328 }
1329 }
1330 }
sewardjaf44c822007-11-25 14:01:38 +00001331 }
bartbedfd232009-03-26 19:07:15 +00001332 }
sewardjaf44c822007-11-25 14:01:38 +00001333}
1334
bart86a87df2009-03-04 19:26:47 +00001335/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001336void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1337 const Addr addr,
1338 const SizeT size,
1339 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001340{
bartbedfd232009-03-26 19:07:15 +00001341 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001342
bartbedfd232009-03-26 19:07:15 +00001343 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1344 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001345
barte278ab52012-01-24 18:28:55 +00001346 list_for_each_entry(p, &DRD_(g_threadinfo)[tid].sg_list, thr_list) {
bart8f822af2009-06-08 18:20:42 +00001347 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001348 thread_report_conflicting_segments_segment(tid, addr, size,
1349 access_type, p);
bartbedfd232009-03-26 19:07:15 +00001350 }
sewardjaf44c822007-11-25 14:01:38 +00001351}
sewardjaf44c822007-11-25 14:01:38 +00001352
bart324a23b2009-02-15 12:14:52 +00001353/**
bart8f822af2009-06-08 18:20:42 +00001354 * Verify whether the conflict set for thread tid is up to date. Only perform
1355 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1356 */
1357static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1358{
1359 static int do_verify_conflict_set = -1;
1360 Bool result;
1361 struct bitmap* computed_conflict_set = 0;
1362
1363 if (do_verify_conflict_set < 0)
1364 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1365
1366 if (do_verify_conflict_set == 0)
1367 return True;
1368
1369 thread_compute_conflict_set(&computed_conflict_set, tid);
1370 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1371 if (! result)
1372 {
1373 VG_(printf)("actual conflict set:\n");
1374 DRD_(bm_print)(DRD_(g_conflict_set));
1375 VG_(printf)("\n");
1376 VG_(printf)("computed conflict set:\n");
1377 DRD_(bm_print)(computed_conflict_set);
1378 VG_(printf)("\n");
1379 }
1380 DRD_(bm_delete)(computed_conflict_set);
1381 return result;
1382}
1383
1384/**
1385 * Compute the conflict set: a bitmap that represents the union of all memory
1386 * accesses of all segments that are unordered to the current segment of the
1387 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001388 */
bart86a87df2009-03-04 19:26:47 +00001389static void thread_compute_conflict_set(struct bitmap** conflict_set,
1390 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001391{
bartbedfd232009-03-26 19:07:15 +00001392 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001393
bartbedfd232009-03-26 19:07:15 +00001394 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1395 && tid != DRD_INVALID_THREADID);
1396 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001397
bart54803fe2009-06-21 09:26:27 +00001398 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001399 s_conflict_set_bitmap_creation_count
1400 -= DRD_(bm_get_bitmap_creation_count)();
1401 s_conflict_set_bitmap2_creation_count
1402 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001403
bartae37e6d2012-01-22 08:58:31 +00001404 if (*conflict_set) {
bartf6ec1fe2009-06-21 18:07:35 +00001405 DRD_(bm_cleanup)(*conflict_set);
1406 DRD_(bm_init)(*conflict_set);
bartae37e6d2012-01-22 08:58:31 +00001407 } else {
bartf6ec1fe2009-06-21 18:07:35 +00001408 *conflict_set = DRD_(bm_new)();
1409 }
bart26f73e12008-02-24 18:37:08 +00001410
bartae37e6d2012-01-22 08:58:31 +00001411 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001412 char* str;
bart26f73e12008-02-24 18:37:08 +00001413
bartc6bf1842012-01-22 08:40:42 +00001414 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001415 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001416 "computing conflict set for thread %d with vc %s\n",
1417 tid, str);
bart8f822af2009-06-08 18:20:42 +00001418 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001419 }
sewardjaf44c822007-11-25 14:01:38 +00001420
barte278ab52012-01-24 18:28:55 +00001421 p = list_last_entry(&DRD_(g_threadinfo)[tid].sg_list, Segment, thr_list);
bartbedfd232009-03-26 19:07:15 +00001422 {
1423 unsigned j;
1424
1425 if (s_trace_conflict_set)
bart26f73e12008-02-24 18:37:08 +00001426 {
bart8f822af2009-06-08 18:20:42 +00001427 char* vc;
bartbedfd232009-03-26 19:07:15 +00001428
bart8f822af2009-06-08 18:20:42 +00001429 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001430 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001431 tid, vc);
1432 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001433 }
sewardjaf44c822007-11-25 14:01:38 +00001434
bart8f822af2009-06-08 18:20:42 +00001435 for (j = 0; j < DRD_N_THREADS; j++)
bartbedfd232009-03-26 19:07:15 +00001436 {
1437 if (j != tid && DRD_(IsValidDrdThreadId)(j))
1438 {
bart8f822af2009-06-08 18:20:42 +00001439 Segment* q;
barte278ab52012-01-24 18:28:55 +00001440 list_for_each_entry_reverse(q, &DRD_(g_threadinfo)[j].sg_list,
1441 thr_list) {
bartbedfd232009-03-26 19:07:15 +00001442 if (! DRD_(vc_lte)(&q->vc, &p->vc)
1443 && ! DRD_(vc_lte)(&p->vc, &q->vc))
1444 {
1445 if (s_trace_conflict_set)
1446 {
bart8f822af2009-06-08 18:20:42 +00001447 char* str;
1448
1449 str = DRD_(vc_aprint)(&q->vc);
1450 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001451 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001452 j, str);
1453 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001454 }
bart8f822af2009-06-08 18:20:42 +00001455 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bartbedfd232009-03-26 19:07:15 +00001456 }
1457 else
1458 {
1459 if (s_trace_conflict_set)
1460 {
bart8f822af2009-06-08 18:20:42 +00001461 char* str;
1462
1463 str = DRD_(vc_aprint)(&q->vc);
1464 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001465 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001466 j, str);
1467 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001468 }
1469 }
1470 }
1471 }
1472 }
1473 }
sewardjaf44c822007-11-25 14:01:38 +00001474
bartbedfd232009-03-26 19:07:15 +00001475 s_conflict_set_bitmap_creation_count
1476 += DRD_(bm_get_bitmap_creation_count)();
1477 s_conflict_set_bitmap2_creation_count
1478 += DRD_(bm_get_bitmap2_creation_count)();
1479
bart8f822af2009-06-08 18:20:42 +00001480 if (s_trace_conflict_set_bm)
bartbedfd232009-03-26 19:07:15 +00001481 {
sewardj1e29ebc2009-07-15 14:49:17 +00001482 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001483 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001484 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001485 }
sewardjaf44c822007-11-25 14:01:38 +00001486}
1487
bart8f822af2009-06-08 18:20:42 +00001488/**
1489 * Update the conflict set after the vector clock of thread tid has been
1490 * updated from old_vc to its current value, either because a new segment has
1491 * been created or because of a synchronization operation.
1492 */
1493void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1494 const VectorClock* const old_vc)
1495{
1496 const VectorClock* new_vc;
1497 Segment* p;
1498 unsigned j;
1499
1500 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1501 && tid != DRD_INVALID_THREADID);
1502 tl_assert(old_vc);
1503 tl_assert(tid == DRD_(g_drd_running_tid));
1504 tl_assert(DRD_(g_conflict_set));
1505
bartae37e6d2012-01-22 08:58:31 +00001506 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001507 char* str;
1508
bartc6bf1842012-01-22 08:40:42 +00001509 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001510 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001511 "updating conflict set for thread %d with vc %s\n",
1512 tid, str);
bart8f822af2009-06-08 18:20:42 +00001513 VG_(free)(str);
1514 }
1515
bartc6bf1842012-01-22 08:40:42 +00001516 new_vc = DRD_(thread_get_vc)(tid);
bartf5fe4b62011-07-03 11:39:30 +00001517 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
bart8f822af2009-06-08 18:20:42 +00001518
1519 DRD_(bm_unmark)(DRD_(g_conflict_set));
1520
1521 for (j = 0; j < DRD_N_THREADS; j++)
1522 {
1523 Segment* q;
1524
1525 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1526 continue;
1527
barte278ab52012-01-24 18:28:55 +00001528 list_for_each_entry_reverse(q, &DRD_(g_threadinfo)[j].sg_list, thr_list) {
1529 Bool included_in_old_conflict_set, included_in_new_conflict_set;
1530
1531 if (DRD_(vc_lte)(&q->vc, new_vc))
1532 break;
1533
1534 included_in_old_conflict_set = !DRD_(vc_lte)(old_vc, &q->vc);
1535 included_in_new_conflict_set = !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001536
1537 if (UNLIKELY(s_trace_conflict_set)) {
1538 char* str;
1539
1540 str = DRD_(vc_aprint)(&q->vc);
1541 VG_(message)(Vg_DebugMsg,
1542 "conflict set: [%d] %s segment %s\n", j,
1543 included_in_old_conflict_set
1544 != included_in_new_conflict_set
1545 ? "merging" : "ignoring", str);
1546 VG_(free)(str);
1547 }
1548 if (included_in_old_conflict_set != included_in_new_conflict_set)
1549 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1550 }
1551
barte278ab52012-01-24 18:28:55 +00001552 list_for_each_entry_reverse_continue(q, &DRD_(g_threadinfo)[j].sg_list,
1553 thr_list) {
1554 Bool included_in_old_conflict_set, included_in_new_conflict_set;
1555
1556 if (DRD_(vc_lte)(&q->vc, old_vc))
1557 break;
1558
1559 included_in_old_conflict_set = !DRD_(vc_lte)(old_vc, &q->vc);
1560 included_in_new_conflict_set
1561 = !DRD_(vc_lte)(&q->vc, new_vc) && !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001562
1563 if (UNLIKELY(s_trace_conflict_set)) {
bartac5b95b2011-07-03 11:43:45 +00001564 char* str;
1565
1566 str = DRD_(vc_aprint)(&q->vc);
1567 VG_(message)(Vg_DebugMsg,
1568 "conflict set: [%d] %s segment %s\n", j,
1569 included_in_old_conflict_set
1570 != included_in_new_conflict_set
1571 ? "merging" : "ignoring", str);
1572 VG_(free)(str);
1573 }
bart8f822af2009-06-08 18:20:42 +00001574 if (included_in_old_conflict_set != included_in_new_conflict_set)
bart8f822af2009-06-08 18:20:42 +00001575 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001576 }
1577 }
1578
1579 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1580
barte278ab52012-01-24 18:28:55 +00001581 p = list_last_entry(&DRD_(g_threadinfo)[tid].sg_list, Segment, thr_list);
bart4b3fdb22011-07-03 11:40:49 +00001582 for (j = 0; j < DRD_N_THREADS; j++)
bart8f822af2009-06-08 18:20:42 +00001583 {
bart4b3fdb22011-07-03 11:40:49 +00001584 if (j != tid && DRD_(IsValidDrdThreadId)(j))
bart8f822af2009-06-08 18:20:42 +00001585 {
bart4b3fdb22011-07-03 11:40:49 +00001586 Segment* q;
barte278ab52012-01-24 18:28:55 +00001587 list_for_each_entry_reverse(q, &DRD_(g_threadinfo)[j].sg_list,
1588 thr_list) {
1589 if (DRD_(vc_lte)(&q->vc, &p->vc))
1590 break;
bart178b6862011-07-29 12:30:43 +00001591 if (!DRD_(vc_lte)(&p->vc, &q->vc))
bart4b3fdb22011-07-03 11:40:49 +00001592 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001593 }
1594 }
1595 }
1596
1597 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1598
bart54803fe2009-06-21 09:26:27 +00001599 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001600
1601 if (s_trace_conflict_set_bm)
1602 {
sewardj1e29ebc2009-07-15 14:49:17 +00001603 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001604 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001605 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001606 }
1607
1608 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1609}
1610
bart86a87df2009-03-04 19:26:47 +00001611/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001612ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001613{
bartbedfd232009-03-26 19:07:15 +00001614 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001615}
1616
bart86a87df2009-03-04 19:26:47 +00001617/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001618ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001619{
bartbedfd232009-03-26 19:07:15 +00001620 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001621}
1622
bart54803fe2009-06-21 09:26:27 +00001623/** Return how many times the conflict set has been updated entirely. */
1624ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001625{
bart54803fe2009-06-21 09:26:27 +00001626 return s_compute_conflict_set_count;
1627}
1628
1629/** Return how many times the conflict set has been updated partially. */
1630ULong DRD_(thread_get_update_conflict_set_count)(void)
1631{
bartbedfd232009-03-26 19:07:15 +00001632 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001633}
1634
bart86a87df2009-03-04 19:26:47 +00001635/**
barte5214662009-06-21 11:51:23 +00001636 * Return how many times the conflict set has been updated partially
1637 * because a new segment has been created.
1638 */
1639ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1640{
1641 return s_update_conflict_set_new_sg_count;
1642}
1643
1644/**
1645 * Return how many times the conflict set has been updated partially
1646 * because of combining vector clocks due to synchronization operations
1647 * other than reader/writer lock or barrier operations.
1648 */
1649ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1650{
1651 return s_update_conflict_set_sync_count;
1652}
1653
1654/**
1655 * Return how many times the conflict set has been updated partially
1656 * because of thread joins.
1657 */
1658ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1659{
1660 return s_update_conflict_set_join_count;
1661}
1662
1663/**
bart86a87df2009-03-04 19:26:47 +00001664 * Return the number of first-level bitmaps that have been created during
1665 * conflict set updates.
1666 */
bart62a784c2009-02-15 13:11:14 +00001667ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001668{
bartbedfd232009-03-26 19:07:15 +00001669 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001670}
1671
bart86a87df2009-03-04 19:26:47 +00001672/**
1673 * Return the number of second-level bitmaps that have been created during
1674 * conflict set updates.
1675 */
bart62a784c2009-02-15 13:11:14 +00001676ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001677{
bartbedfd232009-03-26 19:07:15 +00001678 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001679}