arm, config: Automatically discover available platforms
[gem5.git] / util / cxx_config / main.cc
1 /*
2 * Copyright (c) 2014 ARM Limited
3 * All rights reserved
4 *
5 * The license below extends only to copyright in the software and shall
6 * not be construed as granting a license to any other intellectual
7 * property including but not limited to intellectual property relating
8 * to a hardware implementation of the functionality of the software
9 * licensed hereunder. You may use the software subject to the license
10 * terms below provided that you ensure that this notice is replicated
11 * unmodified and in its entirety in all distributions of the software,
12 * modified or unmodified, in source code or in binary form.
13 *
14 * Redistribution and use in source and binary forms, with or without
15 * modification, are permitted provided that the following conditions are
16 * met: redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer;
18 * redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution;
21 * neither the name of the copyright holders nor the names of its
22 * contributors may be used to endorse or promote products derived from
23 * this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 *
37 * Authors: Andrew Bardsley
38 */
39
40 /**
41 * @file
42 *
43 * C++-only configuration and instantiation support. This allows a
44 * config to be read back from a .ini and instantiated without
45 * Python. Useful if you want to embed gem5 within a larger system
46 * without carrying the integration cost of the fully-featured
47 * configuration system.
48 *
49 * This file contains a demonstration main using CxxConfigManager.
50 * Build with something like:
51 *
52 * scons --without-python build/ARM/libgem5_opt.so
53 *
54 * g++ -DTRACING_ON -std=c++0x -Ibuild/ARM src/sim/cxx_main.cc \
55 * -o gem5cxx.opt -Lbuild/ARM -lgem5_opt
56 */
57
58 #include <cstdlib>
59 #include <iostream>
60 #include <sstream>
61
62 #include "base/inifile.hh"
63 #include "base/statistics.hh"
64 #include "base/str.hh"
65 #include "base/trace.hh"
66 #include "cpu/base.hh"
67 #include "sim/cxx_config_ini.hh"
68 #include "sim/cxx_manager.hh"
69 #include "sim/init_signals.hh"
70 #include "sim/serialize.hh"
71 #include "sim/simulate.hh"
72 #include "sim/stat_control.hh"
73 #include "sim/system.hh"
74 #include "stats.hh"
75
76 void
77 usage(const std::string &prog_name)
78 {
79 std::cerr << "Usage: " << prog_name << (
80 " <config-file.ini> [ <option> ]\n\n"
81 "OPTIONS:\n"
82 " -p <object> <param> <value> -- set a parameter\n"
83 " -v <object> <param> <values> -- set a vector parameter from"
84 " a comma\n"
85 " separated values string\n"
86 " -d <flag> -- set a debug flag (-<flag>\n"
87 " clear a flag)\n"
88 " -s <dir> <ticks> -- save checkpoint to dir after"
89 " the given\n"
90 " number of ticks\n"
91 " -r <dir> -- restore checkpoint from dir\n"
92 " -c <from> <to> <ticks> -- switch from cpu 'from' to cpu"
93 " 'to' after\n"
94 " the given number of ticks\n"
95 "\n"
96 );
97
98 std::exit(EXIT_FAILURE);
99 }
100
101 int
102 main(int argc, char **argv)
103 {
104 std::string prog_name(argv[0]);
105 unsigned int arg_ptr = 1;
106
107 if (argc == 1)
108 usage(prog_name);
109
110 cxxConfigInit();
111
112 initSignals();
113
114 setClockFrequency(1000000000000);
115 curEventQueue(getEventQueue(0));
116
117 Stats::initSimStats();
118 Stats::registerHandlers(CxxConfig::statsReset, CxxConfig::statsDump);
119
120 Trace::enable();
121 setDebugFlag("Terminal");
122 // setDebugFlag("CxxConfig");
123
124 const std::string config_file(argv[arg_ptr]);
125
126 CxxConfigFileBase *conf = new CxxIniFile();
127
128 if (!conf->load(config_file.c_str())) {
129 std::cerr << "Can't open config file: " << config_file << '\n';
130 return EXIT_FAILURE;
131 }
132 arg_ptr++;
133
134 CxxConfigManager *config_manager = new CxxConfigManager(*conf);
135
136 bool checkpoint_restore = false;
137 bool checkpoint_save = false;
138 bool switch_cpus = false;
139 std::string checkpoint_dir = "";
140 std::string from_cpu = "";
141 std::string to_cpu = "";
142 Tick pre_run_time = 1000000;
143 Tick pre_switch_time = 1000000;
144
145 try {
146 while (arg_ptr < argc) {
147 std::string option(argv[arg_ptr]);
148 arg_ptr++;
149 unsigned num_args = argc - arg_ptr;
150
151 if (option == "-p") {
152 if (num_args < 3)
153 usage(prog_name);
154 config_manager->setParam(argv[arg_ptr], argv[arg_ptr + 1],
155 argv[arg_ptr + 2]);
156 arg_ptr += 3;
157 } else if (option == "-v") {
158 std::vector<std::string> values;
159
160 if (num_args < 3)
161 usage(prog_name);
162 tokenize(values, argv[arg_ptr + 2], ',');
163 config_manager->setParamVector(argv[arg_ptr],
164 argv[arg_ptr + 1], values);
165 arg_ptr += 3;
166 } else if (option == "-d") {
167 if (num_args < 1)
168 usage(prog_name);
169 if (argv[arg_ptr][0] == '-')
170 clearDebugFlag(argv[arg_ptr] + 1);
171 else
172 setDebugFlag(argv[arg_ptr]);
173 arg_ptr++;
174 } else if (option == "-r") {
175 if (num_args < 1)
176 usage(prog_name);
177 checkpoint_dir = argv[arg_ptr];
178 checkpoint_restore = true;
179 arg_ptr++;
180 } else if (option == "-s") {
181 if (num_args < 2)
182 usage(prog_name);
183 checkpoint_dir = argv[arg_ptr];
184 std::istringstream(argv[arg_ptr + 1]) >> pre_run_time;
185 checkpoint_save = true;
186 arg_ptr += 2;
187 } else if (option == "-c") {
188 if (num_args < 3)
189 usage(prog_name);
190 switch_cpus = true;
191 from_cpu = argv[arg_ptr];
192 to_cpu = argv[arg_ptr + 1];
193 std::istringstream(argv[arg_ptr + 2]) >> pre_switch_time;
194 arg_ptr += 3;
195 } else {
196 usage(prog_name);
197 }
198 }
199 } catch (CxxConfigManager::Exception &e) {
200 std::cerr << e.name << ": " << e.message << "\n";
201 return EXIT_FAILURE;
202 }
203
204 if (checkpoint_save && checkpoint_restore) {
205 std::cerr << "Don't try and save and restore a checkpoint in the"
206 " same run\n";
207 return EXIT_FAILURE;
208 }
209
210 CxxConfig::statsEnable();
211 getEventQueue(0)->dump();
212
213 try {
214 config_manager->instantiate();
215 if (!checkpoint_restore) {
216 config_manager->initState();
217 config_manager->startup();
218 }
219 } catch (CxxConfigManager::Exception &e) {
220 std::cerr << "Config problem in sim object " << e.name
221 << ": " << e.message << "\n";
222
223 return EXIT_FAILURE;
224 }
225
226 GlobalSimLoopExitEvent *exit_event = NULL;
227
228 if (checkpoint_save) {
229 exit_event = simulate(pre_run_time);
230
231 unsigned int drain_count = 1;
232 do {
233 drain_count = config_manager->drain();
234
235 std::cerr << "Draining " << drain_count << '\n';
236
237 if (drain_count > 0) {
238 exit_event = simulate();
239 }
240 } while (drain_count > 0);
241
242 std::cerr << "Simulation stop at tick " << curTick()
243 << ", cause: " << exit_event->getCause() << '\n';
244
245 std::cerr << "Checkpointing\n";
246
247 /* FIXME, this should really be serialising just for
248 * config_manager rather than using serializeAll's ugly
249 * SimObject static object list */
250 Serializable::serializeAll(checkpoint_dir);
251
252 std::cerr << "Completed checkpoint\n";
253
254 config_manager->drainResume();
255 }
256
257 if (checkpoint_restore) {
258 std::cerr << "Restoring checkpoint\n";
259
260 CheckpointIn *checkpoint = new CheckpointIn(checkpoint_dir,
261 config_manager->getSimObjectResolver());
262
263 DrainManager::instance().preCheckpointRestore();
264 Serializable::unserializeGlobals(*checkpoint);
265 config_manager->loadState(*checkpoint);
266 config_manager->startup();
267
268 config_manager->drainResume();
269
270 std::cerr << "Restored from checkpoint\n";
271 }
272
273 if (switch_cpus) {
274 exit_event = simulate(pre_switch_time);
275
276 std::cerr << "Switching CPU\n";
277
278 /* Assume the system is called system */
279 System &system = config_manager->getObject<System>("system");
280 BaseCPU &old_cpu = config_manager->getObject<BaseCPU>(from_cpu);
281 BaseCPU &new_cpu = config_manager->getObject<BaseCPU>(to_cpu);
282
283 unsigned int drain_count = 1;
284 do {
285 drain_count = config_manager->drain();
286
287 std::cerr << "Draining " << drain_count << '\n';
288
289 if (drain_count > 0) {
290 exit_event = simulate();
291 }
292 } while (drain_count > 0);
293
294 old_cpu.switchOut();
295 system.setMemoryMode(Enums::timing);
296 new_cpu.takeOverFrom(&old_cpu);
297 config_manager->drainResume();
298
299 std::cerr << "Switched CPU\n";
300 }
301
302 exit_event = simulate();
303
304 std::cerr << "Exit at tick " << curTick()
305 << ", cause: " << exit_event->getCause() << '\n';
306
307 getEventQueue(0)->dump();
308
309 #if TRY_CLEAN_DELETE
310 config_manager->deleteObjects();
311 #endif
312
313 delete config_manager;
314
315 return EXIT_SUCCESS;
316 }