blob: 7d5134ac03b5cbd1069898071f5ae242b4279fcb [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
bart86562bd2009-02-16 19:43:56 +00004 Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>.
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
36#include "pub_tool_errormgr.h" // VG_(unique_error)()
37#include "pub_tool_libcassert.h" // tl_assert()
38#include "pub_tool_libcbase.h" // VG_(strlen)()
39#include "pub_tool_libcprint.h" // VG_(printf)()
bart82195c12008-04-13 17:35:08 +000040#include "pub_tool_libcproc.h" // VG_(getenv)()
sewardjaf44c822007-11-25 14:01:38 +000041#include "pub_tool_machine.h"
42#include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)()
sewardj85642922008-01-14 11:54:56 +000043#include "pub_tool_options.h" // VG_(clo_backtrace_size)
sewardjaf44c822007-11-25 14:01:38 +000044#include "pub_tool_threadstate.h" // VG_(get_pthread_id)()
45
bart32ba2082008-06-05 08:53:42 +000046
sewardjaf44c822007-11-25 14:01:38 +000047
bart324a23b2009-02-15 12:14:52 +000048/* Local functions. */
sewardjaf44c822007-11-25 14:01:38 +000049
bart86a87df2009-03-04 19:26:47 +000050static void thread_append_segment(const DrdThreadId tid, Segment* const sg);
51static void thread_discard_segment(const DrdThreadId tid, Segment* const sg);
52static void thread_compute_conflict_set(struct bitmap** conflict_set,
53 const DrdThreadId tid);
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;
60static ULong s_update_conflict_set_count;
61static ULong s_conflict_set_new_segment_count;
62static ULong s_conflict_set_combine_vc_count;
63static ULong s_conflict_set_bitmap_creation_count;
64static ULong s_conflict_set_bitmap2_creation_count;
65static ThreadId s_vg_running_tid = VG_INVALID_THREADID;
bart324a23b2009-02-15 12:14:52 +000066DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID;
67ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS];
68struct bitmap* DRD_(g_conflict_set);
bart86a87df2009-03-04 19:26:47 +000069static Bool s_trace_context_switches = False;
70static Bool s_trace_conflict_set = False;
71static Bool s_trace_fork_join = False;
72static Bool s_segment_merging = True;
sewardjaf44c822007-11-25 14:01:38 +000073
74
bart324a23b2009-02-15 12:14:52 +000075/* Function definitions. */
sewardjaf44c822007-11-25 14:01:38 +000076
bart86a87df2009-03-04 19:26:47 +000077/** Enables/disables context switch tracing. */
bart62a784c2009-02-15 13:11:14 +000078void DRD_(thread_trace_context_switches)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000079{
bart09dc13f2009-02-14 15:13:31 +000080 tl_assert(t == False || t == True);
bart86a87df2009-03-04 19:26:47 +000081 s_trace_context_switches = t;
bart26f73e12008-02-24 18:37:08 +000082}
83
bart86a87df2009-03-04 19:26:47 +000084/** Enables/disables conflict set tracing. */
bart62a784c2009-02-15 13:11:14 +000085void DRD_(thread_trace_conflict_set)(const Bool t)
bart26f73e12008-02-24 18:37:08 +000086{
bart09dc13f2009-02-14 15:13:31 +000087 tl_assert(t == False || t == True);
bart86a87df2009-03-04 19:26:47 +000088 s_trace_conflict_set = t;
bart26f73e12008-02-24 18:37:08 +000089}
90
bart86a87df2009-03-04 19:26:47 +000091/** Report whether fork/join tracing is enabled. */
bart09dc13f2009-02-14 15:13:31 +000092Bool DRD_(thread_get_trace_fork_join)(void)
93{
bart86a87df2009-03-04 19:26:47 +000094 return s_trace_fork_join;
bart09dc13f2009-02-14 15:13:31 +000095}
96
bart86a87df2009-03-04 19:26:47 +000097/** Enables/disables fork/join tracing. */
bart09dc13f2009-02-14 15:13:31 +000098void DRD_(thread_set_trace_fork_join)(const Bool t)
99{
100 tl_assert(t == False || t == True);
bart86a87df2009-03-04 19:26:47 +0000101 s_trace_fork_join = t;
bart09dc13f2009-02-14 15:13:31 +0000102}
103
bart86a87df2009-03-04 19:26:47 +0000104/** Enables/disables segment merging. */
bart62a784c2009-02-15 13:11:14 +0000105void DRD_(thread_set_segment_merging)(const Bool m)
barta9c37392008-03-22 09:38:48 +0000106{
bart09dc13f2009-02-14 15:13:31 +0000107 tl_assert(m == False || m == True);
bart86a87df2009-03-04 19:26:47 +0000108 s_segment_merging = m;
barta9c37392008-03-22 09:38:48 +0000109}
110
sewardjaf44c822007-11-25 14:01:38 +0000111/**
bart86a87df2009-03-04 19:26:47 +0000112 * Convert Valgrind's ThreadId into a DrdThreadId.
113 *
114 * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed
115 * Valgrind ThreadId does not yet exist.
bart324a23b2009-02-15 12:14:52 +0000116 */
bart62a784c2009-02-15 13:11:14 +0000117DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000118{
bart3772a982008-03-15 08:11:03 +0000119 int i;
sewardjaf44c822007-11-25 14:01:38 +0000120
bart3772a982008-03-15 08:11:03 +0000121 if (tid == VG_INVALID_THREADID)
122 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000123
bart3772a982008-03-15 08:11:03 +0000124 for (i = 1; i < DRD_N_THREADS; i++)
125 {
bart324a23b2009-02-15 12:14:52 +0000126 if (DRD_(g_threadinfo)[i].vg_thread_exists == True
127 && DRD_(g_threadinfo)[i].vg_threadid == tid)
bart3772a982008-03-15 08:11:03 +0000128 {
129 return i;
130 }
131 }
sewardjaf44c822007-11-25 14:01:38 +0000132
bart3772a982008-03-15 08:11:03 +0000133 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000134}
135
bart86a87df2009-03-04 19:26:47 +0000136/** Allocate a new DRD thread ID for the specified Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000137static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000138{
bart3772a982008-03-15 08:11:03 +0000139 int i;
sewardjaf44c822007-11-25 14:01:38 +0000140
bart62a784c2009-02-15 13:11:14 +0000141 tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000142
bart3772a982008-03-15 08:11:03 +0000143 for (i = 1; i < DRD_N_THREADS; i++)
144 {
bart324a23b2009-02-15 12:14:52 +0000145 if (DRD_(g_threadinfo)[i].vg_thread_exists == False
146 && DRD_(g_threadinfo)[i].posix_thread_exists == False
147 && DRD_(g_threadinfo)[i].detached_posix_thread == False)
bart3772a982008-03-15 08:11:03 +0000148 {
bart86a87df2009-03-04 19:26:47 +0000149 tl_assert(! DRD_(IsValidDrdThreadId)(i));
150
bart324a23b2009-02-15 12:14:52 +0000151 DRD_(g_threadinfo)[i].vg_thread_exists = True;
152 DRD_(g_threadinfo)[i].vg_threadid = tid;
153 DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID;
154 DRD_(g_threadinfo)[i].stack_min = 0;
155 DRD_(g_threadinfo)[i].stack_min_min = 0;
156 DRD_(g_threadinfo)[i].stack_startup = 0;
157 DRD_(g_threadinfo)[i].stack_max = 0;
158 DRD_(g_threadinfo)[i].is_recording = True;
159 DRD_(g_threadinfo)[i].synchr_nesting = 0;
bart324a23b2009-02-15 12:14:52 +0000160 tl_assert(DRD_(g_threadinfo)[i].first == 0);
161 tl_assert(DRD_(g_threadinfo)[i].last == 0);
bart86a87df2009-03-04 19:26:47 +0000162
163 tl_assert(DRD_(IsValidDrdThreadId)(i));
164
bart3772a982008-03-15 08:11:03 +0000165 return i;
166 }
167 }
sewardjaf44c822007-11-25 14:01:38 +0000168
bart3772a982008-03-15 08:11:03 +0000169 tl_assert(False);
sewardjaf44c822007-11-25 14:01:38 +0000170
bart3772a982008-03-15 08:11:03 +0000171 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000172}
173
bart86a87df2009-03-04 19:26:47 +0000174/** Convert a POSIX thread ID into a DRD thread ID. */
bart62a784c2009-02-15 13:11:14 +0000175DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000176{
bart3772a982008-03-15 08:11:03 +0000177 int i;
sewardjaf44c822007-11-25 14:01:38 +0000178
bart3772a982008-03-15 08:11:03 +0000179 tl_assert(tid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000180
bart3772a982008-03-15 08:11:03 +0000181 for (i = 1; i < DRD_N_THREADS; i++)
182 {
bart324a23b2009-02-15 12:14:52 +0000183 if (DRD_(g_threadinfo)[i].posix_thread_exists
184 && DRD_(g_threadinfo)[i].pt_threadid == tid)
bart3772a982008-03-15 08:11:03 +0000185 {
186 return i;
187 }
188 }
189 return DRD_INVALID_THREADID;
sewardjaf44c822007-11-25 14:01:38 +0000190}
191
bart86a87df2009-03-04 19:26:47 +0000192/** Convert a DRD thread ID into a Valgrind thread ID. */
bart62a784c2009-02-15 13:11:14 +0000193ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000194{
bart74a5f212008-05-11 06:43:07 +0000195 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
196 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000197 return (DRD_(g_threadinfo)[tid].vg_thread_exists
198 ? DRD_(g_threadinfo)[tid].vg_threadid
bart3772a982008-03-15 08:11:03 +0000199 : VG_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000200}
201
bart23d3a4e2008-04-05 12:53:00 +0000202#if 0
bart324a23b2009-02-15 12:14:52 +0000203/**
204 * Sanity check of the doubly linked list of segments referenced by a
205 * ThreadInfo struct.
206 * @return True if sane, False if not.
sewardjaf44c822007-11-25 14:01:38 +0000207 */
bart62a784c2009-02-15 13:11:14 +0000208static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti)
sewardjaf44c822007-11-25 14:01:38 +0000209{
bart3772a982008-03-15 08:11:03 +0000210 Segment* p;
211 for (p = ti->first; p; p = p->next) {
212 if (p->next && p->next->prev != p)
213 return False;
214 if (p->next == 0 && p != ti->last)
215 return False;
216 }
217 for (p = ti->last; p; p = p->prev) {
218 if (p->prev && p->prev->next != p)
219 return False;
220 if (p->prev == 0 && p != ti->first)
221 return False;
222 }
223 return True;
sewardjaf44c822007-11-25 14:01:38 +0000224}
bart23d3a4e2008-04-05 12:53:00 +0000225#endif
sewardjaf44c822007-11-25 14:01:38 +0000226
bart439c55f2009-02-15 10:38:37 +0000227/**
228 * Create the first segment for a newly started thread.
229 *
230 * This function is called from the handler installed via
231 * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler
232 * from the context of the creator thread, before the new thread has been
233 * created.
bart86a87df2009-03-04 19:26:47 +0000234 *
235 * @param[in] creator DRD thread ID of the creator thread.
236 * @param[in] vg_created Valgrind thread ID of the created thread.
237 *
238 * @return DRD thread ID of the created thread.
bart439c55f2009-02-15 10:38:37 +0000239 */
bart62a784c2009-02-15 13:11:14 +0000240DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator,
241 const ThreadId vg_created)
sewardjaf44c822007-11-25 14:01:38 +0000242{
bart3772a982008-03-15 08:11:03 +0000243 DrdThreadId created;
sewardjaf44c822007-11-25 14:01:38 +0000244
bart62a784c2009-02-15 13:11:14 +0000245 tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID);
246 created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created);
bart74a5f212008-05-11 06:43:07 +0000247 tl_assert(0 <= (int)created && created < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000248 && created != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000249
bart324a23b2009-02-15 12:14:52 +0000250 tl_assert(DRD_(g_threadinfo)[created].first == 0);
251 tl_assert(DRD_(g_threadinfo)[created].last == 0);
bart86a87df2009-03-04 19:26:47 +0000252 thread_append_segment(created, DRD_(sg_new)(creator, created));
sewardjaf44c822007-11-25 14:01:38 +0000253
bart3772a982008-03-15 08:11:03 +0000254 return created;
sewardjaf44c822007-11-25 14:01:38 +0000255}
256
bart439c55f2009-02-15 10:38:37 +0000257/**
bart86a87df2009-03-04 19:26:47 +0000258 * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called
259 * after the thread has been created and before any client instructions are run
bart439c55f2009-02-15 10:38:37 +0000260 * on the newly created thread, e.g. from the handler installed via
261 * VG_(track_pre_thread_first_insn)().
bart86a87df2009-03-04 19:26:47 +0000262 *
263 * @param[in] vg_created Valgrind thread ID of the newly created thread.
264 *
265 * @return DRD thread ID for the new thread.
bart439c55f2009-02-15 10:38:37 +0000266 */
bart62a784c2009-02-15 13:11:14 +0000267DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created)
bart439c55f2009-02-15 10:38:37 +0000268{
bart62a784c2009-02-15 13:11:14 +0000269 const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created);
bart439c55f2009-02-15 10:38:37 +0000270
271 tl_assert(0 <= (int)created && created < DRD_N_THREADS
272 && created != DRD_INVALID_THREADID);
273
bart324a23b2009-02-15 12:14:52 +0000274 DRD_(g_threadinfo)[created].stack_max = VG_(thread_get_stack_max)(vg_created);
275 DRD_(g_threadinfo)[created].stack_startup = DRD_(g_threadinfo)[created].stack_max;
276 DRD_(g_threadinfo)[created].stack_min = DRD_(g_threadinfo)[created].stack_max;
277 DRD_(g_threadinfo)[created].stack_min_min = DRD_(g_threadinfo)[created].stack_max;
278 DRD_(g_threadinfo)[created].stack_size = VG_(thread_get_stack_size)(vg_created);
279 tl_assert(DRD_(g_threadinfo)[created].stack_max != 0);
bart439c55f2009-02-15 10:38:37 +0000280
281 return created;
282}
bart09dc13f2009-02-14 15:13:31 +0000283
bart324a23b2009-02-15 12:14:52 +0000284/**
285 * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just
286 * after thread drd_joiner joined thread drd_joinee.
287 */
bart09dc13f2009-02-14 15:13:31 +0000288void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee)
289{
bart62a784c2009-02-15 13:11:14 +0000290 tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner));
291 tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee));
292 DRD_(thread_new_segment)(drd_joinee);
293 DRD_(thread_combine_vc)(drd_joiner, drd_joinee);
294 DRD_(thread_new_segment)(drd_joiner);
bart09dc13f2009-02-14 15:13:31 +0000295
bart86a87df2009-03-04 19:26:47 +0000296 if (s_trace_fork_join)
bart09dc13f2009-02-14 15:13:31 +0000297 {
bart62a784c2009-02-15 13:11:14 +0000298 const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner);
299 const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000300 const unsigned msg_size = 256;
301 char* msg;
302
303 msg = VG_(malloc)("drd.main.dptj.1", msg_size);
304 tl_assert(msg);
305 VG_(snprintf)(msg, msg_size,
306 "drd_post_thread_join joiner = %d/%d, joinee = %d/%d",
307 joiner, drd_joiner, joinee, drd_joinee);
308 if (joiner)
309 {
310 VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
311 ", new vc: ");
bart41b226c2009-02-14 16:55:19 +0000312 DRD_(vc_snprint)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg),
bart62a784c2009-02-15 13:11:14 +0000313 DRD_(thread_get_vc)(drd_joiner));
bart09dc13f2009-02-14 15:13:31 +0000314 }
315 VG_(message)(Vg_DebugMsg, "%s", msg);
316 VG_(free)(msg);
317 }
318
319 if (! DRD_(get_check_stack_accesses)())
320 {
bart62a784c2009-02-15 13:11:14 +0000321 DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee)
322 - DRD_(thread_get_stack_size)(drd_joinee),
323 DRD_(thread_get_stack_max)(drd_joinee));
bart09dc13f2009-02-14 15:13:31 +0000324 }
bartd2c5eae2009-02-21 15:27:04 +0000325 DRD_(clientobj_delete_thread)(drd_joinee);
bart62a784c2009-02-15 13:11:14 +0000326 DRD_(thread_delete)(drd_joinee);
bart09dc13f2009-02-14 15:13:31 +0000327}
328
bart324a23b2009-02-15 12:14:52 +0000329/**
330 * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack,
331 * and accesses this data structure from multiple threads without locking.
332 * Any conflicting accesses in the range stack_startup..stack_max will be
333 * ignored.
334 */
bart62a784c2009-02-15 13:11:14 +0000335void DRD_(thread_set_stack_startup)(const DrdThreadId tid,
336 const Addr stack_startup)
sewardjaf44c822007-11-25 14:01:38 +0000337{
bart74a5f212008-05-11 06:43:07 +0000338 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
339 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000340 tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup);
341 tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max);
342 DRD_(g_threadinfo)[tid].stack_startup = stack_startup;
sewardjaf44c822007-11-25 14:01:38 +0000343}
344
bart86a87df2009-03-04 19:26:47 +0000345/** Return the stack pointer for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000346Addr DRD_(thread_get_stack_min)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000347{
bart74a5f212008-05-11 06:43:07 +0000348 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000349 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000350 return DRD_(g_threadinfo)[tid].stack_min;
sewardjaf44c822007-11-25 14:01:38 +0000351}
352
bart86a87df2009-03-04 19:26:47 +0000353/**
354 * Return the lowest value that was ever assigned to the stack pointer
355 * for the specified thread.
356 */
bart62a784c2009-02-15 13:11:14 +0000357Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000358{
bart74a5f212008-05-11 06:43:07 +0000359 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartcac53462008-03-29 09:27:08 +0000360 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000361 return DRD_(g_threadinfo)[tid].stack_min_min;
bartcac53462008-03-29 09:27:08 +0000362}
363
bart86a87df2009-03-04 19:26:47 +0000364/** Return the top address for the stack of the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000365Addr DRD_(thread_get_stack_max)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000366{
bart74a5f212008-05-11 06:43:07 +0000367 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartd43f8d32008-03-16 17:29:20 +0000368 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000369 return DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000370}
371
bart86a87df2009-03-04 19:26:47 +0000372/** Return the maximum stack size for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000373SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid)
bartcac53462008-03-29 09:27:08 +0000374{
bart74a5f212008-05-11 06:43:07 +0000375 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bartcac53462008-03-29 09:27:08 +0000376 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000377 return DRD_(g_threadinfo)[tid].stack_size;
bartcac53462008-03-29 09:27:08 +0000378}
379
bart09dc13f2009-02-14 15:13:31 +0000380/**
381 * Clean up thread-specific data structures. Call this just after
382 * pthread_join().
sewardjaf44c822007-11-25 14:01:38 +0000383 */
bart62a784c2009-02-15 13:11:14 +0000384void DRD_(thread_delete)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000385{
bart3772a982008-03-15 08:11:03 +0000386 Segment* sg;
387 Segment* sg_prev;
sewardjaf44c822007-11-25 14:01:38 +0000388
bart86a87df2009-03-04 19:26:47 +0000389 tl_assert(DRD_(IsValidDrdThreadId)(tid));
390
bart324a23b2009-02-15 12:14:52 +0000391 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0);
392 for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev)
bart3772a982008-03-15 08:11:03 +0000393 {
394 sg_prev = sg->prev;
barta2b6e1b2008-03-17 18:32:39 +0000395 sg->prev = 0;
396 sg->next = 0;
bart62ada3f2009-02-14 17:19:58 +0000397 DRD_(sg_put)(sg);
bart3772a982008-03-15 08:11:03 +0000398 }
bart324a23b2009-02-15 12:14:52 +0000399 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
400 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
401 tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False);
402 DRD_(g_threadinfo)[tid].first = 0;
403 DRD_(g_threadinfo)[tid].last = 0;
bart86a87df2009-03-04 19:26:47 +0000404
405 tl_assert(! DRD_(IsValidDrdThreadId)(tid));
sewardjaf44c822007-11-25 14:01:38 +0000406}
407
bart324a23b2009-02-15 12:14:52 +0000408/**
409 * Called after a thread performed its last memory access and before
410 * thread_delete() is called. Note: thread_delete() is only called for
411 * joinable threads, not for detached threads.
412 */
bart62a784c2009-02-15 13:11:14 +0000413void DRD_(thread_finished)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000414{
bart74a5f212008-05-11 06:43:07 +0000415 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000416 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000417
bart324a23b2009-02-15 12:14:52 +0000418 DRD_(g_threadinfo)[tid].vg_thread_exists = False;
sewardjaf44c822007-11-25 14:01:38 +0000419
bart324a23b2009-02-15 12:14:52 +0000420 if (DRD_(g_threadinfo)[tid].detached_posix_thread)
bart3772a982008-03-15 08:11:03 +0000421 {
422 /* Once a detached thread has finished, its stack is deallocated and */
barte73b0aa2008-06-28 07:19:56 +0000423 /* should no longer be taken into account when computing the conflict set*/
bart324a23b2009-02-15 12:14:52 +0000424 DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max;
sewardjaf44c822007-11-25 14:01:38 +0000425
bart3772a982008-03-15 08:11:03 +0000426 /* For a detached thread, calling pthread_exit() invalidates the */
427 /* POSIX thread ID associated with the detached thread. For joinable */
428 /* POSIX threads however, the POSIX thread ID remains live after the */
429 /* pthread_exit() call until pthread_join() is called. */
bart324a23b2009-02-15 12:14:52 +0000430 DRD_(g_threadinfo)[tid].posix_thread_exists = False;
bart3772a982008-03-15 08:11:03 +0000431 }
sewardjaf44c822007-11-25 14:01:38 +0000432}
433
bart9b2974a2008-09-27 12:35:31 +0000434/** Called just before pthread_cancel(). */
bart62a784c2009-02-15 13:11:14 +0000435void DRD_(thread_pre_cancel)(const DrdThreadId tid)
bartaf0691b2008-09-27 12:26:50 +0000436{
437 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
438 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000439 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
bartaf0691b2008-09-27 12:26:50 +0000440
bart324a23b2009-02-15 12:14:52 +0000441 DRD_(g_threadinfo)[tid].synchr_nesting = 0;
bartaf0691b2008-09-27 12:26:50 +0000442}
443
bart86a87df2009-03-04 19:26:47 +0000444/** Store the POSIX thread ID for the specified thread. */
bart62a784c2009-02-15 13:11:14 +0000445void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid)
sewardjaf44c822007-11-25 14:01:38 +0000446{
bart74a5f212008-05-11 06:43:07 +0000447 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000448 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000449 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID);
bart3772a982008-03-15 08:11:03 +0000450 tl_assert(ptid != INVALID_POSIX_THREADID);
bart324a23b2009-02-15 12:14:52 +0000451 DRD_(g_threadinfo)[tid].posix_thread_exists = True;
452 DRD_(g_threadinfo)[tid].pt_threadid = ptid;
sewardjaf44c822007-11-25 14:01:38 +0000453}
454
bart86a87df2009-03-04 19:26:47 +0000455/** Returns true for joinable threads and false for detached threads. */
bart62a784c2009-02-15 13:11:14 +0000456Bool DRD_(thread_get_joinable)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000457{
bart74a5f212008-05-11 06:43:07 +0000458 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000459 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000460 return ! DRD_(g_threadinfo)[tid].detached_posix_thread;
sewardjaf44c822007-11-25 14:01:38 +0000461}
462
bart86a87df2009-03-04 19:26:47 +0000463/** Store the thread mode: joinable or detached. */
bart62a784c2009-02-15 13:11:14 +0000464void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable)
sewardjaf44c822007-11-25 14:01:38 +0000465{
bart74a5f212008-05-11 06:43:07 +0000466 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000467 && tid != DRD_INVALID_THREADID);
468 tl_assert(!! joinable == joinable);
bart324a23b2009-02-15 12:14:52 +0000469 tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000470#if 0
bart3772a982008-03-15 08:11:03 +0000471 VG_(message)(Vg_DebugMsg,
472 "thread_set_joinable(%d/%d, %s)",
473 tid,
bart324a23b2009-02-15 12:14:52 +0000474 DRD_(g_threadinfo)[tid].vg_threadid,
bart3772a982008-03-15 08:11:03 +0000475 joinable ? "joinable" : "detached");
sewardjaf44c822007-11-25 14:01:38 +0000476#endif
bart324a23b2009-02-15 12:14:52 +0000477 DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable;
sewardjaf44c822007-11-25 14:01:38 +0000478}
479
bart86a87df2009-03-04 19:26:47 +0000480/**
481 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
482 * conflict set.
483 */
bart62a784c2009-02-15 13:11:14 +0000484void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid)
sewardjaf44c822007-11-25 14:01:38 +0000485{
bart3772a982008-03-15 08:11:03 +0000486 tl_assert(vg_tid != VG_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000487
bart86a87df2009-03-04 19:26:47 +0000488 if (vg_tid != s_vg_running_tid)
bart3772a982008-03-15 08:11:03 +0000489 {
bart62a784c2009-02-15 13:11:14 +0000490 DRD_(thread_set_running_tid)(vg_tid,
491 DRD_(VgThreadIdToDrdThreadId)(vg_tid));
bart3772a982008-03-15 08:11:03 +0000492 }
sewardj8b09d4f2007-12-04 21:27:18 +0000493
bart86a87df2009-03-04 19:26:47 +0000494 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000495 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000496}
497
bart86a87df2009-03-04 19:26:47 +0000498/**
499 * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the
500 * conflict set.
501 */
bart62a784c2009-02-15 13:11:14 +0000502void DRD_(thread_set_running_tid)(const ThreadId vg_tid,
503 const DrdThreadId drd_tid)
sewardj8b09d4f2007-12-04 21:27:18 +0000504{
bart3772a982008-03-15 08:11:03 +0000505 tl_assert(vg_tid != VG_INVALID_THREADID);
506 tl_assert(drd_tid != DRD_INVALID_THREADID);
sewardj8b09d4f2007-12-04 21:27:18 +0000507
bart86a87df2009-03-04 19:26:47 +0000508 if (vg_tid != s_vg_running_tid)
bart3772a982008-03-15 08:11:03 +0000509 {
bart86a87df2009-03-04 19:26:47 +0000510 if (s_trace_context_switches
bart324a23b2009-02-15 12:14:52 +0000511 && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID)
bart3772a982008-03-15 08:11:03 +0000512 {
513 VG_(message)(Vg_DebugMsg,
barta2b6e1b2008-03-17 18:32:39 +0000514 "Context switch from thread %d/%d to thread %d/%d;"
515 " segments: %llu",
bart86a87df2009-03-04 19:26:47 +0000516 s_vg_running_tid, DRD_(g_drd_running_tid),
bart62a784c2009-02-15 13:11:14 +0000517 DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid,
bart62ada3f2009-02-14 17:19:58 +0000518 DRD_(sg_get_segments_alive_count)());
bart3772a982008-03-15 08:11:03 +0000519 }
bart86a87df2009-03-04 19:26:47 +0000520 s_vg_running_tid = vg_tid;
bart324a23b2009-02-15 12:14:52 +0000521 DRD_(g_drd_running_tid) = drd_tid;
bart86a87df2009-03-04 19:26:47 +0000522 thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid);
523 s_context_switch_count++;
bart3772a982008-03-15 08:11:03 +0000524 }
sewardj8b09d4f2007-12-04 21:27:18 +0000525
bart86a87df2009-03-04 19:26:47 +0000526 tl_assert(s_vg_running_tid != VG_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000527 tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000528}
529
bart86a87df2009-03-04 19:26:47 +0000530/**
531 * Increase the synchronization nesting counter. Must be called before the
532 * client calls a synchronization function.
533 */
bart62a784c2009-02-15 13:11:14 +0000534int DRD_(thread_enter_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000535{
bart62a784c2009-02-15 13:11:14 +0000536 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000537 return DRD_(g_threadinfo)[tid].synchr_nesting++;
bart0268dfa2008-03-11 20:10:21 +0000538}
539
bart86a87df2009-03-04 19:26:47 +0000540/**
541 * Decrease the synchronization nesting counter. Must be called after the
542 * client left a synchronization function.
543 */
bart62a784c2009-02-15 13:11:14 +0000544int DRD_(thread_leave_synchr)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000545{
bart62a784c2009-02-15 13:11:14 +0000546 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000547 tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1);
548 return --DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000549}
550
bart86a87df2009-03-04 19:26:47 +0000551/** Returns the synchronization nesting counter. */
bart62a784c2009-02-15 13:11:14 +0000552int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000553{
bart62a784c2009-02-15 13:11:14 +0000554 tl_assert(DRD_(IsValidDrdThreadId)(tid));
bart324a23b2009-02-15 12:14:52 +0000555 return DRD_(g_threadinfo)[tid].synchr_nesting;
bart0268dfa2008-03-11 20:10:21 +0000556}
557
bart1a473c72008-03-13 19:03:38 +0000558/** Append a new segment at the end of the segment list. */
bart62a784c2009-02-15 13:11:14 +0000559static
bart86a87df2009-03-04 19:26:47 +0000560void thread_append_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000561{
bart74a5f212008-05-11 06:43:07 +0000562 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000563 && tid != DRD_INVALID_THREADID);
bart62a784c2009-02-15 13:11:14 +0000564 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
bart324a23b2009-02-15 12:14:52 +0000565 sg->prev = DRD_(g_threadinfo)[tid].last;
bart3772a982008-03-15 08:11:03 +0000566 sg->next = 0;
bart324a23b2009-02-15 12:14:52 +0000567 if (DRD_(g_threadinfo)[tid].last)
568 DRD_(g_threadinfo)[tid].last->next = sg;
569 DRD_(g_threadinfo)[tid].last = sg;
570 if (DRD_(g_threadinfo)[tid].first == 0)
571 DRD_(g_threadinfo)[tid].first = sg;
bart62a784c2009-02-15 13:11:14 +0000572 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000573}
574
bart324a23b2009-02-15 12:14:52 +0000575/**
576 * Remove a segment from the segment list of thread threadid, and free the
577 * associated memory.
sewardjaf44c822007-11-25 14:01:38 +0000578 */
bart62a784c2009-02-15 13:11:14 +0000579static
bart86a87df2009-03-04 19:26:47 +0000580void thread_discard_segment(const DrdThreadId tid, Segment* const sg)
sewardjaf44c822007-11-25 14:01:38 +0000581{
bart74a5f212008-05-11 06:43:07 +0000582 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000583 && tid != DRD_INVALID_THREADID);
bart62a784c2009-02-15 13:11:14 +0000584 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
bart26f73e12008-02-24 18:37:08 +0000585
bart3772a982008-03-15 08:11:03 +0000586 if (sg->prev)
587 sg->prev->next = sg->next;
588 if (sg->next)
589 sg->next->prev = sg->prev;
bart324a23b2009-02-15 12:14:52 +0000590 if (sg == DRD_(g_threadinfo)[tid].first)
591 DRD_(g_threadinfo)[tid].first = sg->next;
592 if (sg == DRD_(g_threadinfo)[tid].last)
593 DRD_(g_threadinfo)[tid].last = sg->prev;
bart62ada3f2009-02-14 17:19:58 +0000594 DRD_(sg_put)(sg);
bart3f749672008-03-22 09:49:40 +0000595
bart62a784c2009-02-15 13:11:14 +0000596 //tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid]));
sewardjaf44c822007-11-25 14:01:38 +0000597}
598
bart86a87df2009-03-04 19:26:47 +0000599/**
600 * Returns a pointer to the vector clock of the most recent segment associated
601 * with thread 'tid'.
602 */
bart62a784c2009-02-15 13:11:14 +0000603VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000604{
bart74a5f212008-05-11 06:43:07 +0000605 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
606 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000607 tl_assert(DRD_(g_threadinfo)[tid].last);
608 return &DRD_(g_threadinfo)[tid].last->vc;
sewardjaf44c822007-11-25 14:01:38 +0000609}
610
bart324a23b2009-02-15 12:14:52 +0000611/**
612 * Return the latest segment of thread 'tid' and increment its reference count.
barta2b6e1b2008-03-17 18:32:39 +0000613 */
bart62a784c2009-02-15 13:11:14 +0000614void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid)
barta2b6e1b2008-03-17 18:32:39 +0000615{
616 tl_assert(sg);
bart74a5f212008-05-11 06:43:07 +0000617 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
618 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000619 tl_assert(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000620
bart62ada3f2009-02-14 17:19:58 +0000621 DRD_(sg_put)(*sg);
bart324a23b2009-02-15 12:14:52 +0000622 *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last);
barta2b6e1b2008-03-17 18:32:39 +0000623}
624
sewardjaf44c822007-11-25 14:01:38 +0000625/**
626 * Compute the minimum of all latest vector clocks of all threads
627 * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA).
bart86a87df2009-03-04 19:26:47 +0000628 *
sewardjaf44c822007-11-25 14:01:38 +0000629 * @param vc pointer to a vectorclock, holds result upon return.
630 */
bart62a784c2009-02-15 13:11:14 +0000631static void DRD_(thread_compute_minimum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000632{
bart3772a982008-03-15 08:11:03 +0000633 unsigned i;
634 Bool first;
635 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000636
bart3772a982008-03-15 08:11:03 +0000637 first = True;
bart62a784c2009-02-15 13:11:14 +0000638 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
639 i++)
bart3772a982008-03-15 08:11:03 +0000640 {
bart324a23b2009-02-15 12:14:52 +0000641 latest_sg = DRD_(g_threadinfo)[i].last;
bart3772a982008-03-15 08:11:03 +0000642 if (latest_sg)
643 {
644 if (first)
bart41b226c2009-02-14 16:55:19 +0000645 DRD_(vc_assign)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000646 else
bart41b226c2009-02-14 16:55:19 +0000647 DRD_(vc_min)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000648 first = False;
649 }
650 }
sewardjaf44c822007-11-25 14:01:38 +0000651}
652
bart86a87df2009-03-04 19:26:47 +0000653/**
654 * Compute the maximum of all latest vector clocks of all threads.
655 *
656 * @param vc pointer to a vectorclock, holds result upon return.
657 */
bart62a784c2009-02-15 13:11:14 +0000658static void DRD_(thread_compute_maximum_vc)(VectorClock* vc)
sewardjaf44c822007-11-25 14:01:38 +0000659{
bart3772a982008-03-15 08:11:03 +0000660 unsigned i;
661 Bool first;
662 Segment* latest_sg;
sewardjaf44c822007-11-25 14:01:38 +0000663
bart3772a982008-03-15 08:11:03 +0000664 first = True;
bart62a784c2009-02-15 13:11:14 +0000665 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
666 i++)
bart3772a982008-03-15 08:11:03 +0000667 {
bart324a23b2009-02-15 12:14:52 +0000668 latest_sg = DRD_(g_threadinfo)[i].last;
bart3772a982008-03-15 08:11:03 +0000669 if (latest_sg)
670 {
671 if (first)
bart41b226c2009-02-14 16:55:19 +0000672 DRD_(vc_assign)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000673 else
bart41b226c2009-02-14 16:55:19 +0000674 DRD_(vc_combine)(vc, &latest_sg->vc);
bart3772a982008-03-15 08:11:03 +0000675 first = False;
676 }
677 }
sewardjaf44c822007-11-25 14:01:38 +0000678}
679
680/**
bart5bd9f2d2008-03-03 20:31:58 +0000681 * Discard all segments that have a defined order against the latest vector
bart86a87df2009-03-04 19:26:47 +0000682 * clock of all threads -- these segments can no longer be involved in a
sewardjaf44c822007-11-25 14:01:38 +0000683 * data race.
684 */
bart62a784c2009-02-15 13:11:14 +0000685static void DRD_(thread_discard_ordered_segments)(void)
sewardjaf44c822007-11-25 14:01:38 +0000686{
bart3772a982008-03-15 08:11:03 +0000687 unsigned i;
688 VectorClock thread_vc_min;
sewardjaf44c822007-11-25 14:01:38 +0000689
bart86a87df2009-03-04 19:26:47 +0000690 s_discard_ordered_segments_count++;
sewardjaf44c822007-11-25 14:01:38 +0000691
bart41b226c2009-02-14 16:55:19 +0000692 DRD_(vc_init)(&thread_vc_min, 0, 0);
bart62a784c2009-02-15 13:11:14 +0000693 DRD_(thread_compute_minimum_vc)(&thread_vc_min);
bart62ada3f2009-02-14 17:19:58 +0000694 if (DRD_(sg_get_trace)())
bart3772a982008-03-15 08:11:03 +0000695 {
696 char msg[256];
697 VectorClock thread_vc_max;
sewardjaf44c822007-11-25 14:01:38 +0000698
bart41b226c2009-02-14 16:55:19 +0000699 DRD_(vc_init)(&thread_vc_max, 0, 0);
bart62a784c2009-02-15 13:11:14 +0000700 DRD_(thread_compute_maximum_vc)(&thread_vc_max);
bart3772a982008-03-15 08:11:03 +0000701 VG_(snprintf)(msg, sizeof(msg),
702 "Discarding ordered segments -- min vc is ");
bart41b226c2009-02-14 16:55:19 +0000703 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
704 &thread_vc_min);
bart3772a982008-03-15 08:11:03 +0000705 VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
706 ", max vc is ");
bart41b226c2009-02-14 16:55:19 +0000707 DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg),
708 &thread_vc_max);
barta2b6e1b2008-03-17 18:32:39 +0000709 VG_(message)(Vg_UserMsg, "%s", msg);
bart41b226c2009-02-14 16:55:19 +0000710 DRD_(vc_cleanup)(&thread_vc_max);
bart3772a982008-03-15 08:11:03 +0000711 }
sewardjaf44c822007-11-25 14:01:38 +0000712
bart62a784c2009-02-15 13:11:14 +0000713 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
714 i++)
bart3772a982008-03-15 08:11:03 +0000715 {
716 Segment* sg;
717 Segment* sg_next;
bart324a23b2009-02-15 12:14:52 +0000718 for (sg = DRD_(g_threadinfo)[i].first;
bart41b226c2009-02-14 16:55:19 +0000719 sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min);
bart3772a982008-03-15 08:11:03 +0000720 sg = sg_next)
721 {
bart86a87df2009-03-04 19:26:47 +0000722 thread_discard_segment(i, sg);
bart3772a982008-03-15 08:11:03 +0000723 }
724 }
bart41b226c2009-02-14 16:55:19 +0000725 DRD_(vc_cleanup)(&thread_vc_min);
sewardjaf44c822007-11-25 14:01:38 +0000726}
727
bart324a23b2009-02-15 12:14:52 +0000728/**
729 * Merge all segments that may be merged without triggering false positives
730 * or discarding real data races. For the theoretical background of segment
731 * merging, see also the following paper:
732 * Mark Christiaens, Michiel Ronsse and Koen De Bosschere.
733 * Bounding the number of segment histories during data race detection.
734 * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238,
735 * September 2002.
barta9c37392008-03-22 09:38:48 +0000736 */
737static void thread_merge_segments(void)
738{
739 unsigned i;
740
bart62a784c2009-02-15 13:11:14 +0000741 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
742 i++)
barta9c37392008-03-22 09:38:48 +0000743 {
744 Segment* sg;
745
bart62a784c2009-02-15 13:11:14 +0000746 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
barta9c37392008-03-22 09:38:48 +0000747
bart324a23b2009-02-15 12:14:52 +0000748 for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next)
barta9c37392008-03-22 09:38:48 +0000749 {
bart62ada3f2009-02-14 17:19:58 +0000750 if (DRD_(sg_get_refcnt)(sg) == 1
barta9c37392008-03-22 09:38:48 +0000751 && sg->next
bart62ada3f2009-02-14 17:19:58 +0000752 && DRD_(sg_get_refcnt)(sg->next) == 1
barta9c37392008-03-22 09:38:48 +0000753 && sg->next->next)
754 {
755 /* Merge sg and sg->next into sg. */
bart62ada3f2009-02-14 17:19:58 +0000756 DRD_(sg_merge)(sg, sg->next);
bart86a87df2009-03-04 19:26:47 +0000757 thread_discard_segment(i, sg->next);
barta9c37392008-03-22 09:38:48 +0000758 }
759 }
760
bart62a784c2009-02-15 13:11:14 +0000761 // tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i]));
barta9c37392008-03-22 09:38:48 +0000762 }
763}
764
bart324a23b2009-02-15 12:14:52 +0000765/**
bart324a23b2009-02-15 12:14:52 +0000766 * Create a new segment for the specified thread, and discard any segments
767 * that cannot cause races anymore.
sewardjaf44c822007-11-25 14:01:38 +0000768 */
bart62a784c2009-02-15 13:11:14 +0000769void DRD_(thread_new_segment)(const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +0000770{
bartd66e3a82008-04-06 15:02:17 +0000771 Segment* new_sg;
772
bart74a5f212008-05-11 06:43:07 +0000773 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
774 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +0000775
bart62ada3f2009-02-14 17:19:58 +0000776 new_sg = DRD_(sg_new)(tid, tid);
bart86a87df2009-03-04 19:26:47 +0000777 thread_append_segment(tid, new_sg);
bartd66e3a82008-04-06 15:02:17 +0000778
bart86a87df2009-03-04 19:26:47 +0000779 thread_compute_conflict_set(&DRD_(g_conflict_set), DRD_(g_drd_running_tid));
780 s_conflict_set_new_segment_count++;
sewardjaf44c822007-11-25 14:01:38 +0000781
bart62a784c2009-02-15 13:11:14 +0000782 DRD_(thread_discard_ordered_segments)();
bart26f73e12008-02-24 18:37:08 +0000783
bart86a87df2009-03-04 19:26:47 +0000784 if (s_segment_merging)
bart324a23b2009-02-15 12:14:52 +0000785 {
barta9c37392008-03-22 09:38:48 +0000786 thread_merge_segments();
bart324a23b2009-02-15 12:14:52 +0000787 }
sewardjaf44c822007-11-25 14:01:38 +0000788}
789
bart26f73e12008-02-24 18:37:08 +0000790/** Call this function after thread 'joiner' joined thread 'joinee'. */
bart62a784c2009-02-15 13:11:14 +0000791void DRD_(thread_combine_vc)(DrdThreadId joiner, DrdThreadId joinee)
sewardjaf44c822007-11-25 14:01:38 +0000792{
bart3772a982008-03-15 08:11:03 +0000793 tl_assert(joiner != joinee);
bart74a5f212008-05-11 06:43:07 +0000794 tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000795 && joiner != DRD_INVALID_THREADID);
bart74a5f212008-05-11 06:43:07 +0000796 tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000797 && joinee != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000798 tl_assert(DRD_(g_threadinfo)[joiner].last);
799 tl_assert(DRD_(g_threadinfo)[joinee].last);
800 DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc,
801 &DRD_(g_threadinfo)[joinee].last->vc);
bart62a784c2009-02-15 13:11:14 +0000802 DRD_(thread_discard_ordered_segments)();
sewardjaf44c822007-11-25 14:01:38 +0000803
bart324a23b2009-02-15 12:14:52 +0000804 if (joiner == DRD_(g_drd_running_tid))
bart3772a982008-03-15 08:11:03 +0000805 {
bart86a87df2009-03-04 19:26:47 +0000806 thread_compute_conflict_set(&DRD_(g_conflict_set), joiner);
bart3772a982008-03-15 08:11:03 +0000807 }
sewardjaf44c822007-11-25 14:01:38 +0000808}
809
bart324a23b2009-02-15 12:14:52 +0000810/**
811 * Call this function after thread 'tid' had to wait because of thread
812 * synchronization until the memory accesses in the segment with vector clock
813 * 'vc' finished.
bart26f73e12008-02-24 18:37:08 +0000814 */
bart62a784c2009-02-15 13:11:14 +0000815void DRD_(thread_combine_vc2)(DrdThreadId tid, const VectorClock* const vc)
sewardjaf44c822007-11-25 14:01:38 +0000816{
bart74a5f212008-05-11 06:43:07 +0000817 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
818 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000819 tl_assert(DRD_(g_threadinfo)[tid].last);
bart3772a982008-03-15 08:11:03 +0000820 tl_assert(vc);
bart324a23b2009-02-15 12:14:52 +0000821 DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc);
bart86a87df2009-03-04 19:26:47 +0000822 thread_compute_conflict_set(&DRD_(g_conflict_set), tid);
bart62a784c2009-02-15 13:11:14 +0000823 DRD_(thread_discard_ordered_segments)();
bart86a87df2009-03-04 19:26:47 +0000824 s_conflict_set_combine_vc_count++;
sewardjaf44c822007-11-25 14:01:38 +0000825}
826
bart324a23b2009-02-15 12:14:52 +0000827/**
828 * Call this function whenever a thread is no longer using the memory
829 * [ a1, a2 [, e.g. because of a call to free() or a stack pointer
830 * increase.
bart26f73e12008-02-24 18:37:08 +0000831 */
bart62a784c2009-02-15 13:11:14 +0000832void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2)
sewardjaf44c822007-11-25 14:01:38 +0000833{
bartd43f8d32008-03-16 17:29:20 +0000834 DrdThreadId other_user;
835 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000836
bart3772a982008-03-15 08:11:03 +0000837 /* For all threads, mark the range [ a1, a2 [ as no longer in use. */
bartd43f8d32008-03-16 17:29:20 +0000838 other_user = DRD_INVALID_THREADID;
bart62a784c2009-02-15 13:11:14 +0000839 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
840 i++)
bart3772a982008-03-15 08:11:03 +0000841 {
842 Segment* p;
bart324a23b2009-02-15 12:14:52 +0000843 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
bart3772a982008-03-15 08:11:03 +0000844 {
845 if (other_user == DRD_INVALID_THREADID
bart324a23b2009-02-15 12:14:52 +0000846 && i != DRD_(g_drd_running_tid))
sewardjaf44c822007-11-25 14:01:38 +0000847 {
bart99edb292009-02-15 15:59:20 +0000848 if (UNLIKELY(DRD_(bm_test_and_clear)(p->bm, a1, a2)))
bart8bf2f8b2008-03-30 17:56:43 +0000849 {
850 other_user = i;
851 }
852 continue;
sewardjaf44c822007-11-25 14:01:38 +0000853 }
bart99edb292009-02-15 15:59:20 +0000854 DRD_(bm_clear)(p->bm, a1, a2);
bart3772a982008-03-15 08:11:03 +0000855 }
856 }
sewardjaf44c822007-11-25 14:01:38 +0000857
bart324a23b2009-02-15 12:14:52 +0000858 /*
859 * If any other thread had accessed memory in [ a1, a2 [, update the
860 * conflict set.
861 */
bart3772a982008-03-15 08:11:03 +0000862 if (other_user != DRD_INVALID_THREADID
bart99edb292009-02-15 15:59:20 +0000863 && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2))
bart3772a982008-03-15 08:11:03 +0000864 {
bart86a87df2009-03-04 19:26:47 +0000865 thread_compute_conflict_set(&DRD_(g_conflict_set),
bart62a784c2009-02-15 13:11:14 +0000866 DRD_(thread_get_running_tid)());
bart3772a982008-03-15 08:11:03 +0000867 }
sewardjaf44c822007-11-25 14:01:38 +0000868}
869
bart86a87df2009-03-04 19:26:47 +0000870/** Start recording memory access information. */
bart62a784c2009-02-15 13:11:14 +0000871void DRD_(thread_start_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000872{
bart74a5f212008-05-11 06:43:07 +0000873 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
874 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000875 tl_assert(! DRD_(g_threadinfo)[tid].is_recording);
876 DRD_(g_threadinfo)[tid].is_recording = True;
bart0268dfa2008-03-11 20:10:21 +0000877}
878
bart86a87df2009-03-04 19:26:47 +0000879/** Stop recording memory access information. */
bart62a784c2009-02-15 13:11:14 +0000880void DRD_(thread_stop_recording)(const DrdThreadId tid)
bart0268dfa2008-03-11 20:10:21 +0000881{
bart74a5f212008-05-11 06:43:07 +0000882 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
883 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +0000884 tl_assert(DRD_(g_threadinfo)[tid].is_recording);
885 DRD_(g_threadinfo)[tid].is_recording = False;
bart0268dfa2008-03-11 20:10:21 +0000886}
887
bart86a87df2009-03-04 19:26:47 +0000888/**
889 * Print the segment information for all threads.
890 *
891 * This function is only used for debugging purposes.
892 */
bart62a784c2009-02-15 13:11:14 +0000893void DRD_(thread_print_all)(void)
sewardjaf44c822007-11-25 14:01:38 +0000894{
bart3772a982008-03-15 08:11:03 +0000895 unsigned i;
896 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +0000897
bart62a784c2009-02-15 13:11:14 +0000898 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
899 i++)
bart3772a982008-03-15 08:11:03 +0000900 {
bart324a23b2009-02-15 12:14:52 +0000901 if (DRD_(g_threadinfo)[i].first)
bart3772a982008-03-15 08:11:03 +0000902 {
903 VG_(printf)("**************\n"
barta2b6e1b2008-03-17 18:32:39 +0000904 "* thread %3d (%d/%d/%d/0x%lx/%d) *\n"
bart3772a982008-03-15 08:11:03 +0000905 "**************\n",
906 i,
bart324a23b2009-02-15 12:14:52 +0000907 DRD_(g_threadinfo)[i].vg_thread_exists,
908 DRD_(g_threadinfo)[i].vg_threadid,
909 DRD_(g_threadinfo)[i].posix_thread_exists,
910 DRD_(g_threadinfo)[i].pt_threadid,
911 DRD_(g_threadinfo)[i].detached_posix_thread);
912 for (p = DRD_(g_threadinfo)[i].first; p; p = p->next)
sewardjaf44c822007-11-25 14:01:38 +0000913 {
bart62ada3f2009-02-14 17:19:58 +0000914 DRD_(sg_print)(p);
sewardjaf44c822007-11-25 14:01:38 +0000915 }
bart3772a982008-03-15 08:11:03 +0000916 }
917 }
sewardjaf44c822007-11-25 14:01:38 +0000918}
919
bart86a87df2009-03-04 19:26:47 +0000920/** Show a call stack involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +0000921static void show_call_stack(const DrdThreadId tid,
922 const Char* const msg,
923 ExeContext* const callstack)
924{
bart62a784c2009-02-15 13:11:14 +0000925 const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid);
sewardjaf44c822007-11-25 14:01:38 +0000926
bartaa97a542008-03-16 17:57:01 +0000927 VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid);
sewardjaf44c822007-11-25 14:01:38 +0000928
bart3772a982008-03-15 08:11:03 +0000929 if (vg_tid != VG_INVALID_THREADID)
930 {
931 if (callstack)
932 {
933 VG_(pp_ExeContext)(callstack);
934 }
935 else
936 {
937 VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size));
938 }
939 }
940 else
941 {
942 VG_(message)(Vg_UserMsg,
943 " (thread finished, call stack no longer available)");
944 }
sewardjaf44c822007-11-25 14:01:38 +0000945}
946
bart86a87df2009-03-04 19:26:47 +0000947/** Print information about the segments involved in a data race. */
sewardjaf44c822007-11-25 14:01:38 +0000948static void
949thread_report_conflicting_segments_segment(const DrdThreadId tid,
950 const Addr addr,
951 const SizeT size,
952 const BmAccessTypeT access_type,
953 const Segment* const p)
954{
bart3772a982008-03-15 08:11:03 +0000955 unsigned i;
sewardjaf44c822007-11-25 14:01:38 +0000956
bart74a5f212008-05-11 06:43:07 +0000957 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +0000958 && tid != DRD_INVALID_THREADID);
959 tl_assert(p);
sewardjaf44c822007-11-25 14:01:38 +0000960
bart62a784c2009-02-15 13:11:14 +0000961 for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]);
962 i++)
bart3772a982008-03-15 08:11:03 +0000963 {
964 if (i != tid)
965 {
966 Segment* q;
bart324a23b2009-02-15 12:14:52 +0000967 for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev)
sewardjaf44c822007-11-25 14:01:38 +0000968 {
bart324a23b2009-02-15 12:14:52 +0000969 /*
970 * Since q iterates over the segments of thread i in order of
971 * decreasing vector clocks, if q->vc <= p->vc, then
972 * q->next->vc <= p->vc will also hold. Hence, break out of the
973 * loop once this condition is met.
974 */
bart41b226c2009-02-14 16:55:19 +0000975 if (DRD_(vc_lte)(&q->vc, &p->vc))
bart3772a982008-03-15 08:11:03 +0000976 break;
bart41b226c2009-02-14 16:55:19 +0000977 if (! DRD_(vc_lte)(&p->vc, &q->vc))
bart3772a982008-03-15 08:11:03 +0000978 {
bart99edb292009-02-15 15:59:20 +0000979 if (DRD_(bm_has_conflict_with)(q->bm, addr, addr + size,
980 access_type))
bart3772a982008-03-15 08:11:03 +0000981 {
982 tl_assert(q->stacktrace);
983 show_call_stack(i, "Other segment start",
984 q->stacktrace);
985 show_call_stack(i, "Other segment end",
986 q->next ? q->next->stacktrace : 0);
987 }
988 }
sewardjaf44c822007-11-25 14:01:38 +0000989 }
bart3772a982008-03-15 08:11:03 +0000990 }
991 }
sewardjaf44c822007-11-25 14:01:38 +0000992}
993
bart86a87df2009-03-04 19:26:47 +0000994/** Print information about all segments involved in a data race. */
bart62a784c2009-02-15 13:11:14 +0000995void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid,
996 const Addr addr,
997 const SizeT size,
998 const BmAccessTypeT access_type)
sewardjaf44c822007-11-25 14:01:38 +0000999{
bart3772a982008-03-15 08:11:03 +00001000 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001001
bart74a5f212008-05-11 06:43:07 +00001002 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
bart3772a982008-03-15 08:11:03 +00001003 && tid != DRD_INVALID_THREADID);
sewardjaf44c822007-11-25 14:01:38 +00001004
bart324a23b2009-02-15 12:14:52 +00001005 for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next)
bart3772a982008-03-15 08:11:03 +00001006 {
bart99edb292009-02-15 15:59:20 +00001007 if (DRD_(bm_has)(p->bm, addr, addr + size, access_type))
bart3772a982008-03-15 08:11:03 +00001008 {
1009 thread_report_conflicting_segments_segment(tid, addr, size,
1010 access_type, p);
1011 }
1012 }
sewardjaf44c822007-11-25 14:01:38 +00001013}
sewardjaf44c822007-11-25 14:01:38 +00001014
bart324a23b2009-02-15 12:14:52 +00001015/**
bart324a23b2009-02-15 12:14:52 +00001016 * Compute a bitmap that represents the union of all memory accesses of all
1017 * segments that are unordered to the current segment of the thread tid.
sewardjaf44c822007-11-25 14:01:38 +00001018 */
bart86a87df2009-03-04 19:26:47 +00001019static void thread_compute_conflict_set(struct bitmap** conflict_set,
1020 const DrdThreadId tid)
sewardjaf44c822007-11-25 14:01:38 +00001021{
bart3772a982008-03-15 08:11:03 +00001022 Segment* p;
sewardjaf44c822007-11-25 14:01:38 +00001023
bart74a5f212008-05-11 06:43:07 +00001024 tl_assert(0 <= (int)tid && tid < DRD_N_THREADS
1025 && tid != DRD_INVALID_THREADID);
bart324a23b2009-02-15 12:14:52 +00001026 tl_assert(tid == DRD_(g_drd_running_tid));
sewardjaf44c822007-11-25 14:01:38 +00001027
bart86a87df2009-03-04 19:26:47 +00001028 s_update_conflict_set_count++;
1029 s_conflict_set_bitmap_creation_count -= DRD_(bm_get_bitmap_creation_count)();
1030 s_conflict_set_bitmap2_creation_count -= DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001031
barte73b0aa2008-06-28 07:19:56 +00001032 if (*conflict_set)
bart3772a982008-03-15 08:11:03 +00001033 {
bart99edb292009-02-15 15:59:20 +00001034 DRD_(bm_delete)(*conflict_set);
bart3772a982008-03-15 08:11:03 +00001035 }
bart99edb292009-02-15 15:59:20 +00001036 *conflict_set = DRD_(bm_new)();
bart26f73e12008-02-24 18:37:08 +00001037
bart86a87df2009-03-04 19:26:47 +00001038 if (s_trace_conflict_set)
bart3772a982008-03-15 08:11:03 +00001039 {
1040 char msg[256];
1041
1042 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001043 "computing conflict set for thread %d/%d with vc ",
bart62a784c2009-02-15 13:11:14 +00001044 DRD_(DrdThreadIdToVgThreadId)(tid), tid);
bart41b226c2009-02-14 16:55:19 +00001045 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1046 sizeof(msg) - VG_(strlen)(msg),
bart324a23b2009-02-15 12:14:52 +00001047 &DRD_(g_threadinfo)[tid].last->vc);
barta2b6e1b2008-03-17 18:32:39 +00001048 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001049 }
1050
bart324a23b2009-02-15 12:14:52 +00001051 p = DRD_(g_threadinfo)[tid].last;
bart3772a982008-03-15 08:11:03 +00001052 {
1053 unsigned j;
1054
bart86a87df2009-03-04 19:26:47 +00001055 if (s_trace_conflict_set)
bart3772a982008-03-15 08:11:03 +00001056 {
bart26f73e12008-02-24 18:37:08 +00001057 char msg[256];
1058
1059 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001060 "conflict set: thread [%d] at vc ",
bart26f73e12008-02-24 18:37:08 +00001061 tid);
bart41b226c2009-02-14 16:55:19 +00001062 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1063 sizeof(msg) - VG_(strlen)(msg),
1064 &p->vc);
barta2b6e1b2008-03-17 18:32:39 +00001065 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001066 }
sewardjaf44c822007-11-25 14:01:38 +00001067
bart324a23b2009-02-15 12:14:52 +00001068 for (j = 0; j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); j++)
bart3772a982008-03-15 08:11:03 +00001069 {
bart62a784c2009-02-15 13:11:14 +00001070 if (j != tid && DRD_(IsValidDrdThreadId)(j))
bart26f73e12008-02-24 18:37:08 +00001071 {
bart3772a982008-03-15 08:11:03 +00001072 const Segment* q;
bart324a23b2009-02-15 12:14:52 +00001073 for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev)
bartd66e3a82008-04-06 15:02:17 +00001074 {
bart41b226c2009-02-14 16:55:19 +00001075 if (! DRD_(vc_lte)(&q->vc, &p->vc) && ! DRD_(vc_lte)(&p->vc, &q->vc))
bart3772a982008-03-15 08:11:03 +00001076 {
bart86a87df2009-03-04 19:26:47 +00001077 if (s_trace_conflict_set)
bart3772a982008-03-15 08:11:03 +00001078 {
1079 char msg[256];
1080 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001081 "conflict set: [%d] merging segment ", j);
bart41b226c2009-02-14 16:55:19 +00001082 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1083 sizeof(msg) - VG_(strlen)(msg),
1084 &q->vc);
barta2b6e1b2008-03-17 18:32:39 +00001085 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001086 }
bart99edb292009-02-15 15:59:20 +00001087 DRD_(bm_merge2)(*conflict_set, q->bm);
bart3772a982008-03-15 08:11:03 +00001088 }
1089 else
1090 {
bart86a87df2009-03-04 19:26:47 +00001091 if (s_trace_conflict_set)
bart3772a982008-03-15 08:11:03 +00001092 {
1093 char msg[256];
1094 VG_(snprintf)(msg, sizeof(msg),
barte73b0aa2008-06-28 07:19:56 +00001095 "conflict set: [%d] ignoring segment ", j);
bart41b226c2009-02-14 16:55:19 +00001096 DRD_(vc_snprint)(msg + VG_(strlen)(msg),
1097 sizeof(msg) - VG_(strlen)(msg),
1098 &q->vc);
barta2b6e1b2008-03-17 18:32:39 +00001099 VG_(message)(Vg_UserMsg, "%s", msg);
bart3772a982008-03-15 08:11:03 +00001100 }
1101 }
bartd66e3a82008-04-06 15:02:17 +00001102 }
bart26f73e12008-02-24 18:37:08 +00001103 }
bart3772a982008-03-15 08:11:03 +00001104 }
bart3772a982008-03-15 08:11:03 +00001105 }
sewardjaf44c822007-11-25 14:01:38 +00001106
bart86a87df2009-03-04 19:26:47 +00001107 s_conflict_set_bitmap_creation_count += DRD_(bm_get_bitmap_creation_count)();
1108 s_conflict_set_bitmap2_creation_count += DRD_(bm_get_bitmap2_creation_count)();
sewardjaf44c822007-11-25 14:01:38 +00001109
bart86a87df2009-03-04 19:26:47 +00001110 if (0 && s_trace_conflict_set)
bart3772a982008-03-15 08:11:03 +00001111 {
barte73b0aa2008-06-28 07:19:56 +00001112 VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid);
bart99edb292009-02-15 15:59:20 +00001113 DRD_(bm_print)(*conflict_set);
barte73b0aa2008-06-28 07:19:56 +00001114 VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid);
bart3772a982008-03-15 08:11:03 +00001115 }
sewardjaf44c822007-11-25 14:01:38 +00001116}
1117
bart86a87df2009-03-04 19:26:47 +00001118/** Report the number of context switches performed. */
bart62a784c2009-02-15 13:11:14 +00001119ULong DRD_(thread_get_context_switch_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001120{
bart86a87df2009-03-04 19:26:47 +00001121 return s_context_switch_count;
sewardjaf44c822007-11-25 14:01:38 +00001122}
1123
bart86a87df2009-03-04 19:26:47 +00001124/** Report the number of ordered segments that have been discarded. */
bart62a784c2009-02-15 13:11:14 +00001125ULong DRD_(thread_get_discard_ordered_segments_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001126{
bart86a87df2009-03-04 19:26:47 +00001127 return s_discard_ordered_segments_count;
sewardjaf44c822007-11-25 14:01:38 +00001128}
1129
bart86a87df2009-03-04 19:26:47 +00001130/** Return how many times the conflict set has been updated. */
bart62a784c2009-02-15 13:11:14 +00001131ULong DRD_(thread_get_update_conflict_set_count)(ULong* dsnsc, ULong* dscvc)
sewardjaf44c822007-11-25 14:01:38 +00001132{
bartd66e3a82008-04-06 15:02:17 +00001133 tl_assert(dsnsc);
1134 tl_assert(dscvc);
bart86a87df2009-03-04 19:26:47 +00001135 *dsnsc = s_conflict_set_new_segment_count;
1136 *dscvc = s_conflict_set_combine_vc_count;
1137 return s_update_conflict_set_count;
sewardjaf44c822007-11-25 14:01:38 +00001138}
1139
bart86a87df2009-03-04 19:26:47 +00001140/**
1141 * Return the number of first-level bitmaps that have been created during
1142 * conflict set updates.
1143 */
bart62a784c2009-02-15 13:11:14 +00001144ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001145{
bart86a87df2009-03-04 19:26:47 +00001146 return s_conflict_set_bitmap_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001147}
1148
bart86a87df2009-03-04 19:26:47 +00001149/**
1150 * Return the number of second-level bitmaps that have been created during
1151 * conflict set updates.
1152 */
bart62a784c2009-02-15 13:11:14 +00001153ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void)
sewardjaf44c822007-11-25 14:01:38 +00001154{
bart86a87df2009-03-04 19:26:47 +00001155 return s_conflict_set_bitmap2_creation_count;
sewardjaf44c822007-11-25 14:01:38 +00001156}