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