Simulation.py revision 5072
12086SN/A# Copyright (c) 2006-2007 The Regents of The University of Michigan 22086SN/A# All rights reserved. 32086SN/A# 42086SN/A# Redistribution and use in source and binary forms, with or without 52086SN/A# modification, are permitted provided that the following conditions are 62086SN/A# met: redistributions of source code must retain the above copyright 72086SN/A# notice, this list of conditions and the following disclaimer; 82086SN/A# redistributions in binary form must reproduce the above copyright 92086SN/A# notice, this list of conditions and the following disclaimer in the 102086SN/A# documentation and/or other materials provided with the distribution; 112086SN/A# neither the name of the copyright holders nor the names of its 122086SN/A# contributors may be used to endorse or promote products derived from 132086SN/A# this software without specific prior written permission. 142086SN/A# 152086SN/A# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 162086SN/A# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 172086SN/A# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 182086SN/A# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 192086SN/A# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 202086SN/A# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 212086SN/A# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 222086SN/A# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 232086SN/A# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 242086SN/A# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 252086SN/A# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 262086SN/A# 272086SN/A# Authors: Lisa Hsu 282665Ssaidi@eecs.umich.edu 292665Ssaidi@eecs.umich.edufrom os import getcwd 302665Ssaidi@eecs.umich.edufrom os.path import join as joinpath 312086SN/Aimport m5 324202Sbinkertn@umich.edufrom m5.objects import * 332086SN/Am5.AddToPath('../common') 344202Sbinkertn@umich.edufrom Caches import L1Cache 354202Sbinkertn@umich.edu 364202Sbinkertn@umich.edudef setCPUClass(options): 378745Sgblack@eecs.umich.edu 386313Sgblack@eecs.umich.edu atomic = False 396365Sgblack@eecs.umich.edu if options.timing: 404997Sgblack@eecs.umich.edu TmpClass = TimingSimpleCPU 414202Sbinkertn@umich.edu elif options.detailed: 424997Sgblack@eecs.umich.edu if not options.caches: 434826Ssaidi@eecs.umich.edu print "O3 CPU must be used with caches" 442086SN/A sys.exit(1) 458745Sgblack@eecs.umich.edu TmpClass = DerivO3CPU 466365Sgblack@eecs.umich.edu else: 478745Sgblack@eecs.umich.edu TmpClass = AtomicSimpleCPU 486365Sgblack@eecs.umich.edu atomic = True 498335Snate@binkert.org 508335Snate@binkert.org CPUClass = None 514997Sgblack@eecs.umich.edu test_mem_mode = 'atomic' 524202Sbinkertn@umich.edu 534486Sbinkertn@umich.edu if not atomic: 544486Sbinkertn@umich.edu if options.checkpoint_restore: 554202Sbinkertn@umich.edu CPUClass = TmpClass 564202Sbinkertn@umich.edu TmpClass = AtomicSimpleCPU 574202Sbinkertn@umich.edu else: 584202Sbinkertn@umich.edu test_mem_mode = 'timing' 594202Sbinkertn@umich.edu 602086SN/A return (TmpClass, test_mem_mode, CPUClass) 614202Sbinkertn@umich.edu 624202Sbinkertn@umich.edu 634202Sbinkertn@umich.edudef run(options, root, testsys, cpu_class): 642086SN/A if options.maxtick: 654202Sbinkertn@umich.edu maxtick = options.maxtick 664202Sbinkertn@umich.edu elif options.maxtime: 672086SN/A simtime = m5.ticks.seconds(simtime) 684202Sbinkertn@umich.edu print "simulating for: ", simtime 694202Sbinkertn@umich.edu maxtick = simtime 704202Sbinkertn@umich.edu else: 714202Sbinkertn@umich.edu maxtick = m5.MaxTick 724202Sbinkertn@umich.edu 734202Sbinkertn@umich.edu if options.checkpoint_dir: 74 cptdir = options.checkpoint_dir 75 else: 76 cptdir = getcwd() 77 78 np = options.num_cpus 79 max_checkpoints = options.max_checkpoints 80 switch_cpus = None 81 82 if cpu_class: 83 switch_cpus = [cpu_class(defer_registration=True, cpu_id=(np+i)) 84 for i in xrange(np)] 85 86 for i in xrange(np): 87 switch_cpus[i].system = testsys 88 if not m5.build_env['FULL_SYSTEM']: 89 switch_cpus[i].workload = testsys.cpu[i].workload 90 switch_cpus[i].clock = testsys.cpu[0].clock 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 not options.caches: 111 # O3 CPU must have a cache to work. 112 switch_cpus_1[i].addPrivateSplitL1Caches(L1Cache(size = '32kB'), 113 L1Cache(size = '64kB')) 114 switch_cpus_1[i].connectMemPorts(testsys.membus) 115 116 117 testsys.switch_cpus = switch_cpus 118 testsys.switch_cpus_1 = switch_cpus_1 119 switch_cpu_list = [(testsys.cpu[i], switch_cpus[i]) for i in xrange(np)] 120 switch_cpu_list1 = [(switch_cpus[i], switch_cpus_1[i]) for i in xrange(np)] 121 122 m5.instantiate(root) 123 124 if options.checkpoint_restore: 125 from os.path import isdir 126 from os import listdir 127 import re 128 129 if not isdir(cptdir): 130 m5.panic("checkpoint dir %s does not exist!" % cptdir) 131 132 dirs = listdir(cptdir) 133 expr = re.compile('cpt.([0-9]*)') 134 cpts = [] 135 for dir in dirs: 136 match = expr.match(dir) 137 if match: 138 cpts.append(match.group(1)) 139 140 cpts.sort(lambda a,b: cmp(long(a), long(b))) 141 142 cpt_num = options.checkpoint_restore 143 144 if cpt_num > len(cpts): 145 m5.panic('Checkpoint %d not found' % cpt_num) 146 147 ## Adjust max tick based on our starting tick 148 maxtick = maxtick - int(cpts[cpt_num - 1]) 149 150 ## Restore the checkpoint 151 m5.restoreCheckpoint(root, 152 joinpath(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.drain(testsys) 170 m5.switchCpus(switch_cpu_list1) 171 m5.resume(testsys) 172 173 num_checkpoints = 0 174 exit_cause = '' 175 176 ## Checkpoints being taken via the command line at <when> and at subsequent 177 ## periods of <period>. Checkpoint instructions received from the benchmark running 178 ## are ignored and skipped in favor of command line checkpoint instructions. 179 if options.take_checkpoints: 180 [when, period] = options.take_checkpoints.split(",", 1) 181 when = int(when) 182 period = int(period) 183 184 exit_event = m5.simulate(when) 185 while exit_event.getCause() == "checkpoint": 186 exit_event = m5.simulate(when - m5.curTick()) 187 188 if exit_event.getCause() == "simulate() limit reached": 189 m5.checkpoint(root, joinpath(cptdir, "cpt.%d")) 190 num_checkpoints += 1 191 192 sim_ticks = when 193 exit_cause = "maximum %d checkpoints dropped" % max_checkpoints 194 while num_checkpoints < max_checkpoints and \ 195 exit_event.getCause() != "user interrupt received": 196 if (sim_ticks + period) > maxtick: 197 exit_event = m5.simulate(maxtick - sim_ticks) 198 exit_cause = exit_event.getCause() 199 break 200 else: 201 exit_event = m5.simulate(period) 202 sim_ticks += period 203 while exit_event.getCause() == "checkpoint": 204 exit_event = m5.simulate(sim_ticks - m5.curTick()) 205 if exit_event.getCause() == "simulate() limit reached": 206 m5.checkpoint(root, joinpath(cptdir, "cpt.%d")) 207 num_checkpoints += 1 208 209 if exit_event.getCause() == "user interrupt received": 210 exit_cause = exit_event.getCause(); 211 212 213 else: #no checkpoints being taken via this script 214 exit_event = m5.simulate(maxtick) 215 216 while exit_event.getCause() == "checkpoint": 217 m5.checkpoint(root, joinpath(cptdir, "cpt.%d")) 218 num_checkpoints += 1 219 if num_checkpoints == max_checkpoints: 220 exit_cause = "maximum %d checkpoints dropped" % max_checkpoints 221 break 222 223 exit_event = m5.simulate(maxtick - m5.curTick()) 224 exit_cause = exit_event.getCause() 225 226 if exit_cause == '': 227 exit_cause = exit_event.getCause() 228 print 'Exiting @ cycle %i because %s' % (m5.curTick(), exit_cause) 229 230