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