2 * Copyright (C) 2007 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import org.apache.harmony.dalvik.ddmc.Chunk;
20 import org.apache.harmony.dalvik.ddmc.ChunkHandler;
21 import org.apache.harmony.dalvik.ddmc.DdmServer;
22 import org.apache.harmony.dalvik.ddmc.DdmVmInternal;
23 import android.os.Debug;
24 import android.util.Config;
25 import android.util.Log;
26 import java.io.IOException;
27 import java.nio.ByteBuffer;
30 * Handle native and virtual heap requests.
32 public class DdmHandleHeap extends ChunkHandler {
34 public static final int CHUNK_HPIF = type("HPIF");
35 public static final int CHUNK_HPSG = type("HPSG");
36 public static final int CHUNK_HPDU = type("HPDU");
37 public static final int CHUNK_HPDS = type("HPDS");
38 public static final int CHUNK_NHSG = type("NHSG");
39 public static final int CHUNK_HPGC = type("HPGC");
40 public static final int CHUNK_REAE = type("REAE");
41 public static final int CHUNK_REAQ = type("REAQ");
42 public static final int CHUNK_REAL = type("REAL");
44 private static DdmHandleHeap mInstance = new DdmHandleHeap();
47 /* singleton, do not instantiate */
48 private DdmHandleHeap() {}
51 * Register for the messages we're interested in.
53 public static void register() {
54 DdmServer.registerHandler(CHUNK_HPIF, mInstance);
55 DdmServer.registerHandler(CHUNK_HPSG, mInstance);
56 DdmServer.registerHandler(CHUNK_HPDU, mInstance);
57 DdmServer.registerHandler(CHUNK_HPDS, mInstance);
58 DdmServer.registerHandler(CHUNK_NHSG, mInstance);
59 DdmServer.registerHandler(CHUNK_HPGC, mInstance);
60 DdmServer.registerHandler(CHUNK_REAE, mInstance);
61 DdmServer.registerHandler(CHUNK_REAQ, mInstance);
62 DdmServer.registerHandler(CHUNK_REAL, mInstance);
66 * Called when the DDM server connects. The handler is allowed to
67 * send messages to the server.
69 public void connected() {}
72 * Called when the DDM server disconnects. Can be used to disable
73 * periodic transmissions or clean up saved state.
75 public void disconnected() {}
78 * Handle a chunk of data.
80 public Chunk handleChunk(Chunk request) {
82 Log.v("ddm-heap", "Handling " + name(request.type) + " chunk");
83 int type = request.type;
85 if (type == CHUNK_HPIF) {
86 return handleHPIF(request);
87 } else if (type == CHUNK_HPSG) {
88 return handleHPSGNHSG(request, false);
89 } else if (type == CHUNK_HPDU) {
90 return handleHPDU(request);
91 } else if (type == CHUNK_HPDS) {
92 return handleHPDS(request);
93 } else if (type == CHUNK_NHSG) {
94 return handleHPSGNHSG(request, true);
95 } else if (type == CHUNK_HPGC) {
96 return handleHPGC(request);
97 } else if (type == CHUNK_REAE) {
98 return handleREAE(request);
99 } else if (type == CHUNK_REAQ) {
100 return handleREAQ(request);
101 } else if (type == CHUNK_REAL) {
102 return handleREAL(request);
104 throw new RuntimeException("Unknown packet "
105 + ChunkHandler.name(type));
110 * Handle a "HeaP InFo" request.
112 private Chunk handleHPIF(Chunk request) {
113 ByteBuffer in = wrapChunk(request);
117 Log.v("ddm-heap", "Heap segment enable: when=" + when);
119 boolean ok = DdmVmInternal.heapInfoNotify(when);
121 return createFailChunk(1, "Unsupported HPIF what");
123 return null; // empty response
128 * Handle a "HeaP SeGment" or "Native Heap SeGment" request.
130 private Chunk handleHPSGNHSG(Chunk request, boolean isNative) {
131 ByteBuffer in = wrapChunk(request);
136 Log.v("ddm-heap", "Heap segment enable: when=" + when
137 + ", what=" + what + ", isNative=" + isNative);
139 boolean ok = DdmVmInternal.heapSegmentNotify(when, what, isNative);
141 return createFailChunk(1, "Unsupported HPSG what/when");
143 // TODO: if "when" is non-zero and we want to see a dump
144 // right away, initiate a GC.
145 return null; // empty response
150 * Handle a "HeaP DUmp" request.
152 * This currently just returns a result code. We could pull up
153 * the entire contents of the file and return them, but hprof dump
154 * files can be a few megabytes.
156 private Chunk handleHPDU(Chunk request) {
157 ByteBuffer in = wrapChunk(request);
160 /* get the filename for the output file */
161 int len = in.getInt();
162 String fileName = getString(in, len);
164 Log.d("ddm-heap", "Heap dump: file='" + fileName + "'");
167 Debug.dumpHprofData(fileName);
169 } catch (UnsupportedOperationException uoe) {
170 Log.w("ddm-heap", "hprof dumps not supported in this VM");
172 } catch (IOException ioe) {
174 } catch (RuntimeException re) {
178 /* create a non-empty reply so the handler fires on completion */
179 byte[] reply = { result };
180 return new Chunk(CHUNK_HPDU, reply, 0, reply.length);
184 * Handle a "HeaP Dump Streaming" request.
186 * This tells the VM to create a heap dump and send it directly to
187 * DDMS. The dumps are large enough that we don't want to copy the
188 * data into a byte[] and send it from here.
190 private Chunk handleHPDS(Chunk request) {
191 ByteBuffer in = wrapChunk(request);
194 /* get the filename for the output file */
196 Log.d("ddm-heap", "Heap dump: [DDMS]");
198 String failMsg = null;
200 Debug.dumpHprofDataDdms();
201 } catch (UnsupportedOperationException uoe) {
202 failMsg = "hprof dumps not supported in this VM";
203 } catch (RuntimeException re) {
204 failMsg = "Exception: " + re.getMessage();
207 if (failMsg != null) {
208 Log.w("ddm-heap", failMsg);
209 return createFailChunk(1, failMsg);
216 * Handle a "HeaP Garbage Collection" request.
218 private Chunk handleHPGC(Chunk request) {
219 //ByteBuffer in = wrapChunk(request);
222 Log.d("ddm-heap", "Heap GC request");
225 return null; // empty response
229 * Handle a "REcent Allocation Enable" request.
231 private Chunk handleREAE(Chunk request) {
232 ByteBuffer in = wrapChunk(request);
235 enable = (in.get() != 0);
238 Log.d("ddm-heap", "Recent allocation enable request: " + enable);
240 DdmVmInternal.enableRecentAllocations(enable);
242 return null; // empty response
246 * Handle a "REcent Allocation Query" request.
248 private Chunk handleREAQ(Chunk request) {
249 //ByteBuffer in = wrapChunk(request);
251 byte[] reply = new byte[1];
252 reply[0] = DdmVmInternal.getRecentAllocationStatus() ? (byte)1 :(byte)0;
253 return new Chunk(CHUNK_REAQ, reply, 0, reply.length);
257 * Handle a "REcent ALlocations" request.
259 private Chunk handleREAL(Chunk request) {
260 //ByteBuffer in = wrapChunk(request);
263 Log.d("ddm-heap", "Recent allocations request");
265 /* generate the reply in a ready-to-go format */
266 byte[] reply = DdmVmInternal.getRecentAllocations();
267 return new Chunk(CHUNK_REAL, reply, 0, reply.length);