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