The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 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 |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 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. |
| 15 | */ |
| 16 | |
| 17 | package android.ddm; |
| 18 | |
| 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 android.os.Debug; |
| 23 | import android.util.Config; |
| 24 | import android.util.Log; |
| 25 | import java.io.IOException; |
| 26 | import java.nio.ByteBuffer; |
| 27 | |
| 28 | /** |
| 29 | * Handle profiling requests. |
| 30 | */ |
| 31 | public class DdmHandleProfiling extends ChunkHandler { |
| 32 | |
| 33 | public static final int CHUNK_MPRS = type("MPRS"); |
| 34 | public static final int CHUNK_MPRE = type("MPRE"); |
Andy McFadden | 72a20db0c | 2010-01-22 12:20:41 -0800 | [diff] [blame] | 35 | public static final int CHUNK_MPSS = type("MPSS"); |
| 36 | public static final int CHUNK_MPSE = type("MPSE"); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 37 | public static final int CHUNK_MPRQ = type("MPRQ"); |
| 38 | |
| 39 | private static DdmHandleProfiling mInstance = new DdmHandleProfiling(); |
| 40 | |
| 41 | |
| 42 | /* singleton, do not instantiate */ |
| 43 | private DdmHandleProfiling() {} |
| 44 | |
| 45 | /** |
| 46 | * Register for the messages we're interested in. |
| 47 | */ |
| 48 | public static void register() { |
| 49 | DdmServer.registerHandler(CHUNK_MPRS, mInstance); |
| 50 | DdmServer.registerHandler(CHUNK_MPRE, mInstance); |
Andy McFadden | 72a20db0c | 2010-01-22 12:20:41 -0800 | [diff] [blame] | 51 | DdmServer.registerHandler(CHUNK_MPSS, mInstance); |
| 52 | DdmServer.registerHandler(CHUNK_MPSE, mInstance); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 53 | DdmServer.registerHandler(CHUNK_MPRQ, mInstance); |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Called when the DDM server connects. The handler is allowed to |
| 58 | * send messages to the server. |
| 59 | */ |
| 60 | public void connected() {} |
| 61 | |
| 62 | /** |
| 63 | * Called when the DDM server disconnects. Can be used to disable |
| 64 | * periodic transmissions or clean up saved state. |
| 65 | */ |
| 66 | public void disconnected() {} |
| 67 | |
| 68 | /** |
| 69 | * Handle a chunk of data. |
| 70 | */ |
| 71 | public Chunk handleChunk(Chunk request) { |
| 72 | if (Config.LOGV) |
| 73 | Log.v("ddm-heap", "Handling " + name(request.type) + " chunk"); |
| 74 | int type = request.type; |
| 75 | |
| 76 | if (type == CHUNK_MPRS) { |
| 77 | return handleMPRS(request); |
| 78 | } else if (type == CHUNK_MPRE) { |
| 79 | return handleMPRE(request); |
Andy McFadden | 72a20db0c | 2010-01-22 12:20:41 -0800 | [diff] [blame] | 80 | } else if (type == CHUNK_MPSS) { |
| 81 | return handleMPSS(request); |
| 82 | } else if (type == CHUNK_MPSE) { |
| 83 | return handleMPSE(request); |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 84 | } else if (type == CHUNK_MPRQ) { |
| 85 | return handleMPRQ(request); |
| 86 | } else { |
| 87 | throw new RuntimeException("Unknown packet " |
| 88 | + ChunkHandler.name(type)); |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Handle a "Method PRofiling Start" request. |
| 94 | */ |
| 95 | private Chunk handleMPRS(Chunk request) { |
| 96 | ByteBuffer in = wrapChunk(request); |
| 97 | |
| 98 | int bufferSize = in.getInt(); |
| 99 | int flags = in.getInt(); |
| 100 | int len = in.getInt(); |
| 101 | String fileName = getString(in, len); |
| 102 | if (Config.LOGV) |
| 103 | Log.v("ddm-heap", "Method profiling start: filename='" + fileName |
| 104 | + "', size=" + bufferSize + ", flags=" + flags); |
| 105 | |
| 106 | try { |
| 107 | Debug.startMethodTracing(fileName, bufferSize, flags); |
| 108 | return null; // empty response |
| 109 | } catch (RuntimeException re) { |
| 110 | return createFailChunk(1, re.getMessage()); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Handle a "Method PRofiling End" request. |
| 116 | */ |
| 117 | private Chunk handleMPRE(Chunk request) { |
| 118 | byte result; |
| 119 | |
| 120 | try { |
| 121 | Debug.stopMethodTracing(); |
| 122 | result = 0; |
| 123 | } catch (RuntimeException re) { |
| 124 | Log.w("ddm-heap", "Method profiling end failed: " |
| 125 | + re.getMessage()); |
| 126 | result = 1; |
| 127 | } |
| 128 | |
| 129 | /* create a non-empty reply so the handler fires on completion */ |
| 130 | byte[] reply = { result }; |
| 131 | return new Chunk(CHUNK_MPRE, reply, 0, reply.length); |
| 132 | } |
| 133 | |
| 134 | /* |
Andy McFadden | 72a20db0c | 2010-01-22 12:20:41 -0800 | [diff] [blame] | 135 | * Handle a "Method Profiling w/Streaming Start" request. |
| 136 | */ |
| 137 | private Chunk handleMPSS(Chunk request) { |
| 138 | ByteBuffer in = wrapChunk(request); |
| 139 | |
| 140 | int bufferSize = in.getInt(); |
| 141 | int flags = in.getInt(); |
| 142 | if (Config.LOGV) { |
| 143 | Log.v("ddm-heap", "Method prof stream start: size=" + bufferSize |
| 144 | + ", flags=" + flags); |
| 145 | } |
| 146 | |
| 147 | try { |
| 148 | Debug.startMethodTracingDdms(bufferSize, flags); |
| 149 | return null; // empty response |
| 150 | } catch (RuntimeException re) { |
| 151 | return createFailChunk(1, re.getMessage()); |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * Handle a "Method Profiling w/Streaming End" request. |
| 157 | */ |
| 158 | private Chunk handleMPSE(Chunk request) { |
| 159 | byte result; |
| 160 | |
| 161 | if (Config.LOGV) { |
| 162 | Log.v("ddm-heap", "Method prof stream end"); |
| 163 | } |
| 164 | |
| 165 | try { |
| 166 | Debug.stopMethodTracing(); |
| 167 | result = 0; |
| 168 | } catch (RuntimeException re) { |
| 169 | Log.w("ddm-heap", "Method prof stream end failed: " |
| 170 | + re.getMessage()); |
| 171 | return createFailChunk(1, re.getMessage()); |
| 172 | } |
| 173 | |
| 174 | /* VM sent the (perhaps very large) response directly */ |
| 175 | return null; |
| 176 | } |
| 177 | |
| 178 | /* |
The Android Open Source Project | 7b0b1ed | 2009-03-18 22:20:26 -0700 | [diff] [blame] | 179 | * Handle a "Method PRofiling Query" request. |
| 180 | */ |
| 181 | private Chunk handleMPRQ(Chunk request) { |
| 182 | int result = Debug.isMethodTracingActive() ? 1 : 0; |
| 183 | |
| 184 | /* create a non-empty reply so the handler fires on completion */ |
| 185 | byte[] reply = { (byte) result }; |
| 186 | return new Chunk(CHUNK_MPRQ, reply, 0, reply.length); |
| 187 | } |
| 188 | } |
| 189 | |