Learn gem5 #2 (Creating a simple configuration script~Extending gem5 for ARM)

Creating a simple configuration script

https://www.gem5.org/documentation/learning_gem5/part1/simple_config/

まずは動かしてみる。

$ mkdir -p configs/tutorial/part1/
$ cp configs/learning_gem5/part1/simple.py configs/tutorial/part1/
$ build/X86/gem5.opt configs/tutorial/part1/simple.py

動くようだ。

gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 16 2024 23:40:41
gem5 started Mar 23 2024 13:54:24
gem5 executing on DESKTOP-1L01RQ8, pid 18841
command line: build/X86/gem5.opt configs/tutorial/part1/simple.py

Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
Beginning simulation!
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 499308000 because exiting with last active thread context

このとき、m5outというログディレクトリができているので、念のためバックアップしておく。

$ mv m5out m5out.1

記事の内容については、Pythonのコードを追っていけばわかる話が多く、割愛する。
要するに、下図のようなシステムを構築している。

注記事項としては、PIOと割り込みを接続する必要があるのはx86の要件で、ほかのCPUでは省略できる点ぐらいであろうか。
あとは、SEモードとFSモードの話もしているが、今後詳細な説明が出てくると予想・・・

Adding cache to the configuration script

前回のシステムにキャッシュを付けるという例題になっている。

キャッシュのモデル化には2つの手法がある。

  • クラシックキャッシュ
  • Ruby

なぜ2つあるかというと、もともとgem5はm5とGEMSが組み合わさったものであり、クラシックキャッシュはm5由来のものであり、RubyはGEMS由来のものであるからである。キャッシュコヒーレンスプロトコルの違いが重要な場合はRubyを使うべきだが、それ以外の場合はクラシックキャッシュで十分である。

実際の実装についてはPythonコードを追っていけば記事の内容もわかるレベルだと思われる。
動かしてみる。

$ cp configs/learning_gem5/part1/caches.py configs/tutorial/part1/
$ cp configs/learning_gem5/part1/two_level.py configs/tutorial/part1/
$ build/X86/gem5.opt configs/tutorial/part1/two_level.py --l2_size='1MB' --l1d_size='128kB'

動いた。

gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 16 2024 23:40:41
gem5 started Mar 23 2024 14:07:50
gem5 executing on DESKTOP-1L01RQ8, pid 18885
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.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
Beginning simulation!
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 57774000 because exiting with last active thread context

キャッシュを追加したことでシミュレーション時間が 499308000 から 57774000 に減っている。

念のためログディレクトリをバックアップしておく。

$ mv m5out m5out.2

Understanding gem5 statistics and output

gem5実行完了後、m5outディレクトリに以下のファイルができる。

  • config.ini: シミュレーション中に作られた全SimObjectのリストとそのパラメータ値
  • config.json: config.iniのJSON形式
  • stats.txt: シミュレーション中に登録された全m5統計のテキスト表現

config.ini

SimObject名は[system.membus]のように[]で囲われたテキストで表現。
よくわからないが、1 tick は 1ps に相当し、クロックを1GHzに設定したので、1クロックサイクルは 1000 ticks に設定されたらしい。

[system.clk_domain]
clock=1000

stats.txt

詳細はgem5 wiki siteに書いてあると言っているがリンク切れになっている。。。
代表的なものとして、以下がある。

  • simSeconds シミュレーション時間(秒)
  • simInsts CPUで実行された命令数
  • hostInstRate シミュレーションのパフォーマンス(1秒当たりの命令実行数)

SimObjectの統計も出力されている。syscall数、キャッシュやtranslation buffer(?)の統計など。ファイルの後半はメモリコントローラの統計も出ている。それぞれのコンポーネントのreadバイト数や平均帯域など。

Using the default configuration scripts

今まではスクラッチから構成スクリプトを書いていたが、デフォルトの構成スクリプトを使うこともできる。

ディレクトリ構成

configs/ 以下にいろいろ入っている。

boot/

FSモードで使用される rcS ファイルが入っている。これらのファイルはLinux起動後にシミュレータによってロードされ、シェルによって実行される。これらのほとんどはFSモード実行中にベンチマークを制御するために使われる。hack_back_ckpt.rcS などのユーティリティ関数もあり、これらはFSシミュレーションの章で詳しく説明される。

common/

