Simulation.py revision 5353
15347Ssaidi@eecs.umich.edu# Copyright (c) 2006-2008 The Regents of The University of Michigan
23395Shsul@eecs.umich.edu# All rights reserved.
33395Shsul@eecs.umich.edu#
43395Shsul@eecs.umich.edu# Redistribution and use in source and binary forms, with or without
53395Shsul@eecs.umich.edu# modification, are permitted provided that the following conditions are
63395Shsul@eecs.umich.edu# met: redistributions of source code must retain the above copyright
73395Shsul@eecs.umich.edu# notice, this list of conditions and the following disclaimer;
83395Shsul@eecs.umich.edu# redistributions in binary form must reproduce the above copyright
93395Shsul@eecs.umich.edu# notice, this list of conditions and the following disclaimer in the
103395Shsul@eecs.umich.edu# documentation and/or other materials provided with the distribution;
113395Shsul@eecs.umich.edu# neither the name of the copyright holders nor the names of its
123395Shsul@eecs.umich.edu# contributors may be used to endorse or promote products derived from
133395Shsul@eecs.umich.edu# this software without specific prior written permission.
143395Shsul@eecs.umich.edu#
153395Shsul@eecs.umich.edu# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
163395Shsul@eecs.umich.edu# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
173395Shsul@eecs.umich.edu# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
183395Shsul@eecs.umich.edu# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
193395Shsul@eecs.umich.edu# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
203395Shsul@eecs.umich.edu# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
213395Shsul@eecs.umich.edu# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
223395Shsul@eecs.umich.edu# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
233395Shsul@eecs.umich.edu# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
243395Shsul@eecs.umich.edu# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
253395Shsul@eecs.umich.edu# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
263395Shsul@eecs.umich.edu#
273395Shsul@eecs.umich.edu# Authors: Lisa Hsu
283395Shsul@eecs.umich.edu
293395Shsul@eecs.umich.edufrom os import getcwd
303509Shsul@eecs.umich.edufrom os.path import join as joinpath
313395Shsul@eecs.umich.eduimport m5
323395Shsul@eecs.umich.edufrom m5.objects import *
333395Shsul@eecs.umich.edum5.AddToPath('../common')
343448Shsul@eecs.umich.edufrom Caches import L1Cache
353395Shsul@eecs.umich.edu
363481Shsul@eecs.umich.edudef setCPUClass(options):
373481Shsul@eecs.umich.edu
383481Shsul@eecs.umich.edu    atomic = False
393481Shsul@eecs.umich.edu    if options.timing:
405347Ssaidi@eecs.umich.edu        class TmpClass(TimingSimpleCPU): pass
413481Shsul@eecs.umich.edu    elif options.detailed:
423681Sktlim@umich.edu        if not options.caches:
433681Sktlim@umich.edu            print "O3 CPU must be used with caches"
443681Sktlim@umich.edu            sys.exit(1)
455347Ssaidi@eecs.umich.edu        class TmpClass(DerivO3CPU): pass
463481Shsul@eecs.umich.edu    else:
475347Ssaidi@eecs.umich.edu        class TmpClass(AtomicSimpleCPU): pass
483481Shsul@eecs.umich.edu        atomic = True
493481Shsul@eecs.umich.edu
503481Shsul@eecs.umich.edu    CPUClass = None
513481Shsul@eecs.umich.edu    test_mem_mode = 'atomic'
523481Shsul@eecs.umich.edu
533481Shsul@eecs.umich.edu    if not atomic:
543481Shsul@eecs.umich.edu        if options.checkpoint_restore:
553481Shsul@eecs.umich.edu            CPUClass = TmpClass
565347Ssaidi@eecs.umich.edu            class TmpClass(AtomicSimpleCPU): pass
573481Shsul@eecs.umich.edu        else:
583481Shsul@eecs.umich.edu            test_mem_mode = 'timing'
593481Shsul@eecs.umich.edu
603481Shsul@eecs.umich.edu    return (TmpClass, test_mem_mode, CPUClass)
613481Shsul@eecs.umich.edu
623481Shsul@eecs.umich.edu
633481Shsul@eecs.umich.edudef run(options, root, testsys, cpu_class):
643395Shsul@eecs.umich.edu    if options.maxtick:
653395Shsul@eecs.umich.edu        maxtick = options.maxtick
663395Shsul@eecs.umich.edu    elif options.maxtime:
674167Sbinkertn@umich.edu        simtime = m5.ticks.seconds(simtime)
683395Shsul@eecs.umich.edu        print "simulating for: ", simtime
693395Shsul@eecs.umich.edu        maxtick = simtime
703395Shsul@eecs.umich.edu    else:
713511Shsul@eecs.umich.edu        maxtick = m5.MaxTick
723395Shsul@eecs.umich.edu
733395Shsul@eecs.umich.edu    if options.checkpoint_dir:
743395Shsul@eecs.umich.edu        cptdir = options.checkpoint_dir
755211Ssaidi@eecs.umich.edu    elif m5.options.outdir:
765211Ssaidi@eecs.umich.edu        cptdir = m5.options.outdir
773395Shsul@eecs.umich.edu    else:
783395Shsul@eecs.umich.edu        cptdir = getcwd()
793395Shsul@eecs.umich.edu
803395Shsul@eecs.umich.edu    np = options.num_cpus
813395Shsul@eecs.umich.edu    max_checkpoints = options.max_checkpoints
823481Shsul@eecs.umich.edu    switch_cpus = None
833481Shsul@eecs.umich.edu
843481Shsul@eecs.umich.edu    if cpu_class:
853481Shsul@eecs.umich.edu        switch_cpus = [cpu_class(defer_registration=True, cpu_id=(np+i))
863481Shsul@eecs.umich.edu                       for i in xrange(np)]
873481Shsul@eecs.umich.edu
883481Shsul@eecs.umich.edu        for i in xrange(np):
893481Shsul@eecs.umich.edu            switch_cpus[i].system =  testsys
903481Shsul@eecs.umich.edu            if not m5.build_env['FULL_SYSTEM']:
913481Shsul@eecs.umich.edu                switch_cpus[i].workload = testsys.cpu[i].workload
923481Shsul@eecs.umich.edu            switch_cpus[i].clock = testsys.cpu[0].clock
933481Shsul@eecs.umich.edu
945311Ssaidi@eecs.umich.edu        testsys.switch_cpus = switch_cpus
953481Shsul@eecs.umich.edu        switch_cpu_list = [(testsys.cpu[i], switch_cpus[i]) for i in xrange(np)]
963395Shsul@eecs.umich.edu
973395Shsul@eecs.umich.edu    if options.standard_switch:
985353Svilas.sridharan@gmail.com        if (options.fast_forward and options.warmup):
995353Svilas.sridharan@gmail.com            m5.panic("Must specify either warmup OR fast-forward with -s!")
1005353Svilas.sridharan@gmail.com
1013395Shsul@eecs.umich.edu        switch_cpus = [TimingSimpleCPU(defer_registration=True, cpu_id=(np+i))
1023395Shsul@eecs.umich.edu                       for i in xrange(np)]
1033478Shsul@eecs.umich.edu        switch_cpus_1 = [DerivO3CPU(defer_registration=True, cpu_id=(2*np+i))
1043395Shsul@eecs.umich.edu                        for i in xrange(np)]
1053478Shsul@eecs.umich.edu
1063395Shsul@eecs.umich.edu        for i in xrange(np):
1073395Shsul@eecs.umich.edu            switch_cpus[i].system =  testsys
1083478Shsul@eecs.umich.edu            switch_cpus_1[i].system =  testsys
1093395Shsul@eecs.umich.edu            if not m5.build_env['FULL_SYSTEM']:
1103395Shsul@eecs.umich.edu                switch_cpus[i].workload = testsys.cpu[i].workload
1113478Shsul@eecs.umich.edu                switch_cpus_1[i].workload = testsys.cpu[i].workload
1123395Shsul@eecs.umich.edu            switch_cpus[i].clock = testsys.cpu[0].clock
1133478Shsul@eecs.umich.edu            switch_cpus_1[i].clock = testsys.cpu[0].clock
1143480Shsul@eecs.umich.edu
1155353Svilas.sridharan@gmail.com            if options.fast_forward:
1165353Svilas.sridharan@gmail.com                switch_cpus[i].max_insts_any_thread = options.fast_forward
1175353Svilas.sridharan@gmail.com            if options.max_inst:
1185353Svilas.sridharan@gmail.com                switch_cpus_1[i].max_insts_any_thread = options.max_inst
1195353Svilas.sridharan@gmail.com
1203514Sktlim@umich.edu            if not options.caches:
1213481Shsul@eecs.umich.edu                # O3 CPU must have a cache to work.
1223480Shsul@eecs.umich.edu                switch_cpus_1[i].addPrivateSplitL1Caches(L1Cache(size = '32kB'),
1233480Shsul@eecs.umich.edu                                                         L1Cache(size = '64kB'))
1243480Shsul@eecs.umich.edu                switch_cpus_1[i].connectMemPorts(testsys.membus)
1253395Shsul@eecs.umich.edu
1263478Shsul@eecs.umich.edu
1273514Sktlim@umich.edu            testsys.switch_cpus = switch_cpus
1283514Sktlim@umich.edu            testsys.switch_cpus_1 = switch_cpus_1
1293395Shsul@eecs.umich.edu            switch_cpu_list = [(testsys.cpu[i], switch_cpus[i]) for i in xrange(np)]
1303478Shsul@eecs.umich.edu            switch_cpu_list1 = [(switch_cpus[i], switch_cpus_1[i]) for i in xrange(np)]
1313395Shsul@eecs.umich.edu
1325353Svilas.sridharan@gmail.com    elif options.fast_forward:
1335353Svilas.sridharan@gmail.com        for i in xrange(np):
1345353Svilas.sridharan@gmail.com            testsys.cpu[i].max_insts_any_thread = options.fast_forward
1355353Svilas.sridharan@gmail.com
1363395Shsul@eecs.umich.edu    m5.instantiate(root)
1373395Shsul@eecs.umich.edu
1383395Shsul@eecs.umich.edu    if options.checkpoint_restore:
1393395Shsul@eecs.umich.edu        from os.path import isdir
1403395Shsul@eecs.umich.edu        from os import listdir
1413395Shsul@eecs.umich.edu        import re
1423395Shsul@eecs.umich.edu
1433395Shsul@eecs.umich.edu        if not isdir(cptdir):
1443395Shsul@eecs.umich.edu            m5.panic("checkpoint dir %s does not exist!" % cptdir)
1453395Shsul@eecs.umich.edu
1463395Shsul@eecs.umich.edu        dirs = listdir(cptdir)
1475073Ssaidi@eecs.umich.edu        expr = re.compile('cpt\.([0-9]*)')
1483395Shsul@eecs.umich.edu        cpts = []
1493395Shsul@eecs.umich.edu        for dir in dirs:
1503395Shsul@eecs.umich.edu            match = expr.match(dir)
1513395Shsul@eecs.umich.edu            if match:
1523395Shsul@eecs.umich.edu                cpts.append(match.group(1))
1533395Shsul@eecs.umich.edu
1543395Shsul@eecs.umich.edu        cpts.sort(lambda a,b: cmp(long(a), long(b)))
1553395Shsul@eecs.umich.edu
1563395Shsul@eecs.umich.edu        cpt_num = options.checkpoint_restore
1573395Shsul@eecs.umich.edu
1583395Shsul@eecs.umich.edu        if cpt_num > len(cpts):
1593395Shsul@eecs.umich.edu            m5.panic('Checkpoint %d not found' % cpt_num)
1603395Shsul@eecs.umich.edu
1613999Ssaidi@eecs.umich.edu        ## Adjust max tick based on our starting tick
1623999Ssaidi@eecs.umich.edu        maxtick = maxtick - int(cpts[cpt_num - 1])
1633999Ssaidi@eecs.umich.edu
1643999Ssaidi@eecs.umich.edu        ## Restore the checkpoint
1653395Shsul@eecs.umich.edu        m5.restoreCheckpoint(root,
1663509Shsul@eecs.umich.edu                             joinpath(cptdir, "cpt.%s" % cpts[cpt_num - 1]))
1673395Shsul@eecs.umich.edu
1683481Shsul@eecs.umich.edu    if options.standard_switch or cpu_class:
1693395Shsul@eecs.umich.edu        exit_event = m5.simulate(10000)
1703395Shsul@eecs.umich.edu
1713395Shsul@eecs.umich.edu        ## when you change to Timing (or Atomic), you halt the system given
1723395Shsul@eecs.umich.edu        ## as argument.  When you are finished with the system changes
1733395Shsul@eecs.umich.edu        ## (including switchCpus), you must resume the system manually.
1743395Shsul@eecs.umich.edu        ## You DON'T need to resume after just switching CPUs if you haven't
1753395Shsul@eecs.umich.edu        ## changed anything on the system level.
1763395Shsul@eecs.umich.edu
1773395Shsul@eecs.umich.edu        m5.changeToTiming(testsys)
1783395Shsul@eecs.umich.edu        m5.switchCpus(switch_cpu_list)
1793395Shsul@eecs.umich.edu        m5.resume(testsys)
1803395Shsul@eecs.umich.edu
1813481Shsul@eecs.umich.edu        if options.standard_switch:
1825353Svilas.sridharan@gmail.com            if (options.warmup):
1835353Svilas.sridharan@gmail.com                exit_event = m5.simulate(options.warmup)
1845353Svilas.sridharan@gmail.com            if options.fast_forward:
1855353Svilas.sridharan@gmail.com                exit_event = m5.simulate()
1865072Ssaidi@eecs.umich.edu            m5.drain(testsys)
1873481Shsul@eecs.umich.edu            m5.switchCpus(switch_cpu_list1)
1885072Ssaidi@eecs.umich.edu            m5.resume(testsys)
1893395Shsul@eecs.umich.edu
1905353Svilas.sridharan@gmail.com    # This should *only* be used by itself to take a checkpoint!
1915353Svilas.sridharan@gmail.com    # Otherwise, use standard_switch
1925353Svilas.sridharan@gmail.com    elif options.fast_forward:
1935353Svilas.sridharan@gmail.com        exit_event = m5.simulate()
1945353Svilas.sridharan@gmail.com
1955353Svilas.sridharan@gmail.com        while exit_event.getCause() != "a thread reached the max instruction count":
1965353Svilas.sridharan@gmail.com            if exit_event.getCause() == "user interrupt received":
1975353Svilas.sridharan@gmail.com                print "User interrupt! Switching to simulation mode"
1985353Svilas.sridharan@gmail.com                break
1995353Svilas.sridharan@gmail.com            else:
2005353Svilas.sridharan@gmail.com                m5.simulate(True)
2015353Svilas.sridharan@gmail.com
2025353Svilas.sridharan@gmail.com        if exit_event.getCause() == "a thread reached the max instruction count":
2035353Svilas.sridharan@gmail.com            print "Reached fast_forward count %d; starting simulation at cycle %d" % (options.fast_forward, m5.curTick())
2045353Svilas.sridharan@gmail.com
2055353Svilas.sridharan@gmail.com        m5.checkpoint(root, joinpath(cptdir, "cpt.%d"))
2065353Svilas.sridharan@gmail.com        return
2075353Svilas.sridharan@gmail.com
2083395Shsul@eecs.umich.edu    num_checkpoints = 0
2093395Shsul@eecs.umich.edu    exit_cause = ''
2103395Shsul@eecs.umich.edu
2113410Shsul@eecs.umich.edu    ## Checkpoints being taken via the command line at <when> and at subsequent
2123410Shsul@eecs.umich.edu    ## periods of <period>.  Checkpoint instructions received from the benchmark running
2133410Shsul@eecs.umich.edu    ## are ignored and skipped in favor of command line checkpoint instructions.
2143395Shsul@eecs.umich.edu    if options.take_checkpoints:
2153395Shsul@eecs.umich.edu        [when, period] = options.take_checkpoints.split(",", 1)
2163395Shsul@eecs.umich.edu        when = int(when)
2173395Shsul@eecs.umich.edu        period = int(period)
2183395Shsul@eecs.umich.edu
2193395Shsul@eecs.umich.edu        exit_event = m5.simulate(when)
2203395Shsul@eecs.umich.edu        while exit_event.getCause() == "checkpoint":
2213395Shsul@eecs.umich.edu            exit_event = m5.simulate(when - m5.curTick())
2223395Shsul@eecs.umich.edu
2233395Shsul@eecs.umich.edu        if exit_event.getCause() == "simulate() limit reached":
2243509Shsul@eecs.umich.edu            m5.checkpoint(root, joinpath(cptdir, "cpt.%d"))
2253395Shsul@eecs.umich.edu            num_checkpoints += 1
2263395Shsul@eecs.umich.edu
2273395Shsul@eecs.umich.edu        sim_ticks = when
2283395Shsul@eecs.umich.edu        exit_cause = "maximum %d checkpoints dropped" % max_checkpoints
2293999Ssaidi@eecs.umich.edu        while num_checkpoints < max_checkpoints and \
2305185Ssaidi@eecs.umich.edu                exit_event.getCause() == "simulate() limit reached":
2313511Shsul@eecs.umich.edu            if (sim_ticks + period) > maxtick:
2323395Shsul@eecs.umich.edu                exit_event = m5.simulate(maxtick - sim_ticks)
2333395Shsul@eecs.umich.edu                exit_cause = exit_event.getCause()
2343395Shsul@eecs.umich.edu                break
2353395Shsul@eecs.umich.edu            else:
2363395Shsul@eecs.umich.edu                exit_event = m5.simulate(period)
2373395Shsul@eecs.umich.edu                sim_ticks += period
2383395Shsul@eecs.umich.edu                while exit_event.getCause() == "checkpoint":
2393395Shsul@eecs.umich.edu                    exit_event = m5.simulate(sim_ticks - m5.curTick())
2403395Shsul@eecs.umich.edu                if exit_event.getCause() == "simulate() limit reached":
2413509Shsul@eecs.umich.edu                    m5.checkpoint(root, joinpath(cptdir, "cpt.%d"))
2423395Shsul@eecs.umich.edu                    num_checkpoints += 1
2433395Shsul@eecs.umich.edu
2445185Ssaidi@eecs.umich.edu        if exit_event.getCause() != "simulate() limit reached":
2453999Ssaidi@eecs.umich.edu            exit_cause = exit_event.getCause();
2463999Ssaidi@eecs.umich.edu
2473999Ssaidi@eecs.umich.edu
2483395Shsul@eecs.umich.edu    else: #no checkpoints being taken via this script
2493395Shsul@eecs.umich.edu        exit_event = m5.simulate(maxtick)
2503395Shsul@eecs.umich.edu
2513395Shsul@eecs.umich.edu        while exit_event.getCause() == "checkpoint":
2523509Shsul@eecs.umich.edu            m5.checkpoint(root, joinpath(cptdir, "cpt.%d"))
2533395Shsul@eecs.umich.edu            num_checkpoints += 1
2543395Shsul@eecs.umich.edu            if num_checkpoints == max_checkpoints:
2553395Shsul@eecs.umich.edu                exit_cause =  "maximum %d checkpoints dropped" % max_checkpoints
2563395Shsul@eecs.umich.edu                break
2573395Shsul@eecs.umich.edu
2583511Shsul@eecs.umich.edu            exit_event = m5.simulate(maxtick - m5.curTick())
2593395Shsul@eecs.umich.edu            exit_cause = exit_event.getCause()
2603395Shsul@eecs.umich.edu
2613395Shsul@eecs.umich.edu    if exit_cause == '':
2623395Shsul@eecs.umich.edu        exit_cause = exit_event.getCause()
2633514Sktlim@umich.edu    print 'Exiting @ cycle %i because %s' % (m5.curTick(), exit_cause)
2643395Shsul@eecs.umich.edu
265