action: improve printing
[c11tester.git] / action.cc
1 #include <stdio.h>
2 #define __STDC_FORMAT_MACROS
3 #include <inttypes.h>
4 #include <vector>
5
6 #include "model.h"
7 #include "action.h"
8 #include "clockvector.h"
9 #include "common.h"
10 #include "threads-model.h"
11 #include "nodestack.h"
12
13 #define ACTION_INITIAL_CLOCK 0
14
15 /**
16  * @brief Construct a new ModelAction
17  *
18  * @param type The type of action
19  * @param order The memory order of this action. A "don't care" for non-ATOMIC
20  * actions (e.g., THREAD_* or MODEL_* actions).
21  * @param loc The location that this action acts upon
22  * @param value (optional) A value associated with the action (e.g., the value
23  * read or written). Defaults to a given macro constant, for debugging purposes.
24  * @param thread (optional) The Thread in which this action occurred. If NULL
25  * (default), then a Thread is assigned according to the scheduler.
26  */
27 ModelAction::ModelAction(action_type_t type, memory_order order, void *loc,
28                 uint64_t value, Thread *thread) :
29         type(type),
30         order(order),
31         location(loc),
32         value(value),
33         reads_from(NULL),
34         node(NULL),
35         seq_number(ACTION_INITIAL_CLOCK),
36         cv(NULL),
37         sleep_flag(false)
38 {
39         /* References to NULL atomic variables can end up here */
40         ASSERT(loc || type == MODEL_FIXUP_RELSEQ);
41
42         Thread *t = thread ? thread : thread_current();
43         this->tid = t->get_id();
44 }
45
46 /** @brief ModelAction destructor */
47 ModelAction::~ModelAction()
48 {
49         /**
50          * We can't free the clock vector:
51          * Clock vectors are snapshotting state. When we delete model actions,
52          * they are at the end of the node list and have invalid old clock
53          * vectors which have already been rolled back to an unallocated state.
54          */
55
56         /*
57          if (cv)
58                 delete cv; */
59 }
60
61 void ModelAction::copy_from_new(ModelAction *newaction)
62 {
63         seq_number = newaction->seq_number;
64 }
65
66 void ModelAction::set_seq_number(modelclock_t num)
67 {
68         ASSERT(seq_number == ACTION_INITIAL_CLOCK);
69         seq_number = num;
70 }
71
72 bool ModelAction::is_relseq_fixup() const
73 {
74         return type == MODEL_FIXUP_RELSEQ;
75 }
76
77 bool ModelAction::is_mutex_op() const
78 {
79         return type == ATOMIC_LOCK || type == ATOMIC_TRYLOCK || type == ATOMIC_UNLOCK;
80 }
81
82 bool ModelAction::is_lock() const
83 {
84         return type == ATOMIC_LOCK;
85 }
86
87 bool ModelAction::is_unlock() const
88 {
89         return type == ATOMIC_UNLOCK;
90 }
91
92 bool ModelAction::is_trylock() const
93 {
94         return type == ATOMIC_TRYLOCK;
95 }
96
97 bool ModelAction::is_success_lock() const
98 {
99         return type == ATOMIC_LOCK || (type == ATOMIC_TRYLOCK && value == VALUE_TRYSUCCESS);
100 }
101
102 bool ModelAction::is_failed_trylock() const
103 {
104         return (type == ATOMIC_TRYLOCK && value == VALUE_TRYFAILED);
105 }
106
107 bool ModelAction::is_read() const
108 {
109         return type == ATOMIC_READ || type == ATOMIC_RMWR || type == ATOMIC_RMW;
110 }
111
112 bool ModelAction::is_write() const
113 {
114         return type == ATOMIC_WRITE || type == ATOMIC_RMW || type == ATOMIC_INIT;
115 }
116
117 bool ModelAction::could_be_write() const
118 {
119         return is_write() || is_rmwr();
120 }
121
122 bool ModelAction::is_rmwr() const
123 {
124         return type == ATOMIC_RMWR;
125 }
126
127 bool ModelAction::is_rmw() const
128 {
129         return type == ATOMIC_RMW;
130 }
131
132 bool ModelAction::is_rmwc() const
133 {
134         return type == ATOMIC_RMWC;
135 }
136
137 bool ModelAction::is_fence() const 
138 {
139         return type == ATOMIC_FENCE;
140 }
141
142 bool ModelAction::is_initialization() const
143 {
144         return type == ATOMIC_INIT;
145 }
146
147 bool ModelAction::is_acquire() const
148 {
149         switch (order) {
150         case std::memory_order_acquire:
151         case std::memory_order_acq_rel:
152         case std::memory_order_seq_cst:
153                 return true;
154         default:
155                 return false;
156         }
157 }
158
159 bool ModelAction::is_release() const
160 {
161         switch (order) {
162         case std::memory_order_release:
163         case std::memory_order_acq_rel:
164         case std::memory_order_seq_cst:
165                 return true;
166         default:
167                 return false;
168         }
169 }
170
171 bool ModelAction::is_seqcst() const
172 {
173         return order==std::memory_order_seq_cst;
174 }
175
176 bool ModelAction::same_var(const ModelAction *act) const
177 {
178         return location == act->location;
179 }
180
181 bool ModelAction::same_thread(const ModelAction *act) const
182 {
183         return tid == act->tid;
184 }
185
186 void ModelAction::copy_typeandorder(ModelAction * act) {
187         this->type = act->type;
188         this->order = act->order;
189 }
190
191 /** This method changes an existing read part of an RMW action into either:
192  *  (1) a full RMW action in case of the completed write or
193  *  (2) a READ action in case a failed action.
194  * @todo  If the memory_order changes, we may potentially need to update our
195  * clock vector.
196  */
197 void ModelAction::process_rmw(ModelAction * act) {
198         this->order=act->order;
199         if (act->is_rmwc())
200                 this->type=ATOMIC_READ;
201         else if (act->is_rmw()) {
202                 this->type=ATOMIC_RMW;
203                 this->value=act->value;
204         }
205 }
206
207 /** The is_synchronizing method should only explore interleavings if:
208  *  (1) the operations are seq_cst and don't commute or
209  *  (2) the reordering may establish or break a synchronization relation.
210  *  Other memory operations will be dealt with by using the reads_from
211  *  relation.
212  *
213  *  @param act is the action to consider exploring a reordering.
214  *  @return tells whether we have to explore a reordering.
215  */
216 bool ModelAction::could_synchronize_with(const ModelAction *act) const
217 {
218         //Same thread can't be reordered
219         if (same_thread(act))
220                 return false;
221
222         // Different locations commute
223         if (!same_var(act))
224                 return false;
225
226         // Explore interleavings of seqcst writes to guarantee total order
227         // of seq_cst operations that don't commute
228         if ((could_be_write() || act->could_be_write()) && is_seqcst() && act->is_seqcst())
229                 return true;
230
231         // Explore synchronizing read/write pairs
232         if (is_read() && is_acquire() && act->could_be_write() && act->is_release())
233                 return true;
234
235         // Otherwise handle by reads_from relation
236         return false;
237 }
238
239 bool ModelAction::is_conflicting_lock(const ModelAction *act) const
240 {
241         //Must be different threads to reorder
242         if (same_thread(act))
243                 return false;
244         
245         //Try to reorder a lock past a successful lock
246         if (act->is_success_lock())
247                 return true;
248         
249         //Try to push a successful trylock past an unlock
250         if (act->is_unlock() && is_trylock() && value == VALUE_TRYSUCCESS)
251                 return true;
252
253         return false;
254 }
255
256 /**
257  * Create a new clock vector for this action. Note that this function allows a
258  * user to clobber (and leak) a ModelAction's existing clock vector. A user
259  * should ensure that the vector has already either been rolled back
260  * (effectively "freed") or freed.
261  *
262  * @param parent A ModelAction from which to inherit a ClockVector
263  */
264 void ModelAction::create_cv(const ModelAction *parent)
265 {
266         if (parent)
267                 cv = new ClockVector(parent->cv, this);
268         else
269                 cv = new ClockVector(NULL, this);
270 }
271
272 void ModelAction::set_try_lock(bool obtainedlock) {
273         if (obtainedlock)
274                 value=VALUE_TRYSUCCESS;
275         else
276                 value=VALUE_TRYFAILED;
277 }
278
279 /**
280  * Update the model action's read_from action
281  * @param act The action to read from; should be a write
282  * @return True if this read established synchronization
283  */
284 bool ModelAction::read_from(const ModelAction *act)
285 {
286         ASSERT(cv);
287         reads_from = act;
288         if (act != NULL && this->is_acquire()) {
289                 rel_heads_list_t release_heads;
290                 model->get_release_seq_heads(this, &release_heads);
291                 int num_heads = release_heads.size();
292                 for (unsigned int i = 0; i < release_heads.size(); i++)
293                         if (!synchronize_with(release_heads[i])) {
294                                 model->set_bad_synchronization();
295                                 num_heads--;
296                         }
297                 return num_heads > 0;
298         }
299         return false;
300 }
301
302 /**
303  * Synchronize the current thread with the thread corresponding to the
304  * ModelAction parameter.
305  * @param act The ModelAction to synchronize with
306  * @return True if this is a valid synchronization; false otherwise
307  */
308 bool ModelAction::synchronize_with(const ModelAction *act) {
309         if (*this < *act && type != THREAD_JOIN && type != ATOMIC_LOCK)
310                 return false;
311         model->check_promises(act->get_tid(), cv, act->cv);
312         cv->merge(act->cv);
313         return true;
314 }
315
316 bool ModelAction::has_synchronized_with(const ModelAction *act) const
317 {
318         return cv->has_synchronized_with(act->cv);
319 }
320
321 /**
322  * Check whether 'this' happens before act, according to the memory-model's
323  * happens before relation. This is checked via the ClockVector constructs.
324  * @return true if this action's thread has synchronized with act's thread
325  * since the execution of act, false otherwise.
326  */
327 bool ModelAction::happens_before(const ModelAction *act) const
328 {
329         return act->cv->synchronized_since(this);
330 }
331
332 /** @brief Print nicely-formatted info about this ModelAction */
333 void ModelAction::print() const
334 {
335         const char *type_str, *mo_str;
336         switch (this->type) {
337         case MODEL_FIXUP_RELSEQ:
338                 type_str = "relseq fixup";
339                 break;
340         case THREAD_CREATE:
341                 type_str = "thread create";
342                 break;
343         case THREAD_START:
344                 type_str = "thread start";
345                 break;
346         case THREAD_YIELD:
347                 type_str = "thread yield";
348                 break;
349         case THREAD_JOIN:
350                 type_str = "thread join";
351                 break;
352         case THREAD_FINISH:
353                 type_str = "thread finish";
354                 break;
355         case ATOMIC_READ:
356                 type_str = "atomic read";
357                 break;
358         case ATOMIC_WRITE:
359                 type_str = "atomic write";
360                 break;
361         case ATOMIC_RMW:
362                 type_str = "atomic rmw";
363                 break;
364         case ATOMIC_FENCE:
365                 type_str = "fence";
366                 break;
367         case ATOMIC_RMWR:
368                 type_str = "atomic rmwr";
369                 break;
370         case ATOMIC_RMWC:
371                 type_str = "atomic rmwc";
372                 break;
373         case ATOMIC_INIT:
374                 type_str = "init atomic";
375                 break;
376         case ATOMIC_LOCK:
377                 type_str = "lock";
378                 break;
379         case ATOMIC_UNLOCK:
380                 type_str = "unlock";
381                 break;
382         case ATOMIC_TRYLOCK:
383                 type_str = "trylock";
384                 break;
385         default:
386                 type_str = "unknown type";
387         }
388
389         uint64_t valuetoprint=type==ATOMIC_READ?(reads_from!=NULL?reads_from->value:VALUE_NONE):value;
390
391         switch (this->order) {
392         case std::memory_order_relaxed:
393                 mo_str = "relaxed";
394                 break;
395         case std::memory_order_acquire:
396                 mo_str = "acquire";
397                 break;
398         case std::memory_order_release:
399                 mo_str = "release";
400                 break;
401         case std::memory_order_acq_rel:
402                 mo_str = "acq_rel";
403                 break;
404         case std::memory_order_seq_cst:
405                 mo_str = "seq_cst";
406                 break;
407         default:
408                 mo_str = "unknown";
409                 break;
410         }
411
412         printf("(%4d) Thread: %-2d   Action: %-13s   MO: %7s  Loc: %14p   Value: %-#18" PRIx64,
413                         seq_number, id_to_int(tid), type_str, mo_str, location, valuetoprint);
414         if (is_read()) {
415                 if (reads_from)
416                         printf("  Rf: %-3d", reads_from->get_seq_number());
417                 else
418                         printf("  Rf: ?  ");
419         }
420         if (cv) {
421                 if (is_read())
422                         printf(" ");
423                 else
424                         printf("          ");
425                 cv->print();
426         } else
427                 printf("\n");
428 }
429
430 /** @brief Print nicely-formatted info about this ModelAction */
431 unsigned int ModelAction::hash() const
432 {
433         unsigned int hash=(unsigned int) this->type;
434         hash^=((unsigned int)this->order)<<3;
435         hash^=seq_number<<5;
436         hash^=tid<<6;
437
438         if (is_read()) {
439                 if (reads_from)
440                         hash^=reads_from->get_seq_number();
441         }
442         return hash;
443 }