~statik/ubuntu/maverick/erlang/erlang-merge-testing

« back to all changes in this revision

Viewing changes to lib/et/doc/src/et_examples.xml

  • Committer: Bazaar Package Importer
  • Author(s): Sergei Golovan
  • Date: 2009-05-01 10:14:38 UTC
  • mfrom: (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20090501101438-6qlr6rsdxgyzrg2z
Tags: 1:13.b-dfsg-2
* Cleaned up patches: removed unneeded patch which helped to support
  different SCTP library versions, made sure that changes for m68k
  architecture applied only when building on this architecture.
* Removed duplicated information from binary packages descriptions.
* Don't require libsctp-dev build-dependency on solaris-i386 architecture
  which allows to build Erlang on Nexenta (thanks to Tim Spriggs for
  the suggestion).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?xml version="1.0" encoding="latin1" ?>
 
2
<!DOCTYPE chapter SYSTEM "chapter.dtd">
 
3
 
 
4
<chapter>
 
5
  <header>
 
6
    <copyright>
 
7
      <year>2002</year><year>2009</year>
 
8
      <holder>Ericsson AB. All Rights Reserved.</holder>
 
9
    </copyright>
 
10
    <legalnotice>
 
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/.
 
16
    
 
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
 
20
      under the License.
 
21
    
 
22
    </legalnotice>
 
23
 
 
24
    <title>Examples</title>
 
25
    <prepared>H&aring;kan Mattsson</prepared>
 
26
    <responsible>H&aring;kan Mattsson</responsible>
 
27
    <docno></docno>
 
28
    <approved>H&aring;kan Mattsson</approved>
 
29
    <checked></checked>
 
30
    <date></date>
 
31
    <rev>%VSN%</rev>
 
32
  </header>
 
33
 
 
34
  <section>
 
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:
 
38
      </p>
 
39
    <p></p>
 
40
    <codeinclude file="../../examples/et_demo.erl" tag="%sim_trans" type="erl"></codeinclude>
 
41
    <p></p>
 
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>
 
46
    <p></p>
 
47
    <code type="none">
 
48
         mnesia:transaction(fun() -> mnesia:write({my_tab, key, val}) end).
 
49
    </code>
 
50
    <p>And the viewer window will look like:</p>
 
51
    <p></p>
 
52
    <code type="none"><![CDATA[
 
53
          $ erl -pa ../examples
 
54
          Erlang (BEAM) emulator version 2002.10.08 [source]
 
55
 
 
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>}}
 
59
          2>
 
60
    ]]></code>
 
61
    <p></p>
 
62
    <image file="sim_trans">
 
63
      <icaption>A simulated Mnesia transaction which writes one record</icaption>
 
64
    </image>
 
65
  </section>
 
66
 
 
67
  <section>
 
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>
 
76
    <p></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>
 
80
    <p></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
 
85
      is skipped:</p>
 
86
    <p></p>
 
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>
 
92
    <p></p>
 
93
    <codeinclude file="../../examples/et_demo.erl" tag="%start" type="erl"></codeinclude>
 
94
    <p>A simple one-liner starts the tool:</p>
 
95
    <code type="none">
 
96
          erl -pa ../examples -s et_demo
 
97
    </code>
 
98
    <p>The filters are included by the following parameters:</p>
 
99
    <p></p>
 
100
    <codeinclude file="../../examples/et_demo.erl" tag="%filters" type="erl"></codeinclude>
 
101
  </section>
 
102
 
 
103
  <section>
 
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
 
114
      same node:</p>
 
115
    <p></p>
 
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>
 
122
    <p></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>
 
125
    <p></p>
 
126
    <code type="none">
 
127
       erl -pa ../examples -s et_demo live_trans
 
128
       Erlang (BEAM) emulator version 2002.10.08 [source]
 
129
 
 
130
       Eshell V2002.10.08  (abort with ^G)
 
131
       1>
 
132
    </code>
 
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>
 
135
    <p></p>
 
136
    <image file="live_trans">
 
137
      <icaption>A real Mnesia transaction which writes one record</icaption>
 
138
    </image>
 
139
  </section>
 
140
 
 
141
  <section>
 
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
 
147
      standard.</p>
 
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
 
151
      simple manner.</p>
 
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>
 
155
    <p></p>
 
156
    <code type="none">
 
157
       -module(megaco_filter).
 
158
       -export([start/0]).
 
159
 
 
160
       start() ->
 
161
           Options =
 
162
               [{event_order, event_ts},
 
163
                {scale, 3},
 
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).
 
171
    </code>
 
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
 
176
      in the viewer.</p>
 
177
    <p></p>
 
178
    <code type="none">
 
179
          erl -sname observer -s megaco_filter
 
180
          Erlang (BEAM) emulator version 2002.10.08 [source]
 
181
 
 
182
          Eshell V2002.10.08  (abort with ^G)
 
183
          (observer@amrod)1> et_viewer: search for: [] ++ ["gateway_tt"]
 
184
    </code>
 
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>
 
