TRACING
The *ns-3 *tracing system is built on the concepts of independent tracing sources and tracing sinks, along with a uniformmechanism for connecting sources to sinks.
Trace sources are entities that can signal events that happen in a simulation and provide access to interesting underlyingdata. For example, a trace source could indicate when a packet is received by a net device and provide access to thepacket contents for interested trace sinks. A trace source might also indicate when an interesting state change happensin a model. For example, the congestion window of a TCP model is a prime candidate for a trace source. Every timethe congestion window changes connected trace sinks are notified with the old and new value.
Trace sources are not useful by themselves; they must be connected to other pieces of code that actually do somethinguseful with the information provided by the source. The entities that consume trace information are called trace sinks.Trace sources are generators of data and trace sinks are consumers. This explicit division allows for large numbers oftrace sources to be scattered around the system in places which model authors believe might be useful. Inserting tracesources introduces a very small execution overhead.
There can be zero or more consumers of trace events generated by a trace source. One can think of a trace source as akind of point-to-multipoint information link. Your code looking for trace events from a particular piece of core codecould happily coexist with other code doing something entirely different from the same information.
Unless a user connects a trace sink to one of these sources, nothing is output. By using the tracing system, both youand other people hooked to the same trace source are getting exactly what they want and only what they want out ofthe system. Neither of you are impacting any other user by changing what information is output by the system. If youhappen to add a trace source, your work as a good open-source citizen may allow other users to provide new utilitiesthat are perhaps very useful overall, without making any changes to the *ns-3 *core.
Callbacks
允許某處代碼訪問C++中某個函數(shù)和方法無需具體模塊之間的依賴。
根本上是你把訪問函數(shù)的地址視為變量漾根,函數(shù)指針搂抒。
每個trace source維護一個internal list of Callbacks戴涝。
class MyObject : public Object
{
public:
/**
* Register this type.
* \return The TypeId.
*/
static TypeId GetTypeId (void)
{
static TypeId tid = TypeId ("MyObject")
.SetParent<Object> ()
.SetGroupName ("Tutorial")
.AddConstructor<MyObject> ()
.AddTraceSource ("MyInteger",
"An integer value to trace.",
MakeTraceSourceAccessor (&MyObject::m_myInt),
"ns3::TracedValueCallback::Int32")
;
return tid;
}
MyObject () {}
TracedValue<int32_t> m_myInt;
};
void
IntTrace (int32_t oldValue, int32_t newValue)
{
std::cout << "Traced " << oldValue << " to " << newValue << std::endl;
}
int
main (int argc, char *argv[])
{
Ptr<MyObject> myObject = CreateObject<MyObject> ();
myObject->TraceConnectWithoutContext ("MyInteger", MakeCallback (&IntTrace));
myObject->m_myInt = 1234;
}
.AddTraceSource第一個屬性是trace source的名字声旺,在config system中可見钻蹬;
第二個屬性是幫助信息珠闰;
第三個屬性,使用要追蹤的TracedValue生成函數(shù)特征調用
The TracedValue<> declaration provides the infrastructure that drives the callback process. Any time the underlying value is changed the TracedValue mechanism will provide both the old and the new value of that variable, in thiscase an int32_t value. The trace sink function traceSink for this TracedValue will need the signature
Connect with Config
TraceConnectWithoutContext這種函數(shù)其實用的時候很少,大多數(shù)用Config來連接,底層調用合適的TraceConnect染乌。
Config系統(tǒng)用Config path來選擇trace source.
void ns3::Config::Connect
(
std::string path,
const CallbackBase & cb
)
Parameters
[in]path
A path to match trace sources.
[in]cb
The callback to connect to the matching trace sources.
This function will attempt to find all trace sources which match the input path and will then connect the input callback to them in such a way that the callback will receive an extra context string upon trace event notification.
通過TracedCallback類中重載的操作符()calls any registered trace sinks
拿CourseChange的例子更加深入底層的講了ns3的底層代碼如何構建TraceSource,以及Trace工作原理
Finding Sources
Okay, I know that there must be trace sources in the simulation core, but how do I find out what trace sources areavailable to me?
Config Paths
Okay, I found a trace source, how do I figure out the Config path to use when I connect to it?
/NodeList/[i]/$ns3::MobilityModel"
還可以使用命令搜索文件
find . -name '*.cc' | xargs grep CourseChange | grep Connect
找到實例 自己模仿著寫
Callback Signatures
Okay, I found a trace source and the Config path, how do I figure out what the return type and formal arguments ofmy callback function need to be?
一種方法去用上面的命令找到實例
一種方法是在API文檔里找
實在找不到的就找TracedCallback定義的變量
總結 自己寫一個trace source需要注冊.AddTraceSource,申明一個想要關注的TracedValue變量山孔,然后寫一個trace sink函數(shù)用來接收source發(fā)出的信息,在程序運行前把sink綁定到source上荷憋,這樣TracedValue在變化時執(zhí)行callback返回新舊兩個TracedValue值台颠。
To summarize, a trace source is, in essence, a variable that holds a list of callbacks. A trace sink is a function usedas the target of a callback. The Attribute and object type information systems are used to provide a way to connecttrace sources to trace sinks. The act of “hitting” a trace source is executing an operator on the trace source which firescallbacks. This results in the trace sink callbacks registering interest in the source being called with the parametersprovided by the source.
看了好幾天ns3的回調實現(xiàn),最后在ns3group里找到了答案勒庄,
https://groups.google.com/forum/#!topic/ns-3-users/xty2I8JV3_c
3.24 ps:想要看C++如何通過模板類實現(xiàn)回調 還要買書學習一下