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