189
    <p></p>
 
190
    <code type="none">
 
191
          erl -sname mgc -pa ../../megaco/examples/simple
 
192
          Erlang (BEAM) emulator version 2002.10.08 [source]
 
193
 
 
194
          Eshell V2002.10.08  (abort with ^G)
 
195
          (mgc@amrod)1> net:ping(observer@amrod). 
 
196
          pong
 
197
          (mgc@amrod)2> megaco:start().
 
198
          ok
 
199
          (mgc@amrod)3> megaco_simple_mgc:start().
 
200
          {ok,[{ok,2944,
 
201
                   {megaco_receive_handle,{deviceName,"controller"},
 
202
                                          megaco_pretty_text_encoder,
 
203
                                          [],
 
204
                                          megaco_tcp}},
 
205
               {ok,2944,
 
206
                   {megaco_receive_handle,{deviceName,"controller"},
 
207
                                          megaco_pretty_text_encoder,
 
208
                                          [],
 
209
                                          megaco_udp}},
 
210
               {ok,2945,
 
211
                   {megaco_receive_handle,{deviceName,"controller"},
 
212
                                          megaco_binary_encoder,
 
213
                                          [],
 
214
                                          megaco_tcp}},
 
215
               {ok,2945,
 
216
                   {megaco_receive_handle,{deviceName,"controller"},
 
217
                                          megaco_binary_encoder,
 
218
                                          [],
 
219
                                          megaco_udp}}]}
 
220
          (mgc@amrod)4> 
 
221
    </code>
 
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>
 
229
    <p></p>
 
230
    <code type="none">
 
231
          erl -sname mg -pa ../../megaco/examples/simple
 
232
          Erlang (BEAM) emulator version 2002.10.08 [source]
 
233
 
 
234
          Eshell V2002.10.08  (abort with ^G)
 
235
          (mg@amrod)1> net:ping(observer@amrod). 
 
236
          pong
 
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().
 
243
          ok
 
244
          (mg@amrod)4>  megaco_simple_mg:start().
 
245
          [{{deviceName,"gateway_tt"},
 
246
            {1,
 
247
             {ok,[{'ActionReply',0,
 
248
                                 asn1_NOVALUE,
 
249
                                 asn1_NOVALUE,
 
250
                                 [{serviceChangeReply,
 
251
                                      {'ServiceChangeReply',
 
252
                                          [{megaco_term_id,false,["root"]}],
 
253
                                          {serviceChangeResParms,
 
254
                                              {'ServiceChangeResParm',
 
255
                                                  {deviceName|...},
 
256
                                                  asn1_NOVALUE|...}}}}]}]}}},
 
257
           {{deviceName,"gateway_tb"},
 
258
            {1,
 
259
             {ok,[{'ActionReply',0,
 
260
                                 asn1_NOVALUE,
 
261
                                 asn1_NOVALUE,
 
262
                                 [{serviceChangeReply,
 
263
                                      {'ServiceChangeReply',
 
264
                                          [{megaco_term_id,false,["root"]}],
 
265
                                          {serviceChangeResParms,
 
266
                                              {'ServiceChangeResParm',
 
267
                                                  {...}|...}}}}]}]}}},
 
268
           {{deviceName,"gateway_ut"},
 
269
            {1,
 
270
             {ok,[{'ActionReply',0,
 
271
                                 asn1_NOVALUE,
 
272
                                 asn1_NOVALUE,
 
273
                                 [{serviceChangeReply,
 
274
                                      {'ServiceChangeReply',
 
275
                                          [{megaco_term_id,false,["root"]}],
 
276
                                          {serviceChangeResParms,
 
277
                                              {'ServiceChangeResParm',{...}|...}}}}]}]}}},
 
278
           {{deviceName,"gateway_ub"},
 
279
            {1,
 
280
             {ok,[{'ActionReply',0,
 
281
                                 asn1_NOVALUE,
 
282
                                 asn1_NOVALUE,
 
283
                                 [{serviceChangeReply,
 
284
                                      {'ServiceChangeReply',
 
285
                                          [{megaco_term_id,false,["root"]}],
 
286
                                          {serviceChangeResParms,
 
287
                                              {'ServiceChangeResParm'|...}}}}]}]}}}]
 
288
          (mg@amrod)5> 
 
289
    </code>
 
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>
 
293
    <p></p>
 
294
    <image file="megaco_tracer">
 
295
      <icaption>The viewer adopted for Megaco</icaption>
 
296
    </image>
 
297
    <p>A pretty printed Megaco message looks like this:</p>
 
298
    <p></p>
 
299
    <image file="megaco_filter">
 
300
      <icaption>A textual Megaco message</icaption>
 
301
    </image>
 
302
    <p>And the corresponding internal form for the same Megaco message
 
303
      looks like this:</p>
 
304
    <p></p>
 
305
    <image file="megaco_collector">
 
306
      <icaption>The internal form of a Megaco message</icaption>
 
307
    </image>
 
308
  </section>
 
309
</chapter>
 
310