f79366fb2d8450bcbbe69373ac45db600aec8caa
[c11tester.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6
7 #include <threads.h>
8 #include "common.h"
9 #include "threads-model.h"
10 #include "output.h"
11
12 #include "datarace.h"
13
14 /* global "model" object */
15 #include "model.h"
16 #include "snapshot-interface.h"
17
18 static void param_defaults(struct model_params * params) {
19         params->maxreads = 0;
20         params->maxfuturedelay = 100;
21         params->fairwindow = 0;
22         params->enabledcount = 1;
23         params->bound = 0;
24         params->maxfuturevalues = 0;
25         params->expireslop = 10;
26 }
27
28 static void print_usage(struct model_params *params) {
29         /* Reset defaults before printing */
30         param_defaults(params);
31
32         model_print(
33 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
34 "\n"
35 "Options:\n"
36 "-h                    Display this help message and exit\n"
37 "-m                    Maximum times a thread can read from the same write\n"
38 "                      while other writes exist. Default: %d\n"
39 "-M                    Maximum number of future values that can be sent to\n"
40 "                      the same read. Default: %d\n"
41 "-s                    Maximum actions that the model checker will wait for\n"
42 "                      a write from the future past the expected number of\n"
43 "                      actions. Default: %d\n"
44 "-S                    Future value expiration sloppiness. Default: %u\n"
45 "-f                    Specify a fairness window in which actions that are\n"
46 "                      enabled sufficiently many times should receive\n"
47 "                      priority for execution. Default: %d\n"
48 "-e                    Enabled count. Default: %d\n"
49 "-b                    Upper length bound. Default: %d\n"
50 "--                    Program arguments follow.\n\n",
51 params->maxreads, params->maxfuturevalues, params->maxfuturedelay, params->expireslop, params->fairwindow, params->enabledcount, params->bound);
52         exit(EXIT_SUCCESS);
53 }
54
55 static void parse_options(struct model_params *params, int *argc, char ***argv) {
56         const char *shortopts = "hm:M:s:S:f:e:b:";
57         int opt;
58         bool error = false;
59         while (!error && (opt = getopt(*argc, *argv, shortopts)) != -1) {
60                 switch (opt) {
61                 case 'h':
62                         print_usage(params);
63                         break;
64                 case 's':
65                         params->maxfuturedelay = atoi(optarg);
66                         break;
67                 case 'S':
68                         params->expireslop = atoi(optarg);
69                         break;
70                 case 'f':
71                         params->fairwindow = atoi(optarg);
72                         break;
73                 case 'e':
74                         params->enabledcount = atoi(optarg);
75                         break;
76                 case 'b':
77                         params->bound = atoi(optarg);
78                         break;
79                 case 'm':
80                         params->maxreads = atoi(optarg);
81                         break;
82                 case 'M':
83                         params->maxfuturevalues = atoi(optarg);
84                         break;
85                 default: /* '?' */
86                         error = true;
87                         break;
88                 }
89         }
90         (*argv)[optind - 1] = (*argv)[0];
91         (*argc) -= (optind - 1);
92         (*argv) += (optind - 1);
93         optind = 1;
94
95         if (error)
96                 print_usage(params);
97 }
98
99 int main_argc;
100 char **main_argv;
101
102 /** Wrapper to run the user's main function, with appropriate arguments */
103 void wrapper_user_main(void *)
104 {
105         user_main(main_argc, main_argv);
106 }
107
108 /** The model_main function contains the main model checking loop. */
109 static void model_main() {
110         thrd_t user_thread;
111         struct model_params params;
112
113         param_defaults(&params);
114
115         parse_options(&params, &main_argc, &main_argv);
116
117         //Initialize race detector
118         initRaceDetector();
119
120         //Create the singleton SnapshotStack object
121         snapshotObject = new SnapshotStack();
122
123         model = new ModelChecker(params);
124
125         snapshotObject->snapshotStep(0);
126         do {
127                 /* Start user program */
128                 model->add_thread(new Thread(&user_thread, &wrapper_user_main, NULL));
129
130                 /* Wait for all threads to complete */
131                 model->finish_execution();
132         } while (model->next_execution());
133
134         model->print_stats();
135
136         delete model;
137
138         DEBUG("Exiting\n");
139 }
140
141 /**
142  * Main function.  Just initializes snapshotting library and the
143  * snapshotting library calls the model_main function.
144  */
145 int main(int argc, char ** argv) {
146         main_argc = argc;
147         main_argv = argv;
148
149         /* Configure output redirection for the model-checker */
150         redirect_output();
151
152         /* Let's jump in quickly and start running stuff */
153         initSnapshotLibrary(10000, 1024, 1024, 4000, &model_main);
154 }