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