Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2011 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 | */ |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 16 | |
| 17 | #include "trace.h" |
| 18 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 19 | #include <sys/uio.h> |
| 20 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 21 | #include "class_linker.h" |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 22 | #include "debugger.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 23 | #include "dex_cache.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 24 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 25 | #include "oat/runtime/oat_support_entrypoints.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 26 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 27 | #include "object_utils.h" |
| 28 | #include "os.h" |
Elliott Hughes | 88c5c35 | 2012-03-15 18:49:48 -0700 | [diff] [blame] | 29 | #include "scoped_thread_list_lock.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 30 | #include "thread.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 31 | #include "thread_list.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 32 | |
| 33 | namespace art { |
| 34 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 35 | static const uint32_t kTraceMethodActionMask = 0x03; // two bits |
| 36 | static const char kTraceTokenChar = '*'; |
| 37 | static const uint16_t kTraceHeaderLength = 32; |
| 38 | static const uint32_t kTraceMagicValue = 0x574f4c53; |
| 39 | static const uint16_t kTraceVersionSingleClock = 2; |
| 40 | static const uint16_t kTraceVersionDualClock = 3; |
| 41 | static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2 |
| 42 | static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps |
| 43 | |
| 44 | static inline uint32_t TraceMethodId(uint32_t methodValue) { |
| 45 | return (methodValue & ~kTraceMethodActionMask); |
| 46 | } |
| 47 | static inline uint32_t TraceMethodCombine(uint32_t method, uint8_t traceEvent) { |
| 48 | return (method | traceEvent); |
| 49 | } |
| 50 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 51 | static bool UseThreadCpuClock() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 52 | // TODO: Allow control over which clock is used |
| 53 | return true; |
| 54 | } |
| 55 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 56 | static bool UseWallClock() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 57 | // TODO: Allow control over which clock is used |
| 58 | return true; |
| 59 | } |
| 60 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 61 | static void MeasureClockOverhead() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 62 | if (UseThreadCpuClock()) { |
| 63 | ThreadCpuMicroTime(); |
| 64 | } |
| 65 | if (UseWallClock()) { |
| 66 | MicroTime(); |
| 67 | } |
| 68 | } |
| 69 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 70 | static uint32_t GetClockOverhead() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 71 | uint64_t start = ThreadCpuMicroTime(); |
| 72 | |
| 73 | for (int i = 4000; i > 0; i--) { |
| 74 | MeasureClockOverhead(); |
| 75 | MeasureClockOverhead(); |
| 76 | MeasureClockOverhead(); |
| 77 | MeasureClockOverhead(); |
| 78 | MeasureClockOverhead(); |
| 79 | MeasureClockOverhead(); |
| 80 | MeasureClockOverhead(); |
| 81 | MeasureClockOverhead(); |
| 82 | } |
| 83 | |
| 84 | uint64_t elapsed = ThreadCpuMicroTime() - start; |
| 85 | return uint32_t (elapsed / 32); |
| 86 | } |
| 87 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 88 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 89 | static void Append2LE(uint8_t* buf, uint16_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 90 | *buf++ = (uint8_t) val; |
| 91 | *buf++ = (uint8_t) (val >> 8); |
| 92 | } |
| 93 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 94 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 95 | static void Append4LE(uint8_t* buf, uint32_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 96 | *buf++ = (uint8_t) val; |
| 97 | *buf++ = (uint8_t) (val >> 8); |
| 98 | *buf++ = (uint8_t) (val >> 16); |
| 99 | *buf++ = (uint8_t) (val >> 24); |
| 100 | } |
| 101 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 102 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 103 | static void Append8LE(uint8_t* buf, uint64_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 104 | *buf++ = (uint8_t) val; |
| 105 | *buf++ = (uint8_t) (val >> 8); |
| 106 | *buf++ = (uint8_t) (val >> 16); |
| 107 | *buf++ = (uint8_t) (val >> 24); |
| 108 | *buf++ = (uint8_t) (val >> 32); |
| 109 | *buf++ = (uint8_t) (val >> 40); |
| 110 | *buf++ = (uint8_t) (val >> 48); |
| 111 | *buf++ = (uint8_t) (val >> 56); |
| 112 | } |
| 113 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 114 | static bool InstallStubsClassVisitor(Class* klass, void*) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 115 | Trace* tracer = Runtime::Current()->GetTracer(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 116 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 117 | Method* method = klass->GetDirectMethod(i); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 118 | if (tracer->GetSavedCodeFromMap(method) == NULL) { |
| 119 | tracer->SaveAndUpdateCode(method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | |
| 123 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 124 | Method* method = klass->GetVirtualMethod(i); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 125 | if (tracer->GetSavedCodeFromMap(method) == NULL) { |
| 126 | tracer->SaveAndUpdateCode(method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 129 | return true; |
| 130 | } |
| 131 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 132 | static bool UninstallStubsClassVisitor(Class* klass, void*) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 133 | Trace* tracer = Runtime::Current()->GetTracer(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 134 | for (size_t i = 0; i < klass->NumDirectMethods(); i++) { |
| 135 | Method* method = klass->GetDirectMethod(i); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 136 | if (tracer->GetSavedCodeFromMap(method) != NULL) { |
| 137 | tracer->ResetSavedCode(method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
| 141 | for (size_t i = 0; i < klass->NumVirtualMethods(); i++) { |
| 142 | Method* method = klass->GetVirtualMethod(i); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 143 | if (tracer->GetSavedCodeFromMap(method) != NULL) { |
| 144 | tracer->ResetSavedCode(method); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 145 | } |
| 146 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 147 | return true; |
| 148 | } |
| 149 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 150 | static void TraceRestoreStack(Thread* t, void*) { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 151 | Frame frame = t->GetTopOfStack(); |
| 152 | if (frame.GetSP() != 0) { |
| 153 | for ( ; frame.GetMethod() != 0; frame.Next()) { |
| 154 | if (t->IsTraceStackEmpty()) { |
| 155 | break; |
| 156 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 157 | #if defined(ART_USE_LLVM_COMPILER) |
| 158 | UNIMPLEMENTED(FATAL); |
| 159 | #else |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 160 | uintptr_t pc = frame.GetReturnPC(); |
| 161 | Method* method = frame.GetMethod(); |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 162 | if (IsTraceExitPc(pc)) { |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 163 | TraceStackFrame trace_frame = t->PopTraceStackFrame(); |
| 164 | frame.SetReturnPC(trace_frame.return_pc_); |
| 165 | CHECK(method == trace_frame.method_); |
| 166 | } |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 167 | #endif |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 168 | } |
| 169 | } |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 170 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 171 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 172 | void Trace::AddSavedCodeToMap(const Method* method, const void* code) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 173 | saved_code_map_.Put(method, code); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | void Trace::RemoveSavedCodeFromMap(const Method* method) { |
| 177 | saved_code_map_.erase(method); |
| 178 | } |
| 179 | |
| 180 | const void* Trace::GetSavedCodeFromMap(const Method* method) { |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 181 | typedef SafeMap<const Method*, const void*>::const_iterator It; // TODO: C++0x auto |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 182 | It it = saved_code_map_.find(method); |
| 183 | if (it == saved_code_map_.end()) { |
| 184 | return NULL; |
| 185 | } else { |
| 186 | return it->second; |
| 187 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Elliott Hughes | 1bac54f | 2012-03-16 12:48:31 -0700 | [diff] [blame] | 190 | void Trace::SaveAndUpdateCode(Method* method) { |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 191 | #if defined(ART_USE_LLVM_COMPILER) |
| 192 | UNIMPLEMENTED(FATAL); |
| 193 | #else |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 194 | void* trace_stub = GetLogTraceEntryPoint(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 195 | CHECK(GetSavedCodeFromMap(method) == NULL); |
| 196 | AddSavedCodeToMap(method, method->GetCode()); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 197 | method->SetCode(trace_stub); |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 198 | #endif |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | void Trace::ResetSavedCode(Method* method) { |
| 202 | CHECK(GetSavedCodeFromMap(method) != NULL); |
| 203 | method->SetCode(GetSavedCodeFromMap(method)); |
| 204 | RemoveSavedCodeFromMap(method); |
| 205 | } |
| 206 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 207 | void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 208 | if (Runtime::Current()->IsMethodTracingActive()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 209 | LOG(INFO) << "Trace already in progress, ignoring this request"; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 210 | return; |
| 211 | } |
| 212 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 213 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 214 | Runtime::Current()->GetThreadList()->SuspendAll(false); |
| 215 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 216 | // Open trace file if not going directly to ddms. |
| 217 | File* trace_file = NULL; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 218 | if (!direct_to_ddms) { |
| 219 | if (trace_fd < 0) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 220 | trace_file = OS::OpenFile(trace_filename, true); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 221 | } else { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 222 | trace_file = OS::FileFromFd("tracefile", trace_fd); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 223 | } |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 224 | if (trace_file == NULL) { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 225 | PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 226 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", |
| 227 | StringPrintf("Unable to open trace file '%s'", trace_filename).c_str()); |
| 228 | Runtime::Current()->GetThreadList()->ResumeAll(false); |
| 229 | return; |
| 230 | } |
| 231 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 232 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 233 | // Create Trace object. |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 234 | Trace* tracer(new Trace(trace_file, buffer_size, flags)); |
| 235 | |
| 236 | // Enable count of allocs if specified in the flags. |
| 237 | if ((flags && kTraceCountAllocs) != 0) { |
| 238 | Runtime::Current()->SetStatsEnabled(true); |
| 239 | } |
| 240 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 241 | Runtime::Current()->EnableMethodTracing(tracer); |
| 242 | tracer->BeginTracing(); |
| 243 | |
| 244 | Runtime::Current()->GetThreadList()->ResumeAll(false); |
| 245 | } |
| 246 | |
| 247 | void Trace::Stop() { |
| 248 | if (!Runtime::Current()->IsMethodTracingActive()) { |
| 249 | LOG(INFO) << "Trace stop requested, but no trace currently running"; |
| 250 | return; |
| 251 | } |
| 252 | |
Elliott Hughes | 34e0696 | 2012-04-09 13:55:55 -0700 | [diff] [blame] | 253 | ScopedThreadStateChange tsc(Thread::Current(), kRunnable); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 254 | Runtime::Current()->GetThreadList()->SuspendAll(false); |
| 255 | |
| 256 | Runtime::Current()->GetTracer()->FinishTracing(); |
| 257 | Runtime::Current()->DisableMethodTracing(); |
| 258 | |
| 259 | Runtime::Current()->GetThreadList()->ResumeAll(false); |
| 260 | } |
| 261 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 262 | void Trace::Shutdown() { |
| 263 | if (!Runtime::Current()->IsMethodTracingActive()) { |
| 264 | LOG(INFO) << "Trace shutdown requested, but no trace currently running"; |
| 265 | return; |
| 266 | } |
| 267 | Runtime::Current()->GetTracer()->FinishTracing(); |
| 268 | Runtime::Current()->DisableMethodTracing(); |
| 269 | } |
| 270 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 271 | void Trace::BeginTracing() { |
| 272 | // Set the start time of tracing. |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 273 | start_time_ = MicroTime(); |
| 274 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 275 | // Set trace version and record size. |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 276 | if (UseThreadCpuClock() && UseWallClock()) { |
| 277 | trace_version_ = kTraceVersionDualClock; |
| 278 | record_size_ = kTraceRecordSizeDualClock; |
| 279 | } else { |
| 280 | trace_version_ = kTraceVersionSingleClock; |
| 281 | record_size_ = kTraceRecordSizeSingleClock; |
| 282 | } |
| 283 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 284 | // Set up the beginning of the trace. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 285 | memset(buf_.get(), 0, kTraceHeaderLength); |
| 286 | Append4LE(buf_.get(), kTraceMagicValue); |
| 287 | Append2LE(buf_.get() + 4, trace_version_); |
| 288 | Append2LE(buf_.get() + 6, kTraceHeaderLength); |
| 289 | Append8LE(buf_.get() + 8, start_time_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 290 | if (trace_version_ >= kTraceVersionDualClock) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 291 | Append2LE(buf_.get() + 16, record_size_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 292 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 293 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 294 | // Update current offset. |
| 295 | cur_offset_ = kTraceHeaderLength; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 296 | |
| 297 | // Install all method tracing stubs. |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 298 | InstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 299 | } |
| 300 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 301 | void Trace::FinishTracing() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 302 | // Uninstall all method tracing stubs. |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 303 | UninstallStubs(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 304 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 305 | // Compute elapsed time. |
| 306 | uint64_t elapsed = MicroTime() - start_time_; |
| 307 | |
| 308 | size_t final_offset = cur_offset_; |
| 309 | uint32_t clock_overhead = GetClockOverhead(); |
| 310 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 311 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 312 | Runtime::Current()->SetStatsEnabled(false); |
| 313 | } |
| 314 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 315 | GetVisitedMethods(final_offset); |
| 316 | |
| 317 | std::ostringstream os; |
| 318 | |
| 319 | os << StringPrintf("%cversion\n", kTraceTokenChar); |
| 320 | os << StringPrintf("%d\n", trace_version_); |
| 321 | os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false"); |
| 322 | if (UseThreadCpuClock()) { |
| 323 | if (UseWallClock()) { |
| 324 | os << StringPrintf("clock=dual\n"); |
| 325 | } else { |
| 326 | os << StringPrintf("clock=thread-cpu\n"); |
| 327 | } |
| 328 | } else { |
| 329 | os << StringPrintf("clock=wall\n"); |
| 330 | } |
| 331 | os << StringPrintf("elapsed-time-usec=%llu\n", elapsed); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 332 | os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 333 | os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead); |
| 334 | os << StringPrintf("vm=art\n"); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 335 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 336 | os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS)); |
| 337 | os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES)); |
| 338 | os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS)); |
| 339 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 340 | os << StringPrintf("%cthreads\n", kTraceTokenChar); |
| 341 | DumpThreadList(os); |
| 342 | os << StringPrintf("%cmethods\n", kTraceTokenChar); |
| 343 | DumpMethodList(os); |
| 344 | os << StringPrintf("%cend\n", kTraceTokenChar); |
| 345 | |
| 346 | std::string header(os.str()); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 347 | if (trace_file_.get() == NULL) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 348 | struct iovec iov[2]; |
| 349 | iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str())); |
| 350 | iov[0].iov_len = header.length(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 351 | iov[1].iov_base = buf_.get(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 352 | iov[1].iov_len = final_offset; |
| 353 | Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2); |
| 354 | } else { |
| 355 | if (!trace_file_->WriteFully(header.c_str(), header.length()) || |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 356 | !trace_file_->WriteFully(buf_.get(), final_offset)) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 357 | int err = errno; |
| 358 | LOG(ERROR) << "Trace data write failed: " << strerror(err); |
| 359 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", |
| 360 | StringPrintf("Trace data write failed: %s", strerror(err)).c_str()); |
| 361 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 362 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | void Trace::LogMethodTraceEvent(Thread* self, const Method* method, Trace::TraceEvent event) { |
| 366 | if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) { |
| 367 | uint64_t time = ThreadCpuMicroTime(); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 368 | thread_clock_base_map_.Put(self, time); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | // Advance cur_offset_ atomically. |
| 372 | int32_t new_offset; |
| 373 | int32_t old_offset; |
| 374 | do { |
| 375 | old_offset = cur_offset_; |
| 376 | new_offset = old_offset + record_size_; |
| 377 | if (new_offset > buffer_size_) { |
| 378 | overflow_ = true; |
| 379 | return; |
| 380 | } |
| 381 | } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0); |
| 382 | |
| 383 | uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event); |
| 384 | |
| 385 | // Write data |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 386 | uint8_t* ptr = buf_.get() + old_offset; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 387 | Append2LE(ptr, self->GetTid()); |
| 388 | Append4LE(ptr + 2, method_value); |
| 389 | ptr += 6; |
| 390 | |
| 391 | if (UseThreadCpuClock()) { |
| 392 | uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second; |
| 393 | uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base; |
| 394 | Append4LE(ptr, thread_clock_diff); |
| 395 | ptr += 4; |
| 396 | } |
| 397 | |
| 398 | if (UseWallClock()) { |
| 399 | uint32_t wall_clock_diff = MicroTime() - start_time_; |
| 400 | Append4LE(ptr, wall_clock_diff); |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | void Trace::GetVisitedMethods(size_t end_offset) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 405 | uint8_t* ptr = buf_.get() + kTraceHeaderLength; |
| 406 | uint8_t* end = buf_.get() + end_offset; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 407 | |
| 408 | while (ptr < end) { |
| 409 | uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
| 410 | Method* method = reinterpret_cast<Method*>(TraceMethodId(method_value)); |
| 411 | visited_methods_.insert(method); |
| 412 | ptr += record_size_; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | void Trace::DumpMethodList(std::ostream& os) { |
| 417 | typedef std::set<const Method*>::const_iterator It; // TODO: C++0x auto |
| 418 | for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) { |
| 419 | const Method* method = *it; |
| 420 | MethodHelper mh(method); |
Elliott Hughes | ba8eee1 | 2012-01-24 20:25:24 -0800 | [diff] [blame] | 421 | os << StringPrintf("%p\t%s\t%s\t%s\t%s\t%d\n", method, |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 422 | PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(), |
| 423 | mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile(), |
| 424 | mh.GetLineNumFromNativePC(0)); |
| 425 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | static void DumpThread(Thread* t, void* arg) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 429 | std::ostream& os = *reinterpret_cast<std::ostream*>(arg); |
| 430 | std::string name; |
| 431 | t->GetThreadName(name); |
| 432 | os << t->GetTid() << "\t" << name << "\n"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 433 | } |
| 434 | |
| 435 | void Trace::DumpThreadList(std::ostream& os) { |
| 436 | ScopedThreadListLock thread_list_lock; |
| 437 | Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 438 | } |
| 439 | |
| 440 | void Trace::InstallStubs() { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 441 | Runtime::Current()->GetClassLinker()->VisitClasses(InstallStubsClassVisitor, NULL); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 442 | } |
| 443 | |
| 444 | void Trace::UninstallStubs() { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 445 | Runtime::Current()->GetClassLinker()->VisitClasses(UninstallStubsClassVisitor, NULL); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 446 | |
| 447 | // Restore stacks of all threads |
| 448 | { |
| 449 | ScopedThreadListLock thread_list_lock; |
| 450 | Runtime::Current()->GetThreadList()->ForEach(TraceRestoreStack, NULL); |
| 451 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 452 | } |
| 453 | |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 454 | uint32_t TraceMethodUnwindFromCode(Thread* self) { |
| 455 | Trace* tracer = Runtime::Current()->GetTracer(); |
| 456 | TraceStackFrame trace_frame = self->PopTraceStackFrame(); |
| 457 | Method* method = trace_frame.method_; |
| 458 | uint32_t lr = trace_frame.return_pc_; |
| 459 | |
| 460 | tracer->LogMethodTraceEvent(self, method, Trace::kMethodTraceUnwind); |
| 461 | |
| 462 | return lr; |
| 463 | } |
| 464 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 465 | } // namespace art |