blob: 6e2dd419674be9fd4ad02e8e6012d8be9688ea0d [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{
barte278ab52012-01-24 18:28:55 +0000145}
146
sewardjaf44c822007-11-25 14:01:38 +0000147/**
bart86a87df2009-03-04 19:26:47 +0000148 * Convert Valgrind's ThreadId into a DrdThreadId.
149 *
150 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
151 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000152 */
bart62a784c2009-02-15 13:11:14 +0000153DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000154{
bartbedfd232009-03-26 19:07:15 +0000155 int i;
sewardjaf44c822007-11-25 14:01:38 +0000156
bartbedfd232009-03-26 19:07:15 +0000157 if (tid == VG_INVALID_THREADID)
158 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000159
bartbedfd232009-03-26 19:07:15 +0000160 for (i = 1; i < DRD_N_THREADS; i++)
161 {
162 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
163 && DRD_(g_threadinfo)[i].vg_threadid == tid)
164 {
165 return i;
166 }
167 }
sewardjaf44c822007-11-25 14:01:38 +0000168
bartbedfd232009-03-26 19:07:15 +0000169 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000170}
171
bart86a87df2009-03-04 19:26:47 +0000172/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000173static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000174{
bartbedfd232009-03-26 19:07:15 +0000175 int i;
sewardjaf44c822007-11-25 14:01:38 +0000176
bartbedfd232009-03-26 19:07:15 +0000177 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000178
bartbedfd232009-03-26 19:07:15 +0000179 for (i = 1; i < DRD_N_THREADS; i++)
180 {
bart6d956dc2011-07-28 09:54:37 +0000181 if (!DRD_(g_threadinfo)[i].valid)
bartbedfd232009-03-26 19:07:15 +0000182 {
183 tl_assert(! DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000184
bart6d956dc2011-07-28 09:54:37 +0000185 DRD_(g_threadinfo)[i].valid = True;
bartbedfd232009-03-26 19:07:15 +0000186 DRD_(g_threadinfo)[i].vg_thread_exists = True;
187 DRD_(g_threadinfo)[i].vg_threadid = tid;
188 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
189 DRD_(g_threadinfo)[i].stack_min = 0;
190 DRD_(g_threadinfo)[i].stack_min_min = 0;
191 DRD_(g_threadinfo)[i].stack_startup = 0;
192 DRD_(g_threadinfo)[i].stack_max = 0;
bartd45d9952009-05-31 18:53:54 +0000193 DRD_(thread_set_name)(i, "");
bart383d6132010-09-02 14:43:18 +0000194 DRD_(g_threadinfo)[i].on_alt_stack = False;
bartd45d9952009-05-31 18:53:54 +0000195 DRD_(g_threadinfo)[i].is_recording_loads = True;
196 DRD_(g_threadinfo)[i].is_recording_stores = True;
bartdd75cdf2009-07-24 08:20:10 +0000197 DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0;
bartbedfd232009-03-26 19:07:15 +0000198 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart6d956dc2011-07-28 09:54:37 +0000199 DRD_(g_threadinfo)[i].deletion_seq = s_deletion_tail - 1;
bart91b7ec32012-01-25 20:36:27 +0000200 tl_assert(DRD_(g_threadinfo)[i].sg_first == NULL);
201 tl_assert(DRD_(g_threadinfo)[i].sg_last == NULL);
bart86a87df2009-03-04 19:26:47 +0000202
bartbedfd232009-03-26 19:07:15 +0000203 tl_assert(DRD_(IsValidDrdThreadId)(i));
bart86a87df2009-03-04 19:26:47 +0000204
bartbedfd232009-03-26 19:07:15 +0000205 return i;
206 }
207 }
sewardjaf44c822007-11-25 14:01:38 +0000208
bart3c9afb12009-07-24 11:11:30 +0000209 VG_(printf)(
210"\nSorry, but the maximum number of threads supported by DRD has been exceeded."
211"Aborting.\n");
212
bartbedfd232009-03-26 19:07:15 +0000213 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000214
bartbedfd232009-03-26 19:07:15 +0000215 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000216}
217
bart86a87df2009-03-04 19:26:47 +0000218/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000219DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000220{
bartbedfd232009-03-26 19:07:15 +0000221 int i;
sewardjaf44c822007-11-25 14:01:38 +0000222
bartb48bde22009-07-31 08:26:17 +0000223 if (tid != INVALID_POSIX_THREADID)
bartbedfd232009-03-26 19:07:15 +0000224 {
bartb48bde22009-07-31 08:26:17 +0000225 for (i = 1; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000226 {
bartb48bde22009-07-31 08:26:17 +0000227 if (DRD_(g_threadinfo)[i].posix_thread_exists
228 && DRD_(g_threadinfo)[i].pt_threadid == tid)
229 {
230 return i;
231 }
bartbedfd232009-03-26 19:07:15 +0000232 }
233 }
234 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000235}
236
bart86a87df2009-03-04 19:26:47 +0000237/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000238ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000239{
bartbedfd232009-03-26 19:07:15 +0000240 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
241 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000242
bartbedfd232009-03-26 19:07:15 +0000243 return (DRD_(g_threadinfo)[tid].vg_thread_exists
244 ? DRD_(g_threadinfo)[tid].vg_threadid
245 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000246}
247
bart8f822af2009-06-08 18:20:42 +0000248#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
bart324a23b2009-02-15 12:14:52 +0000249/**
250 * Sanity check of the doubly linked list of segments referenced by a
251 * ThreadInfo struct.
252 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000253 */
bart62a784c2009-02-15 13:11:14 +0000254static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000255{
bartbedfd232009-03-26 19:07:15 +0000256 Segment* p;
bart8f822af2009-06-08 18:20:42 +0000257
bartbedfd232009-03-26 19:07:15 +0000258 for (p = ti->first; p; p = p->next) {
259 if (p->next && p->next->prev != p)
260 return False;
261 if (p->next == 0 && p != ti->last)
262 return False;
263 }
264 for (p = ti->last; p; p = p->prev) {
265 if (p->prev && p->prev->next != p)
266 return False;
267 if (p->prev == 0 && p != ti->first)
268 return False;
269 }
270 return True;
sewardjaf44c822007-11-25 14:01:38 +0000271}
bart23d3a4e2008-04-05 12:53:00 +0000272#endif
sewardjaf44c822007-11-25 14:01:38 +0000273
bart439c55f2009-02-15 10:38:37 +0000274/**
275 * Create the first segment for a newly started thread.
276 *
277 * This function is called from the handler installed via
278 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
279 * from the context of the creator thread, before the new thread has been
280 * created.
bart86a87df2009-03-04 19:26:47 +0000281 *
282 * @param[in] creator DRD thread ID of the creator thread.
283 * @param[in] vg_created Valgrind thread ID of the created thread.
284 *
285 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000286 */
bart62a784c2009-02-15 13:11:14 +0000287DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
288 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000289{
bartbedfd232009-03-26 19:07:15 +0000290 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000291
bartbedfd232009-03-26 19:07:15 +0000292 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
293 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
294 tl_assert(0 <= (int)created && created < DRD_N_THREADS
295 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000296
bart91b7ec32012-01-25 20:36:27 +0000297 tl_assert(DRD_(g_threadinfo)[created].sg_first == NULL);
298 tl_assert(DRD_(g_threadinfo)[created].sg_last == NULL);
bart8f822af2009-06-08 18:20:42 +0000299 /* Create an initial segment for the newly created thread. */
bartbedfd232009-03-26 19:07:15 +0000300 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000301
bartbedfd232009-03-26 19:07:15 +0000302 return created;
sewardjaf44c822007-11-25 14:01:38 +0000303}
304
bart439c55f2009-02-15 10:38:37 +0000305/**
bart86a87df2009-03-04 19:26:47 +0000306 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
307 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000308 * on the newly created thread, e.g. from the handler installed via
309 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000310 *
311 * @param[in] vg_created Valgrind thread ID of the newly created thread.
312 *
313 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000314 */
bart62a784c2009-02-15 13:11:14 +0000315DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000316{
bartbedfd232009-03-26 19:07:15 +0000317 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000318
bartbedfd232009-03-26 19:07:15 +0000319 tl_assert(0 <= (int)created && created < DRD_N_THREADS
320 && created != DRD_INVALID_THREADID);
bart439c55f2009-02-15 10:38:37 +0000321
bartbedfd232009-03-26 19:07:15 +0000322 DRD_(g_threadinfo)[created].stack_max
323 = VG_(thread_get_stack_max)(vg_created);
324 DRD_(g_threadinfo)[created].stack_startup
325 = DRD_(g_threadinfo)[created].stack_max;
326 DRD_(g_threadinfo)[created].stack_min
327 = DRD_(g_threadinfo)[created].stack_max;
328 DRD_(g_threadinfo)[created].stack_min_min
329 = DRD_(g_threadinfo)[created].stack_max;
330 DRD_(g_threadinfo)[created].stack_size
331 = VG_(thread_get_stack_size)(vg_created);
332 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000333
bartbedfd232009-03-26 19:07:15 +0000334 return created;
bart439c55f2009-02-15 10:38:37 +0000335}
bart09dc13f2009-02-14 15:13:31 +0000336
bart6d956dc2011-07-28 09:54:37 +0000337static void DRD_(thread_delayed_delete)(const DrdThreadId tid)
338{
339 int j;
340
341 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
342 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
343 DRD_(g_threadinfo)[tid].deletion_seq = s_deletion_head++;
344#if 0
345 VG_(message)(Vg_DebugMsg, "Adding thread %d to the deletion list\n", tid);
346#endif
347 if (s_deletion_head - s_deletion_tail >= s_join_list_vol) {
348 for (j = 0; j < DRD_N_THREADS; ++j) {
349 if (DRD_(IsValidDrdThreadId)(j)
350 && DRD_(g_threadinfo)[j].deletion_seq == s_deletion_tail)
351 {
352 s_deletion_tail++;
353#if 0
354 VG_(message)(Vg_DebugMsg, "Delayed delete of thread %d\n", j);
355#endif
356 DRD_(thread_delete)(j, False);
357 break;
358 }
359 }
360 }
361}
362
bart324a23b2009-02-15 12:14:52 +0000363/**
364 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
365 * after thread drd_joiner joined thread drd_joinee.
366 */
bart09dc13f2009-02-14 15:13:31 +0000367void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
368{
bartbedfd232009-03-26 19:07:15 +0000369 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
370 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
bart7627be32009-06-06 12:26:05 +0000371
bartbedfd232009-03-26 19:07:15 +0000372 DRD_(thread_new_segment)(drd_joiner);
bart8f822af2009-06-08 18:20:42 +0000373 DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee);
bart7627be32009-06-06 12:26:05 +0000374 DRD_(thread_new_segment)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000375
bartbedfd232009-03-26 19:07:15 +0000376 if (s_trace_fork_join)
377 {
378 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
bartbedfd232009-03-26 19:07:15 +0000379 const unsigned msg_size = 256;
380 char* msg;
bart09dc13f2009-02-14 15:13:31 +0000381
bartbedfd232009-03-26 19:07:15 +0000382 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
383 tl_assert(msg);
384 VG_(snprintf)(msg, msg_size,
bart63c92ea2009-07-19 17:53:56 +0000385 "drd_post_thread_join joiner = %d, joinee = %d",
386 drd_joiner, drd_joinee);
bartbedfd232009-03-26 19:07:15 +0000387 if (joiner)
388 {
bart8f822af2009-06-08 18:20:42 +0000389 char* vc;
390
391 vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner));
bartbedfd232009-03-26 19:07:15 +0000392 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart8f822af2009-06-08 18:20:42 +0000393 ", new vc: %s", vc);
394 VG_(free)(vc);
bartbedfd232009-03-26 19:07:15 +0000395 }
bartad994e82011-10-13 18:04:30 +0000396 DRD_(trace_msg)("%pS", msg);
bartbedfd232009-03-26 19:07:15 +0000397 VG_(free)(msg);
398 }
bart09dc13f2009-02-14 15:13:31 +0000399
bartbedfd232009-03-26 19:07:15 +0000400 if (! DRD_(get_check_stack_accesses)())
401 {
402 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
403 - DRD_(thread_get_stack_size)(drd_joinee),
404 DRD_(thread_get_stack_max)(drd_joinee));
405 }
406 DRD_(clientobj_delete_thread)(drd_joinee);
bart6d956dc2011-07-28 09:54:37 +0000407 DRD_(thread_delayed_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000408}
409
bart324a23b2009-02-15 12:14:52 +0000410/**
411 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
412 * and accesses this data structure from multiple threads without locking.
413 * Any conflicting accesses in the range stack_startup..stack_max will be
414 * ignored.
415 */
bart62a784c2009-02-15 13:11:14 +0000416void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
417 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000418{
bartbedfd232009-03-26 19:07:15 +0000419 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
420 && tid != DRD_INVALID_THREADID);
421 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
422 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
423 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000424}
425
bart86a87df2009-03-04 19:26:47 +0000426/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000427Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000428{
bartbedfd232009-03-26 19:07:15 +0000429 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
430 && tid != DRD_INVALID_THREADID);
431 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000432}
433
bart86a87df2009-03-04 19:26:47 +0000434/**
435 * Return the lowest value that was ever assigned to the stack pointer
436 * for the specified thread.
437 */
bart62a784c2009-02-15 13:11:14 +0000438Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000439{
bartbedfd232009-03-26 19:07:15 +0000440 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
441 && tid != DRD_INVALID_THREADID);
442 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000443}
444
bart86a87df2009-03-04 19:26:47 +0000445/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000446Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000447{
bartbedfd232009-03-26 19:07:15 +0000448 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
449 && tid != DRD_INVALID_THREADID);
450 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000451}
452
bart86a87df2009-03-04 19:26:47 +0000453/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000454SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000455{
bartbedfd232009-03-26 19:07:15 +0000456 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
457 && tid != DRD_INVALID_THREADID);
458 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000459}
460
bart383d6132010-09-02 14:43:18 +0000461Bool DRD_(thread_get_on_alt_stack)(const DrdThreadId tid)
462{
463 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
464 && tid != DRD_INVALID_THREADID);
465 return DRD_(g_threadinfo)[tid].on_alt_stack;
466}
467
468void DRD_(thread_set_on_alt_stack)(const DrdThreadId tid,
469 const Bool on_alt_stack)
470{
471 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
472 && tid != DRD_INVALID_THREADID);
473 tl_assert(on_alt_stack == !!on_alt_stack);
474 DRD_(g_threadinfo)[tid].on_alt_stack = on_alt_stack;
475}
476
477Int DRD_(thread_get_threads_on_alt_stack)(void)
478{
479 int i, n = 0;
480
481 for (i = 1; i < DRD_N_THREADS; i++)
482 n += DRD_(g_threadinfo)[i].on_alt_stack;
483 return n;
484}
485
bart09dc13f2009-02-14 15:13:31 +0000486/**
bart6d956dc2011-07-28 09:54:37 +0000487 * Clean up thread-specific data structures.
sewardjaf44c822007-11-25 14:01:38 +0000488 */
bart9194e932011-02-09 11:55:12 +0000489void DRD_(thread_delete)(const DrdThreadId tid, const Bool detached)
sewardjaf44c822007-11-25 14:01:38 +0000490{
bartbedfd232009-03-26 19:07:15 +0000491 Segment* sg;
492 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000493
bartbedfd232009-03-26 19:07:15 +0000494 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart86a87df2009-03-04 19:26:47 +0000495
bartbedfd232009-03-26 19:07:15 +0000496 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
bart91b7ec32012-01-25 20:36:27 +0000497 for (sg = DRD_(g_threadinfo)[tid].sg_last; sg; sg = sg_prev) {
498 sg_prev = sg->thr_prev;
499 sg->thr_next = NULL;
500 sg->thr_prev = NULL;
bartbedfd232009-03-26 19:07:15 +0000501 DRD_(sg_put)(sg);
502 }
bart6d956dc2011-07-28 09:54:37 +0000503 DRD_(g_threadinfo)[tid].valid = False;
bartbedfd232009-03-26 19:07:15 +0000504 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
505 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart9194e932011-02-09 11:55:12 +0000506 if (detached)
507 DRD_(g_threadinfo)[tid].detached_posix_thread = False;
508 else
509 tl_assert(!DRD_(g_threadinfo)[tid].detached_posix_thread);
bart91b7ec32012-01-25 20:36:27 +0000510 DRD_(g_threadinfo)[tid].sg_first = NULL;
511 DRD_(g_threadinfo)[tid].sg_last = NULL;
bart86a87df2009-03-04 19:26:47 +0000512
bart91b7ec32012-01-25 20:36:27 +0000513 tl_assert(!DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000514}
515
bart324a23b2009-02-15 12:14:52 +0000516/**
517 * Called after a thread performed its last memory access and before
518 * thread_delete() is called. Note: thread_delete() is only called for
519 * joinable threads, not for detached threads.
520 */
bart62a784c2009-02-15 13:11:14 +0000521void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000522{
bartbedfd232009-03-26 19:07:15 +0000523 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
524 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000525
bartbedfd232009-03-26 19:07:15 +0000526 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000527
bartbedfd232009-03-26 19:07:15 +0000528 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
529 {
530 /*
531 * Once a detached thread has finished, its stack is deallocated and
532 * should no longer be taken into account when computing the conflict set.
533 */
534 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000535
bartbedfd232009-03-26 19:07:15 +0000536 /*
537 * For a detached thread, calling pthread_exit() invalidates the
538 * POSIX thread ID associated with the detached thread. For joinable
539 * POSIX threads however, the POSIX thread ID remains live after the
540 * pthread_exit() call until pthread_join() is called.
541 */
542 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
543 }
sewardjaf44c822007-11-25 14:01:38 +0000544}
545
bart5c7e6b62011-02-03 17:47:50 +0000546/** Called just after fork() in the child process. */
547void DRD_(drd_thread_atfork_child)(const DrdThreadId tid)
548{
549 unsigned i;
550
551 for (i = 1; i < DRD_N_THREADS; i++)
552 {
553 if (i == tid)
554 continue;
555 if (DRD_(IsValidDrdThreadId(i)))
bart9194e932011-02-09 11:55:12 +0000556 DRD_(thread_delete)(i, True);
bart5c7e6b62011-02-03 17:47:50 +0000557 tl_assert(!DRD_(IsValidDrdThreadId(i)));
bartf7a5b3f2011-12-11 20:34:03 +0000558 }
bart5c7e6b62011-02-03 17:47:50 +0000559}
560
bart9b2974a2008-09-27 12:35:31 +0000561/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000562void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000563{
bartbedfd232009-03-26 19:07:15 +0000564 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
565 && tid != DRD_INVALID_THREADID);
566 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000567
bart0c0cd772011-03-03 19:59:20 +0000568 if (DRD_(thread_get_trace_fork_join)())
bartad994e82011-10-13 18:04:30 +0000569 DRD_(trace_msg)("[%d] drd_thread_pre_cancel %d",
bartb92ff0f2011-10-08 08:29:29 +0000570 DRD_(g_drd_running_tid), tid);
bartaf0691b2008-09-27 12:26:50 +0000571}
572
barte7dff242009-04-23 17:12:39 +0000573/**
574 * Store the POSIX thread ID for the specified thread.
575 *
576 * @note This function can be called two times for the same thread -- see also
577 * the comment block preceding the pthread_create() wrapper in
578 * drd_pthread_intercepts.c.
579 */
bart62a784c2009-02-15 13:11:14 +0000580void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000581{
bartbedfd232009-03-26 19:07:15 +0000582 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
583 && tid != DRD_INVALID_THREADID);
barte7dff242009-04-23 17:12:39 +0000584 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID
585 || DRD_(g_threadinfo)[tid].pt_threadid == ptid);
bartbedfd232009-03-26 19:07:15 +0000586 tl_assert(ptid != INVALID_POSIX_THREADID);
587 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
588 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000589}
590
bart86a87df2009-03-04 19:26:47 +0000591/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000592Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000593{
bartbedfd232009-03-26 19:07:15 +0000594 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
595 && tid != DRD_INVALID_THREADID);
596 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000597}
598
bart86a87df2009-03-04 19:26:47 +0000599/** Store the thread mode: joinable or detached. */
sewardj5db15402012-06-07 09:13:21 +0000600#if defined(VGP_mips32_linux)
601 /* There is a cse related issue in gcc for MIPS. Optimization level
602 has to be lowered, so cse related optimizations are not
603 included.*/
604 __attribute__((optimize("O1")))
605#endif
bart62a784c2009-02-15 13:11:14 +0000606void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000607{
bartbedfd232009-03-26 19:07:15 +0000608 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
609 && tid != DRD_INVALID_THREADID);
610 tl_assert(!! joinable == joinable);
611 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bart8f822af2009-06-08 18:20:42 +0000612
bartbedfd232009-03-26 19:07:15 +0000613 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000614}
615
bartdd75cdf2009-07-24 08:20:10 +0000616/** Tells DRD that the calling thread is about to enter pthread_create(). */
617void DRD_(thread_entering_pthread_create)(const DrdThreadId tid)
618{
619 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
620 && tid != DRD_INVALID_THREADID);
621 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
622 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0);
623
624 DRD_(g_threadinfo)[tid].pthread_create_nesting_level++;
625}
626
627/** Tells DRD that the calling thread has left pthread_create(). */
628void DRD_(thread_left_pthread_create)(const DrdThreadId tid)
629{
630 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
631 && tid != DRD_INVALID_THREADID);
632 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
633 tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0);
634
635 DRD_(g_threadinfo)[tid].pthread_create_nesting_level--;
636}
637
bartd45d9952009-05-31 18:53:54 +0000638/** Obtain the thread number and the user-assigned thread name. */
639const char* DRD_(thread_get_name)(const DrdThreadId tid)
640{
641 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
642 && tid != DRD_INVALID_THREADID);
643
644 return DRD_(g_threadinfo)[tid].name;
645}
646
647/** Set the name of the specified thread. */
648void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name)
649{
650 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
651 && tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000652
bartd45d9952009-05-31 18:53:54 +0000653 if (name == NULL || name[0] == 0)
654 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
655 sizeof(DRD_(g_threadinfo)[tid].name),
656 "Thread %d",
657 tid);
658 else
659 VG_(snprintf)(DRD_(g_threadinfo)[tid].name,
660 sizeof(DRD_(g_threadinfo)[tid].name),
661 "Thread %d (%s)",
662 tid, name);
663 DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0;
664}
665
bart86a87df2009-03-04 19:26:47 +0000666/**
667 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
668 * conflict set.
669 */
bart62a784c2009-02-15 13:11:14 +0000670void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000671{
bartbedfd232009-03-26 19:07:15 +0000672 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000673
bartbedfd232009-03-26 19:07:15 +0000674 if (vg_tid != s_vg_running_tid)
675 {
676 DRD_(thread_set_running_tid)(vg_tid,
677 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
678 }
sewardj8b09d4f2007-12-04 21:27:18 +0000679
bartbedfd232009-03-26 19:07:15 +0000680 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
681 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000682}
683
bart86a87df2009-03-04 19:26:47 +0000684/**
685 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
686 * conflict set.
687 */
bart62a784c2009-02-15 13:11:14 +0000688void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
689 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000690{
bartbedfd232009-03-26 19:07:15 +0000691 tl_assert(vg_tid != VG_INVALID_THREADID);
692 tl_assert(drd_tid != DRD_INVALID_THREADID);
bart31b983d2010-02-21 14:52:59 +0000693
bartbedfd232009-03-26 19:07:15 +0000694 if (vg_tid != s_vg_running_tid)
695 {
696 if (s_trace_context_switches
697 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
698 {
699 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +0000700 "Context switch from thread %d to thread %d;"
sewardj1e29ebc2009-07-15 14:49:17 +0000701 " segments: %llu\n",
bart63c92ea2009-07-19 17:53:56 +0000702 DRD_(g_drd_running_tid), drd_tid,
bartbedfd232009-03-26 19:07:15 +0000703 DRD_(sg_get_segments_alive_count)());
704 }
705 s_vg_running_tid = vg_tid;
706 DRD_(g_drd_running_tid) = drd_tid;
707 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
708 s_context_switch_count++;
709 }
sewardj8b09d4f2007-12-04 21:27:18 +0000710
bartbedfd232009-03-26 19:07:15 +0000711 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
712 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000713}
714
bart86a87df2009-03-04 19:26:47 +0000715/**
716 * Increase the synchronization nesting counter. Must be called before the
717 * client calls a synchronization function.
718 */
bart62a784c2009-02-15 13:11:14 +0000719int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000720{
bartbedfd232009-03-26 19:07:15 +0000721 tl_assert(DRD_(IsValidDrdThreadId)(tid));
722 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000723}
724
bart86a87df2009-03-04 19:26:47 +0000725/**
726 * Decrease the synchronization nesting counter. Must be called after the
727 * client left a synchronization function.
728 */
bart62a784c2009-02-15 13:11:14 +0000729int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000730{
bartbedfd232009-03-26 19:07:15 +0000731 tl_assert(DRD_(IsValidDrdThreadId)(tid));
732 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
733 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000734}
735
bart86a87df2009-03-04 19:26:47 +0000736/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000737int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000738{
bartbedfd232009-03-26 19:07:15 +0000739 tl_assert(DRD_(IsValidDrdThreadId)(tid));
740 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000741}
742
bart1a473c72008-03-13 19:03:38 +0000743/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000744static
bart86a87df2009-03-04 19:26:47 +0000745void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000746{
bartbedfd232009-03-26 19:07:15 +0000747 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
748 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000749
750#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
751 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
752#endif
753
bart91b7ec32012-01-25 20:36:27 +0000754 // add at tail
755 sg->thr_prev = DRD_(g_threadinfo)[tid].sg_last;
756 sg->thr_next = NULL;
757 if (DRD_(g_threadinfo)[tid].sg_last)
758 DRD_(g_threadinfo)[tid].sg_last->thr_next = sg;
759 DRD_(g_threadinfo)[tid].sg_last = sg;
760 if (DRD_(g_threadinfo)[tid].sg_first == NULL)
761 DRD_(g_threadinfo)[tid].sg_first = sg;
bart8f822af2009-06-08 18:20:42 +0000762
763#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
764 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
765#endif
sewardjaf44c822007-11-25 14:01:38 +0000766}
767
bart324a23b2009-02-15 12:14:52 +0000768/**
769 * Remove a segment from the segment list of thread threadid, and free the
770 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000771 */
bart62a784c2009-02-15 13:11:14 +0000772static
bart86a87df2009-03-04 19:26:47 +0000773void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000774{
bartbedfd232009-03-26 19:07:15 +0000775 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
776 && tid != DRD_INVALID_THREADID);
bart8f822af2009-06-08 18:20:42 +0000777
778#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
779 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
780#endif
bart26f73e12008-02-24 18:37:08 +0000781
bart91b7ec32012-01-25 20:36:27 +0000782 if (sg->thr_prev)
783 sg->thr_prev->thr_next = sg->thr_next;
784 if (sg->thr_next)
785 sg->thr_next->thr_prev = sg->thr_prev;
786 if (sg == DRD_(g_threadinfo)[tid].sg_first)
787 DRD_(g_threadinfo)[tid].sg_first = sg->thr_next;
788 if (sg == DRD_(g_threadinfo)[tid].sg_last)
789 DRD_(g_threadinfo)[tid].sg_last = sg->thr_prev;
bartbedfd232009-03-26 19:07:15 +0000790 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000791
bart8f822af2009-06-08 18:20:42 +0000792#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
793 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
794#endif
sewardjaf44c822007-11-25 14:01:38 +0000795}
796
bart86a87df2009-03-04 19:26:47 +0000797/**
798 * Returns a pointer to the vector clock of the most recent segment associated
799 * with thread 'tid'.
800 */
bart62a784c2009-02-15 13:11:14 +0000801VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000802{
bart91b7ec32012-01-25 20:36:27 +0000803 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000804
bartbedfd232009-03-26 19:07:15 +0000805 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
806 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000807 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
808 tl_assert(latest_sg);
809 return &latest_sg->vc;
sewardjaf44c822007-11-25 14:01:38 +0000810}
811
bart324a23b2009-02-15 12:14:52 +0000812/**
813 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000814 */
bart62a784c2009-02-15 13:11:14 +0000815void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000816{
bart91b7ec32012-01-25 20:36:27 +0000817 Segment* latest_sg;
barte278ab52012-01-24 18:28:55 +0000818
bartbedfd232009-03-26 19:07:15 +0000819 tl_assert(sg);
820 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
821 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +0000822 latest_sg = DRD_(g_threadinfo)[tid].sg_last;
823 tl_assert(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000824
bartbedfd232009-03-26 19:07:15 +0000825 DRD_(sg_put)(*sg);
bart91b7ec32012-01-25 20:36:27 +0000826 *sg = DRD_(sg_get)(latest_sg);
barta2b6e1b2008-03-17 18:32:39 +0000827}
828
sewardjaf44c822007-11-25 14:01:38 +0000829/**
830 * Compute the minimum of all latest vector clocks of all threads
831 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000832 *
sewardjaf44c822007-11-25 14:01:38 +0000833 * @param vc pointer to a vectorclock, holds result upon return.
834 */
bart62a784c2009-02-15 13:11:14 +0000835static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000836{
bartbedfd232009-03-26 19:07:15 +0000837 unsigned i;
838 Bool first;
839 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000840
bartbedfd232009-03-26 19:07:15 +0000841 first = True;
bart8f822af2009-06-08 18:20:42 +0000842 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000843 {
bart91b7ec32012-01-25 20:36:27 +0000844 latest_sg = DRD_(g_threadinfo)[i].sg_last;
845 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000846 if (first)
847 DRD_(vc_assign)(vc, &latest_sg->vc);
848 else
849 DRD_(vc_min)(vc, &latest_sg->vc);
850 first = False;
851 }
852 }
sewardjaf44c822007-11-25 14:01:38 +0000853}
854
bart86a87df2009-03-04 19:26:47 +0000855/**
856 * Compute the maximum of all latest vector clocks of all threads.
857 *
858 * @param vc pointer to a vectorclock, holds result upon return.
859 */
bart62a784c2009-02-15 13:11:14 +0000860static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000861{
bartbedfd232009-03-26 19:07:15 +0000862 unsigned i;
863 Bool first;
864 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000865
bartbedfd232009-03-26 19:07:15 +0000866 first = True;
bart8f822af2009-06-08 18:20:42 +0000867 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +0000868 {
bart91b7ec32012-01-25 20:36:27 +0000869 latest_sg = DRD_(g_threadinfo)[i].sg_last;
870 if (latest_sg) {
bartbedfd232009-03-26 19:07:15 +0000871 if (first)
872 DRD_(vc_assign)(vc, &latest_sg->vc);
873 else
874 DRD_(vc_combine)(vc, &latest_sg->vc);
875 first = False;
876 }
877 }
sewardjaf44c822007-11-25 14:01:38 +0000878}
879
880/**
bart5bd9f2d2008-03-03 20:31:58 +0000881 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000882 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000883 * data race.
884 */
bart8f822af2009-06-08 18:20:42 +0000885static void thread_discard_ordered_segments(void)
sewardjaf44c822007-11-25 14:01:38 +0000886{
bartbedfd232009-03-26 19:07:15 +0000887 unsigned i;
888 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000889
bartbedfd232009-03-26 19:07:15 +0000890 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000891
bartbedfd232009-03-26 19:07:15 +0000892 DRD_(vc_init)(&thread_vc_min, 0, 0);
893 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
894 if (DRD_(sg_get_trace)())
895 {
bart8f822af2009-06-08 18:20:42 +0000896 char *vc_min, *vc_max;
bartbedfd232009-03-26 19:07:15 +0000897 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000898
bartbedfd232009-03-26 19:07:15 +0000899 DRD_(vc_init)(&thread_vc_max, 0, 0);
900 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart8f822af2009-06-08 18:20:42 +0000901 vc_min = DRD_(vc_aprint)(&thread_vc_min);
902 vc_max = DRD_(vc_aprint)(&thread_vc_max);
903 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +0000904 "Discarding ordered segments -- min vc is %s, max vc is %s\n",
bart8f822af2009-06-08 18:20:42 +0000905 vc_min, vc_max);
906 VG_(free)(vc_min);
907 VG_(free)(vc_max);
bartbedfd232009-03-26 19:07:15 +0000908 DRD_(vc_cleanup)(&thread_vc_max);
909 }
sewardjaf44c822007-11-25 14:01:38 +0000910
bart91b7ec32012-01-25 20:36:27 +0000911 for (i = 0; i < DRD_N_THREADS; i++) {
bartbedfd232009-03-26 19:07:15 +0000912 Segment* sg;
913 Segment* sg_next;
barte278ab52012-01-24 18:28:55 +0000914
bart91b7ec32012-01-25 20:36:27 +0000915 for (sg = DRD_(g_threadinfo)[i].sg_first;
916 sg && (sg_next = sg->thr_next)
917 && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
918 sg = sg_next)
919 {
bartbedfd232009-03-26 19:07:15 +0000920 thread_discard_segment(i, sg);
921 }
922 }
923 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000924}
925
bart324a23b2009-02-15 12:14:52 +0000926/**
bart8f822af2009-06-08 18:20:42 +0000927 * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper
928 * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if
929 * all segments in the set CS are ordered consistently against both sg1 and
930 * sg2. The set CS is defined as the set of segments that can immediately
931 * precede future segments via inter-thread synchronization operations. In
932 * DRD the set CS consists of the latest segment of each thread combined with
933 * all segments for which the reference count is strictly greater than one.
934 * The code below is an optimized version of the following:
935 *
936 * for (i = 0; i < DRD_N_THREADS; i++)
937 * {
938 * Segment* sg;
939 *
940 * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
941 * {
942 * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1)
943 * {
944 * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc)
945 * != DRD_(vc_lte)(&sg2->vc, &sg->vc)
946 * || DRD_(vc_lte)(&sg->vc, &sg1->vc)
947 * != DRD_(vc_lte)(&sg->vc, &sg2->vc))
948 * {
949 * return False;
950 * }
951 * }
952 * }
953 * }
954 */
955static Bool thread_consistent_segment_ordering(const DrdThreadId tid,
956 Segment* const sg1,
957 Segment* const sg2)
958{
959 unsigned i;
960
bart91b7ec32012-01-25 20:36:27 +0000961 tl_assert(sg1->thr_next);
962 tl_assert(sg2->thr_next);
963 tl_assert(sg1->thr_next == sg2);
bart8f822af2009-06-08 18:20:42 +0000964 tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc));
965
966 for (i = 0; i < DRD_N_THREADS; i++)
967 {
968 Segment* sg;
969
bart91b7ec32012-01-25 20:36:27 +0000970 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
971 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +0000972 if (DRD_(vc_lte)(&sg2->vc, &sg->vc))
973 break;
974 if (DRD_(vc_lte)(&sg1->vc, &sg->vc))
975 return False;
976 }
977 }
bart91b7ec32012-01-25 20:36:27 +0000978 for (sg = DRD_(g_threadinfo)[i].sg_last; sg; sg = sg->thr_prev) {
979 if (!sg->thr_next || DRD_(sg_get_refcnt)(sg) > 1) {
bart8f822af2009-06-08 18:20:42 +0000980 if (DRD_(vc_lte)(&sg->vc, &sg1->vc))
981 break;
982 if (DRD_(vc_lte)(&sg->vc, &sg2->vc))
983 return False;
984 }
985 }
986 }
987 return True;
988}
989
990/**
bart324a23b2009-02-15 12:14:52 +0000991 * Merge all segments that may be merged without triggering false positives
992 * or discarding real data races. For the theoretical background of segment
bart8f822af2009-06-08 18:20:42 +0000993 * merging, see also the following paper: Mark Christiaens, Michiel Ronsse
994 * and Koen De Bosschere. Bounding the number of segment histories during
995 * data race detection. Parallel Computing archive, Volume 28, Issue 9,
996 * pp 1221-1238, September 2002. This paper contains a proof that merging
997 * consecutive segments for which the property equiv(s1,s2) holds can be
998 * merged without reducing the accuracy of datarace detection. Furthermore
999 * it is also proven that the total number of all segments will never grow
1000 * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged
1001 * every time a new segment is created. The property equiv(s1, s2) is defined
1002 * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector
1003 * clocks of segments s and s1 are ordered in the same way as those of segments
1004 * s and s2. The set CS is defined as the set of existing segments s that have
1005 * the potential to conflict with not yet created segments, either because the
1006 * segment s is the latest segment of a thread or because it can become the
1007 * immediate predecessor of a new segment due to a synchronization operation.
barta9c37392008-03-22 09:38:48 +00001008 */
1009static void thread_merge_segments(void)
1010{
bartbedfd232009-03-26 19:07:15 +00001011 unsigned i;
barta9c37392008-03-22 09:38:48 +00001012
bart8f822af2009-06-08 18:20:42 +00001013 s_new_segments_since_last_merge = 0;
1014
1015 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001016 {
1017 Segment* sg;
barta9c37392008-03-22 09:38:48 +00001018
bart8f822af2009-06-08 18:20:42 +00001019#ifdef ENABLE_DRD_CONSISTENCY_CHECKS
1020 tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
1021#endif
barta9c37392008-03-22 09:38:48 +00001022
bart91b7ec32012-01-25 20:36:27 +00001023 for (sg = DRD_(g_threadinfo)[i].sg_first; sg; sg = sg->thr_next) {
1024 if (DRD_(sg_get_refcnt)(sg) == 1 && sg->thr_next) {
1025 Segment* const sg_next = sg->thr_next;
barte278ab52012-01-24 18:28:55 +00001026 if (DRD_(sg_get_refcnt)(sg_next) == 1
bart91b7ec32012-01-25 20:36:27 +00001027 && sg_next->thr_next
barte278ab52012-01-24 18:28:55 +00001028 && 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{
bart8f822af2009-06-08 18:20:42 +00001049 Segment* last_sg;
bartbedfd232009-03-26 19:07:15 +00001050 Segment* new_sg;
bartd66e3a82008-04-06 15:02:17 +00001051
bartbedfd232009-03-26 19:07:15 +00001052 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1053 && tid != DRD_INVALID_THREADID);
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
bart91b7ec32012-01-25 20:36:27 +00001056 last_sg = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001057 new_sg = DRD_(sg_new)(tid, tid);
1058 thread_append_segment(tid, new_sg);
bart8f822af2009-06-08 18:20:42 +00001059 if (tid == DRD_(g_drd_running_tid) && last_sg)
barte5214662009-06-21 11:51:23 +00001060 {
bart8f822af2009-06-08 18:20:42 +00001061 DRD_(thread_update_conflict_set)(tid, &last_sg->vc);
barte5214662009-06-21 11:51:23 +00001062 s_update_conflict_set_new_sg_count++;
1063 }
bartd66e3a82008-04-06 15:02:17 +00001064
bart8f822af2009-06-08 18:20:42 +00001065 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
sewardjaf44c822007-11-25 14:01:38 +00001066
bart8f822af2009-06-08 18:20:42 +00001067 if (s_segment_merging
1068 && ++s_new_segments_since_last_merge >= s_segment_merge_interval)
bartbedfd232009-03-26 19:07:15 +00001069 {
bart8f822af2009-06-08 18:20:42 +00001070 thread_discard_ordered_segments();
bartbedfd232009-03-26 19:07:15 +00001071 thread_merge_segments();
1072 }
sewardjaf44c822007-11-25 14:01:38 +00001073}
1074
bart26f73e12008-02-24 18:37:08 +00001075/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart8f822af2009-06-08 18:20:42 +00001076void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +00001077{
bartbedfd232009-03-26 19:07:15 +00001078 tl_assert(joiner != joinee);
1079 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
1080 && joiner != DRD_INVALID_THREADID);
1081 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
1082 && joinee != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001083 tl_assert(DRD_(g_threadinfo)[joiner].sg_first);
1084 tl_assert(DRD_(g_threadinfo)[joiner].sg_last);
1085 tl_assert(DRD_(g_threadinfo)[joinee].sg_first);
1086 tl_assert(DRD_(g_threadinfo)[joinee].sg_last);
bart8f822af2009-06-08 18:20:42 +00001087
1088 if (DRD_(sg_get_trace)())
1089 {
1090 char *str1, *str2;
bartc6bf1842012-01-22 08:40:42 +00001091 str1 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
1092 str2 = DRD_(vc_aprint)(DRD_(thread_get_vc)(joinee));
sewardj1e29ebc2009-07-15 14:49:17 +00001093 VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n",
bart8f822af2009-06-08 18:20:42 +00001094 str1, str2);
1095 VG_(free)(str1);
1096 VG_(free)(str2);
1097 }
bartae37e6d2012-01-22 08:58:31 +00001098 if (joiner == DRD_(g_drd_running_tid)) {
barte5214662009-06-21 11:51:23 +00001099 VectorClock old_vc;
1100
bartc6bf1842012-01-22 08:40:42 +00001101 DRD_(vc_copy)(&old_vc, DRD_(thread_get_vc)(joiner));
1102 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1103 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001104 DRD_(thread_update_conflict_set)(joiner, &old_vc);
1105 s_update_conflict_set_join_count++;
1106 DRD_(vc_cleanup)(&old_vc);
bartae37e6d2012-01-22 08:58:31 +00001107 } else {
bartc6bf1842012-01-22 08:40:42 +00001108 DRD_(vc_combine)(DRD_(thread_get_vc)(joiner),
1109 DRD_(thread_get_vc)(joinee));
barte5214662009-06-21 11:51:23 +00001110 }
1111
1112 thread_discard_ordered_segments();
1113
bartae37e6d2012-01-22 08:58:31 +00001114 if (DRD_(sg_get_trace)()) {
bart8f822af2009-06-08 18:20:42 +00001115 char* str;
bartae37e6d2012-01-22 08:58:31 +00001116
bartc6bf1842012-01-22 08:40:42 +00001117 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(joiner));
sewardj1e29ebc2009-07-15 14:49:17 +00001118 VG_(message)(Vg_DebugMsg, "After join: %s\n", str);
bart8f822af2009-06-08 18:20:42 +00001119 VG_(free)(str);
1120 }
sewardjaf44c822007-11-25 14:01:38 +00001121}
1122
bart324a23b2009-02-15 12:14:52 +00001123/**
bart8f822af2009-06-08 18:20:42 +00001124 * Update the vector clock of the last segment of thread tid with the
bartf6ec1fe2009-06-21 18:07:35 +00001125 * the vector clock of segment sg.
bart26f73e12008-02-24 18:37:08 +00001126 */
bartf6ec1fe2009-06-21 18:07:35 +00001127static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg)
sewardjaf44c822007-11-25 14:01:38 +00001128{
bart8f822af2009-06-08 18:20:42 +00001129 const VectorClock* const vc = &sg->vc;
1130
bartbedfd232009-03-26 19:07:15 +00001131 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1132 && tid != DRD_INVALID_THREADID);
bart91b7ec32012-01-25 20:36:27 +00001133 tl_assert(DRD_(g_threadinfo)[tid].sg_first);
1134 tl_assert(DRD_(g_threadinfo)[tid].sg_last);
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
bart91b7ec32012-01-25 20:36:27 +00001197 for (p = DRD_(g_sg_list); p; p = p->g_next)
barte278ab52012-01-24 18:28:55 +00001198 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;
1231 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001232
bart8f822af2009-06-08 18:20:42 +00001233 for (i = 0; i < DRD_N_THREADS; i++)
bartbedfd232009-03-26 19:07:15 +00001234 {
bart91b7ec32012-01-25 20:36:27 +00001235 p = DRD_(g_threadinfo)[i].sg_first;
1236 if (p) {
bartbedfd232009-03-26 19:07:15 +00001237 VG_(printf)("**************\n"
bart6d956dc2011-07-28 09:54:37 +00001238 "* thread %3d (%d/%d/%d/%d/0x%lx/%d) *\n"
bartbedfd232009-03-26 19:07:15 +00001239 "**************\n",
1240 i,
bart6d956dc2011-07-28 09:54:37 +00001241 DRD_(g_threadinfo)[i].valid,
bartbedfd232009-03-26 19:07:15 +00001242 DRD_(g_threadinfo)[i].vg_thread_exists,
1243 DRD_(g_threadinfo)[i].vg_threadid,
1244 DRD_(g_threadinfo)[i].posix_thread_exists,
1245 DRD_(g_threadinfo)[i].pt_threadid,
1246 DRD_(g_threadinfo)[i].detached_posix_thread);
bart91b7ec32012-01-25 20:36:27 +00001247 for ( ; p; p = p->thr_next)
bartbedfd232009-03-26 19:07:15 +00001248 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +00001249 }
bartbedfd232009-03-26 19:07:15 +00001250 }
sewardjaf44c822007-11-25 14:01:38 +00001251}
1252
bart86a87df2009-03-04 19:26:47 +00001253/** Show a call stack involved in a data race. */
barte7086002011-10-11 19:08:39 +00001254static void show_call_stack(const DrdThreadId tid, ExeContext* const callstack)
sewardjaf44c822007-11-25 14:01:38 +00001255{
bartbedfd232009-03-26 19:07:15 +00001256 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +00001257
barte7086002011-10-11 19:08:39 +00001258 if (vg_tid != VG_INVALID_THREADID) {
bartbedfd232009-03-26 19:07:15 +00001259 if (callstack)
bartbedfd232009-03-26 19:07:15 +00001260 VG_(pp_ExeContext)(callstack);
bartbedfd232009-03-26 19:07:15 +00001261 else
bartbedfd232009-03-26 19:07:15 +00001262 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
barte7086002011-10-11 19:08:39 +00001263 } else {
1264 if (!VG_(clo_xml))
1265 VG_(message)(Vg_UserMsg,
1266 " (thread finished, call stack no longer available)\n");
bartbedfd232009-03-26 19:07:15 +00001267 }
sewardjaf44c822007-11-25 14:01:38 +00001268}
1269
bart86a87df2009-03-04 19:26:47 +00001270/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +00001271static void
1272thread_report_conflicting_segments_segment(const DrdThreadId tid,
1273 const Addr addr,
1274 const SizeT size,
1275 const BmAccessTypeT access_type,
1276 const Segment* const p)
1277{
bartbedfd232009-03-26 19:07:15 +00001278 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +00001279
bartbedfd232009-03-26 19:07:15 +00001280 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1281 && tid != DRD_INVALID_THREADID);
1282 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +00001283
bart91b7ec32012-01-25 20:36:27 +00001284 for (i = 0; i < DRD_N_THREADS; i++) {
1285 if (i != tid) {
bartbedfd232009-03-26 19:07:15 +00001286 Segment* q;
barte278ab52012-01-24 18:28:55 +00001287
bart91b7ec32012-01-25 20:36:27 +00001288 for (q = DRD_(g_threadinfo)[i].sg_last; q; q = q->thr_prev) {
bartbedfd232009-03-26 19:07:15 +00001289 /*
bart31b983d2010-02-21 14:52:59 +00001290 * Since q iterates over the segments of thread i in order of
1291 * decreasing vector clocks, if q->vc <= p->vc, then
bartbedfd232009-03-26 19:07:15 +00001292 * q->next->vc <= p->vc will also hold. Hence, break out of the
1293 * loop once this condition is met.
1294 */
1295 if (DRD_(vc_lte)(&q->vc, &p->vc))
1296 break;
bart91b7ec32012-01-25 20:36:27 +00001297 if (!DRD_(vc_lte)(&p->vc, &q->vc)) {
bart8f822af2009-06-08 18:20:42 +00001298 if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size,
bart91b7ec32012-01-25 20:36:27 +00001299 access_type)) {
barte278ab52012-01-24 18:28:55 +00001300 Segment* q_next;
1301
bartbedfd232009-03-26 19:07:15 +00001302 tl_assert(q->stacktrace);
barte7086002011-10-11 19:08:39 +00001303 if (VG_(clo_xml))
1304 VG_(printf_xml)(" <other_segment_start>\n");
1305 else
1306 VG_(message)(Vg_UserMsg,
1307 "Other segment start (thread %d)\n", i);
1308 show_call_stack(i, q->stacktrace);
1309 if (VG_(clo_xml))
1310 VG_(printf_xml)(" </other_segment_start>\n"
1311 " <other_segment_end>\n");
1312 else
1313 VG_(message)(Vg_UserMsg,
1314 "Other segment end (thread %d)\n", i);
bart91b7ec32012-01-25 20:36:27 +00001315 q_next = q->thr_next;
barte278ab52012-01-24 18:28:55 +00001316 show_call_stack(i, q_next ? q_next->stacktrace : 0);
barte7086002011-10-11 19:08:39 +00001317 if (VG_(clo_xml))
1318 VG_(printf_xml)(" </other_segment_end>\n");
bartbedfd232009-03-26 19:07:15 +00001319 }
1320 }
1321 }
sewardjaf44c822007-11-25 14:01:38 +00001322 }
bartbedfd232009-03-26 19:07:15 +00001323 }
sewardjaf44c822007-11-25 14:01:38 +00001324}
1325
bart86a87df2009-03-04 19:26:47 +00001326/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +00001327void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
1328 const Addr addr,
1329 const SizeT size,
1330 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +00001331{
bartbedfd232009-03-26 19:07:15 +00001332 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001333
bartbedfd232009-03-26 19:07:15 +00001334 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1335 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001336
bart91b7ec32012-01-25 20:36:27 +00001337 for (p = DRD_(g_threadinfo)[tid].sg_first; p; p = p->thr_next) {
bart8f822af2009-06-08 18:20:42 +00001338 if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type))
bartbedfd232009-03-26 19:07:15 +00001339 thread_report_conflicting_segments_segment(tid, addr, size,
1340 access_type, p);
bartbedfd232009-03-26 19:07:15 +00001341 }
sewardjaf44c822007-11-25 14:01:38 +00001342}
sewardjaf44c822007-11-25 14:01:38 +00001343
bart324a23b2009-02-15 12:14:52 +00001344/**
bart8f822af2009-06-08 18:20:42 +00001345 * Verify whether the conflict set for thread tid is up to date. Only perform
1346 * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set.
1347 */
1348static Bool thread_conflict_set_up_to_date(const DrdThreadId tid)
1349{
1350 static int do_verify_conflict_set = -1;
1351 Bool result;
1352 struct bitmap* computed_conflict_set = 0;
1353
1354 if (do_verify_conflict_set < 0)
1355 do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0;
1356
1357 if (do_verify_conflict_set == 0)
1358 return True;
1359
1360 thread_compute_conflict_set(&computed_conflict_set, tid);
1361 result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set);
1362 if (! result)
1363 {
1364 VG_(printf)("actual conflict set:\n");
1365 DRD_(bm_print)(DRD_(g_conflict_set));
1366 VG_(printf)("\n");
1367 VG_(printf)("computed conflict set:\n");
1368 DRD_(bm_print)(computed_conflict_set);
1369 VG_(printf)("\n");
1370 }
1371 DRD_(bm_delete)(computed_conflict_set);
1372 return result;
1373}
1374
1375/**
1376 * Compute the conflict set: a bitmap that represents the union of all memory
1377 * accesses of all segments that are unordered to the current segment of the
1378 * thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001379 */
bart86a87df2009-03-04 19:26:47 +00001380static void thread_compute_conflict_set(struct bitmap** conflict_set,
1381 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001382{
bartbedfd232009-03-26 19:07:15 +00001383 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001384
bartbedfd232009-03-26 19:07:15 +00001385 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1386 && tid != DRD_INVALID_THREADID);
1387 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001388
bart54803fe2009-06-21 09:26:27 +00001389 s_compute_conflict_set_count++;
bartbedfd232009-03-26 19:07:15 +00001390 s_conflict_set_bitmap_creation_count
1391 -= DRD_(bm_get_bitmap_creation_count)();
1392 s_conflict_set_bitmap2_creation_count
1393 -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001394
bartae37e6d2012-01-22 08:58:31 +00001395 if (*conflict_set) {
bartf6ec1fe2009-06-21 18:07:35 +00001396 DRD_(bm_cleanup)(*conflict_set);
1397 DRD_(bm_init)(*conflict_set);
bartae37e6d2012-01-22 08:58:31 +00001398 } else {
bartf6ec1fe2009-06-21 18:07:35 +00001399 *conflict_set = DRD_(bm_new)();
1400 }
bart26f73e12008-02-24 18:37:08 +00001401
bartae37e6d2012-01-22 08:58:31 +00001402 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001403 char* str;
bart26f73e12008-02-24 18:37:08 +00001404
bartc6bf1842012-01-22 08:40:42 +00001405 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001406 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001407 "computing conflict set for thread %d with vc %s\n",
1408 tid, str);
bart8f822af2009-06-08 18:20:42 +00001409 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001410 }
sewardjaf44c822007-11-25 14:01:38 +00001411
bart91b7ec32012-01-25 20:36:27 +00001412 p = DRD_(g_threadinfo)[tid].sg_last;
bartbedfd232009-03-26 19:07:15 +00001413 {
1414 unsigned j;
1415
bart91b7ec32012-01-25 20:36:27 +00001416 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001417 char* vc;
bartbedfd232009-03-26 19:07:15 +00001418
bart8f822af2009-06-08 18:20:42 +00001419 vc = DRD_(vc_aprint)(&p->vc);
sewardj1e29ebc2009-07-15 14:49:17 +00001420 VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n",
bart8f822af2009-06-08 18:20:42 +00001421 tid, vc);
1422 VG_(free)(vc);
bart26f73e12008-02-24 18:37:08 +00001423 }
sewardjaf44c822007-11-25 14:01:38 +00001424
bart91b7ec32012-01-25 20:36:27 +00001425 for (j = 0; j < DRD_N_THREADS; j++) {
1426 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart8f822af2009-06-08 18:20:42 +00001427 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001428
1429 for (q = DRD_(g_threadinfo)[j].sg_last; q; q = q->thr_prev) {
1430 if (!DRD_(vc_lte)(&q->vc, &p->vc)
1431 && !DRD_(vc_lte)(&p->vc, &q->vc)) {
1432 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001433 char* str;
1434
1435 str = DRD_(vc_aprint)(&q->vc);
1436 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001437 "conflict set: [%d] merging segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001438 j, str);
1439 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001440 }
bart8f822af2009-06-08 18:20:42 +00001441 DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q));
bart91b7ec32012-01-25 20:36:27 +00001442 } else {
1443 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001444 char* str;
1445
1446 str = DRD_(vc_aprint)(&q->vc);
1447 VG_(message)(Vg_DebugMsg,
sewardj1e29ebc2009-07-15 14:49:17 +00001448 "conflict set: [%d] ignoring segment %s\n",
bart8f822af2009-06-08 18:20:42 +00001449 j, str);
1450 VG_(free)(str);
bartbedfd232009-03-26 19:07:15 +00001451 }
1452 }
1453 }
1454 }
1455 }
1456 }
sewardjaf44c822007-11-25 14:01:38 +00001457
bartbedfd232009-03-26 19:07:15 +00001458 s_conflict_set_bitmap_creation_count
1459 += DRD_(bm_get_bitmap_creation_count)();
1460 s_conflict_set_bitmap2_creation_count
1461 += DRD_(bm_get_bitmap2_creation_count)();
1462
bart91b7ec32012-01-25 20:36:27 +00001463 if (s_trace_conflict_set_bm) {
sewardj1e29ebc2009-07-15 14:49:17 +00001464 VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid);
bartbedfd232009-03-26 19:07:15 +00001465 DRD_(bm_print)(*conflict_set);
sewardj1e29ebc2009-07-15 14:49:17 +00001466 VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid);
bartbedfd232009-03-26 19:07:15 +00001467 }
sewardjaf44c822007-11-25 14:01:38 +00001468}
1469
bart8f822af2009-06-08 18:20:42 +00001470/**
1471 * Update the conflict set after the vector clock of thread tid has been
1472 * updated from old_vc to its current value, either because a new segment has
1473 * been created or because of a synchronization operation.
1474 */
1475void DRD_(thread_update_conflict_set)(const DrdThreadId tid,
1476 const VectorClock* const old_vc)
1477{
1478 const VectorClock* new_vc;
1479 Segment* p;
1480 unsigned j;
1481
1482 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1483 && tid != DRD_INVALID_THREADID);
1484 tl_assert(old_vc);
1485 tl_assert(tid == DRD_(g_drd_running_tid));
1486 tl_assert(DRD_(g_conflict_set));
1487
bartae37e6d2012-01-22 08:58:31 +00001488 if (s_trace_conflict_set) {
bart8f822af2009-06-08 18:20:42 +00001489 char* str;
1490
bartc6bf1842012-01-22 08:40:42 +00001491 str = DRD_(vc_aprint)(DRD_(thread_get_vc)(tid));
bart8f822af2009-06-08 18:20:42 +00001492 VG_(message)(Vg_DebugMsg,
bart63c92ea2009-07-19 17:53:56 +00001493 "updating conflict set for thread %d with vc %s\n",
1494 tid, str);
bart8f822af2009-06-08 18:20:42 +00001495 VG_(free)(str);
1496 }
1497
bartc6bf1842012-01-22 08:40:42 +00001498 new_vc = DRD_(thread_get_vc)(tid);
bartf5fe4b62011-07-03 11:39:30 +00001499 tl_assert(DRD_(vc_lte)(old_vc, new_vc));
bart8f822af2009-06-08 18:20:42 +00001500
1501 DRD_(bm_unmark)(DRD_(g_conflict_set));
1502
1503 for (j = 0; j < DRD_N_THREADS; j++)
1504 {
1505 Segment* q;
1506
1507 if (j == tid || ! DRD_(IsValidDrdThreadId)(j))
1508 continue;
1509
bart91b7ec32012-01-25 20:36:27 +00001510 for (q = DRD_(g_threadinfo)[j].sg_last;
1511 q && !DRD_(vc_lte)(&q->vc, new_vc);
1512 q = q->thr_prev) {
1513 const Bool included_in_old_conflict_set
1514 = !DRD_(vc_lte)(old_vc, &q->vc);
1515 const Bool included_in_new_conflict_set
1516 = !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001517
1518 if (UNLIKELY(s_trace_conflict_set)) {
1519 char* str;
1520
1521 str = DRD_(vc_aprint)(&q->vc);
1522 VG_(message)(Vg_DebugMsg,
1523 "conflict set: [%d] %s segment %s\n", j,
1524 included_in_old_conflict_set
1525 != included_in_new_conflict_set
1526 ? "merging" : "ignoring", str);
1527 VG_(free)(str);
1528 }
1529 if (included_in_old_conflict_set != included_in_new_conflict_set)
1530 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
1531 }
1532
bart91b7ec32012-01-25 20:36:27 +00001533 for ( ; q && !DRD_(vc_lte)(&q->vc, old_vc); q = q->thr_prev) {
1534 const Bool included_in_old_conflict_set
1535 = !DRD_(vc_lte)(old_vc, &q->vc);
1536 const Bool included_in_new_conflict_set
1537 = !DRD_(vc_lte)(&q->vc, new_vc)
1538 && !DRD_(vc_lte)(new_vc, &q->vc);
bart178b6862011-07-29 12:30:43 +00001539
1540 if (UNLIKELY(s_trace_conflict_set)) {
bartac5b95b2011-07-03 11:43:45 +00001541 char* str;
1542
1543 str = DRD_(vc_aprint)(&q->vc);
1544 VG_(message)(Vg_DebugMsg,
1545 "conflict set: [%d] %s segment %s\n", j,
1546 included_in_old_conflict_set
1547 != included_in_new_conflict_set
1548 ? "merging" : "ignoring", str);
1549 VG_(free)(str);
1550 }
bart8f822af2009-06-08 18:20:42 +00001551 if (included_in_old_conflict_set != included_in_new_conflict_set)
bart8f822af2009-06-08 18:20:42 +00001552 DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001553 }
1554 }
1555
1556 DRD_(bm_clear_marked)(DRD_(g_conflict_set));
1557
bart91b7ec32012-01-25 20:36:27 +00001558 p = DRD_(g_threadinfo)[tid].sg_last;
1559 for (j = 0; j < DRD_N_THREADS; j++) {
1560 if (j != tid && DRD_(IsValidDrdThreadId)(j)) {
bart4b3fdb22011-07-03 11:40:49 +00001561 Segment* q;
bart91b7ec32012-01-25 20:36:27 +00001562 for (q = DRD_(g_threadinfo)[j].sg_last;
1563 q && !DRD_(vc_lte)(&q->vc, &p->vc);
1564 q = q->thr_prev) {
bart178b6862011-07-29 12:30:43 +00001565 if (!DRD_(vc_lte)(&p->vc, &q->vc))
bart4b3fdb22011-07-03 11:40:49 +00001566 DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q));
bart8f822af2009-06-08 18:20:42 +00001567 }
1568 }
1569 }
1570
1571 DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set));
1572
bart54803fe2009-06-21 09:26:27 +00001573 s_update_conflict_set_count++;
bart8f822af2009-06-08 18:20:42 +00001574
1575 if (s_trace_conflict_set_bm)
1576 {
sewardj1e29ebc2009-07-15 14:49:17 +00001577 VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid);
bart8f822af2009-06-08 18:20:42 +00001578 DRD_(bm_print)(DRD_(g_conflict_set));
sewardj1e29ebc2009-07-15 14:49:17 +00001579 VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid);
bart8f822af2009-06-08 18:20:42 +00001580 }
1581
1582 tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid)));
1583}
1584
bart86a87df2009-03-04 19:26:47 +00001585/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001586ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001587{
bartbedfd232009-03-26 19:07:15 +00001588 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001589}
1590
bart86a87df2009-03-04 19:26:47 +00001591/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001592ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001593{
bartbedfd232009-03-26 19:07:15 +00001594 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001595}
1596
bart54803fe2009-06-21 09:26:27 +00001597/** Return how many times the conflict set has been updated entirely. */
1598ULong DRD_(thread_get_compute_conflict_set_count)()
sewardjaf44c822007-11-25 14:01:38 +00001599{
bart54803fe2009-06-21 09:26:27 +00001600 return s_compute_conflict_set_count;
1601}
1602
1603/** Return how many times the conflict set has been updated partially. */
1604ULong DRD_(thread_get_update_conflict_set_count)(void)
1605{
bartbedfd232009-03-26 19:07:15 +00001606 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001607}
1608
bart86a87df2009-03-04 19:26:47 +00001609/**
barte5214662009-06-21 11:51:23 +00001610 * Return how many times the conflict set has been updated partially
1611 * because a new segment has been created.
1612 */
1613ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void)
1614{
1615 return s_update_conflict_set_new_sg_count;
1616}
1617
1618/**
1619 * Return how many times the conflict set has been updated partially
1620 * because of combining vector clocks due to synchronization operations
1621 * other than reader/writer lock or barrier operations.
1622 */
1623ULong DRD_(thread_get_update_conflict_set_sync_count)(void)
1624{
1625 return s_update_conflict_set_sync_count;
1626}
1627
1628/**
1629 * Return how many times the conflict set has been updated partially
1630 * because of thread joins.
1631 */
1632ULong DRD_(thread_get_update_conflict_set_join_count)(void)
1633{
1634 return s_update_conflict_set_join_count;
1635}
1636
1637/**
bart86a87df2009-03-04 19:26:47 +00001638 * Return the number of first-level bitmaps that have been created during
1639 * conflict set updates.
1640 */
bart62a784c2009-02-15 13:11:14 +00001641ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001642{
bartbedfd232009-03-26 19:07:15 +00001643 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001644}
1645
bart86a87df2009-03-04 19:26:47 +00001646/**
1647 * Return the number of second-level bitmaps that have been created during
1648 * conflict set updates.
1649 */
bart62a784c2009-02-15 13:11:14 +00001650ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001651{
bartbedfd232009-03-26 19:07:15 +00001652 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001653}