GEM5 & NVMain

  • 该篇主要记录 gem5 的安装和简单使用过程
  • 计算机系统设计课程实验环境需要,部分论文的实验仿真环境为 gem5
  • 结合部分 Paper,简要介绍 gem5 主要模拟的场景和测试方法


What is gem5?

  • A modular platform for computer-system architecture research


  • The gem5 simulator is a modular platform for computer-system architecture research, encompassing system-level architecture as well as processor microarchitecture.

Install And Test

Install dependencies firstly

  • sudo apt-get install mercurial scons swig gcc m4 python python-dev libgoogle-perftools-dev g++ libprotobuf-dev
  • sudo apt-get install build-essential
  • Noticeapt get is for Ubuntu

Download the source code

Build the source code

  • cd {gem5-source-code-dir}
  • scons build/X86/gem5.opt

Simple Test

  • cd {gem5-source-code-dir}
  • Check and edit the hello.c:vim tests/test-progs/hello/src/hello.c
  • Run the hello.c:build/X86/gem5.opt configs/example/ -c tests/test-progs/hello/bin/x86/linux/hello
  • Check the result if contains "Hello world!"

Check the output

  • cd m5out/
  • ls
  • config.ini and config.json describe the configuration info of the simulator.
  • stats.txt describes the info of this test.
  • Notice:This is in SE - System-call Emulation mode (which is designed for running the test program separately). The other mode FS - Full System mode is designed for running the whole linux os.


  • Run<gem5 binary> [gem5 options] <simulation script> [script options]

SE mode

  • Edit the source code and save: vim Test.c
int main()
    printf("Hello Test!\n");
    return 0;
  • Build:gcc -o {Output-File-Name} {Source-File-Name} -static
  • Test:./{Output-File-Name}
  • gcc -o Test Test.c -static
  • ./Test
  • Notice:If gcc return error info : /usr/bin/ld: cannot find -lc, try to install sudo yum install glibc-static
Build and Run
  • Enter gem5 directory
  • Build the arch if never build :scons build/X86/gem5.opt
  • Run the code:build/X86/gem5.opt configs/example/ -c {Output-File-Name}
  • build/X86/gem5.opt configs/example/ -c /shunzi-test/Test
  • Check the result if contains "Hello Test!".
  • In order to pass command line arguments to a binary you can use --options="arg1 arg2 ..." to specify them as a script option in your simulation command.
  • SE mode : Static Compile & Single Thread

FS mode

  • Use x86 Arch as an example.
  • Download and install the full-system binary and disk image files.
  • For Alpha full system files:wget (We will use one file in this tar in next step.)
  • For x86 full system files:wget
  • Create fs-image directory and unzip the x86 system files to here:tar -xjf /shunzi-test/m5/system/x86-system.tar.bz2
  • Create new directory such as alpha to store the data unziped from Alpha full system files:tar -xjf /shunzi-test/m5/system/m5_system_2.0b3.tar.bz2
  • Copy the file linux-bigswap2.img in Alpha full system files to ../fs-image/disk directory: cp linux-bigswap2.img /shunzi-test/gem5-master/fs-image/
  • Change the environment varibles in file .bashrc.And add path export M5_PATH=$M5_PATH:/shunzi-test/gem5-master/fs-image/. Make the change work:source .bashrc.
  • In gem5 directory, run the py program:build/X86/gem5.opt configs/example/ And it may throw exceptions and errors.You can viee details in Notice part as first two tips.
  • Rerun with command given kernel and disk image as params:build/X86/gem5.opt configs/example/ --kernel=x86_64-vmlinux- --disk-image=linux-x86.img.And check the output if contains following msgs:
gem5 Simulator System.
gem5 is copyrighted software; use the --copyright option for details.

gem5 compiled Nov 11 2019 18:23:53
gem5 started Nov 14 2019 15:31:57
gem5 executing on ceph-node1.localdomain, pid 729112
command line: build/X86/gem5.opt configs/example/ --kernel=x86_64-vmlinux- --disk-image=linux-x86.img

Global frequency set at 1000000000000 ticks per second
warn: DRAM device capacity (8192 Mbytes) does not match the address range assigned (512 Mbytes)
info: kernel located at: /shunzi-test/gem5-master/fs-image/binaries/x86_64-vmlinux-
system.pc.com_1.device: Listening for connections on port 3456
      0: system.pc.south_bridge.cmos.rtc: Real-time clock set to Sun Jan  1 00:00:00 2012
