blob: d4ca878fe295271656b76a84962db60fa542206e [file] [log] [blame]
sewardjaf44c822007-11-25 14:01:38 +00001/*
2 This file is part of drd, a data race detector.
3
sewardj85642922008-01-14 11:54:56 +00004 Copyright (C) 2006-2008 Bart Van Assche
sewardjaf44c822007-11-25 14:01:38 +00005 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
35static
36void vc_reserve(VectorClock* const vc, const unsigned new_capacity);
37
38
39void 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
56void vc_cleanup(VectorClock* const vc)
57{
58 vc_reserve(vc, 0);
59}
60
bartc46c2322008-02-24 18:26:46 +000061/** Copy constructor -- initializes *new. */
sewardjaf44c822007-11-25 14:01:38 +000062void vc_copy(VectorClock* const new,
63 const VectorClock* const rhs)
64{
65 vc_init(new, rhs->vc, rhs->size);
66}
67
bartc46c2322008-02-24 18:26:46 +000068/** Assignment operator -- *lhs is already a valid vector clock. */
69void vc_assign(VectorClock* const lhs,
70 const VectorClock* const rhs)
71{
72 vc_cleanup(lhs);
73 vc_copy(lhs, rhs);
74}
75
sewardjaf44c822007-11-25 14:01:38 +000076void 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 */
bart635a5812008-03-16 08:31:04 +0000107Bool vc_lte(const VectorClock* const vc1, const VectorClock* const vc2)
sewardjaf44c822007-11-25 14:01:38 +0000108{
109 unsigned i;
110 unsigned j = 0;
111 for (i = 0; i < vc1->size; i++)
112 {
113 while (j < vc2->size && vc2->vc[j].threadid < vc1->vc[i].threadid)
114 {
115 j++;
116 }
117 if (j >= vc2->size || vc2->vc[j].threadid > vc1->vc[i].threadid)
118 return False;
bart635a5812008-03-16 08:31:04 +0000119 //tl_assert(j < vc2->size && vc2->vc[j].threadid == vc1->vc[i].threadid);
sewardjaf44c822007-11-25 14:01:38 +0000120 if (vc1->vc[i].count > vc2->vc[j].count)
121 return False;
122 }
123 return True;
124}
125
126/**
127 * @return True if vector clocks vc1 and vc2 are ordered, and false otherwise.
128 * Order is as imposed by thread synchronization actions ("happens before").
129 */
130Bool vc_ordered(const VectorClock* const vc1,
131 const VectorClock* const vc2)
132{
133 return vc_lte(vc1, vc2) || vc_lte(vc2, vc1);
134}
135
bart5bd9f2d2008-03-03 20:31:58 +0000136/** Compute elementwise minimum. */
barta7faf672008-03-06 18:02:37 +0000137void vc_min(VectorClock* const result, const VectorClock* const rhs)
sewardjaf44c822007-11-25 14:01:38 +0000138{
139 unsigned i;
140 unsigned j;
sewardjaf44c822007-11-25 14:01:38 +0000141
142 tl_assert(result);
143 tl_assert(rhs);
144
sewardjaf44c822007-11-25 14:01:38 +0000145 vc_check(result);
146
bart5bd9f2d2008-03-03 20:31:58 +0000147 /* Next, combine both vector clocks into one. */
sewardjaf44c822007-11-25 14:01:38 +0000148 i = 0;
149 for (j = 0; j < rhs->size; j++)
150 {
sewardjaf44c822007-11-25 14:01:38 +0000151 while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid)
bart5bd9f2d2008-03-03 20:31:58 +0000152 {
153 /* Thread ID is missing in second vector clock. Clear the count. */
154 result->vc[i].count = 0;
sewardjaf44c822007-11-25 14:01:38 +0000155 i++;
bart5bd9f2d2008-03-03 20:31:58 +0000156 }
sewardjaf44c822007-11-25 14:01:38 +0000157 if (i >= result->size)
158 {
barta7faf672008-03-06 18:02:37 +0000159 break;
sewardjaf44c822007-11-25 14:01:38 +0000160 }
barta7faf672008-03-06 18:02:37 +0000161 if (result->vc[i].threadid <= rhs->vc[j].threadid)
sewardjaf44c822007-11-25 14:01:38 +0000162 {
bart5bd9f2d2008-03-03 20:31:58 +0000163 /* The thread ID is present in both vector clocks. Compute the minimum */
164 /* of vc[i].count and vc[j].count. */
sewardjaf44c822007-11-25 14:01:38 +0000165 tl_assert(result->vc[i].threadid == rhs->vc[j].threadid);
166 if (rhs->vc[j].count < result->vc[i].count)
167 {
168 result->vc[i].count = rhs->vc[j].count;
169 }
sewardjaf44c822007-11-25 14:01:38 +0000170 }
171 }
172 vc_check(result);
sewardjaf44c822007-11-25 14:01:38 +0000173}
174
175/**
176 * Compute elementwise maximum.
177 */
178void vc_combine(VectorClock* const result,
179 const VectorClock* const rhs)
180{
181 unsigned i;
182 unsigned j;
183 unsigned shared;
184 unsigned new_size;
185
186 tl_assert(result);
187 tl_assert(rhs);
188
189 // First count the number of shared thread id's.
190 j = 0;
191 shared = 0;
192 for (i = 0; i < result->size; i++)
193 {
194 while (j < rhs->size && rhs->vc[j].threadid < result->vc[i].threadid)
195 j++;
196 if (j >= rhs->size)
197 break;
198 if (result->vc[i].threadid == rhs->vc[j].threadid)
199 shared++;
200 }
201
202 vc_check(result);
203
204 new_size = result->size + rhs->size - shared;
205 if (new_size > result->capacity)
206 vc_reserve(result, new_size);
207
208 vc_check(result);
209
210 // Next, combine both vector clocks into one.
211 i = 0;
212 for (j = 0; j < rhs->size; j++)
213 {
sewardjaf44c822007-11-25 14:01:38 +0000214 while (i < result->size && result->vc[i].threadid < rhs->vc[j].threadid)
215 i++;
216 if (i >= result->size)
217 {
218 result->size++;
219 result->vc[i] = rhs->vc[j];
sewardjaf44c822007-11-25 14:01:38 +0000220 }
221 else if (result->vc[i].threadid > rhs->vc[j].threadid)
222 {
223 unsigned k;
224 for (k = result->size; k > i; k--)
225 {
226 result->vc[k] = result->vc[k - 1];
227 }
228 result->size++;
229 result->vc[i] = rhs->vc[j];
sewardjaf44c822007-11-25 14:01:38 +0000230 }
231 else
232 {
233 tl_assert(result->vc[i].threadid == rhs->vc[j].threadid);
234 if (rhs->vc[j].count > result->vc[i].count)
235 {
236 result->vc[i].count = rhs->vc[j].count;
237 }
sewardjaf44c822007-11-25 14:01:38 +0000238 }
239 }
240 vc_check(result);
241 tl_assert(result->size == new_size);
242}
243
244void vc_print(const VectorClock* const vc)
245{
246 unsigned i;
247
248 tl_assert(vc);
249 VG_(printf)("[");
250 for (i = 0; i < vc->size; i++)
251 {
252 tl_assert(vc->vc);
253 VG_(printf)("%s %d: %d", i > 0 ? "," : "",
254 vc->vc[i].threadid, vc->vc[i].count);
255 }
256 VG_(printf)(" ]");
257}
258
259void vc_snprint(Char* const str, Int const size,
260 const VectorClock* const vc)
261{
262 unsigned i;
bartfdb65582008-04-06 13:03:49 +0000263 unsigned j = 1;
sewardjaf44c822007-11-25 14:01:38 +0000264
265 tl_assert(vc);
266 VG_(snprintf)(str, size, "[");
267 for (i = 0; i < vc->size; i++)
268 {
269 tl_assert(vc->vc);
bartfdb65582008-04-06 13:03:49 +0000270 for ( ; j <= vc->vc[i].threadid; j++)
271 {
272 VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str),
273 "%s %d",
274 i > 0 ? "," : "",
275 (j == vc->vc[i].threadid) ? vc->vc[i].count : 0);
276 }
sewardjaf44c822007-11-25 14:01:38 +0000277 }
278 VG_(snprintf)(str + VG_(strlen)(str), size - VG_(strlen)(str), " ]");
279}
280
281/**
282 * Invariant test.
283 */
284void vc_check(const VectorClock* const vc)
285{
286 unsigned i;
287 tl_assert(vc->size <= vc->capacity);
288 for (i = 1; i < vc->size; i++)
289 {
290 tl_assert(vc->vc[i-1].threadid < vc->vc[i].threadid);
291 }
292}
293
294/**
295 * Change the size of the memory block pointed at by vc->vc.
296 * Changes capacity, but does not change size. If the size of the memory
297 * block is increased, the newly allocated memory is not initialized.
298 */
299static
300void vc_reserve(VectorClock* const vc, const unsigned new_capacity)
301{
302 tl_assert(vc);
303 if (new_capacity > vc->capacity)
304 {
305 if (vc->vc)
306 {
307 vc->vc = VG_(realloc)(vc->vc, new_capacity * sizeof(vc->vc[0]));
308 }
309 else if (new_capacity > 0)
310 {
311 vc->vc = VG_(malloc)(new_capacity * sizeof(vc->vc[0]));
312 }
313 else
314 {
315 tl_assert(vc->vc == 0 && new_capacity == 0);
316 }
317 vc->capacity = new_capacity;
318 }
319 tl_assert(new_capacity == 0 || vc->vc != 0);
320}
321
322/**
323 * Unit test.
324 */
325void vc_test(void)
326{
327 VectorClock vc1;
328 VCElem vc1elem[] = { { 3, 7 }, { 5, 8 }, };
329 VectorClock vc2;
330 VCElem vc2elem[] = { { 1, 4 }, { 3, 9 }, };
331 VectorClock vc3;
332 VCElem vc4elem[] = { { 1, 3 }, { 2, 1 }, };
333 VectorClock vc4;
334 VCElem vc5elem[] = { { 1, 4 }, };
335 VectorClock vc5;
336
337 vc_init(&vc1, vc1elem, sizeof(vc1elem)/sizeof(vc1elem[0]));
338 vc_init(&vc2, vc2elem, sizeof(vc2elem)/sizeof(vc2elem[0]));
339 vc_init(&vc3, 0, 0);
340 vc_init(&vc4, vc4elem, sizeof(vc4elem)/sizeof(vc4elem[0]));
341 vc_init(&vc5, vc5elem, sizeof(vc5elem)/sizeof(vc5elem[0]));
342
343 vc_combine(&vc3, &vc1);
344 vc_combine(&vc3, &vc2);
345
346 VG_(printf)("vc1: ");
347 vc_print(&vc1);
348 VG_(printf)("\nvc2: ");
349 vc_print(&vc2);
350 VG_(printf)("\nvc3: ");
351 vc_print(&vc3);
352 VG_(printf)("\n");
353 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));
354 vc_print(&vc4);
355 VG_(printf)(", ");
356 vc_print(&vc5);
357 VG_(printf)(") = %d sw %d\n", vc_lte(&vc4, &vc5), vc_lte(&vc5, &vc4));
358
359 vc_cleanup(&vc1);
360 vc_cleanup(&vc2);
361 vc_cleanup(&vc3);
362}