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