0: system.remote_gdb: listening for remote gdb on port 7000
warn: Reading current count from inactive timer.
info: Entering event queue @ 0.  Starting simulation...
warn: Don't know what interrupt to clear for console.
warn: x86 cpuid: unknown family 0x8086
  • Mount the image file to /mnt:mount -o,loop,offset=32256 /shunzi-test/m5/system/linux-x86.img /mnt
  • Mount the example code:cp tests/test-progs/hello/bin/x86/linux/hello /mntor mount code wrote by yourself. cp Test /mnt
  • Umount the mnt:umount /mnt
  • Rerun the gem5 with FS mode:build/X86/gem5.opt configs/example/ --kernel=x86_64-vmlinux- --disk-image=linux-x86.img
  • In another session, run the /util/term/m5term:m5term 3456
  • Change path in :vim ./configs/common/ And adjust the code.
// Old Version
except KeyError:
    path = [ '/dist/m5/system', '/n/poolfs/z/dist/m5/system' ]

// New Version
except KeyError:
    path = [ '/dist/m5/system', '/shunzi-test/gem5-master/fs-image' ]
  • Change the x86 image name in configs/common/
// Old Version
elif buildEnv['TARGET_ISA'] == 'x86':
    return env.get('LINUX_IMAGE', disk('x86root.img'))

// New Version
elif buildEnv['TARGET_ISA'] == 'x86':
    return env.get('LINUX_IMAGE', disk('linux-x86.img'))
  • If you run the code written by yourself, please confirm the version of gcc between the image and the compiled version. Otherwise, you will have to face the problem of too old kenerl version.


Install And Build

Download Source

  • hg clone
  • or wget
  • or download to local system and upload (recommand)
  • or download from github


  • scons –build-type=fast may throw error messages.
  • If success, the file will be generated in the nvmain directory.



  • Use inner test program and test data to test:./ Config/PCM_ISSCC_2012_4GB.config Tests/Traces/hello_world.nvt 10000000

DataType Check

Define 4 types
  • write-unit 8 bytes:00000000,0000XXXX,00XX00XX,OtherType
  • Define four counter in nvmain.h.
    ncounter_t total00000000;
    ncounter_t total0000xxxx;
    ncounter_t total00xx00xx;
    ncounter_t totalOtherType;
  • Init the counter in constructor.
    total00000000 = 0;
    total0000xxxx = 0;
    total00xx00xx = 0;
    totalOtherType = 0;
  • Count the amount of different data type.
     NVMDataBlock recvData;
     recvData = request->data;
     int index = 0;
     uint8_t newByte;
     for (index = 0; index < 64; index+=8) {
         //uint8_t byteOne, byteTwo, byteThree, byteFour, byteFive, byteSix, byteEight;
         uint8_t byteData[8] = {1,1,1,1,1,1,1,1};
         for (int i = 0; i < 8; i++) {
             newByte = recvData.GetByte(index + i);
             if (!(newByte & 0xff)) {
                 byteData[i] = 0;
         if (!(byteData[0] | byteData[1] | byteData[2] | byteData[3] | byteData[4]
               | byteData[5] | byteData[6] | byteData[7])) {
         } else if (!(byteData[0] | byteData[1] | byteData[2] | byteData[3])) {
         } else if (!(byteData[0] | byteData[1] | byteData[4] | byteData[5])) {
         } else {
  • Print the result
void NVMain::RegisterStats( )


Gem5 & NVMain


  • Build Alpha Arch:scons EXTRAS=/shunzi-test/nvmain/gem5-nvmain-hybrid-simulator/nvmain-gem5/nvmain ./build/ALPHA/gem5.opt -j8


  • Run hello-world:./build/ALPHA/gem5.opt configs/example/ -c tests/test-progs/hello/bin/alpha/linux/hello --cpu-type=detailed --caches --l2cache --mem-type=NVMainMemory --nvmain-config=../nvmain/Config/PCM_ISSCC_2012_4GB.config


Download neccessary dependencies

  • wget
  • wget
  • wget
  • wget

Use Parsec2.1

  • Use util to generate shell:./ blackscholes 4
  • Rebuild the gem5:scons build/ALPHA/gem5.opt -j8
  • Run with blackscholes_4c_simmedium.rcS Script without nvmain:./build/ALPHA/gem5.opt ./configs/example/ -n 2 --script=/shunzi-test/nvmain/gem5-nvmain-hybrid-simulator/nvmain-gem5/gem5/TR-09-32-parsec-2.1-alpha-files/blackscholes_4c_simmedium.rcS -F 5000000000
  • Run with nvmain:./build/ALPHA/gem5.opt ./configs/example/ -n 2 --script=/shunzi-test/nvmain/gem5-nvmain-hybrid-simulator/nvmain-gem5/gem5/TR-09-32-parsec-2.1-alpha-files/blackscholes_4c_simsmall.rcS --mem-type=NVMainMemory --nvmain-config=../nvmain/Config/PCM_ISSCC_2012_4GB.config -F 500000000