Sampling

From gem5
Jump to: navigation, search

Sampling (switching between functional and detailed models) can be implemented via your Python script. In your script you can direct the simulator to switch between two sets of CPUs. To do this, in your script setup a list of tuples of (oldCPU, newCPU). If there are multiple CPUs you wish to switch simultaneously, they can all be added to that list. For example:

run_cpu1 = SimpleCPU()
switch_cpu1 = DetailedCPU(defer_registration=True)
run_cpu2 = SimpleCPU()
switch_cpu2 = FooCPU(defer_registration=True)
switch_cpu_list = [(run_cpu1,switch_cpu1),(run_cpu2,switch_cpu2)]

Note that the CPU that does not immediately run should have the parameter "defer_registration=True". This keeps those CPUs from adding themselves to the list of CPUs to run; they will instead get added when you switch them in.

In order for M5 to instantiate all of your CPUs, you must make the CPUs that will be switched in a child of something that is in the configuration hierarchy. Unfortunately at the moment some configuration limitations force the switch CPU to be placed outside of the System object. The Root object is the next most convenient place to place the CPU, as shown below:

root1 = Root()
root1.system = System(cpu = run_cpu1)
root1.switch_cpu = switch_cpu1
root2 = Root()
root2.system = System(cpu = run_cpu2)
root2.switch_cpu = switch_cpu2

This will add the swtich CPUs as children of each root object. Note that switch_cpu is not an actual parameter for Root, but is just an assignment to indicate that it has a child, switch_cpu.

After the systems and the CPU list is setup, your script can direct M5 to switch the CPUs at the appropriate cycle. This is achieved by calling switchCpus(cpus_list). For example, assuming the code above, and a system that is setup running run_cpu1 and run_cpu2 initially:

m5.simulate(500)  # simulate for 500 cycles
m5.switchCpus(switch_cpu_list)
m5.simulate(500)  # simulate another 500 cycles after switching

Note that M5 may have to simulate for a few cycles prior to switching CPUs due to any outstanding state that may be present in the CPUs being switched out.