model: remove redundant code (is_enabled)
[cdsspec-compiler.git] / main.cc
1 /** @file main.cc
2  *  @brief Entry point for the model checker.
3  */
4
5 #include <unistd.h>
6 #include <getopt.h>
7 #include <string.h>
8
9 #include "common.h"
10 #include "output.h"
11
12 #include "datarace.h"
13
14 /* global "model" object */
15 #include "model.h"
16 #include "params.h"
17 #include "snapshot-interface.h"
18 #include "scanalysis.h"
19 #include "plugins.h"
20
21 static void param_defaults(struct model_params *params)
22 {
23         params->maxreads = 0;
24         params->maxfuturedelay = 6;
25         params->fairwindow = 0;
26         params->yieldon = false;
27         params->yieldblock = false;
28         params->enabledcount = 1;
29         params->bound = 0;
30         params->maxfuturevalues = 0;
31         params->expireslop = 4;
32         params->verbose = !!DBG_ENABLED();
33         params->uninitvalue = 0;
34 }
35
36 static void print_usage(const char *program_name, struct model_params *params)
37 {
38         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
39         /* Reset defaults before printing */
40         param_defaults(params);
41
42         model_print(
43 "Copyright (c) 2013 Regents of the University of California. All rights reserved.\n"
44 "Distributed under the GPLv2\n"
45 "Written by Brian Norris and Brian Demsky\n"
46 "\n"
47 "Usage: %s [MODEL-CHECKER OPTIONS] -- [PROGRAM ARGS]\n"
48 "\n"
49 "MODEL-CHECKER OPTIONS can be any of the model-checker options listed below. Arguments\n"
50 "provided after the `--' (the PROGRAM ARGS) are passed to the user program.\n"
51 "\n"
52 "Model-checker options:\n"
53 "-h, --help                  Display this help message and exit\n"
54 "-m, --liveness=NUM          Maximum times a thread can read from the same write\n"
55 "                              while other writes exist.\n"
56 "                              Default: %d\n"
57 "-M, --maxfv=NUM             Maximum number of future values that can be sent to\n"
58 "                              the same read.\n"
59 "                              Default: %d\n"
60 "-s, --maxfvdelay=NUM        Maximum actions that the model checker will wait for\n"
61 "                              a write from the future past the expected number\n"
62 "                              of actions.\n"
63 "                              Default: %d\n"
64 "-S, --fvslop=NUM            Future value expiration sloppiness.\n"
65 "                              Default: %u\n"
66 "-y, --yield                 Enable CHESS-like yield-based fairness support.\n"
67 "                              Default: %s\n"
68 "-Y, --yieldblock            Prohibit an execution from running a yield.\n"
69 "                              Default: %s\n"
70 "-f, --fairness=WINDOW       Specify a fairness window in which actions that are\n"
71 "                              enabled sufficiently many times should receive\n"
72 "                              priority for execution (not recommended).\n"
73 "                              Default: %d\n"
74 "-e, --enabled=COUNT         Enabled count.\n"
75 "                              Default: %d\n"
76 "-b, --bound=MAX             Upper length bound.\n"
77 "                              Default: %d\n"
78 "-v[NUM], --verbose[=NUM]    Print verbose execution information. NUM is optional:\n"
79 "                              0 is quiet; 1 is noisy; 2 is noisier.\n"
80 "                              Default: %d\n"
81 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
82 "                              uninitialized atomic.\n"
83 "                              Default: %u\n"
84 "-t, --analysis=NAME         Use Analysis Plugin.\n"
85 "-o, --options=NAME          Option for previous analysis plugin.  \n"
86 "                            -o help for a list of options\n"
87 " --                         Program arguments follow.\n\n",
88                 program_name,
89                 params->maxreads,
90                 params->maxfuturevalues,
91                 params->maxfuturedelay,
92                 params->expireslop,
93                 params->yieldon ? "enabled" : "disabled",
94                 params->yieldblock ? "enabled" : "disabled",
95                 params->fairwindow,
96                 params->enabledcount,
97                 params->bound,
98                 params->verbose,
99                 params->uninitvalue);
100         model_print("Analysis plugins:\n");
101         for(unsigned int i=0;i<registeredanalysis->size();i++) {
102                 TraceAnalysis * analysis=(*registeredanalysis)[i];
103                 model_print("%s\n", analysis->name());
104         }
105         exit(EXIT_SUCCESS);
106 }
107
108 bool install_plugin(char * name) {
109         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
110         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
111
112         for(unsigned int i=0;i<registeredanalysis->size();i++) {
113                 TraceAnalysis * analysis=(*registeredanalysis)[i];
114                 if (strcmp(name, analysis->name())==0) {
115                         installedanalysis->push_back(analysis);
116                         return false;
117                 }
118         }
119         model_print("Analysis %s Not Found\n", name);
120         return true;
121 }
122
123 static void parse_options(struct model_params *params, int argc, char **argv)
124 {
125         const char *shortopts = "hyYt:o:m:M:s:S:f:e:b:u:v::";
126         const struct option longopts[] = {
127                 {"help", no_argument, NULL, 'h'},
128                 {"liveness", required_argument, NULL, 'm'},
129                 {"maxfv", required_argument, NULL, 'M'},
130                 {"maxfvdelay", required_argument, NULL, 's'},
131                 {"fvslop", required_argument, NULL, 'S'},
132                 {"fairness", required_argument, NULL, 'f'},
133                 {"yield", no_argument, NULL, 'y'},
134                 {"yieldblock", no_argument, NULL, 'Y'},
135                 {"enabled", required_argument, NULL, 'e'},
136                 {"bound", required_argument, NULL, 'b'},
137                 {"verbose", optional_argument, NULL, 'v'},
138                 {"uninitialized", optional_argument, NULL, 'u'},
139                 {"analysis", optional_argument, NULL, 't'},
140                 {"options", optional_argument, NULL, 'o'},
141                 {0, 0, 0, 0} /* Terminator */
142         };
143         int opt, longindex;
144         bool error = false;
145         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
146                 switch (opt) {
147                 case 'h':
148                         print_usage(argv[0], params);
149                         break;
150                 case 's':
151                         params->maxfuturedelay = atoi(optarg);
152                         break;
153                 case 'S':
154                         params->expireslop = atoi(optarg);
155                         break;
156                 case 'f':
157                         params->fairwindow = atoi(optarg);
158                         break;
159                 case 'e':
160                         params->enabledcount = atoi(optarg);
161                         break;
162                 case 'b':
163                         params->bound = atoi(optarg);
164                         break;
165                 case 'm':
166                         params->maxreads = atoi(optarg);
167                         break;
168                 case 'M':
169                         params->maxfuturevalues = atoi(optarg);
170                         break;
171                 case 'v':
172                         params->verbose = optarg ? atoi(optarg) : 1;
173                         break;
174                 case 'u':
175                         params->uninitvalue = atoi(optarg);
176                         break;
177                 case 'y':
178                         params->yieldon = true;
179                         break;
180                 case 't':
181                         if (install_plugin(optarg))
182                                 error = true;
183                         break;
184                 case 'o':
185                         {
186                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
187                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
188                                         error = true;
189                         }
190                         break;
191                 case 'Y':
192                         params->yieldblock = true;
193                         break;
194                 default: /* '?' */
195                         error = true;
196                         break;
197                 }
198         }
199
200         /* Pass remaining arguments to user program */
201         params->argc = argc - (optind - 1);
202         params->argv = argv + (optind - 1);
203
204         /* Reset program name */
205         params->argv[0] = argv[0];
206
207         /* Reset (global) optind for potential use by user program */
208         optind = 1;
209
210         if (error)
211                 print_usage(argv[0], params);
212 }
213
214 int main_argc;
215 char **main_argv;
216
217 static void install_trace_analyses(ModelExecution *execution)
218 {
219         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
220         for(unsigned int i=0;i<installedanalysis->size();i++) {
221                 TraceAnalysis * ta=(*installedanalysis)[i];
222                 ta->setExecution(execution);
223                 model->add_trace_analysis(ta);
224         }
225 }
226
227 /** The model_main function contains the main model checking loop. */
228 static void model_main()
229 {
230         struct model_params params;
231
232         param_defaults(&params);
233         register_plugins();
234
235         parse_options(&params, main_argc, main_argv);
236
237         //Initialize race detector
238         initRaceDetector();
239
240         snapshot_stack_init();
241
242         model = new ModelChecker(params);
243         install_trace_analyses(model->get_execution());
244
245         snapshot_record(0);
246         model->run();
247         delete model;
248
249         DEBUG("Exiting\n");
250 }
251
252 /**
253  * Main function.  Just initializes snapshotting library and the
254  * snapshotting library calls the model_main function.
255  */
256 int main(int argc, char **argv)
257 {
258         main_argc = argc;
259         main_argv = argv;
260
261         /* Configure output redirection for the model-checker */
262         redirect_output();
263
264         /* Let's jump in quickly and start running stuff */
265         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
266 }