ea526f7499bff4e919385fa9fda5d31f2d207eec
[c11tester.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 "config.h"
14 #include "modeltypes.h"
15 #include "stl-model.h"
16 #include "context.h"
17 #include "params.h"
18 #include "classlist.h"
19
20 typedef SnapList<ModelAction *> action_list_t;
21
22 /** @brief Model checker execution stats */
23 struct execution_stats {
24         int num_total;  /**< @brief Total number of executions */
25         int num_buggy_executions;       /** @brief Number of buggy executions */
26         int num_complete;       /**< @brief Number of feasible, non-buggy, complete executions */
27 };
28
29 /** @brief The central structure for model-checking */
30 class ModelChecker {
31 public:
32         ModelChecker();
33         ~ModelChecker();
34         model_params * getParams();
35         void run();
36
37         /** Restart the model checker, intended for pluggins. */
38         void restart();
39
40         /** Exit the model checker, intended for pluggins. */
41         void exit_model_checker();
42
43         /** @returns the context for the main model-checking system thread */
44         ucontext_t * get_system_context() { return &system_context; }
45
46         ModelExecution * get_execution() const { return execution; }
47         ModelHistory * get_history() const { return history; }
48
49         int get_execution_number() const { return execution_number; }
50
51         Thread * get_thread(thread_id_t tid) const;
52         Thread * get_thread(const ModelAction *act) const;
53
54         Thread * get_current_thread() const;
55
56         void switch_from_master(Thread *thread);
57         uint64_t switch_to_master(ModelAction *act);
58
59         void assert_bug(const char *msg, ...);
60
61         void assert_user_bug(const char *msg);
62
63         model_params params;
64         void add_trace_analysis(TraceAnalysis *a) {     trace_analyses.push_back(a); }
65         void set_inspect_plugin(TraceAnalysis *a) {     inspect_plugin=a;       }
66         void startChecker();
67         Thread * getInitThread() {return init_thread;}
68         Scheduler * getScheduler() {return scheduler;}
69         MEMALLOC
70 private:
71         /** Flag indicates whether to restart the model checker. */
72         bool restart_flag;
73
74         /** The scheduler to use: tracks the running/ready Threads */
75         Scheduler * const scheduler;
76         ModelHistory * history;
77         ModelExecution *execution;
78         Thread * init_thread;
79
80         int execution_number;
81
82         unsigned int get_num_threads() const;
83
84         bool next_execution();
85         bool should_terminate_execution();
86
87         Thread * get_next_thread();
88         void reset_to_initial_state();
89
90         ucontext_t system_context;
91
92         ModelVector<TraceAnalysis *> trace_analyses;
93
94         /** @bref Implement restart. */
95         void do_restart();
96         /** @bref Plugin that can inspect new actions. */
97         TraceAnalysis *inspect_plugin;
98         /** @brief The cumulative execution stats */
99         struct execution_stats stats;
100         void record_stats();
101         void run_trace_analyses();
102         void print_bugs() const;
103         void print_execution(bool printbugs) const;
104         void print_stats() const;
105 };
106
107 extern ModelChecker *model;
108 void parse_options(struct model_params *params);
109 void install_trace_analyses(ModelExecution *execution);
110
111 #endif  /* __MODEL_H__ */