diff options
author | Andreas Sandberg <andreas@sandberg.pp.se> | 2015-11-26 10:11:57 +0000 |
---|---|---|
committer | Andreas Sandberg <andreas@sandberg.pp.se> | 2015-11-26 10:11:57 +0000 |
commit | a91c1e69a880dd6eec3cc980801ea18ddcbe7c31 (patch) | |
tree | 14f6f2b498e035e163b50bb88fede28be4a45f00 /src | |
parent | 6de52699bb10ec445e45400b419bb61122a68162 (diff) | |
download | gem5-a91c1e69a880dd6eec3cc980801ea18ddcbe7c31.tar.xz |
sim: Add support for forking
This changeset adds forking capabilities to the gem5 python scripts. A fork
method is added to simulate.py. This method is responsible for forking the
simulator itself, and will direct all output files to a new output directory
based on the fork sequence number. 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 fork method provides the option to specify if the system should be
drained prior to forking, or not. By default the system is drained to ensure
that there are no in-flight transactions.
When forking the simulator, the fork method returns the PID of the child
process, or returns 0 if running in the child. This is in line with the standard
Python forking interface.
Signed-off-by: Andreas Sandberg <andreas@sandberg.pp.se>
[sascha.bischoff@arm.com: Rebased patches onto a newer gem5 version]
Signed-off-by: Sascha Bischoff <sascha.bischoff@arm.com>
[andreas.sandberg@arm.com: Updated to comply with modern draining semantics ]
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/python/m5/simulate.py | 53 | ||||
-rw-r--r-- | src/python/swig/core.i | 3 |
2 files changed, 56 insertions, 0 deletions
diff --git a/src/python/m5/simulate.py b/src/python/m5/simulate.py index d89629d2d..5d70f4b18 100644 --- a/src/python/m5/simulate.py +++ b/src/python/m5/simulate.py @@ -312,4 +312,57 @@ def notifyFork(root): 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 --git a/src/python/swig/core.i b/src/python/swig/core.i index 862c0e37e..08fbe01d1 100644 --- a/src/python/swig/core.i +++ b/src/python/swig/core.i @@ -55,6 +55,8 @@ const bool flag_TRACING_ON = TRACING_ON; inline void disableAllListeners() { ListenSocket::disableAll(); } +inline bool listenersDisabled() { return ListenSocket::allDisabled(); } + inline void seedRandom(uint64_t seed) { @@ -71,6 +73,7 @@ seedRandom(uint64_t seed) void setOutputDir(const std::string &dir); void doExitCleanup(); void disableAllListeners(); +bool listenersDisabled(); void seedRandom(uint64_t seed); %immutable compileDate; |