<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.haidhi.my.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Gem5%3A_hello_world</id>
	<title>Gem5: hello world - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.haidhi.my.id/wiki/index.php?action=history&amp;feed=atom&amp;title=Gem5%3A_hello_world"/>
	<link rel="alternate" type="text/html" href="https://wiki.haidhi.my.id/wiki/index.php?title=Gem5:_hello_world&amp;action=history"/>
	<updated>2026-11-01T21:45:51Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://wiki.haidhi.my.id/wiki/index.php?title=Gem5:_hello_world&amp;diff=868&amp;oldid=prev</id>
		<title>SanzSkuy: Created page with &quot;&#039;&#039;&#039;gem5&#039;&#039;&#039; adalah simulator arsitektur komputer open-source yang dipakai untuk riset dan pembelajaran. Dengan gem5, sebuah komputer virtual (CPU, cache, bus, memori) dirakit lewat script Python, lalu sebuah program dijalankan di atasnya dan gem5 melaporkan statistik detail seperti jumlah cycle, jumlah instruksi, dan cache miss.  Catatan ini mencakup pemasangan gem5 sampai menjalankan program &quot;Hello World&quot; pertama.  == Persiapan ==  Catatan ini memakai Linux (Ubuntu/Debia...&quot;</title>
		<link rel="alternate" type="text/html" href="https://wiki.haidhi.my.id/wiki/index.php?title=Gem5:_hello_world&amp;diff=868&amp;oldid=prev"/>
		<updated>2026-10-11T08:43:05Z</updated>

		<summary type="html">&lt;p&gt;Created page with &amp;quot;&amp;#039;&amp;#039;&amp;#039;gem5&amp;#039;&amp;#039;&amp;#039; adalah simulator arsitektur komputer open-source yang dipakai untuk riset dan pembelajaran. Dengan gem5, sebuah komputer virtual (CPU, cache, bus, memori) dirakit lewat script Python, lalu sebuah program dijalankan di atasnya dan gem5 melaporkan statistik detail seperti jumlah cycle, jumlah instruksi, dan cache miss.  Catatan ini mencakup pemasangan gem5 sampai menjalankan program &amp;quot;Hello World&amp;quot; pertama.  == Persiapan ==  Catatan ini memakai Linux (Ubuntu/Debia...&amp;quot;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;&amp;#039;&amp;#039;&amp;#039;gem5&amp;#039;&amp;#039;&amp;#039; adalah simulator arsitektur komputer open-source yang dipakai untuk riset dan pembelajaran. Dengan gem5, sebuah komputer virtual (CPU, cache, bus, memori) dirakit lewat script Python, lalu sebuah program dijalankan di atasnya dan gem5 melaporkan statistik detail seperti jumlah cycle, jumlah instruksi, dan cache miss.&lt;br /&gt;
