2 * Copyright (c) 2014 ARM Limited
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.
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.
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.
37 * Authors: Andrew Bardsley
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.
49 * This file contains a demonstration main using CxxConfigManager.
50 * Build with something like:
52 * scons --without-python build/ARM/libgem5_opt.so
54 * g++ -DTRACING_ON -std=c++0x -Ibuild/ARM src/sim/cxx_main.cc \
55 * -o gem5cxx.opt -Lbuild/ARM -lgem5_opt
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"
77 usage(const std::string
&prog_name
)
79 std::cerr
<< "Usage: " << prog_name
<< (
80 " <config-file.ini> [ <option> ]\n\n"
82 " -p <object> <param> <value> -- set a parameter\n"
83 " -v <object> <param> <values> -- set a vector parameter from"
85 " separated values string\n"
86 " -d <flag> -- set a debug flag (-<flag>\n"
88 " -s <dir> <ticks> -- save checkpoint to dir after"
91 " -r <dir> -- restore checkpoint from dir\n"
92 " -c <from> <to> <ticks> -- switch from cpu 'from' to cpu"
94 " the given number of ticks\n"
98 std::exit(EXIT_FAILURE
);
102 main(int argc
, char **argv
)
104 std::string
prog_name(argv
[0]);
105 unsigned int arg_ptr
= 1;
114 setClockFrequency(1000000000000);
115 curEventQueue(getEventQueue(0));
117 Stats::initSimStats();
118 Stats::registerHandlers(CxxConfig::statsReset
, CxxConfig::statsDump
);
121 setDebugFlag("Terminal");
122 // setDebugFlag("CxxConfig");
124 const std::string
config_file(argv
[arg_ptr
]);
126 CxxConfigFileBase
*conf
= new CxxIniFile();
128 if (!conf
->load(config_file
.c_str())) {
129 std::cerr
<< "Can't open config file: " << config_file
<< '\n';
134 CxxConfigManager
*config_manager
= new CxxConfigManager(*conf
);
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;
146 while (arg_ptr
< argc
) {
147 std::string
option(argv
[arg_ptr
]);
149 unsigned num_args
= argc
- arg_ptr
;
151 if (option
== "-p") {
154 config_manager
->setParam(argv
[arg_ptr
], argv
[arg_ptr
+ 1],
157 } else if (option
== "-v") {
158 std::vector
<std::string
> values
;
162 tokenize(values
, argv
[arg_ptr
+ 2], ',');
163 config_manager
->setParamVector(argv
[arg_ptr
],
164 argv
[arg_ptr
+ 1], values
);
166 } else if (option
== "-d") {
169 if (argv
[arg_ptr
][0] == '-')
170 clearDebugFlag(argv
[arg_ptr
] + 1);
172 setDebugFlag(argv
[arg_ptr
]);
174 } else if (option
== "-r") {
177 checkpoint_dir
= argv
[arg_ptr
];
178 checkpoint_restore
= true;
180 } else if (option
== "-s") {
183 checkpoint_dir
= argv
[arg_ptr
];
184 std::istringstream(argv
[arg_ptr
+ 1]) >> pre_run_time
;
185 checkpoint_save
= true;
187 } else if (option
== "-c") {
191 from_cpu
= argv
[arg_ptr
];
192 to_cpu
= argv
[arg_ptr
+ 1];
193 std::istringstream(argv
[arg_ptr
+ 2]) >> pre_switch_time
;
199 } catch (CxxConfigManager::Exception
&e
) {
200 std::cerr
<< e
.name
<< ": " << e
.message
<< "\n";
204 if (checkpoint_save
&& checkpoint_restore
) {
205 std::cerr
<< "Don't try and save and restore a checkpoint in the"
210 CxxConfig::statsEnable();
211 getEventQueue(0)->dump();
214 config_manager
->instantiate();
215 if (!checkpoint_restore
) {
216 config_manager
->initState();
217 config_manager
->startup();
219 } catch (CxxConfigManager::Exception
&e
) {
220 std::cerr
<< "Config problem in sim object " << e
.name
221 << ": " << e
.message
<< "\n";
226 GlobalSimLoopExitEvent
*exit_event
= NULL
;
228 if (checkpoint_save
) {
229 exit_event
= simulate(pre_run_time
);
231 unsigned int drain_count
= 1;
233 drain_count
= config_manager
->drain();
235 std::cerr
<< "Draining " << drain_count
<< '\n';
237 if (drain_count
> 0) {
238 exit_event
= simulate();
240 } while (drain_count
> 0);
242 std::cerr
<< "Simulation stop at tick " << curTick()
243 << ", cause: " << exit_event
->getCause() << '\n';
245 std::cerr
<< "Checkpointing\n";
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
);
252 std::cerr
<< "Completed checkpoint\n";
254 config_manager
->drainResume();
257 if (checkpoint_restore
) {
258 std::cerr
<< "Restoring checkpoint\n";
260 CheckpointIn
*checkpoint
= new CheckpointIn(checkpoint_dir
,
261 config_manager
->getSimObjectResolver());
263 DrainManager::instance().preCheckpointRestore();
264 Serializable::unserializeGlobals(*checkpoint
);
265 config_manager
->loadState(*checkpoint
);
266 config_manager
->startup();
268 config_manager
->drainResume();
270 std::cerr
<< "Restored from checkpoint\n";
274 exit_event
= simulate(pre_switch_time
);
276 std::cerr
<< "Switching CPU\n";
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
);
283 unsigned int drain_count
= 1;
285 drain_count
= config_manager
->drain();
287 std::cerr
<< "Draining " << drain_count
<< '\n';
289 if (drain_count
> 0) {
290 exit_event
= simulate();
292 } while (drain_count
> 0);
295 system
.setMemoryMode(Enums::timing
);
296 new_cpu
.takeOverFrom(&old_cpu
);
297 config_manager
->drainResume();
299 std::cerr
<< "Switched CPU\n";
302 exit_event
= simulate();
304 std::cerr
<< "Exit at tick " << curTick()
305 << ", cause: " << exit_event
->getCause() << '\n';
307 getEventQueue(0)->dump();
310 config_manager
->deleteObjects();
313 delete config_manager
;