| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1 | /* -*- mode: C; c-basic-offset: 3; -*- */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 2 | /* |
| bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 3 | This file is part of drd, a thread error detector. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 4 | |
| bart | 86562bd | 2009-02-16 19:43:56 +0000 | [diff] [blame] | 5 | Copyright (C) 2006-2009 Bart Van Assche <bart.vanassche@gmail.com>. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_error.h" |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 27 | #include "drd_barrier.h" |
| bart | d2c5eae | 2009-02-21 15:27:04 +0000 | [diff] [blame] | 28 | #include "drd_clientobj.h" |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 29 | #include "drd_cond.h" |
| 30 | #include "drd_mutex.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 31 | #include "drd_segment.h" |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 32 | #include "drd_semaphore.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 33 | #include "drd_suppression.h" |
| 34 | #include "drd_thread.h" |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 35 | #include "pub_tool_vki.h" |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 36 | #include "pub_tool_basics.h" // Addr, SizeT |
| 37 | #include "pub_tool_errormgr.h" // VG_(unique_error)() |
| 38 | #include "pub_tool_libcassert.h" // tl_assert() |
| 39 | #include "pub_tool_libcbase.h" // VG_(strlen)() |
| 40 | #include "pub_tool_libcprint.h" // VG_(printf)() |
| bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 41 | #include "pub_tool_libcproc.h" // VG_(getenv)() |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 42 | #include "pub_tool_machine.h" |
| 43 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
| sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 44 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 45 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 46 | |
| bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame] | 47 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 48 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 49 | /* Local functions. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 50 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 51 | static void thread_append_segment(const DrdThreadId tid, Segment* const sg); |
| 52 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg); |
| 53 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 54 | const DrdThreadId tid); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 55 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
| 57 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 58 | /* Local variables. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 60 | static ULong s_context_switch_count; |
| 61 | static ULong s_discard_ordered_segments_count; |
| bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 62 | static ULong s_compute_conflict_set_count; |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 63 | static ULong s_update_conflict_set_count; |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 64 | static ULong s_update_conflict_set_new_sg_count; |
| 65 | static ULong s_update_conflict_set_sync_count; |
| 66 | static ULong s_update_conflict_set_join_count; |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 67 | static ULong s_conflict_set_bitmap_creation_count; |
| 68 | static ULong s_conflict_set_bitmap2_creation_count; |
| 69 | static ThreadId s_vg_running_tid = VG_INVALID_THREADID; |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 70 | DrdThreadId DRD_(g_drd_running_tid) = DRD_INVALID_THREADID; |
| 71 | ThreadInfo DRD_(g_threadinfo)[DRD_N_THREADS]; |
| 72 | struct bitmap* DRD_(g_conflict_set); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 73 | static Bool s_trace_context_switches = False; |
| 74 | static Bool s_trace_conflict_set = False; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 75 | static Bool s_trace_conflict_set_bm = False; |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 76 | static Bool s_trace_fork_join = False; |
| 77 | static Bool s_segment_merging = True; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 78 | static Bool s_new_segments_since_last_merge; |
| 79 | static int s_segment_merge_interval = 64; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 80 | |
| 81 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 82 | /* Function definitions. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 83 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 84 | /** Enables/disables context switch tracing. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 85 | void DRD_(thread_trace_context_switches)(const Bool t) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 86 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 87 | tl_assert(t == False || t == True); |
| 88 | s_trace_context_switches = t; |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 91 | /** Enables/disables conflict set tracing. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 92 | void DRD_(thread_trace_conflict_set)(const Bool t) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 93 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 94 | tl_assert(t == False || t == True); |
| 95 | s_trace_conflict_set = t; |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 98 | /** Enables/disables conflict set bitmap tracing. */ |
| 99 | void DRD_(thread_trace_conflict_set_bm)(const Bool t) |
| 100 | { |
| 101 | tl_assert(t == False || t == True); |
| 102 | s_trace_conflict_set_bm = t; |
| 103 | } |
| 104 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 105 | /** Report whether fork/join tracing is enabled. */ |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 106 | Bool DRD_(thread_get_trace_fork_join)(void) |
| 107 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 108 | return s_trace_fork_join; |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 111 | /** Enables/disables fork/join tracing. */ |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 112 | void DRD_(thread_set_trace_fork_join)(const Bool t) |
| 113 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 114 | tl_assert(t == False || t == True); |
| 115 | s_trace_fork_join = t; |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 118 | /** Enables/disables segment merging. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 119 | void DRD_(thread_set_segment_merging)(const Bool m) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 120 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 121 | tl_assert(m == False || m == True); |
| 122 | s_segment_merging = m; |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 125 | /** Get the segment merging interval. */ |
| 126 | int DRD_(thread_get_segment_merge_interval)(void) |
| 127 | { |
| 128 | return s_segment_merge_interval; |
| 129 | } |
| 130 | |
| 131 | /** Set the segment merging interval. */ |
| 132 | void DRD_(thread_set_segment_merge_interval)(const int i) |
| 133 | { |
| 134 | s_segment_merge_interval = i; |
| 135 | } |
| 136 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 137 | /** |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 138 | * Convert Valgrind's ThreadId into a DrdThreadId. |
| 139 | * |
| 140 | * @return DRD thread ID upon success and DRD_INVALID_THREADID if the passed |
| 141 | * Valgrind ThreadId does not yet exist. |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 142 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 143 | DrdThreadId DRD_(VgThreadIdToDrdThreadId)(const ThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 144 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 145 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 146 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 147 | if (tid == VG_INVALID_THREADID) |
| 148 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 149 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 150 | for (i = 1; i < DRD_N_THREADS; i++) |
| 151 | { |
| 152 | if (DRD_(g_threadinfo)[i].vg_thread_exists == True |
| 153 | && DRD_(g_threadinfo)[i].vg_threadid == tid) |
| 154 | { |
| 155 | return i; |
| 156 | } |
| 157 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 158 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 159 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 160 | } |
| 161 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 162 | /** Allocate a new DRD thread ID for the specified Valgrind thread ID. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 163 | static DrdThreadId DRD_(VgThreadIdToNewDrdThreadId)(const ThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 164 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 165 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 166 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 167 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(tid) == DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 168 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 169 | for (i = 1; i < DRD_N_THREADS; i++) |
| 170 | { |
| 171 | if (DRD_(g_threadinfo)[i].vg_thread_exists == False |
| 172 | && DRD_(g_threadinfo)[i].posix_thread_exists == False |
| 173 | && DRD_(g_threadinfo)[i].detached_posix_thread == False) |
| 174 | { |
| 175 | tl_assert(! DRD_(IsValidDrdThreadId)(i)); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 176 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 177 | DRD_(g_threadinfo)[i].vg_thread_exists = True; |
| 178 | DRD_(g_threadinfo)[i].vg_threadid = tid; |
| 179 | DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID; |
| 180 | DRD_(g_threadinfo)[i].stack_min = 0; |
| 181 | DRD_(g_threadinfo)[i].stack_min_min = 0; |
| 182 | DRD_(g_threadinfo)[i].stack_startup = 0; |
| 183 | DRD_(g_threadinfo)[i].stack_max = 0; |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 184 | DRD_(thread_set_name)(i, ""); |
| 185 | DRD_(g_threadinfo)[i].is_recording_loads = True; |
| 186 | DRD_(g_threadinfo)[i].is_recording_stores = True; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 187 | DRD_(g_threadinfo)[i].synchr_nesting = 0; |
| 188 | tl_assert(DRD_(g_threadinfo)[i].first == 0); |
| 189 | tl_assert(DRD_(g_threadinfo)[i].last == 0); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 190 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 191 | tl_assert(DRD_(IsValidDrdThreadId)(i)); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 192 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 193 | return i; |
| 194 | } |
| 195 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 196 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 197 | tl_assert(False); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 198 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 199 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 200 | } |
| 201 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 202 | /** Convert a POSIX thread ID into a DRD thread ID. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 203 | DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 204 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 205 | int i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 206 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 207 | tl_assert(tid != INVALID_POSIX_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 208 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 209 | for (i = 1; i < DRD_N_THREADS; i++) |
| 210 | { |
| 211 | if (DRD_(g_threadinfo)[i].posix_thread_exists |
| 212 | && DRD_(g_threadinfo)[i].pt_threadid == tid) |
| 213 | { |
| 214 | return i; |
| 215 | } |
| 216 | } |
| 217 | return DRD_INVALID_THREADID; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 218 | } |
| 219 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 220 | /** Convert a DRD thread ID into a Valgrind thread ID. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 221 | ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 222 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 223 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 224 | && tid != DRD_INVALID_THREADID); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 225 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 226 | return (DRD_(g_threadinfo)[tid].vg_thread_exists |
| 227 | ? DRD_(g_threadinfo)[tid].vg_threadid |
| 228 | : VG_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 231 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 232 | /** |
| 233 | * Sanity check of the doubly linked list of segments referenced by a |
| 234 | * ThreadInfo struct. |
| 235 | * @return True if sane, False if not. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 236 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 237 | static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 238 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 239 | Segment* p; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 240 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 241 | for (p = ti->first; p; p = p->next) { |
| 242 | if (p->next && p->next->prev != p) |
| 243 | return False; |
| 244 | if (p->next == 0 && p != ti->last) |
| 245 | return False; |
| 246 | } |
| 247 | for (p = ti->last; p; p = p->prev) { |
| 248 | if (p->prev && p->prev->next != p) |
| 249 | return False; |
| 250 | if (p->prev == 0 && p != ti->first) |
| 251 | return False; |
| 252 | } |
| 253 | return True; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 254 | } |
| bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 255 | #endif |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 256 | |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 257 | /** |
| 258 | * Create the first segment for a newly started thread. |
| 259 | * |
| 260 | * This function is called from the handler installed via |
| 261 | * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler |
| 262 | * from the context of the creator thread, before the new thread has been |
| 263 | * created. |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 264 | * |
| 265 | * @param[in] creator DRD thread ID of the creator thread. |
| 266 | * @param[in] vg_created Valgrind thread ID of the created thread. |
| 267 | * |
| 268 | * @return DRD thread ID of the created thread. |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 269 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 270 | DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator, |
| 271 | const ThreadId vg_created) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 272 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 273 | DrdThreadId created; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 274 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 275 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID); |
| 276 | created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created); |
| 277 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 278 | && created != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 279 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 280 | tl_assert(DRD_(g_threadinfo)[created].first == 0); |
| 281 | tl_assert(DRD_(g_threadinfo)[created].last == 0); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 282 | /* Create an initial segment for the newly created thread. */ |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 283 | thread_append_segment(created, DRD_(sg_new)(creator, created)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 284 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 285 | return created; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 286 | } |
| 287 | |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 288 | /** |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 289 | * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called |
| 290 | * after the thread has been created and before any client instructions are run |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 291 | * on the newly created thread, e.g. from the handler installed via |
| 292 | * VG_(track_pre_thread_first_insn)(). |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 293 | * |
| 294 | * @param[in] vg_created Valgrind thread ID of the newly created thread. |
| 295 | * |
| 296 | * @return DRD thread ID for the new thread. |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 297 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 298 | DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created) |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 299 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 300 | const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created); |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 301 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 302 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 303 | && created != DRD_INVALID_THREADID); |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 304 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 305 | DRD_(g_threadinfo)[created].stack_max |
| 306 | = VG_(thread_get_stack_max)(vg_created); |
| 307 | DRD_(g_threadinfo)[created].stack_startup |
| 308 | = DRD_(g_threadinfo)[created].stack_max; |
| 309 | DRD_(g_threadinfo)[created].stack_min |
| 310 | = DRD_(g_threadinfo)[created].stack_max; |
| 311 | DRD_(g_threadinfo)[created].stack_min_min |
| 312 | = DRD_(g_threadinfo)[created].stack_max; |
| 313 | DRD_(g_threadinfo)[created].stack_size |
| 314 | = VG_(thread_get_stack_size)(vg_created); |
| 315 | tl_assert(DRD_(g_threadinfo)[created].stack_max != 0); |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 316 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 317 | return created; |
| bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 318 | } |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 319 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 320 | /** |
| 321 | * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just |
| 322 | * after thread drd_joiner joined thread drd_joinee. |
| 323 | */ |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 324 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 325 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 326 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner)); |
| 327 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee)); |
| bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 328 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 329 | DRD_(thread_new_segment)(drd_joiner); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 330 | DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee); |
| bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 331 | DRD_(thread_new_segment)(drd_joinee); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 332 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 333 | if (s_trace_fork_join) |
| 334 | { |
| 335 | const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner); |
| 336 | const ThreadId joinee = DRD_(DrdThreadIdToVgThreadId)(drd_joinee); |
| 337 | const unsigned msg_size = 256; |
| 338 | char* msg; |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 339 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 340 | msg = VG_(malloc)("drd.main.dptj.1", msg_size); |
| 341 | tl_assert(msg); |
| 342 | VG_(snprintf)(msg, msg_size, |
| 343 | "drd_post_thread_join joiner = %d/%d, joinee = %d/%d", |
| 344 | joiner, drd_joiner, joinee, drd_joinee); |
| 345 | if (joiner) |
| 346 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 347 | char* vc; |
| 348 | |
| 349 | vc = DRD_(vc_aprint)(DRD_(thread_get_vc)(drd_joiner)); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 350 | VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 351 | ", new vc: %s", vc); |
| 352 | VG_(free)(vc); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 353 | } |
| 354 | VG_(message)(Vg_DebugMsg, "%s", msg); |
| 355 | VG_(free)(msg); |
| 356 | } |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 357 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 358 | if (! DRD_(get_check_stack_accesses)()) |
| 359 | { |
| 360 | DRD_(finish_suppression)(DRD_(thread_get_stack_max)(drd_joinee) |
| 361 | - DRD_(thread_get_stack_size)(drd_joinee), |
| 362 | DRD_(thread_get_stack_max)(drd_joinee)); |
| 363 | } |
| 364 | DRD_(clientobj_delete_thread)(drd_joinee); |
| 365 | DRD_(thread_delete)(drd_joinee); |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 366 | } |
| 367 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 368 | /** |
| 369 | * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, |
| 370 | * and accesses this data structure from multiple threads without locking. |
| 371 | * Any conflicting accesses in the range stack_startup..stack_max will be |
| 372 | * ignored. |
| 373 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 374 | void DRD_(thread_set_stack_startup)(const DrdThreadId tid, |
| 375 | const Addr stack_startup) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 376 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 377 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 378 | && tid != DRD_INVALID_THREADID); |
| 379 | tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup); |
| 380 | tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max); |
| 381 | DRD_(g_threadinfo)[tid].stack_startup = stack_startup; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 382 | } |
| 383 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 384 | /** Return the stack pointer for the specified thread. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 385 | Addr DRD_(thread_get_stack_min)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 386 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 387 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 388 | && tid != DRD_INVALID_THREADID); |
| 389 | return DRD_(g_threadinfo)[tid].stack_min; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 390 | } |
| 391 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 392 | /** |
| 393 | * Return the lowest value that was ever assigned to the stack pointer |
| 394 | * for the specified thread. |
| 395 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 396 | Addr DRD_(thread_get_stack_min_min)(const DrdThreadId tid) |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 397 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 398 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 399 | && tid != DRD_INVALID_THREADID); |
| 400 | return DRD_(g_threadinfo)[tid].stack_min_min; |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 401 | } |
| 402 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 403 | /** Return the top address for the stack of the specified thread. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 404 | Addr DRD_(thread_get_stack_max)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 405 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 406 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 407 | && tid != DRD_INVALID_THREADID); |
| 408 | return DRD_(g_threadinfo)[tid].stack_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 409 | } |
| 410 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 411 | /** Return the maximum stack size for the specified thread. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 412 | SizeT DRD_(thread_get_stack_size)(const DrdThreadId tid) |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 413 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 414 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 415 | && tid != DRD_INVALID_THREADID); |
| 416 | return DRD_(g_threadinfo)[tid].stack_size; |
| bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 417 | } |
| 418 | |
| bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 419 | /** |
| 420 | * Clean up thread-specific data structures. Call this just after |
| 421 | * pthread_join(). |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 422 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 423 | void DRD_(thread_delete)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 424 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 425 | Segment* sg; |
| 426 | Segment* sg_prev; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 427 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 428 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 429 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 430 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0); |
| 431 | for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev) |
| 432 | { |
| 433 | sg_prev = sg->prev; |
| 434 | sg->prev = 0; |
| 435 | sg->next = 0; |
| 436 | DRD_(sg_put)(sg); |
| 437 | } |
| 438 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| 439 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 440 | tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False); |
| 441 | DRD_(g_threadinfo)[tid].first = 0; |
| 442 | DRD_(g_threadinfo)[tid].last = 0; |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 443 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 444 | tl_assert(! DRD_(IsValidDrdThreadId)(tid)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 447 | /** |
| 448 | * Called after a thread performed its last memory access and before |
| 449 | * thread_delete() is called. Note: thread_delete() is only called for |
| 450 | * joinable threads, not for detached threads. |
| 451 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 452 | void DRD_(thread_finished)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 453 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 454 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 455 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 456 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 457 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 458 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 459 | if (DRD_(g_threadinfo)[tid].detached_posix_thread) |
| 460 | { |
| 461 | /* |
| 462 | * Once a detached thread has finished, its stack is deallocated and |
| 463 | * should no longer be taken into account when computing the conflict set. |
| 464 | */ |
| 465 | DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 466 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 467 | /* |
| 468 | * For a detached thread, calling pthread_exit() invalidates the |
| 469 | * POSIX thread ID associated with the detached thread. For joinable |
| 470 | * POSIX threads however, the POSIX thread ID remains live after the |
| 471 | * pthread_exit() call until pthread_join() is called. |
| 472 | */ |
| 473 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 474 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 475 | } |
| 476 | |
| bart | 9b2974a | 2008-09-27 12:35:31 +0000 | [diff] [blame] | 477 | /** Called just before pthread_cancel(). */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 478 | void DRD_(thread_pre_cancel)(const DrdThreadId tid) |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 479 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 480 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 481 | && tid != DRD_INVALID_THREADID); |
| 482 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 483 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 484 | DRD_(g_threadinfo)[tid].synchr_nesting = 0; |
| bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| bart | e7dff24 | 2009-04-23 17:12:39 +0000 | [diff] [blame] | 487 | /** |
| 488 | * Store the POSIX thread ID for the specified thread. |
| 489 | * |
| 490 | * @note This function can be called two times for the same thread -- see also |
| 491 | * the comment block preceding the pthread_create() wrapper in |
| 492 | * drd_pthread_intercepts.c. |
| 493 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 494 | void DRD_(thread_set_pthreadid)(const DrdThreadId tid, const PThreadId ptid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 495 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 496 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 497 | && tid != DRD_INVALID_THREADID); |
| bart | e7dff24 | 2009-04-23 17:12:39 +0000 | [diff] [blame] | 498 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID |
| 499 | || DRD_(g_threadinfo)[tid].pt_threadid == ptid); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 500 | tl_assert(ptid != INVALID_POSIX_THREADID); |
| 501 | DRD_(g_threadinfo)[tid].posix_thread_exists = True; |
| 502 | DRD_(g_threadinfo)[tid].pt_threadid = ptid; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 503 | } |
| 504 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 505 | /** Returns true for joinable threads and false for detached threads. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 506 | Bool DRD_(thread_get_joinable)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 507 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 508 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 509 | && tid != DRD_INVALID_THREADID); |
| 510 | return ! DRD_(g_threadinfo)[tid].detached_posix_thread; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 511 | } |
| 512 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 513 | /** Store the thread mode: joinable or detached. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 514 | void DRD_(thread_set_joinable)(const DrdThreadId tid, const Bool joinable) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 515 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 516 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 517 | && tid != DRD_INVALID_THREADID); |
| 518 | tl_assert(!! joinable == joinable); |
| 519 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 520 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 521 | DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 522 | } |
| 523 | |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 524 | /** Obtain the thread number and the user-assigned thread name. */ |
| 525 | const char* DRD_(thread_get_name)(const DrdThreadId tid) |
| 526 | { |
| 527 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 528 | && tid != DRD_INVALID_THREADID); |
| 529 | |
| 530 | return DRD_(g_threadinfo)[tid].name; |
| 531 | } |
| 532 | |
| 533 | /** Set the name of the specified thread. */ |
| 534 | void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name) |
| 535 | { |
| 536 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 537 | && tid != DRD_INVALID_THREADID); |
| 538 | |
| 539 | if (name == NULL || name[0] == 0) |
| 540 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 541 | sizeof(DRD_(g_threadinfo)[tid].name), |
| 542 | "Thread %d", |
| 543 | tid); |
| 544 | else |
| 545 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 546 | sizeof(DRD_(g_threadinfo)[tid].name), |
| 547 | "Thread %d (%s)", |
| 548 | tid, name); |
| 549 | DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0; |
| 550 | } |
| 551 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 552 | /** |
| 553 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 554 | * conflict set. |
| 555 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 556 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 557 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 558 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 559 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 560 | if (vg_tid != s_vg_running_tid) |
| 561 | { |
| 562 | DRD_(thread_set_running_tid)(vg_tid, |
| 563 | DRD_(VgThreadIdToDrdThreadId)(vg_tid)); |
| 564 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 565 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 566 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 567 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 568 | } |
| 569 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 570 | /** |
| 571 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 572 | * conflict set. |
| 573 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 574 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
| 575 | const DrdThreadId drd_tid) |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 576 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 577 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 578 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 579 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 580 | if (vg_tid != s_vg_running_tid) |
| 581 | { |
| 582 | if (s_trace_context_switches |
| 583 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
| 584 | { |
| 585 | VG_(message)(Vg_DebugMsg, |
| 586 | "Context switch from thread %d/%d to thread %d/%d;" |
| 587 | " segments: %llu", |
| 588 | s_vg_running_tid, DRD_(g_drd_running_tid), |
| 589 | DRD_(DrdThreadIdToVgThreadId)(drd_tid), drd_tid, |
| 590 | DRD_(sg_get_segments_alive_count)()); |
| 591 | } |
| 592 | s_vg_running_tid = vg_tid; |
| 593 | DRD_(g_drd_running_tid) = drd_tid; |
| 594 | thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid); |
| 595 | s_context_switch_count++; |
| 596 | } |
| sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 597 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 598 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 599 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 600 | } |
| 601 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 602 | /** |
| 603 | * Increase the synchronization nesting counter. Must be called before the |
| 604 | * client calls a synchronization function. |
| 605 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 606 | int DRD_(thread_enter_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 607 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 608 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 609 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 610 | } |
| 611 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 612 | /** |
| 613 | * Decrease the synchronization nesting counter. Must be called after the |
| 614 | * client left a synchronization function. |
| 615 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 616 | int DRD_(thread_leave_synchr)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 617 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 618 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 619 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 620 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 621 | } |
| 622 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 623 | /** Returns the synchronization nesting counter. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 624 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 625 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 626 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 627 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 628 | } |
| 629 | |
| bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 630 | /** Append a new segment at the end of the segment list. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 631 | static |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 632 | void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 633 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 634 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 635 | && tid != DRD_INVALID_THREADID); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 636 | |
| 637 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 638 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 639 | #endif |
| 640 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 641 | sg->prev = DRD_(g_threadinfo)[tid].last; |
| 642 | sg->next = 0; |
| 643 | if (DRD_(g_threadinfo)[tid].last) |
| 644 | DRD_(g_threadinfo)[tid].last->next = sg; |
| 645 | DRD_(g_threadinfo)[tid].last = sg; |
| 646 | if (DRD_(g_threadinfo)[tid].first == 0) |
| 647 | DRD_(g_threadinfo)[tid].first = sg; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 648 | |
| 649 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 650 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 651 | #endif |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 652 | } |
| 653 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 654 | /** |
| 655 | * Remove a segment from the segment list of thread threadid, and free the |
| 656 | * associated memory. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 657 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 658 | static |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 659 | void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 660 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 661 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 662 | && tid != DRD_INVALID_THREADID); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 663 | |
| 664 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 665 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 666 | #endif |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 667 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 668 | if (sg->prev) |
| 669 | sg->prev->next = sg->next; |
| 670 | if (sg->next) |
| 671 | sg->next->prev = sg->prev; |
| 672 | if (sg == DRD_(g_threadinfo)[tid].first) |
| 673 | DRD_(g_threadinfo)[tid].first = sg->next; |
| 674 | if (sg == DRD_(g_threadinfo)[tid].last) |
| 675 | DRD_(g_threadinfo)[tid].last = sg->prev; |
| 676 | DRD_(sg_put)(sg); |
| bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 677 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 678 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 679 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 680 | #endif |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 681 | } |
| 682 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 683 | /** |
| 684 | * Returns a pointer to the vector clock of the most recent segment associated |
| 685 | * with thread 'tid'. |
| 686 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 687 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 688 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 689 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 690 | && tid != DRD_INVALID_THREADID); |
| 691 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| 692 | return &DRD_(g_threadinfo)[tid].last->vc; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 695 | /** |
| 696 | * Return the latest segment of thread 'tid' and increment its reference count. |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 697 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 698 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid) |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 699 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 700 | tl_assert(sg); |
| 701 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 702 | && tid != DRD_INVALID_THREADID); |
| 703 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 704 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 705 | DRD_(sg_put)(*sg); |
| 706 | *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last); |
| bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 707 | } |
| 708 | |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 709 | /** |
| 710 | * Compute the minimum of all latest vector clocks of all threads |
| 711 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 712 | * |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 713 | * @param vc pointer to a vectorclock, holds result upon return. |
| 714 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 715 | static void DRD_(thread_compute_minimum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 716 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 717 | unsigned i; |
| 718 | Bool first; |
| 719 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 720 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 721 | first = True; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 722 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 723 | { |
| 724 | latest_sg = DRD_(g_threadinfo)[i].last; |
| 725 | if (latest_sg) |
| 726 | { |
| 727 | if (first) |
| 728 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 729 | else |
| 730 | DRD_(vc_min)(vc, &latest_sg->vc); |
| 731 | first = False; |
| 732 | } |
| 733 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 734 | } |
| 735 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 736 | /** |
| 737 | * Compute the maximum of all latest vector clocks of all threads. |
| 738 | * |
| 739 | * @param vc pointer to a vectorclock, holds result upon return. |
| 740 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 741 | static void DRD_(thread_compute_maximum_vc)(VectorClock* vc) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 742 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 743 | unsigned i; |
| 744 | Bool first; |
| 745 | Segment* latest_sg; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 746 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 747 | first = True; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 748 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 749 | { |
| 750 | latest_sg = DRD_(g_threadinfo)[i].last; |
| 751 | if (latest_sg) |
| 752 | { |
| 753 | if (first) |
| 754 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 755 | else |
| 756 | DRD_(vc_combine)(vc, &latest_sg->vc); |
| 757 | first = False; |
| 758 | } |
| 759 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | /** |
| bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 763 | * Discard all segments that have a defined order against the latest vector |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 764 | * clock of all threads -- these segments can no longer be involved in a |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 765 | * data race. |
| 766 | */ |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 767 | static void thread_discard_ordered_segments(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 768 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 769 | unsigned i; |
| 770 | VectorClock thread_vc_min; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 771 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 772 | s_discard_ordered_segments_count++; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 773 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 774 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
| 775 | DRD_(thread_compute_minimum_vc)(&thread_vc_min); |
| 776 | if (DRD_(sg_get_trace)()) |
| 777 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 778 | char *vc_min, *vc_max; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 779 | VectorClock thread_vc_max; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 780 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 781 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
| 782 | DRD_(thread_compute_maximum_vc)(&thread_vc_max); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 783 | vc_min = DRD_(vc_aprint)(&thread_vc_min); |
| 784 | vc_max = DRD_(vc_aprint)(&thread_vc_max); |
| 785 | VG_(message)(Vg_DebugMsg, |
| 786 | "Discarding ordered segments -- min vc is %s, max vc is %s", |
| 787 | vc_min, vc_max); |
| 788 | VG_(free)(vc_min); |
| 789 | VG_(free)(vc_max); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 790 | DRD_(vc_cleanup)(&thread_vc_max); |
| 791 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 792 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 793 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 794 | { |
| 795 | Segment* sg; |
| 796 | Segment* sg_next; |
| 797 | for (sg = DRD_(g_threadinfo)[i].first; |
| 798 | sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
| 799 | sg = sg_next) |
| 800 | { |
| 801 | thread_discard_segment(i, sg); |
| 802 | } |
| 803 | } |
| 804 | DRD_(vc_cleanup)(&thread_vc_min); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 805 | } |
| 806 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 807 | /** |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 808 | * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper |
| 809 | * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if |
| 810 | * all segments in the set CS are ordered consistently against both sg1 and |
| 811 | * sg2. The set CS is defined as the set of segments that can immediately |
| 812 | * precede future segments via inter-thread synchronization operations. In |
| 813 | * DRD the set CS consists of the latest segment of each thread combined with |
| 814 | * all segments for which the reference count is strictly greater than one. |
| 815 | * The code below is an optimized version of the following: |
| 816 | * |
| 817 | * for (i = 0; i < DRD_N_THREADS; i++) |
| 818 | * { |
| 819 | * Segment* sg; |
| 820 | * |
| 821 | * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| 822 | * { |
| 823 | * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1) |
| 824 | * { |
| 825 | * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc) |
| 826 | * != DRD_(vc_lte)(&sg2->vc, &sg->vc) |
| 827 | * || DRD_(vc_lte)(&sg->vc, &sg1->vc) |
| 828 | * != DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 829 | * { |
| 830 | * return False; |
| 831 | * } |
| 832 | * } |
| 833 | * } |
| 834 | * } |
| 835 | */ |
| 836 | static Bool thread_consistent_segment_ordering(const DrdThreadId tid, |
| 837 | Segment* const sg1, |
| 838 | Segment* const sg2) |
| 839 | { |
| 840 | unsigned i; |
| 841 | |
| 842 | tl_assert(sg1->next); |
| 843 | tl_assert(sg2->next); |
| 844 | tl_assert(sg1->next == sg2); |
| 845 | tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc)); |
| 846 | |
| 847 | for (i = 0; i < DRD_N_THREADS; i++) |
| 848 | { |
| 849 | Segment* sg; |
| 850 | |
| 851 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| 852 | { |
| 853 | if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1) |
| 854 | { |
| 855 | if (DRD_(vc_lte)(&sg2->vc, &sg->vc)) |
| 856 | break; |
| 857 | if (DRD_(vc_lte)(&sg1->vc, &sg->vc)) |
| 858 | return False; |
| 859 | } |
| 860 | } |
| 861 | for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev) |
| 862 | { |
| 863 | if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1) |
| 864 | { |
| 865 | if (DRD_(vc_lte)(&sg->vc, &sg1->vc)) |
| 866 | break; |
| 867 | if (DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 868 | return False; |
| 869 | } |
| 870 | } |
| 871 | } |
| 872 | return True; |
| 873 | } |
| 874 | |
| 875 | /** |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 876 | * Merge all segments that may be merged without triggering false positives |
| 877 | * or discarding real data races. For the theoretical background of segment |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 878 | * merging, see also the following paper: Mark Christiaens, Michiel Ronsse |
| 879 | * and Koen De Bosschere. Bounding the number of segment histories during |
| 880 | * data race detection. Parallel Computing archive, Volume 28, Issue 9, |
| 881 | * pp 1221-1238, September 2002. This paper contains a proof that merging |
| 882 | * consecutive segments for which the property equiv(s1,s2) holds can be |
| 883 | * merged without reducing the accuracy of datarace detection. Furthermore |
| 884 | * it is also proven that the total number of all segments will never grow |
| 885 | * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged |
| 886 | * every time a new segment is created. The property equiv(s1, s2) is defined |
| 887 | * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector |
| 888 | * clocks of segments s and s1 are ordered in the same way as those of segments |
| 889 | * s and s2. The set CS is defined as the set of existing segments s that have |
| 890 | * the potential to conflict with not yet created segments, either because the |
| 891 | * segment s is the latest segment of a thread or because it can become the |
| 892 | * immediate predecessor of a new segment due to a synchronization operation. |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 893 | */ |
| 894 | static void thread_merge_segments(void) |
| 895 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 896 | unsigned i; |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 897 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 898 | s_new_segments_since_last_merge = 0; |
| 899 | |
| 900 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 901 | { |
| 902 | Segment* sg; |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 903 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 904 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 905 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 906 | #endif |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 907 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 908 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 909 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 910 | if (DRD_(sg_get_refcnt)(sg) == 1 |
| 911 | && sg->next |
| 912 | && DRD_(sg_get_refcnt)(sg->next) == 1 |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 913 | && sg->next->next |
| 914 | && thread_consistent_segment_ordering(i, sg, sg->next)) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 915 | { |
| 916 | /* Merge sg and sg->next into sg. */ |
| 917 | DRD_(sg_merge)(sg, sg->next); |
| 918 | thread_discard_segment(i, sg->next); |
| 919 | } |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 920 | } |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 921 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 922 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 923 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 924 | #endif |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 925 | } |
| bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 926 | } |
| 927 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 928 | /** |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 929 | * Create a new segment for the specified thread, and discard any segments |
| 930 | * that cannot cause races anymore. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 931 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 932 | void DRD_(thread_new_segment)(const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 933 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 934 | Segment* last_sg; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 935 | Segment* new_sg; |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 936 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 937 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 938 | && tid != DRD_INVALID_THREADID); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 939 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 940 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 941 | last_sg = DRD_(g_threadinfo)[tid].last; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 942 | new_sg = DRD_(sg_new)(tid, tid); |
| 943 | thread_append_segment(tid, new_sg); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 944 | if (tid == DRD_(g_drd_running_tid) && last_sg) |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 945 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 946 | DRD_(thread_update_conflict_set)(tid, &last_sg->vc); |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 947 | s_update_conflict_set_new_sg_count++; |
| 948 | } |
| bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 949 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 950 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 951 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 952 | if (s_segment_merging |
| 953 | && ++s_new_segments_since_last_merge >= s_segment_merge_interval) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 954 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 955 | thread_discard_ordered_segments(); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 956 | thread_merge_segments(); |
| 957 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 958 | } |
| 959 | |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 960 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 961 | void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 962 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 963 | tl_assert(joiner != joinee); |
| 964 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
| 965 | && joiner != DRD_INVALID_THREADID); |
| 966 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
| 967 | && joinee != DRD_INVALID_THREADID); |
| 968 | tl_assert(DRD_(g_threadinfo)[joiner].last); |
| 969 | tl_assert(DRD_(g_threadinfo)[joinee].last); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 970 | |
| 971 | if (DRD_(sg_get_trace)()) |
| 972 | { |
| 973 | char *str1, *str2; |
| 974 | str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc); |
| 975 | str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc); |
| 976 | VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s", |
| 977 | str1, str2); |
| 978 | VG_(free)(str1); |
| 979 | VG_(free)(str2); |
| 980 | } |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 981 | if (joiner == DRD_(g_drd_running_tid)) |
| 982 | { |
| 983 | VectorClock old_vc; |
| 984 | |
| 985 | DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc); |
| 986 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
| 987 | &DRD_(g_threadinfo)[joinee].last->vc); |
| 988 | DRD_(thread_update_conflict_set)(joiner, &old_vc); |
| 989 | s_update_conflict_set_join_count++; |
| 990 | DRD_(vc_cleanup)(&old_vc); |
| 991 | } |
| 992 | else |
| 993 | { |
| 994 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
| 995 | &DRD_(g_threadinfo)[joinee].last->vc); |
| 996 | } |
| 997 | |
| 998 | thread_discard_ordered_segments(); |
| 999 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1000 | if (DRD_(sg_get_trace)()) |
| 1001 | { |
| 1002 | char* str; |
| 1003 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc); |
| 1004 | VG_(message)(Vg_DebugMsg, "After join: %s", str); |
| 1005 | VG_(free)(str); |
| 1006 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1009 | /** |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1010 | * Update the vector clock of the last segment of thread tid with the |
| 1011 | * the vector clock of segment sg. Call this function after thread tid had |
| 1012 | * to wait because of thread synchronization until the memory accesses in the |
| 1013 | * segment sg finished. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1014 | */ |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1015 | void DRD_(thread_combine_vc_sync)(DrdThreadId tid, const Segment* sg) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1016 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1017 | const VectorClock* const vc = &sg->vc; |
| 1018 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1019 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1020 | && tid != DRD_INVALID_THREADID); |
| 1021 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1022 | tl_assert(sg); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1023 | tl_assert(vc); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1024 | |
| 1025 | if (tid != sg->tid) |
| 1026 | { |
| 1027 | VectorClock old_vc; |
| 1028 | |
| 1029 | DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc); |
| 1030 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc); |
| 1031 | if (DRD_(sg_get_trace)()) |
| 1032 | { |
| 1033 | char *str1, *str2; |
| 1034 | str1 = DRD_(vc_aprint)(&old_vc); |
| 1035 | str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
| 1036 | VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s", tid, str1, str2); |
| 1037 | VG_(free)(str1); |
| 1038 | VG_(free)(str2); |
| 1039 | } |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 1040 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1041 | thread_discard_ordered_segments(); |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 1042 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1043 | DRD_(thread_update_conflict_set)(tid, &old_vc); |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 1044 | s_update_conflict_set_sync_count++; |
| 1045 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1046 | DRD_(vc_cleanup)(&old_vc); |
| 1047 | } |
| 1048 | else |
| 1049 | { |
| 1050 | tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc)); |
| 1051 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1052 | } |
| 1053 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1054 | /** |
| 1055 | * Call this function whenever a thread is no longer using the memory |
| 1056 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 1057 | * increase. |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1058 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1059 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1060 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1061 | DrdThreadId other_user; |
| 1062 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1063 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1064 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
| 1065 | other_user = DRD_INVALID_THREADID; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1066 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1067 | { |
| 1068 | Segment* p; |
| 1069 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1070 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1071 | if (other_user == DRD_INVALID_THREADID |
| 1072 | && i != DRD_(g_drd_running_tid)) |
| 1073 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1074 | if (UNLIKELY(DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2))) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1075 | { |
| 1076 | other_user = i; |
| 1077 | } |
| 1078 | continue; |
| 1079 | } |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1080 | DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1081 | } |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1082 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1083 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1084 | /* |
| 1085 | * If any other thread had accessed memory in [ a1, a2 [, update the |
| 1086 | * conflict set. |
| 1087 | */ |
| 1088 | if (other_user != DRD_INVALID_THREADID |
| 1089 | && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2)) |
| 1090 | { |
| 1091 | thread_compute_conflict_set(&DRD_(g_conflict_set), |
| 1092 | DRD_(thread_get_running_tid)()); |
| 1093 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1094 | } |
| 1095 | |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1096 | /** Specify whether memory loads should be recorded. */ |
| 1097 | void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1098 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1099 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1100 | && tid != DRD_INVALID_THREADID); |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1101 | tl_assert(enabled == !! enabled); |
| 1102 | |
| 1103 | DRD_(g_threadinfo)[tid].is_recording_loads = enabled; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1106 | /** Specify whether memory stores should be recorded. */ |
| 1107 | void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled) |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1108 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1109 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1110 | && tid != DRD_INVALID_THREADID); |
| bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1111 | tl_assert(enabled == !! enabled); |
| 1112 | |
| 1113 | DRD_(g_threadinfo)[tid].is_recording_stores = enabled; |
| bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1114 | } |
| 1115 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1116 | /** |
| 1117 | * Print the segment information for all threads. |
| 1118 | * |
| 1119 | * This function is only used for debugging purposes. |
| 1120 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1121 | void DRD_(thread_print_all)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1122 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1123 | unsigned i; |
| 1124 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1125 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1126 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1127 | { |
| 1128 | if (DRD_(g_threadinfo)[i].first) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1129 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1130 | VG_(printf)("**************\n" |
| 1131 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
| 1132 | "**************\n", |
| 1133 | i, |
| 1134 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 1135 | DRD_(g_threadinfo)[i].vg_threadid, |
| 1136 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 1137 | DRD_(g_threadinfo)[i].pt_threadid, |
| 1138 | DRD_(g_threadinfo)[i].detached_posix_thread); |
| 1139 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| 1140 | { |
| 1141 | DRD_(sg_print)(p); |
| 1142 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1143 | } |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1144 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1145 | } |
| 1146 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1147 | /** Show a call stack involved in a data race. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1148 | static void show_call_stack(const DrdThreadId tid, |
| 1149 | const Char* const msg, |
| 1150 | ExeContext* const callstack) |
| 1151 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1152 | const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1153 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1154 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1155 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1156 | if (vg_tid != VG_INVALID_THREADID) |
| 1157 | { |
| 1158 | if (callstack) |
| 1159 | { |
| 1160 | VG_(pp_ExeContext)(callstack); |
| 1161 | } |
| 1162 | else |
| 1163 | { |
| 1164 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 1165 | } |
| 1166 | } |
| 1167 | else |
| 1168 | { |
| 1169 | VG_(message)(Vg_UserMsg, |
| 1170 | " (thread finished, call stack no longer available)"); |
| 1171 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1174 | /** Print information about the segments involved in a data race. */ |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1175 | static void |
| 1176 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 1177 | const Addr addr, |
| 1178 | const SizeT size, |
| 1179 | const BmAccessTypeT access_type, |
| 1180 | const Segment* const p) |
| 1181 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1182 | unsigned i; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1183 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1184 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1185 | && tid != DRD_INVALID_THREADID); |
| 1186 | tl_assert(p); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1187 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1188 | for (i = 0; i < DRD_N_THREADS; i++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1189 | { |
| 1190 | if (i != tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1191 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1192 | Segment* q; |
| 1193 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
| 1194 | { |
| 1195 | /* |
| 1196 | * Since q iterates over the segments of thread i in order of |
| 1197 | * decreasing vector clocks, if q->vc <= p->vc, then |
| 1198 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 1199 | * loop once this condition is met. |
| 1200 | */ |
| 1201 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
| 1202 | break; |
| 1203 | if (! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1204 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1205 | if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size, |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1206 | access_type)) |
| 1207 | { |
| 1208 | tl_assert(q->stacktrace); |
| 1209 | show_call_stack(i, "Other segment start", |
| 1210 | q->stacktrace); |
| 1211 | show_call_stack(i, "Other segment end", |
| 1212 | q->next ? q->next->stacktrace : 0); |
| 1213 | } |
| 1214 | } |
| 1215 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1216 | } |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1217 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1220 | /** Print information about all segments involved in a data race. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1221 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
| 1222 | const Addr addr, |
| 1223 | const SizeT size, |
| 1224 | const BmAccessTypeT access_type) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1225 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1226 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1227 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1228 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1229 | && tid != DRD_INVALID_THREADID); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1230 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1231 | for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next) |
| 1232 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1233 | if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type)) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1234 | { |
| 1235 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1236 | access_type, p); |
| 1237 | } |
| 1238 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1239 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1240 | |
| bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1241 | /** |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1242 | * Verify whether the conflict set for thread tid is up to date. Only perform |
| 1243 | * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set. |
| 1244 | */ |
| 1245 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid) |
| 1246 | { |
| 1247 | static int do_verify_conflict_set = -1; |
| 1248 | Bool result; |
| 1249 | struct bitmap* computed_conflict_set = 0; |
| 1250 | |
| 1251 | if (do_verify_conflict_set < 0) |
| 1252 | do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0; |
| 1253 | |
| 1254 | if (do_verify_conflict_set == 0) |
| 1255 | return True; |
| 1256 | |
| 1257 | thread_compute_conflict_set(&computed_conflict_set, tid); |
| 1258 | result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set); |
| 1259 | if (! result) |
| 1260 | { |
| 1261 | VG_(printf)("actual conflict set:\n"); |
| 1262 | DRD_(bm_print)(DRD_(g_conflict_set)); |
| 1263 | VG_(printf)("\n"); |
| 1264 | VG_(printf)("computed conflict set:\n"); |
| 1265 | DRD_(bm_print)(computed_conflict_set); |
| 1266 | VG_(printf)("\n"); |
| 1267 | } |
| 1268 | DRD_(bm_delete)(computed_conflict_set); |
| 1269 | return result; |
| 1270 | } |
| 1271 | |
| 1272 | /** |
| 1273 | * Compute the conflict set: a bitmap that represents the union of all memory |
| 1274 | * accesses of all segments that are unordered to the current segment of the |
| 1275 | * thread tid. |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1276 | */ |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1277 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 1278 | const DrdThreadId tid) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1279 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1280 | Segment* p; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1281 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1282 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1283 | && tid != DRD_INVALID_THREADID); |
| 1284 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1285 | |
| bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1286 | s_compute_conflict_set_count++; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1287 | s_conflict_set_bitmap_creation_count |
| 1288 | -= DRD_(bm_get_bitmap_creation_count)(); |
| 1289 | s_conflict_set_bitmap2_creation_count |
| 1290 | -= DRD_(bm_get_bitmap2_creation_count)(); |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1291 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1292 | if (*conflict_set) |
| 1293 | { |
| 1294 | DRD_(bm_delete)(*conflict_set); |
| 1295 | } |
| 1296 | *conflict_set = DRD_(bm_new)(); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1297 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1298 | if (s_trace_conflict_set) |
| 1299 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1300 | char* str; |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1301 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1302 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
| 1303 | VG_(message)(Vg_DebugMsg, |
| 1304 | "computing conflict set for thread %d/%d with vc %s", |
| 1305 | DRD_(DrdThreadIdToVgThreadId)(tid), tid, str); |
| 1306 | VG_(free)(str); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1307 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1308 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1309 | p = DRD_(g_threadinfo)[tid].last; |
| 1310 | { |
| 1311 | unsigned j; |
| 1312 | |
| 1313 | if (s_trace_conflict_set) |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1314 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1315 | char* vc; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1316 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1317 | vc = DRD_(vc_aprint)(&p->vc); |
| 1318 | VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s", |
| 1319 | tid, vc); |
| 1320 | VG_(free)(vc); |
| bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1321 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1322 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1323 | for (j = 0; j < DRD_N_THREADS; j++) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1324 | { |
| 1325 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| 1326 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1327 | Segment* q; |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1328 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1329 | { |
| 1330 | if (! DRD_(vc_lte)(&q->vc, &p->vc) |
| 1331 | && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1332 | { |
| 1333 | if (s_trace_conflict_set) |
| 1334 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1335 | char* str; |
| 1336 | |
| 1337 | str = DRD_(vc_aprint)(&q->vc); |
| 1338 | VG_(message)(Vg_DebugMsg, |
| 1339 | "conflict set: [%d] merging segment %s", |
| 1340 | j, str); |
| 1341 | VG_(free)(str); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1342 | } |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1343 | DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q)); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1344 | } |
| 1345 | else |
| 1346 | { |
| 1347 | if (s_trace_conflict_set) |
| 1348 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1349 | char* str; |
| 1350 | |
| 1351 | str = DRD_(vc_aprint)(&q->vc); |
| 1352 | VG_(message)(Vg_DebugMsg, |
| 1353 | "conflict set: [%d] ignoring segment %s", |
| 1354 | j, str); |
| 1355 | VG_(free)(str); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1356 | } |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1362 | |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1363 | s_conflict_set_bitmap_creation_count |
| 1364 | += DRD_(bm_get_bitmap_creation_count)(); |
| 1365 | s_conflict_set_bitmap2_creation_count |
| 1366 | += DRD_(bm_get_bitmap2_creation_count)(); |
| 1367 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1368 | if (s_trace_conflict_set_bm) |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1369 | { |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1370 | VG_(message)(Vg_DebugMsg, "[%d] new conflict set:", tid); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1371 | DRD_(bm_print)(*conflict_set); |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1372 | VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.", tid); |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1373 | } |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1374 | } |
| 1375 | |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1376 | /** |
| 1377 | * Update the conflict set after the vector clock of thread tid has been |
| 1378 | * updated from old_vc to its current value, either because a new segment has |
| 1379 | * been created or because of a synchronization operation. |
| 1380 | */ |
| 1381 | void DRD_(thread_update_conflict_set)(const DrdThreadId tid, |
| 1382 | const VectorClock* const old_vc) |
| 1383 | { |
| 1384 | const VectorClock* new_vc; |
| 1385 | Segment* p; |
| 1386 | unsigned j; |
| 1387 | |
| 1388 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1389 | && tid != DRD_INVALID_THREADID); |
| 1390 | tl_assert(old_vc); |
| 1391 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| 1392 | tl_assert(DRD_(g_conflict_set)); |
| 1393 | |
| 1394 | if (s_trace_conflict_set) |
| 1395 | { |
| 1396 | char* str; |
| 1397 | |
| 1398 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
| 1399 | VG_(message)(Vg_DebugMsg, |
| 1400 | "updating conflict set for thread %d/%d with vc %s", |
| 1401 | DRD_(DrdThreadIdToVgThreadId)(tid), tid, str); |
| 1402 | VG_(free)(str); |
| 1403 | } |
| 1404 | |
| 1405 | new_vc = &DRD_(g_threadinfo)[tid].last->vc; |
| 1406 | |
| 1407 | DRD_(bm_unmark)(DRD_(g_conflict_set)); |
| 1408 | |
| 1409 | for (j = 0; j < DRD_N_THREADS; j++) |
| 1410 | { |
| 1411 | Segment* q; |
| 1412 | |
| 1413 | if (j == tid || ! DRD_(IsValidDrdThreadId)(j)) |
| 1414 | continue; |
| 1415 | |
| 1416 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1417 | { |
| 1418 | const int included_in_old_conflict_set |
| 1419 | = ! DRD_(vc_lte)(&q->vc, old_vc) |
| 1420 | && ! DRD_(vc_lte)(old_vc, &q->vc); |
| 1421 | const int included_in_new_conflict_set |
| 1422 | = ! DRD_(vc_lte)(&q->vc, new_vc) |
| 1423 | && ! DRD_(vc_lte)(new_vc, &q->vc); |
| 1424 | if (included_in_old_conflict_set != included_in_new_conflict_set) |
| 1425 | { |
| 1426 | if (s_trace_conflict_set) |
| 1427 | { |
| 1428 | char* str; |
| 1429 | |
| 1430 | str = DRD_(vc_aprint)(&q->vc); |
| 1431 | VG_(message)(Vg_DebugMsg, |
| 1432 | "conflict set: [%d] merging segment %s", j, str); |
| 1433 | VG_(free)(str); |
| 1434 | } |
| 1435 | DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
| 1436 | } |
| 1437 | else |
| 1438 | { |
| 1439 | if (s_trace_conflict_set) |
| 1440 | { |
| 1441 | char* str; |
| 1442 | |
| 1443 | str = DRD_(vc_aprint)(&q->vc); |
| 1444 | VG_(message)(Vg_DebugMsg, |
| 1445 | "conflict set: [%d] ignoring segment %s", j, str); |
| 1446 | VG_(free)(str); |
| 1447 | } |
| 1448 | } |
| 1449 | } |
| 1450 | } |
| 1451 | |
| 1452 | DRD_(bm_clear_marked)(DRD_(g_conflict_set)); |
| 1453 | |
| 1454 | p = DRD_(g_threadinfo)[tid].last; |
| 1455 | { |
| 1456 | for (j = 0; j < DRD_N_THREADS; j++) |
| 1457 | { |
| 1458 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| 1459 | { |
| 1460 | Segment* q; |
| 1461 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1462 | { |
| 1463 | if (! DRD_(vc_lte)(&q->vc, &p->vc) |
| 1464 | && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1465 | { |
| 1466 | DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
| 1467 | } |
| 1468 | } |
| 1469 | } |
| 1470 | } |
| 1471 | } |
| 1472 | |
| 1473 | DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set)); |
| 1474 | |
| bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1475 | s_update_conflict_set_count++; |
| bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1476 | |
| 1477 | if (s_trace_conflict_set_bm) |
| 1478 | { |
| 1479 | VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:", tid); |
| 1480 | DRD_(bm_print)(DRD_(g_conflict_set)); |
| 1481 | VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.", tid); |
| 1482 | } |
| 1483 | |
| 1484 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| 1485 | } |
| 1486 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1487 | /** Report the number of context switches performed. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1488 | ULong DRD_(thread_get_context_switch_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1489 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1490 | return s_context_switch_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1493 | /** Report the number of ordered segments that have been discarded. */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1494 | ULong DRD_(thread_get_discard_ordered_segments_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1495 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1496 | return s_discard_ordered_segments_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
| bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1499 | /** Return how many times the conflict set has been updated entirely. */ |
| 1500 | ULong DRD_(thread_get_compute_conflict_set_count)() |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1501 | { |
| bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1502 | return s_compute_conflict_set_count; |
| 1503 | } |
| 1504 | |
| 1505 | /** Return how many times the conflict set has been updated partially. */ |
| 1506 | ULong DRD_(thread_get_update_conflict_set_count)(void) |
| 1507 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1508 | return s_update_conflict_set_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1509 | } |
| 1510 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1511 | /** |
| bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame^] | 1512 | * Return how many times the conflict set has been updated partially |
| 1513 | * because a new segment has been created. |
| 1514 | */ |
| 1515 | ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void) |
| 1516 | { |
| 1517 | return s_update_conflict_set_new_sg_count; |
| 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * Return how many times the conflict set has been updated partially |
| 1522 | * because of combining vector clocks due to synchronization operations |
| 1523 | * other than reader/writer lock or barrier operations. |
| 1524 | */ |
| 1525 | ULong DRD_(thread_get_update_conflict_set_sync_count)(void) |
| 1526 | { |
| 1527 | return s_update_conflict_set_sync_count; |
| 1528 | } |
| 1529 | |
| 1530 | /** |
| 1531 | * Return how many times the conflict set has been updated partially |
| 1532 | * because of thread joins. |
| 1533 | */ |
| 1534 | ULong DRD_(thread_get_update_conflict_set_join_count)(void) |
| 1535 | { |
| 1536 | return s_update_conflict_set_join_count; |
| 1537 | } |
| 1538 | |
| 1539 | /** |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1540 | * Return the number of first-level bitmaps that have been created during |
| 1541 | * conflict set updates. |
| 1542 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1543 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1544 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1545 | return s_conflict_set_bitmap_creation_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1546 | } |
| 1547 | |
| bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1548 | /** |
| 1549 | * Return the number of second-level bitmaps that have been created during |
| 1550 | * conflict set updates. |
| 1551 | */ |
| bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1552 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void) |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1553 | { |
| bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1554 | return s_conflict_set_bitmap2_creation_count; |
| sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1555 | } |