Gem5模拟器学习(九)————配置文件

fs.py配置文件

fs.py文件是可以运行FS模式模拟的通用配置文件,但由于其编写的时间很早,部分指令集的新功能没有更新。

如不支持RISCV

一、导入库

fs.py自己的路径configs/example/fs.py是知道的,它用addToPath(“…/…/”),使得可以直接导入confis/中的模块,然后引用了configs里自带的一些python文件。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# [fs.py]
import argparse
import sys
# 由于在之前的配置文件中将src/python/文件夹也加入了库搜索路径,所以可以import m5
import m5
from m5.defines import buildEnv
from m5.objects import *
from m5.util import addToPath, fatal, warn
from m5.util.fdthelper import *

addToPath('../')
# confis/ 中含有ruby/和common/
from ruby import Ruby

from common.FSConfig import *
from common.SysPaths import *
from common.Benchmarks import *
from common import Simulation
from common import CacheConfig
from common import CpuConfig
from common import MemConfig
from common import ObjectList
from common.Caches import *
from common import Options

二、命令行参数

1
2
3
4
5
6
7
8
9
10
11
# [fs.py]
# Add args
parser = argparse.ArgumentParser()
Options.addCommonOptions(parser) # 添加常用参数。如CPU类型、CPU时钟频率等
Options.addFSOptions(parser) # 添加FS模拟参数。如使用的内核、镜像文件等

# Add the ruby specific and protocol specific args
if '--ruby' in sys.argv:
Ruby.define_options(parser)

args = parser.parse_args()

第2行通过标准库argparse的ArgumentParser()方法初始化了parser类

第3、4行通过configs/common/Options.py模块中的两个方法中添加相应命令行选项。

第7行检查是否配置了ruby。如果配置了,则在第8行,通过confis/ruby/Ruby.py模块中的define_options()方法配置ruby相关的参数,其中,network相关参数也会配置。

最后,通过标准库argparse的解析函数对parser进行参数解析,得到了args,所有参数都存储在args中。

args 是一个包含所有命令行参数值的命名空间对象。每个命令行参数都成为这个对象的一个属性,可以通过 args.参数名 的形式访问。
在 gem5 的上下文中,这意味着 args 包含了模拟器运行所需的所有配置信息,如是否启用 Ruby 模拟、文件系统的配置等。

以上代码定义了argparse解析器并添加了命令行参数。然后它解析了这些参数,并基于这些参数来设置CPU类和内存类。

三、确定CPU和Memory类型

1
2
3
4
5
# system under test can be any CPU
(TestCPUClass, test_mem_mode, FutureClass) = Simulation.setCPUClass(args)

# Match the memories with the CPUs, based on the options for the test system
TestMemClass = Simulation.setMemClass(args)

四、配置benchmark

Benchmarks为一个字典,在configs/common/Benchmark.py中定义

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if args.benchmark:
try:
bm = Benchmarks[args.benchmark]
except KeyError:
print("Error benchmark %s has not been defined." % args.benchmark)
print("Valid benchmarks are: %s" % DefinedBenchmarks)
sys.exit(1)
else:
if args.dual:
bm = [SysConfig(disks=args.disk_image, rootdev=args.root_device,
mem=args.mem_size, os_type=args.os_type),
SysConfig(disks=args.disk_image, rootdev=args.root_device,
mem=args.mem_size, os_type=args.os_type)]
else:
bm = [SysConfig(disks=args.disk_image, rootdev=args.root_device,
mem=args.mem_size, os_type=args.os_type)]

五、root

在gem5中,root是整个模拟环境的最顶层容器,它通常包含了模拟的计算机系统(如处理器、内存、总线等)以及与这些组件连接的所有设备和系统的配置。这样的结构设计允许gem5能够方便地访问和管理模拟的不同部分。

本质上,fs.py只是运行了最后一行的代码:

1
2
# [fs.py]
Simulation.run(args, root, test_sys, FutureClass)

其中,args是所有参数,root是仿真的硬件,test_sys是模拟的系统

如果模拟的是一个双系统(dual系统),那么root会通过调用makeDualRoot()函数被创建,这意味着会有两个系统(test_sys和drive_sys)并行运行在模拟中。
如果启用了分布式模拟(dist模式),则通过调用makeDistRoot()函数创建root,此时root代表的系统会作为分布式模拟中的一个节点。
如果模拟的是单个系统,那么root会简单地通过Root(full_system=True, system=test_sys)创建,其中test_sys是通过build_test_system()函数构建的测试系统

之后,它构建测试系统,可能还会构建驱动系统,最后创建一个root根对象来启动模拟。

在gem5中,root是整个模拟环境的最顶层容器,它通常包含了模拟的计算机系统(如处理器、内存、总线等)以及与这些组件连接的所有设备和系统的配置。

六、构建系统

构造系统是配置文件的主要工作,因此通过函数单独实现。

通过代码中的build_test_system()函数构造测试系统;如果双系统,还会通过build_drive_system()构建驱动系统。

