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