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 | |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 21 | #include "base/unix_file/fd_file.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 22 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 23 | #include "common_throws.h" |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 24 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 25 | #include "dex_file-inl.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 26 | #include "instrumentation.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 27 | #include "mirror/abstract_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 28 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 29 | #include "mirror/dex_cache.h" |
| 30 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 31 | #include "mirror/object-inl.h" |
Ian Rogers | c928de9 | 2013-02-27 14:30:44 -0800 | [diff] [blame] | 32 | #if !defined(ART_USE_PORTABLE_COMPILER) |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 33 | #include "oat/runtime/oat_support_entrypoints.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 34 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 35 | #include "object_utils.h" |
| 36 | #include "os.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 37 | #include "scoped_thread_state_change.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 38 | #include "thread.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 39 | #include "thread_list.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 40 | |
| 41 | namespace art { |
| 42 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 43 | // File format: |
| 44 | // header |
| 45 | // record 0 |
| 46 | // record 1 |
| 47 | // ... |
| 48 | // |
| 49 | // Header format: |
| 50 | // u4 magic ('SLOW') |
| 51 | // u2 version |
| 52 | // u2 offset to data |
| 53 | // u8 start date/time in usec |
| 54 | // u2 record size in bytes (version >= 2 only) |
| 55 | // ... padding to 32 bytes |
| 56 | // |
| 57 | // Record format v1: |
| 58 | // u1 thread ID |
| 59 | // u4 method ID | method action |
| 60 | // u4 time delta since start, in usec |
| 61 | // |
| 62 | // Record format v2: |
| 63 | // u2 thread ID |
| 64 | // u4 method ID | method action |
| 65 | // u4 time delta since start, in usec |
| 66 | // |
| 67 | // Record format v3: |
| 68 | // u2 thread ID |
| 69 | // u4 method ID | method action |
| 70 | // u4 time delta since start, in usec |
| 71 | // u4 wall time since start, in usec (when clock == "dual" only) |
| 72 | // |
| 73 | // 32 bits of microseconds is 70 minutes. |
| 74 | // |
| 75 | // All values are stored in little-endian order. |
| 76 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 77 | enum TraceAction { |
| 78 | kTraceMethodEnter = 0x00, // method entry |
| 79 | kTraceMethodExit = 0x01, // method exit |
| 80 | kTraceUnroll = 0x02, // method exited by exception unrolling |
| 81 | // 0x03 currently unused |
| 82 | kTraceMethodActionMask = 0x03, // two bits |
| 83 | }; |
| 84 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 85 | static const char kTraceTokenChar = '*'; |
| 86 | static const uint16_t kTraceHeaderLength = 32; |
| 87 | static const uint32_t kTraceMagicValue = 0x574f4c53; |
| 88 | static const uint16_t kTraceVersionSingleClock = 2; |
| 89 | static const uint16_t kTraceVersionDualClock = 3; |
| 90 | static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2 |
| 91 | static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps |
| 92 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 93 | #if defined(HAVE_POSIX_CLOCKS) |
| 94 | ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceDual; |
| 95 | #else |
| 96 | ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceWall; |
| 97 | #endif |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 98 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 99 | Trace* Trace::the_trace_ = NULL; |
| 100 | |
| 101 | static mirror::AbstractMethod* DecodeTraceMethodId(uint32_t tmid) { |
| 102 | return reinterpret_cast<mirror::AbstractMethod*>(tmid & ~kTraceMethodActionMask); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 103 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 105 | static TraceAction DecodeTraceAction(uint32_t tmid) { |
| 106 | return static_cast<TraceAction>(tmid & kTraceMethodActionMask); |
| 107 | } |
| 108 | |
| 109 | static uint32_t EncodeTraceMethodAndAction(const mirror::AbstractMethod* method, |
| 110 | TraceAction action) { |
| 111 | uint32_t tmid = reinterpret_cast<uint32_t>(method) | action; |
| 112 | DCHECK_EQ(method, DecodeTraceMethodId(tmid)); |
| 113 | return tmid; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 116 | void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 117 | #if defined(HAVE_POSIX_CLOCKS) |
| 118 | default_clock_source_ = clock_source; |
| 119 | #else |
| 120 | if (clock_source != kProfilerClockSourceWall) { |
| 121 | LOG(WARNING) << "Ignoring tracing request to use "; |
| 122 | } |
| 123 | #endif |
| 124 | } |
| 125 | |
| 126 | static uint16_t GetTraceVersion(ProfilerClockSource clock_source) { |
| 127 | return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock |
| 128 | : kTraceVersionSingleClock; |
| 129 | } |
| 130 | |
| 131 | static uint16_t GetRecordSize(ProfilerClockSource clock_source) { |
| 132 | return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock |
| 133 | : kTraceRecordSizeSingleClock; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 134 | } |
| 135 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 136 | bool Trace::UseThreadCpuClock() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 137 | return (clock_source_ == kProfilerClockSourceThreadCpu) || |
| 138 | (clock_source_ == kProfilerClockSourceDual); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 139 | } |
| 140 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 141 | bool Trace::UseWallClock() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 142 | return (clock_source_ == kProfilerClockSourceWall) || |
| 143 | (clock_source_ == kProfilerClockSourceDual); |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | static void MeasureClockOverhead(Trace* trace) { |
| 147 | if (trace->UseThreadCpuClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 148 | ThreadCpuMicroTime(); |
| 149 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 150 | if (trace->UseWallClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 151 | MicroTime(); |
| 152 | } |
| 153 | } |
| 154 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 155 | static uint32_t GetClockOverhead(Trace* trace) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 156 | uint64_t start = ThreadCpuMicroTime(); |
| 157 | |
| 158 | for (int i = 4000; i > 0; i--) { |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 159 | MeasureClockOverhead(trace); |
| 160 | MeasureClockOverhead(trace); |
| 161 | MeasureClockOverhead(trace); |
| 162 | MeasureClockOverhead(trace); |
| 163 | MeasureClockOverhead(trace); |
| 164 | MeasureClockOverhead(trace); |
| 165 | MeasureClockOverhead(trace); |
| 166 | MeasureClockOverhead(trace); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | uint64_t elapsed = ThreadCpuMicroTime() - start; |
| 170 | return uint32_t (elapsed / 32); |
| 171 | } |
| 172 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 173 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 174 | static void Append2LE(uint8_t* buf, uint16_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 175 | *buf++ = (uint8_t) val; |
| 176 | *buf++ = (uint8_t) (val >> 8); |
| 177 | } |
| 178 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 179 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 180 | static void Append4LE(uint8_t* buf, uint32_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 181 | *buf++ = (uint8_t) val; |
| 182 | *buf++ = (uint8_t) (val >> 8); |
| 183 | *buf++ = (uint8_t) (val >> 16); |
| 184 | *buf++ = (uint8_t) (val >> 24); |
| 185 | } |
| 186 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 187 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 188 | static void Append8LE(uint8_t* buf, uint64_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 189 | *buf++ = (uint8_t) val; |
| 190 | *buf++ = (uint8_t) (val >> 8); |
| 191 | *buf++ = (uint8_t) (val >> 16); |
| 192 | *buf++ = (uint8_t) (val >> 24); |
| 193 | *buf++ = (uint8_t) (val >> 32); |
| 194 | *buf++ = (uint8_t) (val >> 40); |
| 195 | *buf++ = (uint8_t) (val >> 48); |
| 196 | *buf++ = (uint8_t) (val >> 56); |
| 197 | } |
| 198 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 199 | void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, |
| 200 | bool direct_to_ddms) { |
| 201 | Thread* self = Thread::Current(); |
| 202 | { |
| 203 | MutexLock mu(self, *Locks::trace_lock_); |
| 204 | if (the_trace_ != NULL) { |
| 205 | LOG(ERROR) << "Trace already in progress, ignoring this request"; |
| 206 | return; |
| 207 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 208 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 209 | Runtime* runtime = Runtime::Current(); |
| 210 | runtime->GetThreadList()->SuspendAll(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 211 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 212 | // Open trace file if not going directly to ddms. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 213 | UniquePtr<File> trace_file; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 214 | if (!direct_to_ddms) { |
| 215 | if (trace_fd < 0) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 216 | trace_file.reset(OS::OpenFile(trace_filename, true)); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 217 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 218 | trace_file.reset(new File(trace_fd, "tracefile")); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 219 | trace_file->DisableAutoClose(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 220 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 221 | if (trace_file.get() == NULL) { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 222 | PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 223 | runtime->GetThreadList()->ResumeAll(); |
| 224 | ScopedObjectAccess soa(self); |
| 225 | ThrowRuntimeException("Unable to open trace file '%s'", trace_filename); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 226 | return; |
| 227 | } |
| 228 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 229 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 230 | // Create Trace object. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 231 | { |
| 232 | MutexLock mu(self, *Locks::trace_lock_); |
| 233 | if(the_trace_ != NULL) { |
| 234 | LOG(ERROR) << "Trace already in progress, ignoring this request"; |
| 235 | } else { |
| 236 | the_trace_ = new Trace(trace_file.release(), buffer_size, flags); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 237 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 238 | // Enable count of allocs if specified in the flags. |
| 239 | if ((flags && kTraceCountAllocs) != 0) { |
| 240 | runtime->SetStatsEnabled(true); |
| 241 | } |
| 242 | |
| 243 | runtime->GetInstrumentation()->AddListener(the_trace_, |
| 244 | instrumentation::Instrumentation::kMethodEntered | |
| 245 | instrumentation::Instrumentation::kMethodExited | |
| 246 | instrumentation::Instrumentation::kMethodUnwind); |
| 247 | } |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 248 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 249 | runtime->GetThreadList()->ResumeAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | void Trace::Stop() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 253 | Runtime* runtime = Runtime::Current(); |
| 254 | runtime->GetThreadList()->SuspendAll(); |
| 255 | Trace* the_trace = NULL; |
| 256 | { |
| 257 | MutexLock mu(Thread::Current(), *Locks::trace_lock_); |
| 258 | if (the_trace_ == NULL) { |
| 259 | LOG(ERROR) << "Trace stop requested, but no trace currently running"; |
| 260 | } else { |
| 261 | the_trace = the_trace_; |
| 262 | the_trace_ = NULL; |
| 263 | } |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 264 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 265 | if (the_trace != NULL) { |
| 266 | the_trace->FinishTracing(); |
| 267 | runtime->GetInstrumentation()->RemoveListener(the_trace, |
| 268 | instrumentation::Instrumentation::kMethodEntered | |
| 269 | instrumentation::Instrumentation::kMethodExited | |
| 270 | instrumentation::Instrumentation::kMethodUnwind); |
| 271 | delete the_trace; |
| 272 | } |
| 273 | runtime->GetThreadList()->ResumeAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 274 | } |
| 275 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 276 | void Trace::Shutdown() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 277 | if (IsMethodTracingActive()) { |
| 278 | Stop(); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 279 | } |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 280 | } |
| 281 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 282 | bool Trace::IsMethodTracingActive() { |
| 283 | MutexLock mu(Thread::Current(), *Locks::trace_lock_); |
| 284 | return the_trace_ != NULL; |
| 285 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 286 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 287 | Trace::Trace(File* trace_file, int buffer_size, int flags) |
| 288 | : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags), |
| 289 | clock_source_(default_clock_source_), buffer_size_(buffer_size), start_time_(MicroTime()), |
| 290 | cur_offset_(0), overflow_(false) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 291 | // Set up the beginning of the trace. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 292 | uint16_t trace_version = GetTraceVersion(clock_source_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 293 | memset(buf_.get(), 0, kTraceHeaderLength); |
| 294 | Append4LE(buf_.get(), kTraceMagicValue); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 295 | Append2LE(buf_.get() + 4, trace_version); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 296 | Append2LE(buf_.get() + 6, kTraceHeaderLength); |
| 297 | Append8LE(buf_.get() + 8, start_time_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 298 | if (trace_version >= kTraceVersionDualClock) { |
| 299 | uint16_t record_size = GetRecordSize(clock_source_); |
| 300 | Append2LE(buf_.get() + 16, record_size); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 301 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 302 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 303 | // Update current offset. |
| 304 | cur_offset_ = kTraceHeaderLength; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 305 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 306 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 307 | static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source) |
| 308 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 309 | uint8_t* ptr = buf + kTraceHeaderLength; |
| 310 | uint8_t* end = buf + buf_size; |
| 311 | |
| 312 | while (ptr < end) { |
| 313 | uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
| 314 | mirror::AbstractMethod* method = DecodeTraceMethodId(tmid); |
| 315 | TraceAction action = DecodeTraceAction(tmid); |
| 316 | LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action); |
| 317 | ptr += GetRecordSize(clock_source); |
| 318 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 319 | } |
| 320 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 321 | void Trace::FinishTracing() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 322 | // Compute elapsed time. |
| 323 | uint64_t elapsed = MicroTime() - start_time_; |
| 324 | |
| 325 | size_t final_offset = cur_offset_; |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 326 | uint32_t clock_overhead = GetClockOverhead(this); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 327 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 328 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 329 | Runtime::Current()->SetStatsEnabled(false); |
| 330 | } |
| 331 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 332 | std::set<mirror::AbstractMethod*> visited_methods; |
| 333 | GetVisitedMethods(final_offset, &visited_methods); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 334 | |
| 335 | std::ostringstream os; |
| 336 | |
| 337 | os << StringPrintf("%cversion\n", kTraceTokenChar); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 338 | os << StringPrintf("%d\n", GetTraceVersion(clock_source_)); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 339 | os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false"); |
| 340 | if (UseThreadCpuClock()) { |
| 341 | if (UseWallClock()) { |
| 342 | os << StringPrintf("clock=dual\n"); |
| 343 | } else { |
| 344 | os << StringPrintf("clock=thread-cpu\n"); |
| 345 | } |
| 346 | } else { |
| 347 | os << StringPrintf("clock=wall\n"); |
| 348 | } |
| 349 | os << StringPrintf("elapsed-time-usec=%llu\n", elapsed); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 350 | size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_); |
| 351 | os << StringPrintf("num-method-calls=%zd\n", num_records); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 352 | os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead); |
| 353 | os << StringPrintf("vm=art\n"); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 354 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 355 | os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS)); |
| 356 | os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES)); |
| 357 | os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS)); |
| 358 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 359 | os << StringPrintf("%cthreads\n", kTraceTokenChar); |
| 360 | DumpThreadList(os); |
| 361 | os << StringPrintf("%cmethods\n", kTraceTokenChar); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 362 | DumpMethodList(os, visited_methods); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 363 | os << StringPrintf("%cend\n", kTraceTokenChar); |
| 364 | |
| 365 | std::string header(os.str()); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 366 | if (trace_file_.get() == NULL) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 367 | iovec iov[2]; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 368 | iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str())); |
| 369 | iov[0].iov_len = header.length(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 370 | iov[1].iov_base = buf_.get(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 371 | iov[1].iov_len = final_offset; |
| 372 | Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 373 | const bool kDumpTraceInfo = false; |
| 374 | if (kDumpTraceInfo) { |
| 375 | LOG(INFO) << "Trace sent:\n" << header; |
| 376 | DumpBuf(buf_.get(), final_offset, clock_source_); |
| 377 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 378 | } else { |
| 379 | if (!trace_file_->WriteFully(header.c_str(), header.length()) || |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 380 | !trace_file_->WriteFully(buf_.get(), final_offset)) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 381 | std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno))); |
| 382 | PLOG(ERROR) << detail; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 383 | ThrowRuntimeException("%s", detail.c_str()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 384 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 385 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 386 | } |
| 387 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 388 | void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object, |
| 389 | const mirror::AbstractMethod* method, uint32_t new_dex_pc) { |
| 390 | // We're not recorded to listen to this kind of event, so complain. |
| 391 | LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc; |
| 392 | }; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 393 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 394 | void Trace::MethodEntered(Thread* thread, mirror::Object* this_object, |
| 395 | const mirror::AbstractMethod* method, uint32_t dex_pc) { |
| 396 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered); |
| 397 | } |
| 398 | |
| 399 | void Trace::MethodExited(Thread* thread, mirror::Object* this_object, |
| 400 | const mirror::AbstractMethod* method, uint32_t dex_pc, |
| 401 | const JValue& return_value) { |
| 402 | UNUSED(return_value); |
| 403 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited); |
| 404 | } |
| 405 | |
| 406 | void Trace::MethodUnwind(Thread* thread, const mirror::AbstractMethod* method, uint32_t dex_pc) { |
| 407 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind); |
| 408 | } |
| 409 | |
| 410 | void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
| 411 | mirror::AbstractMethod* catch_method, uint32_t catch_dex_pc, |
| 412 | mirror::Throwable* exception_object) |
| 413 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 414 | LOG(ERROR) << "Unexpected exception caught event in tracing"; |
| 415 | } |
| 416 | |
| 417 | void Trace::LogMethodTraceEvent(Thread* thread, const mirror::AbstractMethod* method, |
| 418 | instrumentation::Instrumentation::InstrumentationEvent event) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 419 | // Advance cur_offset_ atomically. |
| 420 | int32_t new_offset; |
| 421 | int32_t old_offset; |
| 422 | do { |
| 423 | old_offset = cur_offset_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 424 | new_offset = old_offset + GetRecordSize(clock_source_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 425 | if (new_offset > buffer_size_) { |
| 426 | overflow_ = true; |
| 427 | return; |
| 428 | } |
| 429 | } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0); |
| 430 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 431 | TraceAction action = kTraceMethodEnter; |
| 432 | switch (event) { |
| 433 | case instrumentation::Instrumentation::kMethodEntered: |
| 434 | action = kTraceMethodEnter; |
| 435 | break; |
| 436 | case instrumentation::Instrumentation::kMethodExited: |
| 437 | action = kTraceMethodExit; |
| 438 | break; |
| 439 | case instrumentation::Instrumentation::kMethodUnwind: |
| 440 | action = kTraceUnroll; |
| 441 | break; |
| 442 | default: |
| 443 | UNIMPLEMENTED(FATAL) << "Unexpected event: " << event; |
| 444 | } |
| 445 | |
| 446 | uint32_t method_value = EncodeTraceMethodAndAction(method, action); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 447 | |
| 448 | // Write data |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 449 | uint8_t* ptr = buf_.get() + old_offset; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 450 | Append2LE(ptr, thread->GetTid()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 451 | Append4LE(ptr + 2, method_value); |
| 452 | ptr += 6; |
| 453 | |
| 454 | if (UseThreadCpuClock()) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 455 | // TODO: this isn't vaguely thread safe. |
| 456 | SafeMap<Thread*, uint64_t>::iterator it = thread_clock_base_map_.find(thread); |
| 457 | uint32_t thread_clock_diff = 0; |
| 458 | if (UNLIKELY(it == thread_clock_base_map_.end())) { |
| 459 | // First event, the diff is 0, record the base time in the map. |
| 460 | uint64_t time = ThreadCpuMicroTime(); |
| 461 | thread_clock_base_map_.Put(thread, time); |
| 462 | } else { |
| 463 | uint64_t thread_clock_base = it->second; |
| 464 | thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base; |
| 465 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 466 | Append4LE(ptr, thread_clock_diff); |
| 467 | ptr += 4; |
| 468 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 469 | if (UseWallClock()) { |
| 470 | uint32_t wall_clock_diff = MicroTime() - start_time_; |
| 471 | Append4LE(ptr, wall_clock_diff); |
| 472 | } |
| 473 | } |
| 474 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 475 | void Trace::GetVisitedMethods(size_t buf_size, |
| 476 | std::set<mirror::AbstractMethod*>* visited_methods) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 477 | uint8_t* ptr = buf_.get() + kTraceHeaderLength; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 478 | uint8_t* end = buf_.get() + buf_size; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 479 | |
| 480 | while (ptr < end) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 481 | uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
| 482 | mirror::AbstractMethod* method = DecodeTraceMethodId(tmid); |
| 483 | visited_methods->insert(method); |
| 484 | ptr += GetRecordSize(clock_source_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 485 | } |
| 486 | } |
| 487 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame^] | 488 | void Trace::DumpMethodList(std::ostream& os, |
| 489 | const std::set<mirror::AbstractMethod*>& visited_methods) { |
| 490 | typedef std::set<mirror::AbstractMethod*>::const_iterator It; // TODO: C++0x auto |
| 491 | MethodHelper mh; |
| 492 | for (It it = visited_methods.begin(); it != visited_methods.end(); ++it) { |
| 493 | mirror::AbstractMethod* method = *it; |
| 494 | mh.ChangeMethod(method); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 495 | os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method, |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 496 | PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 497 | mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 498 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static void DumpThread(Thread* t, void* arg) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 502 | std::ostream& os = *reinterpret_cast<std::ostream*>(arg); |
| 503 | std::string name; |
| 504 | t->GetThreadName(name); |
| 505 | os << t->GetTid() << "\t" << name << "\n"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | void Trace::DumpThreadList(std::ostream& os) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 509 | Thread* self = Thread::Current(); |
| 510 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 511 | MutexLock mu(self, *Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 512 | Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 513 | } |
| 514 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 515 | } // namespace art |