使用TicToc学习OMNeT++——第 2 部分 - 运行仿真

系列文章目录

提示:本文是本系列教程的第3篇,前期准备工作请参考之前的2篇 :
使用TicToc学习OMNeT++——第 0 部分 - 简介
使用TicToc学习OMNeT++——第 1 部分 - 入门


提示:本人的第3篇文章,写得不好,请读者多多包涵


前言

提示:这里说明了本文要记录的大概内容:

本文基于原版英文教程的第2部分:Part 2 - Running the Simulation进行翻译和论述,并结合仿真手册,用户指南和AI问答以厘清基本概念。


提示:以下是本篇文章正文内容

一、原文翻译

原文经过本人的人工润色,主要对一些复杂嵌套的英文语句调整语序,修改一些术语翻译错误,然后是修正句内的修饰关系。此外,原文翻译中还补充了一些自己实操过程中的截图。

Part 2 - Running the Simulation 第 2 部分 - 运行仿真

2.1 Launching the simulation program 2.1 启动仿真程序

Once you complete the above steps, you can launch the simulation by selecting %omnetpp.ini (in either the editor area or the Project Explorer), and pressing the Run button.
完成上述步骤后,您可以通过选择“%omnetpp.ini”(在编辑器区域或“项目资源管理器”中)并按“运行”按钮来启动仿真。
启动仿真
The IDE will build your project automatically. If there are compilation errors, you need to rectify those until you get an error-free compilation and linking. You can manually trigger a build by hitting choosing Project -> Build All from the menu, or hitting Ctrl+B.
IDE 将自动构建您的项目。如果存在编译错误,则需要纠正这些错误,直到获得无错误的编译和链接。您可以通过从菜单中选择“项目”->“全部构建”或按 Ctrl+B 来手动触发生成。
全部构建

Note 注意
If you want to build the simulation executable on the command-line, create a Makefile using the opp_makemake command, then enter make to build the project. It will produce an executable that can be run by entering ./tictoc.
如果要在命令行上构建模拟可执行文件,请使用命令 opp_makemake 创建一个 Makefile,然后输入 make 来构建项目。它将生成一个可执行文件,可以通过输入 ./tictoc 来运行。

2.2 Running the simulation 2.2 运行模拟

After successfully building and launching your simulation, you should see a new GUI window appear, similar to the one in the screenshot below. The window belongs to Qtenv, the main OMNeT++ simulation runtime GUI. You should also see the network containing tic and toc displayed graphically in the main area.
成功构建并启动模拟后,您应该会看到一个新的 GUI 窗口,类似于以下屏幕截图中的窗口。该窗口属于 Qtenv,即 OMNeT++ 仿真运行时 主GUI。您还应该看到包含 tic 和 toc 的网络以图形方式显示在主区域中。

Press the Run button on the toolbar to start the simulation. What you should see is that tic and toc are exchanging messages with each other.
按工具栏上的“运行”按钮开始模拟。您应该看到的是 tic 和 toc 正在和对方交换消息。
tic 和 toc 正在和对方交换消息
The main window toolbar displays the current simulation time. This is virtual time, it has nothing to do with the actual (or wall-clock) time that the program takes to execute. Actually, how many seconds you can simulate in one real-world second depends highly on the speed of your hardware and even more on the nature and complexity of the simulation model itself.
主窗口工具栏显示当前仿真时间。这是虚拟时间,它与程序执行所需的实际(或挂钟)时间无关。实际上,在实际的一秒钟内可以模拟多少秒,很大程度上取决于硬件的速度,甚至更多地取决于仿真模型本身的性质和复杂性。

Note that it takes zero simulation time for a node to process the message. The only thing that makes the simulation time pass in this model is the propagation delay on the connections.
请注意,节点处理消息所需的模拟时间为零。在此模型中,唯一使仿真时间流逝的是连接上的传播延迟。

