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