并分别通过Root()、makeDualRoot()、makeDistRoot()构建单系统、双系统、分布式系统的root。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
np = args.num_cpus

test_sys = build_test_system(np)

if len(bm) == 2:
drive_sys = build_drive_system(np)
root = makeDualRoot(True, test_sys, drive_sys, args.etherdump)
elif len(bm) == 1 and args.dist:
# This system is part of a dist-gem5 simulation
root = makeDistRoot(test_sys,
args.dist_rank,
args.dist_size,
args.dist_server_name,
args.dist_server_port,
args.dist_sync_repeat,
args.dist_sync_start,
args.ethernet_linkspeed,
args.ethernet_linkdelay,
args.etherdump);
elif len(bm) == 1:
root = Root(full_system=True, system=test_sys)
else:
print("Error I don't know how to create more than 2 systems.")
sys.exit(1)

构造测试系统

不同的ISA不同,以X86为例:

第3行~第30行:首先通过makeLinuxX86System,创建一个基础的test_sys。

第32行~第62行:然后再指定一些细节,如cpu等。

第64行~第134行:如果使用ruby,test_sys有更多细节。

第136行~第148行:如果使用KVM加速,则设置KVM。

最后返回测试系统test_sys。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
def build_test_system(np):
cmdline = cmd_line_template()
if buildEnv['TARGET_ISA'] == "mips":
test_sys = makeLinuxMipsSystem(test_mem_mode, bm[0], cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == "sparc":
test_sys = makeSparcSystem(test_mem_mode, bm[0], cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == "riscv":
test_sys = makeBareMetalRiscvSystem(test_mem_mode, bm[0],
cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == "x86":
test_sys = makeLinuxX86System(test_mem_mode, np, bm[0], args.ruby,
cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == "arm":
test_sys = makeArmSystem(
test_mem_mode,
args.machine_type,
np,
bm[0],
args.dtb_filename,
bare_metal=args.bare_metal,
cmdline=cmdline,
external_memory=args.external_memory_system,
ruby=args.ruby,
vio_9p=args.vio_9p,
bootloader=args.bootloader,
)
if args.enable_context_switch_stats_dump:
test_sys.enable_context_switch_stats_dump = True
else:
fatal("Incapable of building %s full system!", buildEnv['TARGET_ISA'])

# Set the cache line size for the entire system
test_sys.cache_line_size = args.cacheline_size

# Create a top-level voltage domain
test_sys.voltage_domain = VoltageDomain(voltage = args.sys_voltage)

# Create a source clock for the system and set the clock period
test_sys.clk_domain = SrcClockDomain(clock = args.sys_clock,
voltage_domain = test_sys.voltage_domain)

# Create a CPU voltage domain
test_sys.cpu_voltage_domain = VoltageDomain()

# Create a source clock for the CPUs and set the clock period
test_sys.cpu_clk_domain = SrcClockDomain(clock = args.cpu_clock,
voltage_domain =
test_sys.cpu_voltage_domain)

if buildEnv['TARGET_ISA'] == 'riscv':
test_sys.workload.bootloader = args.kernel
elif args.kernel is not None:
test_sys.workload.object_file = binary(args.kernel)

if args.script is not None:
test_sys.readfile = args.script

test_sys.init_param = args.init_param

# For now, assign all the CPUs to the same clock domain
test_sys.cpu = [TestCPUClass(clk_domain=test_sys.cpu_clk_domain, cpu_id=i)
for i in range(np)]

if args.ruby:
bootmem = getattr(test_sys, '_bootmem', None)
Ruby.create_system(args, True, test_sys, test_sys.iobus,
test_sys._dma_ports, bootmem)

# Create a seperate clock domain for Ruby
test_sys.ruby.clk_domain = SrcClockDomain(clock = args.ruby_clock,
voltage_domain = test_sys.voltage_domain)

# Connect the ruby io port to the PIO bus,
# assuming that there is just one such port.
test_sys.iobus.mem_side_ports = test_sys.ruby._io_port.in_ports

for (i, cpu) in enumerate(test_sys.cpu):
#
# Tie the cpu ports to the correct ruby system ports
#
cpu.clk_domain = test_sys.cpu_clk_domain
cpu.createThreads()
cpu.createInterruptController()

test_sys.ruby._cpu_ports[i].connectCpuPorts(cpu)

else:
if args.caches or args.l2cache:
# By default the IOCache runs at the system clock
test_sys.iocache = IOCache(addr_ranges = test_sys.mem_ranges)
test_sys.iocache.cpu_side = test_sys.iobus.mem_side_ports
test_sys.iocache.mem_side = test_sys.membus.cpu_side_ports
elif not args.external_memory_system:
test_sys.iobridge = Bridge(delay='50ns', ranges = test_sys.mem_ranges)
test_sys.iobridge.cpu_side_port = test_sys.iobus.mem_side_ports
test_sys.iobridge.mem_side_port = test_sys.membus.cpu_side_ports

# Sanity check
if args.simpoint_profile:
if not ObjectList.is_noncaching_cpu(TestCPUClass):
fatal("SimPoint generation should be done with atomic cpu")
if np > 1:
fatal("SimPoint generation not supported with more than one CPUs")

for i in range(np):
if args.simpoint_profile:
test_sys.cpu[i].addSimPointProbe(args.simpoint_interval)
if args.checker:
test_sys.cpu[i].addCheckerCpu()
if not ObjectList.is_kvm_cpu(TestCPUClass):
if args.bp_type:
bpClass = ObjectList.bp_list.get(args.bp_type)
test_sys.cpu[i].branchPred = bpClass()
if args.indirect_bp_type:
IndirectBPClass = ObjectList.indirect_bp_list.get(
args.indirect_bp_type)
test_sys.cpu[i].branchPred.indirectBranchPred = \
IndirectBPClass()
test_sys.cpu[i].createThreads()

# If elastic tracing is enabled when not restoring from checkpoint and
# when not fast forwarding using the atomic cpu, then check that the
# TestCPUClass is DerivO3CPU or inherits from DerivO3CPU. If the check
# passes then attach the elastic trace probe.
# If restoring from checkpoint or fast forwarding, the code that does this for
# FutureCPUClass is in the Simulation module. If the check passes then the
# elastic trace probe is attached to the switch CPUs.
if args.elastic_trace_en and args.checkpoint_restore == None and \
not args.fast_forward:
CpuConfig.config_etrace(TestCPUClass, test_sys.cpu, args)

CacheConfig.config_cache(args, test_sys)

MemConfig.config_mem(args, test_sys)

if ObjectList.is_kvm_cpu(TestCPUClass) or \
ObjectList.is_kvm_cpu(FutureClass):
# Assign KVM CPUs to their own event queues / threads. This
# has to be done after creating caches and other child objects
# since these mustn't inherit the CPU event queue.
for i,cpu in enumerate(test_sys.cpu):
# Child objects usually inherit the parent's event
# queue. Override that and use the same event queue for
# all devices.
for obj in cpu.descendants():
obj.eventq_index = 0
cpu.eventq_index = i + 1
test_sys.kvm_vm = KvmVM()

return test_sys

构造驱动系统

与测试系统的构造大致一致,先构建基本系统,然后再指定一些细节。

最后返回驱动系统drive_sys

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
def build_drive_system(np):
# driver system CPU is always simple, so is the memory
# Note this is an assignment of a class, not an instance.
DriveCPUClass = AtomicSimpleCPU
drive_mem_mode = 'atomic'
DriveMemClass = SimpleMemory

cmdline = cmd_line_template()
if buildEnv['TARGET_ISA'] == 'mips':
drive_sys = makeLinuxMipsSystem(drive_mem_mode, bm[1], cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == 'sparc':
drive_sys = makeSparcSystem(drive_mem_mode, bm[1], cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == 'x86':
drive_sys = makeLinuxX86System(drive_mem_mode, np, bm[1],
cmdline=cmdline)
elif buildEnv['TARGET_ISA'] == 'arm':
drive_sys = makeArmSystem(drive_mem_mode, args.machine_type, np,
bm[1], args.dtb_filename, cmdline=cmdline)

# Create a top-level voltage domain
drive_sys.voltage_domain = VoltageDomain(voltage = args.sys_voltage)

# Create a source clock for the system and set the clock period
drive_sys.clk_domain = SrcClockDomain(clock = args.sys_clock,
voltage_domain = drive_sys.voltage_domain)

# Create a CPU voltage domain
drive_sys.cpu_voltage_domain = VoltageDomain()

# Create a source clock for the CPUs and set the clock period
drive_sys.cpu_clk_domain = SrcClockDomain(clock = args.cpu_clock,
voltage_domain =
drive_sys.cpu_voltage_domain)

drive_sys.cpu = DriveCPUClass(clk_domain=drive_sys.cpu_clk_domain,
cpu_id=0)
drive_sys.cpu.createThreads()
drive_sys.cpu.createInterruptController()
drive_sys.cpu.connectBus(drive_sys.membus)
if args.kernel is not None:
drive_sys.workload.object_file = binary(args.kernel)

if ObjectList.is_kvm_cpu(DriveCPUClass):
drive_sys.kvm_vm = KvmVM()

drive_sys.iobridge = Bridge(delay='50ns',
ranges = drive_sys.mem_ranges)
drive_sys.iobridge.cpu_side_port = drive_sys.iobus.mem_side_ports
drive_sys.iobridge.mem_side_port = drive_sys.membus.cpu_side_ports

# Create the appropriate memory controllers and connect them to the
# memory bus
drive_sys.mem_ctrls = [DriveMemClass(range = r)
for r in drive_sys.mem_ranges]
for i in range(len(drive_sys.mem_ctrls)):
drive_sys.mem_ctrls[i].port = drive_sys.membus.mem_side_ports

drive_sys.init_param = args.init_param

return drive_sys

se.py配置文件

fs_linux.py配置文件