前言
上次学习,在配置系统时我们使用了很多基本的SimObject类型
SimObject是包装好的C ++对象,可从Python配置脚本访问
gem5中几乎所有对象都继承自基本SimObject类型。 SimObjects将主接口导出到gem5中的所有对象。 SimObject是包装的C ++对象,可从Python配置脚本访问,也可使用python文件配置参数(参数类型可以是整数、浮点数,也可以是其他SimObjects)
gem5/src/learning_gem5/part2中有学习实例
本次我们将学习一个简单的SimObject是如何创建的
(通过逐步理解gem5/src/learning_gem5/part2中的代码来学习)
创建过程
(次序不是特别确定)
- 写SimObject的c++头文件.h
#ifndef __LEARNING_GEM5_HELLO_OBJECT_HH__
#define __LEARNING_GEM5_HELLO_OBJECT_HH__
#include <string>
#include "learning_gem5/part2/goodbye_object.hh"
//构建系统会自动生成这两个文件把,在build/X86/params文件夹下和build/X86/sim文件夹下
#include "params/HelloObject.hh"
#include "sim/sim_object.hh"
namespace gem5
{
class HelloObject : public SimObject
{
private:
/**
* Example function to execute on an event trigger
*/
void processEvent();
/// An event that wraps the above function
EventFunctionWrapper event;
/// Pointer to the corresponding GoodbyeObject. Set via Python
GoodbyeObject* goodbye;
/// The name of this object in the Python config file
const std::string myName;
/// Latency between calling the event (in ticks)
const Tick latency;
/// Number of times left to fire the event before goodbye
int timesLeft;
public:
HelloObject(const HelloObjectParams &p);
/**
* Part of a SimObject's initilaization. Startup is called after all
* SimObjects have been constructed. It is called after the user calls
* * simulate() for the first time.
*/
void startup();
};
} // namespace gem5
#endif // __LEARNING_GEM5_HELLO_OBJECT_HH__
- 写.cc文件实现SimObject
#include "learning_gem5/part2/hello_object.hh"
#include "base/logging.hh"
#include "base/trace.hh"
//使用这个调试标志,这个文件也是在build/X86/debug文件夹下自动生成的
#include "debug/HelloExample.hh"
namespace gem5
{
HelloObject::HelloObject(const HelloObjectParams ¶ms) :
SimObject(params),
// This is a C++ lambda. When the event is triggered, it will call the
// processEvent() function. (this must be captured)
event([this]{ processEvent(); }, name() + ".event"),
goodbye(params.goodbye_object),
// Note: This is not needed as you can *always* reference this->name()
myName(params.name),
latency(params.time_to_wait),
timesLeft(params.number_of_fires)
{
DPRINTF(HelloExample, "Created the hello object\n");
panic_if(!goodbye, "HelloObject must have a non-null GoodbyeObject");
}
void
HelloObject::startup()
{
// Before simulation starts, we need to schedule the event
schedule(event, latency);
}
void
HelloObject::processEvent()
{
timesLeft--;
DPRINTF(HelloExample, "Hello world! Processing the event! %d left\n",
timesLeft);
if (timesLeft <= 0) {
DPRINTF(HelloExample, "Done firing!\n");
goodbye->sayGoodbye(myName);
} else {
schedule(event, curTick() + latency);
}
}
} // namespace gem5
- 创建一个python文件控制SimObject的参数
from m5.params import *
from m5.SimObject import SimObject
class HelloObject(SimObject):
type = 'HelloObject'
cxx_header = "learning_gem5/part2/hello_object.hh"
cxx_class = 'gem5::HelloObject'
time_to_wait = Param.Latency("Time before firing the event")
number_of_fires = Param.Int(1, "Number of times to fire the event before "
"goodbye")
goodbye_object = Param.GoodbyeObject("A goodbye object")
class GoodbyeObject(SimObject):
type = 'GoodbyeObject'
cxx_header = "learning_gem5/part2/goodbye_object.hh"
cxx_class = 'gem5::GoodbyeObject'
buffer_size = Param.MemorySize('1kB',
"Size of buffer to fill with goodbye")
write_bandwidth = Param.MemoryBandwidth('100MB/s', "Bandwidth to fill "
"the buffer")
- 在SConscript文件重新声明SimObject和.cc文件
Import('*')
SimObject('SimpleObject.py', sim_objects=['SimpleObject'])
SimObject('HelloObject.py', sim_objects=['HelloObject', 'GoodbyeObject'])
SimObject('SimpleMemobj.py', sim_objects=['SimpleMemobj'])
SimObject('SimpleCache.py', sim_objects=['SimpleCache'])
Source('simple_object.cc')
Source('hello_object.cc')
Source('goodbye_object.cc')
Source('simple_memobj.cc')
Source('simple_cache.cc')
#声明调试标志
DebugFlag('HelloExample', "For Learning gem5 Part 2. Simple example debug flag")
DebugFlag('SimpleMemobj', "For Learning gem5 Part 2.")
DebugFlag('SimpleCache', "For Learning gem5 Part 2.")
- 重新编译构建
scons build/X86/gem5.opt
其对应的配置脚本可在在configs/learning_gem5/part2/hello_goodbye.py
命令行输入运行测试:
build/X86/gem5.opt --debug-flags=HelloExample configs/learning_gem5/part2/hello_goodbye.py
--debug-flags
可以查看调试信息
参考: