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