cpt_upgrader.py revision 9293:df7c3f99ebca
1#!/usr/bin/env python 2 3# Copyright (c) 2012 ARM Limited 4# All rights reserved 5# 6# The license below extends only to copyright in the software and shall 7# not be construed as granting a license to any other intellectual 8# property including but not limited to intellectual property relating 9# to a hardware implementation of the functionality of the software 10# licensed hereunder. You may use the software subject to the license 11# terms below provided that you ensure that this notice is replicated 12# unmodified and in its entirety in all distributions of the software, 13# modified or unmodified, in source code or in binary form. 14# 15# Redistribution and use in source and binary forms, with or without 16# modification, are permitted provided that the following conditions are 17# met: redistributions of source code must retain the above copyright 18# notice, this list of conditions and the following disclaimer; 19# redistributions in binary form must reproduce the above copyright 20# notice, this list of conditions and the following disclaimer in the 21# documentation and/or other materials provided with the distribution; 22# neither the name of the copyright holders nor the names of its 23# contributors may be used to endorse or promote products derived from 24# this software without specific prior written permission. 25# 26# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 27# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 28# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 29# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 30# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 31# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 32# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 33# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 34# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 35# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 36# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 37# 38# Authors: Ali Saidi 39# 40 41# This python code is used to migrate checkpoints that were created in one 42# version of the simulator to newer version. As features are added or bugs are 43# fixed some of the state that needs to be checkpointed can change. If you have 44# many historic checkpoints that you use, manually editing them to fix them is 45# both time consuming and error-prone. 46 47# This script provides a way to migrate checkpoints to the newer repository in 48# a programatic way. It can be imported into another script or used on the 49# command line. From the command line the script will either migrate every 50# checkpoint it finds recursively (-r option) or a single checkpoint. When a 51# change is made to the gem5 repository that breaks previous checkpoints a 52# from_N() method should be implemented here and the gem5CheckpointVersion 53# variable in src/sim/serialize.hh should be incremented. For each version 54# between the checkpoints current version and the new version the from_N() 55# method will be run, passing in a ConfigParser object which contains the open 56# file. As these operations can be isa specific the method can verify the isa 57# and use regexes to find the correct sections that need to be updated. 58 59 60import ConfigParser 61import sys, os 62import os.path as osp 63 64# An example of a translator 65def from_0(cpt): 66 if cpt.get('root','isa') == 'arm': 67 for sec in cpt.sections(): 68 import re 69 # Search for all the execution contexts 70 if re.search('.*sys.*\.cpu.*\.x.\..*', sec): 71 # Update each one 72 mr = cpt.get(sec, 'miscRegs').split() 73 #mr.insert(21,0) 74 #mr.insert(26,0) 75 cpt.set(sec, 'miscRegs', ' '.join(str(x) for x in mr)) 76 77# The backing store supporting the memories in the system has changed 78# in that it is now stored globally per address range. As a result the 79# actual storage is separate from the memory controllers themselves. 80def from_1(cpt): 81 for sec in cpt.sections(): 82 import re 83 # Search for a physical memory 84 if re.search('.*sys.*\.physmem$', sec): 85 # Add the number of stores attribute to the global physmem 86 cpt.set(sec, 'nbr_of_stores', '1') 87 88 # Get the filename and size as this is moving to the 89 # specific backing store 90 mem_filename = cpt.get(sec, 'filename') 91 mem_size = cpt.get(sec, '_size') 92 cpt.remove_option(sec, 'filename') 93 cpt.remove_option(sec, '_size') 94 95 # Get the name so that we can create the new section 96 system_name = str(sec).split('.')[0] 97 section_name = system_name + '.physmem.store0' 98 cpt.add_section(section_name) 99 cpt.set(section_name, 'store_id', '0') 100 cpt.set(section_name, 'range_size', mem_size) 101 cpt.set(section_name, 'filename', mem_filename) 102 elif re.search('.*sys.*\.\w*mem$', sec): 103 # Due to the lack of information about a start address, 104 # this migration only works if there is a single memory in 105 # the system, thus starting at 0 106 raise ValueError("more than one memory detected (" + sec + ")") 107 108migrations = [] 109migrations.append(from_0) 110migrations.append(from_1) 111 112verbose_print = False 113 114def verboseprint(*args): 115 if not verbose_print: 116 return 117 for arg in args: 118 print arg, 119 print 120 121def process_file(path, **kwargs): 122 if not osp.isfile(path): 123 import errno 124 raise IOError(ennro.ENOENT, "No such file", path) 125 126 verboseprint("Processing file %s...." % path) 127 128 if kwargs.get('backup', True): 129 import shutil 130 shutil.copyfile(path, path + '.bak') 131 132 cpt = ConfigParser.SafeConfigParser() 133 134 # gem5 is case sensitive with paramaters 135 cpt.optionxform = str 136 137 # Read the current data 138 cpt_file = file(path, 'r') 139 cpt.readfp(cpt_file) 140 cpt_file.close() 141 142 # Make sure we know what we're starting from 143 if not cpt.has_option('root','cpt_ver'): 144 raise LookupError("cannot determine version of checkpoint") 145 146 cpt_ver = cpt.getint('root','cpt_ver') 147 148 # If the current checkpoint is longer than the migrations list, we have a problem 149 # and someone didn't update this file 150 if cpt_ver > len(migrations): 151 raise ValueError("upgrade script is too old and needs updating") 152 153 verboseprint("\t...file is at version %#x" % cpt_ver) 154 155 if cpt_ver == len(migrations): 156 verboseprint("\t...nothing to do") 157 return 158 159 # Walk through every function from now until the end fixing the checkpoint 160 for v in xrange(cpt_ver,len(migrations)): 161 verboseprint("\t...migrating to version %#x" % (v + 1)) 162 migrations[v](cpt) 163 cpt.set('root','cpt_ver', str(v + 1)) 164 165 # Write the old data back 166 verboseprint("\t...completed") 167 cpt.write(file(path, 'w')) 168 169 170if __name__ == '__main__': 171 from optparse import OptionParser 172 parser = OptionParser("usage: %prog [options] <filename or directory>") 173 parser.add_option("-r", "--recurse", action="store_true", 174 help="Recurse through all subdirectories modifying "\ 175 "each checkpoint that is found") 176 parser.add_option("-N", "--no-backup", action="store_false", 177 dest="backup", default=True, 178 help="Do no backup each checkpoint before modifying it") 179 parser.add_option("-v", "--verbose", action="store_true", 180 help="Print out debugging information as") 181 182 (options, args) = parser.parse_args() 183 if len(args) != 1: 184 parser.error("You must specify a checkpoint file to modify or a "\ 185 "directory of checkpoints to recursively update") 186 187 verbose_print = options.verbose 188 189 # Deal with shell variables and ~ 190 path = osp.expandvars(osp.expanduser(args[0])) 191 192 # Process a single file if we have it 193 if osp.isfile(path): 194 process_file(path, **vars(options)) 195 # Process an entire directory 196 elif osp.isdir(path): 197 cpt_file = osp.join(path, 'm5.cpt') 198 if options.recurse: 199 # Visit very file and see if it matches 200 for root,dirs,files in os.walk(path): 201 for name in files: 202 if name == 'm5.cpt': 203 process_file(osp.join(root,name), **vars(options)) 204 for dir in dirs: 205 pass 206 # Maybe someone passed a cpt.XXXXXXX directory and not m5.cpt 207 elif osp.isfile(cpt_file): 208 process_file(cpt_file, **vars(options)) 209 else: 210 print "Error: checkpoint file not found at in %s " % path, 211 print "and recurse not specified" 212 sys.exit(1) 213 sys.exit(0) 214 215