You can play with slowing down the animation or making it faster with the slider at the top of the graphics window. You can stop the simulation by hitting F8 (equivalent to the STOP button on the toolbar), single-step through it (F4), run it with (F5) or without (F6) animation. F7 (express mode) completely turns off tracing features for maximum speed. Note the event/sec and simsec/sec gauges on the status bar of the main window (only visible when the simulation is running in fast or express mode).
您可以使用图形窗口顶部的滑块减慢动画速度或使其更快。您可以通过按 F8(相当于工具栏上的 STOP 按钮)、单步完成模拟 (F4)、使用 (F5) 或不使用 (F6) 动画运行模拟来停止模拟。F7(特快模式)完全关闭跟踪功能以获得最大速度。请注意主窗口状态栏上的 event/sec 和 simsec/sec 仪表(仅在仿真以快速或特快模式运行时可见)。
主窗口工具栏

Exercise 锻炼
Explore the GUI by running the simulation several times. Try Run, Run Until, Rebuild Network, and other functions.
通过多次运行模拟来探索 GUI。尝试运行、运行到、重建网络等功能。

You can exit the simulation program by clicking its Close icon or choosing File -> Exit.
您可以通过单击其“关闭”图标或选择“文件”->“退出”来退出模拟程序。

“文件”->“退出”

2.3 Debugging 2.3 调试

The simulation is just a C++ program, and as such, it often needs to be debugged while it is being developed. In this section we’ll look at the basics of debugging to help you acquire this vital task.
仿真只是一个 C++ 程序,因此,在开发过程中经常需要对其进行调试。在本节中,我们将介绍调试的基础知识,以帮助您完成这项重要任务。

The simulation can be started in debug mode by clicking the Debug button on the IDE’s main toolbar.
通过单击 IDE 主工具栏上的 “Debug”(调试)按钮,可以在调试模式下启动仿真。
单击“调试”按钮
This will cause the simulation program to be launched under a debugger (usually gdb). The IDE will also switch into “Debug perspective”, i.e. rearrange its various panes and views to a layout which is better suited to debugging. You can end the debugging session with the Terminate button (a red square) on the toolbar.
这将导致仿真程序在调试器(通常是 gdb) 下启动。IDE 还将切换到“调试视角”,即将其各种窗格和视图重新排列为更适合调试的布局。您可以使用工具栏上的“终止”按钮(红色方块)结束调试会话。

提示:从仿真视角切换到调试视角会弹出一个 确认视角 switch/切换 的消息框,选择"switch/切换"。
确认视角切换

提示:调试视角和仿真视角可以通过右上角的按钮手动切换。

Runtime errors 运行时错误

Debugging is most often needed to track down runtime errors. Let’s try it! First, deliberately introduce an error into the program. In txc1.cc, duplicate the send() line inside handleMessage(), so that the code looks like this:
最常需要调试来跟踪运行时错误。让我们试试吧!首先,故意在程序中引入错误。在 txc1.cc 中,复制 handleMessage() 里面的 send() 行,使代码如下所示:

void Txc1::handleMessage(cMessage *msg)
{
    //...
    send(msg, "out"); // send out the message 发出消息
    send(msg, "out"); // THIS SHOULD CAUSE AN ERROR 这应该会导致一个错误
}

When you launch the simulation in normal mode (Run button) and try to run it, you’ll get an error message like this:
当您在正常模式(“运行”按钮)下启动模拟并尝试运行它时,您将收到如下错误消息:
提示:在6.01中弹出的消息框标题变成了"Runtime Error"
错误消息
Now, run the simulation in Debug mode. Due to a debug-on-errors option being enabled by default, the simulation program will stop in the debugger. You can locate the error by examining the stack trace (the list of nested function calls) in the Debug view:
现在,在调试模式下运行仿真。由于默认情况下启用了一个 debug-on-errors 选项,因此仿真程序将在调试器中停止。您可以通过检查 Debug 视图中的 堆栈跟踪(嵌套函数调用列表) 来定位错误:
Debug 视图中的堆栈跟踪
提示:在6.01中,改成了notifyenvir()方法来激活调试器
You can see that it was OMNeT++'s breakIntoDebuggerIfRequested() method that activated the debugger. From then on, you need to search for a function that looks familiar, i.e. for one that is part of the model. In our case, that is the “Txc1::handleMessage() at txc1.cc:54” line. Selecting that line will show you the corresponding source code in the editor area, and lets you examine the values of variables in the Variables view. This information will help you determine the cause of the error and fix it.
您可以看到是 OMNeT++ breakIntoDebuggerIfRequested() 的方法激活了调试器。从那时起,您需要搜索一个看起来熟悉的函数,即作为模型一部分的函数。在我们的例子中,这是“Txc1::handleMessage() at txc1.cc:54”行。选择该行将在编辑器区域中显示相应的源代码,并允许您检查“变量”视图中的变量值。此信息将帮助您确定错误的原因并对其进行修复。

