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