.gitignore: ignore .dot graph files
[model-checker.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 <list>
9 #include <vector>
10 #include <cstddef>
11 #include <ucontext.h>
12
13 #include "schedule.h"
14 #include "mymemory.h"
15 #include "libthreads.h"
16 #include "threads.h"
17 #include "action.h"
18 #include "clockvector.h"
19 #include "hashtable.h"
20
21 /* Forward declaration */
22 class NodeStack;
23 class CycleGraph;
24 class Promise;
25
26 /**
27  * Model checker parameter structure. Holds run-time configuration options for
28  * the model checker.
29  */
30 struct model_params {
31         int maxreads;
32         int maxfuturedelay;
33 };
34
35 struct PendingFutureValue {
36         uint64_t value;
37         modelclock_t expiration;
38         ModelAction * act;
39 };
40
41 /**
42  * Structure for holding small ModelChecker members that should be snapshotted
43  */
44 struct model_snapshot_members {
45         ModelAction *current_action;
46         int next_thread_id;
47         modelclock_t used_sequence_numbers;
48         Thread *nextThread;
49         ModelAction *next_backtrack;
50
51         /** @see ModelChecker::lazy_sync_size */
52         unsigned int lazy_sync_size;
53 };
54
55 /** @brief The central structure for model-checking */
56 class ModelChecker {
57 public:
58         ModelChecker(struct model_params params);
59         ~ModelChecker();
60
61         /** @returns the context for the main model-checking system thread */
62         ucontext_t * get_system_context() { return &system_context; }
63
64         /** Prints an execution summary with trace information. */
65         void print_summary();
66
67         void add_thread(Thread *t);
68         void remove_thread(Thread *t);
69         Thread * get_thread(thread_id_t tid) { return thread_map->get(id_to_int(tid)); }
70         Thread * get_thread(ModelAction *act) { return get_thread(act->get_tid()); }
71
72         thread_id_t get_next_id();
73         int get_num_threads();
74         modelclock_t get_next_seq_num();
75
76         /** @return The currently executing Thread. */
77         Thread * get_current_thread() { return scheduler->get_current_thread(); }
78
79         int switch_to_master(ModelAction *act);
80         ClockVector * get_cv(thread_id_t tid);
81         ModelAction * get_parent_action(thread_id_t tid);
82         bool next_execution();
83         bool isfeasible();
84         bool isfeasibleotherthanRMW();
85         bool isfinalfeasible();
86         void check_promises(ClockVector *old_cv, ClockVector * merge_cv);
87         void get_release_seq_heads(ModelAction *act,
88                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads);
89         void finish_execution();
90         bool isfeasibleprefix();
91         void set_assert() {asserted=true;}
92
93         MEMALLOC
94 private:
95         /** The scheduler to use: tracks the running/ready Threads */
96         Scheduler *scheduler;
97
98         bool thin_air_constraint_may_allow(const ModelAction * writer, const ModelAction *reader);
99         bool has_asserted() {return asserted;}
100         void reset_asserted() {asserted=false;}
101         int num_executions;
102         int num_feasible_executions;
103         bool promises_expired();
104         const model_params params;
105
106         /**
107          * Stores the ModelAction for the current thread action.  Call this
108          * immediately before switching from user- to system-context to pass
109          * data between them.
110          * @param act The ModelAction created by the user-thread action
111          */
112         void set_current_action(ModelAction *act) { priv->current_action = act; }
113         Thread * check_current_action(ModelAction *curr);
114         bool process_read(ModelAction *curr, Thread * th, bool second_part_of_rmw);
115
116         bool take_step();
117
118         void check_recency(ModelAction *curr, bool already_added);
119         ModelAction * get_last_conflict(ModelAction *act);
120         void set_backtracking(ModelAction *act);
121         Thread * get_next_thread(ModelAction *curr);
122         ModelAction * get_next_backtrack();
123         void reset_to_initial_state();
124         bool resolve_promises(ModelAction *curr);
125         void compute_promises(ModelAction *curr);
126
127         void check_curr_backtracking(ModelAction * curr);
128         void add_action_to_lists(ModelAction *act);
129         ModelAction * get_last_action(thread_id_t tid);
130         ModelAction * get_last_seq_cst(const void *location);
131         void build_reads_from_past(ModelAction *curr);
132         ModelAction * process_rmw(ModelAction *curr);
133         void post_r_modification_order(ModelAction *curr, const ModelAction *rf);
134         bool r_modification_order(ModelAction *curr, const ModelAction *rf);
135         bool w_modification_order(ModelAction *curr);
136         bool release_seq_head(const ModelAction *rf,
137                         std::vector< const ModelAction *, MyAlloc<const ModelAction *> > *release_heads) const;
138         bool resolve_release_sequences(void *location);
139
140         ModelAction *diverge;
141
142         ucontext_t system_context;
143         action_list_t *action_trace;
144         HashTable<int, Thread *, int> *thread_map;
145
146         /** Per-object list of actions. Maps an object (i.e., memory location)
147          * to a trace of all actions performed on the object. */
148         HashTable<const void *, action_list_t, uintptr_t, 4> *obj_map;
149
150         HashTable<void *, std::vector<action_list_t>, uintptr_t, 4 > *obj_thrd_map;
151         std::vector<Promise *> *promises;
152         std::vector<struct PendingFutureValue> *futurevalues;
153
154         /**
155          * Collection of lists of objects that might synchronize with one or
156          * more release sequence. Release sequences might be determined lazily
157          * as promises are fulfilled and modification orders are established.
158          * This structure maps its lists by object location. Each ModelAction
159          * in the lists should be an acquire operation.
160          */
161         HashTable<void *, std::list<ModelAction *>, uintptr_t, 4> *lazy_sync_with_release;
162
163         /**
164          * Represents the total size of the
165          * ModelChecker::lazy_sync_with_release lists. This count should be
166          * snapshotted, so it is actually a pointer to a location within
167          * ModelChecker::priv
168          */
169         unsigned int *lazy_sync_size;
170
171         std::vector<ModelAction *> *thrd_last_action;
172         NodeStack *node_stack;
173
174         /** Private data members that should be snapshotted. They are grouped
175          * together for efficiency and maintainability. */
176         struct model_snapshot_members *priv;
177
178         /**
179          * @brief The modification order graph
180          *
181          * A directed acyclic graph recording observations of the modification
182          * order on all the atomic objects in the system. This graph should
183          * never contain any cycles, as that represents a violation of the
184          * memory model (total ordering). This graph really consists of many
185          * disjoint (unconnected) subgraphs, each graph corresponding to a
186          * separate ordering on a distinct object.
187          *
188          * The edges in this graph represent the "ordered before" relation,
189          * such that <tt>a --> b</tt> means <tt>a</tt> was ordered before
190          * <tt>b</tt>.
191          */
192         CycleGraph *mo_graph;
193         bool failed_promise;
194         bool too_many_reads;
195         bool asserted;
196 };
197
198 extern ModelChecker *model;
199
200 #endif /* __MODEL_H__ */