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; |
bart | 6f1d716 | 2009-06-24 18:34:10 +0000 | [diff] [blame] | 79 | static int s_segment_merge_interval = 10; |
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 | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame^] | 187 | DRD_(g_threadinfo)[i].pthread_create_nesting_level = 0; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 188 | DRD_(g_threadinfo)[i].synchr_nesting = 0; |
| 189 | tl_assert(DRD_(g_threadinfo)[i].first == 0); |
| 190 | tl_assert(DRD_(g_threadinfo)[i].last == 0); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 191 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 192 | tl_assert(DRD_(IsValidDrdThreadId)(i)); |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 193 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 194 | return i; |
| 195 | } |
| 196 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 197 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 198 | tl_assert(False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 199 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 200 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 201 | } |
| 202 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 203 | /** Convert a POSIX thread ID into a DRD thread ID. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 204 | DrdThreadId DRD_(PtThreadIdToDrdThreadId)(const PThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 205 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 206 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 207 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 208 | tl_assert(tid != INVALID_POSIX_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 209 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 210 | for (i = 1; i < DRD_N_THREADS; i++) |
| 211 | { |
| 212 | if (DRD_(g_threadinfo)[i].posix_thread_exists |
| 213 | && DRD_(g_threadinfo)[i].pt_threadid == tid) |
| 214 | { |
| 215 | return i; |
| 216 | } |
| 217 | } |
| 218 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 219 | } |
| 220 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 221 | /** Convert a DRD thread ID into a Valgrind thread ID. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 222 | ThreadId DRD_(DrdThreadIdToVgThreadId)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 223 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 224 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 225 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 226 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 227 | return (DRD_(g_threadinfo)[tid].vg_thread_exists |
| 228 | ? DRD_(g_threadinfo)[tid].vg_threadid |
| 229 | : VG_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 232 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 233 | /** |
| 234 | * Sanity check of the doubly linked list of segments referenced by a |
| 235 | * ThreadInfo struct. |
| 236 | * @return True if sane, False if not. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 237 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 238 | static Bool DRD_(sane_ThreadInfo)(const ThreadInfo* const ti) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 240 | Segment* p; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 241 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 242 | for (p = ti->first; p; p = p->next) { |
| 243 | if (p->next && p->next->prev != p) |
| 244 | return False; |
| 245 | if (p->next == 0 && p != ti->last) |
| 246 | return False; |
| 247 | } |
| 248 | for (p = ti->last; p; p = p->prev) { |
| 249 | if (p->prev && p->prev->next != p) |
| 250 | return False; |
| 251 | if (p->prev == 0 && p != ti->first) |
| 252 | return False; |
| 253 | } |
| 254 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 255 | } |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 256 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 257 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 258 | /** |
| 259 | * Create the first segment for a newly started thread. |
| 260 | * |
| 261 | * This function is called from the handler installed via |
| 262 | * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler |
| 263 | * from the context of the creator thread, before the new thread has been |
| 264 | * created. |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 265 | * |
| 266 | * @param[in] creator DRD thread ID of the creator thread. |
| 267 | * @param[in] vg_created Valgrind thread ID of the created thread. |
| 268 | * |
| 269 | * @return DRD thread ID of the created thread. |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 270 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 271 | DrdThreadId DRD_(thread_pre_create)(const DrdThreadId creator, |
| 272 | const ThreadId vg_created) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 273 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 274 | DrdThreadId created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 275 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 276 | tl_assert(DRD_(VgThreadIdToDrdThreadId)(vg_created) == DRD_INVALID_THREADID); |
| 277 | created = DRD_(VgThreadIdToNewDrdThreadId)(vg_created); |
| 278 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 279 | && created != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 280 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 281 | tl_assert(DRD_(g_threadinfo)[created].first == 0); |
| 282 | tl_assert(DRD_(g_threadinfo)[created].last == 0); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 283 | /* Create an initial segment for the newly created thread. */ |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 284 | thread_append_segment(created, DRD_(sg_new)(creator, created)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 285 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 286 | return created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 287 | } |
| 288 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 289 | /** |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 290 | * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called |
| 291 | * after the thread has been created and before any client instructions are run |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 292 | * on the newly created thread, e.g. from the handler installed via |
| 293 | * VG_(track_pre_thread_first_insn)(). |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 294 | * |
| 295 | * @param[in] vg_created Valgrind thread ID of the newly created thread. |
| 296 | * |
| 297 | * @return DRD thread ID for the new thread. |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 298 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 299 | DrdThreadId DRD_(thread_post_create)(const ThreadId vg_created) |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 300 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 301 | const DrdThreadId created = DRD_(VgThreadIdToDrdThreadId)(vg_created); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 302 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 303 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 304 | && created != DRD_INVALID_THREADID); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 305 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 306 | DRD_(g_threadinfo)[created].stack_max |
| 307 | = VG_(thread_get_stack_max)(vg_created); |
| 308 | DRD_(g_threadinfo)[created].stack_startup |
| 309 | = DRD_(g_threadinfo)[created].stack_max; |
| 310 | DRD_(g_threadinfo)[created].stack_min |
| 311 | = DRD_(g_threadinfo)[created].stack_max; |
| 312 | DRD_(g_threadinfo)[created].stack_min_min |
| 313 | = DRD_(g_threadinfo)[created].stack_max; |
| 314 | DRD_(g_threadinfo)[created].stack_size |
| 315 | = VG_(thread_get_stack_size)(vg_created); |
| 316 | tl_assert(DRD_(g_threadinfo)[created].stack_max != 0); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 317 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 318 | return created; |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 319 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 320 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 321 | /** |
| 322 | * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just |
| 323 | * after thread drd_joiner joined thread drd_joinee. |
| 324 | */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 325 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 326 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 327 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joiner)); |
| 328 | tl_assert(DRD_(IsValidDrdThreadId)(drd_joinee)); |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 329 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 330 | DRD_(thread_new_segment)(drd_joiner); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 331 | DRD_(thread_combine_vc_join)(drd_joiner, drd_joinee); |
bart | 7627be3 | 2009-06-06 12:26:05 +0000 | [diff] [blame] | 332 | DRD_(thread_new_segment)(drd_joinee); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 333 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 334 | if (s_trace_fork_join) |
| 335 | { |
| 336 | const ThreadId joiner = DRD_(DrdThreadIdToVgThreadId)(drd_joiner); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 343 | "drd_post_thread_join joiner = %d, joinee = %d", |
| 344 | drd_joiner, drd_joinee); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | } |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 354 | VG_(message)(Vg_DebugMsg, "%s\n", msg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 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 | dd75cdf | 2009-07-24 08:20:10 +0000 | [diff] [blame^] | 524 | /** Tells DRD that the calling thread is about to enter pthread_create(). */ |
| 525 | void DRD_(thread_entering_pthread_create)(const DrdThreadId tid) |
| 526 | { |
| 527 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 528 | && tid != DRD_INVALID_THREADID); |
| 529 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| 530 | tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level >= 0); |
| 531 | |
| 532 | DRD_(g_threadinfo)[tid].pthread_create_nesting_level++; |
| 533 | } |
| 534 | |
| 535 | /** Tells DRD that the calling thread has left pthread_create(). */ |
| 536 | void DRD_(thread_left_pthread_create)(const DrdThreadId tid) |
| 537 | { |
| 538 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 539 | && tid != DRD_INVALID_THREADID); |
| 540 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
| 541 | tl_assert(DRD_(g_threadinfo)[tid].pthread_create_nesting_level > 0); |
| 542 | |
| 543 | DRD_(g_threadinfo)[tid].pthread_create_nesting_level--; |
| 544 | } |
| 545 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 546 | /** Obtain the thread number and the user-assigned thread name. */ |
| 547 | const char* DRD_(thread_get_name)(const DrdThreadId tid) |
| 548 | { |
| 549 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 550 | && tid != DRD_INVALID_THREADID); |
| 551 | |
| 552 | return DRD_(g_threadinfo)[tid].name; |
| 553 | } |
| 554 | |
| 555 | /** Set the name of the specified thread. */ |
| 556 | void DRD_(thread_set_name)(const DrdThreadId tid, const char* const name) |
| 557 | { |
| 558 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 559 | && tid != DRD_INVALID_THREADID); |
| 560 | |
| 561 | if (name == NULL || name[0] == 0) |
| 562 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 563 | sizeof(DRD_(g_threadinfo)[tid].name), |
| 564 | "Thread %d", |
| 565 | tid); |
| 566 | else |
| 567 | VG_(snprintf)(DRD_(g_threadinfo)[tid].name, |
| 568 | sizeof(DRD_(g_threadinfo)[tid].name), |
| 569 | "Thread %d (%s)", |
| 570 | tid, name); |
| 571 | DRD_(g_threadinfo)[tid].name[sizeof(DRD_(g_threadinfo)[tid].name) - 1] = 0; |
| 572 | } |
| 573 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 574 | /** |
| 575 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 576 | * conflict set. |
| 577 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 578 | void DRD_(thread_set_vg_running_tid)(const ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 579 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 580 | tl_assert(vg_tid != VG_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 581 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 582 | if (vg_tid != s_vg_running_tid) |
| 583 | { |
| 584 | DRD_(thread_set_running_tid)(vg_tid, |
| 585 | DRD_(VgThreadIdToDrdThreadId)(vg_tid)); |
| 586 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 587 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 588 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 589 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 590 | } |
| 591 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 592 | /** |
| 593 | * Update s_vg_running_tid, DRD_(g_drd_running_tid) and recalculate the |
| 594 | * conflict set. |
| 595 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 596 | void DRD_(thread_set_running_tid)(const ThreadId vg_tid, |
| 597 | const DrdThreadId drd_tid) |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 598 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 599 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 600 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 601 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 602 | if (vg_tid != s_vg_running_tid) |
| 603 | { |
| 604 | if (s_trace_context_switches |
| 605 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
| 606 | { |
| 607 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 608 | "Context switch from thread %d to thread %d;" |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 609 | " segments: %llu\n", |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 610 | DRD_(g_drd_running_tid), drd_tid, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 611 | DRD_(sg_get_segments_alive_count)()); |
| 612 | } |
| 613 | s_vg_running_tid = vg_tid; |
| 614 | DRD_(g_drd_running_tid) = drd_tid; |
| 615 | thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid); |
| 616 | s_context_switch_count++; |
| 617 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 618 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 619 | tl_assert(s_vg_running_tid != VG_INVALID_THREADID); |
| 620 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 621 | } |
| 622 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 623 | /** |
| 624 | * Increase the synchronization nesting counter. Must be called before the |
| 625 | * client calls a synchronization function. |
| 626 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 627 | int DRD_(thread_enter_synchr)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 628 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 629 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 630 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 631 | } |
| 632 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 633 | /** |
| 634 | * Decrease the synchronization nesting counter. Must be called after the |
| 635 | * client left a synchronization function. |
| 636 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 637 | int DRD_(thread_leave_synchr)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 638 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 639 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 640 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 641 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 642 | } |
| 643 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 644 | /** Returns the synchronization nesting counter. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 645 | int DRD_(thread_get_synchr_nesting_count)(const DrdThreadId tid) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 646 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 647 | tl_assert(DRD_(IsValidDrdThreadId)(tid)); |
| 648 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 649 | } |
| 650 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 651 | /** Append a new segment at the end of the segment list. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 652 | static |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 653 | void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 654 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 655 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 656 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 657 | |
| 658 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 659 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 660 | #endif |
| 661 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 662 | sg->prev = DRD_(g_threadinfo)[tid].last; |
| 663 | sg->next = 0; |
| 664 | if (DRD_(g_threadinfo)[tid].last) |
| 665 | DRD_(g_threadinfo)[tid].last->next = sg; |
| 666 | DRD_(g_threadinfo)[tid].last = sg; |
| 667 | if (DRD_(g_threadinfo)[tid].first == 0) |
| 668 | DRD_(g_threadinfo)[tid].first = sg; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 669 | |
| 670 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 671 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 672 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 673 | } |
| 674 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 675 | /** |
| 676 | * Remove a segment from the segment list of thread threadid, and free the |
| 677 | * associated memory. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 678 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 679 | static |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 680 | void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 681 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 682 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 683 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 684 | |
| 685 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 686 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 687 | #endif |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 688 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 689 | if (sg->prev) |
| 690 | sg->prev->next = sg->next; |
| 691 | if (sg->next) |
| 692 | sg->next->prev = sg->prev; |
| 693 | if (sg == DRD_(g_threadinfo)[tid].first) |
| 694 | DRD_(g_threadinfo)[tid].first = sg->next; |
| 695 | if (sg == DRD_(g_threadinfo)[tid].last) |
| 696 | DRD_(g_threadinfo)[tid].last = sg->prev; |
| 697 | DRD_(sg_put)(sg); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 698 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 699 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 700 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[tid])); |
| 701 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 702 | } |
| 703 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 704 | /** |
| 705 | * Returns a pointer to the vector clock of the most recent segment associated |
| 706 | * with thread 'tid'. |
| 707 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 708 | VectorClock* DRD_(thread_get_vc)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 709 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 710 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 711 | && tid != DRD_INVALID_THREADID); |
| 712 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| 713 | return &DRD_(g_threadinfo)[tid].last->vc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 714 | } |
| 715 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 716 | /** |
| 717 | * Return the latest segment of thread 'tid' and increment its reference count. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 718 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 719 | void DRD_(thread_get_latest_segment)(Segment** sg, const DrdThreadId tid) |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 720 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 721 | tl_assert(sg); |
| 722 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 723 | && tid != DRD_INVALID_THREADID); |
| 724 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 725 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 726 | DRD_(sg_put)(*sg); |
| 727 | *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 728 | } |
| 729 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 730 | /** |
| 731 | * Compute the minimum of all latest vector clocks of all threads |
| 732 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 733 | * |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 734 | * @param vc pointer to a vectorclock, holds result upon return. |
| 735 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 736 | static void DRD_(thread_compute_minimum_vc)(VectorClock* vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 737 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 738 | unsigned i; |
| 739 | Bool first; |
| 740 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 741 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 742 | first = True; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 743 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 744 | { |
| 745 | latest_sg = DRD_(g_threadinfo)[i].last; |
| 746 | if (latest_sg) |
| 747 | { |
| 748 | if (first) |
| 749 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 750 | else |
| 751 | DRD_(vc_min)(vc, &latest_sg->vc); |
| 752 | first = False; |
| 753 | } |
| 754 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 755 | } |
| 756 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 757 | /** |
| 758 | * Compute the maximum of all latest vector clocks of all threads. |
| 759 | * |
| 760 | * @param vc pointer to a vectorclock, holds result upon return. |
| 761 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 762 | static void DRD_(thread_compute_maximum_vc)(VectorClock* vc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 763 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 764 | unsigned i; |
| 765 | Bool first; |
| 766 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 767 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 768 | first = True; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 769 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 770 | { |
| 771 | latest_sg = DRD_(g_threadinfo)[i].last; |
| 772 | if (latest_sg) |
| 773 | { |
| 774 | if (first) |
| 775 | DRD_(vc_assign)(vc, &latest_sg->vc); |
| 776 | else |
| 777 | DRD_(vc_combine)(vc, &latest_sg->vc); |
| 778 | first = False; |
| 779 | } |
| 780 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 781 | } |
| 782 | |
| 783 | /** |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 784 | * Discard all segments that have a defined order against the latest vector |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 785 | * clock of all threads -- these segments can no longer be involved in a |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 786 | * data race. |
| 787 | */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 788 | static void thread_discard_ordered_segments(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 789 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 790 | unsigned i; |
| 791 | VectorClock thread_vc_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 792 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 793 | s_discard_ordered_segments_count++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 794 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 795 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
| 796 | DRD_(thread_compute_minimum_vc)(&thread_vc_min); |
| 797 | if (DRD_(sg_get_trace)()) |
| 798 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 799 | char *vc_min, *vc_max; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 800 | VectorClock thread_vc_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 801 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 802 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
| 803 | DRD_(thread_compute_maximum_vc)(&thread_vc_max); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 804 | vc_min = DRD_(vc_aprint)(&thread_vc_min); |
| 805 | vc_max = DRD_(vc_aprint)(&thread_vc_max); |
| 806 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 807 | "Discarding ordered segments -- min vc is %s, max vc is %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 808 | vc_min, vc_max); |
| 809 | VG_(free)(vc_min); |
| 810 | VG_(free)(vc_max); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 811 | DRD_(vc_cleanup)(&thread_vc_max); |
| 812 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 813 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 814 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 815 | { |
| 816 | Segment* sg; |
| 817 | Segment* sg_next; |
| 818 | for (sg = DRD_(g_threadinfo)[i].first; |
| 819 | sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
| 820 | sg = sg_next) |
| 821 | { |
| 822 | thread_discard_segment(i, sg); |
| 823 | } |
| 824 | } |
| 825 | DRD_(vc_cleanup)(&thread_vc_min); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 826 | } |
| 827 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 828 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 829 | * An implementation of the property 'equiv(sg1, sg2)' as defined in the paper |
| 830 | * by Mark Christiaens e.a. The property equiv(sg1, sg2) holds if and only if |
| 831 | * all segments in the set CS are ordered consistently against both sg1 and |
| 832 | * sg2. The set CS is defined as the set of segments that can immediately |
| 833 | * precede future segments via inter-thread synchronization operations. In |
| 834 | * DRD the set CS consists of the latest segment of each thread combined with |
| 835 | * all segments for which the reference count is strictly greater than one. |
| 836 | * The code below is an optimized version of the following: |
| 837 | * |
| 838 | * for (i = 0; i < DRD_N_THREADS; i++) |
| 839 | * { |
| 840 | * Segment* sg; |
| 841 | * |
| 842 | * for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| 843 | * { |
| 844 | * if (sg == DRD_(g_threadinfo)[i].last || DRD_(sg_get_refcnt)(sg) > 1) |
| 845 | * { |
| 846 | * if ( DRD_(vc_lte)(&sg1->vc, &sg->vc) |
| 847 | * != DRD_(vc_lte)(&sg2->vc, &sg->vc) |
| 848 | * || DRD_(vc_lte)(&sg->vc, &sg1->vc) |
| 849 | * != DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 850 | * { |
| 851 | * return False; |
| 852 | * } |
| 853 | * } |
| 854 | * } |
| 855 | * } |
| 856 | */ |
| 857 | static Bool thread_consistent_segment_ordering(const DrdThreadId tid, |
| 858 | Segment* const sg1, |
| 859 | Segment* const sg2) |
| 860 | { |
| 861 | unsigned i; |
| 862 | |
| 863 | tl_assert(sg1->next); |
| 864 | tl_assert(sg2->next); |
| 865 | tl_assert(sg1->next == sg2); |
| 866 | tl_assert(DRD_(vc_lte)(&sg1->vc, &sg2->vc)); |
| 867 | |
| 868 | for (i = 0; i < DRD_N_THREADS; i++) |
| 869 | { |
| 870 | Segment* sg; |
| 871 | |
| 872 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
| 873 | { |
| 874 | if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1) |
| 875 | { |
| 876 | if (DRD_(vc_lte)(&sg2->vc, &sg->vc)) |
| 877 | break; |
| 878 | if (DRD_(vc_lte)(&sg1->vc, &sg->vc)) |
| 879 | return False; |
| 880 | } |
| 881 | } |
| 882 | for (sg = DRD_(g_threadinfo)[i].last; sg; sg = sg->prev) |
| 883 | { |
| 884 | if (! sg->next || DRD_(sg_get_refcnt)(sg) > 1) |
| 885 | { |
| 886 | if (DRD_(vc_lte)(&sg->vc, &sg1->vc)) |
| 887 | break; |
| 888 | if (DRD_(vc_lte)(&sg->vc, &sg2->vc)) |
| 889 | return False; |
| 890 | } |
| 891 | } |
| 892 | } |
| 893 | return True; |
| 894 | } |
| 895 | |
| 896 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 897 | * Merge all segments that may be merged without triggering false positives |
| 898 | * or discarding real data races. For the theoretical background of segment |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 899 | * merging, see also the following paper: Mark Christiaens, Michiel Ronsse |
| 900 | * and Koen De Bosschere. Bounding the number of segment histories during |
| 901 | * data race detection. Parallel Computing archive, Volume 28, Issue 9, |
| 902 | * pp 1221-1238, September 2002. This paper contains a proof that merging |
| 903 | * consecutive segments for which the property equiv(s1,s2) holds can be |
| 904 | * merged without reducing the accuracy of datarace detection. Furthermore |
| 905 | * it is also proven that the total number of all segments will never grow |
| 906 | * unbounded if all segments s1, s2 for which equiv(s1, s2) holds are merged |
| 907 | * every time a new segment is created. The property equiv(s1, s2) is defined |
| 908 | * as follows: equiv(s1, s2) <=> for all segments in the set CS, the vector |
| 909 | * clocks of segments s and s1 are ordered in the same way as those of segments |
| 910 | * s and s2. The set CS is defined as the set of existing segments s that have |
| 911 | * the potential to conflict with not yet created segments, either because the |
| 912 | * segment s is the latest segment of a thread or because it can become the |
| 913 | * immediate predecessor of a new segment due to a synchronization operation. |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 914 | */ |
| 915 | static void thread_merge_segments(void) |
| 916 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 917 | unsigned i; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 918 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 919 | s_new_segments_since_last_merge = 0; |
| 920 | |
| 921 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 922 | { |
| 923 | Segment* sg; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 924 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 925 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 926 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 927 | #endif |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 928 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 929 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 930 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 931 | if (DRD_(sg_get_refcnt)(sg) == 1 |
| 932 | && sg->next |
| 933 | && DRD_(sg_get_refcnt)(sg->next) == 1 |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 934 | && sg->next->next |
| 935 | && thread_consistent_segment_ordering(i, sg, sg->next)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 936 | { |
| 937 | /* Merge sg and sg->next into sg. */ |
| 938 | DRD_(sg_merge)(sg, sg->next); |
| 939 | thread_discard_segment(i, sg->next); |
| 940 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 941 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 942 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 943 | #ifdef ENABLE_DRD_CONSISTENCY_CHECKS |
| 944 | tl_assert(DRD_(sane_ThreadInfo)(&DRD_(g_threadinfo)[i])); |
| 945 | #endif |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 946 | } |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 947 | } |
| 948 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 949 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 950 | * Create a new segment for the specified thread, and discard any segments |
| 951 | * that cannot cause races anymore. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 952 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 953 | void DRD_(thread_new_segment)(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 954 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 955 | Segment* last_sg; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 956 | Segment* new_sg; |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 957 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 958 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 959 | && tid != DRD_INVALID_THREADID); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 960 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 961 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 962 | last_sg = DRD_(g_threadinfo)[tid].last; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 963 | new_sg = DRD_(sg_new)(tid, tid); |
| 964 | thread_append_segment(tid, new_sg); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 965 | if (tid == DRD_(g_drd_running_tid) && last_sg) |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 966 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 967 | DRD_(thread_update_conflict_set)(tid, &last_sg->vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 968 | s_update_conflict_set_new_sg_count++; |
| 969 | } |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 970 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 971 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 972 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 973 | if (s_segment_merging |
| 974 | && ++s_new_segments_since_last_merge >= s_segment_merge_interval) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 975 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 976 | thread_discard_ordered_segments(); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 977 | thread_merge_segments(); |
| 978 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 979 | } |
| 980 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 981 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 982 | void DRD_(thread_combine_vc_join)(DrdThreadId joiner, DrdThreadId joinee) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 983 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 984 | tl_assert(joiner != joinee); |
| 985 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
| 986 | && joiner != DRD_INVALID_THREADID); |
| 987 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
| 988 | && joinee != DRD_INVALID_THREADID); |
| 989 | tl_assert(DRD_(g_threadinfo)[joiner].last); |
| 990 | tl_assert(DRD_(g_threadinfo)[joinee].last); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 991 | |
| 992 | if (DRD_(sg_get_trace)()) |
| 993 | { |
| 994 | char *str1, *str2; |
| 995 | str1 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc); |
| 996 | str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joinee].last->vc); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 997 | VG_(message)(Vg_DebugMsg, "Before join: joiner %s, joinee %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 998 | str1, str2); |
| 999 | VG_(free)(str1); |
| 1000 | VG_(free)(str2); |
| 1001 | } |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1002 | if (joiner == DRD_(g_drd_running_tid)) |
| 1003 | { |
| 1004 | VectorClock old_vc; |
| 1005 | |
| 1006 | DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[joiner].last->vc); |
| 1007 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1008 | &DRD_(g_threadinfo)[joinee].last->vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1009 | DRD_(thread_update_conflict_set)(joiner, &old_vc); |
| 1010 | s_update_conflict_set_join_count++; |
| 1011 | DRD_(vc_cleanup)(&old_vc); |
| 1012 | } |
| 1013 | else |
| 1014 | { |
| 1015 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1016 | &DRD_(g_threadinfo)[joinee].last->vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
| 1019 | thread_discard_ordered_segments(); |
| 1020 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1021 | if (DRD_(sg_get_trace)()) |
| 1022 | { |
| 1023 | char* str; |
| 1024 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[joiner].last->vc); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1025 | VG_(message)(Vg_DebugMsg, "After join: %s\n", str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1026 | VG_(free)(str); |
| 1027 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1028 | } |
| 1029 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1030 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1031 | * Update the vector clock of the last segment of thread tid with the |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1032 | * the vector clock of segment sg. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1033 | */ |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1034 | static void thread_combine_vc_sync(DrdThreadId tid, const Segment* sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1035 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1036 | const VectorClock* const vc = &sg->vc; |
| 1037 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1038 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1039 | && tid != DRD_INVALID_THREADID); |
| 1040 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1041 | tl_assert(sg); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1042 | tl_assert(vc); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1043 | |
| 1044 | if (tid != sg->tid) |
| 1045 | { |
| 1046 | VectorClock old_vc; |
| 1047 | |
| 1048 | DRD_(vc_copy)(&old_vc, &DRD_(g_threadinfo)[tid].last->vc); |
| 1049 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc); |
| 1050 | if (DRD_(sg_get_trace)()) |
| 1051 | { |
| 1052 | char *str1, *str2; |
| 1053 | str1 = DRD_(vc_aprint)(&old_vc); |
| 1054 | str2 = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1055 | VG_(message)(Vg_DebugMsg, "thread %d: vc %s -> %s\n", tid, str1, str2); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1056 | VG_(free)(str1); |
| 1057 | VG_(free)(str2); |
| 1058 | } |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1059 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1060 | thread_discard_ordered_segments(); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1061 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1062 | DRD_(thread_update_conflict_set)(tid, &old_vc); |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1063 | s_update_conflict_set_sync_count++; |
| 1064 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1065 | DRD_(vc_cleanup)(&old_vc); |
| 1066 | } |
| 1067 | else |
| 1068 | { |
| 1069 | tl_assert(DRD_(vc_lte)(vc, &DRD_(g_threadinfo)[tid].last->vc)); |
| 1070 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1071 | } |
| 1072 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1073 | /** |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1074 | * Create a new segment for thread tid and update the vector clock of the last |
| 1075 | * segment of this thread with the the vector clock of segment sg. Call this |
| 1076 | * function after thread tid had to wait because of thread synchronization |
| 1077 | * until the memory accesses in the segment sg finished. |
| 1078 | */ |
| 1079 | void DRD_(thread_new_segment_and_combine_vc)(DrdThreadId tid, const Segment* sg) |
| 1080 | { |
| 1081 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1082 | && tid != DRD_INVALID_THREADID); |
| 1083 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| 1084 | tl_assert(sg); |
| 1085 | |
| 1086 | thread_append_segment(tid, DRD_(sg_new)(tid, tid)); |
| 1087 | |
| 1088 | thread_combine_vc_sync(tid, sg); |
| 1089 | |
| 1090 | if (s_segment_merging |
| 1091 | && ++s_new_segments_since_last_merge >= s_segment_merge_interval) |
| 1092 | { |
| 1093 | thread_discard_ordered_segments(); |
| 1094 | thread_merge_segments(); |
| 1095 | } |
| 1096 | } |
| 1097 | |
| 1098 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1099 | * Call this function whenever a thread is no longer using the memory |
| 1100 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 1101 | * increase. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1102 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1103 | void DRD_(thread_stop_using_mem)(const Addr a1, const Addr a2) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1104 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1105 | DrdThreadId other_user; |
| 1106 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1107 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1108 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
| 1109 | other_user = DRD_INVALID_THREADID; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1110 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1111 | { |
| 1112 | Segment* p; |
| 1113 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1114 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1115 | if (other_user == DRD_INVALID_THREADID |
| 1116 | && i != DRD_(g_drd_running_tid)) |
| 1117 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1118 | if (UNLIKELY(DRD_(bm_test_and_clear)(DRD_(sg_bm)(p), a1, a2))) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1119 | { |
| 1120 | other_user = i; |
| 1121 | } |
| 1122 | continue; |
| 1123 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1124 | DRD_(bm_clear)(DRD_(sg_bm)(p), a1, a2); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1125 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1126 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1127 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1128 | /* |
| 1129 | * If any other thread had accessed memory in [ a1, a2 [, update the |
| 1130 | * conflict set. |
| 1131 | */ |
| 1132 | if (other_user != DRD_INVALID_THREADID |
| 1133 | && DRD_(bm_has_any_access)(DRD_(g_conflict_set), a1, a2)) |
| 1134 | { |
| 1135 | thread_compute_conflict_set(&DRD_(g_conflict_set), |
| 1136 | DRD_(thread_get_running_tid)()); |
| 1137 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1138 | } |
| 1139 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1140 | /** Specify whether memory loads should be recorded. */ |
| 1141 | void DRD_(thread_set_record_loads)(const DrdThreadId tid, const Bool enabled) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1142 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1143 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1144 | && tid != DRD_INVALID_THREADID); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1145 | tl_assert(enabled == !! enabled); |
| 1146 | |
| 1147 | DRD_(g_threadinfo)[tid].is_recording_loads = enabled; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1148 | } |
| 1149 | |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1150 | /** Specify whether memory stores should be recorded. */ |
| 1151 | void DRD_(thread_set_record_stores)(const DrdThreadId tid, const Bool enabled) |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1152 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1153 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1154 | && tid != DRD_INVALID_THREADID); |
bart | d45d995 | 2009-05-31 18:53:54 +0000 | [diff] [blame] | 1155 | tl_assert(enabled == !! enabled); |
| 1156 | |
| 1157 | DRD_(g_threadinfo)[tid].is_recording_stores = enabled; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1160 | /** |
| 1161 | * Print the segment information for all threads. |
| 1162 | * |
| 1163 | * This function is only used for debugging purposes. |
| 1164 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1165 | void DRD_(thread_print_all)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1166 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1167 | unsigned i; |
| 1168 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1169 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1170 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1171 | { |
| 1172 | if (DRD_(g_threadinfo)[i].first) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1173 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1174 | VG_(printf)("**************\n" |
| 1175 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
| 1176 | "**************\n", |
| 1177 | i, |
| 1178 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 1179 | DRD_(g_threadinfo)[i].vg_threadid, |
| 1180 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 1181 | DRD_(g_threadinfo)[i].pt_threadid, |
| 1182 | DRD_(g_threadinfo)[i].detached_posix_thread); |
| 1183 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
| 1184 | { |
| 1185 | DRD_(sg_print)(p); |
| 1186 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1187 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1188 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1189 | } |
| 1190 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1191 | /** Show a call stack involved in a data race. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1192 | static void show_call_stack(const DrdThreadId tid, |
| 1193 | const Char* const msg, |
| 1194 | ExeContext* const callstack) |
| 1195 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1196 | const ThreadId vg_tid = DRD_(DrdThreadIdToVgThreadId)(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1197 | |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 1198 | VG_(message)(Vg_UserMsg, "%s (thread %d)\n", msg, tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1199 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1200 | if (vg_tid != VG_INVALID_THREADID) |
| 1201 | { |
| 1202 | if (callstack) |
| 1203 | { |
| 1204 | VG_(pp_ExeContext)(callstack); |
| 1205 | } |
| 1206 | else |
| 1207 | { |
| 1208 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 1209 | } |
| 1210 | } |
| 1211 | else |
| 1212 | { |
| 1213 | VG_(message)(Vg_UserMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1214 | " (thread finished, call stack no longer available)\n"); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1215 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1216 | } |
| 1217 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1218 | /** Print information about the segments involved in a data race. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1219 | static void |
| 1220 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 1221 | const Addr addr, |
| 1222 | const SizeT size, |
| 1223 | const BmAccessTypeT access_type, |
| 1224 | const Segment* const p) |
| 1225 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1226 | unsigned i; |
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); |
| 1230 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1231 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1232 | for (i = 0; i < DRD_N_THREADS; i++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1233 | { |
| 1234 | if (i != tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1235 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1236 | Segment* q; |
| 1237 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
| 1238 | { |
| 1239 | /* |
| 1240 | * Since q iterates over the segments of thread i in order of |
| 1241 | * decreasing vector clocks, if q->vc <= p->vc, then |
| 1242 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 1243 | * loop once this condition is met. |
| 1244 | */ |
| 1245 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
| 1246 | break; |
| 1247 | if (! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1248 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1249 | if (DRD_(bm_has_conflict_with)(DRD_(sg_bm)(q), addr, addr + size, |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1250 | access_type)) |
| 1251 | { |
| 1252 | tl_assert(q->stacktrace); |
| 1253 | show_call_stack(i, "Other segment start", |
| 1254 | q->stacktrace); |
| 1255 | show_call_stack(i, "Other segment end", |
| 1256 | q->next ? q->next->stacktrace : 0); |
| 1257 | } |
| 1258 | } |
| 1259 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1260 | } |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1261 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1264 | /** Print information about all segments involved in a data race. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1265 | void DRD_(thread_report_conflicting_segments)(const DrdThreadId tid, |
| 1266 | const Addr addr, |
| 1267 | const SizeT size, |
| 1268 | const BmAccessTypeT access_type) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1269 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1270 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1271 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1272 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1273 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1274 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1275 | for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next) |
| 1276 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1277 | if (DRD_(bm_has)(DRD_(sg_bm)(p), addr, addr + size, access_type)) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1278 | { |
| 1279 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1280 | access_type, p); |
| 1281 | } |
| 1282 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1283 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1284 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame] | 1285 | /** |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1286 | * Verify whether the conflict set for thread tid is up to date. Only perform |
| 1287 | * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set. |
| 1288 | */ |
| 1289 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid) |
| 1290 | { |
| 1291 | static int do_verify_conflict_set = -1; |
| 1292 | Bool result; |
| 1293 | struct bitmap* computed_conflict_set = 0; |
| 1294 | |
| 1295 | if (do_verify_conflict_set < 0) |
| 1296 | do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0; |
| 1297 | |
| 1298 | if (do_verify_conflict_set == 0) |
| 1299 | return True; |
| 1300 | |
| 1301 | thread_compute_conflict_set(&computed_conflict_set, tid); |
| 1302 | result = DRD_(bm_equal)(DRD_(g_conflict_set), computed_conflict_set); |
| 1303 | if (! result) |
| 1304 | { |
| 1305 | VG_(printf)("actual conflict set:\n"); |
| 1306 | DRD_(bm_print)(DRD_(g_conflict_set)); |
| 1307 | VG_(printf)("\n"); |
| 1308 | VG_(printf)("computed conflict set:\n"); |
| 1309 | DRD_(bm_print)(computed_conflict_set); |
| 1310 | VG_(printf)("\n"); |
| 1311 | } |
| 1312 | DRD_(bm_delete)(computed_conflict_set); |
| 1313 | return result; |
| 1314 | } |
| 1315 | |
| 1316 | /** |
| 1317 | * Compute the conflict set: a bitmap that represents the union of all memory |
| 1318 | * accesses of all segments that are unordered to the current segment of the |
| 1319 | * thread tid. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1320 | */ |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1321 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 1322 | const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1323 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1324 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1325 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1326 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1327 | && tid != DRD_INVALID_THREADID); |
| 1328 | tl_assert(tid == DRD_(g_drd_running_tid)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1329 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1330 | s_compute_conflict_set_count++; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1331 | s_conflict_set_bitmap_creation_count |
| 1332 | -= DRD_(bm_get_bitmap_creation_count)(); |
| 1333 | s_conflict_set_bitmap2_creation_count |
| 1334 | -= DRD_(bm_get_bitmap2_creation_count)(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1335 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1336 | if (*conflict_set) |
| 1337 | { |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1338 | DRD_(bm_cleanup)(*conflict_set); |
| 1339 | DRD_(bm_init)(*conflict_set); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1340 | } |
bart | f6ec1fe | 2009-06-21 18:07:35 +0000 | [diff] [blame] | 1341 | else |
| 1342 | { |
| 1343 | *conflict_set = DRD_(bm_new)(); |
| 1344 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1345 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1346 | if (s_trace_conflict_set) |
| 1347 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1348 | char* str; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1349 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1350 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
| 1351 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 1352 | "computing conflict set for thread %d with vc %s\n", |
| 1353 | tid, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1354 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1355 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1356 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1357 | p = DRD_(g_threadinfo)[tid].last; |
| 1358 | { |
| 1359 | unsigned j; |
| 1360 | |
| 1361 | if (s_trace_conflict_set) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1362 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1363 | char* vc; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1364 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1365 | vc = DRD_(vc_aprint)(&p->vc); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1366 | VG_(message)(Vg_DebugMsg, "conflict set: thread [%d] at vc %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1367 | tid, vc); |
| 1368 | VG_(free)(vc); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1369 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1370 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1371 | for (j = 0; j < DRD_N_THREADS; j++) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1372 | { |
| 1373 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| 1374 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1375 | Segment* q; |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1376 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1377 | { |
| 1378 | if (! DRD_(vc_lte)(&q->vc, &p->vc) |
| 1379 | && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1380 | { |
| 1381 | if (s_trace_conflict_set) |
| 1382 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1383 | char* str; |
| 1384 | |
| 1385 | str = DRD_(vc_aprint)(&q->vc); |
| 1386 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1387 | "conflict set: [%d] merging segment %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1388 | j, str); |
| 1389 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1390 | } |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1391 | DRD_(bm_merge2)(*conflict_set, DRD_(sg_bm)(q)); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1392 | } |
| 1393 | else |
| 1394 | { |
| 1395 | if (s_trace_conflict_set) |
| 1396 | { |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1397 | char* str; |
| 1398 | |
| 1399 | str = DRD_(vc_aprint)(&q->vc); |
| 1400 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1401 | "conflict set: [%d] ignoring segment %s\n", |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1402 | j, str); |
| 1403 | VG_(free)(str); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1404 | } |
| 1405 | } |
| 1406 | } |
| 1407 | } |
| 1408 | } |
| 1409 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1410 | |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1411 | s_conflict_set_bitmap_creation_count |
| 1412 | += DRD_(bm_get_bitmap_creation_count)(); |
| 1413 | s_conflict_set_bitmap2_creation_count |
| 1414 | += DRD_(bm_get_bitmap2_creation_count)(); |
| 1415 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1416 | if (s_trace_conflict_set_bm) |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1417 | { |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1418 | VG_(message)(Vg_DebugMsg, "[%d] new conflict set:\n", tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1419 | DRD_(bm_print)(*conflict_set); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1420 | VG_(message)(Vg_DebugMsg, "[%d] end of new conflict set.\n", tid); |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1421 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1424 | /** |
| 1425 | * Update the conflict set after the vector clock of thread tid has been |
| 1426 | * updated from old_vc to its current value, either because a new segment has |
| 1427 | * been created or because of a synchronization operation. |
| 1428 | */ |
| 1429 | void DRD_(thread_update_conflict_set)(const DrdThreadId tid, |
| 1430 | const VectorClock* const old_vc) |
| 1431 | { |
| 1432 | const VectorClock* new_vc; |
| 1433 | Segment* p; |
| 1434 | unsigned j; |
| 1435 | |
| 1436 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1437 | && tid != DRD_INVALID_THREADID); |
| 1438 | tl_assert(old_vc); |
| 1439 | tl_assert(tid == DRD_(g_drd_running_tid)); |
| 1440 | tl_assert(DRD_(g_conflict_set)); |
| 1441 | |
| 1442 | if (s_trace_conflict_set) |
| 1443 | { |
| 1444 | char* str; |
| 1445 | |
| 1446 | str = DRD_(vc_aprint)(&DRD_(g_threadinfo)[tid].last->vc); |
| 1447 | VG_(message)(Vg_DebugMsg, |
bart | 63c92ea | 2009-07-19 17:53:56 +0000 | [diff] [blame] | 1448 | "updating conflict set for thread %d with vc %s\n", |
| 1449 | tid, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1450 | VG_(free)(str); |
| 1451 | } |
| 1452 | |
| 1453 | new_vc = &DRD_(g_threadinfo)[tid].last->vc; |
| 1454 | |
| 1455 | DRD_(bm_unmark)(DRD_(g_conflict_set)); |
| 1456 | |
| 1457 | for (j = 0; j < DRD_N_THREADS; j++) |
| 1458 | { |
| 1459 | Segment* q; |
| 1460 | |
| 1461 | if (j == tid || ! DRD_(IsValidDrdThreadId)(j)) |
| 1462 | continue; |
| 1463 | |
| 1464 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1465 | { |
| 1466 | const int included_in_old_conflict_set |
| 1467 | = ! DRD_(vc_lte)(&q->vc, old_vc) |
| 1468 | && ! DRD_(vc_lte)(old_vc, &q->vc); |
| 1469 | const int included_in_new_conflict_set |
| 1470 | = ! DRD_(vc_lte)(&q->vc, new_vc) |
| 1471 | && ! DRD_(vc_lte)(new_vc, &q->vc); |
| 1472 | if (included_in_old_conflict_set != included_in_new_conflict_set) |
| 1473 | { |
| 1474 | if (s_trace_conflict_set) |
| 1475 | { |
| 1476 | char* str; |
| 1477 | |
| 1478 | str = DRD_(vc_aprint)(&q->vc); |
| 1479 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1480 | "conflict set: [%d] merging segment %s\n", j, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1481 | VG_(free)(str); |
| 1482 | } |
| 1483 | DRD_(bm_mark)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
| 1484 | } |
| 1485 | else |
| 1486 | { |
| 1487 | if (s_trace_conflict_set) |
| 1488 | { |
| 1489 | char* str; |
| 1490 | |
| 1491 | str = DRD_(vc_aprint)(&q->vc); |
| 1492 | VG_(message)(Vg_DebugMsg, |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1493 | "conflict set: [%d] ignoring segment %s\n", j, str); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1494 | VG_(free)(str); |
| 1495 | } |
| 1496 | } |
| 1497 | } |
| 1498 | } |
| 1499 | |
| 1500 | DRD_(bm_clear_marked)(DRD_(g_conflict_set)); |
| 1501 | |
| 1502 | p = DRD_(g_threadinfo)[tid].last; |
| 1503 | { |
| 1504 | for (j = 0; j < DRD_N_THREADS; j++) |
| 1505 | { |
| 1506 | if (j != tid && DRD_(IsValidDrdThreadId)(j)) |
| 1507 | { |
| 1508 | Segment* q; |
| 1509 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
| 1510 | { |
| 1511 | if (! DRD_(vc_lte)(&q->vc, &p->vc) |
| 1512 | && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
| 1513 | { |
| 1514 | DRD_(bm_merge2_marked)(DRD_(g_conflict_set), DRD_(sg_bm)(q)); |
| 1515 | } |
| 1516 | } |
| 1517 | } |
| 1518 | } |
| 1519 | } |
| 1520 | |
| 1521 | DRD_(bm_remove_cleared_marked)(DRD_(g_conflict_set)); |
| 1522 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1523 | s_update_conflict_set_count++; |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1524 | |
| 1525 | if (s_trace_conflict_set_bm) |
| 1526 | { |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1527 | VG_(message)(Vg_DebugMsg, "[%d] updated conflict set:\n", tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1528 | DRD_(bm_print)(DRD_(g_conflict_set)); |
sewardj | 1e29ebc | 2009-07-15 14:49:17 +0000 | [diff] [blame] | 1529 | VG_(message)(Vg_DebugMsg, "[%d] end of updated conflict set.\n", tid); |
bart | 8f822af | 2009-06-08 18:20:42 +0000 | [diff] [blame] | 1530 | } |
| 1531 | |
| 1532 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
| 1533 | } |
| 1534 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1535 | /** Report the number of context switches performed. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1536 | ULong DRD_(thread_get_context_switch_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1537 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1538 | return s_context_switch_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1539 | } |
| 1540 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1541 | /** Report the number of ordered segments that have been discarded. */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1542 | ULong DRD_(thread_get_discard_ordered_segments_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1543 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1544 | return s_discard_ordered_segments_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1545 | } |
| 1546 | |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1547 | /** Return how many times the conflict set has been updated entirely. */ |
| 1548 | ULong DRD_(thread_get_compute_conflict_set_count)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1549 | { |
bart | 54803fe | 2009-06-21 09:26:27 +0000 | [diff] [blame] | 1550 | return s_compute_conflict_set_count; |
| 1551 | } |
| 1552 | |
| 1553 | /** Return how many times the conflict set has been updated partially. */ |
| 1554 | ULong DRD_(thread_get_update_conflict_set_count)(void) |
| 1555 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1556 | return s_update_conflict_set_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1557 | } |
| 1558 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1559 | /** |
bart | e521466 | 2009-06-21 11:51:23 +0000 | [diff] [blame] | 1560 | * Return how many times the conflict set has been updated partially |
| 1561 | * because a new segment has been created. |
| 1562 | */ |
| 1563 | ULong DRD_(thread_get_update_conflict_set_new_sg_count)(void) |
| 1564 | { |
| 1565 | return s_update_conflict_set_new_sg_count; |
| 1566 | } |
| 1567 | |
| 1568 | /** |
| 1569 | * Return how many times the conflict set has been updated partially |
| 1570 | * because of combining vector clocks due to synchronization operations |
| 1571 | * other than reader/writer lock or barrier operations. |
| 1572 | */ |
| 1573 | ULong DRD_(thread_get_update_conflict_set_sync_count)(void) |
| 1574 | { |
| 1575 | return s_update_conflict_set_sync_count; |
| 1576 | } |
| 1577 | |
| 1578 | /** |
| 1579 | * Return how many times the conflict set has been updated partially |
| 1580 | * because of thread joins. |
| 1581 | */ |
| 1582 | ULong DRD_(thread_get_update_conflict_set_join_count)(void) |
| 1583 | { |
| 1584 | return s_update_conflict_set_join_count; |
| 1585 | } |
| 1586 | |
| 1587 | /** |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1588 | * Return the number of first-level bitmaps that have been created during |
| 1589 | * conflict set updates. |
| 1590 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1591 | ULong DRD_(thread_get_conflict_set_bitmap_creation_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1592 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1593 | return s_conflict_set_bitmap_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1594 | } |
| 1595 | |
bart | 86a87df | 2009-03-04 19:26:47 +0000 | [diff] [blame] | 1596 | /** |
| 1597 | * Return the number of second-level bitmaps that have been created during |
| 1598 | * conflict set updates. |
| 1599 | */ |
bart | 62a784c | 2009-02-15 13:11:14 +0000 | [diff] [blame] | 1600 | ULong DRD_(thread_get_conflict_set_bitmap2_creation_count)(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1601 | { |
bart | bedfd23 | 2009-03-26 19:07:15 +0000 | [diff] [blame] | 1602 | return s_conflict_set_bitmap2_creation_count; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1603 | } |