a number of fixes to add missing mo_graph edges to speed up detection of infeasible
[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 "libthreads.h"
8 #include "common.h"
9 #include "threads.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 }
23
24 static void print_usage(struct model_params *params) {
25         printf(
26 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
27 "\n"
28 "Options:\n"
29 "-h                    Display this help message and exit\n"
30 "-m                    Maximum times a thread can read from the same write\n"
31 "                      while other writes exist. Default: %d\n"
32 "-s                    Maximum actions that the model checker will wait for\n"
33 "                      a write from the future past the expected number of\n"
34 "                      actions. Default: %d\n"
35 "-f                    Specify a fairness window in which actions that are\n"
36 "                      enabled sufficiently many times should receive\n"
37 "                      priority for execution. Default: %d\n"
38 "-e                    Enabled count. Default: %d\n"
39 "--                    Program arguments follow.\n\n",
40 params->maxreads, params->maxfuturedelay, params->fairwindow, params->enabledcount);
41         exit(EXIT_SUCCESS);
42 }
43
44 static void parse_options(struct model_params *params, int *argc, char ***argv) {
45         const char *shortopts = "hm:s:f:e:";
46         int opt;
47         bool error = false;
48         while (!error && (opt = getopt(*argc, *argv, shortopts)) != -1) {
49                 switch (opt) {
50                 case 'h':
51                         print_usage(params);
52                         break;
53                 case 's':
54                         params->maxfuturedelay = atoi(optarg);
55                         break;
56                 case 'f':
57                         params->fairwindow = atoi(optarg);
58                         break;
59                 case 'e':
60                         params->enabledcount = atoi(optarg);
61                         break;
62                 case 'm':
63                         params->maxreads = atoi(optarg);
64                         break;
65                 default: /* '?' */
66                         error = true;
67                         break;
68                 }
69         }
70         (*argc) -= optind;
71         (*argv) += optind;
72
73         if (error)
74                 print_usage(params);
75 }
76
77 int main_argc;
78 char **main_argv;
79
80 /** The real_main function contains the main model checking loop. */
81 static void real_main() {
82         thrd_t user_thread;
83         struct model_params params;
84
85         param_defaults(&params);
86
87         parse_options(&params, &main_argc, &main_argv);
88
89         //Initialize race detector
90         initRaceDetector();
91
92         //Create the singleton SnapshotStack object
93         snapshotObject = new SnapshotStack();
94
95         model = new ModelChecker(params);
96
97         snapshotObject->snapshotStep(0);
98         do {
99                 /* Start user program */
100                 model->add_thread(new Thread(&user_thread, (void (*)(void *)) &user_main, NULL));
101
102                 /* Wait for all threads to complete */
103                 model->finish_execution();
104         } while (model->next_execution());
105
106         delete model;
107
108         DEBUG("Exiting\n");
109 }
110
111 /**
112  * Main function.  Just initializes snapshotting library and the
113  * snapshotting library calls the real_main function.
114  */
115 int main(int argc, char ** argv) {
116         main_argc = argc;
117         main_argv = argv;
118
119         /* Let's jump in quickly and start running stuff */
120         initSnapShotLibrary(10000, 1024, 1024, 4000, &real_main);
121 }