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