向配置文件中添加Cache
现在,让我们把刚刚创建的缓存添加到我们在上一章创建的配置脚本中。
首先,让我们把脚本复制到一个新的文件名。
cp./configs/tutorial/part1/simple.py ./configs/tutorial/part1/two_level.py
首先,我们需要将caches.py文件中的名字导入命名空间。我们可以在文件的顶部添加以下内容(在m5.objects的导入之后),就像你对任何Python源文件一样。
from caches import *
现在,在创建了CPU之后,让我们来创建L1缓存:
system.cpu.icache = L1ICache()
system.cpu.dcache = L1DCache()
并用我们创建的辅助函数将缓存连接到CPU端口。
system.cpu.icache.connectCPU(system.cpu)
system.cpu.dcache.connectCPU(system.cpu)
你需要删除以下两行,这两行是直接将缓存端口连接到内存总线上的。
system.cpu.icache_port = system.membus.cpu_side_ports
system.cpu.dcache_port = system.membus.cpu_side_ports
我们不能直接将L1缓存连接到L2缓存,因为L2缓存只希望有一个端口连接到它。因此,我们需要创建一个L2总线来连接我们的L1缓存和L2缓存。我们可以使用我们的辅助函数来连接L1缓存和L2总线。
system.l2bus = L2XBar()
system.cpu.icache.connectBus(system.l2bus)
system.cpu.dcache.connectBus(system.l2bus)
接下来,我们可以创建我们的二级缓存,并将其连接到二级总线和内存总线上。
system.l2cache = L2Cache()
system.l2cache.connectCPUSideBus(system.l2bus)
system.membus = SystemXBar()
system.l2cache.connectMemSideBus(system.membus)
请注意,在system.l2cache.connectMemSideBus之前已经定义了system.membus = SystemXBar(),所以我们可以把它传给system.l2cache.connectMemSideBus。文件中的其他内容保持不变! 现在我们有了一个完整的配置,有了两级缓存的层次结构。如果你运行当前的文件,hello现在应该在57467000个时间戳中完成。完整的脚本可以在gem5源代码中找到,地址是configs/learning_gem5/part1/two_level.py。
为你的脚本添加参数
在使用gem5进行实验时,您不希望每次想用不同参数测试系统时都要编辑配置脚本。为了解决这个问题,你可以在你的 gem5 配置脚本中添加命令行参数。同样,由于配置脚本只是 Python,你可以使用支持参数解析的 Python 库。尽管 pyoptparse 已被正式废弃,但由于 gem5 的最低 Python 版本曾是 2.5,所以许多随 gem5 一起提供的配置脚本都使用它而不是 pyargparse。现在的最小Python版本是3.6,所以当编写不需要与当前gem5脚本交互的新脚本时,Python的argparse是一个更好的选择。要开始使用 :pyoptparse,你可以查阅在线 Python 文档。
为了给我们的两级缓存配置添加选项,在导入我们的缓存后,让我们添加一些选项。
import argparse
parser = argparse.ArgumentParser(description='A simple system with 2-level cache.')
parser.add_argument("binary", default="", nargs="?", type=str,
help="Path to the binary to execute.")
parser.add_argument("--l1i_size",
help=f"L1 instruction cache size. Default: 16kB.")
parser.add_argument("--l1d_size",
help="L1 data cache size. Default: Default: 64kB.")
parser.add_argument("--l2_size",
help="L2 cache size. Default: 256kB.")
options = parser.parse_args()
注意,如果你想按照上面的方式传递二进制文件的路径,并通过选项使用它,你应该把它指定为options.binary。比如说
system.workload = SEWorkload.init_compatible(options.binary)
现在,你可以运行
build/X86/gem5.opt configs/tutorial/part1/two_level.py --help
它将显示你刚刚添加的选项。
接下来,我们需要将这些选项传递给我们在配置脚本中创建的缓存。要做到这一点,我们只需修改two_level_opts.py,将选项作为参数传入缓存的构造器,并添加一个适当的构造器,接下来。
system.cpu.icache = L1ICache(options)
system.cpu.dcache = L1DCache(options)
...
system.l2cache = L2Cache(options)
在caches.py中,我们需要为我们的每个类添加构造函数(Python中的__init__函数)。从我们的基础 L1 缓存开始,我们只需添加一个空的构造函数,因为我们没有任何适用于基础 L1 缓存的参数。然而,在这种情况下,我们不能忘记调用超类的构造函数。如果跳过了对超类构造函数的调用,gem5的SimObject属性查找函数就会失败,当你试图实例化缓存对象时,结果将是 "RuntimeError: maximum recursion depth exceeded"。因此,在L1Cache中,我们需要在静态类成员后添加以下内容。
def __init__(self, options=None):
super(L1Cache, self).__init__()
pass
接下来,在L1ICache中,我们需要使用我们创建的选项(l1i_size)来设置大小。在下面的代码中,如果options没有被传递给L1ICache构造函数,以及在命令行中没有指定options,就会有防护措施。在这些情况下,我们将直接使用我们已经指定的默认大小。
def __init__(self, options=None):
super(L1ICache, self).__init__(options)
if not options or not options.l1i_size:
return
self.size = options.l1i_size
我们可以对L1DCache使用相同的代码:
def __init__(self, options=None):
super(L1DCache, self).__init__(options)
if not options or not options.l1d_size:
return
self.size = options.l1d_size
以及统一的L2Cache:
def __init__(self, options=None):
super(L2Cache, self).__init__()
if not options or not options.l2_size:
return
self.size = options.l2_size
有了这些变化,你现在可以从命令行中把缓存的大小传给你的脚本,就像下面这样。
build/X86/gem5.opt configs/tutorial/part1/two_level.py --l2_size='1MB' --l1d_size='128kB'
gem5 Simulator System. http://gem5.org
gem5 is copyrighted software; use the --copyright option for details.
gem5 version 21.0.0.0
gem5 compiled May 17 2021 18:05:59
gem5 started May 18 2021 00:00:33
gem5 executing on amarillo, pid 83118
command line: build/X86/gem5.opt configs/tutorial/part1/two_level.py --l2_size=1MB --l1d_size=128kB
Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
0: system.remote_gdb: listening for remote gdb on port 7005
Beginning simulation!
info: Entering event queue @ 0. Starting simulation...
Hello world!
Exiting @ tick 57467000 because exiting with last active thread context
完整的脚本可以在gem5源代码中找到:configs/learning_gem5/part1/caches.py 和 configs/learning_gem5/part1/two_level.py。
文章作者:速易芯李昊翔