1
<?xml version="1.0" encoding="latin1" ?>
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
7
<year>2002</year><year>2009</year>
8
<holder>Ericsson AB. All Rights Reserved.</holder>
11
The contents of this file are subject to the Erlang Public License,
12
Version 1.1, (the "License"); you may not use this file except in
13
compliance with the License. You should have received a copy of the
14
Erlang Public License along with this software. If not, it can be
15
retrieved online at http://www.erlang.org/.
17
Software distributed under the License is distributed on an "AS IS"
18
basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
19
the License for the specific language governing rights and limitations
24
<title>Examples</title>
25
<prepared>Håkan Mattsson</prepared>
26
<responsible>Håkan Mattsson</responsible>
28
<approved>Håkan Mattsson</approved>
32
<file>et_examples.xml</file>
36
<title>A simulated Mnesia transaction</title>
37
<p>The Erlang code for running the simulated Mnesia transaction example
38
in the previous chapter is included in the <c>et/examples/et_demo.erl</c> file:
41
<codeinclude file="../../examples/et_demo.erl" tag="%sim_trans" type="erl"></codeinclude>
43
<codeinclude file="../../examples/et_demo.erl" tag="%mgr_actors" type="erl"></codeinclude>
44
<p>If you invoke the <c>et_demo:sim_trans()</c> function a viewer
45
window will pop up and the sequence trace will be almost the same as
46
if the following Mnesia transaction would have been run:</p>
49
mnesia:transaction(fun() -> mnesia:write({my_tab, key, val}) end).
51
<p>And the viewer window will look like:</p>
53
<code type="none"><![CDATA[
55
Erlang (BEAM) emulator version 2002.10.08 [source]
57
Eshell V2002.10.08 (abort with ^G)
58
1> et_demo:sim_trans().
59
{ok,{table_handle,<0.30.0>,11,trace_ts,#Fun<et_collector.0.83904657>}}
63
<image file="sim_trans.gif">
64
<icaption>A simulated Mnesia transaction which writes one record</icaption>
69
<title>Some convenient functions used in the Mnesia transaction example</title>
70
<p>The <c>module_as_actor</c> filter converts the event-records so
71
the module names becomes actors and the invoked functions becomes
72
labels. If the information about who the caller was it will be
73
displayed as an arrow directed from the caller to the callee. The
74
<c>[{message, {caller}}, {return_trace}]</c> options to <c>dbg:tpl/2</c>
75
function will imply the necessary information in the Erlang traces.
76
Here follows the <c>module_as_actor</c> filter:</p>
78
<codeinclude file="../../examples/et_demo.erl" tag="%module_as_actor" type="erl"></codeinclude>
79
<p>The <c>plain_process_info</c> filter does not alter the event-records.
80
It merely ensures that the event not related to processes are skipped:</p>
82
<codeinclude file="../../examples/et_demo.erl" tag="%plain_process_info" type="erl"></codeinclude>
83
<p>The <c>plain_process_info_nolink</c> filter does not alter the
84
event-records. It do makes use of the <c>plain_process_info</c> , but
85
do also ensure that the process info related to linking and unlinking
88
<codeinclude file="../../examples/et_demo.erl" tag="%plain_process_info_nolink" type="erl"></codeinclude>
89
<p>In order to simplify the startup of an <c>et_viewer</c> process
90
with the filters mentioned above, plus some others (that also are
91
found in <c>et/examples/et_demo.erl</c> src/et_collector.erl the
92
<c>et_demo:start/0,1</c> functions can be used:</p>
94
<codeinclude file="../../examples/et_demo.erl" tag="%start" type="erl"></codeinclude>
95
<p>A simple one-liner starts the tool:</p>
97
erl -pa ../examples -s et_demo
99
<p>The filters are included by the following parameters:</p>
101
<codeinclude file="../../examples/et_demo.erl" tag="%filters" type="erl"></codeinclude>
105
<title>Erlang trace of a Mnesia transaction</title>
106
<p>The following piece of code <c>et_demo:trace_mnesia/0</c>
107
activates call tracing of both local and external function calls for
108
all modules in the Mnesia application. The call traces are configured
109
cover all processes (both existing and those that are spawned in the
110
future) and include timestamps for trace data. It do also activate
111
tracing of process related events for Mnesia's static processes plus
112
the calling process (that is your shell). Please, observe that the
113
<c>whereis/1</c> call in the following code requires that both the
114
traced Mnesia application and the <c>et_viewer</c>is running on the
117
<codeinclude file="../../examples/et_demo.erl" tag="%trace_mnesia" type="erl"></codeinclude>
118
<p>The <c>et_demo:live_trans/0</c> function starts the a global
119
controller, starts a viewer, starts Mnesia, creates a local table,
120
activates tracing (as described above) and registers the shell
121
process is as 'my_shell' for clarity. Finally the a simple Mnesia
122
transaction that writes a single record is run:</p>
124
<codeinclude file="../../examples/et_demo.erl" tag="%live_trans" type="erl"></codeinclude>
125
<p>Now we run the <c>et_demo:live_trans/0</c> function:</p>
128
erl -pa ../examples -s et_demo live_trans
129
Erlang (BEAM) emulator version 2002.10.08 [source]
131
Eshell V2002.10.08 (abort with ^G)
134
<p>Please, explore the different filters in order to see how the traced
135
transaction can be seen from different point of views:</p>
137
<image file="live_trans.gif">
138
<icaption>A real Mnesia transaction which writes one record</icaption>
143
<title>Erlang trace of Megaco startup</title>
144
<p>The Event Tracer (ET) tool was initially written in order to
145
demonstrate how messages where sent over the Megaco protocol. This
146
were back in the old days before the standard bodies of IETF and ITU
147
had approved Megaco (also called H.248) as an international
149
<p>In the Megaco application of Erlang/OTP, the code is carefully
150
instrumented with calls to <c>et:report_event/5</c>. For call a detail
151
level is set in order to dynamically control the trace level in a
153
<p>The <c>megaco_filter</c> module implements a customized filter
154
for Megaco messages. It does also make use of <c>trace_global</c>
155
combined with usage of the <c>trace_pattern</c>:</p>
158
-module(megaco_filter).
163
[{event_order, event_ts},
165
{max_actors, infinity},
166
{trace_pattern, {megaco, max}},
167
{trace_global, true},
168
{dict_insert, {filter, megaco_filter}, fun filter/1},
169
{active_filter, megaco_filter},
170
{title, "Megaco tracer - Erlang/OTP"}],
171
et_viewer:start(Options).
173
<p>First we start an Erlang node with the a global collector and
174
its viewer. The <c>et_viewer: search for: [] ++ ["gateway_tt"]</c>
175
printout is caused by a click on the "gateway_tt" actor name in the
176
viewer. It means that only events with that actor will be displayed
180
erl -sname observer -s megaco_filter
181
Erlang (BEAM) emulator version 2002.10.08 [source]
183
Eshell V2002.10.08 (abort with ^G)
184
(observer@amrod)1> et_viewer: search for: [] ++ ["gateway_tt"]
186
<p>Secondly we start another Erlang node which we connect the
187
observer node, before we start the application that we want to
188
trace. In this case we start a Media Gateway Controller that listens
189
for both TCP and UDP on the text and binary ports for Megaco:</p>
192
erl -sname mgc -pa ../../megaco/examples/simple
193
Erlang (BEAM) emulator version 2002.10.08 [source]
195
Eshell V2002.10.08 (abort with ^G)
196
(mgc@amrod)1> net:ping(observer@amrod).
198
(mgc@amrod)2> megaco:start().
200
(mgc@amrod)3> megaco_simple_mgc:start().
202
{megaco_receive_handle,{deviceName,"controller"},
203
megaco_pretty_text_encoder,
207
{megaco_receive_handle,{deviceName,"controller"},
208
megaco_pretty_text_encoder,
212
{megaco_receive_handle,{deviceName,"controller"},
213
megaco_binary_encoder,
217
{megaco_receive_handle,{deviceName,"controller"},
218
megaco_binary_encoder,
223
<p>And finally we start an Erlang node for the Media Gateways and
224
connect to the observer node. Each Media Gateway connects to the
225
controller and sends an initial Service Change message. The controller
226
accepts the gateways and sends a reply to each one using the same
227
transport mechanism and message encoding according to the preference
228
of each gateway. That is all combinations of TCP/IP transport, UDP/IP
229
transport, text encoding and ASN.1 BER encoding:</p>
232
erl -sname mg -pa ../../megaco/examples/simple
233
Erlang (BEAM) emulator version 2002.10.08 [source]
235
Eshell V2002.10.08 (abort with ^G)
236
(mg@amrod)1> net:ping(observer@amrod).
238
(mg@amrod)2> megaco_simple_mg:start().
239
[{{deviceName,"gateway_tt"},{error,{start_user,megaco_not_started}}},
240
{{deviceName,"gateway_tb"},{error,{start_user,megaco_not_started}}},
241
{{deviceName,"gateway_ut"},{error,{start_user,megaco_not_started}}},
242
{{deviceName,"gateway_ub"},{error,{start_user,megaco_not_started}}}]
243
(mg@amrod)3> megaco:start().
245
(mg@amrod)4> megaco_simple_mg:start().
246
[{{deviceName,"gateway_tt"},
248
{ok,[{'ActionReply',0,
251
[{serviceChangeReply,
252
{'ServiceChangeReply',
253
[{megaco_term_id,false,["root"]}],
254
{serviceChangeResParms,
255
{'ServiceChangeResParm',
257
asn1_NOVALUE|...}}}}]}]}}},
258
{{deviceName,"gateway_tb"},
260
{ok,[{'ActionReply',0,
263
[{serviceChangeReply,
264
{'ServiceChangeReply',
265
[{megaco_term_id,false,["root"]}],
266
{serviceChangeResParms,
267
{'ServiceChangeResParm',
269
{{deviceName,"gateway_ut"},
271
{ok,[{'ActionReply',0,
274
[{serviceChangeReply,
275
{'ServiceChangeReply',
276
[{megaco_term_id,false,["root"]}],
277
{serviceChangeResParms,
278
{'ServiceChangeResParm',{...}|...}}}}]}]}}},
279
{{deviceName,"gateway_ub"},
281
{ok,[{'ActionReply',0,
284
[{serviceChangeReply,
285
{'ServiceChangeReply',
286
[{megaco_term_id,false,["root"]}],
287
{serviceChangeResParms,
288
{'ServiceChangeResParm'|...}}}}]}]}}}]
291
<p>The Megaco adopted viewer looks like this, when we have clicked
292
on the "gateway_tt" actor name in order to only display the events
293
regarding that actor:</p>
295
<image file="megaco_tracer.gif">
296
<icaption>The viewer adopted for Megaco</icaption>
298
<p>A pretty printed Megaco message looks like this:</p>
300
<image file="megaco_filter.gif">
301
<icaption>A textual Megaco message</icaption>
303
<p>And the corresponding internal form for the same Megaco message
306
<image file="megaco_collector.gif">
307
<icaption>The internal form of a Megaco message</icaption>