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