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