&lt;br /&gt;
Catatan ini mencakup pemasangan gem5 sampai menjalankan program &amp;quot;Hello World&amp;quot; pertama.&lt;br /&gt;
&lt;br /&gt;
== Persiapan ==&lt;br /&gt;
&lt;br /&gt;
Catatan ini memakai Linux (Ubuntu/Debian). Paket yang dibutuhkan (mengikuti dokumentasi gem5; cek [https://www.gem5.org/documentation/general_docs/building dokumentasi resmi] kalau versinya berbeda):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
sudo apt install build-essential scons python3-dev git pre-commit \&lt;br /&gt;
    zlib1g zlib1g-dev libprotobuf-dev protobuf-compiler libprotoc-dev \&lt;br /&gt;
    libgoogle-perftools-dev libboost-all-dev libhdf5-serial-dev \&lt;br /&gt;
    python3-pydot python3-venv python3-tk mypy m4 libcapstone-dev \&lt;br /&gt;
    libpng-dev libelf-dev pkg-config wget cmake doxygen&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Clone dan build ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
git clone https://github.com/gem5/gem5&lt;br /&gt;
cd gem5&lt;br /&gt;
scons build/ALL/gem5.opt -j &amp;lt;JUMLAH CPU&amp;gt;&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Keterangan:&lt;br /&gt;
* &amp;lt;code&amp;gt;build/ALL&amp;lt;/code&amp;gt; artinya build mencakup semua ISA (x86, ARM, RISC-V, dan lainnya). Alternatifnya &amp;lt;code&amp;gt;build/X86/gem5.opt&amp;lt;/code&amp;gt; kalau hanya butuh x86, build-nya lebih ringan.&lt;br /&gt;
* &amp;lt;code&amp;gt;gem5.opt&amp;lt;/code&amp;gt; adalah varian dengan optimasi dan debug info secukupnya. Untuk debugging dengan gdb, pakai &amp;lt;code&amp;gt;gem5.debug&amp;lt;/code&amp;gt; (lebih lambat, build lebih lama).&lt;br /&gt;
* &amp;lt;code&amp;gt;-j&amp;lt;/code&amp;gt; menentukan jumlah proses paralel. Isi dengan hasil &amp;lt;code&amp;gt;nproc&amp;lt;/code&amp;gt;. Build pertama memakan waktu lama dan banyak RAM; kalau RAM kurang, kecilkan nilai &amp;lt;code&amp;gt;-j&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Hello World pertama ==&lt;br /&gt;
&lt;br /&gt;
gem5 menyertakan script contoh untuk menjalankan program sederhana:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
build/ALL/gem5.opt configs/learning_gem5/part1/simple.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Script ini merakit satu CPU sederhana dengan memori, lalu menjalankan program &amp;quot;Hello World&amp;quot; di dalamnya (mode &amp;#039;&amp;#039;Syscall Emulation&amp;#039;&amp;#039;). Kalau berhasil, di terminal muncul tulisan &amp;lt;code&amp;gt;Hello world!&amp;lt;/code&amp;gt; dan baris penutup yang menyebut tick terakhir simulasi. Angka tick bisa berbeda antarversi gem5.&lt;br /&gt;
&lt;br /&gt;
=== Isi folder &amp;lt;code&amp;gt;m5out/&amp;lt;/code&amp;gt; ===&lt;br /&gt;
&lt;br /&gt;
Setiap simulasi menghasilkan folder &amp;lt;code&amp;gt;m5out/&amp;lt;/code&amp;gt; di direktori tempat gem5 dijalankan:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! File !! Isi&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;stats.txt&amp;lt;/code&amp;gt; || Semua statistik simulasi (cycle, instruksi, cache miss, dan lainnya)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;config.ini&amp;lt;/code&amp;gt; / &amp;lt;code&amp;gt;config.json&amp;lt;/code&amp;gt; || Konfigurasi sistem yang disimulasikan, lengkap dengan semua parameter&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;config.dot.svg&amp;lt;/code&amp;gt; || Diagram blok sistem&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Nilai yang pertama kali layak dilihat di &amp;lt;code&amp;gt;stats.txt&amp;lt;/code&amp;gt;: &amp;lt;code&amp;gt;simSeconds&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;simInsts&amp;lt;/code&amp;gt;, dan &amp;lt;code&amp;gt;system.cpu.numCycles&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
== Merakit sistem sendiri ==&lt;br /&gt;
&lt;br /&gt;
Script berikut melakukan hal yang sama dengan &amp;lt;code&amp;gt;simple.py&amp;lt;/code&amp;gt;, tetapi ditulis manual agar terlihat tiap komponennya.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot;&amp;gt;&lt;br /&gt;
import m5&lt;br /&gt;
from m5.objects import *&lt;br /&gt;
from gem5.resources.resource import obtain_resource&lt;br /&gt;
&lt;br /&gt;
system = System()&lt;br /&gt;
system.clk_domain = SrcClockDomain()&lt;br /&gt;
system.clk_domain.clock = &amp;quot;1GHz&amp;quot;&lt;br /&gt;
system.clk_domain.voltage_domain = VoltageDomain()&lt;br /&gt;
&lt;br /&gt;
system.mem_mode = &amp;quot;timing&amp;quot;&lt;br /&gt;
system.mem_ranges = [AddrRange(&amp;quot;512MB&amp;quot;)]&lt;br /&gt;
&lt;br /&gt;
system.cpu = X86TimingSimpleCPU()&lt;br /&gt;
&lt;br /&gt;
system.membus = SystemXBar()&lt;br /&gt;
&lt;br /&gt;
# Tanpa cache: CPU langsung ke membus&lt;br /&gt;
system.cpu.icache_port = system.membus.cpu_side_ports&lt;br /&gt;
system.cpu.dcache_port = system.membus.cpu_side_ports&lt;br /&gt;
&lt;br /&gt;
# Khusus x86: interrupt controller disambung manual&lt;br /&gt;
system.cpu.createInterruptController()&lt;br /&gt;
system.cpu.interrupts[0].pio = system.membus.mem_side_ports&lt;br /&gt;
system.cpu.interrupts[0].int_requestor = system.membus.cpu_side_ports&lt;br /&gt;
system.cpu.interrupts[0].int_responder = system.membus.mem_side_ports&lt;br /&gt;
&lt;br /&gt;
system.system_port = system.membus.cpu_side_ports&lt;br /&gt;
&lt;br /&gt;
system.mem_ctrl = MemCtrl()&lt;br /&gt;
system.mem_ctrl.dram = DDR3_1600_8x8()&lt;br /&gt;
system.mem_ctrl.dram.range = system.mem_ranges[0]&lt;br /&gt;
system.mem_ctrl.port = system.membus.mem_side_ports&lt;br /&gt;
&lt;br /&gt;
# Workload: program Hello World (SE mode)&lt;br /&gt;
binary = obtain_resource(&amp;quot;x86-hello64-static&amp;quot;).get_local_path()&lt;br /&gt;
system.workload = SEWorkload.init_compatible(binary)&lt;br /&gt;
&lt;br /&gt;
process = Process()&lt;br /&gt;
process.cmd = [binary]&lt;br /&gt;
system.cpu.workload = process&lt;br /&gt;
system.cpu.createThreads()&lt;br /&gt;
&lt;br /&gt;
root = Root(full_system=False, system=system)&lt;br /&gt;
m5.instantiate()&lt;br /&gt;
&lt;br /&gt;
print(&amp;quot;Beginning simulation!&amp;quot;)&lt;br /&gt;
exit_event = m5.simulate()&lt;br /&gt;
print(f&amp;quot;Exiting @ tick {m5.curTick()} because {exit_event.getCause()}&amp;quot;)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Jalankan dengan:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;bash&amp;quot;&amp;gt;&lt;br /&gt;
build/ALL/gem5.opt my_simple.py&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Catatan: kalau resource &amp;lt;code&amp;gt;x86-hello64-static&amp;lt;/code&amp;gt; tidak ditemukan di versi gem5 yang dipakai, ganti &amp;lt;code&amp;gt;binary&amp;lt;/code&amp;gt; dengan path ke binary hello di source tree (&amp;lt;code&amp;gt;tests/test-progs/hello/bin/x86/linux/hello&amp;lt;/code&amp;gt;).&lt;br /&gt;
&lt;br /&gt;
=== Komponen yang dirakit ===&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Komponen !! Fungsi&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;System&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;clk_domain&amp;lt;/code&amp;gt; || Kerangka sistem dan clock (1 GHz)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;mem_mode = &amp;quot;timing&amp;quot;&amp;lt;/code&amp;gt; || Akses memori dimodelkan dengan latency&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;X86TimingSimpleCPU&amp;lt;/code&amp;gt; || Satu core x86 sederhana yang menunggu respons memori&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;SystemXBar&amp;lt;/code&amp;gt; (&amp;lt;code&amp;gt;membus&amp;lt;/code&amp;gt;) || Bus penghubung. &amp;lt;code&amp;gt;cpu_side_ports&amp;lt;/code&amp;gt; untuk pihak yang meminta data (CPU), &amp;lt;code&amp;gt;mem_side_ports&amp;lt;/code&amp;gt; untuk pihak yang menyediakan data (memori, device)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;interrupts[0]&amp;lt;/code&amp;gt; || Interrupt controller x86, wajib disambung manual&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;system_port&amp;lt;/code&amp;gt; || Jalur khusus gem5 (misalnya untuk memuat binary ke memori sebelum simulasi)&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;code&amp;gt;MemCtrl&amp;lt;/code&amp;gt; + &amp;lt;code&amp;gt;DDR3_1600_8x8&amp;lt;/code&amp;gt; || Memory controller dan DRAM&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Tips: Makefile ==&lt;br /&gt;
&lt;br /&gt;
Agar tidak mengetik path panjang berulang kali:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;make&amp;quot;&amp;gt;&lt;br /&gt;
GEM5   ?= /home/haidhi/gem5/build/ALL/gem5.opt&lt;br /&gt;
SCRIPT ?= simple.py&lt;br /&gt;
OUTDIR ?= m5out&lt;br /&gt;
FLAGS  ?= Exec&lt;br /&gt;
&lt;br /&gt;
.PHONY: run stats debug clean&lt;br /&gt;
&lt;br /&gt;
run:&lt;br /&gt;
	$(GEM5) --outdir=$(OUTDIR) $(SCRIPT)&lt;br /&gt;
&lt;br /&gt;
stats:&lt;br /&gt;
	@grep -E &amp;quot;simSeconds|simInsts|numCycles|hostSeconds&amp;quot; $(OUTDIR)/stats.txt&lt;br /&gt;
&lt;br /&gt;
debug:&lt;br /&gt;
	$(GEM5) --debug-flags=$(FLAGS) --debug-file=debug.out --outdir=$(OUTDIR) $(SCRIPT)&lt;br /&gt;
&lt;br /&gt;
clean:&lt;br /&gt;
	rm -rf $(OUTDIR)&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Baris perintah di Makefile harus diawali karakter &amp;#039;&amp;#039;&amp;#039;tab&amp;#039;&amp;#039;&amp;#039;, bukan spasi.&lt;br /&gt;
&lt;br /&gt;
== Masalah umum ==&lt;br /&gt;
&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;&amp;lt;code&amp;gt;No module named &amp;#039;_m5&amp;#039;&amp;lt;/code&amp;gt;&amp;#039;&amp;#039;&amp;#039;: script gem5 tidak bisa dijalankan dengan &amp;lt;code&amp;gt;python3 script.py&amp;lt;/code&amp;gt;. Modul &amp;lt;code&amp;gt;_m5&amp;lt;/code&amp;gt; ditanam di dalam binary, jadi script harus dijalankan lewat &amp;lt;code&amp;gt;gem5.opt&amp;lt;/code&amp;gt;.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Garis bawah merah di VSCode untuk &amp;lt;code&amp;gt;System&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;SrcClockDomain&amp;lt;/code&amp;gt;, dll&amp;#039;&amp;#039;&amp;#039;: modul &amp;lt;code&amp;gt;m5.objects&amp;lt;/code&amp;gt; dibangkitkan saat build dan source-nya di-embed ke binary, sehingga IDE tidak menemukannya. Script tetap berjalan normal. Garis bawahnya bisa dimatikan lewat setting &amp;lt;code&amp;gt;python.analysis.diagnosticSeverityOverrides&amp;lt;/code&amp;gt; atau komentar &amp;lt;code&amp;gt;# pyright: reportUndefinedVariable=false&amp;lt;/code&amp;gt; di baris atas script.&lt;br /&gt;
* &amp;#039;&amp;#039;&amp;#039;Build gagal karena kehabisan memori&amp;#039;&amp;#039;&amp;#039;: kecilkan nilai &amp;lt;code&amp;gt;-j&amp;lt;/code&amp;gt;.&lt;/div&gt;</summary>
		<author><name>SanzSkuy</name></author>
	</entry>
</feed>