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