sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1 | /* |
| 2 | This file is part of drd, a data race detector. |
| 3 | |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 4 | Copyright (C) 2006-2008 Bart Van Assche |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 5 | bart.vanassche@gmail.com |
| 6 | |
| 7 | This program is free software; you can redistribute it and/or |
| 8 | modify it under the terms of the GNU General Public License as |
| 9 | published by the Free Software Foundation; either version 2 of the |
| 10 | License, or (at your option) any later version. |
| 11 | |
| 12 | This program is distributed in the hope that it will be useful, but |
| 13 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | General Public License for more details. |
| 16 | |
| 17 | You should have received a copy of the GNU General Public License |
| 18 | along with this program; if not, write to the Free Software |
| 19 | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
| 20 | 02111-1307, USA. |
| 21 | |
| 22 | The GNU General Public License is contained in the file COPYING. |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | #include "drd_error.h" |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 27 | #include "drd_barrier.h" |
| 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 |
| 36 | #include "pub_tool_errormgr.h" // VG_(unique_error)() |
| 37 | #include "pub_tool_libcassert.h" // tl_assert() |
| 38 | #include "pub_tool_libcbase.h" // VG_(strlen)() |
| 39 | #include "pub_tool_libcprint.h" // VG_(printf)() |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 40 | #include "pub_tool_libcproc.h" // VG_(getenv)() |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 41 | #include "pub_tool_machine.h" |
| 42 | #include "pub_tool_mallocfree.h" // VG_(malloc)(), VG_(free)() |
sewardj | 8564292 | 2008-01-14 11:54:56 +0000 | [diff] [blame] | 43 | #include "pub_tool_options.h" // VG_(clo_backtrace_size) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 44 | #include "pub_tool_threadstate.h" // VG_(get_pthread_id)() |
| 45 | |
bart | 32ba208 | 2008-06-05 08:53:42 +0000 | [diff] [blame] | 46 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 47 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 48 | /* Local functions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 49 | |
| 50 | static void thread_append_segment(const DrdThreadId tid, |
| 51 | Segment* const sg); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 52 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg); |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 53 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid); |
| 54 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 55 | const DrdThreadId tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 56 | |
| 57 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 58 | /* Local variables. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 59 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 60 | static ULong DRD_(s_context_switch_count); |
| 61 | static ULong DRD_(s_discard_ordered_segments_count); |
| 62 | static ULong DRD_(s_update_conflict_set_count); |
| 63 | static ULong DRD_(s_conflict_set_new_segment_count); |
| 64 | static ULong DRD_(s_conflict_set_combine_vc_count); |
| 65 | static ULong DRD_(s_conflict_set_bitmap_creation_count); |
| 66 | static ULong DRD_(s_conflict_set_bitmap2_creation_count); |
| 67 | static ThreadId DRD_(s_vg_running_tid) = VG_INVALID_THREADID; |
| 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); |
| 71 | static Bool DRD_(s_trace_context_switches) = False; |
| 72 | static Bool DRD_(s_trace_conflict_set) = False; |
| 73 | static Bool DRD_(s_trace_fork_join) = False; |
| 74 | static Bool DRD_(s_segment_merging) = True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 75 | |
| 76 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 77 | /* Function definitions. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 78 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 79 | void thread_trace_context_switches(const Bool t) |
| 80 | { |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 81 | tl_assert(t == False || t == True); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 82 | DRD_(s_trace_context_switches) = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 83 | } |
| 84 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 85 | void thread_trace_conflict_set(const Bool t) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 86 | { |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 87 | tl_assert(t == False || t == True); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 88 | DRD_(s_trace_conflict_set) = t; |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 89 | } |
| 90 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 91 | Bool DRD_(thread_get_trace_fork_join)(void) |
| 92 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 93 | return DRD_(s_trace_fork_join); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | void DRD_(thread_set_trace_fork_join)(const Bool t) |
| 97 | { |
| 98 | tl_assert(t == False || t == True); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 99 | DRD_(s_trace_fork_join) = t; |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 100 | } |
| 101 | |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 102 | void thread_set_segment_merging(const Bool m) |
| 103 | { |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 104 | tl_assert(m == False || m == True); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 105 | DRD_(s_segment_merging) = m; |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 106 | } |
| 107 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Convert Valgrind's ThreadId into a DrdThreadId. Report failure if |
| 110 | * Valgrind's ThreadId does not yet exist. |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 111 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 112 | DrdThreadId VgThreadIdToDrdThreadId(const ThreadId tid) |
| 113 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 114 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 115 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 116 | if (tid == VG_INVALID_THREADID) |
| 117 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 118 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 119 | for (i = 1; i < DRD_N_THREADS; i++) |
| 120 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 121 | if (DRD_(g_threadinfo)[i].vg_thread_exists == True |
| 122 | && DRD_(g_threadinfo)[i].vg_threadid == tid) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 123 | { |
| 124 | return i; |
| 125 | } |
| 126 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 127 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 128 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | static |
| 132 | DrdThreadId VgThreadIdToNewDrdThreadId(const ThreadId tid) |
| 133 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 134 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 135 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 136 | tl_assert(VgThreadIdToDrdThreadId(tid) == DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 137 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 138 | for (i = 1; i < DRD_N_THREADS; i++) |
| 139 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 140 | if (DRD_(g_threadinfo)[i].vg_thread_exists == False |
| 141 | && DRD_(g_threadinfo)[i].posix_thread_exists == False |
| 142 | && DRD_(g_threadinfo)[i].detached_posix_thread == False) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 143 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 144 | DRD_(g_threadinfo)[i].vg_thread_exists = True; |
| 145 | DRD_(g_threadinfo)[i].vg_threadid = tid; |
| 146 | DRD_(g_threadinfo)[i].pt_threadid = INVALID_POSIX_THREADID; |
| 147 | DRD_(g_threadinfo)[i].stack_min = 0; |
| 148 | DRD_(g_threadinfo)[i].stack_min_min = 0; |
| 149 | DRD_(g_threadinfo)[i].stack_startup = 0; |
| 150 | DRD_(g_threadinfo)[i].stack_max = 0; |
| 151 | DRD_(g_threadinfo)[i].is_recording = True; |
| 152 | DRD_(g_threadinfo)[i].synchr_nesting = 0; |
| 153 | if (DRD_(g_threadinfo)[i].first != 0) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 154 | VG_(printf)("drd thread id = %d\n", i); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 155 | tl_assert(DRD_(g_threadinfo)[i].first == 0); |
| 156 | tl_assert(DRD_(g_threadinfo)[i].last == 0); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 157 | return i; |
| 158 | } |
| 159 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 160 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 161 | tl_assert(False); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 162 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 163 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | DrdThreadId PtThreadIdToDrdThreadId(const PThreadId tid) |
| 167 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 168 | int i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 169 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 170 | tl_assert(tid != INVALID_POSIX_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 171 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 172 | for (i = 1; i < DRD_N_THREADS; i++) |
| 173 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 174 | if (DRD_(g_threadinfo)[i].posix_thread_exists |
| 175 | && DRD_(g_threadinfo)[i].pt_threadid == tid) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 176 | { |
| 177 | return i; |
| 178 | } |
| 179 | } |
| 180 | return DRD_INVALID_THREADID; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | ThreadId DrdThreadIdToVgThreadId(const DrdThreadId tid) |
| 184 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 185 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 186 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 187 | return (DRD_(g_threadinfo)[tid].vg_thread_exists |
| 188 | ? DRD_(g_threadinfo)[tid].vg_threadid |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 189 | : VG_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 190 | } |
| 191 | |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 192 | #if 0 |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 193 | /** |
| 194 | * Sanity check of the doubly linked list of segments referenced by a |
| 195 | * ThreadInfo struct. |
| 196 | * @return True if sane, False if not. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 197 | */ |
| 198 | static Bool sane_ThreadInfo(const ThreadInfo* const ti) |
| 199 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 200 | Segment* p; |
| 201 | for (p = ti->first; p; p = p->next) { |
| 202 | if (p->next && p->next->prev != p) |
| 203 | return False; |
| 204 | if (p->next == 0 && p != ti->last) |
| 205 | return False; |
| 206 | } |
| 207 | for (p = ti->last; p; p = p->prev) { |
| 208 | if (p->prev && p->prev->next != p) |
| 209 | return False; |
| 210 | if (p->prev == 0 && p != ti->first) |
| 211 | return False; |
| 212 | } |
| 213 | return True; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 214 | } |
bart | 23d3a4e | 2008-04-05 12:53:00 +0000 | [diff] [blame] | 215 | #endif |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 216 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 217 | /** |
| 218 | * Create the first segment for a newly started thread. |
| 219 | * |
| 220 | * This function is called from the handler installed via |
| 221 | * VG_(track_pre_thread_ll_create)(). The Valgrind core invokes this handler |
| 222 | * from the context of the creator thread, before the new thread has been |
| 223 | * created. |
| 224 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 225 | DrdThreadId thread_pre_create(const DrdThreadId creator, |
| 226 | const ThreadId vg_created) |
| 227 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 228 | DrdThreadId created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 229 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 230 | tl_assert(VgThreadIdToDrdThreadId(vg_created) == DRD_INVALID_THREADID); |
| 231 | created = VgThreadIdToNewDrdThreadId(vg_created); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 232 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 233 | && created != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 234 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 235 | tl_assert(DRD_(g_threadinfo)[created].first == 0); |
| 236 | tl_assert(DRD_(g_threadinfo)[created].last == 0); |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 237 | thread_append_segment(created, DRD_(sg_new)(creator, created)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 238 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 239 | return created; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 240 | } |
| 241 | |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 242 | /** |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 243 | * Initialize DRD_(g_threadinfo)[] for a newly created thread. Must be called after |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 244 | * the thread has been created and before any client instructioins are run |
| 245 | * on the newly created thread, e.g. from the handler installed via |
| 246 | * VG_(track_pre_thread_first_insn)(). |
| 247 | */ |
| 248 | DrdThreadId thread_post_create(const ThreadId vg_created) |
| 249 | { |
| 250 | const DrdThreadId created = VgThreadIdToDrdThreadId(vg_created); |
| 251 | |
| 252 | tl_assert(0 <= (int)created && created < DRD_N_THREADS |
| 253 | && created != DRD_INVALID_THREADID); |
| 254 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 255 | DRD_(g_threadinfo)[created].stack_max = VG_(thread_get_stack_max)(vg_created); |
| 256 | DRD_(g_threadinfo)[created].stack_startup = DRD_(g_threadinfo)[created].stack_max; |
| 257 | DRD_(g_threadinfo)[created].stack_min = DRD_(g_threadinfo)[created].stack_max; |
| 258 | DRD_(g_threadinfo)[created].stack_min_min = DRD_(g_threadinfo)[created].stack_max; |
| 259 | DRD_(g_threadinfo)[created].stack_size = VG_(thread_get_stack_size)(vg_created); |
| 260 | tl_assert(DRD_(g_threadinfo)[created].stack_max != 0); |
bart | 439c55f | 2009-02-15 10:38:37 +0000 | [diff] [blame] | 261 | |
| 262 | return created; |
| 263 | } |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 264 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 265 | /** |
| 266 | * Process VG_USERREQ__POST_THREAD_JOIN. This client request is invoked just |
| 267 | * after thread drd_joiner joined thread drd_joinee. |
| 268 | */ |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 269 | void DRD_(thread_post_join)(DrdThreadId drd_joiner, DrdThreadId drd_joinee) |
| 270 | { |
| 271 | tl_assert(IsValidDrdThreadId(drd_joiner)); |
| 272 | tl_assert(IsValidDrdThreadId(drd_joinee)); |
| 273 | thread_new_segment(drd_joinee); |
| 274 | thread_combine_vc(drd_joiner, drd_joinee); |
| 275 | thread_new_segment(drd_joiner); |
| 276 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 277 | if (DRD_(s_trace_fork_join)) |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 278 | { |
| 279 | const ThreadId joiner = DrdThreadIdToVgThreadId(drd_joiner); |
| 280 | const ThreadId joinee = DrdThreadIdToVgThreadId(drd_joinee); |
| 281 | const unsigned msg_size = 256; |
| 282 | char* msg; |
| 283 | |
| 284 | msg = VG_(malloc)("drd.main.dptj.1", msg_size); |
| 285 | tl_assert(msg); |
| 286 | VG_(snprintf)(msg, msg_size, |
| 287 | "drd_post_thread_join joiner = %d/%d, joinee = %d/%d", |
| 288 | joiner, drd_joiner, joinee, drd_joinee); |
| 289 | if (joiner) |
| 290 | { |
| 291 | VG_(snprintf)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
| 292 | ", new vc: "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 293 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), msg_size - VG_(strlen)(msg), |
| 294 | thread_get_vc(drd_joiner)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 295 | } |
| 296 | VG_(message)(Vg_DebugMsg, "%s", msg); |
| 297 | VG_(free)(msg); |
| 298 | } |
| 299 | |
| 300 | if (! DRD_(get_check_stack_accesses)()) |
| 301 | { |
bart | 1335ecc | 2009-02-14 16:10:53 +0000 | [diff] [blame] | 302 | DRD_(finish_suppression)(thread_get_stack_max(drd_joinee) |
| 303 | - thread_get_stack_size(drd_joinee), |
| 304 | thread_get_stack_max(drd_joinee)); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 305 | } |
| 306 | thread_delete(drd_joinee); |
| 307 | mutex_thread_delete(drd_joinee); |
| 308 | cond_thread_delete(drd_joinee); |
| 309 | semaphore_thread_delete(drd_joinee); |
bart | a8cf765 | 2009-02-15 11:00:29 +0000 | [diff] [blame] | 310 | DRD_(barrier_thread_delete)(drd_joinee); |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 311 | } |
| 312 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 313 | /** |
| 314 | * NPTL hack: NPTL allocates the 'struct pthread' on top of the stack, |
| 315 | * and accesses this data structure from multiple threads without locking. |
| 316 | * Any conflicting accesses in the range stack_startup..stack_max will be |
| 317 | * ignored. |
| 318 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 319 | void thread_set_stack_startup(const DrdThreadId tid, const Addr stack_startup) |
| 320 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 321 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 322 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 323 | tl_assert(DRD_(g_threadinfo)[tid].stack_min <= stack_startup); |
| 324 | tl_assert(stack_startup <= DRD_(g_threadinfo)[tid].stack_max); |
| 325 | DRD_(g_threadinfo)[tid].stack_startup = stack_startup; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | Addr thread_get_stack_min(const DrdThreadId tid) |
| 329 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 330 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 331 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 332 | return DRD_(g_threadinfo)[tid].stack_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 333 | } |
| 334 | |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 335 | Addr thread_get_stack_min_min(const DrdThreadId tid) |
| 336 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 337 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 338 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 339 | return DRD_(g_threadinfo)[tid].stack_min_min; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 340 | } |
| 341 | |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 342 | Addr thread_get_stack_max(const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 343 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 344 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 345 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 346 | return DRD_(g_threadinfo)[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 347 | } |
| 348 | |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 349 | SizeT thread_get_stack_size(const DrdThreadId tid) |
| 350 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 351 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 352 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 353 | return DRD_(g_threadinfo)[tid].stack_size; |
bart | cac5346 | 2008-03-29 09:27:08 +0000 | [diff] [blame] | 354 | } |
| 355 | |
bart | 09dc13f | 2009-02-14 15:13:31 +0000 | [diff] [blame] | 356 | /** |
| 357 | * Clean up thread-specific data structures. Call this just after |
| 358 | * pthread_join(). |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 359 | */ |
| 360 | void thread_delete(const DrdThreadId tid) |
| 361 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 362 | Segment* sg; |
| 363 | Segment* sg_prev; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 364 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 365 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 366 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 367 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 0); |
| 368 | for (sg = DRD_(g_threadinfo)[tid].last; sg; sg = sg_prev) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 369 | { |
| 370 | sg_prev = sg->prev; |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 371 | sg->prev = 0; |
| 372 | sg->next = 0; |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 373 | DRD_(sg_put)(sg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 374 | } |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 375 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
| 376 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
| 377 | tl_assert(DRD_(g_threadinfo)[tid].detached_posix_thread == False); |
| 378 | DRD_(g_threadinfo)[tid].first = 0; |
| 379 | DRD_(g_threadinfo)[tid].last = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 380 | } |
| 381 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 382 | /** |
| 383 | * Called after a thread performed its last memory access and before |
| 384 | * thread_delete() is called. Note: thread_delete() is only called for |
| 385 | * joinable threads, not for detached threads. |
| 386 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 387 | void thread_finished(const DrdThreadId tid) |
| 388 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 389 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 390 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 391 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 392 | DRD_(g_threadinfo)[tid].vg_thread_exists = False; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 393 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 394 | if (DRD_(g_threadinfo)[tid].detached_posix_thread) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 395 | { |
| 396 | /* Once a detached thread has finished, its stack is deallocated and */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 397 | /* should no longer be taken into account when computing the conflict set*/ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 398 | DRD_(g_threadinfo)[tid].stack_min = DRD_(g_threadinfo)[tid].stack_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 399 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 400 | /* For a detached thread, calling pthread_exit() invalidates the */ |
| 401 | /* POSIX thread ID associated with the detached thread. For joinable */ |
| 402 | /* POSIX threads however, the POSIX thread ID remains live after the */ |
| 403 | /* pthread_exit() call until pthread_join() is called. */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 404 | DRD_(g_threadinfo)[tid].posix_thread_exists = False; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 405 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 406 | } |
| 407 | |
bart | 9b2974a | 2008-09-27 12:35:31 +0000 | [diff] [blame] | 408 | /** Called just before pthread_cancel(). */ |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 409 | void thread_pre_cancel(const DrdThreadId tid) |
| 410 | { |
| 411 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 412 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 413 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 414 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 415 | DRD_(g_threadinfo)[tid].synchr_nesting = 0; |
bart | af0691b | 2008-09-27 12:26:50 +0000 | [diff] [blame] | 416 | } |
| 417 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 418 | void thread_set_pthreadid(const DrdThreadId tid, const PThreadId ptid) |
| 419 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 420 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 421 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 422 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid == INVALID_POSIX_THREADID); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 423 | tl_assert(ptid != INVALID_POSIX_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 424 | DRD_(g_threadinfo)[tid].posix_thread_exists = True; |
| 425 | DRD_(g_threadinfo)[tid].pt_threadid = ptid; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | Bool thread_get_joinable(const DrdThreadId tid) |
| 429 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 430 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 431 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 432 | return ! DRD_(g_threadinfo)[tid].detached_posix_thread; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void thread_set_joinable(const DrdThreadId tid, const Bool joinable) |
| 436 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 437 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 438 | && tid != DRD_INVALID_THREADID); |
| 439 | tl_assert(!! joinable == joinable); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 440 | tl_assert(DRD_(g_threadinfo)[tid].pt_threadid != INVALID_POSIX_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 441 | #if 0 |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 442 | VG_(message)(Vg_DebugMsg, |
| 443 | "thread_set_joinable(%d/%d, %s)", |
| 444 | tid, |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 445 | DRD_(g_threadinfo)[tid].vg_threadid, |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 446 | joinable ? "joinable" : "detached"); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 447 | #endif |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 448 | DRD_(g_threadinfo)[tid].detached_posix_thread = ! joinable; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 449 | } |
| 450 | |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 451 | void thread_set_vg_running_tid(const ThreadId vg_tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 452 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 453 | tl_assert(vg_tid != VG_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 454 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 455 | if (vg_tid != DRD_(s_vg_running_tid)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 456 | { |
| 457 | thread_set_running_tid(vg_tid, VgThreadIdToDrdThreadId(vg_tid)); |
| 458 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 459 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 460 | tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID); |
| 461 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | void thread_set_running_tid(const ThreadId vg_tid, const DrdThreadId drd_tid) |
| 465 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 466 | tl_assert(vg_tid != VG_INVALID_THREADID); |
| 467 | tl_assert(drd_tid != DRD_INVALID_THREADID); |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 468 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 469 | if (vg_tid != DRD_(s_vg_running_tid)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 470 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 471 | if (DRD_(s_trace_context_switches) |
| 472 | && DRD_(g_drd_running_tid) != DRD_INVALID_THREADID) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 473 | { |
| 474 | VG_(message)(Vg_DebugMsg, |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 475 | "Context switch from thread %d/%d to thread %d/%d;" |
| 476 | " segments: %llu", |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 477 | DRD_(s_vg_running_tid), DRD_(g_drd_running_tid), |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 478 | DrdThreadIdToVgThreadId(drd_tid), drd_tid, |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 479 | DRD_(sg_get_segments_alive_count)()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 480 | } |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 481 | DRD_(s_vg_running_tid) = vg_tid; |
| 482 | DRD_(g_drd_running_tid) = drd_tid; |
| 483 | thread_compute_conflict_set(&DRD_(g_conflict_set), drd_tid); |
| 484 | DRD_(s_context_switch_count)++; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 485 | } |
sewardj | 8b09d4f | 2007-12-04 21:27:18 +0000 | [diff] [blame] | 486 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 487 | tl_assert(DRD_(s_vg_running_tid) != VG_INVALID_THREADID); |
| 488 | tl_assert(DRD_(g_drd_running_tid) != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 489 | } |
| 490 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 491 | int thread_enter_synchr(const DrdThreadId tid) |
| 492 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 493 | tl_assert(IsValidDrdThreadId(tid)); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 494 | return DRD_(g_threadinfo)[tid].synchr_nesting++; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 495 | } |
| 496 | |
| 497 | int thread_leave_synchr(const DrdThreadId tid) |
| 498 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 499 | tl_assert(IsValidDrdThreadId(tid)); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 500 | tl_assert(DRD_(g_threadinfo)[tid].synchr_nesting >= 1); |
| 501 | return --DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| 504 | int thread_get_synchr_nesting_count(const DrdThreadId tid) |
| 505 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 506 | tl_assert(IsValidDrdThreadId(tid)); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 507 | return DRD_(g_threadinfo)[tid].synchr_nesting; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 508 | } |
| 509 | |
bart | 1a473c7 | 2008-03-13 19:03:38 +0000 | [diff] [blame] | 510 | /** Append a new segment at the end of the segment list. */ |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 511 | static void thread_append_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 512 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 513 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 514 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 515 | // tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[tid])); |
| 516 | sg->prev = DRD_(g_threadinfo)[tid].last; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 517 | sg->next = 0; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 518 | if (DRD_(g_threadinfo)[tid].last) |
| 519 | DRD_(g_threadinfo)[tid].last->next = sg; |
| 520 | DRD_(g_threadinfo)[tid].last = sg; |
| 521 | if (DRD_(g_threadinfo)[tid].first == 0) |
| 522 | DRD_(g_threadinfo)[tid].first = sg; |
| 523 | // tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[tid])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 524 | } |
| 525 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 526 | /** |
| 527 | * Remove a segment from the segment list of thread threadid, and free the |
| 528 | * associated memory. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 529 | */ |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 530 | static void thread_discard_segment(const DrdThreadId tid, Segment* const sg) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 531 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 532 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 533 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 534 | //tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[tid])); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 535 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 536 | if (sg->prev) |
| 537 | sg->prev->next = sg->next; |
| 538 | if (sg->next) |
| 539 | sg->next->prev = sg->prev; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 540 | if (sg == DRD_(g_threadinfo)[tid].first) |
| 541 | DRD_(g_threadinfo)[tid].first = sg->next; |
| 542 | if (sg == DRD_(g_threadinfo)[tid].last) |
| 543 | DRD_(g_threadinfo)[tid].last = sg->prev; |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 544 | DRD_(sg_put)(sg); |
bart | 3f74967 | 2008-03-22 09:49:40 +0000 | [diff] [blame] | 545 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 546 | //tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[tid])); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | VectorClock* thread_get_vc(const DrdThreadId tid) |
| 550 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 551 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 552 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 553 | tl_assert(DRD_(g_threadinfo)[tid].last); |
| 554 | return &DRD_(g_threadinfo)[tid].last->vc; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 555 | } |
| 556 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 557 | /** |
| 558 | * Return the latest segment of thread 'tid' and increment its reference count. |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 559 | */ |
| 560 | void thread_get_latest_segment(Segment** sg, const DrdThreadId tid) |
| 561 | { |
| 562 | tl_assert(sg); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 563 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 564 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 565 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 566 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 567 | DRD_(sg_put)(*sg); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 568 | *sg = DRD_(sg_get)(DRD_(g_threadinfo)[tid].last); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 569 | } |
| 570 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 571 | /** |
| 572 | * Compute the minimum of all latest vector clocks of all threads |
| 573 | * (Michiel Ronsse calls this "clock snooping" in his papers about DIOTA). |
| 574 | * @param vc pointer to a vectorclock, holds result upon return. |
| 575 | */ |
| 576 | static void thread_compute_minimum_vc(VectorClock* vc) |
| 577 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 578 | unsigned i; |
| 579 | Bool first; |
| 580 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 581 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 582 | first = True; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 583 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 584 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 585 | latest_sg = DRD_(g_threadinfo)[i].last; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 586 | if (latest_sg) |
| 587 | { |
| 588 | if (first) |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 589 | DRD_(vc_assign)(vc, &latest_sg->vc); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 590 | else |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 591 | DRD_(vc_min)(vc, &latest_sg->vc); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 592 | first = False; |
| 593 | } |
| 594 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 595 | } |
| 596 | |
| 597 | static void thread_compute_maximum_vc(VectorClock* vc) |
| 598 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 599 | unsigned i; |
| 600 | Bool first; |
| 601 | Segment* latest_sg; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 602 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 603 | first = True; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 604 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 605 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 606 | latest_sg = DRD_(g_threadinfo)[i].last; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 607 | if (latest_sg) |
| 608 | { |
| 609 | if (first) |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 610 | DRD_(vc_assign)(vc, &latest_sg->vc); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 611 | else |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 612 | DRD_(vc_combine)(vc, &latest_sg->vc); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 613 | first = False; |
| 614 | } |
| 615 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 616 | } |
| 617 | |
| 618 | /** |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 619 | * Discard all segments that have a defined order against the latest vector |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 620 | * clock of every thread -- these segments can no longer be involved in a |
| 621 | * data race. |
| 622 | */ |
| 623 | static void thread_discard_ordered_segments(void) |
| 624 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 625 | unsigned i; |
| 626 | VectorClock thread_vc_min; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 627 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 628 | DRD_(s_discard_ordered_segments_count)++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 629 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 630 | DRD_(vc_init)(&thread_vc_min, 0, 0); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 631 | thread_compute_minimum_vc(&thread_vc_min); |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 632 | if (DRD_(sg_get_trace)()) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 633 | { |
| 634 | char msg[256]; |
| 635 | VectorClock thread_vc_max; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 636 | |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 637 | DRD_(vc_init)(&thread_vc_max, 0, 0); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 638 | thread_compute_maximum_vc(&thread_vc_max); |
| 639 | VG_(snprintf)(msg, sizeof(msg), |
| 640 | "Discarding ordered segments -- min vc is "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 641 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 642 | &thread_vc_min); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 643 | VG_(snprintf)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 644 | ", max vc is "); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 645 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), sizeof(msg) - VG_(strlen)(msg), |
| 646 | &thread_vc_max); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 647 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 648 | DRD_(vc_cleanup)(&thread_vc_max); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 649 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 650 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 651 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 652 | { |
| 653 | Segment* sg; |
| 654 | Segment* sg_next; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 655 | for (sg = DRD_(g_threadinfo)[i].first; |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 656 | sg && (sg_next = sg->next) && DRD_(vc_lte)(&sg->vc, &thread_vc_min); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 657 | sg = sg_next) |
| 658 | { |
| 659 | thread_discard_segment(i, sg); |
| 660 | } |
| 661 | } |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 662 | DRD_(vc_cleanup)(&thread_vc_min); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 663 | } |
| 664 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 665 | /** |
| 666 | * Merge all segments that may be merged without triggering false positives |
| 667 | * or discarding real data races. For the theoretical background of segment |
| 668 | * merging, see also the following paper: |
| 669 | * Mark Christiaens, Michiel Ronsse and Koen De Bosschere. |
| 670 | * Bounding the number of segment histories during data race detection. |
| 671 | * Parallel Computing archive, Volume 28, Issue 9, pp 1221-1238, |
| 672 | * September 2002. |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 673 | */ |
| 674 | static void thread_merge_segments(void) |
| 675 | { |
| 676 | unsigned i; |
| 677 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 678 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 679 | { |
| 680 | Segment* sg; |
| 681 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 682 | // tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[i])); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 683 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 684 | for (sg = DRD_(g_threadinfo)[i].first; sg; sg = sg->next) |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 685 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 686 | if (DRD_(sg_get_refcnt)(sg) == 1 |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 687 | && sg->next |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 688 | && DRD_(sg_get_refcnt)(sg->next) == 1 |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 689 | && sg->next->next) |
| 690 | { |
| 691 | /* Merge sg and sg->next into sg. */ |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 692 | DRD_(sg_merge)(sg, sg->next); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 693 | thread_discard_segment(i, sg->next); |
| 694 | } |
| 695 | } |
| 696 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 697 | // tl_assert(sane_ThreadInfo(&DRD_(g_threadinfo)[i])); |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 698 | } |
| 699 | } |
| 700 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 701 | /** |
| 702 | * Every change in the vector clock of a thread may cause segments that |
| 703 | * were previously ordered to this thread to become unordered. Hence, |
| 704 | * it may be necessary to recalculate the conflict set if the vector clock |
| 705 | * of the current thread is updated. This function check whether such a |
| 706 | * recalculation is necessary. |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 707 | * |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 708 | * @param tid Thread ID of the thread to which a new segment has been |
| 709 | * appended. |
| 710 | * @param new_sg Pointer to the most recent segment of thread tid. |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 711 | */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 712 | static Bool conflict_set_update_needed(const DrdThreadId tid, |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 713 | const Segment* const new_sg) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 714 | { |
bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 715 | #if 0 |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 716 | unsigned i; |
| 717 | const Segment* old_sg; |
| 718 | |
| 719 | tl_assert(new_sg); |
| 720 | |
| 721 | /* If a new segment was added to another thread than the running thread, */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 722 | /* just tell the caller to update the conflict set. */ |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 723 | if (tid != DRD_(g_drd_running_tid)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 724 | return True; |
| 725 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 726 | /* Always let the caller update the conflict set after creation of the */ |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 727 | /* first segment. */ |
| 728 | old_sg = new_sg->prev; |
| 729 | if (old_sg == 0) |
| 730 | return True; |
| 731 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 732 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 733 | { |
| 734 | Segment* q; |
| 735 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 736 | if (i == DRD_(g_drd_running_tid)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 737 | continue; |
| 738 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 739 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 740 | { |
| 741 | /* If the expression below evaluates to false, this expression will */ |
| 742 | /* also evaluate to false for all subsequent iterations. So stop */ |
| 743 | /* iterating. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 744 | if (DRD_(vc_lte)(&q->vc, &old_sg->vc)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 745 | break; |
| 746 | /* If the vector clock of the 2nd the last segment is not ordered */ |
| 747 | /* to the vector clock of segment q, and the last segment is, ask */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 748 | /* the caller to update the conflict set. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 749 | if (! DRD_(vc_lte)(&old_sg->vc, &q->vc)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 750 | { |
| 751 | return True; |
| 752 | } |
| 753 | /* If the vector clock of the last segment is not ordered to the */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 754 | /* vector clock of segment q, ask the caller to update the conflict */ |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 755 | /* set. */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 756 | if (! DRD_(vc_lte)(&q->vc, &new_sg->vc) && ! DRD_(vc_lte)(&new_sg->vc, &q->vc)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 757 | { |
| 758 | return True; |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | return False; |
bart | 5d421ba | 2008-04-19 15:15:12 +0000 | [diff] [blame] | 764 | #else |
| 765 | return True; |
| 766 | #endif |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 767 | } |
| 768 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 769 | /** |
| 770 | * Create a new segment for the specified thread, and discard any segments |
| 771 | * that cannot cause races anymore. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 772 | */ |
| 773 | void thread_new_segment(const DrdThreadId tid) |
| 774 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 775 | Segment* new_sg; |
| 776 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 777 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 778 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 779 | |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 780 | new_sg = DRD_(sg_new)(tid, tid); |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 781 | thread_append_segment(tid, new_sg); |
| 782 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 783 | if (conflict_set_update_needed(tid, new_sg)) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 784 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 785 | thread_compute_conflict_set(&DRD_(g_conflict_set), |
| 786 | DRD_(g_drd_running_tid)); |
| 787 | DRD_(s_conflict_set_new_segment_count)++; |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 788 | } |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 789 | else if (tid == DRD_(g_drd_running_tid)) |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 790 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 791 | tl_assert(thread_conflict_set_up_to_date(DRD_(g_drd_running_tid))); |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 792 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 793 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 794 | thread_discard_ordered_segments(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 795 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 796 | if (DRD_(s_segment_merging)) |
| 797 | { |
bart | a9c3739 | 2008-03-22 09:38:48 +0000 | [diff] [blame] | 798 | thread_merge_segments(); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 799 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 800 | } |
| 801 | |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 802 | /** Call this function after thread 'joiner' joined thread 'joinee'. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 803 | void thread_combine_vc(DrdThreadId joiner, DrdThreadId joinee) |
| 804 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 805 | tl_assert(joiner != joinee); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 806 | tl_assert(0 <= (int)joiner && joiner < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 807 | && joiner != DRD_INVALID_THREADID); |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 808 | tl_assert(0 <= (int)joinee && joinee < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 809 | && joinee != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 810 | tl_assert(DRD_(g_threadinfo)[joiner].last); |
| 811 | tl_assert(DRD_(g_threadinfo)[joinee].last); |
| 812 | DRD_(vc_combine)(&DRD_(g_threadinfo)[joiner].last->vc, |
| 813 | &DRD_(g_threadinfo)[joinee].last->vc); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 814 | thread_discard_ordered_segments(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 815 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 816 | if (joiner == DRD_(g_drd_running_tid)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 817 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 818 | thread_compute_conflict_set(&DRD_(g_conflict_set), joiner); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 819 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 820 | } |
| 821 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 822 | /** |
| 823 | * Call this function after thread 'tid' had to wait because of thread |
| 824 | * synchronization until the memory accesses in the segment with vector clock |
| 825 | * 'vc' finished. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 826 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 827 | void thread_combine_vc2(DrdThreadId tid, const VectorClock* const vc) |
| 828 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 829 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 830 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 831 | tl_assert(DRD_(g_threadinfo)[tid].last); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 832 | tl_assert(vc); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 833 | DRD_(vc_combine)(&DRD_(g_threadinfo)[tid].last->vc, vc); |
| 834 | thread_compute_conflict_set(&DRD_(g_conflict_set), tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 835 | thread_discard_ordered_segments(); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 836 | DRD_(s_conflict_set_combine_vc_count)++; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 837 | } |
| 838 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 839 | /** |
| 840 | * Call this function whenever a thread is no longer using the memory |
| 841 | * [ a1, a2 [, e.g. because of a call to free() or a stack pointer |
| 842 | * increase. |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 843 | */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 844 | void thread_stop_using_mem(const Addr a1, const Addr a2) |
| 845 | { |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 846 | DrdThreadId other_user; |
| 847 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 848 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 849 | /* For all threads, mark the range [ a1, a2 [ as no longer in use. */ |
bart | d43f8d3 | 2008-03-16 17:29:20 +0000 | [diff] [blame] | 850 | other_user = DRD_INVALID_THREADID; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 851 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 852 | { |
| 853 | Segment* p; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 854 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 855 | { |
| 856 | if (other_user == DRD_INVALID_THREADID |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 857 | && i != DRD_(g_drd_running_tid)) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 858 | { |
bart | 8bf2f8b | 2008-03-30 17:56:43 +0000 | [diff] [blame] | 859 | if (UNLIKELY(bm_test_and_clear(p->bm, a1, a2))) |
| 860 | { |
| 861 | other_user = i; |
| 862 | } |
| 863 | continue; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 864 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 865 | bm_clear(p->bm, a1, a2); |
| 866 | } |
| 867 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 868 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 869 | /* |
| 870 | * If any other thread had accessed memory in [ a1, a2 [, update the |
| 871 | * conflict set. |
| 872 | */ |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 873 | if (other_user != DRD_INVALID_THREADID |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 874 | && bm_has_any_access(DRD_(g_conflict_set), a1, a2)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 875 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 876 | thread_compute_conflict_set(&DRD_(g_conflict_set), thread_get_running_tid()); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 877 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 878 | } |
| 879 | |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 880 | void thread_start_recording(const DrdThreadId tid) |
| 881 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 882 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 883 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 884 | tl_assert(! DRD_(g_threadinfo)[tid].is_recording); |
| 885 | DRD_(g_threadinfo)[tid].is_recording = True; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | void thread_stop_recording(const DrdThreadId tid) |
| 889 | { |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 890 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 891 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 892 | tl_assert(DRD_(g_threadinfo)[tid].is_recording); |
| 893 | DRD_(g_threadinfo)[tid].is_recording = False; |
bart | 0268dfa | 2008-03-11 20:10:21 +0000 | [diff] [blame] | 894 | } |
| 895 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 896 | void thread_print_all(void) |
| 897 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 898 | unsigned i; |
| 899 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 900 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 901 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 902 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 903 | if (DRD_(g_threadinfo)[i].first) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 904 | { |
| 905 | VG_(printf)("**************\n" |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 906 | "* thread %3d (%d/%d/%d/0x%lx/%d) *\n" |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 907 | "**************\n", |
| 908 | i, |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 909 | DRD_(g_threadinfo)[i].vg_thread_exists, |
| 910 | DRD_(g_threadinfo)[i].vg_threadid, |
| 911 | DRD_(g_threadinfo)[i].posix_thread_exists, |
| 912 | DRD_(g_threadinfo)[i].pt_threadid, |
| 913 | DRD_(g_threadinfo)[i].detached_posix_thread); |
| 914 | for (p = DRD_(g_threadinfo)[i].first; p; p = p->next) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 915 | { |
bart | 62ada3f | 2009-02-14 17:19:58 +0000 | [diff] [blame] | 916 | DRD_(sg_print)(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 917 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 918 | } |
| 919 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 920 | } |
| 921 | |
| 922 | static void show_call_stack(const DrdThreadId tid, |
| 923 | const Char* const msg, |
| 924 | ExeContext* const callstack) |
| 925 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 926 | const ThreadId vg_tid = DrdThreadIdToVgThreadId(tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 927 | |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 928 | VG_(message)(Vg_UserMsg, "%s (thread %d/%d)", msg, vg_tid, tid); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 929 | |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 930 | if (vg_tid != VG_INVALID_THREADID) |
| 931 | { |
| 932 | if (callstack) |
| 933 | { |
| 934 | VG_(pp_ExeContext)(callstack); |
| 935 | } |
| 936 | else |
| 937 | { |
| 938 | VG_(get_and_pp_StackTrace)(vg_tid, VG_(clo_backtrace_size)); |
| 939 | } |
| 940 | } |
| 941 | else |
| 942 | { |
| 943 | VG_(message)(Vg_UserMsg, |
| 944 | " (thread finished, call stack no longer available)"); |
| 945 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 946 | } |
| 947 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 948 | static void |
| 949 | thread_report_conflicting_segments_segment(const DrdThreadId tid, |
| 950 | const Addr addr, |
| 951 | const SizeT size, |
| 952 | const BmAccessTypeT access_type, |
| 953 | const Segment* const p) |
| 954 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 955 | unsigned i; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 956 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 957 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 958 | && tid != DRD_INVALID_THREADID); |
| 959 | tl_assert(p); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 960 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 961 | for (i = 0; i < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); i++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 962 | { |
| 963 | if (i != tid) |
| 964 | { |
| 965 | Segment* q; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 966 | for (q = DRD_(g_threadinfo)[i].last; q; q = q->prev) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 967 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 968 | /* |
| 969 | * Since q iterates over the segments of thread i in order of |
| 970 | * decreasing vector clocks, if q->vc <= p->vc, then |
| 971 | * q->next->vc <= p->vc will also hold. Hence, break out of the |
| 972 | * loop once this condition is met. |
| 973 | */ |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 974 | if (DRD_(vc_lte)(&q->vc, &p->vc)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 975 | break; |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 976 | if (! DRD_(vc_lte)(&p->vc, &q->vc)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 977 | { |
| 978 | if (bm_has_conflict_with(q->bm, addr, addr + size, access_type)) |
| 979 | { |
| 980 | tl_assert(q->stacktrace); |
| 981 | show_call_stack(i, "Other segment start", |
| 982 | q->stacktrace); |
| 983 | show_call_stack(i, "Other segment end", |
| 984 | q->next ? q->next->stacktrace : 0); |
| 985 | } |
| 986 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 987 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 988 | } |
| 989 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 990 | } |
| 991 | |
| 992 | void thread_report_conflicting_segments(const DrdThreadId tid, |
| 993 | const Addr addr, |
| 994 | const SizeT size, |
| 995 | const BmAccessTypeT access_type) |
| 996 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 997 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 998 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 999 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1000 | && tid != DRD_INVALID_THREADID); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1001 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1002 | for (p = DRD_(g_threadinfo)[tid].first; p; p = p->next) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1003 | { |
| 1004 | if (bm_has(p->bm, addr, addr + size, access_type)) |
| 1005 | { |
| 1006 | thread_report_conflicting_segments_segment(tid, addr, size, |
| 1007 | access_type, p); |
| 1008 | } |
| 1009 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1010 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1011 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1012 | /** |
| 1013 | * Verify whether the conflict set for thread tid is up to date. Only perform |
| 1014 | * the check if the environment variable DRD_VERIFY_CONFLICT_SET has been set. |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1015 | */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1016 | static Bool thread_conflict_set_up_to_date(const DrdThreadId tid) |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1017 | { |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1018 | static int do_verify_conflict_set = -1; |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1019 | Bool result; |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1020 | struct bitmap* computed_conflict_set = 0; |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1021 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1022 | if (do_verify_conflict_set < 0) |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1023 | { |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1024 | //VG_(message)(Vg_DebugMsg, "%s", VG_(getenv)("DRD_VERIFY_CONFLICT_SET")); |
| 1025 | do_verify_conflict_set = VG_(getenv)("DRD_VERIFY_CONFLICT_SET") != 0; |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1026 | } |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1027 | if (do_verify_conflict_set == 0) |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1028 | return True; |
| 1029 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1030 | thread_compute_conflict_set(&computed_conflict_set, tid); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1031 | result = bm_equal(DRD_(g_conflict_set), computed_conflict_set); |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1032 | bm_delete(computed_conflict_set); |
bart | 82195c1 | 2008-04-13 17:35:08 +0000 | [diff] [blame] | 1033 | return result; |
| 1034 | } |
| 1035 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1036 | /** |
| 1037 | * Compute a bitmap that represents the union of all memory accesses of all |
| 1038 | * segments that are unordered to the current segment of the thread tid. |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1039 | */ |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1040 | static void thread_compute_conflict_set(struct bitmap** conflict_set, |
| 1041 | const DrdThreadId tid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1042 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1043 | Segment* p; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1044 | |
bart | 74a5f21 | 2008-05-11 06:43:07 +0000 | [diff] [blame] | 1045 | tl_assert(0 <= (int)tid && tid < DRD_N_THREADS |
| 1046 | && tid != DRD_INVALID_THREADID); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1047 | tl_assert(tid == DRD_(g_drd_running_tid)); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1048 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1049 | DRD_(s_update_conflict_set_count)++; |
| 1050 | DRD_(s_conflict_set_bitmap_creation_count) -= bm_get_bitmap_creation_count(); |
| 1051 | DRD_(s_conflict_set_bitmap2_creation_count) -= bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1052 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1053 | if (*conflict_set) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1054 | { |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1055 | bm_delete(*conflict_set); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1056 | } |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1057 | *conflict_set = bm_new(); |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1058 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1059 | if (DRD_(s_trace_conflict_set)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1060 | { |
| 1061 | char msg[256]; |
| 1062 | |
| 1063 | VG_(snprintf)(msg, sizeof(msg), |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1064 | "computing conflict set for thread %d/%d with vc ", |
bart | aa97a54 | 2008-03-16 17:57:01 +0000 | [diff] [blame] | 1065 | DrdThreadIdToVgThreadId(tid), tid); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1066 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1067 | sizeof(msg) - VG_(strlen)(msg), |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1068 | &DRD_(g_threadinfo)[tid].last->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1069 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1072 | p = DRD_(g_threadinfo)[tid].last; |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1073 | { |
| 1074 | unsigned j; |
| 1075 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1076 | if (DRD_(s_trace_conflict_set)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1077 | { |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1078 | char msg[256]; |
| 1079 | |
| 1080 | VG_(snprintf)(msg, sizeof(msg), |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1081 | "conflict set: thread [%d] at vc ", |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1082 | tid); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1083 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1084 | sizeof(msg) - VG_(strlen)(msg), |
| 1085 | &p->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1086 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1087 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1088 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1089 | for (j = 0; j < sizeof(DRD_(g_threadinfo)) / sizeof(DRD_(g_threadinfo)[0]); j++) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1090 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1091 | if (j != tid && IsValidDrdThreadId(j)) |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1092 | { |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1093 | const Segment* q; |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1094 | for (q = DRD_(g_threadinfo)[j].last; q; q = q->prev) |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1095 | { |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1096 | if (! DRD_(vc_lte)(&q->vc, &p->vc) && ! DRD_(vc_lte)(&p->vc, &q->vc)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1097 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1098 | if (DRD_(s_trace_conflict_set)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1099 | { |
| 1100 | char msg[256]; |
| 1101 | VG_(snprintf)(msg, sizeof(msg), |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1102 | "conflict set: [%d] merging segment ", j); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1103 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1104 | sizeof(msg) - VG_(strlen)(msg), |
| 1105 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1106 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1107 | } |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1108 | bm_merge2(*conflict_set, q->bm); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1109 | } |
| 1110 | else |
| 1111 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1112 | if (DRD_(s_trace_conflict_set)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1113 | { |
| 1114 | char msg[256]; |
| 1115 | VG_(snprintf)(msg, sizeof(msg), |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1116 | "conflict set: [%d] ignoring segment ", j); |
bart | 41b226c | 2009-02-14 16:55:19 +0000 | [diff] [blame] | 1117 | DRD_(vc_snprint)(msg + VG_(strlen)(msg), |
| 1118 | sizeof(msg) - VG_(strlen)(msg), |
| 1119 | &q->vc); |
bart | a2b6e1b | 2008-03-17 18:32:39 +0000 | [diff] [blame] | 1120 | VG_(message)(Vg_UserMsg, "%s", msg); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1121 | } |
| 1122 | } |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1123 | } |
bart | 26f73e1 | 2008-02-24 18:37:08 +0000 | [diff] [blame] | 1124 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1125 | } |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1126 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1127 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1128 | DRD_(s_conflict_set_bitmap_creation_count) += bm_get_bitmap_creation_count(); |
| 1129 | DRD_(s_conflict_set_bitmap2_creation_count) += bm_get_bitmap2_creation_count(); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1130 | |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1131 | if (0 && DRD_(s_trace_conflict_set)) |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1132 | { |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1133 | VG_(message)(Vg_UserMsg, "[%d] new conflict set:", tid); |
| 1134 | bm_print(*conflict_set); |
| 1135 | VG_(message)(Vg_UserMsg, "[%d] end of new conflict set.", tid); |
bart | 3772a98 | 2008-03-15 08:11:03 +0000 | [diff] [blame] | 1136 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1137 | } |
| 1138 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1139 | ULong thread_get_context_switch_count(void) |
| 1140 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1141 | return DRD_(s_context_switch_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1142 | } |
| 1143 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1144 | ULong thread_get_discard_ordered_segments_count(void) |
| 1145 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1146 | return DRD_(s_discard_ordered_segments_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1149 | ULong thread_get_update_conflict_set_count(ULong* dsnsc, ULong* dscvc) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1150 | { |
bart | d66e3a8 | 2008-04-06 15:02:17 +0000 | [diff] [blame] | 1151 | tl_assert(dsnsc); |
| 1152 | tl_assert(dscvc); |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1153 | *dsnsc = DRD_(s_conflict_set_new_segment_count); |
| 1154 | *dscvc = DRD_(s_conflict_set_combine_vc_count); |
| 1155 | return DRD_(s_update_conflict_set_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1158 | ULong thread_get_conflict_set_bitmap_creation_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1159 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1160 | return DRD_(s_conflict_set_bitmap_creation_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
bart | e73b0aa | 2008-06-28 07:19:56 +0000 | [diff] [blame] | 1163 | ULong thread_get_conflict_set_bitmap2_creation_count(void) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1164 | { |
bart | 324a23b | 2009-02-15 12:14:52 +0000 | [diff] [blame^] | 1165 | return DRD_(s_conflict_set_bitmap2_creation_count); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 1166 | } |