C++ NS3记录时间精度
我正在使用NS3.20来查找从一个节点发送到FAT树拓扑中另一个节点的数据包的时间戳C++ NS3记录时间精度,c++,python-2.7,gcc,ns-3,C++,Python 2.7,Gcc,Ns 3,我正在使用NS3.20来查找从一个节点发送到FAT树拓扑中另一个节点的数据包的时间戳 NS_LOG= "*=level_info|prefix_func|prefix_time|prefix_node" ./waf --run scratch/fat-tree &> save-log.txt 将包含pkt 196相关信息的几行选定行灰显后。输出为 时间戳,Nodeid,Functioncalled,DevNo,pktid 示例行如下所示 1.74547s 34 CsmaChann
NS_LOG= "*=level_info|prefix_func|prefix_time|prefix_node" ./waf --run scratch/fat-tree &> save-log.txt
将包含pkt 196相关信息的几行选定行灰显后。输出为
时间戳,Nodeid,Functioncalled,DevNo,pktid
示例行如下所示
1.74547s 34 CsmaChannel:TransmitStart(): UID is 196)
1.74548s 34 CsmaChannel:TransmitEnd(): UID is 196)
1.74548s 23 Node:ReceiveFromDevice(): Node 23 ReceiveFromDevice: dev 2 (type=ns3::CsmaNetDevice) Packet UID 196
1.74548s 23 BridgeNetDevice:ReceiveFromDevice(): UID is 196
1.74548s 23 CsmaChannel:TransmitStart(): UID is 196)
1.74548s 34 CsmaChannel:PropagationCompleteEvent(): UID is 196)
.
.
.
为了我的研究,我需要时间戳以纳秒为单位。有没有办法进行配置?
注意:我在函数中尝试了cout和fout,但它只是打印时间而没有节点id,这对我来说是无用的
请帮助首先,我建议使用最新版本的ns-3,ns-3.30.1。一般来说,保持最新是个好主意。也就是说,我的解决方案是使用ns-3.30.1进行测试的,所以很可能它不适用于ns-3.20。如果不起作用,请参阅中的注释 我引用了第11.3条,该条为我指明了示例。基于该示例,这里是您所需输出的最小可复制示例
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
// set the time format and precision of LOG macros
// https://stackoverflow.com/questions/60148908
// this example is adapted from ./src/core/examples/sample-log-time-format.cc
#include "ns3/simulator.h"
#include "ns3/log.h"
#include "ns3/random-variable-stream.h"
using namespace ns3;
void
ReplacementTimePrinter (std::ostream &os)
{
os << Simulator::Now ().GetNanoSeconds () << " ns";
}
void
ReplaceTimePrinter (void)
{
std::cout << "Replacing time printer function after Simulator::Run ()" << std::endl;
LogSetTimePrinter (&ReplacementTimePrinter);
}
int
main (int argc, char *argv[])
{
LogComponentEnable ("RandomVariableStream", LOG_LEVEL_ALL);
LogComponentEnableAll (LOG_PREFIX_TIME);
Ptr<UniformRandomVariable> uniformRv = CreateObject<UniformRandomVariable> ();
Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);
// schedule some bogus events to demonstrate the new TimePrinter
Simulator::Schedule (NanoSeconds (1), &UniformRandomVariable::SetAntithetic, uniformRv, false);
Simulator::Schedule (NanoSeconds (123), &UniformRandomVariable::SetAntithetic, uniformRv, false);
Simulator::Schedule (NanoSeconds (123456), &UniformRandomVariable::SetAntithetic, uniformRv, false);
Simulator::Schedule (NanoSeconds (123456789), &UniformRandomVariable::SetAntithetic, uniformRv, false);
Simulator::Run ();
Simulator::Destroy ();
}
请注意,我们没有在模拟器代码中直接调用LogSetTimePrinter
,而是安排了一个事件来调用LogSetTimePrinter
Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);
我不确定为什么会出现这种情况,但进一步检查后,似乎
GetImpl()
调用了LogSetTimePrinter
GetImpl()
由Simulator::Run()
调用,这意味着Simulator::Run()
将在调用Simulator::Run()
之前“覆盖”对LogSetTimePrinter的任何调用。因此,我们需要创建一个事件,在创建SimulatorImpl
之后设置TimePrinter
您可以基于此链接为数据包添加时间戳
RandomVariableStream:RandomVariableStream(0x7fc0aee287d0)
RandomVariableStream:UniformRandomVariable(0x7fc0aee287d0)
RandomVariableStream:SetStream(0x7fc0aee287d0, -1)
RandomVariableStream:SetAntithetic(0x7fc0aee287d0, 0)
Replacing time printer function after Simulator::Run ()
1 ns RandomVariableStream:SetAntithetic(0x7fc0aee287d0, 0)
123 ns RandomVariableStream:SetAntithetic(0x7fc0aee287d0, 0)
123456 ns RandomVariableStream:SetAntithetic(0x7fc0aee287d0, 0)
123456789 ns RandomVariableStream:SetAntithetic(0x7fc0aee287d0, 0)
RandomVariableStream:~RandomVariableStream(0x7fc0aee287d0)
Simulator::Schedule (Seconds (0), &ReplaceTimePrinter);