Crashes 崩溃

Tracking down crashes i.e. segfaults is similar, let’s try that as well. Undo the previous source code edit (remove the duplicate send() line), and introduce another error. Let’s pretend we forgot to create the message before sending it, and change the following lines in initialize()
跟踪崩溃(即段错误) 是类似的,让我们也尝试一下。撤消之前的源代码编辑(删除重复的 send() 行),并引入另一个错误。让我们假装我们在发送消息之前忘记创建消息,并更改 initialize() 中的以下各行

        cMessage *msg = new cMessage("tictocMsg");
        send(msg, "out");

to simply 简单地说

        cMessage *msg; // no initialization!
        send(msg, "out");

When you run the simulation, it will crash. (You will get an error message similar to “Simulation terminated with exit code: 139”). If you launch the simulation again, this time in Debug mode, the crash will bring you into the debugger. Once there, you’ll be able to locate the error in the Debug view and examine variables, which will help you identify and fix the bug.
当您运行模拟时,它将崩溃。(您将收到类似于“模拟终止并(返回)退出代码:139”的错误消息)。如果再次启动仿真,这次是在调试模式下,崩溃将带您进入调试器。到达那里后,您将能够在“调试”视图中定位错误并检查变量,这将帮助您识别和修复错误。
提示:在6.0.1中,好像直接就进入了调试视角
问题发生

Breakpoints 断点

You can also manually place breakpoints into the code. Breakpoints will stop execution, and let you examine variables, execute the code line-by-line, or resume execution (until the next breakpoint).
还可以手动将断点放入代码中。断点将停止执行,并允许你检查变量、逐行执行代码或恢复执行(直到下一个断点)。

A breakpoint can be placed at a specific line in the source code by double-clicking on the left gutter in the editor, or choosing Toggle Breakpoint from the context menu. The list of active (and inactive) breakpoints can be examined in the Breakpoints view.
断点可以放置在源代码中的特定行处,方法是双击编辑器中的左侧装订线,或从上下文菜单中选择“切换断点”。可以在“断点”视图中检查活动(和非活动)断点列表
提示:断点在右侧变量视图的右边,表达式视图的左边,也就是两者中间

Exercise 锻炼
Experiment with breakpoints! Place a breakpoint at the beginning of the handleMessage() method function, and run the simulation. Use appropriate buttons on the toolbar to single-step, continue execution until next time the breakpoint is hit, and so on.
尝试断点!在 handleMessage() 方法函数的开头放置一个断点,然后运行模拟。使用工具栏上的相应按钮来单步执行,继续执行,直到下次命中断点,依此类推。

经验: 调试视角的单步执行(step into?)似乎不行,会找不到文件。Qtenv窗口中的单事件执行也不行

Debug next event 调试下一个事件

