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