113540Sandrea.mondelli@ucf.edu#! /usr/bin/env python2.7
27489Ssteve.reinhardt@amd.com
37489Ssteve.reinhardt@amd.com# Copyright (c) 2010 Advanced Micro Devices, Inc.
47489Ssteve.reinhardt@amd.com# All rights reserved.
57489Ssteve.reinhardt@amd.com#
67489Ssteve.reinhardt@amd.com# Redistribution and use in source and binary forms, with or without
77489Ssteve.reinhardt@amd.com# modification, are permitted provided that the following conditions are
87489Ssteve.reinhardt@amd.com# met: redistributions of source code must retain the above copyright
97489Ssteve.reinhardt@amd.com# notice, this list of conditions and the following disclaimer;
107489Ssteve.reinhardt@amd.com# redistributions in binary form must reproduce the above copyright
117489Ssteve.reinhardt@amd.com# notice, this list of conditions and the following disclaimer in the
127489Ssteve.reinhardt@amd.com# documentation and/or other materials provided with the distribution;
137489Ssteve.reinhardt@amd.com# neither the name of the copyright holders nor the names of its
147489Ssteve.reinhardt@amd.com# contributors may be used to endorse or promote products derived from
157489Ssteve.reinhardt@amd.com# this software without specific prior written permission.
167489Ssteve.reinhardt@amd.com#
177489Ssteve.reinhardt@amd.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
187489Ssteve.reinhardt@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
197489Ssteve.reinhardt@amd.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
207489Ssteve.reinhardt@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
217489Ssteve.reinhardt@amd.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
227489Ssteve.reinhardt@amd.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
237489Ssteve.reinhardt@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
247489Ssteve.reinhardt@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
257489Ssteve.reinhardt@amd.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
267489Ssteve.reinhardt@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
277489Ssteve.reinhardt@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
287489Ssteve.reinhardt@amd.com#
297489Ssteve.reinhardt@amd.com# Author: Steve Reinhardt
307489Ssteve.reinhardt@amd.com#
317489Ssteve.reinhardt@amd.com
327489Ssteve.reinhardt@amd.com# Basic test script for checkpointing.
337489Ssteve.reinhardt@amd.com#
347489Ssteve.reinhardt@amd.com# Given an M5 command and an interval (in ticks), this script will:
357489Ssteve.reinhardt@amd.com# 1. Run the command, dumping periodic checkpoints at the given interval.
367489Ssteve.reinhardt@amd.com# 2. Rerun the command for each pair of adjacent checkpoints:
377489Ssteve.reinhardt@amd.com#    a. Restore from checkpoint N
387489Ssteve.reinhardt@amd.com#    b. Run until the timestamp of checkpoint N+1
397489Ssteve.reinhardt@amd.com#    c. Dump a checkpoint and end the simulation
407489Ssteve.reinhardt@amd.com#    d. Diff the new checkpoint with the original checkpoint N+1
417489Ssteve.reinhardt@amd.com#
427489Ssteve.reinhardt@amd.com# Note that '--' must be used to separate the script options from the
437489Ssteve.reinhardt@amd.com# M5 command line.
447489Ssteve.reinhardt@amd.com#
457489Ssteve.reinhardt@amd.com# Caveats:
467489Ssteve.reinhardt@amd.com#
477489Ssteve.reinhardt@amd.com# - This script relies on the checkpoint options implemented in
487489Ssteve.reinhardt@amd.com#   configs/common/Simulation.py, so it works with commands based on
497489Ssteve.reinhardt@amd.com#   the se.py and fs.py scripts in configs/example, but does not work
507489Ssteve.reinhardt@amd.com#   directly with the existing regression tests.
517489Ssteve.reinhardt@amd.com# - Interleaving simulator and program output can cause discrepancies
527489Ssteve.reinhardt@amd.com#   in the file position checkpoint information since different runs
537489Ssteve.reinhardt@amd.com#   have different amount of simulator output.
547489Ssteve.reinhardt@amd.com# - Probably lots more issues we don't even know about yet.
557489Ssteve.reinhardt@amd.com#
567489Ssteve.reinhardt@amd.com# Examples:
577489Ssteve.reinhardt@amd.com#
587489Ssteve.reinhardt@amd.com# util/checkpoint-tester.py -i 400000 -- build/ALPHA_SE/m5.opt \
597489Ssteve.reinhardt@amd.com#      configs/example/se.py -c tests/test-progs/hello/bin/alpha/tru64/hello \
607489Ssteve.reinhardt@amd.com#      --output=progout --errout=progerr
617489Ssteve.reinhardt@amd.com#
627489Ssteve.reinhardt@amd.com# util/checkpoint-tester.py -i 200000000000 -- build/ALPHA_FS/m5.opt \
637489Ssteve.reinhardt@amd.com#      configs/example/fs.py --script tests/halt.sh
647489Ssteve.reinhardt@amd.com#
657489Ssteve.reinhardt@amd.com
667489Ssteve.reinhardt@amd.com
677489Ssteve.reinhardt@amd.comimport os, sys, re
687489Ssteve.reinhardt@amd.comimport subprocess
697489Ssteve.reinhardt@amd.comimport optparse
707489Ssteve.reinhardt@amd.com
717489Ssteve.reinhardt@amd.comparser = optparse.OptionParser()
727489Ssteve.reinhardt@amd.com
737489Ssteve.reinhardt@amd.comparser.add_option('-i', '--interval', type='int')
747489Ssteve.reinhardt@amd.comparser.add_option('-d', '--directory', default='checkpoint-test')
757489Ssteve.reinhardt@amd.com
767489Ssteve.reinhardt@amd.com(options, args) = parser.parse_args()
777489Ssteve.reinhardt@amd.com
787489Ssteve.reinhardt@amd.cominterval = options.interval
797489Ssteve.reinhardt@amd.com
807489Ssteve.reinhardt@amd.comif os.path.exists(options.directory):
817489Ssteve.reinhardt@amd.com    print 'Error: test directory', options.directory, 'exists'
827489Ssteve.reinhardt@amd.com    print '       Tester needs to create directory from scratch'
837489Ssteve.reinhardt@amd.com    sys.exit(1)
847489Ssteve.reinhardt@amd.com
857489Ssteve.reinhardt@amd.comtop_dir = options.directory
867489Ssteve.reinhardt@amd.comos.mkdir(top_dir)
877489Ssteve.reinhardt@amd.com
887489Ssteve.reinhardt@amd.comcmd_echo = open(os.path.join(top_dir, 'command'), 'w')
897489Ssteve.reinhardt@amd.comprint >>cmd_echo, ' '.join(sys.argv)
907489Ssteve.reinhardt@amd.comcmd_echo.close()
917489Ssteve.reinhardt@amd.com
927489Ssteve.reinhardt@amd.comm5_binary = args[0]
937489Ssteve.reinhardt@amd.com
947489Ssteve.reinhardt@amd.comoptions = args[1:]
957489Ssteve.reinhardt@amd.com
967489Ssteve.reinhardt@amd.cominitial_args = ['--take-checkpoints', '%d,%d' % (interval, interval)]
977489Ssteve.reinhardt@amd.com
987489Ssteve.reinhardt@amd.comcptdir = os.path.join(top_dir, 'm5out')
997489Ssteve.reinhardt@amd.com
1007489Ssteve.reinhardt@amd.comprint '===> Running initial simulation.'
1017489Ssteve.reinhardt@amd.comsubprocess.call([m5_binary] + ['-red', cptdir] + options + initial_args)
1027489Ssteve.reinhardt@amd.com
1037489Ssteve.reinhardt@amd.comdirs = os.listdir(cptdir)
1047489Ssteve.reinhardt@amd.comexpr = re.compile('cpt\.([0-9]*)')
1057489Ssteve.reinhardt@amd.comcpts = []
1067489Ssteve.reinhardt@amd.comfor dir in dirs:
1077489Ssteve.reinhardt@amd.com    match = expr.match(dir)
1087489Ssteve.reinhardt@amd.com    if match:
1097489Ssteve.reinhardt@amd.com        cpts.append(int(match.group(1)))
1107489Ssteve.reinhardt@amd.com
1117489Ssteve.reinhardt@amd.comcpts.sort()
11211320Ssteve.reinhardt@amd.com
1137489Ssteve.reinhardt@amd.com# We test by loading checkpoint N, simulating to (and dumping at)
1147489Ssteve.reinhardt@amd.com# checkpoint N+1, then comparing the resulting checkpoint with the
1157489Ssteve.reinhardt@amd.com# original checkpoint N+1.  Thus the number of tests we can run is one
1167489Ssteve.reinhardt@amd.com# less than tha number of checkpoints.
1177489Ssteve.reinhardt@amd.comfor i in range(1, len(cpts)):
1187489Ssteve.reinhardt@amd.com    print '===> Running test %d of %d.' % (i, len(cpts)-1)
1197489Ssteve.reinhardt@amd.com    mydir = os.path.join(top_dir, 'test.%d' % i)
1207489Ssteve.reinhardt@amd.com    subprocess.call([m5_binary] + ['-red', mydir] + options + initial_args +
1217489Ssteve.reinhardt@amd.com                    ['--max-checkpoints' , '1', '--checkpoint-dir', cptdir,
1227489Ssteve.reinhardt@amd.com                     '--checkpoint-restore', str(i)])
1237489Ssteve.reinhardt@amd.com    cpt_name = 'cpt.%d' % cpts[i]
1247489Ssteve.reinhardt@amd.com    diff_name = os.path.join(mydir, 'diffout')
1257489Ssteve.reinhardt@amd.com    diffout = open(diff_name, 'w')
1267489Ssteve.reinhardt@amd.com    subprocess.call(['diff', '-ru', '-I', '^##.*',
1277489Ssteve.reinhardt@amd.com                     '%s/%s' % (cptdir, cpt_name),
1287489Ssteve.reinhardt@amd.com                     '%s/%s' % (mydir, cpt_name)], stdout=diffout)
1297489Ssteve.reinhardt@amd.com    diffout.close()
1307489Ssteve.reinhardt@amd.com    # print out the diff
1317489Ssteve.reinhardt@amd.com    diffout = open(diff_name)
1327489Ssteve.reinhardt@amd.com    print diffout.read(),
1337489Ssteve.reinhardt@amd.com    diffout.close()
1347489Ssteve.reinhardt@amd.com
13511320Ssteve.reinhardt@amd.com
136