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