Simulation.py revision 3481
110448Snilay@cs.wisc.edu# Copyright (c) 2006 The Regents of The University of Michigan 210448Snilay@cs.wisc.edu# All rights reserved. 310448Snilay@cs.wisc.edu# 410448Snilay@cs.wisc.edu# Redistribution and use in source and binary forms, with or without 510448Snilay@cs.wisc.edu# modification, are permitted provided that the following conditions are 610448Snilay@cs.wisc.edu# met: redistributions of source code must retain the above copyright 710448Snilay@cs.wisc.edu# notice, this list of conditions and the following disclaimer; 810448Snilay@cs.wisc.edu# redistributions in binary form must reproduce the above copyright 910448Snilay@cs.wisc.edu# notice, this list of conditions and the following disclaimer in the 1010448Snilay@cs.wisc.edu# documentation and/or other materials provided with the distribution; 1110448Snilay@cs.wisc.edu# neither the name of the copyright holders nor the names of its 1210448Snilay@cs.wisc.edu# contributors may be used to endorse or promote products derived from 1310448Snilay@cs.wisc.edu# this software without specific prior written permission. 1410448Snilay@cs.wisc.edu# 1510448Snilay@cs.wisc.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1610448Snilay@cs.wisc.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1710448Snilay@cs.wisc.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1810448Snilay@cs.wisc.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1910448Snilay@cs.wisc.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2010448Snilay@cs.wisc.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2110448Snilay@cs.wisc.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2210447Snilay@cs.wisc.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2310447Snilay@cs.wisc.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2410447Snilay@cs.wisc.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2510447Snilay@cs.wisc.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2610447Snilay@cs.wisc.edu# 2710447Snilay@cs.wisc.edu# Authors: Lisa Hsu 2810447Snilay@cs.wisc.edu 2910447Snilay@cs.wisc.edufrom os import getcwd 3010447Snilay@cs.wisc.eduimport m5 3110447Snilay@cs.wisc.edufrom m5.objects import * 3210447Snilay@cs.wisc.edum5.AddToPath('../common') 3310447Snilay@cs.wisc.edufrom Caches import L1Cache 3410447Snilay@cs.wisc.edu 3510447Snilay@cs.wisc.edudef setCPUClass(options): 3610447Snilay@cs.wisc.edu 3710447Snilay@cs.wisc.edu atomic = False 3810447Snilay@cs.wisc.edu if options.timing: 3910447Snilay@cs.wisc.edu TmpClass = TimingSimpleCPU 4010447Snilay@cs.wisc.edu elif options.detailed: 4110447Snilay@cs.wisc.edu TmpClass = DerivO3CPU 4210447Snilay@cs.wisc.edu else: 4310447Snilay@cs.wisc.edu TmpClass = AtomicSimpleCPU 4410447Snilay@cs.wisc.edu atomic = True 4510447Snilay@cs.wisc.edu 4610447Snilay@cs.wisc.edu CPUClass = None 4710447Snilay@cs.wisc.edu test_mem_mode = 'atomic' 4810447Snilay@cs.wisc.edu 4910447Snilay@cs.wisc.edu if not atomic: 5010447Snilay@cs.wisc.edu if options.checkpoint_restore: 5110447Snilay@cs.wisc.edu CPUClass = TmpClass 5210447Snilay@cs.wisc.edu TmpClass = AtomicSimpleCPU 5310447Snilay@cs.wisc.edu else: 5410447Snilay@cs.wisc.edu test_mem_mode = 'timing' 55 56 return (TmpClass, test_mem_mode, CPUClass) 57 58 59def run(options, root, testsys, cpu_class): 60 if options.maxtick: 61 maxtick = options.maxtick 62 elif options.maxtime: 63 simtime = int(options.maxtime * root.clock.value) 64 print "simulating for: ", simtime 65 maxtick = simtime 66 else: 67 maxtick = -1 68 69 if options.checkpoint_dir: 70 cptdir = options.checkpoint_dir 71 else: 72 cptdir = getcwd() 73 74 np = options.num_cpus 75 max_checkpoints = options.max_checkpoints 76 switch_cpus = None 77 78 if cpu_class: 79 switch_cpus = [cpu_class(defer_registration=True, cpu_id=(np+i)) 80 for i in xrange(np)] 81 82 for i in xrange(np): 83 switch_cpus[i].system = testsys 84 if not m5.build_env['FULL_SYSTEM']: 85 switch_cpus[i].workload = testsys.cpu[i].workload 86 switch_cpus[i].clock = testsys.cpu[0].clock 87 if options.caches: 88 switch_cpus[i].addPrivateSplitL1Caches(L1Cache(size = '32kB'), 89 L1Cache(size = '64kB')) 90 switch_cpus[i].connectMemPorts(testsys.membus) 91 92 root.switch_cpus = switch_cpus 93 switch_cpu_list = [(testsys.cpu[i], switch_cpus[i]) for i in xrange(np)] 94 95 if options.standard_switch: 96 switch_cpus = [TimingSimpleCPU(defer_registration=True, cpu_id=(np+i)) 97 for i in xrange(np)] 98 switch_cpus_1 = [DerivO3CPU(defer_registration=True, cpu_id=(2*np+i)) 99 for i in xrange(np)] 100 101 for i in xrange(np): 102 switch_cpus[i].system = testsys 103 switch_cpus_1[i].system = testsys 104 if not m5.build_env['FULL_SYSTEM']: 105 switch_cpus[i].workload = testsys.cpu[i].workload 106 switch_cpus_1[i].workload = testsys.cpu[i].workload 107 switch_cpus[i].clock = testsys.cpu[0].clock 108 switch_cpus_1[i].clock = testsys.cpu[0].clock 109 110 if options.caches: 111 switch_cpus[i].addPrivateSplitL1Caches(L1Cache(size = '32kB'), 112 L1Cache(size = '64kB')) 113 switch_cpus[i].connectMemPorts(testsys.membus) 114 else: 115 # O3 CPU must have a cache to work. 116 switch_cpus_1[i].addPrivateSplitL1Caches(L1Cache(size = '32kB'), 117 L1Cache(size = '64kB')) 118 switch_cpus_1[i].connectMemPorts(testsys.membus) 119 120 121 root.switch_cpus = switch_cpus 122 root.switch_cpus_1 = switch_cpus_1 123 switch_cpu_list = [(testsys.cpu[i], switch_cpus[i]) for i in xrange(np)] 124 switch_cpu_list1 = [(switch_cpus[i], switch_cpus_1[i]) for i in xrange(np)] 125 126 m5.instantiate(root) 127 128 if options.checkpoint_restore: 129 from os.path import isdir 130 from os import listdir 131 import re 132 133 if not isdir(cptdir): 134 m5.panic("checkpoint dir %s does not exist!" % cptdir) 135 136 dirs = listdir(cptdir) 137 expr = re.compile('cpt.([0-9]*)') 138 cpts = [] 139 for dir in dirs: 140 match = expr.match(dir) 141 if match: 142 cpts.append(match.group(1)) 143 144 cpts.sort(lambda a,b: cmp(long(a), long(b))) 145 146 cpt_num = options.checkpoint_restore 147 148 if cpt_num > len(cpts): 149 m5.panic('Checkpoint %d not found' % cpt_num) 150 151 m5.restoreCheckpoint(root, 152 "/".join([cptdir, "cpt.%s" % cpts[cpt_num - 1]])) 153 154 if options.standard_switch or cpu_class: 155 exit_event = m5.simulate(10000) 156 157 ## when you change to Timing (or Atomic), you halt the system given 158 ## as argument. When you are finished with the system changes 159 ## (including switchCpus), you must resume the system manually. 160 ## You DON'T need to resume after just switching CPUs if you haven't 161 ## changed anything on the system level. 162 163 m5.changeToTiming(testsys) 164 m5.switchCpus(switch_cpu_list) 165 m5.resume(testsys) 166 167 if options.standard_switch: 168 exit_event = m5.simulate(options.warmup) 169 m5.switchCpus(switch_cpu_list1) 170 171 num_checkpoints = 0 172 exit_cause = '' 173 174 ## Checkpoints being taken via the command line at <when> and at subsequent 175 ## periods of <period>. Checkpoint instructions received from the benchmark running 176 ## are ignored and skipped in favor of command line checkpoint instructions. 177 if options.take_checkpoints: 178 [when, period] = options.take_checkpoints.split(",", 1) 179 when = int(when) 180 period = int(period) 181 182 exit_event = m5.simulate(when) 183 while exit_event.getCause() == "checkpoint": 184 exit_event = m5.simulate(when - m5.curTick()) 185 186 if exit_event.getCause() == "simulate() limit reached": 187 m5.checkpoint(root, "/".join([cptdir,"cpt.%d"])) 188 num_checkpoints += 1 189 190 sim_ticks = when 191 exit_cause = "maximum %d checkpoints dropped" % max_checkpoints 192 while num_checkpoints < max_checkpoints: 193 if (sim_ticks + period) > maxtick and maxtick != -1: 194 exit_event = m5.simulate(maxtick - sim_ticks) 195 exit_cause = exit_event.getCause() 196 break 197 else: 198 exit_event = m5.simulate(period) 199 sim_ticks += period 200 while exit_event.getCause() == "checkpoint": 201 exit_event = m5.simulate(sim_ticks - m5.curTick()) 202 if exit_event.getCause() == "simulate() limit reached": 203 m5.checkpoint(root, "/".join([cptdir,"cpt.%d"])) 204 num_checkpoints += 1 205 206 else: #no checkpoints being taken via this script 207 exit_event = m5.simulate(maxtick) 208 209 while exit_event.getCause() == "checkpoint": 210 m5.checkpoint(root, "/".join([cptdir,"cpt.%d"])) 211 num_checkpoints += 1 212 if num_checkpoints == max_checkpoints: 213 exit_cause = "maximum %d checkpoints dropped" % max_checkpoints 214 break 215 216 if maxtick == -1: 217 exit_event = m5.simulate(maxtick) 218 else: 219 exit_event = m5.simulate(maxtick - m5.curTick()) 220 221 exit_cause = exit_event.getCause() 222 223 if exit_cause == '': 224 exit_cause = exit_event.getCause() 225 print 'Exiting @ cycle', m5.curTick(), 'because ', exit_cause 226 227