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