If you did the previous exercise, you must have noticed that the breakpoint was triggered at each and every event in the Txc1 simple module. In real life it often occurs that an error only surfaces at, say, the 357th event in that module, so ideally that’s when you’d want to start debugging. It is not very convenient to have to hit Resume 356 times just to get to the place of the error. A possible solution is to add a condition or an ignore-count to the breakpoint (see Breakpoint Properties in its context menu). However, there is a potentially more convenient solution.
如果您执行了前面的练习,您一定已经注意到,断点是在 Txc1 简单模块中的每个事件中触发的。在现实生活中,错误通常只出现在该模块的 357 th 事件中,因此理想情况下,这是您想要开始调试的时候。必须点击 “Resume/恢复” 356次才能到达错误位置,这并不是很方便。一种可能的解决方案是向断点添加条件或忽略计数(请参阅其上下文菜单中的断点属性)。但是,有一个可能更方便的解决方案。

In Qtenv, use Run Until to get to the event to be debugged. Then, choose Simulation -> Debug Next Event from the menu. This will trigger a breakpoint in the debugger at the beginning of handleMessage() of the next event, and you can start debugging that event.
在 Qtenv 中,使用“运行直到”获取要调试的事件。然后,从菜单中选择模拟 -> 调试下一个事件。这将在下一个事件的 handleMessage() 开始时触发调试器中的断点,你可以开始调试该事件。
仿真->调试下一个事件
个人经验:不知道为什么,不论我怎么操作,最后都会死机😭

2.4 The Debug/Run dialog 2.4 调试/运行对话框

Let us return to launching simulations once more.
让我们再次回到启动模拟。

When you launch the simulation program with the Run or Debug buttons on the IDE toolbar, settings associated with the launch are saved in a launch configuration. Launch configurations can be viewed in the Run/Debug Configurations dialog which can be opened e.g. by clicking the little down arrow next to the Run (Debug) toolbar button to open a menu, and choosing Run (Debug) Configurations… in it. In the same menu, you can also click the name of a launch configuration (e.g. tictoc) while holding down the Ctrl key to open the dialog with the corresponding configuration.
使用 IDE 工具栏上的 “Run”(运行) 或“调试”(Debug) 按钮启动仿真程序时,与启动关联的设置将保存在启动配置中。可以在“运行/调试配置”对话框中查看启动配置,该对话框可以打开,例如,单击“运行(调试)”工具栏按钮旁边的小向下箭头以打开菜单,然后选择里面的“运行(调试)配置…”。在同一菜单中,您还可以单击启动配置的名称(例如 tictoc),同时按住 Ctrl 键打开具有相应配置的对话框。

The dialog allows you activate various settings for the launch.
该对话框允许您激活启动的各种设置。
运行配置对话框

2.5 Visualizing on a Sequence Chart 2.5 在序列图上可视化

The OMNeT++ simulation kernel can record the message exchanges during the simulation into an event log file. To enable recording the event log, check the Record eventlog checkbox in the launch configuration dialog. Alternatively, you can specify record-eventlog = true in omnetpp.ini, or even, use the Record button in the Qtenv graphical runtime environment after launching,
OMNeT++ 仿真内核可以将仿真期间的消息交换 记录到事件日志文件中。要启用记录事件日志,请选中启动(运行?)配置对话框中的 记录事件日志 复选框。或者,您可以在omnetpp.ini中指定 record-eventlog = true,甚至可以在启动后使用 Qtenv 图形运行时环境中 的 Record 按钮,
提示:在6.0.1的运行配置对话框中,在选好左侧的配置tictoc-Tictoc1后,要将Main选项卡的右侧的滑块一直向下拉,才能找到对应的output区域的 Record eventlog的复选框。
Record eventlog复选框

The log file can be analyzed later with the Sequence Chart tool in the IDE. The results directory in the project folder contains the .elog file.
稍后可以使用 IDE 中的 “Sequence Chart”(序列图)工具分析日志文件。项目文件夹中的 results 目录包含该 .elog 文件。
事件日志文件
Double-clicking on it in the OMNeT++ IDE opens the Sequence Chart tool, and the event log tab at the bottom of the window.
在 OMNeT++ IDE 中双击它,将打开“序列图”工具,并在窗口底部显示“事件日志”选项卡。

Note 注意
The resulting log file can be quite large, so enable this feature only if you really need it.
生成的日志文件可能非常大,因此仅当您真正需要时才启用此功能。

