1#! /usr/bin/env python2.7
2
3# Copyright (c) 2010 Advanced Micro Devices, Inc.
4# All rights reserved.
5#
6# Redistribution and use in source and binary forms, with or without
7# modification, are permitted provided that the following conditions are
8# met: redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer;
10# redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution;
13# neither the name of the copyright holders nor the names of its
14# contributors may be used to endorse or promote products derived from
15# this software without specific prior written permission.
16#
17# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28#
29# Author: Steve Reinhardt
30#
31
32# Basic test script for checkpointing.
33#
34# Given an M5 command and an interval (in ticks), this script will:
35# 1. Run the command, dumping periodic checkpoints at the given interval.
36# 2. Rerun the command for each pair of adjacent checkpoints:
37#    a. Restore from checkpoint N
38#    b. Run until the timestamp of checkpoint N+1
39#    c. Dump a checkpoint and end the simulation
40#    d. Diff the new checkpoint with the original checkpoint N+1
41#
42# Note that '--' must be used to separate the script options from the
43# M5 command line.
44#
45# Caveats:
46#
47# - This script relies on the checkpoint options implemented in
48#   configs/common/Simulation.py, so it works with commands based on
49#   the se.py and fs.py scripts in configs/example, but does not work
50#   directly with the existing regression tests.
51# - Interleaving simulator and program output can cause discrepancies
52#   in the file position checkpoint information since different runs
53#   have different amount of simulator output.
54# - Probably lots more issues we don't even know about yet.
55#
56# Examples:
57#
58# util/checkpoint-tester.py -i 400000 -- build/ALPHA_SE/m5.opt \
59#      configs/example/se.py -c tests/test-progs/hello/bin/alpha/tru64/hello \
60#      --output=progout --errout=progerr
61#
62# util/checkpoint-tester.py -i 200000000000 -- build/ALPHA_FS/m5.opt \
63#      configs/example/fs.py --script tests/halt.sh
64#
65
66
67import os, sys, re
68import subprocess
69import optparse
70
71parser = optparse.OptionParser()
72
73parser.add_option('-i', '--interval', type='int')
74parser.add_option('-d', '--directory', default='checkpoint-test')
75
76(options, args) = parser.parse_args()
77
78interval = options.interval
79
80if os.path.exists(options.directory):
81    print 'Error: test directory', options.directory, 'exists'
82    print '       Tester needs to create directory from scratch'
83    sys.exit(1)
84
85top_dir = options.directory
86os.mkdir(top_dir)
87
88cmd_echo = open(os.path.join(top_dir, 'command'), 'w')
89print >>cmd_echo, ' '.join(sys.argv)
90cmd_echo.close()
91
92m5_binary = args[0]
93
94options = args[1:]
95
96initial_args = ['--take-checkpoints', '%d,%d' % (interval, interval)]
97
98cptdir = os.path.join(top_dir, 'm5out')
99
100print '===> Running initial simulation.'
101subprocess.call([m5_binary] + ['-red', cptdir] + options + initial_args)
102
103dirs = os.listdir(cptdir)
104expr = re.compile('cpt\.([0-9]*)')
105cpts = []
106for dir in dirs:
107    match = expr.match(dir)
108    if match:
109        cpts.append(int(match.group(1)))
110
111cpts.sort()
112
113# We test by loading checkpoint N, simulating to (and dumping at)
114# checkpoint N+1, then comparing the resulting checkpoint with the
115# original checkpoint N+1.  Thus the number of tests we can run is one
116# less than tha number of checkpoints.
117for i in range(1, len(cpts)):
118    print '===> Running test %d of %d.' % (i, len(cpts)-1)
119    mydir = os.path.join(top_dir, 'test.%d' % i)
120    subprocess.call([m5_binary] + ['-red', mydir] + options + initial_args +
121                    ['--max-checkpoints' , '1', '--checkpoint-dir', cptdir,
122                     '--checkpoint-restore', str(i)])
123    cpt_name = 'cpt.%d' % cpts[i]
124    diff_name = os.path.join(mydir, 'diffout')
125    diffout = open(diff_name, 'w')
126    subprocess.call(['diff', '-ru', '-I', '^##.*',
127                     '%s/%s' % (cptdir, cpt_name),
128                     '%s/%s' % (mydir, cpt_name)], stdout=diffout)
129    diffout.close()
130    # print out the diff
131    diffout = open(diff_name)
132    print diffout.read(),
133    diffout.close()
134
135
136