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