このディレクトリはシミュレーションシステムを作る際に使われるたくさんのヘルパースクリプト・ヘルパー関数が入っている。たとえば、Caches.pyは以前のチュートリアルで作ったcaches.pyやcaches_opts.pyに似たものである。

Options.pyはコマンドラインで設定されるさまざまなオプションが含まれる。CPU数、システムクロックなどなど。使いたいコマンドラインパラメータがあるときにすでに含まれていないか事前に確認すると良い。

CacheConfig.py はクラシックメモリシステムのキャッシュのオプションと機能が含まれる。

MemConfig.py はメモリシステム設定のためのヘルパー関数が含まれる。

FSConfig.py は様々なシステムをFSシミュレーションするための必須な関数が含まれる。FSシミュレーションについては独立した章で議論する。

Simulation.pyはgem5を動かしたり設定するための様々なヘルパー関数を含む。チェックポイントの保存や回復など。examples/以下の構成ファイルはこのファイルを使う。このファイルは非常に複雑だが、使うことで柔軟性を高める。

dram/

DRAMテストのスクリプト

examples/

例題となる構成スクリプトが入っている。特に se.py と fs.py は有用であり、次のセクションで解説する。

learning_gem5/

learning_gem5 book のスクリプト

network/

HeteroGarnet(?)ネットワークのスクリプト
補注:どうやらOn-chipインタコネクトやNoC関連のものらしい?

nvm/

NVMインタフェースの例

ruby/

Rubyで作られたキャッシュコヒーレンスプロトコル。詳細はRubyの章で解説。

splash2/

splash2ベンチマークスイートのスクリプト

topologies/

Rubyキャッシュ階層のトポロジ実装。詳細はRubyの章。

Using se.py and fs.py

ここではse.py, fs.pyの共通オプションを見ていく。オプションの多くは Options.py の addCommonOptions で登録される。全オプションを見るには --help を使ったり、スクリプトのコードを読めばいい。

以下を実行してみる。

$ build/X86/gem5.opt configs/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello

予想外の出力が出てきた・・・。将来的に廃止されるらしい?

fatal: The 'configs/example/se.py' script has been deprecated. It can be found in 'configs/deprecated/example' if required. Its usage should be avoided as it will be removed in future releases of gem5.

とりあえず、スクリプトのパスを変えて実行してみる。

$ build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello

実行できたみたいだ。

gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 16 2024 23:40:41
gem5 started Mar 23 2024 17:39:43
gem5 executing on DESKTOP-1L01RQ8, pid 18954
command line: build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello

warn: The se.py script is deprecated. It will be removed in future releases of  gem5.
Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
**** REAL SIMULATION ****
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 5943000 because exiting with last active thread context

しかし、このチュートリアルの作者的には全く面白くないらしい。アトミックCPU、アトミックメモリアクセスがデフォルトなので実際のタイミング情報が得られないらしい。このことは m5out/config.ini を見ると確認できる。オプションを変えて実行してみる。

$ build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello --cpu-type=TimingSimpleCPU --l1d_size=64kB --l1i_size=16kB
gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 16 2024 23:40:41
gem5 started Mar 23 2024 17:42:16
gem5 executing on DESKTOP-1L01RQ8, pid 18955
command line: build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello --cpu-type=TimingSimpleCPU --l1d_size=64kB --l1i_size=16kB

warn: The se.py script is deprecated. It will be removed in future releases of  gem5.
Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
**** REAL SIMULATION ****
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 454646000 because exiting with last active thread context

m5out/config.ini のcacheを見てみるとキャッシュが作成されてないことがわかる。キャッシュサイズを指定したところでキャッシュを使用することを指定しないとだめらしい。--caches オプションを追加する。

$ build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello --cpu-type=TimingSimpleCPU --l1d_size=64kB --l1i_size=16kB --caches
gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 16 2024 23:40:41
gem5 started Mar 23 2024 17:44:53
gem5 executing on DESKTOP-1L01RQ8, pid 18956
command line: build/X86/gem5.opt configs/deprecated/example/se.py --cmd=tests/test-progs/hello/bin/x86/linux/hello --cpu-type=TimingSimpleCPU --l1d_size=64kB --l1i_size=16kB --caches

warn: The se.py script is deprecated. It will be removed in future releases of  gem5.
Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
**** REAL SIMULATION ****
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 31680000 because exiting with last active thread context

キャッシュが入ったことで実行時間が早くなり、454646000 ticks から 31680000 に減った。このように、常に config.ini を確認することは重要である。

