author | Tom Henderson <tomh@tomh.org> |
Sat, 16 Jan 2016 08:14:40 -0800 | |
changeset 11683 | 9142266fbb25 |
parent 11248 | 88468adb47b5 |
permissions | -rw-r--r-- |
6754 | 1 |
.. include:: replace.txt |
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
2 |
.. highlight:: cpp |
6754 | 3 |
|
4 |
||
5 |
Tweaking |
|
6 |
-------- |
|
7 |
||
11088
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
8 |
.. _UsingLogging: |
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
9 |
|
6754 | 10 |
Using the Logging Module |
11 |
************************ |
|
12 |
||
13 |
We have already taken a brief look at the |ns3| logging module while |
|
14 |
going over the ``first.cc`` script. We will now take a closer look and |
|
15 |
see what kind of use-cases the logging subsystem was designed to cover. |
|
16 |
||
17 |
Logging Overview |
|
18 |
++++++++++++++++ |
|
19 |
Many large systems support some kind of message logging facility, and |
|
20 |
|ns3| is not an exception. In some cases, only error messages are |
|
21 |
logged to the "operator console" (which is typically ``stderr`` in Unix- |
|
22 |
based systems). In other systems, warning messages may be output as well as |
|
23 |
more detailed informational messages. In some cases, logging facilities are |
|
24 |
used to output debug messages which can quickly turn the output into a blur. |
|
25 |
||
26 |
|ns3| takes the view that all of these verbosity levels are useful |
|
27 |
and we provide a selectable, multi-level approach to message logging. Logging |
|
28 |
can be disabled completely, enabled on a component-by-component basis, or |
|
29 |
enabled globally; and it provides selectable verbosity levels. The |
|
30 |
|ns3| log module provides a straightforward, relatively easy to use |
|
31 |
way to get useful information out of your simulation. |
|
32 |
||
33 |
You should understand that we do provide a general purpose mechanism --- |
|
34 |
tracing --- to get data out of your models which should be preferred for |
|
35 |
simulation output (see the tutorial section Using the Tracing System for |
|
36 |
more details on our tracing system). Logging should be preferred for |
|
37 |
debugging information, warnings, error messages, or any time you want to |
|
38 |
easily get a quick message out of your scripts or models. |
|
39 |
||
40 |
There are currently seven levels of log messages of increasing verbosity |
|
41 |
defined in the system. |
|
42 |
||
9104
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
43 |
* LOG_ERROR --- Log error messages (associated macro: NS_LOG_ERROR); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
44 |
* LOG_WARN --- Log warning messages (associated macro: NS_LOG_WARN); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
45 |
* LOG_DEBUG --- Log relatively rare, ad-hoc debugging messages |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
46 |
(associated macro: NS_LOG_DEBUG); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
47 |
* LOG_INFO --- Log informational messages about program progress |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
48 |
(associated macro: NS_LOG_INFO); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
49 |
* LOG_FUNCTION --- Log a message describing each function called |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
50 |
(two associated macros: NS_LOG_FUNCTION, used for member functions, |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
51 |
and NS_LOG_FUNCTION_NOARGS, used for static functions); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
52 |
* LOG_LOGIC -- Log messages describing logical flow within a function |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
53 |
(associated macro: NS_LOG_LOGIC); |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
54 |
* LOG_ALL --- Log everything mentioned above (no associated macro). |
6754 | 55 |
|
9104
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
56 |
For each LOG_TYPE there is also LOG_LEVEL_TYPE that, if used, enables |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
57 |
logging of all the levels above it in addition to it's level. (As a |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
58 |
consequence of this, LOG_ERROR and LOG_LEVEL_ERROR and also LOG_ALL |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
59 |
and LOG_LEVEL_ALL are functionally equivalent.) For example, |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
60 |
enabling LOG_INFO will only enable messages provided by NS_LOG_INFO macro, |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
61 |
while enabling LOG_LEVEL_INFO will also enable messages provided by |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
62 |
NS_LOG_DEBUG, NS_LOG_WARN and NS_LOG_ERROR macros. |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
63 |
|
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
64 |
We also provide an unconditional logging macro that is always displayed, |
6754 | 65 |
irrespective of logging levels or component selection. |
66 |
||
9104
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
67 |
* NS_LOG_UNCOND -- Log the associated message unconditionally (no associated |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
68 |
log level). |
6754 | 69 |
|
70 |
Each level can be requested singly or cumulatively; and logging can be set |
|
71 |
up using a shell environment variable (NS_LOG) or by logging system function |
|
72 |
call. As was seen earlier in the tutorial, the logging system has Doxygen |
|
73 |
documentation and now would be a good time to peruse the Logging Module |
|
74 |
documentation if you have not done so. |
|
75 |
||
76 |
Now that you have read the documentation in great detail, let's use some of |
|
77 |
that knowledge to get some interesting information out of the |
|
78 |
``scratch/myfirst.cc`` example script you have already built. |
|
79 |
||
80 |
Enabling Logging |
|
81 |
++++++++++++++++ |
|
82 |
Let's use the NS_LOG environment variable to turn on some more logging, but |
|
83 |
first, just to get our bearings, go ahead and run the last script just as you |
|
84 |
did previously, |
|
85 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
86 |
.. sourcecode:: bash |
6754 | 87 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
88 |
$ ./waf --run scratch/myfirst |
6754 | 89 |
|
90 |
You should see the now familiar output of the first |ns3| example |
|
91 |
program |
|
92 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
93 |
.. sourcecode:: bash |
6754 | 94 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
95 |
$ Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
6754 | 96 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
97 |
'build' finished successfully (0.413s) |
|
98 |
Sent 1024 bytes to 10.1.1.2 |
|
99 |
Received 1024 bytes from 10.1.1.1 |
|
100 |
Received 1024 bytes from 10.1.1.2 |
|
101 |
||
102 |
It turns out that the "Sent" and "Received" messages you see above are |
|
103 |
actually logging messages from the ``UdpEchoClientApplication`` and |
|
104 |
``UdpEchoServerApplication``. We can ask the client application, for |
|
105 |
example, to print more information by setting its logging level via the |
|
106 |
NS_LOG environment variable. |
|
107 |
||
108 |
I am going to assume from here on that you are using an sh-like shell that uses |
|
109 |
the"VARIABLE=value" syntax. If you are using a csh-like shell, then you |
|
110 |
will have to convert my examples to the "setenv VARIABLE value" syntax |
|
111 |
required by those shells. |
|
112 |
||
113 |
Right now, the UDP echo client application is responding to the following line |
|
114 |
of code in ``scratch/myfirst.cc``, |
|
115 |
||
116 |
:: |
|
117 |
||
118 |
LogComponentEnable("UdpEchoClientApplication", LOG_LEVEL_INFO); |
|
119 |
||
120 |
This line of code enables the ``LOG_LEVEL_INFO`` level of logging. When |
|
121 |
we pass a logging level flag, we are actually enabling the given level and |
|
122 |
all lower levels. In this case, we have enabled ``NS_LOG_INFO``, |
|
123 |
``NS_LOG_DEBUG``, ``NS_LOG_WARN`` and ``NS_LOG_ERROR``. We can |
|
124 |
increase the logging level and get more information without changing the |
|
125 |
script and recompiling by setting the NS_LOG environment variable like this: |
|
126 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
127 |
.. sourcecode:: bash |
6754 | 128 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
129 |
$ export NS_LOG=UdpEchoClientApplication=level_all |
6754 | 130 |
|
131 |
This sets the shell environment variable ``NS_LOG`` to the string, |
|
132 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
133 |
.. sourcecode:: bash |
6754 | 134 |
|
135 |
UdpEchoClientApplication=level_all |
|
136 |
||
137 |
The left hand side of the assignment is the name of the logging component we |
|
138 |
want to set, and the right hand side is the flag we want to use. In this case, |
|
139 |
we are going to turn on all of the debugging levels for the application. If |
|
140 |
you run the script with NS_LOG set this way, the |ns3| logging |
|
141 |
system will pick up the change and you should see the following output: |
|
142 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
143 |
.. sourcecode:: bash |
6754 | 144 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
145 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
6754 | 146 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
147 |
'build' finished successfully (0.404s) |
|
148 |
UdpEchoClientApplication:UdpEchoClient() |
|
149 |
UdpEchoClientApplication:SetDataSize(1024) |
|
150 |
UdpEchoClientApplication:StartApplication() |
|
151 |
UdpEchoClientApplication:ScheduleTransmit() |
|
152 |
UdpEchoClientApplication:Send() |
|
153 |
Sent 1024 bytes to 10.1.1.2 |
|
154 |
Received 1024 bytes from 10.1.1.1 |
|
155 |
UdpEchoClientApplication:HandleRead(0x6241e0, 0x624a20) |
|
156 |
Received 1024 bytes from 10.1.1.2 |
|
157 |
UdpEchoClientApplication:StopApplication() |
|
158 |
UdpEchoClientApplication:DoDispose() |
|
159 |
UdpEchoClientApplication:~UdpEchoClient() |
|
160 |
||
161 |
The additional debug information provided by the application is from |
|
162 |
the NS_LOG_FUNCTION level. This shows every time a function in the application |
|
9104
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
163 |
is called during script execution. Generally, use of (at least) |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
164 |
NS_LOG_FUNCTION(this) in member functions is preferred. Use |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
165 |
NS_LOG_FUNCTION_NOARGS() only in static functions. Note, however, that there |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
166 |
are no requirements in the |ns3| system that models must support any particular |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
167 |
logging functionality. The decision regarding how much information is logged |
5de0d2c78514
Improve logging part in tutorial, and make clear distinction between logging level and logging macro.
Vedran Miletić <rivanvx@gmail.com>
parents:
7140
diff
changeset
|
168 |
is left to the individual model developer. In the case of the echo |
6754 | 169 |
applications, a good deal of log output is available. |
170 |
||
171 |
You can now see a log of the function calls that were made to the application. |
|
172 |
If you look closely you will notice a single colon between the string |
|
173 |
``UdpEchoClientApplication`` and the method name where you might have |
|
174 |
expected a C++ scope operator (``::``). This is intentional. |
|
175 |
||
176 |
The name is not actually a class name, it is a logging component name. When |
|
177 |
there is a one-to-one correspondence between a source file and a class, this |
|
178 |
will generally be the class name but you should understand that it is not |
|
179 |
actually a class name, and there is a single colon there instead of a double |
|
180 |
colon to remind you in a relatively subtle way to conceptually separate the |
|
181 |
logging component name from the class name. |
|
182 |
||
183 |
It turns out that in some cases, it can be hard to determine which method |
|
184 |
actually generates a log message. If you look in the text above, you may |
|
185 |
wonder where the string "``Received 1024 bytes from 10.1.1.2``" comes |
|
186 |
from. You can resolve this by OR'ing the ``prefix_func`` level into the |
|
187 |
``NS_LOG`` environment variable. Try doing the following, |
|
188 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
189 |
.. sourcecode:: bash |
6754 | 190 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
191 |
$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func' |
6754 | 192 |
|
193 |
Note that the quotes are required since the vertical bar we use to indicate an |
|
194 |
OR operation is also a Unix pipe connector. |
|
195 |
||
196 |
Now, if you run the script you will see that the logging system makes sure |
|
197 |
that every message from the given log component is prefixed with the component |
|
198 |
name. |
|
199 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
200 |
.. sourcecode:: bash |
6754 | 201 |
|
202 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
203 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
204 |
'build' finished successfully (0.417s) |
|
205 |
UdpEchoClientApplication:UdpEchoClient() |
|
206 |
UdpEchoClientApplication:SetDataSize(1024) |
|
207 |
UdpEchoClientApplication:StartApplication() |
|
208 |
UdpEchoClientApplication:ScheduleTransmit() |
|
209 |
UdpEchoClientApplication:Send() |
|
210 |
UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2 |
|
211 |
Received 1024 bytes from 10.1.1.1 |
|
212 |
UdpEchoClientApplication:HandleRead(0x6241e0, 0x624a20) |
|
213 |
UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2 |
|
214 |
UdpEchoClientApplication:StopApplication() |
|
215 |
UdpEchoClientApplication:DoDispose() |
|
216 |
UdpEchoClientApplication:~UdpEchoClient() |
|
217 |
||
218 |
You can now see all of the messages coming from the UDP echo client application |
|
219 |
are identified as such. The message "Received 1024 bytes from 10.1.1.2" is |
|
220 |
now clearly identified as coming from the echo client application. The |
|
221 |
remaining message must be coming from the UDP echo server application. We |
|
222 |
can enable that component by entering a colon separated list of components in |
|
223 |
the NS_LOG environment variable. |
|
224 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
225 |
.. sourcecode:: bash |
6754 | 226 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
227 |
$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func: |
6754 | 228 |
UdpEchoServerApplication=level_all|prefix_func' |
229 |
||
230 |
Warning: You will need to remove the newline after the ``:`` in the |
|
231 |
example text above which is only there for document formatting purposes. |
|
232 |
||
233 |
Now, if you run the script you will see all of the log messages from both the |
|
234 |
echo client and server applications. You may see that this can be very useful |
|
235 |
in debugging problems. |
|
236 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
237 |
.. sourcecode:: bash |
6754 | 238 |
|
239 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
240 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
241 |
'build' finished successfully (0.406s) |
|
242 |
UdpEchoServerApplication:UdpEchoServer() |
|
243 |
UdpEchoClientApplication:UdpEchoClient() |
|
244 |
UdpEchoClientApplication:SetDataSize(1024) |
|
245 |
UdpEchoServerApplication:StartApplication() |
|
246 |
UdpEchoClientApplication:StartApplication() |
|
247 |
UdpEchoClientApplication:ScheduleTransmit() |
|
248 |
UdpEchoClientApplication:Send() |
|
249 |
UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2 |
|
250 |
UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1 |
|
251 |
UdpEchoServerApplication:HandleRead(): Echoing packet |
|
252 |
UdpEchoClientApplication:HandleRead(0x624920, 0x625160) |
|
253 |
UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2 |
|
254 |
UdpEchoServerApplication:StopApplication() |
|
255 |
UdpEchoClientApplication:StopApplication() |
|
256 |
UdpEchoClientApplication:DoDispose() |
|
257 |
UdpEchoServerApplication:DoDispose() |
|
258 |
UdpEchoClientApplication:~UdpEchoClient() |
|
259 |
UdpEchoServerApplication:~UdpEchoServer() |
|
260 |
||
261 |
It is also sometimes useful to be able to see the simulation time at which a |
|
262 |
log message is generated. You can do this by ORing in the prefix_time bit. |
|
263 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
264 |
.. sourcecode:: bash |
6754 | 265 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
266 |
$ export 'NS_LOG=UdpEchoClientApplication=level_all|prefix_func|prefix_time: |
6754 | 267 |
UdpEchoServerApplication=level_all|prefix_func|prefix_time' |
268 |
||
269 |
Again, you will have to remove the newline above. If you run the script now, |
|
270 |
you should see the following output: |
|
271 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
272 |
.. sourcecode:: bash |
6754 | 273 |
|
274 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
275 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
276 |
'build' finished successfully (0.418s) |
|
277 |
0s UdpEchoServerApplication:UdpEchoServer() |
|
278 |
0s UdpEchoClientApplication:UdpEchoClient() |
|
279 |
0s UdpEchoClientApplication:SetDataSize(1024) |
|
280 |
1s UdpEchoServerApplication:StartApplication() |
|
281 |
2s UdpEchoClientApplication:StartApplication() |
|
282 |
2s UdpEchoClientApplication:ScheduleTransmit() |
|
283 |
2s UdpEchoClientApplication:Send() |
|
284 |
2s UdpEchoClientApplication:Send(): Sent 1024 bytes to 10.1.1.2 |
|
285 |
2.00369s UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1 |
|
286 |
2.00369s UdpEchoServerApplication:HandleRead(): Echoing packet |
|
287 |
2.00737s UdpEchoClientApplication:HandleRead(0x624290, 0x624ad0) |
|
288 |
2.00737s UdpEchoClientApplication:HandleRead(): Received 1024 bytes from 10.1.1.2 |
|
289 |
10s UdpEchoServerApplication:StopApplication() |
|
290 |
10s UdpEchoClientApplication:StopApplication() |
|
291 |
UdpEchoClientApplication:DoDispose() |
|
292 |
UdpEchoServerApplication:DoDispose() |
|
293 |
UdpEchoClientApplication:~UdpEchoClient() |
|
294 |
UdpEchoServerApplication:~UdpEchoServer() |
|
295 |
||
296 |
You can see that the constructor for the UdpEchoServer was called at a |
|
297 |
simulation time of 0 seconds. This is actually happening before the |
|
298 |
simulation starts, but the time is displayed as zero seconds. The same is true |
|
299 |
for the UdpEchoClient constructor message. |
|
300 |
||
301 |
Recall that the ``scratch/first.cc`` script started the echo server |
|
302 |
application at one second into the simulation. You can now see that the |
|
303 |
``StartApplication`` method of the server is, in fact, called at one second. |
|
304 |
You can also see that the echo client application is started at a simulation |
|
305 |
time of two seconds as we requested in the script. |
|
306 |
||
307 |
You can now follow the progress of the simulation from the |
|
308 |
``ScheduleTransmit`` call in the client that calls ``Send`` to the |
|
309 |
``HandleRead`` callback in the echo server application. Note that the |
|
310 |
elapsed time for the packet to be sent across the point-to-point link is 3.69 |
|
311 |
milliseconds. You see the echo server logging a message telling you that it |
|
312 |
has echoed the packet and then, after another channel delay, you see the echo |
|
313 |
client receive the echoed packet in its ``HandleRead`` method. |
|
314 |
||
315 |
There is a lot that is happening under the covers in this simulation that you |
|
316 |
are not seeing as well. You can very easily follow the entire process by |
|
317 |
turning on all of the logging components in the system. Try setting the |
|
318 |
``NS_LOG`` variable to the following, |
|
319 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
320 |
.. sourcecode:: bash |
6754 | 321 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
322 |
$ export 'NS_LOG=*=level_all|prefix_func|prefix_time' |
6754 | 323 |
|
324 |
The asterisk above is the logging component wildcard. This will turn on all |
|
325 |
of the logging in all of the components used in the simulation. I won't |
|
326 |
reproduce the output here (as of this writing it produces 1265 lines of output |
|
327 |
for the single packet echo) but you can redirect this information into a file |
|
328 |
and look through it with your favorite editor if you like, |
|
329 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
330 |
.. sourcecode:: bash |
6754 | 331 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
332 |
$ ./waf --run scratch/myfirst > log.out 2>&1 |
6754 | 333 |
|
334 |
I personally use this extremely verbose version of logging when I am presented |
|
335 |
with a problem and I have no idea where things are going wrong. I can follow the |
|
336 |
progress of the code quite easily without having to set breakpoints and step |
|
337 |
through code in a debugger. I can just edit up the output in my favorite editor |
|
338 |
and search around for things I expect, and see things happening that I don't |
|
339 |
expect. When I have a general idea about what is going wrong, I transition into |
|
340 |
a debugger for a fine-grained examination of the problem. This kind of output |
|
341 |
can be especially useful when your script does something completely unexpected. |
|
342 |
If you are stepping using a debugger you may miss an unexpected excursion |
|
343 |
completely. Logging the excursion makes it quickly visible. |
|
344 |
||
345 |
Adding Logging to your Code |
|
346 |
+++++++++++++++++++++++++++ |
|
347 |
You can add new logging to your simulations by making calls to the log |
|
348 |
component via several macros. Let's do so in the ``myfirst.cc`` script we |
|
349 |
have in the ``scratch`` directory. |
|
350 |
||
351 |
Recall that we have defined a logging component in that script: |
|
352 |
||
353 |
:: |
|
354 |
||
355 |
NS_LOG_COMPONENT_DEFINE ("FirstScriptExample"); |
|
356 |
||
357 |
You now know that you can enable all of the logging for this component by |
|
358 |
setting the ``NS_LOG`` environment variable to the various levels. Let's |
|
359 |
go ahead and add some logging to the script. The macro used to add an |
|
360 |
informational level log message is ``NS_LOG_INFO``. Go ahead and add one |
|
361 |
(just before we start creating the nodes) that tells you that the script is |
|
362 |
"Creating Topology." This is done as in this code snippet, |
|
363 |
||
364 |
Open ``scratch/myfirst.cc`` in your favorite editor and add the line, |
|
365 |
||
366 |
:: |
|
367 |
||
368 |
NS_LOG_INFO ("Creating Topology"); |
|
369 |
||
370 |
right before the lines, |
|
371 |
||
372 |
:: |
|
373 |
||
374 |
NodeContainer nodes; |
|
375 |
nodes.Create (2); |
|
376 |
||
377 |
Now build the script using waf and clear the ``NS_LOG`` variable to turn |
|
378 |
off the torrent of logging we previously enabled: |
|
379 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
380 |
.. sourcecode:: bash |
6754 | 381 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
382 |
$ ./waf |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
383 |
$ export NS_LOG= |
6754 | 384 |
|
385 |
Now, if you run the script, |
|
386 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
387 |
.. sourcecode:: bash |
6754 | 388 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
389 |
$ ./waf --run scratch/myfirst |
6754 | 390 |
|
391 |
you will ``not`` see your new message since its associated logging |
|
392 |
component (``FirstScriptExample``) has not been enabled. In order to see your |
|
393 |
message you will have to enable the ``FirstScriptExample`` logging component |
|
394 |
with a level greater than or equal to ``NS_LOG_INFO``. If you just want to |
|
395 |
see this particular level of logging, you can enable it by, |
|
396 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
397 |
.. sourcecode:: bash |
6754 | 398 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
399 |
$ export NS_LOG=FirstScriptExample=info |
6754 | 400 |
|
401 |
If you now run the script you will see your new "Creating Topology" log |
|
402 |
message, |
|
403 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
404 |
.. sourcecode:: bash |
6754 | 405 |
|
406 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
407 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
408 |
'build' finished successfully (0.404s) |
|
409 |
Creating Topology |
|
410 |
Sent 1024 bytes to 10.1.1.2 |
|
411 |
Received 1024 bytes from 10.1.1.1 |
|
412 |
Received 1024 bytes from 10.1.1.2 |
|
413 |
||
414 |
Using Command Line Arguments |
|
415 |
**************************** |
|
416 |
||
11088
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
417 |
.. _Attribute: |
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
418 |
|
6754 | 419 |
Overriding Default Attributes |
420 |
+++++++++++++++++++++++++++++ |
|
421 |
Another way you can change how |ns3| scripts behave without editing |
|
422 |
and building is via *command line arguments.* We provide a mechanism to |
|
423 |
parse command line arguments and automatically set local and global variables |
|
424 |
based on those arguments. |
|
425 |
||
426 |
The first step in using the command line argument system is to declare the |
|
427 |
command line parser. This is done quite simply (in your main program) as |
|
428 |
in the following code, |
|
429 |
||
430 |
:: |
|
431 |
||
7140
d203296efb63
Fix an example dependency problem and fix some paths in the documentation
Mitch Watrous <watrous@u.washington.edu>
parents:
6754
diff
changeset
|
432 |
int |
6754 | 433 |
main (int argc, char *argv[]) |
434 |
{ |
|
435 |
... |
|
436 |
||
437 |
CommandLine cmd; |
|
438 |
cmd.Parse (argc, argv); |
|
439 |
||
440 |
... |
|
441 |
} |
|
442 |
||
443 |
This simple two line snippet is actually very useful by itself. It opens the |
|
444 |
door to the |ns3| global variable and ``Attribute`` systems. Go |
|
445 |
ahead and add that two lines of code to the ``scratch/myfirst.cc`` script at |
|
446 |
the start of ``main``. Go ahead and build the script and run it, but ask |
|
447 |
the script for help in the following way, |
|
448 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
449 |
.. sourcecode:: bash |
6754 | 450 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
451 |
$ ./waf --run "scratch/myfirst --PrintHelp" |
6754 | 452 |
|
453 |
This will ask Waf to run the ``scratch/myfirst`` script and pass the command |
|
454 |
line argument ``--PrintHelp`` to the script. The quotes are required to |
|
455 |
sort out which program gets which argument. The command line parser will |
|
456 |
now see the ``--PrintHelp`` argument and respond with, |
|
457 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
458 |
.. sourcecode:: bash |
6754 | 459 |
|
460 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
461 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
462 |
'build' finished successfully (0.413s) |
|
463 |
TcpL4Protocol:TcpStateMachine() |
|
464 |
CommandLine:HandleArgument(): Handle arg name=PrintHelp value= |
|
465 |
--PrintHelp: Print this help message. |
|
466 |
--PrintGroups: Print the list of groups. |
|
467 |
--PrintTypeIds: Print all TypeIds. |
|
468 |
--PrintGroup=[group]: Print all TypeIds of group. |
|
469 |
--PrintAttributes=[typeid]: Print all attributes of typeid. |
|
470 |
--PrintGlobals: Print the list of globals. |
|
471 |
||
472 |
Let's focus on the ``--PrintAttributes`` option. We have already hinted |
|
473 |
at the |ns3| ``Attribute`` system while walking through the |
|
474 |
``first.cc`` script. We looked at the following lines of code, |
|
475 |
||
476 |
:: |
|
477 |
||
478 |
PointToPointHelper pointToPoint; |
|
479 |
pointToPoint.SetDeviceAttribute ("DataRate", StringValue ("5Mbps")); |
|
480 |
pointToPoint.SetChannelAttribute ("Delay", StringValue ("2ms")); |
|
481 |
||
482 |
and mentioned that ``DataRate`` was actually an ``Attribute`` of the |
|
483 |
``PointToPointNetDevice``. Let's use the command line argument parser |
|
484 |
to take a look at the ``Attributes`` of the PointToPointNetDevice. The help |
|
485 |
listing says that we should provide a ``TypeId``. This corresponds to the |
|
486 |
class name of the class to which the ``Attributes`` belong. In this case it |
|
487 |
will be ``ns3::PointToPointNetDevice``. Let's go ahead and type in, |
|
488 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
489 |
.. sourcecode:: bash |
6754 | 490 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
491 |
$ ./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointNetDevice" |
6754 | 492 |
|
493 |
The system will print out all of the ``Attributes`` of this kind of net device. |
|
494 |
Among the ``Attributes`` you will see listed is, |
|
495 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
496 |
.. sourcecode:: bash |
6754 | 497 |
|
498 |
--ns3::PointToPointNetDevice::DataRate=[32768bps]: |
|
499 |
The default data rate for point to point links |
|
500 |
||
501 |
This is the default value that will be used when a ``PointToPointNetDevice`` |
|
502 |
is created in the system. We overrode this default with the ``Attribute`` |
|
503 |
setting in the ``PointToPointHelper`` above. Let's use the default values |
|
504 |
for the point-to-point devices and channels by deleting the |
|
505 |
``SetDeviceAttribute`` call and the ``SetChannelAttribute`` call from |
|
506 |
the ``myfirst.cc`` we have in the scratch directory. |
|
507 |
||
508 |
Your script should now just declare the ``PointToPointHelper`` and not do |
|
509 |
any ``set`` operations as in the following example, |
|
510 |
||
511 |
:: |
|
512 |
||
513 |
... |
|
514 |
||
515 |
NodeContainer nodes; |
|
516 |
nodes.Create (2); |
|
517 |
||
518 |
PointToPointHelper pointToPoint; |
|
519 |
||
520 |
NetDeviceContainer devices; |
|
521 |
devices = pointToPoint.Install (nodes); |
|
522 |
||
523 |
... |
|
524 |
||
525 |
Go ahead and build the new script with Waf (``./waf``) and let's go back |
|
526 |
and enable some logging from the UDP echo server application and turn on the |
|
527 |
time prefix. |
|
528 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
529 |
.. sourcecode:: bash |
6754 | 530 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
531 |
$ export 'NS_LOG=UdpEchoServerApplication=level_all|prefix_time' |
6754 | 532 |
|
533 |
If you run the script, you should now see the following output, |
|
534 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
535 |
.. sourcecode:: bash |
6754 | 536 |
|
537 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
538 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
539 |
'build' finished successfully (0.405s) |
|
540 |
0s UdpEchoServerApplication:UdpEchoServer() |
|
541 |
1s UdpEchoServerApplication:StartApplication() |
|
542 |
Sent 1024 bytes to 10.1.1.2 |
|
543 |
2.25732s Received 1024 bytes from 10.1.1.1 |
|
544 |
2.25732s Echoing packet |
|
545 |
Received 1024 bytes from 10.1.1.2 |
|
546 |
10s UdpEchoServerApplication:StopApplication() |
|
547 |
UdpEchoServerApplication:DoDispose() |
|
548 |
UdpEchoServerApplication:~UdpEchoServer() |
|
549 |
||
550 |
Recall that the last time we looked at the simulation time at which the packet |
|
551 |
was received by the echo server, it was at 2.00369 seconds. |
|
552 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
553 |
.. sourcecode:: bash |
6754 | 554 |
|
555 |
2.00369s UdpEchoServerApplication:HandleRead(): Received 1024 bytes from 10.1.1.1 |
|
556 |
||
557 |
Now it is receiving the packet at 2.25732 seconds. This is because we just dropped |
|
558 |
the data rate of the ``PointToPointNetDevice`` down to its default of |
|
559 |
32768 bits per second from five megabits per second. |
|
560 |
||
561 |
If we were to provide a new ``DataRate`` using the command line, we could |
|
562 |
speed our simulation up again. We do this in the following way, according to |
|
563 |
the formula implied by the help item: |
|
564 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
565 |
.. sourcecode:: bash |
6754 | 566 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
567 |
$ ./waf --run "scratch/myfirst --ns3::PointToPointNetDevice::DataRate=5Mbps" |
6754 | 568 |
|
569 |
This will set the default value of the ``DataRate`` ``Attribute`` back to |
|
570 |
five megabits per second. Are you surprised by the result? It turns out that |
|
571 |
in order to get the original behavior of the script back, we will have to set |
|
572 |
the speed-of-light delay of the channel as well. We can ask the command line |
|
573 |
system to print out the ``Attributes`` of the channel just like we did for |
|
574 |
the net device: |
|
575 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
576 |
.. sourcecode:: bash |
6754 | 577 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
578 |
$ ./waf --run "scratch/myfirst --PrintAttributes=ns3::PointToPointChannel" |
6754 | 579 |
|
580 |
We discover the ``Delay`` ``Attribute`` of the channel is set in the following |
|
581 |
way: |
|
582 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
583 |
.. sourcecode:: bash |
6754 | 584 |
|
585 |
--ns3::PointToPointChannel::Delay=[0ns]: |
|
586 |
Transmission delay through the channel |
|
587 |
||
588 |
We can then set both of these default values through the command line system, |
|
589 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
590 |
.. sourcecode:: bash |
6754 | 591 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
592 |
$ ./waf --run "scratch/myfirst |
6754 | 593 |
--ns3::PointToPointNetDevice::DataRate=5Mbps |
594 |
--ns3::PointToPointChannel::Delay=2ms" |
|
595 |
||
596 |
in which case we recover the timing we had when we explicitly set the |
|
597 |
``DataRate`` and ``Delay`` in the script: |
|
598 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
599 |
.. sourcecode:: bash |
6754 | 600 |
|
601 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
602 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
603 |
'build' finished successfully (0.417s) |
|
604 |
0s UdpEchoServerApplication:UdpEchoServer() |
|
605 |
1s UdpEchoServerApplication:StartApplication() |
|
606 |
Sent 1024 bytes to 10.1.1.2 |
|
607 |
2.00369s Received 1024 bytes from 10.1.1.1 |
|
608 |
2.00369s Echoing packet |
|
609 |
Received 1024 bytes from 10.1.1.2 |
|
610 |
10s UdpEchoServerApplication:StopApplication() |
|
611 |
UdpEchoServerApplication:DoDispose() |
|
612 |
UdpEchoServerApplication:~UdpEchoServer() |
|
613 |
||
614 |
Note that the packet is again received by the server at 2.00369 seconds. We |
|
615 |
could actually set any of the ``Attributes`` used in the script in this way. |
|
616 |
In particular we could set the ``UdpEchoClient Attribute MaxPackets`` |
|
617 |
to some other value than one. |
|
618 |
||
619 |
How would you go about that? Give it a try. Remember you have to comment |
|
620 |
out the place we override the default ``Attribute`` and explicitly set |
|
621 |
``MaxPackets`` in the script. Then you have to rebuild the script. You |
|
622 |
will also have to find the syntax for actually setting the new default attribute |
|
623 |
value using the command line help facility. Once you have this figured out |
|
624 |
you should be able to control the number of packets echoed from the command |
|
625 |
line. Since we're nice folks, we'll tell you that your command line should |
|
626 |
end up looking something like, |
|
627 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
628 |
.. sourcecode:: bash |
6754 | 629 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
630 |
$ ./waf --run "scratch/myfirst |
6754 | 631 |
--ns3::PointToPointNetDevice::DataRate=5Mbps |
632 |
--ns3::PointToPointChannel::Delay=2ms |
|
633 |
--ns3::UdpEchoClient::MaxPackets=2" |
|
634 |
||
11248
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
635 |
A natural question to arise at this point is how to learn about the existence |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
636 |
of all of these attributes. Again, the command line help facility has |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
637 |
a feature for this. If we ask for command line help we should see: |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
638 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
639 |
.. sourcecode:: bash |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
640 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
641 |
$ ./waf --run "scratch/myfirst --PrintHelp" |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
642 |
myfirst [Program Arguments] [General Arguments] |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
643 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
644 |
General Arguments: |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
645 |
--PrintGlobals: Print the list of globals. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
646 |
--PrintGroups: Print the list of groups. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
647 |
--PrintGroup=[group]: Print all TypeIds of group. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
648 |
--PrintTypeIds: Print all TypeIds. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
649 |
--PrintAttributes=[typeid]: Print all attributes of typeid. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
650 |
--PrintHelp: Print this help message. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
651 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
652 |
If you select the "PrintGroups" argument, you should see a list of all |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
653 |
registered TypeId groups. The group names are aligned with the module names |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
654 |
in the source directory (although with a leading capital letter). Printing |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
655 |
out all of the information at once would be too much, so a further filter |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
656 |
is available to print information on a per-group basis. So, focusing |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
657 |
again on the point-to-point module: |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
658 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
659 |
.. sourcecode:: bash |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
660 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
661 |
./waf --run "scratch/myfirst --PrintGroup=PointToPoint" |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
662 |
TypeIds in group PointToPoint: |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
663 |
ns3::PointToPointChannel |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
664 |
ns3::PointToPointNetDevice |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
665 |
ns3::PointToPointRemoteChannel |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
666 |
ns3::PppHeader |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
667 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
668 |
and from here, one can find the possible TypeId names to search for |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
669 |
attributes, such as in the ``--PrintAttributes=ns3::PointToPointChannel`` |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
670 |
example shown above. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
671 |
|
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
672 |
Another way to find out about attributes is through the ns-3 Doxygen; there |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
673 |
is a page that lists out all of the registered attributes in the simulator. |
88468adb47b5
additional coverage of group name usage in the tutorial
Tom Henderson <tomh@tomh.org>
parents:
11088
diff
changeset
|
674 |
|
6754 | 675 |
Hooking Your Own Values |
676 |
+++++++++++++++++++++++ |
|
677 |
You can also add your own hooks to the command line system. This is done |
|
678 |
quite simply by using the ``AddValue`` method to the command line parser. |
|
679 |
||
680 |
Let's use this facility to specify the number of packets to echo in a |
|
681 |
completely different way. Let's add a local variable called ``nPackets`` |
|
682 |
to the ``main`` function. We'll initialize it to one to match our previous |
|
683 |
default behavior. To allow the command line parser to change this value, we |
|
684 |
need to hook the value into the parser. We do this by adding a call to |
|
685 |
``AddValue``. Go ahead and change the ``scratch/myfirst.cc`` script to |
|
686 |
start with the following code, |
|
687 |
||
688 |
:: |
|
689 |
||
690 |
int |
|
691 |
main (int argc, char *argv[]) |
|
692 |
{ |
|
693 |
uint32_t nPackets = 1; |
|
694 |
||
695 |
CommandLine cmd; |
|
696 |
cmd.AddValue("nPackets", "Number of packets to echo", nPackets); |
|
697 |
cmd.Parse (argc, argv); |
|
698 |
||
699 |
... |
|
700 |
||
701 |
Scroll down to the point in the script where we set the ``MaxPackets`` |
|
702 |
``Attribute`` and change it so that it is set to the variable ``nPackets`` |
|
703 |
instead of the constant ``1`` as is shown below. |
|
704 |
||
705 |
:: |
|
706 |
||
707 |
echoClient.SetAttribute ("MaxPackets", UintegerValue (nPackets)); |
|
708 |
||
709 |
Now if you run the script and provide the ``--PrintHelp`` argument, you |
|
710 |
should see your new ``User Argument`` listed in the help display. |
|
711 |
||
712 |
Try, |
|
713 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
714 |
.. sourcecode:: bash |
6754 | 715 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
716 |
$ ./waf --run "scratch/myfirst --PrintHelp" |
6754 | 717 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
718 |
.. sourcecode:: bash |
6754 | 719 |
|
720 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
721 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
722 |
'build' finished successfully (0.403s) |
|
723 |
--PrintHelp: Print this help message. |
|
724 |
--PrintGroups: Print the list of groups. |
|
725 |
--PrintTypeIds: Print all TypeIds. |
|
726 |
--PrintGroup=[group]: Print all TypeIds of group. |
|
727 |
--PrintAttributes=[typeid]: Print all attributes of typeid. |
|
728 |
--PrintGlobals: Print the list of globals. |
|
729 |
User Arguments: |
|
730 |
--nPackets: Number of packets to echo |
|
731 |
||
732 |
If you want to specify the number of packets to echo, you can now do so by |
|
733 |
setting the ``--nPackets`` argument in the command line, |
|
734 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
735 |
.. sourcecode:: bash |
6754 | 736 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
737 |
$ ./waf --run "scratch/myfirst --nPackets=2" |
6754 | 738 |
|
739 |
You should now see |
|
740 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
741 |
.. sourcecode:: bash |
6754 | 742 |
|
743 |
Waf: Entering directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
744 |
Waf: Leaving directory `/home/craigdo/repos/ns-3-allinone/ns-3-dev/build' |
|
745 |
'build' finished successfully (0.404s) |
|
746 |
0s UdpEchoServerApplication:UdpEchoServer() |
|
747 |
1s UdpEchoServerApplication:StartApplication() |
|
748 |
Sent 1024 bytes to 10.1.1.2 |
|
749 |
2.25732s Received 1024 bytes from 10.1.1.1 |
|
750 |
2.25732s Echoing packet |
|
751 |
Received 1024 bytes from 10.1.1.2 |
|
752 |
Sent 1024 bytes to 10.1.1.2 |
|
753 |
3.25732s Received 1024 bytes from 10.1.1.1 |
|
754 |
3.25732s Echoing packet |
|
755 |
Received 1024 bytes from 10.1.1.2 |
|
756 |
10s UdpEchoServerApplication:StopApplication() |
|
757 |
UdpEchoServerApplication:DoDispose() |
|
758 |
UdpEchoServerApplication:~UdpEchoServer() |
|
759 |
||
760 |
You have now echoed two packets. Pretty easy, isn't it? |
|
761 |
||
762 |
You can see that if you are an |ns3| user, you can use the command |
|
763 |
line argument system to control global values and ``Attributes``. If you are |
|
764 |
a model author, you can add new ``Attributes`` to your ``Objects`` and |
|
765 |
they will automatically be available for setting by your users through the |
|
766 |
command line system. If you are a script author, you can add new variables to |
|
767 |
your scripts and hook them into the command line system quite painlessly. |
|
768 |
||
11088
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
769 |
.. _UsingTracingSystem: |
50e89dc6d8b1
[Sphinx] Update the Tracing chapter in the Tutorial.
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9957
diff
changeset
|
770 |
|
6754 | 771 |
Using the Tracing System |
772 |
************************ |
|
773 |
||
774 |
The whole point of simulation is to generate output for further study, and |
|
775 |
the |ns3| tracing system is a primary mechanism for this. Since |
|
776 |
|ns3| is a C++ program, standard facilities for generating output |
|
777 |
from C++ programs could be used: |
|
778 |
||
779 |
:: |
|
780 |
||
781 |
#include <iostream> |
|
782 |
... |
|
783 |
int main () |
|
784 |
{ |
|
785 |
... |
|
786 |
std::cout << "The value of x is " << x << std::endl; |
|
787 |
... |
|
788 |
} |
|
789 |
||
790 |
You could even use the logging module to add a little structure to your |
|
791 |
solution. There are many well-known problems generated by such approaches |
|
792 |
and so we have provided a generic event tracing subsystem to address the |
|
793 |
issues we thought were important. |
|
794 |
||
795 |
The basic goals of the |ns3| tracing system are: |
|
796 |
||
797 |
* For basic tasks, the tracing system should allow the user to generate |
|
798 |
standard tracing for popular tracing sources, and to customize which objects |
|
799 |
generate the tracing; |
|
800 |
* Intermediate users must be able to extend the tracing system to modify |
|
801 |
the output format generated, or to insert new tracing sources, without |
|
802 |
modifying the core of the simulator; |
|
803 |
* Advanced users can modify the simulator core to add new tracing sources |
|
804 |
and sinks. |
|
805 |
||
806 |
The |ns3| tracing system is built on the concepts of independent |
|
807 |
tracing sources and tracing sinks, and a uniform mechanism for connecting |
|
808 |
sources to sinks. Trace sources are entities that can signal events that |
|
809 |
happen in a simulation and provide access to interesting underlying data. |
|
810 |
For example, a trace source could indicate when a packet is received by a net |
|
811 |
device and provide access to the packet contents for interested trace sinks. |
|
812 |
||
813 |
Trace sources are not useful by themselves, they must be "connected" to |
|
814 |
other pieces of code that actually do something useful with the information |
|
815 |
provided by the sink. Trace sinks are consumers of the events and data |
|
816 |
provided by the trace sources. For example, one could create a trace sink |
|
817 |
that would (when connected to the trace source of the previous example) print |
|
818 |
out interesting parts of the received packet. |
|
819 |
||
820 |
The rationale for this explicit division is to allow users to attach new |
|
821 |
types of sinks to existing tracing sources, without requiring editing and |
|
822 |
recompilation of the core of the simulator. Thus, in the example above, |
|
823 |
a user could define a new tracing sink in her script and attach it to an |
|
824 |
existing tracing source defined in the simulation core by editing only the |
|
825 |
user script. |
|
826 |
||
827 |
In this tutorial, we will walk through some pre-defined sources and sinks and |
|
828 |
show how they may be customized with little user effort. See the ns-3 manual |
|
829 |
or how-to sections for information on advanced tracing configuration including |
|
830 |
extending the tracing namespace and creating new tracing sources. |
|
831 |
||
832 |
ASCII Tracing |
|
833 |
+++++++++++++ |
|
834 |
|ns3| provides helper functionality that wraps the low-level tracing |
|
835 |
system to help you with the details involved in configuring some easily |
|
836 |
understood packet traces. If you enable this functionality, you will see |
|
837 |
output in a ASCII files --- thus the name. For those familiar with |
|
838 |
|ns2| output, this type of trace is analogous to the ``out.tr`` |
|
839 |
generated by many scripts. |
|
840 |
||
841 |
Let's just jump right in and add some ASCII tracing output to our |
|
842 |
``scratch/myfirst.cc`` script. Right before the call to |
|
843 |
``Simulator::Run ()``, add the following lines of code: |
|
844 |
||
845 |
:: |
|
846 |
||
847 |
AsciiTraceHelper ascii; |
|
848 |
pointToPoint.EnableAsciiAll (ascii.CreateFileStream ("myfirst.tr")); |
|
849 |
||
850 |
Like in many other |ns3| idioms, this code uses a helper object to |
|
851 |
help create ASCII traces. The second line contains two nested method calls. |
|
852 |
The "inside" method, ``CreateFileStream()`` uses an unnamed object idiom |
|
853 |
to create a file stream object on the stack (without an object name) and pass |
|
854 |
it down to the called method. We'll go into this more in the future, but all |
|
855 |
you have to know at this point is that you are creating an object representing |
|
856 |
a file named "myfirst.tr" and passing it into ``ns-3``. You are telling |
|
857 |
``ns-3`` to deal with the lifetime issues of the created object and also to |
|
858 |
deal with problems caused by a little-known (intentional) limitation of C++ |
|
859 |
ofstream objects relating to copy constructors. |
|
860 |
||
861 |
The outside call, to ``EnableAsciiAll()``, tells the helper that you |
|
862 |
want to enable ASCII tracing on all point-to-point devices in your simulation; |
|
863 |
and you want the (provided) trace sinks to write out information about packet |
|
864 |
movement in ASCII format. |
|
865 |
||
866 |
For those familiar with |ns2|, the traced events are equivalent to |
|
867 |
the popular trace points that log "+", "-", "d", and "r" events. |
|
868 |
||
869 |
You can now build the script and run it from the command line: |
|
870 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
871 |
.. sourcecode:: bash |
6754 | 872 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
873 |
$ ./waf --run scratch/myfirst |
6754 | 874 |
|
875 |
Just as you have seen many times before, you will see some messages from Waf and then |
|
876 |
"'build' finished successfully" with some number of messages from |
|
877 |
the running program. |
|
878 |
||
879 |
When it ran, the program will have created a file named ``myfirst.tr``. |
|
880 |
Because of the way that Waf works, the file is not created in the local |
|
881 |
directory, it is created at the top-level directory of the repository by |
|
882 |
default. If you want to control where the traces are saved you can use the |
|
883 |
``--cwd`` option of Waf to specify this. We have not done so, thus we |
|
884 |
need to change into the top level directory of our repo and take a look at |
|
885 |
the ASCII trace file ``myfirst.tr`` in your favorite editor. |
|
886 |
||
887 |
Parsing Ascii Traces |
|
888 |
~~~~~~~~~~~~~~~~~~~~ |
|
889 |
There's a lot of information there in a pretty dense form, but the first thing |
|
890 |
to notice is that there are a number of distinct lines in this file. It may |
|
891 |
be difficult to see this clearly unless you widen your window considerably. |
|
892 |
||
893 |
Each line in the file corresponds to a *trace event*. In this case |
|
894 |
we are tracing events on the *transmit queue* present in every |
|
895 |
point-to-point net device in the simulation. The transmit queue is a queue |
|
896 |
through which every packet destined for a point-to-point channel must pass. |
|
897 |
Note that each line in the trace file begins with a lone character (has a |
|
898 |
space after it). This character will have the following meaning: |
|
899 |
||
900 |
* ``+``: An enqueue operation occurred on the device queue; |
|
901 |
* ``-``: A dequeue operation occurred on the device queue; |
|
902 |
* ``d``: A packet was dropped, typically because the queue was full; |
|
903 |
* ``r``: A packet was received by the net device. |
|
904 |
||
905 |
Let's take a more detailed view of the first line in the trace file. I'll |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
906 |
break it down into sections (indented for clarity) with a reference |
6754 | 907 |
number on the left side: |
908 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
909 |
.. sourcecode:: text |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
910 |
:linenos: |
6754 | 911 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
912 |
+ |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
913 |
2 |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
914 |
/NodeList/0/DeviceList/0/$ns3::PointToPointNetDevice/TxQueue/Enqueue |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
915 |
ns3::PppHeader ( |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
916 |
Point-to-Point Protocol: IP (0x0021)) |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
917 |
ns3::Ipv4Header ( |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
918 |
tos 0x0 ttl 64 id 0 protocol 17 offset 0 flags [none] |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
919 |
length: 1052 10.1.1.1 > 10.1.1.2) |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
920 |
ns3::UdpHeader ( |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
921 |
length: 1032 49153 > 9) |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
922 |
Payload (size=1024) |
6754 | 923 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
924 |
The first section of this expanded trace event (reference number 0) is the |
6754 | 925 |
operation. We have a ``+`` character, so this corresponds to an |
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
926 |
*enqueue* operation on the transmit queue. The second section (reference 1) |
6754 | 927 |
is the simulation time expressed in seconds. You may recall that we asked the |
928 |
``UdpEchoClientApplication`` to start sending packets at two seconds. Here |
|
929 |
we see confirmation that this is, indeed, happening. |
|
930 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
931 |
The next section of the example trace (reference 2) tell us which trace source |
6754 | 932 |
originated this event (expressed in the tracing namespace). You can think |
933 |
of the tracing namespace somewhat like you would a filesystem namespace. The |
|
934 |
root of the namespace is the ``NodeList``. This corresponds to a container |
|
935 |
managed in the |ns3| core code that contains all of the nodes that are |
|
936 |
created in a script. Just as a filesystem may have directories under the |
|
937 |
root, we may have node numbers in the ``NodeList``. The string |
|
938 |
``/NodeList/0`` therefore refers to the zeroth node in the ``NodeList`` |
|
939 |
which we typically think of as "node 0". In each node there is a list of |
|
940 |
devices that have been installed. This list appears next in the namespace. |
|
941 |
You can see that this trace event comes from ``DeviceList/0`` which is the |
|
942 |
zeroth device installed in the node. |
|
943 |
||
944 |
The next string, ``$ns3::PointToPointNetDevice`` tells you what kind of |
|
945 |
device is in the zeroth position of the device list for node zero. |
|
946 |
Recall that the operation ``+`` found at reference 00 meant that an enqueue |
|
947 |
operation happened on the transmit queue of the device. This is reflected in |
|
948 |
the final segments of the "trace path" which are ``TxQueue/Enqueue``. |
|
949 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
950 |
The remaining sections in the trace should be fairly intuitive. References 3-4 |
6754 | 951 |
indicate that the packet is encapsulated in the point-to-point protocol. |
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
952 |
References 5-7 show that the packet has an IP version four header and has |
6754 | 953 |
originated from IP address 10.1.1.1 and is destined for 10.1.1.2. References |
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
954 |
8-9 show that this packet has a UDP header and, finally, reference 10 shows |
6754 | 955 |
that the payload is the expected 1024 bytes. |
956 |
||
957 |
The next line in the trace file shows the same packet being dequeued from the |
|
958 |
transmit queue on the same node. |
|
959 |
||
960 |
The Third line in the trace file shows the packet being received by the net |
|
961 |
device on the node with the echo server. I have reproduced that event below. |
|
962 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
963 |
.. sourcecode:: text |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
964 |
:linenos: |
6754 | 965 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
966 |
r |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
967 |
2.25732 |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
968 |
/NodeList/1/DeviceList/0/$ns3::PointToPointNetDevice/MacRx |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
969 |
ns3::Ipv4Header ( |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
970 |
tos 0x0 ttl 64 id 0 protocol 17 offset 0 flags [none] |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
971 |
length: 1052 10.1.1.1 > 10.1.1.2) |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
972 |
ns3::UdpHeader ( |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
973 |
length: 1032 49153 > 9) |
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
974 |
Payload (size=1024) |
6754 | 975 |
|
976 |
Notice that the trace operation is now ``r`` and the simulation time has |
|
977 |
increased to 2.25732 seconds. If you have been following the tutorial steps |
|
978 |
closely this means that you have left the ``DataRate`` of the net devices |
|
979 |
and the channel ``Delay`` set to their default values. This time should |
|
980 |
be familiar as you have seen it before in a previous section. |
|
981 |
||
982 |
The trace source namespace entry (reference 02) has changed to reflect that |
|
983 |
this event is coming from node 1 (``/NodeList/1``) and the packet reception |
|
984 |
trace source (``/MacRx``). It should be quite easy for you to follow the |
|
985 |
progress of the packet through the topology by looking at the rest of the |
|
986 |
traces in the file. |
|
987 |
||
988 |
PCAP Tracing |
|
989 |
++++++++++++ |
|
990 |
The |ns3| device helpers can also be used to create trace files in the |
|
991 |
``.pcap`` format. The acronym pcap (usually written in lower case) stands |
|
992 |
for packet capture, and is actually an API that includes the |
|
993 |
definition of a ``.pcap`` file format. The most popular program that can |
|
994 |
read and display this format is Wireshark (formerly called Ethereal). |
|
995 |
However, there are many traffic trace analyzers that use this packet format. |
|
996 |
We encourage users to exploit the many tools available for analyzing pcap |
|
997 |
traces. In this tutorial, we concentrate on viewing pcap traces with tcpdump. |
|
998 |
||
999 |
The code used to enable pcap tracing is a one-liner. |
|
1000 |
||
1001 |
:: |
|
1002 |
||
1003 |
pointToPoint.EnablePcapAll ("myfirst"); |
|
1004 |
||
1005 |
Go ahead and insert this line of code after the ASCII tracing code we just |
|
1006 |
added to ``scratch/myfirst.cc``. Notice that we only passed the string |
|
1007 |
"myfirst," and not "myfirst.pcap" or something similar. This is because the |
|
1008 |
parameter is a prefix, not a complete file name. The helper will actually |
|
1009 |
create a trace file for every point-to-point device in the simulation. The |
|
1010 |
file names will be built using the prefix, the node number, the device number |
|
1011 |
and a ".pcap" suffix. |
|
1012 |
||
1013 |
In our example script, we will eventually see files named "myfirst-0-0.pcap" |
|
1014 |
and "myfirst-1-0.pcap" which are the pcap traces for node 0-device 0 and |
|
1015 |
node 1-device 0, respectively. |
|
1016 |
||
1017 |
Once you have added the line of code to enable pcap tracing, you can run the |
|
1018 |
script in the usual way: |
|
1019 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
1020 |
.. sourcecode:: bash |
6754 | 1021 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
1022 |
$ ./waf --run scratch/myfirst |
6754 | 1023 |
|
1024 |
If you look at the top level directory of your distribution, you should now |
|
1025 |
see three log files: ``myfirst.tr`` is the ASCII trace file we have |
|
1026 |
previously examined. ``myfirst-0-0.pcap`` and ``myfirst-1-0.pcap`` |
|
1027 |
are the new pcap files we just generated. |
|
1028 |
||
1029 |
Reading output with tcpdump |
|
1030 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1031 |
The easiest thing to do at this point will be to use ``tcpdump`` to look |
|
1032 |
at the ``pcap`` files. |
|
1033 |
||
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
1034 |
.. sourcecode:: bash |
6754 | 1035 |
|
9957
1a4d84a85bad
Manual and Tutorial syntax coloring
Peter D. Barnes, Jr. <barnes26@llnl.gov>
parents:
9104
diff
changeset
|
1036 |
$ tcpdump -nn -tt -r myfirst-0-0.pcap |
6754 | 1037 |
reading from file myfirst-0-0.pcap, link-type PPP (PPP) |
1038 |
2.000000 IP 10.1.1.1.49153 > 10.1.1.2.9: UDP, length 1024 |
|
1039 |
2.514648 IP 10.1.1.2.9 > 10.1.1.1.49153: UDP, length 1024 |
|
1040 |
||
1041 |
tcpdump -nn -tt -r myfirst-1-0.pcap |
|
1042 |
reading from file myfirst-1-0.pcap, link-type PPP (PPP) |
|
1043 |
2.257324 IP 10.1.1.1.49153 > 10.1.1.2.9: UDP, length 1024 |
|
1044 |
2.257324 IP 10.1.1.2.9 > 10.1.1.1.49153: UDP, length 1024 |
|
1045 |
||
1046 |
You can see in the dump of ``myfirst-0-0.pcap`` (the client device) that the |
|
1047 |
echo packet is sent at 2 seconds into the simulation. If you look at the |
|
1048 |
second dump (``myfirst-1-0.pcap``) you can see that packet being received |
|
1049 |
at 2.257324 seconds. You see the packet being echoed back at 2.257324 seconds |
|
1050 |
in the second dump, and finally, you see the packet being received back at |
|
1051 |
the client in the first dump at 2.514648 seconds. |
|
1052 |
||
1053 |
Reading output with Wireshark |
|
1054 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
|
1055 |
If you are unfamiliar with Wireshark, there is a web site available from which |
|
1056 |
you can download programs and documentation: http://www.wireshark.org/. |
|
1057 |
||
1058 |
Wireshark is a graphical user interface which can be used for displaying these |
|
1059 |
trace files. If you have Wireshark available, you can open each of the trace |
|
1060 |
files and display the contents as if you had captured the packets using a |
|
1061 |
*packet sniffer*. |