提示:在6.0.1中,序列图的右上角的positon应该指的是起始点位置,即事件0后55ms。range含义见补充内容😢
tictoc1的序列图

The following figure has been created with the Sequence Chart tool, and shows how the message is routed between the different nodes in the network. In this instance the chart is very simple, but when you have a complex model, sequence charts can be very valuable in debugging, exploring or documenting the model’s behaviour.
下图是使用序列图工具创建的,显示了消息如何在网络中的不同节点之间路由。在这种情况下,图表非常简单,但是当您有一个复杂的模型时,序列图在调试、探索或记录模型的行为方面可能非常有价值。
序列图
Sources: tictoc1.ned, txc1.cc, omnetpp.ini
源(代码):tictoc1.ned, txc1.cc, omnetpp.ini

二、补充内容

对于教程中涉及到的概念,本人查阅OMNeT++文件中的仿真手册,用户指南得到的相关内容。以及询问AI得到的回答。

1. 序列图中右上角的range的意思

根据用户指南的8.3.5的第1段

个人认为range指的是当前窗口可见的事件数量和模拟时间范围。

2. 为什么不论我怎么操作omnet++的调试器,Qtenv界面最后都会卡死?

AI的回答:

当你在使用OMNeT++的调试器时,遇到QtEnv界面卡死的问题,可能有几个不同的原因。以下是一些可能的原因以及相应的解决方案:

  1. 内存不足

    • 如果你的模型很大或者模拟过程中产生了大量的数据,可能会耗尽计算机的内存,导致QtEnv界面无响应。
    • 解决方案:尝试关闭其他不必要的应用程序,释放内存。如果问题依旧,考虑升级你的计算机硬件,特别是增加内存。
  2. 模型或代码问题

    • 你的OMNeT++模型或代码可能存在bug,导致在模拟或调试过程中产生异常,从而卡死QtEnv。
    • 解决方案:检查你的代码和模型,确保它们没有逻辑错误或内存泄漏。使用OMNeT++的日志功能查看是否有任何错误或警告信息。
  3. QtEnv或调试器插件问题

    • QtEnv或与之相关的调试器插件可能存在兼容性问题或bug。
    • 解决方案:尝试更新OMNeT++和QtEnv到最新版本。如果问题依旧,可以在OMNeT++的社区论坛或GitHub仓库中搜索是否有其他人遇到了类似的问题,并查看是否有可用的解决方案。
  4. IDE或操作系统问题

    • 你使用的IDE(如Eclipse、Visual Studio等)或操作系统可能存在与OMNeT++或QtEnv不兼容的问题。
    • 解决方案:尝试更换IDE或更新操作系统。此外,确保你的开发环境满足OMNeT++的官方要求。
  5. 不正确的调试配置

    • 如果你的调试配置不正确,可能会导致QtEnv在调试过程中卡死。
    • 解决方案:检查你的调试配置,确保它们正确无误。特别是要注意调试器的路径、端口和参数设置。
  6. 资源竞争或死锁

    • 如果你的代码中存在多线程或并发操作,并且没有正确处理资源竞争或死锁问题,可能会导致QtEnv卡死。
    • 解决方案:使用调试工具检查多线程或并发操作,确保它们正确无误。特别是要注意线程同步和互斥锁的使用。

如果以上方法都不能解决问题,建议你在OMNeT++的社区论坛或相关技术支持渠道寻求帮助。在提问时,尽量提供详细的问题描述、代码示例和错误日志,以便他人更好地帮助你解决问题。
评述:感觉AI的回答也不行😭


总结

提示:这里对文章进行总结:

感觉这篇写得更顺利些,可能是因为不涉及太多理解代码的问题。终于算是完成了运行仿真篇,对于调试的卡死问题只能留待以后解决,也欢迎精通调试的同学能够热心指导一下,非常感谢🙏!
最后,这一组文章也许并没有什么大的用处,但也是一种自我学习的过程,也是对自己的学习心得的一种记录,最后如果能对读者有点用处的话,那就再好不过了😂。

  • 19
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值