Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
Andreas Gampe | 277ccbd | 2014-11-03 21:36:10 -0800 | [diff] [blame] | 17 | #include "dalvik_system_VMDebug.h" |
| 18 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 22 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 23 | #include "common_throws.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 24 | #include "debugger.h" |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 25 | #include "gc/space/bump_pointer_space.h" |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 26 | #include "gc/space/dlmalloc_space.h" |
| 27 | #include "gc/space/large_object_space.h" |
| 28 | #include "gc/space/space-inl.h" |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 29 | #include "gc/space/zygote_space.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 30 | #include "hprof/hprof.h" |
| 31 | #include "jni_internal.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 32 | #include "mirror/class.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 33 | #include "ScopedLocalRef.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 34 | #include "ScopedUtfChars.h" |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 35 | #include "scoped_fast_native_object_access.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 36 | #include "trace.h" |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 37 | #include "well_known_classes.h" |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 39 | namespace art { |
| 40 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 41 | static jobjectArray VMDebug_getVmFeatureList(JNIEnv* env, jclass) { |
Ian Rogers | dd157d7 | 2014-05-15 14:47:50 -0700 | [diff] [blame] | 42 | static const char* features[] = { |
| 43 | "method-trace-profiling", |
| 44 | "method-trace-profiling-streaming", |
| 45 | "method-sample-profiling", |
| 46 | "hprof-heap-dump", |
| 47 | "hprof-heap-dump-streaming", |
| 48 | }; |
| 49 | jobjectArray result = env->NewObjectArray(arraysize(features), |
| 50 | WellKnownClasses::java_lang_String, |
| 51 | nullptr); |
| 52 | if (result != nullptr) { |
| 53 | for (size_t i = 0; i < arraysize(features); ++i) { |
| 54 | ScopedLocalRef<jstring> jfeature(env, env->NewStringUTF(features[i])); |
| 55 | if (jfeature.get() == nullptr) { |
| 56 | return nullptr; |
| 57 | } |
| 58 | env->SetObjectArrayElement(result, i, jfeature.get()); |
| 59 | } |
| 60 | } |
| 61 | return result; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 64 | static void VMDebug_startAllocCounting(JNIEnv*, jclass) { |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 65 | Runtime::Current()->SetStatsEnabled(true); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 66 | } |
| 67 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 68 | static void VMDebug_stopAllocCounting(JNIEnv*, jclass) { |
Mathieu Chartier | 9ef78b5 | 2014-09-25 17:03:12 -0700 | [diff] [blame] | 69 | Runtime::Current()->SetStatsEnabled(false); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 72 | static jint VMDebug_getAllocCount(JNIEnv*, jclass, jint kind) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 73 | return Runtime::Current()->GetStat(kind); |
| 74 | } |
| 75 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 76 | static void VMDebug_resetAllocCount(JNIEnv*, jclass, jint kinds) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 77 | Runtime::Current()->ResetStats(kinds); |
| 78 | } |
| 79 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 80 | static void VMDebug_startMethodTracingDdmsImpl(JNIEnv*, jclass, jint bufferSize, jint flags, |
| 81 | jboolean samplingEnabled, jint intervalUs) { |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame^] | 82 | Trace::Start("[DDMS]", -1, bufferSize, flags, Trace::TraceOutputMode::kDDMS, |
| 83 | samplingEnabled ? Trace::TraceMode::kSampling : Trace::TraceMode::kMethodTracing, |
| 84 | intervalUs); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 87 | static void VMDebug_startMethodTracingFd(JNIEnv* env, jclass, jstring javaTraceFilename, |
Jeff Hao | 4044bda | 2014-01-06 15:50:45 -0800 | [diff] [blame] | 88 | jobject javaFd, jint bufferSize, jint flags, |
| 89 | jboolean samplingEnabled, jint intervalUs) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 90 | int originalFd = jniGetFDFromFileDescriptor(env, javaFd); |
| 91 | if (originalFd < 0) { |
| 92 | return; |
| 93 | } |
| 94 | |
| 95 | int fd = dup(originalFd); |
| 96 | if (fd < 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 97 | ScopedObjectAccess soa(env); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 98 | soa.Self()->ThrowNewExceptionF("Ljava/lang/RuntimeException;", |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 99 | "dup(%d) failed: %s", originalFd, strerror(errno)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | ScopedUtfChars traceFilename(env, javaTraceFilename); |
| 104 | if (traceFilename.c_str() == NULL) { |
| 105 | return; |
| 106 | } |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame^] | 107 | Trace::Start(traceFilename.c_str(), fd, bufferSize, flags, Trace::TraceOutputMode::kFile, |
| 108 | samplingEnabled ? Trace::TraceMode::kSampling : Trace::TraceMode::kMethodTracing, |
| 109 | intervalUs); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 112 | static void VMDebug_startMethodTracingFilename(JNIEnv* env, jclass, jstring javaTraceFilename, |
Jeff Hao | 4044bda | 2014-01-06 15:50:45 -0800 | [diff] [blame] | 113 | jint bufferSize, jint flags, |
| 114 | jboolean samplingEnabled, jint intervalUs) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 115 | ScopedUtfChars traceFilename(env, javaTraceFilename); |
| 116 | if (traceFilename.c_str() == NULL) { |
| 117 | return; |
| 118 | } |
Andreas Gampe | 7e7e0f4 | 2015-03-29 15:26:23 -0700 | [diff] [blame^] | 119 | Trace::Start(traceFilename.c_str(), -1, bufferSize, flags, Trace::TraceOutputMode::kFile, |
| 120 | samplingEnabled ? Trace::TraceMode::kSampling : Trace::TraceMode::kMethodTracing, |
| 121 | intervalUs); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 124 | static jint VMDebug_getMethodTracingMode(JNIEnv*, jclass) { |
| 125 | return Trace::GetMethodTracingMode(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 128 | static void VMDebug_stopMethodTracing(JNIEnv*, jclass) { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 129 | Trace::Stop(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 132 | static void VMDebug_startEmulatorTracing(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 133 | UNIMPLEMENTED(WARNING); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 134 | // dvmEmulatorTraceStart(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 135 | } |
| 136 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 137 | static void VMDebug_stopEmulatorTracing(JNIEnv*, jclass) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 138 | UNIMPLEMENTED(WARNING); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 139 | // dvmEmulatorTraceStop(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 140 | } |
| 141 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 142 | static jboolean VMDebug_isDebuggerConnected(JNIEnv*, jclass) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 143 | return Dbg::IsDebuggerActive(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 146 | static jboolean VMDebug_isDebuggingEnabled(JNIEnv*, jclass) { |
Elliott Hughes | c0f0933 | 2012-03-26 13:27:06 -0700 | [diff] [blame] | 147 | return Dbg::IsJdwpConfigured(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 150 | static jlong VMDebug_lastDebuggerActivity(JNIEnv*, jclass) { |
Elliott Hughes | 872d4ec | 2011-10-21 17:07:15 -0700 | [diff] [blame] | 151 | return Dbg::LastDebuggerActivity(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 154 | static void ThrowUnsupportedOperationException(JNIEnv* env) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 155 | ScopedObjectAccess soa(env); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 156 | soa.Self()->ThrowNewException("Ljava/lang/UnsupportedOperationException;", NULL); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static void VMDebug_startInstructionCounting(JNIEnv* env, jclass) { |
| 160 | ThrowUnsupportedOperationException(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 163 | static void VMDebug_stopInstructionCounting(JNIEnv* env, jclass) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 164 | ThrowUnsupportedOperationException(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 167 | static void VMDebug_getInstructionCount(JNIEnv* env, jclass, jintArray /*javaCounts*/) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 168 | ThrowUnsupportedOperationException(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 171 | static void VMDebug_resetInstructionCount(JNIEnv* env, jclass) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 172 | ThrowUnsupportedOperationException(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 173 | } |
| 174 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 175 | static void VMDebug_printLoadedClasses(JNIEnv* env, jclass, jint flags) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 176 | ScopedFastNativeObjectAccess soa(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 177 | return Runtime::Current()->GetClassLinker()->DumpAllClasses(flags); |
| 178 | } |
| 179 | |
Ian Rogers | 7dfb28c | 2013-08-22 08:18:36 -0700 | [diff] [blame] | 180 | static jint VMDebug_getLoadedClassCount(JNIEnv* env, jclass) { |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 181 | ScopedFastNativeObjectAccess soa(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 182 | return Runtime::Current()->GetClassLinker()->NumLoadedClasses(); |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Returns the thread-specific CPU-time clock value for the current thread, |
| 187 | * or -1 if the feature isn't supported. |
| 188 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 189 | static jlong VMDebug_threadCpuTimeNanos(JNIEnv*, jclass) { |
| 190 | return ThreadCpuNanoTime(); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | /* |
| 194 | * static void dumpHprofData(String fileName, FileDescriptor fd) |
| 195 | * |
| 196 | * Cause "hprof" data to be dumped. We can throw an IOException if an |
| 197 | * error occurs during file handling. |
| 198 | */ |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 199 | static void VMDebug_dumpHprofData(JNIEnv* env, jclass, jstring javaFilename, jobject javaFd) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 200 | // Only one of these may be NULL. |
| 201 | if (javaFilename == NULL && javaFd == NULL) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 202 | ScopedObjectAccess soa(env); |
Nicolas Geoffray | 0aa50ce | 2015-03-10 11:03:29 +0000 | [diff] [blame] | 203 | ThrowNullPointerException("fileName == null && fd == null"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 204 | return; |
| 205 | } |
| 206 | |
| 207 | std::string filename; |
| 208 | if (javaFilename != NULL) { |
| 209 | ScopedUtfChars chars(env, javaFilename); |
| 210 | if (env->ExceptionCheck()) { |
| 211 | return; |
| 212 | } |
| 213 | filename = chars.c_str(); |
| 214 | } else { |
| 215 | filename = "[fd]"; |
| 216 | } |
| 217 | |
| 218 | int fd = -1; |
| 219 | if (javaFd != NULL) { |
| 220 | fd = jniGetFDFromFileDescriptor(env, javaFd); |
| 221 | if (fd < 0) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 222 | ScopedObjectAccess soa(env); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 223 | ThrowRuntimeException("Invalid file descriptor"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | } |
| 227 | |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 228 | hprof::DumpHeap(filename.c_str(), fd, false); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 231 | static void VMDebug_dumpHprofDataDdms(JNIEnv*, jclass) { |
Elliott Hughes | 622a698 | 2012-06-08 17:58:54 -0700 | [diff] [blame] | 232 | hprof::DumpHeap("[DDMS]", -1, true); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 233 | } |
| 234 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 235 | static void VMDebug_dumpReferenceTables(JNIEnv* env, jclass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 236 | ScopedObjectAccess soa(env); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 237 | LOG(INFO) << "--- reference table dump ---"; |
| 238 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 239 | soa.Env()->DumpReferenceTables(LOG(INFO)); |
| 240 | soa.Vm()->DumpReferenceTables(LOG(INFO)); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 241 | |
| 242 | LOG(INFO) << "---"; |
| 243 | } |
| 244 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 245 | static void VMDebug_crash(JNIEnv*, jclass) { |
Elliott Hughes | 81ff318 | 2012-03-23 20:35:56 -0700 | [diff] [blame] | 246 | LOG(FATAL) << "Crashing runtime on request"; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 249 | static void VMDebug_infopoint(JNIEnv*, jclass, jint id) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 250 | LOG(INFO) << "VMDebug infopoint " << id << " hit"; |
| 251 | } |
| 252 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 253 | static jlong VMDebug_countInstancesOfClass(JNIEnv* env, jclass, jclass javaClass, |
| 254 | jboolean countAssignable) { |
| 255 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 256 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 83c8ee0 | 2014-01-28 14:50:23 -0800 | [diff] [blame] | 257 | // We only want reachable instances, so do a GC. Heap::VisitObjects visits all of the heap |
| 258 | // objects in the all spaces and the allocation stack. |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 259 | heap->CollectGarbage(false); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 260 | mirror::Class* c = soa.Decode<mirror::Class*>(javaClass); |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 261 | if (c == nullptr) { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 262 | return 0; |
| 263 | } |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 264 | std::vector<mirror::Class*> classes; |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 265 | classes.push_back(c); |
| 266 | uint64_t count = 0; |
Mathieu Chartier | 412c7fc | 2014-02-07 12:18:39 -0800 | [diff] [blame] | 267 | heap->CountInstances(classes, countAssignable, &count); |
Elliott Hughes | ec0f83d | 2013-01-15 16:54:08 -0800 | [diff] [blame] | 268 | return count; |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 269 | } |
| 270 | |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 271 | // We export the VM internal per-heap-space size/alloc/free metrics |
| 272 | // for the zygote space, alloc space (application heap), and the large |
| 273 | // object space for dumpsys meminfo. The other memory region data such |
| 274 | // as PSS, private/shared dirty/shared data are available via |
| 275 | // /proc/<pid>/smaps. |
| 276 | static void VMDebug_getHeapSpaceStats(JNIEnv* env, jclass, jlongArray data) { |
Ian Rogers | 6b99dd1 | 2013-07-25 12:11:32 -0700 | [diff] [blame] | 277 | jlong* arr = reinterpret_cast<jlong*>(env->GetPrimitiveArrayCritical(data, 0)); |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 278 | if (arr == nullptr || env->GetArrayLength(data) < 9) { |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 279 | return; |
| 280 | } |
| 281 | |
| 282 | size_t allocSize = 0; |
| 283 | size_t allocUsed = 0; |
| 284 | size_t zygoteSize = 0; |
| 285 | size_t zygoteUsed = 0; |
| 286 | size_t largeObjectsSize = 0; |
| 287 | size_t largeObjectsUsed = 0; |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 288 | gc::Heap* heap = Runtime::Current()->GetHeap(); |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 289 | for (gc::space::ContinuousSpace* space : heap->GetContinuousSpaces()) { |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 290 | if (space->IsImageSpace()) { |
| 291 | // Currently don't include the image space. |
| 292 | } else if (space->IsZygoteSpace()) { |
Mathieu Chartier | 1f3b535 | 2014-02-03 14:00:42 -0800 | [diff] [blame] | 293 | gc::space::ZygoteSpace* zygote_space = space->AsZygoteSpace(); |
| 294 | zygoteSize += zygote_space->Size(); |
| 295 | zygoteUsed += zygote_space->GetBytesAllocated(); |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 296 | } else if (space->IsMallocSpace()) { |
| 297 | // This is a malloc space. |
Hiroshi Yamauchi | cf58d4a | 2013-09-26 14:21:22 -0700 | [diff] [blame] | 298 | gc::space::MallocSpace* malloc_space = space->AsMallocSpace(); |
| 299 | allocSize += malloc_space->GetFootprint(); |
| 300 | allocUsed += malloc_space->GetBytesAllocated(); |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 301 | } else if (space->IsBumpPointerSpace()) { |
Mathieu Chartier | 692fafd | 2013-11-29 17:24:40 -0800 | [diff] [blame] | 302 | ScopedObjectAccess soa(env); |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 303 | gc::space::BumpPointerSpace* bump_pointer_space = space->AsBumpPointerSpace(); |
| 304 | allocSize += bump_pointer_space->Size(); |
| 305 | allocUsed += bump_pointer_space->GetBytesAllocated(); |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 306 | } |
| 307 | } |
Mathieu Chartier | 7410f29 | 2013-11-24 13:17:35 -0800 | [diff] [blame] | 308 | for (gc::space::DiscontinuousSpace* space : heap->GetDiscontinuousSpaces()) { |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 309 | if (space->IsLargeObjectSpace()) { |
| 310 | largeObjectsSize += space->AsLargeObjectSpace()->GetBytesAllocated(); |
| 311 | largeObjectsUsed += largeObjectsSize; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | size_t allocFree = allocSize - allocUsed; |
| 316 | size_t zygoteFree = zygoteSize - zygoteUsed; |
| 317 | size_t largeObjectsFree = largeObjectsSize - largeObjectsUsed; |
| 318 | |
| 319 | int j = 0; |
| 320 | arr[j++] = allocSize; |
| 321 | arr[j++] = allocUsed; |
| 322 | arr[j++] = allocFree; |
| 323 | arr[j++] = zygoteSize; |
| 324 | arr[j++] = zygoteUsed; |
| 325 | arr[j++] = zygoteFree; |
| 326 | arr[j++] = largeObjectsSize; |
| 327 | arr[j++] = largeObjectsUsed; |
| 328 | arr[j++] = largeObjectsFree; |
| 329 | env->ReleasePrimitiveArrayCritical(data, arr, 0); |
| 330 | } |
| 331 | |
Elliott Hughes | 0512f02 | 2012-03-15 22:10:52 -0700 | [diff] [blame] | 332 | static JNINativeMethod gMethods[] = { |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 333 | NATIVE_METHOD(VMDebug, countInstancesOfClass, "(Ljava/lang/Class;Z)J"), |
| 334 | NATIVE_METHOD(VMDebug, crash, "()V"), |
| 335 | NATIVE_METHOD(VMDebug, dumpHprofData, "(Ljava/lang/String;Ljava/io/FileDescriptor;)V"), |
| 336 | NATIVE_METHOD(VMDebug, dumpHprofDataDdms, "()V"), |
| 337 | NATIVE_METHOD(VMDebug, dumpReferenceTables, "()V"), |
| 338 | NATIVE_METHOD(VMDebug, getAllocCount, "(I)I"), |
Hiroshi Yamauchi | 09b07a9 | 2013-07-15 13:17:06 -0700 | [diff] [blame] | 339 | NATIVE_METHOD(VMDebug, getHeapSpaceStats, "([J)V"), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 340 | NATIVE_METHOD(VMDebug, getInstructionCount, "([I)V"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 341 | NATIVE_METHOD(VMDebug, getLoadedClassCount, "!()I"), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 342 | NATIVE_METHOD(VMDebug, getVmFeatureList, "()[Ljava/lang/String;"), |
| 343 | NATIVE_METHOD(VMDebug, infopoint, "(I)V"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 344 | NATIVE_METHOD(VMDebug, isDebuggerConnected, "!()Z"), |
| 345 | NATIVE_METHOD(VMDebug, isDebuggingEnabled, "!()Z"), |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 346 | NATIVE_METHOD(VMDebug, getMethodTracingMode, "()I"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 347 | NATIVE_METHOD(VMDebug, lastDebuggerActivity, "!()J"), |
| 348 | NATIVE_METHOD(VMDebug, printLoadedClasses, "!(I)V"), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 349 | NATIVE_METHOD(VMDebug, resetAllocCount, "(I)V"), |
| 350 | NATIVE_METHOD(VMDebug, resetInstructionCount, "()V"), |
| 351 | NATIVE_METHOD(VMDebug, startAllocCounting, "()V"), |
| 352 | NATIVE_METHOD(VMDebug, startEmulatorTracing, "()V"), |
| 353 | NATIVE_METHOD(VMDebug, startInstructionCounting, "()V"), |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 354 | NATIVE_METHOD(VMDebug, startMethodTracingDdmsImpl, "(IIZI)V"), |
Jeff Hao | 4044bda | 2014-01-06 15:50:45 -0800 | [diff] [blame] | 355 | NATIVE_METHOD(VMDebug, startMethodTracingFd, "(Ljava/lang/String;Ljava/io/FileDescriptor;IIZI)V"), |
| 356 | NATIVE_METHOD(VMDebug, startMethodTracingFilename, "(Ljava/lang/String;IIZI)V"), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 357 | NATIVE_METHOD(VMDebug, stopAllocCounting, "()V"), |
| 358 | NATIVE_METHOD(VMDebug, stopEmulatorTracing, "()V"), |
| 359 | NATIVE_METHOD(VMDebug, stopInstructionCounting, "()V"), |
| 360 | NATIVE_METHOD(VMDebug, stopMethodTracing, "()V"), |
Ian Rogers | 53b8b09 | 2014-03-13 23:45:53 -0700 | [diff] [blame] | 361 | NATIVE_METHOD(VMDebug, threadCpuTimeNanos, "!()J"), |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 362 | }; |
| 363 | |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 364 | void register_dalvik_system_VMDebug(JNIEnv* env) { |
Elliott Hughes | eac7667 | 2012-05-24 21:56:51 -0700 | [diff] [blame] | 365 | REGISTER_NATIVE_METHODS("dalvik/system/VMDebug"); |
Elliott Hughes | 9d5ccec | 2011-09-19 13:19:50 -0700 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | } // namespace art |