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