Skip to content

Commit 2b056c6

Browse files
labtopiaAndroid (Google) Code Review
authored andcommitted
Merge "Doc update: DDMS Network Traffic tool." into ics-mr1
2 parents ce06c00 + 4099e17 commit 2b056c6

File tree

2 files changed

+79
-6
lines changed

2 files changed

+79
-6
lines changed

docs/html/guide/developing/debugging/ddms.jd

Lines changed: 79 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,19 @@ parent.link=index.html
1111
<li><a href="#running">Running DDMS</a></li>
1212
<li><a href="#how-ddms-works">How DDMS Interacts with a Debugger</a></li>
1313

14-
<li><a href="#using-ddms">Using DDMS</a></li>
14+
<li><a href="#using-ddms">Using DDMS</a>
15+
<ol>
16+
<li><a href="#heap">Viewing heap usage for a process</a></li>
17+
<li><a href="#alloc">Tracking memory allocation of objects</a></li>
18+
<li><a href="#emulator">Working with an emulator or device's file system</a></li>
19+
<li><a href="#thread">Examining thread information</a></li>
20+
<li><a href="#profiling">Starting method profiling</a></li>
21+
<li><a href="#network">Using the Network Traffic tool</a></li>
22+
<li><a href="#logcat">Using LogCat</a></li>
23+
<li><a href="#ops-location">Emulating phone operations and location</a></li>
24+
</ol>
25+
26+
</li>
1527
</ol>
1628
</div>
1729
</div>
@@ -90,7 +102,7 @@ parent.link=index.html
90102
<a href="#running">Running DDMS</a>.
91103

92104

93-
<h3>Viewing heap usage for a process</h3>
105+
<h3 id="heap">Viewing heap usage for a process</h3>
94106

95107
<p>DDMS allows you to view how much heap memory a process is using. This information is useful in
96108
tracking heap usage at a certain point of time during the execution of your application.</p>
@@ -110,7 +122,7 @@ parent.link=index.html
110122
allocated for a particular memory size in bytes.</li>
111123
</ol>
112124

113-
<h3>Tracking memory allocation of objects</h3>
125+
<h3 id="alloc">Tracking memory allocation of objects</h3>
114126

115127
<p>DDMS provides a feature to track objects that are being allocated to memory and to see which
116128
classes and threads are allocating the objects. This allows you to track, in real time, where
@@ -140,7 +152,7 @@ parent.link=index.html
140152
line number of the code that allocated the object.</li>
141153
</ol>
142154

143-
<h3>Working with an emulator or device's file system</h3>
155+
<h3 id="emulator">Working with an emulator or device's file system</h3>
144156

145157
<p>DDMS provides a File Explorer tab that allows you to view, copy, and delete files on the
146158
device. This feature is useful in examining files that are created by your application or if you
@@ -160,7 +172,7 @@ parent.link=index.html
160172
<!-- Need to elaborate more on where things are stored in the file system,
161173
databases, apks, user info, files that are important to look at -->
162174

163-
<h3>Examining thread information</h3>
175+
<h3 id="thread">Examining thread information</h3>
164176

165177
<p>The Threads tab in DDMS shows you the currently running threads for a selected process.</p>
166178

@@ -204,6 +216,67 @@ parent.link=index.html
204216
Profiling</strong>.</li>
205217
</ol>
206218

219+
<h3 id="network">Using the Network Traffic tool</h3>
220+
221+
<p>In Android 4.0, the DDMS (Dalvik Debug Monitor Server) includes a Detailed
222+
Network Usage tab that makes it possible to track when your application is
223+
making network requests. Using this tool, you can monitor how and when your app
224+
transfers data and optimize the underlying code appropriately. You can also
225+
distinguish between different traffic types by applying a “tag” to network
226+
sockets before use.</p>
227+
228+
<p>These tags are shown in a stack area chart in DDMS, as shown in figure 2:</p>
229+
230+
<img src="{@docRoot}images/developing/ddms-network.png" />
231+
<p class="img-caption"><strong>Figure 2.</strong> Network Usage tab.</p>
232+
233+
<p>By monitoring the frequency of your data transfers, and the amount of data
234+
transferred during each connection, you can identify areas of your application
235+
that can be made more battery-efficient. Generally, you should look for
236+
short spikes that can be delayed, or that should cause a later transfer to be
237+
pre-empted. </p>
238+
239+
<p>To better identify the cause of transfer spikes, the
240+
{@link android.net.TrafficStats} API allows you
241+
to tag the data transfers occurring within a thread using {@link
242+
android.net.TrafficStats#setThreadStatsTag setThreadStatsTag()}, followed
243+
by manually tagging (and untagging) individual sockets using {@link
244+
android.net.TrafficStats#tagSocket tagSocket()} and {@link
245+
android.net.TrafficStats#untagSocket untagSocket()}. For example:</p>
246+
247+
<pre>TrafficStats.setThreadStatsTag(0xF00D);
248+
TrafficStats.tagSocket(outputSocket);
249+
// Transfer data using socket
250+
TrafficStats.untagSocket(outputSocket);</pre>
251+
252+
<p>Alternatively, the Apache {@link org.apache.http.client.HttpClient} and
253+
{@link java.net.URLConnection} APIs included in the platform
254+
automatically tag sockets internally based on the active tag (as
255+
identified by
256+
{@link android.net.TrafficStats#getThreadStatsTag getThreadStatsTag()}).
257+
These APIs correctly tag/untag sockets when recycled through
258+
keep-alive pools. In the following example,
259+
{@link android.net.TrafficStats#setThreadStatsTag setThreadStatsTag()}
260+
sets the active tag to be {@code 0xF00D}.
261+
There can only be one active tag per thread.
262+
That is the value that will
263+
be returned by {@link android.net.TrafficStats#getThreadStatsTag getThreadStatsTag()}
264+
and thus used by {@link org.apache.http.client.HttpClient}
265+
to tag sockets. The {@code finally} statement
266+
invokes
267+
{@link android.net.TrafficStats#clearThreadStatsTag clearThreadStatsTag()}
268+
to clear the tag.</p>
269+
270+
<pre>TrafficStats.setThreadStatsTag(0xF00D);
271+
try {
272+
// Make network request using HttpClient.execute()
273+
} finally {
274+
TrafficStats.clearThreadStatsTag();
275+
}</pre>
276+
277+
<p>Socket tagging is supported in Android 4.0, but real-time stats will only be
278+
displayed on devices running Android 4.0.3 or higher.</p>
279+
207280
<h3 id="logcat">Using LogCat</h3>
208281

209282
<p>LogCat is integrated into DDMS, and outputs the messages that you print out using the {@link android.util.Log}
@@ -230,7 +303,7 @@ parent.link=index.html
230303
with the log tags or with the process id that generated the log message. The add filter,
231304
edit filter, and delete filter buttons let you manage your custom filters.</p>
232305

233-
<h3>Emulating phone operations and location</h3>
306+
<h3 id="ops-location">Emulating phone operations and location</h3>
234307
<p>The Emulator control tab lets you simulate a
235308
phone's voice and data network status. This is useful when you want to test your application's
236309
robustness in differing network environments.</p>
85.4 KB
Loading

0 commit comments

Comments
 (0)