main.cc revision 10538
13806SN/A/* 23806SN/A * Copyright (c) 2014 ARM Limited 33806SN/A * All rights reserved 43806SN/A * 53806SN/A * The license below extends only to copyright in the software and shall 63806SN/A * not be construed as granting a license to any other intellectual 73806SN/A * property including but not limited to intellectual property relating 83806SN/A * to a hardware implementation of the functionality of the software 93806SN/A * licensed hereunder. You may use the software subject to the license 103806SN/A * terms below provided that you ensure that this notice is replicated 113806SN/A * unmodified and in its entirety in all distributions of the software, 123806SN/A * modified or unmodified, in source code or in binary form. 133806SN/A * 143806SN/A * Redistribution and use in source and binary forms, with or without 153806SN/A * modification, are permitted provided that the following conditions are 163806SN/A * met: redistributions of source code must retain the above copyright 173806SN/A * notice, this list of conditions and the following disclaimer; 183806SN/A * redistributions in binary form must reproduce the above copyright 193806SN/A * notice, this list of conditions and the following disclaimer in the 203806SN/A * documentation and/or other materials provided with the distribution; 213806SN/A * neither the name of the copyright holders nor the names of its 223806SN/A * contributors may be used to endorse or promote products derived from 233806SN/A * this software without specific prior written permission. 243806SN/A * 253806SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 263806SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 273806SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 283806SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 293806SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 303806SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 318105Sgblack@eecs.umich.edu * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 328105Sgblack@eecs.umich.edu * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 333806SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 343806SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 353806SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 363806SN/A * 373806SN/A * Authors: Andrew Bardsley 383806SN/A */ 393806SN/A 409897Sandreas@sandberg.pp.se/** 418229Snate@binkert.org * @file 423806SN/A * 433806SN/A * C++-only configuration and instantiation support. This allows a 443806SN/A * config to be read back from a .ini and instantiated without 453806SN/A * Python. Useful if you want to embed gem5 within a larger system 463806SN/A * without carrying the integration cost of the fully-featured 477741SN/A * configuration system. 489180Sandreas.hansson@arm.com * 493806SN/A * This file contains a demonstration main using CxxConfigManager. 503806SN/A * Build with something like: 519897Sandreas@sandberg.pp.se * 529897Sandreas@sandberg.pp.se * scons --without-python build/ARM/libgem5_opt.so 539897Sandreas@sandberg.pp.se * 5412406Sgabeblack@google.com * g++ -DTRACING_ON -std=c++0x -Ibuild/ARM src/sim/cxx_main.cc \ 553806SN/A * -o gem5cxx.opt -Lbuild/ARM -lgem5_opt 563806SN/A */ 579180Sandreas.hansson@arm.com 583806SN/A#include <cstdlib> 593806SN/A#include <iostream> 609897Sandreas@sandberg.pp.se#include <sstream> 619897Sandreas@sandberg.pp.se 629897Sandreas@sandberg.pp.se#include "base/inifile.hh" 6312406Sgabeblack@google.com#include "base/statistics.hh" 643806SN/A#include "base/str.hh" 653806SN/A#include "base/trace.hh" 663806SN/A#include "cpu/base.hh" 673806SN/A#include "sim/cxx_config_ini.hh" 683806SN/A#include "sim/cxx_manager.hh" 693806SN/A#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 76void 77usage(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 101int 102main(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::enabled = true; 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 DrainManager drain_manager; 232 unsigned int drain_count = 1; 233 do { 234 drain_count = config_manager->drain(&drain_manager); 235 236 std::cerr << "Draining " << drain_count << '\n'; 237 238 if (drain_count > 0) { 239 drain_manager.setCount(drain_count); 240 exit_event = simulate(); 241 } 242 } while (drain_count > 0); 243 244 std::cerr << "Simulation stop at tick " << curTick() 245 << ", cause: " << exit_event->getCause() << '\n'; 246 247 std::cerr << "Checkpointing\n"; 248 249 /* FIXME, this should really be serialising just for 250 * config_manager rather than using serializeAll's ugly 251 * SimObject static object list */ 252 Serializable::serializeAll(checkpoint_dir); 253 254 std::cerr << "Completed checkpoint\n"; 255 256 config_manager->drainResume(); 257 } 258 259 if (checkpoint_restore) { 260 std::cerr << "Restoring checkpoint\n"; 261 262 Checkpoint *checkpoint = new Checkpoint(checkpoint_dir, 263 config_manager->getSimObjectResolver()); 264 265 Serializable::unserializeGlobals(checkpoint); 266 config_manager->loadState(checkpoint); 267 config_manager->startup(); 268 269 config_manager->drainResume(); 270 271 std::cerr << "Restored from checkpoint\n"; 272 } 273 274 if (switch_cpus) { 275 exit_event = simulate(pre_switch_time); 276 277 std::cerr << "Switching CPU\n"; 278 279 /* Assume the system is called system */ 280 System &system = config_manager->getObject<System>("system"); 281 BaseCPU &old_cpu = config_manager->getObject<BaseCPU>(from_cpu); 282 BaseCPU &new_cpu = config_manager->getObject<BaseCPU>(to_cpu); 283 284 DrainManager drain_manager; 285 unsigned int drain_count = 1; 286 do { 287 drain_count = config_manager->drain(&drain_manager); 288 289 std::cerr << "Draining " << drain_count << '\n'; 290 291 if (drain_count > 0) { 292 drain_manager.setCount(drain_count); 293 exit_event = simulate(); 294 } 295 } while (drain_count > 0); 296 297 old_cpu.switchOut(); 298 system.setMemoryMode(Enums::timing); 299 new_cpu.takeOverFrom(&old_cpu); 300 config_manager->drainResume(); 301 302 std::cerr << "Switched CPU\n"; 303 } 304 305 exit_event = simulate(); 306 307 std::cerr << "Exit at tick " << curTick() 308 << ", cause: " << exit_event->getCause() << '\n'; 309 310 getEventQueue(0)->dump(); 311 312#if TRY_CLEAN_DELETE 313 config_manager->deleteObjects(); 314#endif 315 316 delete config_manager; 317 318 return EXIT_SUCCESS; 319} 320