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