model: use get_thread() helper
[cdsspec-compiler.git] / model.h
1 /** @file model.h
2  *  @brief Core model checker.
3  */
4
5 #ifndef __MODEL_H__
6 #define __MODEL_H__
7
8 #include <cstddef>
9 #include <inttypes.h>
10
11 #include "mymemory.h"
12 #include "hashtable.h"
13 #include "workqueue.h"
14 #include "config.h"
15 #include "modeltypes.h"
16 #include "stl-model.h"
17 #include "context.h"
18
19 /* Forward declaration */
20 class Node;
21 class NodeStack;
22 class CycleGraph;
23 class Promise;
24 class Scheduler;
25 class Thread;
26 class ClockVector;
27 struct model_snapshot_members;
28
29 /** @brief Shorthand for a list of release sequence heads */
30 typedef ModelVector<const ModelAction *> rel_heads_list_t;
31
32 typedef SnapList<ModelAction *> action_list_t;
33
34 /**
35  * Model checker parameter structure. Holds run-time configuration options for
36  * the model checker.
37  */
38 struct model_params {
39         int maxreads;
40         int maxfuturedelay;
41         bool yieldon;
42         unsigned int fairwindow;
43         unsigned int enabledcount;
44         unsigned int bound;
45         unsigned int uninitvalue;
46
47         /** @brief Maximum number of future values that can be sent to the same
48          *  read */
49         int maxfuturevalues;
50
51         /** @brief Only generate a new future value/expiration pair if the
52          *  expiration time exceeds the existing one by more than the slop
53          *  value */
54         unsigned int expireslop;
55
56         /** @brief Verbosity (0 = quiet; 1 = noisy) */
57         int verbose;
58
59         /** @brief Command-line argument count to pass to user program */
60         int argc;
61
62         /** @brief Command-line arguments to pass to user program */
63         char **argv;
64 };
65
66 /** @brief Model checker execution stats */
67 struct execution_stats {
68         int num_total; /**< @brief Total number of executions */
69         int num_infeasible; /**< @brief Number of infeasible executions */
70         int num_buggy_executions; /** @brief Number of buggy executions */
71         int num_complete; /**< @brief Number of feasible, non-buggy, complete executions */
72         int num_redundant; /**< @brief Number of redundant, aborted executions */
73 };
74
75 struct PendingFutureValue {
76         PendingFutureValue(ModelAction *writer, ModelAction *reader) :
77                 writer(writer), reader(reader)
78         { }
79         const ModelAction *writer;
80         ModelAction *reader;
81 };
82
83 /** @brief Records information regarding a single pending release sequence */
84 struct release_seq {
85         /** @brief The acquire operation */
86         ModelAction *acquire;
87         /** @brief The read operation that may read from a release sequence;
88          *  may be the same as acquire, or else an earlier action in the same
89          *  thread (i.e., when 'acquire' is a fence-acquire) */
90         const ModelAction *read;
91         /** @brief The head of the RMW chain from which 'read' reads; may be
92          *  equal to 'release' */
93         const ModelAction *rf;
94         /** @brief The head of the potential longest release sequence chain */
95         const ModelAction *release;
96         /** @brief The write(s) that may break the release sequence */
97         SnapVector<const ModelAction *> writes;
98 };
99
100 /** @brief The central structure for model-checking */
101 class ModelChecker {
102 public:
103         ModelChecker(struct model_params params);
104         ~ModelChecker();
105
106         void run();
107
108         /** @returns the context for the main model-checking system thread */
109         ucontext_t * get_system_context() { return &system_context; }
110
111         void print_summary() const;
112 #if SUPPORT_MOD_ORDER_DUMP
113         void dumpGraph(char *filename) const;
114 #endif
115
116         Thread * get_thread(thread_id_t tid) const;
117         Thread * get_thread(const ModelAction *act) const;
118         int get_promise_number(const Promise *promise) const;
119
120         bool is_enabled(Thread *t) const;
121         bool is_enabled(thread_id_t tid) const;
122
123         thread_id_t get_next_id();
124         unsigned int get_num_threads() const;
125         Thread * get_current_thread() const;
126
127         void switch_from_master(Thread *thread);
128         uint64_t switch_to_master(ModelAction *act);
129         ClockVector * get_cv(thread_id_t tid) const;
130         ModelAction * get_parent_action(thread_id_t tid) const;
131         void check_promises_thread_disabled();
132         void check_promises(thread_id_t tid, ClockVector *old_cv, ClockVector *merge_cv);
133         bool isfeasibleprefix() const;
134
135         bool assert_bug(const char *msg);
136         void assert_user_bug(const char *msg);
137
138         const model_params params;
139         Node * get_curr_node() const;
140
141         MEMALLOC
142 private:
143         /** The scheduler to use: tracks the running/ready Threads */
144         Scheduler * const scheduler;
145
146         void add_thread(Thread *t);
147
148         bool sleep_can_read_from(ModelAction *curr, const ModelAction *write);
149         bool thin_air_constraint_may_allow(const ModelAction *writer, const ModelAction *reader) const;
150         bool mo_may_allow(const ModelAction *writer, const ModelAction *reader);
151         bool promises_may_allow(const ModelAction *writer, const ModelAction *reader) const;
152         bool has_asserted() const;
153         void set_assert();
154         void set_bad_synchronization();
155         bool promises_expired() const;
156         void execute_sleep_set();
157         bool should_wake_up(const ModelAction *curr, const Thread *thread) const;
158         void wake_up_sleeping_actions(ModelAction *curr);
159         modelclock_t get_next_seq_num();
160
161         bool next_execution();
162         ModelAction * check_current_action(ModelAction *curr);
163         bool initialize_curr_action(ModelAction **curr);
164         bool process_read(ModelAction *curr);
165         bool process_write(ModelAction *curr);
166         bool process_fence(ModelAction *curr);
167         bool process_mutex(ModelAction *curr);
168         bool process_thread_action(ModelAction *curr);
169         void process_relseq_fixup(ModelAction *curr, work_queue_t *work_queue);
170         bool read_from(ModelAction *act, const ModelAction *rf);
171         bool check_action_enabled(ModelAction *curr);
172
173         Thread * take_step(ModelAction *curr);
174
175         template <typename T>
176         bool check_recency(ModelAction *curr, const T *rf) const;
177
178         template <typename T, typename U>
179         bool should_read_instead(const ModelAction *curr, const T *rf, const U *other_rf) const;
180
181         ModelAction * get_last_fence_conflict(ModelAction *act) const;
182         ModelAction * get_last_conflict(ModelAction *act) const;
183         void set_backtracking(ModelAction *act);
184         Thread * action_select_next_thread(const ModelAction *curr) const;
185         Thread * get_next_thread();
186         bool set_latest_backtrack(ModelAction *act);
187         ModelAction * get_next_backtrack();
188         void reset_to_initial_state();
189         Promise * pop_promise_to_resolve(const ModelAction *curr);
190         bool resolve_promise(ModelAction *curr, Promise *promise);
191         void compute_promises(ModelAction *curr);
192         void compute_relseq_breakwrites(ModelAction *curr);
193
194         void mo_check_promises(const ModelAction *act, bool is_read_check);
195         void thread_blocking_check_promises(Thread *blocker, Thread *waiting);
196
197         void check_curr_backtracking(ModelAction *curr);
198         void add_action_to_lists(ModelAction *act);
199         ModelAction * get_last_action(thread_id_t tid) const;
200         ModelAction * get_last_fence_release(thread_id_t tid) const;
201         ModelAction * get_last_seq_cst_write(ModelAction *curr) const;
202         ModelAction * get_last_seq_cst_fence(thread_id_t tid, const ModelAction *before_fence) const;
203         ModelAction * get_last_unlock(ModelAction *curr) const;
204         void build_may_read_from(ModelAction *curr);
205         ModelAction * process_rmw(ModelAction *curr);
206
207         template <typename rf_type>
208         bool r_modification_order(ModelAction *curr, const rf_type *rf);
209
210         bool w_modification_order(ModelAction *curr, ModelVector<ModelAction *> *send_fv);
211         void get_release_seq_heads(ModelAction *acquire, ModelAction *read, rel_heads_list_t *release_heads);
212         bool release_seq_heads(const ModelAction *rf, rel_heads_list_t *release_heads, struct release_seq *pending) const;
213         bool resolve_release_sequences(void *location, work_queue_t *work_queue);
214         void add_future_value(const ModelAction *writer, ModelAction *reader);
215
216         ModelAction * get_uninitialized_action(const ModelAction *curr) const;
217
218         ModelAction *diverge;
219         ModelAction *earliest_diverge;
220
221         ucontext_t system_context;
222         action_list_t * const action_trace;
223         HashTable<int, Thread *, int> * const thread_map;
224
225         /** Per-object list of actions. Maps an object (i.e., memory location)
226          * to a trace of all actions performed on the object. */
227         HashTable<const void *, action_list_t *, uintptr_t, 4> * const obj_map;
228
229         /** Per-object list of actions. Maps an object (i.e., memory location)
230          * to a trace of all actions performed on the object. */
231         HashTable<const void *, action_list_t *, uintptr_t, 4> * const condvar_waiters_map;
232
233         HashTable<void *, SnapVector<action_list_t> *, uintptr_t, 4 > * const obj_thrd_map;
234         SnapVector<Promise *> * const promises;
235         SnapVector<struct PendingFutureValue> * const futurevalues;
236
237         /**
238          * List of pending release sequences. Release sequences might be
239          * determined lazily as promises are fulfilled and modification orders
240          * are established. Each entry in the list may only be partially
241          * filled, depending on its pending status.
242          */
243         SnapVector<struct release_seq *> * const pending_rel_seqs;
244
245         SnapVector<ModelAction *> * const thrd_last_action;
246         SnapVector<ModelAction *> * const thrd_last_fence_release;
247         NodeStack * const node_stack;
248
249         /** Private data members that should be snapshotted. They are grouped
250          * together for efficiency and maintainability. */
251         struct model_snapshot_members * const priv;
252
253         /** A special model-checker Thread; used for associating with
254          *  model-checker-related ModelAcitons */
255         Thread *model_thread;
256
257         /**
258          * @brief The modification order graph
259          *
260          * A directed acyclic graph recording observations of the modification
261          * order on all the atomic objects in the system. This graph should
262          * never contain any cycles, as that represents a violation of the
263          * memory model (total ordering). This graph really consists of many
264          * disjoint (unconnected) subgraphs, each graph corresponding to a
265          * separate ordering on a distinct object.
266          *
267          * The edges in this graph represent the "ordered before" relation,
268          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
269          * <tt>b</tt>.
270          */
271         CycleGraph * const mo_graph;
272
273         /** @brief The cumulative execution stats */
274         struct execution_stats stats;
275         void record_stats();
276
277         void print_infeasibility(const char *prefix) const;
278         bool is_feasible_prefix_ignore_relseq() const;
279         bool is_infeasible() const;
280         bool is_deadlocked() const;
281         bool is_complete_execution() const;
282         bool have_bug_reports() const;
283         void print_bugs() const;
284         void print_execution(bool printbugs) const;
285         void print_stats() const;
286
287         friend void user_main_wrapper();
288 };
289
290 extern ModelChecker *model;
291
292 #endif /* __MODEL_H__ */