checkpoint.py revision 10751
14997SN/A# Copyright (c) 2015 ARM Limited 27944SN/A# All rights reserved. 37944SN/A# 47944SN/A# The license below extends only to copyright in the software and shall 57944SN/A# not be construed as granting a license to any other intellectual 67944SN/A# property including but not limited to intellectual property relating 77944SN/A# to a hardware implementation of the functionality of the software 87944SN/A# licensed hereunder. You may use the software subject to the license 97944SN/A# terms below provided that you ensure that this notice is replicated 107944SN/A# unmodified and in its entirety in all distributions of the software, 117944SN/A# modified or unmodified, in source code or in binary form. 127944SN/A# 137944SN/A# Redistribution and use in source and binary forms, with or without 144997SN/A# modification, are permitted provided that the following conditions are 154997SN/A# met: redistributions of source code must retain the above copyright 164997SN/A# notice, this list of conditions and the following disclaimer; 174997SN/A# redistributions in binary form must reproduce the above copyright 184997SN/A# notice, this list of conditions and the following disclaimer in the 194997SN/A# documentation and/or other materials provided with the distribution; 204997SN/A# neither the name of the copyright holders nor the names of its 214997SN/A# contributors may be used to endorse or promote products derived from 224997SN/A# this software without specific prior written permission. 234997SN/A# 244997SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 254997SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 264997SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 274997SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 284997SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 294997SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 304997SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 314997SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 324997SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 334997SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 344997SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 354997SN/A# 364997SN/A# Authors: Andreas Sandberg 374997SN/A 384997SN/Afrom multiprocessing import Process 394997SN/Aimport sys 404997SN/Aimport os 414997SN/A 424997SN/Aimport m5 4310687SAndreas.Sandberg@ARM.comm5.util.addToPath('../configs/common') 4410687SAndreas.Sandberg@ARM.com 454997SN/A_exit_normal = ( 4612334Sgabeblack@google.com "target called exit()", 474997SN/A "m5_exit instruction encountered", 4813892Sgabeblack@google.com ) 494997SN/A 504997SN/A_exit_limit = ( 519294SN/A "simulate() limit reached", 524997SN/A ) 5313892Sgabeblack@google.com 545004SN/A_exitcode_done = 0 555004SN/A_exitcode_fail = 1 5613892Sgabeblack@google.com_exitcode_checkpoint = 42 575004SN/A 584997SN/A 5913741SAndrea.Mondelli@ucf.edudef _run_step(name, restore=None, interval=0.5): 606023SN/A """ 616023SN/A Instantiate (optionally from a checkpoint if restore is set to the 625894SN/A checkpoitn name) the system and run for interval seconds of 635894SN/A simulated time. At the end of the simulation interval, create a 645894SN/A checkpoint and exit. 655894SN/A 665894SN/A As this function is intended to run in its own process using the 675894SN/A multiprocessing framework, the exit is a true call to exit which 687944SN/A terminates the process. Exit codes are used to pass information to 697944SN/A the parent. 707944SN/A """ 717944SN/A if restore is not None: 727944SN/A m5.instantiate(restore) 737944SN/A else: 745894SN/A m5.instantiate() 755894SN/A 765894SN/A e = m5.simulate(m5.ticks.fromSeconds(interval)) 775894SN/A cause = e.getCause() 785894SN/A if cause in _exit_limit: 7912749Sgiacomo.travaglini@arm.com m5.checkpoint(name) 8010379SN/A sys.exit(_exitcode_checkpoint) 819258SN/A elif cause in _exit_normal: 829258SN/A sys.exit(_exitcode_done) 839258SN/A else: 849258SN/A print "Test failed: Unknown exit cause: %s" % cause 859258SN/A sys.exit(_exitcode_fail) 869258SN/A 879258SN/Adef run_test(root, interval=0.5, max_checkpoints=5): 885894SN/A """ 895358SN/A Run the simulated system for a fixed amount of time and take a 905358SN/A checkpoint, then restore from the same checkpoint and run until 9112406Sgabeblack@google.com the system calls m5 exit. 925358SN/A """ 9312406Sgabeblack@google.com 9412749Sgiacomo.travaglini@arm.com cpt_name = os.path.join(m5.options.outdir, "test.cpt") 9512406Sgabeblack@google.com restore = None 9612749Sgiacomo.travaglini@arm.com 9712406Sgabeblack@google.com for cpt_no in range(max_checkpoints): 9812406Sgabeblack@google.com # Create a checkpoint from a separate child process. This enables 9912749Sgiacomo.travaglini@arm.com # us to get back to a (mostly) pristine state and restart 10012406Sgabeblack@google.com # simulation from the checkpoint. 10112406Sgabeblack@google.com p = Process(target=_run_step, 10212406Sgabeblack@google.com args=(cpt_name, ), 1039738SN/A kwargs={ 1049738SN/A "restore" : restore, 1059738SN/A "interval" : interval, 1069738SN/A }) 1079738SN/A p.start() 1089738SN/A 1099738SN/A # Wait for the child to return 1109738SN/A p.join() 1119738SN/A 1129738SN/A # Restore from the checkpoint next iteration 1139738SN/A restore = cpt_name 1149738SN/A 1159738SN/A if p.exitcode == _exitcode_done: 1169738SN/A print >> sys.stderr, "Test done." 1179738SN/A sys.exit(0) 11812406Sgabeblack@google.com elif p.exitcode == _exitcode_checkpoint: 11912749Sgiacomo.travaglini@arm.com pass 12012406Sgabeblack@google.com else: 12112406Sgabeblack@google.com print >> sys.stderr, "Test failed." 12212406Sgabeblack@google.com sys.exit(1) 12312406Sgabeblack@google.com 12412406Sgabeblack@google.com # Maximum number of checkpoints reached. Just run full-speed from 12512406Sgabeblack@google.com # now on. 12612406Sgabeblack@google.com m5.instantiate() 12712406Sgabeblack@google.com e = m5.simulate() 12812406Sgabeblack@google.com cause = e.getCause() 12912406Sgabeblack@google.com if cause in _exit_normal: 13012406Sgabeblack@google.com sys.exit(0) 13112406Sgabeblack@google.com else: 13213784Sgabeblack@google.com print "Test failed: Unknown exit cause: %s" % cause 13312406Sgabeblack@google.com sys.exit(1) 13412406Sgabeblack@google.com