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_vc.h" |
| 27 | #include "pub_tool_basics.h" // Addr, SizeT |
| 28 | #include "pub_tool_libcassert.h" // tl_assert() |
| 29 | #include "pub_tool_libcbase.h" // VG_(memset), VG_(memmove) |
| 30 | #include "pub_tool_libcprint.h" // VG_(printf) |
| 31 | #include "pub_tool_mallocfree.h" // VG_(malloc), VG_(free) |
| 32 | #include "pub_tool_threadstate.h" // VG_(get_running_tid) |
| 33 | |
| 34 | |
| 35 | static |
| 36 | void vc_reserve(VectorClock* const vc, const unsigned new_capacity); |
| 37 | |
| 38 | |
| 39 | void vc_init(VectorClock* const vc, |
| 40 | const VCElem* const vcelem, |
| 41 | const unsigned size) |
| 42 | { |
| 43 | tl_assert(vc); |
| 44 | vc->size = 0; |
| 45 | vc->capacity = 0; |
| 46 | vc->vc = 0; |
| 47 | vc_reserve(vc, size); |
| 48 | tl_assert(size == 0 || vc->vc != 0); |
| 49 | if (vcelem) |
| 50 | { |
| 51 | VG_(memcpy)(vc->vc, vcelem, size * sizeof(vcelem[0])); |
| 52 | vc->size = size; |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | void vc_cleanup(VectorClock* const vc) |
| 57 | { |
| 58 | vc_reserve(vc, 0); |
| 59 | } |
| 60 | |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 61 | /** Copy constructor -- initializes *new. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 62 | void vc_copy(VectorClock* const new, |
| 63 | const VectorClock* const rhs) |
| 64 | { |
| 65 | vc_init(new, rhs->vc, rhs->size); |
| 66 | } |
| 67 | |
bart | c46c232 | 2008-02-24 18:26:46 +0000 | [diff] [blame] | 68 | /** Assignment operator -- *lhs is already a valid vector clock. */ |
| 69 | void vc_assign(VectorClock* const lhs, |
| 70 | const VectorClock* const rhs) |
| 71 | { |
| 72 | vc_cleanup(lhs); |
| 73 | vc_copy(lhs, rhs); |
| 74 | } |
| 75 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 76 | void vc_increment(VectorClock* const vc, ThreadId const threadid) |
| 77 | { |
| 78 | unsigned i; |
| 79 | for (i = 0; i < vc->size; i++) |
| 80 | { |
| 81 | if (vc->vc[i].threadid == threadid) |
| 82 | { |
| 83 | typeof(vc->vc[i].count) const oldcount = vc->vc[i].count; |
| 84 | vc->vc[i].count++; |
| 85 | // Check for integer overflow. |
| 86 | tl_assert(oldcount < vc->vc[i].count); |
| 87 | return; |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | // The specified thread ID does not yet exist in the vector clock |
| 92 | // -- insert it. |
| 93 | { |
| 94 | VCElem vcelem = { threadid, 1 }; |
| 95 | VectorClock vc2; |
| 96 | vc_init(&vc2, &vcelem, 1); |
| 97 | vc_combine(vc, &vc2); |
| 98 | vc_cleanup(&vc2); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * @return True if all thread id's that are present in vc1 also exist in |
| 104 | * vc2, and if additionally all corresponding counters in v2 are higher or |
| 105 | * equal. |
| 106 | */ |
| 107 | Bool vc_lte(const VectorClock* const vc1, |
| 108 | const VectorClock* const vc2) |
| 109 | { |
| 110 | unsigned i; |
| 111 | unsigned j = 0; |
| 112 | for (i = 0; i < vc1->size; i++) |
| 113 | { |
| 114 | while (j < vc2->size && vc2->vc[j].threadid < vc1->vc[i].threadid) |
| 115 | { |
| 116 | j++; |
| 117 | } |
| 118 | if (j >= vc2->size || vc2->vc[j].threadid > vc1->vc[i].threadid) |
| 119 | return False; |
| 120 | tl_assert(j < vc2->size && vc2->vc[j].threadid == vc1->vc[i].threadid); |
| 121 | if (vc1->vc[i].count > vc2->vc[j].count) |
| 122 | return False; |
| 123 | } |
| 124 | return True; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @return True if vector clocks vc1 and vc2 are ordered, and false otherwise. |
| 129 | * Order is as imposed by thread synchronization actions ("happens before"). |
| 130 | */ |
| 131 | Bool vc_ordered(const VectorClock* const vc1, |
| 132 | const VectorClock* const vc2) |
| 133 | { |
| 134 | return vc_lte(vc1, vc2) || vc_lte(vc2, vc1); |
| 135 | } |
| 136 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 137 | /** Compute elementwise minimum. */ |
bart | a7faf67 | 2008-03-06 18:02:37 +0000 | [diff] [blame] | 138 | void vc_min(VectorClock* const result, const VectorClock* const rhs) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 139 | { |
| 140 | unsigned i; |
| 141 | unsigned j; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 142 | |
| 143 | tl_assert(result); |
| 144 | tl_assert(rhs); |
| 145 | |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 146 | vc_check(result); |
| 147 | |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 148 | /* Next, combine both vector clocks into one. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 149 | i = 0; |
| 150 | for (j = 0; j < rhs->size; j++) |
| 151 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 152 | while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid) |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 153 | { |
| 154 | /* Thread ID is missing in second vector clock. Clear the count. */ |
| 155 | result->vc[i].count = 0; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 156 | i++; |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 157 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 158 | if (i >= result->size) |
| 159 | { |
bart | a7faf67 | 2008-03-06 18:02:37 +0000 | [diff] [blame] | 160 | break; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 161 | } |
bart | a7faf67 | 2008-03-06 18:02:37 +0000 | [diff] [blame] | 162 | if (result->vc[i].threadid <= rhs->vc[j].threadid) |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 163 | { |
bart | 5bd9f2d | 2008-03-03 20:31:58 +0000 | [diff] [blame] | 164 | /* The thread ID is present in both vector clocks. Compute the minimum */ |
| 165 | /* of vc[i].count and vc[j].count. */ |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 166 | tl_assert(result->vc[i].threadid == rhs->vc[j].threadid); |
| 167 | if (rhs->vc[j].count < result->vc[i].count) |
| 168 | { |
| 169 | result->vc[i].count = rhs->vc[j].count; |
| 170 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 171 | } |
| 172 | } |
| 173 | vc_check(result); |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Compute elementwise maximum. |
| 178 | */ |
| 179 | void vc_combine(VectorClock* const result, |
| 180 | const VectorClock* const rhs) |
| 181 | { |
| 182 | unsigned i; |
| 183 | unsigned j; |
| 184 | unsigned shared; |
| 185 | unsigned new_size; |
| 186 | |
| 187 | tl_assert(result); |
| 188 | tl_assert(rhs); |
| 189 | |
| 190 | // First count the number of shared thread id's. |
| 191 | j = 0; |
| 192 | shared = 0; |
| 193 | for (i = 0; i < result->size; i++) |
| 194 | { |
| 195 | while (j < rhs->size && rhs->vc[j].threadid < result->vc[i].threadid) |
| 196 | j++; |
| 197 | if (j >= rhs->size) |
| 198 | break; |
| 199 | if (result->vc[i].threadid == rhs->vc[j].threadid) |
| 200 | shared++; |
| 201 | } |
| 202 | |
| 203 | vc_check(result); |
| 204 | |
| 205 | new_size = result->size + rhs->size - shared; |
| 206 | if (new_size > result->capacity) |
| 207 | vc_reserve(result, new_size); |
| 208 | |
| 209 | vc_check(result); |
| 210 | |
| 211 | // Next, combine both vector clocks into one. |
| 212 | i = 0; |
| 213 | for (j = 0; j < rhs->size; j++) |
| 214 | { |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 215 | while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid) |
| 216 | i++; |
| 217 | if (i >= result->size) |
| 218 | { |
| 219 | result->size++; |
| 220 | result->vc[i] = rhs->vc[j]; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 221 | } |
| 222 | else if (result->vc[i].threadid > rhs->vc[j].threadid) |
| 223 | { |
| 224 | unsigned k; |
| 225 | for (k = result->size; k > i; k--) |
| 226 | { |
| 227 | result->vc[k] = result->vc[k - 1]; |
| 228 | } |
| 229 | result->size++; |
| 230 | result->vc[i] = rhs->vc[j]; |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 231 | } |
| 232 | else |
| 233 | { |
| 234 | tl_assert(result->vc[i].threadid == rhs->vc[j].threadid); |
| 235 | if (rhs->vc[j].count > result->vc[i].count) |
| 236 | { |
| 237 | result->vc[i].count = rhs->vc[j].count; |
| 238 | } |
sewardj | af44c82 | 2007-11-25 14:01:38 +0000 | [diff] [blame] | 239 | } |
| 240 | } |
| 241 | vc_check(result); |
| 242 | tl_assert(result->size == new_size); |
| 243 | } |
| 244 | |
| 245 | void vc_print(const VectorClock* const vc) |
| 246 | { |
| 247 | unsigned i; |
| 248 | |
| 249 | tl_assert(vc); |
| 250 | VG_(printf)("["); |
| 251 | for (i = 0; i < vc->size; i++) |
| 252 | { |
| 253 | tl_assert(vc->vc); |
| 254 | VG_(printf)("%s %d: %d", i > 0 ? "," : "", |
| 255 | vc->vc[i].threadid, vc->vc[i].count); |
| 256 | } |
| 257 | VG_(printf)(" ]"); |
| 258 | } |
| 259 | |
| 260 | void vc_snprint(Char* const str, Int const size, |
| 261 | const VectorClock* const vc) |
| 262 | { |
| 263 | unsigned i; |
| 264 | |
| 265 | tl_assert(vc); |
| 266 | VG_(snprintf)(str, size, "["); |
| 267 | for (i = 0; i < vc->size; i++) |
| 268 | { |
| 269 | tl_assert(vc->vc); |
| 270 | VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str), |
| 271 | "%s %d: %d", i > 0 ? "," : "", |
| 272 | vc->vc[i].threadid, vc->vc[i].count); |
| 273 | } |
| 274 | VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str), " ]"); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Invariant test. |
| 279 | */ |
| 280 | void vc_check(const VectorClock* const vc) |
| 281 | { |
| 282 | unsigned i; |
| 283 | tl_assert(vc->size <= vc->capacity); |
| 284 | for (i = 1; i < vc->size; i++) |
| 285 | { |
| 286 | tl_assert(vc->vc[i-1].threadid < vc->vc[i].threadid); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | /** |
| 291 | * Change the size of the memory block pointed at by vc->vc. |
| 292 | * Changes capacity, but does not change size. If the size of the memory |
| 293 | * block is increased, the newly allocated memory is not initialized. |
| 294 | */ |
| 295 | static |
| 296 | void vc_reserve(VectorClock* const vc, const unsigned new_capacity) |
| 297 | { |
| 298 | tl_assert(vc); |
| 299 | if (new_capacity > vc->capacity) |
| 300 | { |
| 301 | if (vc->vc) |
| 302 | { |
| 303 | vc->vc = VG_(realloc)(vc->vc, new_capacity * sizeof(vc->vc[0])); |
| 304 | } |
| 305 | else if (new_capacity > 0) |
| 306 | { |
| 307 | vc->vc = VG_(malloc)(new_capacity * sizeof(vc->vc[0])); |
| 308 | } |
| 309 | else |
| 310 | { |
| 311 | tl_assert(vc->vc == 0 && new_capacity == 0); |
| 312 | } |
| 313 | vc->capacity = new_capacity; |
| 314 | } |
| 315 | tl_assert(new_capacity == 0 || vc->vc != 0); |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Unit test. |
| 320 | */ |
| 321 | void vc_test(void) |
| 322 | { |
| 323 | VectorClock vc1; |
| 324 | VCElem vc1elem[] = { { 3, 7 }, { 5, 8 }, }; |
| 325 | VectorClock vc2; |
| 326 | VCElem vc2elem[] = { { 1, 4 }, { 3, 9 }, }; |
| 327 | VectorClock vc3; |
| 328 | VCElem vc4elem[] = { { 1, 3 }, { 2, 1 }, }; |
| 329 | VectorClock vc4; |
| 330 | VCElem vc5elem[] = { { 1, 4 }, }; |
| 331 | VectorClock vc5; |
| 332 | |
| 333 | vc_init(&vc1, vc1elem, sizeof(vc1elem)/sizeof(vc1elem[0])); |
| 334 | vc_init(&vc2, vc2elem, sizeof(vc2elem)/sizeof(vc2elem[0])); |
| 335 | vc_init(&vc3, 0, 0); |
| 336 | vc_init(&vc4, vc4elem, sizeof(vc4elem)/sizeof(vc4elem[0])); |
| 337 | vc_init(&vc5, vc5elem, sizeof(vc5elem)/sizeof(vc5elem[0])); |
| 338 | |
| 339 | vc_combine(&vc3, &vc1); |
| 340 | vc_combine(&vc3, &vc2); |
| 341 | |
| 342 | VG_(printf)("vc1: "); |
| 343 | vc_print(&vc1); |
| 344 | VG_(printf)("\nvc2: "); |
| 345 | vc_print(&vc2); |
| 346 | VG_(printf)("\nvc3: "); |
| 347 | vc_print(&vc3); |
| 348 | VG_(printf)("\n"); |
| 349 | VG_(printf)("vc_lte(vc1, vc2) = %d, vc_lte(vc1, vc3) = %d, vc_lte(vc2, vc3) = %d, vc_lte(", vc_lte(&vc1, &vc2), vc_lte(&vc1, &vc3), vc_lte(&vc2, &vc3)); |
| 350 | vc_print(&vc4); |
| 351 | VG_(printf)(", "); |
| 352 | vc_print(&vc5); |
| 353 | VG_(printf)(") = %d sw %d\n", vc_lte(&vc4, &vc5), vc_lte(&vc5, &vc4)); |
| 354 | |
| 355 | vc_cleanup(&vc1); |
| 356 | vc_cleanup(&vc2); |
| 357 | vc_cleanup(&vc3); |
| 358 | } |