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>
35
<title>A simulated Mnesia transaction</title>
36
<p>The Erlang code for running the simulated Mnesia transaction example
37
in the previous chapter is included in the <c>et/examples/et_demo.erl</c> file:
40
<codeinclude file="../../examples/et_demo.erl" tag="%sim_trans" type="erl"></codeinclude>
42
<codeinclude file="../../examples/et_demo.erl" tag="%mgr_actors" type="erl"></codeinclude>
43
<p>If you invoke the <c>et_demo:sim_trans()</c> function a viewer
44
window will pop up and the sequence trace will be almost the same as
45
if the following Mnesia transaction would have been run:</p>
48
mnesia:transaction(fun() -> mnesia:write({my_tab, key, val}) end).
50
<p>And the viewer window will look like:</p>
52
<code type="none"><![CDATA[
54
Erlang (BEAM) emulator version 2002.10.08 [source]
56
Eshell V2002.10.08 (abort with ^G)
57
1> et_demo:sim_trans().
58
{ok,{table_handle,<0.30.0>,11,trace_ts,#Fun<et_collector.0.83904657>}}
62
<image file="sim_trans">
63
<icaption>A simulated Mnesia transaction which writes one record</icaption>
68
<title>Some convenient functions used in the Mnesia transaction example</title>
69
<p>The <c>module_as_actor</c> filter converts the event-records so
70
the module names becomes actors and the invoked functions becomes
71
labels. If the information about who the caller was it will be
72
displayed as an arrow directed from the caller to the callee. The
73
<c>[{message, {caller}}, {return_trace}]</c> options to <c>dbg:tpl/2</c>
74
function will imply the neccessary information in the Erlang traces.
75
Here follows the <c>module_as_actor</c> filter:</p>
77
<codeinclude file="../../examples/et_demo.erl" tag="%module_as_actor" type="erl"></codeinclude>
78
<p>The <c>plain_process_info</c> filter does not alter the event-records.
79
It merely ensures that the event not related to processes are skipped:</p>
81
<codeinclude file="../../examples/et_demo.erl" tag="%plain_process_info" type="erl"></codeinclude>
82
<p>The <c>plain_process_info_nolink</c> filter does not alter the
83
event-records. It do makes use of the <c>plain_process_info</c> , but
84
do also ensure that the process info related to linking and unlinking
87
<codeinclude file="../../examples/et_demo.erl" tag="%plain_process_info_nolink" type="erl"></codeinclude>
88
<p>In order to simplify the startup of an <c>et_viewer</c> process
89
with the filters mentioned above, plus some others (that also are
90
found in <c>et/examples/et_demo.erl</c> src/et_collector.erl the
91
<c>et_demo:start/0,1</c> functions can be used:</p>
93
<codeinclude file="../../examples/et_demo.erl" tag="%start" type="erl"></codeinclude>
94
<p>A simple one-liner starts the tool:</p>
96
erl -pa ../examples -s et_demo
98
<p>The filters are included by the following parameters:</p>
100
<codeinclude file="../../examples/et_demo.erl" tag="%filters" type="erl"></codeinclude>
104
<title>Erlang trace of a Mnesia transaction</title>
105
<p>The following piece of code <c>et_demo:trace_mnesia/0</c>
106
activates call tracing of both local and external function calls for
107
all modules in the Mnesia application. The call traces are configured
108
cover all processes (both existing and those that are spawned in the
109
future) and include timestamps for trace data. It do also activate
110
tracing of process related events for Mnesia's static processes plus
111
the calling process (that is your shell). Please, observe that the
112
<c>whereis/1</c> call in the following code requires that both the
113
traced Mnesia application and the <c>et_viewer</c>is running on the
116
<codeinclude file="../../examples/et_demo.erl" tag="%trace_mnesia" type="erl"></codeinclude>
117
<p>The <c>et_demo:live_trans/0</c> function starts the a global
118
controller, starts a viewer, starts Mnesia, creates a local table,
119
activates tracing (as described above) and registeres the shell
120
process is as 'my_shell' for clarity. Finally the a simple Mnesia
121
transaction that writes a single record is run:</p>
123
<codeinclude file="../../examples/et_demo.erl" tag="%live_trans" type="erl"></codeinclude>
124
<p>Now we run the <c>et_demo:live_trans/0</c> function:</p>
127
erl -pa ../examples -s et_demo live_trans
128
Erlang (BEAM) emulator version 2002.10.08 [source]
130
Eshell V2002.10.08 (abort with ^G)
133
<p>Please, explore the different filters in order to see how the traced
134
transaction can be seen from different point of views:</p>
136
<image file="live_trans">
137
<icaption>A real Mnesia transaction which writes one record</icaption>
142
<title>Erlang trace of Megaco startup</title>
143
<p>The Event Tracer (ET) tool was initially written in order to
144
demonstrate how messages where sent over the Megaco protocol. This
145
were back in the old days before the standard bodies of IETF and ITU
146
had approved Megaco (also called H.248) as an international
148
<p>In the Megaco application of Erlang/OTP, the code is carefully
149
instrumented with calls to <c>et:report_event/5</c>. For call a detail
150
level is set in order to dynamically control the trace level in a
152
<p>The <c>megaco_filter</c> module implements a customized filter
153
for Megaco messages. It does also make use of <c>trace_global</c>
154
combined with usage of the <c>trace_pattern</c>:</p>
157
-module(megaco_filter).
162
[{event_order, event_ts},
164
{max_actors, infinity},
165
{trace_pattern, {megaco, max}},
166
{trace_global, true},
167
{dict_insert, {filter, megaco_filter}, fun filter/1},
168
{active_filter, megaco_filter},
169
{title, "Megaco tracer - Erlang/OTP"}],
170
et_viewer:start(Options).
172
<p>First we start an Erlang node with the a global collecor and
173
its viewer. The <c>et_viewer: search for: [] ++ ["gateway_tt"]</c>
174
printout is caused by a click on the "gateway_tt" actor name in the
175
viewer. It means that only events with that actor will be displayed
179
erl -sname observer -s megaco_filter
180
Erlang (BEAM) emulator version 2002.10.08 [source]
182
Eshell V2002.10.08 (abort with ^G)
183
(observer@amrod)1> et_viewer: search for: [] ++ ["gateway_tt"]
185
<p>Secondly we start another Erlang node which we connect the
186
observer node, before we start the application that we want to
187
trace. In this case we start a Media Gateway Controller that listens
188
for both TCP and UDP on the text and binary ports for Megaco:</p>
191
erl -sname mgc -pa ../../megaco/examples/simple
192
Erlang (BEAM) emulator version 2002.10.08 [source]
194
Eshell V2002.10.08 (abort with ^G)
195
(mgc@amrod)1> net:ping(observer@amrod).
197
(mgc@amrod)2> megaco:start().
199
(mgc@amrod)3> megaco_simple_mgc:start().
201
{megaco_receive_handle,{deviceName,"controller"},
202
megaco_pretty_text_encoder,
206
{megaco_receive_handle,{deviceName,"controller"},
207
megaco_pretty_text_encoder,
211
{megaco_receive_handle,{deviceName,"controller"},
212
megaco_binary_encoder,
216
{megaco_receive_handle,{deviceName,"controller"},
217
megaco_binary_encoder,
222
<p>And finally we start an Erlang node for the Media Gateways and
223
connect to the observer node. Each Media Gateway connects to the
224
controller and sends an inital Service Change message. The controller
225
accepts the gateways and sends a reply to each one using the same
226
transport mechanism and message encoding according to the preference
227
of each gateway. That is all combinations of TCP/IP transport, UDP/IP
228
transport, text encoding and ASN.1 BER encoding:</p>
231
erl -sname mg -pa ../../megaco/examples/simple
232
Erlang (BEAM) emulator version 2002.10.08 [source]
234
Eshell V2002.10.08 (abort with ^G)
235
(mg@amrod)1> net:ping(observer@amrod).
237
(mg@amrod)2> megaco_simple_mg:start().
238
[{{deviceName,"gateway_tt"},{error,{start_user,megaco_not_started}}},
239
{{deviceName,"gateway_tb"},{error,{start_user,megaco_not_started}}},
240
{{deviceName,"gateway_ut"},{error,{start_user,megaco_not_started}}},
241
{{deviceName,"gateway_ub"},{error,{start_user,megaco_not_started}}}]
242
(mg@amrod)3> megaco:start().
244
(mg@amrod)4> megaco_simple_mg:start().
245
[{{deviceName,"gateway_tt"},
247
{ok,[{'ActionReply',0,
250
[{serviceChangeReply,
251
{'ServiceChangeReply',
252
[{megaco_term_id,false,["root"]}],
253
{serviceChangeResParms,
254
{'ServiceChangeResParm',
256
asn1_NOVALUE|...}}}}]}]}}},
257
{{deviceName,"gateway_tb"},
259
{ok,[{'ActionReply',0,
262
[{serviceChangeReply,
263
{'ServiceChangeReply',
264
[{megaco_term_id,false,["root"]}],
265
{serviceChangeResParms,
266
{'ServiceChangeResParm',
268
{{deviceName,"gateway_ut"},
270
{ok,[{'ActionReply',0,
273
[{serviceChangeReply,
274
{'ServiceChangeReply',
275
[{megaco_term_id,false,["root"]}],
276
{serviceChangeResParms,
277
{'ServiceChangeResParm',{...}|...}}}}]}]}}},
278
{{deviceName,"gateway_ub"},
280
{ok,[{'ActionReply',0,
283
[{serviceChangeReply,
284
{'ServiceChangeReply',
285
[{megaco_term_id,false,["root"]}],
286
{serviceChangeResParms,
287
{'ServiceChangeResParm'|...}}}}]}]}}}]
290
<p>The Megaco adopted viewer looks like this, when we have clicked
291
on the "gateway_tt" actor name in order to only display the events
292
regarding that actor:</p>
294
<image file="megaco_tracer">
295
<icaption>The viewer adopted for Megaco</icaption>
297
<p>A pretty printed Megaco message looks like this:</p>
299
<image file="megaco_filter">
300
<icaption>A textual Megaco message</icaption>
302
<p>And the corresponding internal form for the same Megaco message
305
<image file="megaco_collector">
306
<icaption>The internal form of a Megaco message</icaption>