cyclegraph: simplify resolvePromise / mergeNodes
[model-checker.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6
7 #include "common.h"
8 #include "output.h"
9
10 #include "datarace.h"
11
12 /* global "model" object */
13 #include "model.h"
14 #include "snapshot-interface.h"
15
16 static void param_defaults(struct model_params *params)
17 {
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         params->expireslop = 10;
25         params->verbose = 0;
26 }
27
28 static void print_usage(struct model_params *params)
29 {
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 {
59         const char *shortopts = "hm:M:s:S:f:e:b:v";
60         int opt;
61         bool error = false;
62         while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
63                 switch (opt) {
64                 case 'h':
65                         print_usage(params);
66                         break;
67                 case 's':
68                         params->maxfuturedelay = atoi(optarg);
69                         break;
70                 case 'S':
71                         params->expireslop = atoi(optarg);
72                         break;
73                 case 'f':
74                         params->fairwindow = atoi(optarg);
75                         break;
76                 case 'e':
77                         params->enabledcount = atoi(optarg);
78                         break;
79                 case 'b':
80                         params->bound = atoi(optarg);
81                         break;
82                 case 'm':
83                         params->maxreads = atoi(optarg);
84                         break;
85                 case 'M':
86                         params->maxfuturevalues = atoi(optarg);
87                         break;
88                 case 'v':
89                         params->verbose = 1;
90                         break;
91                 default: /* '?' */
92                         error = true;
93                         break;
94                 }
95         }
96
97         /* Pass remaining arguments to user program */
98         params->argc = argc - (optind - 1);
99         params->argv = argv + (optind - 1);
100
101         /* Reset program name */
102         params->argv[0] = argv[0];
103
104         /* Reset (global) optind for potential use by user program */
105         optind = 1;
106
107         if (error)
108                 print_usage(params);
109 }
110
111 int main_argc;
112 char **main_argv;
113
114 /** The model_main function contains the main model checking loop. */
115 static void model_main()
116 {
117         struct model_params params;
118
119         param_defaults(&params);
120
121         parse_options(&params, main_argc, main_argv);
122
123         //Initialize race detector
124         initRaceDetector();
125
126         snapshot_stack_init();
127
128         model = new ModelChecker(params);
129         snapshot_record(0);
130         model->run();
131         delete model;
132
133         DEBUG("Exiting\n");
134 }
135
136 /**
137  * Main function.  Just initializes snapshotting library and the
138  * snapshotting library calls the model_main function.
139  */
140 int main(int argc, char **argv)
141 {
142         main_argc = argc;
143         main_argv = argv;
144
145         /* Configure output redirection for the model-checker */
146         redirect_output();
147
148         /* Let's jump in quickly and start running stuff */
149         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
150 }