diff -r b421f396097a -r a3879dcefc27 src/python/m5/simulate.py --- a/src/python/m5/simulate.py Thu Nov 26 10:11:52 2015 +0000 +++ b/src/python/m5/simulate.py Thu Nov 26 10:11:57 2015 +0000 @@ -312,4 +312,57 @@ for obj in root.descendants(): obj.notifyFork() +fork_count = 0 +def fork(simout="%(parent)s.f%(fork_seq)i"): + """Fork the simulator. + + This function forks the simulator. After forking the simulator, + the child process gets its output files redirected to a new output + directory. The default name of the output directory is the same as + the parent with the suffix ".fN" added where N is the fork + sequence number. The name of the output directory can be + overridden using the simout keyword argument. + + Output file formatting dictionary: + parent -- Path to the parent process's output directory. + fork_seq -- Fork sequence number. + pid -- PID of the child process. + + Keyword Arguments: + simout -- New simulation output directory. + + Return Value: + pid of the child process or 0 if running in the child. + """ + from m5 import options + global fork_count + + if not internal.core.listenersDisabled(): + raise RuntimeError, "Can not fork a simulator with listeners enabled" + + drain() + + try: + pid = os.fork() + except OSError, e: + raise e + + if pid == 0: + # In child, notify objects of the fork + root = objects.Root.getInstance() + notifyFork(root) + # Setup a new output directory + parent = options.outdir + options.outdir = simout % { + "parent" : parent, + "fork_seq" : fork_count, + "pid" : os.getpid(), + } + core.setOutputDir(options.outdir) + else: + fork_count += 1 + + return pid + from internal.core import disableAllListeners +from internal.core import listenersDisabled diff -r b421f396097a -r a3879dcefc27 src/python/swig/core.i --- a/src/python/swig/core.i Thu Nov 26 10:11:52 2015 +0000 +++ b/src/python/swig/core.i Thu Nov 26 10:11:57 2015 +0000 @@ -55,6 +55,8 @@ inline void disableAllListeners() { ListenSocket::disableAll(); } +inline bool listenersDisabled() { return ListenSocket::allDisabled(); } + inline void seedRandom(uint64_t seed) { @@ -71,6 +73,7 @@ void setOutputDir(const std::string &dir); void doExitCleanup(); void disableAllListeners(); +bool listenersDisabled(); void seedRandom(uint64_t seed); %immutable compileDate;