cleanup plugin interface a little more.
[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 "MODLE-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, --verbose               Print verbose execution information.\n"
79 "-u, --uninitialized=VALUE   Return VALUE any load which may read from an\n"
80 "                              uninitialized atomic.\n"
81 "                              Default: %u\n"
82 "-t, --analysis=NAME         Use Analysis Plugin.\n"
83 "-o, --options=NAME          Option for previous analysis plugin.  \n"
84 "                            -o help for a list of options\n"
85 " --                         Program arguments follow.\n\n",
86                 program_name,
87                 params->maxreads,
88                 params->maxfuturevalues,
89                 params->maxfuturedelay,
90                 params->expireslop,
91                 params->yieldon ? "enabled" : "disabled",
92                 params->yieldblock ? "enabled" : "disabled",
93                 params->fairwindow,
94                 params->enabledcount,
95                 params->bound,
96                 params->uninitvalue);
97         model_print("Analysis plug ins:\n");
98         for(unsigned int i=0;i<registeredanalysis->size();i++) {
99                 TraceAnalysis * analysis=(*registeredanalysis)[i];
100                 model_print("%s\n", analysis->name());
101         }
102         exit(EXIT_SUCCESS);
103 }
104
105 bool install_plugin(char * name) {
106         ModelVector<TraceAnalysis *> * registeredanalysis=getRegisteredTraceAnalysis();
107         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
108
109         for(unsigned int i=0;i<registeredanalysis->size();i++) {
110                 TraceAnalysis * analysis=(*registeredanalysis)[i];
111                 if (strcmp(name, analysis->name())==0) {
112                         installedanalysis->push_back(analysis);
113                         return false;
114                 }
115         }
116         model_print("Analysis %s Not Found\n", name);
117         return true;
118 }
119
120 static void parse_options(struct model_params *params, int argc, char **argv)
121 {
122         const char *shortopts = "hyYt:o:m:M:s:S:f:e:b:u:v::";
123         const struct option longopts[] = {
124                 {"help", no_argument, NULL, 'h'},
125                 {"liveness", required_argument, NULL, 'm'},
126                 {"maxfv", required_argument, NULL, 'M'},
127                 {"maxfvdelay", required_argument, NULL, 's'},
128                 {"fvslop", required_argument, NULL, 'S'},
129                 {"fairness", required_argument, NULL, 'f'},
130                 {"yield", no_argument, NULL, 'y'},
131                 {"yieldblock", no_argument, NULL, 'Y'},
132                 {"enabled", required_argument, NULL, 'e'},
133                 {"bound", required_argument, NULL, 'b'},
134                 {"verbose", optional_argument, NULL, 'v'},
135                 {"uninitialized", optional_argument, NULL, 'u'},
136                 {"analysis", optional_argument, NULL, 't'},
137                 {"options", optional_argument, NULL, 'o'},
138                 {0, 0, 0, 0} /* Terminator */
139         };
140         int opt, longindex;
141         bool error = false;
142         while (!error && (opt = getopt_long(argc, argv, shortopts, longopts, &longindex)) != -1) {
143                 switch (opt) {
144                 case 'h':
145                         print_usage(argv[0], params);
146                         break;
147                 case 's':
148                         params->maxfuturedelay = atoi(optarg);
149                         break;
150                 case 'S':
151                         params->expireslop = atoi(optarg);
152                         break;
153                 case 'f':
154                         params->fairwindow = atoi(optarg);
155                         break;
156                 case 'e':
157                         params->enabledcount = atoi(optarg);
158                         break;
159                 case 'b':
160                         params->bound = atoi(optarg);
161                         break;
162                 case 'm':
163                         params->maxreads = atoi(optarg);
164                         break;
165                 case 'M':
166                         params->maxfuturevalues = atoi(optarg);
167                         break;
168                 case 'v':
169                         params->verbose = optarg ? atoi(optarg) : 1;
170                         break;
171                 case 'u':
172                         params->uninitvalue = atoi(optarg);
173                         break;
174                 case 'y':
175                         params->yieldon = true;
176                         break;
177                 case 't':
178                         if (install_plugin(optarg))
179                                 error = true;
180                         break;
181                 case 'o':
182                         {
183                                 ModelVector<TraceAnalysis *> * analyses = getInstalledTraceAnalysis();
184                                 if ( analyses->size() == 0 || (*analyses)[analyses->size()-1]->option(optarg))
185                                         error = true;
186                         }
187                         break;
188                 case 'Y':
189                         params->yieldblock = true;
190                         break;
191                 default: /* '?' */
192                         error = true;
193                         break;
194                 }
195         }
196
197         /* Pass remaining arguments to user program */
198         params->argc = argc - (optind - 1);
199         params->argv = argv + (optind - 1);
200
201         /* Reset program name */
202         params->argv[0] = argv[0];
203
204         /* Reset (global) optind for potential use by user program */
205         optind = 1;
206
207         if (error)
208                 print_usage(argv[0], params);
209 }
210
211 int main_argc;
212 char **main_argv;
213
214 static void install_trace_analyses(ModelExecution *execution)
215 {
216         ModelVector<TraceAnalysis *> * installedanalysis=getInstalledTraceAnalysis();
217         for(unsigned int i=0;i<installedanalysis->size();i++) {
218                 TraceAnalysis * ta=(*installedanalysis)[i];
219                 ta->setExecution(execution);
220                 model->add_trace_analysis(ta);
221         }
222 }
223
224 /** The model_main function contains the main model checking loop. */
225 static void model_main()
226 {
227         struct model_params params;
228
229         param_defaults(&params);
230         register_plugins();
231
232         parse_options(&params, main_argc, main_argv);
233
234         //Initialize race detector
235         initRaceDetector();
236
237         snapshot_stack_init();
238
239         model = new ModelChecker(params);
240         install_trace_analyses(model->get_execution());
241
242         snapshot_record(0);
243         model->run();
244         delete model;
245
246         DEBUG("Exiting\n");
247 }
248
249 /**
250  * Main function.  Just initializes snapshotting library and the
251  * snapshotting library calls the model_main function.
252  */
253 int main(int argc, char **argv)
254 {
255         main_argc = argc;
256         main_argv = argv;
257
258         /* Configure output redirection for the model-checker */
259         redirect_output();
260
261         /* Let's jump in quickly and start running stuff */
262         snapshot_system_init(10000, 1024, 1024, 4000, &model_main);
263 }