Merge cleanup branch
[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 "common.h"
8 #include "output.h"
9
10 #include "datarace.h"
11
12 /* global "model" object */
13 #include "model.h"
14 #include "params.h"
15 #include "snapshot-interface.h"
16 #include "scanalysis.h"
17
18 static void param_defaults(struct model_params *params)
19 {
20         params->maxreads = 0;
21         params->maxfuturedelay = 6;
22         params->fairwindow = 0;
23         params->yieldon = false;
24         params->sc_trace_analysis = false;
25         params->enabledcount = 1;
26         params->bound = 0;
27         params->maxfuturevalues = 0;
28         params->expireslop = 4;
29         params->verbose = !!DBG_ENABLED();
30         params->uninitvalue = 0;
31 }
32
33 static void print_usage(struct model_params *params)
34 {
35         /* Reset defaults before printing */
36         param_defaults(params);
37
38         model_print(
39 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
40 "Distributed under the GPLv2\n"
41 "Written by Brian Norris and Brian Demsky\n"
42 "\n"
43 "Usage: <program name> [MC_OPTIONS] -- [PROGRAM ARGUMENTS]\n"
44 "\n"
45 "Options:\n"
46 "-h                    Display this help message and exit\n"
47 "-m                    Maximum times a thread can read from the same write\n"
48 "                      while other writes exist. Default: %d\n"
49 "-M                    Maximum number of future values that can be sent to\n"
50 "                      the same read. Default: %d\n"
51 "-s                    Maximum actions that the model checker will wait for\n"
52 "                      a write from the future past the expected number of\n"
53 "                      actions. Default: %d\n"
54 "-S                    Future value expiration sloppiness. Default: %u\n"
55 "-f                    Specify a fairness window in which actions that are\n"
56 "                      enabled sufficiently many times should receive\n"
57 "                      priority for execution. Default: %d\n"
58 "-y                    Turn on CHESS yield-based fairness support.\n"
59 "                      Default: %d\n"
60 "-e                    Enabled count. Default: %d\n"
61 "-b                    Upper length bound. Default: %d\n"
62 "-v                    Print verbose execution information.\n"
63 "-u                    Value for uninitialized reads. Default: %u\n"
64 "-c                    Use SC Trace Analysis.\n"
65 "--                    Program arguments follow.\n\n",
66 params->maxreads, params->maxfuturevalues, params->maxfuturedelay, params->expireslop, params->fairwindow, params->yieldon, params->enabledcount, params->bound, params->uninitvalue);
67         exit(EXIT_SUCCESS);
68 }
69
70 static void parse_options(struct model_params *params, int argc, char **argv)
71 {
72         const char *shortopts = "hycm:M:s:S:f:e:b:u:v";
73         int opt;
74         bool error = false;
75         while (!error && (opt = getopt(argc, argv, shortopts)) != -1) {
76                 switch (opt) {
77                 case 'h':
78                         print_usage(params);
79                         break;
80                 case 's':
81                         params->maxfuturedelay = atoi(optarg);
82                         break;
83                 case 'S':
84                         params->expireslop = atoi(optarg);
85                         break;
86                 case 'f':
87                         params->fairwindow = atoi(optarg);
88                         break;
89                 case 'e':
90                         params->enabledcount = atoi(optarg);
91                         break;
92                 case 'b':
93                         params->bound = atoi(optarg);
94                         break;
95                 case 'm':
96                         params->maxreads = atoi(optarg);
97                         break;
98                 case 'M':
99                         params->maxfuturevalues = atoi(optarg);
100                         break;
101                 case 'v':
102                         params->verbose = 1;
103                         break;
104                 case 'u':
105                         params->uninitvalue = atoi(optarg);
106                         break;
107                 case 'c':
108                         params->sc_trace_analysis = true;
109                         break;
110                 case 'y':
111                         params->yieldon = true;
112                         break;
113                 default: /* '?' */
114                         error = true;
115                         break;
116                 }
117         }
118
119         /* Pass remaining arguments to user program */
120         params->argc = argc - (optind - 1);
121         params->argv = argv + (optind - 1);
122
123         /* Reset program name */
124         params->argv[0] = argv[0];
125
126         /* Reset (global) optind for potential use by user program */
127         optind = 1;
128
129         if (error)
130                 print_usage(params);
131 }
132
133 int main_argc;
134 char **main_argv;
135
136 static void install_trace_analyses(const ModelExecution *execution)
137 {
138         if (model->params.sc_trace_analysis)
139                 model->add_trace_analysis(new SCAnalysis(execution));
140 }
141
142 /** The model_main function contains the main model checking loop. */
143 static void model_main()
144 {
145         struct model_params params;
146
147         param_defaults(&params);
148
149         parse_options(&params, main_argc, main_argv);
150
151         //Initialize race detector
152         initRaceDetector();
153
154         snapshot_stack_init();
155
156         model = new ModelChecker(params);
157         install_trace_analyses(model->get_execution());
158
159         snapshot_record(0);
160         model->run();
161         delete model;
162
163         DEBUG("Exiting\n");
164 }
165
166 /**
167  * Main function.  Just initializes snapshotting library and the
168  * snapshotting library calls the model_main function.
169  */
170 int main(int argc, char **argv)
171 {
172         main_argc = argc;
173         main_argv = argv;
174
175         /* Configure output redirection for the model-checker */
176         redirect_output();
177
178         /* Let's jump in quickly and start running stuff */
179         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
180 }