GEM5 & NVMain

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

gem5

What is gem5?

  • A modular platform for computer-system architecture research

About

  • 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/se.py -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.

Use

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

SE mode

Prepare
  • Edit the source code and save: vim Test.c
#include<stdio.h>
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/se.py -c {Output-File-Name}
  • build/X86/gem5.opt configs/example/se.py -c /shunzi-test/Test
  • Check the result if contains "Hello Test!".
Notice
  • 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.
Prepare
  • Download and install the full-system binary and disk image files.
  • For Alpha full system files:wget http://www.m5sim.org/dist/current/m5_system_2.0b3.tar.bz2. (We will use one file in this tar in next step.)
  • For x86 full system files:wget http://www.m5sim.org/dist/current/x86/x86-system.tar.bz2
  • 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.
Run
  • In gem5 directory, run the py program:build/X86/gem5.opt configs/example/fs.py. 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/fs.py --kernel=x86_64-vmlinux-2.6.22.9 --disk-image=linux-x86.img.And check the output if contains following msgs:
gem5 Simulator System.  http://gem5.org
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/fs.py --kernel=x86_64-vmlinux-2.6.22.9 --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-2.6.22.9
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.
**** REAL SIMULATION ****
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/fs.py --kernel=x86_64-vmlinux-2.6.22.9 --disk-image=linux-x86.img
  • In another session, run the /util/term/m5term:m5term 127.0.0.1 3456
Notice
  • Change path in SysPaths.py :vim ./configs/common/SysPaths.py. 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 Benchmarks.py:vim configs/common/Benchmarks.py
// 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.

NVMain

Install And Build

Download Source

  • hg clone https://bitbucket.org/mrp5060/nvmain
  • or wget https://bitbucket.org/mrp5060/nvmain/get/9c0e87b164bc.zip
  • or download to local system and upload (recommand)
  • or download from github https://github.com/cyjseagull/gem5-nvmain-hybrid-simulator.

Build

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

Use

Test

  • Use inner test program and test data to test:./nvmain.fast 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
Implementation
  • 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.
{
     totalWriteRequests++;
     NVMDataBlock recvData;
     recvData = request->data;
     recvData.SetSize(64);
     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])) {
             total00000000++;
         } else if (!(byteData[0] | byteData[1] | byteData[2] | byteData[3])) {
             total0000xxxx++;
         } else if (!(byteData[0] | byteData[1] | byteData[4] | byteData[5])) {
             total00xx00xx++;
         } else {
             totalOtherType++;
         }
     }
}
  • Print the result
void NVMain::RegisterStats( )
{
    AddStat(totalReadRequests);
    AddStat(totalWriteRequests);
    AddStat(successfulPrefetches);
    AddStat(unsuccessfulPrefetches);

    AddStat(total00000000);
    AddStat(total0000xxxx);
    AddStat(total00xx00xx);
    AddStat(totalOtherType);
    AddStat(totalWriteBytes);
    AddStat(totalCompressWriteBytes);
}

Gem5 & NVMain

Build

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

Test

  • Run hello-world:./build/ALPHA/gem5.opt configs/example/se.py -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

Parsec2.1

Download neccessary dependencies

  • wget http://www.cs.utexas.edu/~parsec_m5/vmlinux_2.6.27-gcc_4.3.4
  • wget http://www.cs.utexas.edu/~parsec_m5/tsb_osfpal
  • wget http://www.cs.utexas.edu/~parsec_m5/linux-parsec-2-1-m5-with-test-inputs.img.bz2
  • wget http://www.cs.utexas.edu/~parsec_m5/linux-parsec-2-1-m5-with-test-inputs.img.bz2

Use Parsec2.1

  • Use util to generate shell:./writescripts.pl 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/fs.py -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/fs.py -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

References