Some common options se.py and fs.py

 --help で全オプションを確認できる。重要なオプションは以下の通り。

--cpu-type=CPU_TYPE
--sys-clock=SYS_CLOCK
--cpu-clock=CPU_CLOCK
--mem-type=MEM_TYPE
--caches
--l2cache
--ruby
-m TICKS, --abs-max-tick=TICKS
-I MAXINSTS, --maxinsts=MAXINSTS
-c CMD, --cmd=CMD
-o OPTIONS, --options=OPTIONS
--output=OUTPUT
--errout=ERROUT

Extending gem5 for ARM

まずは指示通りにビルド。ビルドには30分弱かかる。。。。

$ mkdir -p cpu_tests/benchmarks/bin/arm
$ cd cpu_tests/benchmarks/bin/arm
$ wget dist.gem5.org/dist/v22-0/test-progs/cpu-tests/bin/arm/Bubblesort
$ wget dist.gem5.org/dist/v22-0/test-progs/cpu-tests/bin/arm/FloatMM
$ cd -
$ scons build/ARM/gem5.opt -j 20

Modifying simple.py to run ARM Binaries

$ cp configs/tutorial/part1/simple.py configs/tutorial/part1/simple_arm.py
$ vi configs/tutorial/part1/simple_arm.py

コメントアウトする。

#system.cpu.interrupts[0].pio = system.membus.mem_side_ports
#system.cpu.interrupts[0].int_requestor = system.membus.cpu_side_ports
#system.cpu.interrupts[0].int_responder = system.membus.mem_side_ports

バイナリの指定。binary変数はほかでも使われてるので、cmd.processの方を直接書き換えるとエラーが出る。

binary = os.path.join(
    thispath,
    "../../../",
    #"tests/test-progs/hello/bin/x86/linux/hello",
    "tests/test-progs/hello/bin/arm/linux/hello",
)

実行する。

build/ARM/gem5.opt configs/tutorial/part1/simple_arm.py

なんかエラーが出る。

NameError: name 'X86TimingSimpleCPU' is not defined

CPUの指定を書き換えて再度実行。。。

#system.cpu = X86TimingSimpleCPU()
system.cpu = ArmTimingSimpleCPU()
gem5 Simulator System.  https://www.gem5.org
gem5 is copyrighted software; use the --copyright option for details.

gem5 version 23.1.0.0
gem5 compiled Mar 23 2024 18:16:56
gem5 started Mar 23 2024 18:33:38
gem5 executing on DESKTOP-1L01RQ8, pid 5640
command line: build/ARM/gem5.opt configs/tutorial/part1/simple_arm.py

Global frequency set at 1000000000000 ticks per second
warn: No dot file generated. Please install pydot to generate the dot file and pdf.
src/mem/dram_interface.cc:690: warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
src/base/statistics.hh:279: warn: One of the stats is a legacy stat. Legacy stat is a stat that does not belong to any statistics::Group. Legacy stat is deprecated.
system.remote_gdb: Listening for connections on port 7000
Beginning simulation!
src/sim/simulate.cc:199: info: Entering event queue @ 0.  Starting simulation...
Hello world!
Exiting @ tick 374532000 because exiting with last active thread context

なんかWarningが出るが、とりあえずは実行できてるっぽい。

ARM Full System Simulation

動かしてみたもののうまくいかなかった。

$ cd util/term/
$ make
$ cd -
$ mkdir -p fs_image
$ cd fs_image
$ wget http://dist.gem5.org/dist/v22-0/arm/aarch-system-20220707.tar.bz2
$ tar jxvf aarch-system-20220707.tar.bz2
$ cd -

絶対パスの指定

$ export IMG_ROOT=/absolute/path/to/fs_images

実行。何を指定したらいいのかわからないので、適当に指定した。

$ build/ARM/gem5.opt configs/example/arm/fs_bigLITTLE.py --caches --bootloader="$IMG_ROOT/binaries/boot.arm" --ke
rnel="$IMG_ROOT/binaries/vmlinux.arm" --disk="$IMG_ROOT/disks/m5_exit.squashfs.arm" --bootscript=configs/boot/bbench-gb.rcS

なんか動いてはいるっぽいが、よくわからない・・・。おそらく動かし方が間違っているのだろう。
今のところARMのFSシミュレーションについてモチベーションがないため実験はここまでにしておく。