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