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 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 21 | #include "base/stl_util.h" |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 22 | #include "base/unix_file/fd_file.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 23 | #include "class_linker.h" |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 24 | #include "common_throws.h" |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 25 | #include "debugger.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 26 | #include "dex_file-inl.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame] | 27 | #include "instrumentation.h" |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 28 | #include "mirror/art_method-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 29 | #include "mirror/class-inl.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 30 | #include "mirror/dex_cache.h" |
| 31 | #include "mirror/object_array-inl.h" |
Ian Rogers | 4f6ad8a | 2013-03-18 15:27:28 -0700 | [diff] [blame] | 32 | #include "mirror/object-inl.h" |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 33 | #include "object_utils.h" |
| 34 | #include "os.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 35 | #include "scoped_thread_state_change.h" |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 36 | #include "ScopedLocalRef.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 37 | #include "thread.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 38 | #include "thread_list.h" |
Ian Rogers | 166db04 | 2013-07-26 12:05:57 -0700 | [diff] [blame] | 39 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 40 | #include "entrypoints/quick/quick_entrypoints.h" |
| 41 | #endif |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 45 | // File format: |
| 46 | // header |
| 47 | // record 0 |
| 48 | // record 1 |
| 49 | // ... |
| 50 | // |
| 51 | // Header format: |
| 52 | // u4 magic ('SLOW') |
| 53 | // u2 version |
| 54 | // u2 offset to data |
| 55 | // u8 start date/time in usec |
| 56 | // u2 record size in bytes (version >= 2 only) |
| 57 | // ... padding to 32 bytes |
| 58 | // |
| 59 | // Record format v1: |
| 60 | // u1 thread ID |
| 61 | // u4 method ID | method action |
| 62 | // u4 time delta since start, in usec |
| 63 | // |
| 64 | // Record format v2: |
| 65 | // u2 thread ID |
| 66 | // u4 method ID | method action |
| 67 | // u4 time delta since start, in usec |
| 68 | // |
| 69 | // Record format v3: |
| 70 | // u2 thread ID |
| 71 | // u4 method ID | method action |
| 72 | // u4 time delta since start, in usec |
| 73 | // u4 wall time since start, in usec (when clock == "dual" only) |
| 74 | // |
| 75 | // 32 bits of microseconds is 70 minutes. |
| 76 | // |
| 77 | // All values are stored in little-endian order. |
| 78 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 79 | enum TraceAction { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 80 | kTraceMethodEnter = 0x00, // method entry |
| 81 | kTraceMethodExit = 0x01, // method exit |
| 82 | kTraceUnroll = 0x02, // method exited by exception unrolling |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 83 | // 0x03 currently unused |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 84 | kTraceMethodActionMask = 0x03, // two bits |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 85 | }; |
| 86 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 87 | class BuildStackTraceVisitor : public StackVisitor { |
| 88 | public: |
| 89 | explicit BuildStackTraceVisitor(Thread* thread) : StackVisitor(thread, NULL), |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 90 | method_trace_(Trace::AllocStackTrace()) {} |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 91 | |
| 92 | bool VisitFrame() { |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 93 | mirror::ArtMethod* m = GetMethod(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 94 | // Ignore runtime frames (in particular callee save). |
| 95 | if (!m->IsRuntimeMethod()) { |
| 96 | method_trace_->push_back(m); |
| 97 | } |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | // Returns a stack trace where the topmost frame corresponds with the first element of the vector. |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 102 | std::vector<mirror::ArtMethod*>* GetStackTrace() const { |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 103 | return method_trace_; |
| 104 | } |
| 105 | |
| 106 | private: |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 107 | std::vector<mirror::ArtMethod*>* const method_trace_; |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 110 | static const char kTraceTokenChar = '*'; |
| 111 | static const uint16_t kTraceHeaderLength = 32; |
| 112 | static const uint32_t kTraceMagicValue = 0x574f4c53; |
| 113 | static const uint16_t kTraceVersionSingleClock = 2; |
| 114 | static const uint16_t kTraceVersionDualClock = 3; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 115 | static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2 |
| 116 | static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 117 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 118 | #if defined(HAVE_POSIX_CLOCKS) |
| 119 | ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceDual; |
| 120 | #else |
| 121 | ProfilerClockSource Trace::default_clock_source_ = kProfilerClockSourceWall; |
| 122 | #endif |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 123 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 124 | Trace* volatile Trace::the_trace_ = NULL; |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 125 | pthread_t Trace::sampling_pthread_ = 0U; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 126 | UniquePtr<std::vector<mirror::ArtMethod*> > Trace::temp_stack_trace_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 127 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 128 | static mirror::ArtMethod* DecodeTraceMethodId(uint32_t tmid) { |
| 129 | return reinterpret_cast<mirror::ArtMethod*>(tmid & ~kTraceMethodActionMask); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 130 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 131 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 132 | static TraceAction DecodeTraceAction(uint32_t tmid) { |
| 133 | return static_cast<TraceAction>(tmid & kTraceMethodActionMask); |
| 134 | } |
| 135 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 136 | static uint32_t EncodeTraceMethodAndAction(const mirror::ArtMethod* method, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 137 | TraceAction action) { |
| 138 | uint32_t tmid = reinterpret_cast<uint32_t>(method) | action; |
| 139 | DCHECK_EQ(method, DecodeTraceMethodId(tmid)); |
| 140 | return tmid; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 141 | } |
| 142 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 143 | std::vector<mirror::ArtMethod*>* Trace::AllocStackTrace() { |
| 144 | if (temp_stack_trace_.get() != NULL) { |
| 145 | return temp_stack_trace_.release(); |
| 146 | } else { |
| 147 | return new std::vector<mirror::ArtMethod*>(); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | void Trace::FreeStackTrace(std::vector<mirror::ArtMethod*>* stack_trace) { |
| 152 | stack_trace->clear(); |
| 153 | temp_stack_trace_.reset(stack_trace); |
| 154 | } |
| 155 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 156 | void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 157 | #if defined(HAVE_POSIX_CLOCKS) |
| 158 | default_clock_source_ = clock_source; |
| 159 | #else |
| 160 | if (clock_source != kProfilerClockSourceWall) { |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 161 | LOG(WARNING) << "Ignoring tracing request to use CPU time."; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 162 | } |
| 163 | #endif |
| 164 | } |
| 165 | |
| 166 | static uint16_t GetTraceVersion(ProfilerClockSource clock_source) { |
| 167 | return (clock_source == kProfilerClockSourceDual) ? kTraceVersionDualClock |
| 168 | : kTraceVersionSingleClock; |
| 169 | } |
| 170 | |
| 171 | static uint16_t GetRecordSize(ProfilerClockSource clock_source) { |
| 172 | return (clock_source == kProfilerClockSourceDual) ? kTraceRecordSizeDualClock |
| 173 | : kTraceRecordSizeSingleClock; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 174 | } |
| 175 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 176 | bool Trace::UseThreadCpuClock() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 177 | return (clock_source_ == kProfilerClockSourceThreadCpu) || |
| 178 | (clock_source_ == kProfilerClockSourceDual); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 179 | } |
| 180 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 181 | bool Trace::UseWallClock() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 182 | return (clock_source_ == kProfilerClockSourceWall) || |
| 183 | (clock_source_ == kProfilerClockSourceDual); |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | static void MeasureClockOverhead(Trace* trace) { |
| 187 | if (trace->UseThreadCpuClock()) { |
Jeff Hao | 57dac6e | 2013-08-15 16:36:24 -0700 | [diff] [blame] | 188 | Thread::Current()->GetCpuMicroTime(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 189 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 190 | if (trace->UseWallClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 191 | MicroTime(); |
| 192 | } |
| 193 | } |
| 194 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 195 | // Compute an average time taken to measure clocks. |
| 196 | static uint32_t GetClockOverheadNanoSeconds(Trace* trace) { |
Jeff Hao | 57dac6e | 2013-08-15 16:36:24 -0700 | [diff] [blame] | 197 | Thread* self = Thread::Current(); |
| 198 | uint64_t start = self->GetCpuMicroTime(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 199 | |
| 200 | for (int i = 4000; i > 0; i--) { |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 201 | MeasureClockOverhead(trace); |
| 202 | MeasureClockOverhead(trace); |
| 203 | MeasureClockOverhead(trace); |
| 204 | MeasureClockOverhead(trace); |
| 205 | MeasureClockOverhead(trace); |
| 206 | MeasureClockOverhead(trace); |
| 207 | MeasureClockOverhead(trace); |
| 208 | MeasureClockOverhead(trace); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 209 | } |
| 210 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 211 | uint64_t elapsed_us = self->GetCpuMicroTime() - start; |
| 212 | return static_cast<uint32_t>(elapsed_us / 32); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 215 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 216 | static void Append2LE(uint8_t* buf, uint16_t val) { |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 217 | *buf++ = static_cast<uint8_t>(val); |
| 218 | *buf++ = static_cast<uint8_t>(val >> 8); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 219 | } |
| 220 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 221 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 222 | static void Append4LE(uint8_t* buf, uint32_t val) { |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 223 | *buf++ = static_cast<uint8_t>(val); |
| 224 | *buf++ = static_cast<uint8_t>(val >> 8); |
| 225 | *buf++ = static_cast<uint8_t>(val >> 16); |
| 226 | *buf++ = static_cast<uint8_t>(val >> 24); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 229 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 230 | static void Append8LE(uint8_t* buf, uint64_t val) { |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 231 | *buf++ = static_cast<uint8_t>(val); |
| 232 | *buf++ = static_cast<uint8_t>(val >> 8); |
| 233 | *buf++ = static_cast<uint8_t>(val >> 16); |
| 234 | *buf++ = static_cast<uint8_t>(val >> 24); |
| 235 | *buf++ = static_cast<uint8_t>(val >> 32); |
| 236 | *buf++ = static_cast<uint8_t>(val >> 40); |
| 237 | *buf++ = static_cast<uint8_t>(val >> 48); |
| 238 | *buf++ = static_cast<uint8_t>(val >> 56); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 239 | } |
| 240 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 241 | static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 242 | BuildStackTraceVisitor build_trace_visitor(thread); |
| 243 | build_trace_visitor.WalkStack(); |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 244 | std::vector<mirror::ArtMethod*>* stack_trace = build_trace_visitor.GetStackTrace(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 245 | Trace* the_trace = reinterpret_cast<Trace*>(arg); |
| 246 | the_trace->CompareAndUpdateStackTrace(thread, stack_trace); |
| 247 | } |
| 248 | |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 249 | static void ClearThreadStackTraceAndClockBase(Thread* thread, void* arg) { |
| 250 | thread->SetTraceClockBase(0); |
| 251 | std::vector<mirror::ArtMethod*>* stack_trace = thread->GetStackTraceSample(); |
| 252 | thread->SetStackTraceSample(NULL); |
| 253 | delete stack_trace; |
| 254 | } |
| 255 | |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 256 | void Trace::CompareAndUpdateStackTrace(Thread* thread, |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 257 | std::vector<mirror::ArtMethod*>* stack_trace) { |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 258 | CHECK_EQ(pthread_self(), sampling_pthread_); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 259 | std::vector<mirror::ArtMethod*>* old_stack_trace = thread->GetStackTraceSample(); |
| 260 | // Update the thread's stack trace sample. |
| 261 | thread->SetStackTraceSample(stack_trace); |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 262 | // Read timer clocks to use for all events in this trace. |
| 263 | uint32_t thread_clock_diff = 0; |
| 264 | uint32_t wall_clock_diff = 0; |
| 265 | ReadClocks(thread, &thread_clock_diff, &wall_clock_diff); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 266 | if (old_stack_trace == NULL) { |
| 267 | // If there's no previous stack trace sample for this thread, log an entry event for all |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 268 | // methods in the trace. |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 269 | for (std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 270 | rit != stack_trace->rend(); ++rit) { |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 271 | LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered, |
| 272 | thread_clock_diff, wall_clock_diff); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 273 | } |
| 274 | } else { |
| 275 | // If there's a previous stack trace for this thread, diff the traces and emit entry and exit |
| 276 | // events accordingly. |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 277 | std::vector<mirror::ArtMethod*>::reverse_iterator old_rit = old_stack_trace->rbegin(); |
| 278 | std::vector<mirror::ArtMethod*>::reverse_iterator rit = stack_trace->rbegin(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 279 | // Iterate bottom-up over both traces until there's a difference between them. |
| 280 | while (old_rit != old_stack_trace->rend() && rit != stack_trace->rend() && *old_rit == *rit) { |
| 281 | old_rit++; |
| 282 | rit++; |
| 283 | } |
| 284 | // Iterate top-down over the old trace until the point where they differ, emitting exit events. |
Ian Rogers | e2f77e7 | 2013-08-13 19:19:40 -0700 | [diff] [blame] | 285 | for (std::vector<mirror::ArtMethod*>::iterator old_it = old_stack_trace->begin(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 286 | old_it != old_rit.base(); ++old_it) { |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 287 | LogMethodTraceEvent(thread, *old_it, instrumentation::Instrumentation::kMethodExited, |
| 288 | thread_clock_diff, wall_clock_diff); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 289 | } |
| 290 | // Iterate bottom-up over the new trace from the point where they differ, emitting entry events. |
| 291 | for (; rit != stack_trace->rend(); ++rit) { |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 292 | LogMethodTraceEvent(thread, *rit, instrumentation::Instrumentation::kMethodEntered, |
| 293 | thread_clock_diff, wall_clock_diff); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 294 | } |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 295 | FreeStackTrace(old_stack_trace); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
| 299 | void* Trace::RunSamplingThread(void* arg) { |
| 300 | Runtime* runtime = Runtime::Current(); |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 301 | int interval_us = reinterpret_cast<int>(arg); |
Jeff Hao | 4044bda | 2014-01-06 15:50:45 -0800 | [diff] [blame] | 302 | CHECK_GE(interval_us, 0); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 303 | CHECK(runtime->AttachCurrentThread("Sampling Profiler", true, runtime->GetSystemThreadGroup(), |
| 304 | !runtime->IsCompiler())); |
| 305 | |
| 306 | while (true) { |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 307 | usleep(interval_us); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 308 | ATRACE_BEGIN("Profile sampling"); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 309 | Thread* self = Thread::Current(); |
| 310 | Trace* the_trace; |
| 311 | { |
| 312 | MutexLock mu(self, *Locks::trace_lock_); |
| 313 | the_trace = the_trace_; |
| 314 | if (the_trace == NULL) { |
| 315 | break; |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | runtime->GetThreadList()->SuspendAll(); |
| 320 | { |
| 321 | MutexLock mu(self, *Locks::thread_list_lock_); |
| 322 | runtime->GetThreadList()->ForEach(GetSample, the_trace); |
| 323 | } |
| 324 | runtime->GetThreadList()->ResumeAll(); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 325 | ATRACE_END(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | runtime->DetachCurrentThread(); |
| 329 | return NULL; |
| 330 | } |
| 331 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 332 | void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 333 | bool direct_to_ddms, bool sampling_enabled, int interval_us) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 334 | Thread* self = Thread::Current(); |
| 335 | { |
| 336 | MutexLock mu(self, *Locks::trace_lock_); |
| 337 | if (the_trace_ != NULL) { |
| 338 | LOG(ERROR) << "Trace already in progress, ignoring this request"; |
| 339 | return; |
| 340 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 341 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 342 | Runtime* runtime = Runtime::Current(); |
| 343 | runtime->GetThreadList()->SuspendAll(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 344 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 345 | // Open trace file if not going directly to ddms. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 346 | UniquePtr<File> trace_file; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 347 | if (!direct_to_ddms) { |
| 348 | if (trace_fd < 0) { |
Brian Carlstrom | 7571e8b | 2013-08-12 17:04:14 -0700 | [diff] [blame] | 349 | trace_file.reset(OS::CreateEmptyFile(trace_filename)); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 350 | } else { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 351 | trace_file.reset(new File(trace_fd, "tracefile")); |
Elliott Hughes | 7616005 | 2012-12-12 16:31:20 -0800 | [diff] [blame] | 352 | trace_file->DisableAutoClose(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 353 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 354 | if (trace_file.get() == NULL) { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 355 | PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'"; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 356 | runtime->GetThreadList()->ResumeAll(); |
| 357 | ScopedObjectAccess soa(self); |
| 358 | ThrowRuntimeException("Unable to open trace file '%s'", trace_filename); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 359 | return; |
| 360 | } |
| 361 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 362 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 363 | // Create Trace object. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 364 | { |
| 365 | MutexLock mu(self, *Locks::trace_lock_); |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 366 | if (the_trace_ != NULL) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 367 | LOG(ERROR) << "Trace already in progress, ignoring this request"; |
| 368 | } else { |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 369 | the_trace_ = new Trace(trace_file.release(), buffer_size, flags, sampling_enabled); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 370 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 371 | // Enable count of allocs if specified in the flags. |
| 372 | if ((flags && kTraceCountAllocs) != 0) { |
| 373 | runtime->SetStatsEnabled(true); |
| 374 | } |
| 375 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 376 | |
| 377 | |
| 378 | if (sampling_enabled) { |
| 379 | CHECK_PTHREAD_CALL(pthread_create, (&sampling_pthread_, NULL, &RunSamplingThread, |
| 380 | reinterpret_cast<void*>(interval_us)), |
| 381 | "Sampling profiler thread"); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 382 | } else { |
| 383 | runtime->GetInstrumentation()->AddListener(the_trace_, |
| 384 | instrumentation::Instrumentation::kMethodEntered | |
| 385 | instrumentation::Instrumentation::kMethodExited | |
| 386 | instrumentation::Instrumentation::kMethodUnwind); |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 387 | runtime->GetInstrumentation()->EnableMethodTracing(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 388 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 389 | } |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 390 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 391 | runtime->GetThreadList()->ResumeAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 392 | } |
| 393 | |
| 394 | void Trace::Stop() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 395 | Runtime* runtime = Runtime::Current(); |
| 396 | runtime->GetThreadList()->SuspendAll(); |
| 397 | Trace* the_trace = NULL; |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 398 | pthread_t sampling_pthread = 0U; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 399 | { |
| 400 | MutexLock mu(Thread::Current(), *Locks::trace_lock_); |
| 401 | if (the_trace_ == NULL) { |
| 402 | LOG(ERROR) << "Trace stop requested, but no trace currently running"; |
| 403 | } else { |
| 404 | the_trace = the_trace_; |
| 405 | the_trace_ = NULL; |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 406 | sampling_pthread = sampling_pthread_; |
| 407 | sampling_pthread_ = 0U; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 408 | } |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 409 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 410 | if (the_trace != NULL) { |
| 411 | the_trace->FinishTracing(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 412 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 413 | if (the_trace->sampling_enabled_) { |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 414 | MutexLock mu(Thread::Current(), *Locks::thread_list_lock_); |
| 415 | runtime->GetThreadList()->ForEach(ClearThreadStackTraceAndClockBase, NULL); |
| 416 | } else { |
Sebastien Hertz | 138dbfc | 2013-12-04 18:15:25 +0100 | [diff] [blame] | 417 | runtime->GetInstrumentation()->DisableMethodTracing(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 418 | runtime->GetInstrumentation()->RemoveListener(the_trace, |
| 419 | instrumentation::Instrumentation::kMethodEntered | |
| 420 | instrumentation::Instrumentation::kMethodExited | |
| 421 | instrumentation::Instrumentation::kMethodUnwind); |
| 422 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 423 | delete the_trace; |
| 424 | } |
| 425 | runtime->GetThreadList()->ResumeAll(); |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 426 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 427 | if (sampling_pthread != 0U) { |
Jeff Hao | 0abc72e | 2013-08-13 13:45:14 -0700 | [diff] [blame] | 428 | CHECK_PTHREAD_CALL(pthread_join, (sampling_pthread, NULL), "sampling thread shutdown"); |
| 429 | } |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 430 | } |
| 431 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 432 | void Trace::Shutdown() { |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 433 | if (GetMethodTracingMode() != kTracingInactive) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 434 | Stop(); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 435 | } |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 436 | } |
| 437 | |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 438 | TracingMode Trace::GetMethodTracingMode() { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 439 | MutexLock mu(Thread::Current(), *Locks::trace_lock_); |
Jeff Hao | 64caa7d | 2013-08-29 11:18:01 -0700 | [diff] [blame] | 440 | if (the_trace_ == NULL) { |
| 441 | return kTracingInactive; |
| 442 | } else if (the_trace_->sampling_enabled_) { |
| 443 | return kSampleProfilingActive; |
| 444 | } else { |
| 445 | return kMethodTracingActive; |
| 446 | } |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 447 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 448 | |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 449 | Trace::Trace(File* trace_file, int buffer_size, int flags, bool sampling_enabled) |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 450 | : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags), |
Jeff Hao | 23009dc | 2013-08-22 15:36:42 -0700 | [diff] [blame] | 451 | sampling_enabled_(sampling_enabled), clock_source_(default_clock_source_), |
| 452 | buffer_size_(buffer_size), start_time_(MicroTime()), cur_offset_(0), overflow_(false) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 453 | // Set up the beginning of the trace. |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 454 | uint16_t trace_version = GetTraceVersion(clock_source_); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 455 | memset(buf_.get(), 0, kTraceHeaderLength); |
| 456 | Append4LE(buf_.get(), kTraceMagicValue); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 457 | Append2LE(buf_.get() + 4, trace_version); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 458 | Append2LE(buf_.get() + 6, kTraceHeaderLength); |
| 459 | Append8LE(buf_.get() + 8, start_time_); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 460 | if (trace_version >= kTraceVersionDualClock) { |
| 461 | uint16_t record_size = GetRecordSize(clock_source_); |
| 462 | Append2LE(buf_.get() + 16, record_size); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 463 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 464 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 465 | // Update current offset. |
| 466 | cur_offset_ = kTraceHeaderLength; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 467 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 468 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 469 | static void DumpBuf(uint8_t* buf, size_t buf_size, ProfilerClockSource clock_source) |
| 470 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 471 | uint8_t* ptr = buf + kTraceHeaderLength; |
| 472 | uint8_t* end = buf + buf_size; |
| 473 | |
| 474 | while (ptr < end) { |
| 475 | uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 476 | mirror::ArtMethod* method = DecodeTraceMethodId(tmid); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 477 | TraceAction action = DecodeTraceAction(tmid); |
| 478 | LOG(INFO) << PrettyMethod(method) << " " << static_cast<int>(action); |
| 479 | ptr += GetRecordSize(clock_source); |
| 480 | } |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 481 | } |
| 482 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 483 | void Trace::FinishTracing() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 484 | // Compute elapsed time. |
| 485 | uint64_t elapsed = MicroTime() - start_time_; |
| 486 | |
| 487 | size_t final_offset = cur_offset_; |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 488 | uint32_t clock_overhead_ns = GetClockOverheadNanoSeconds(this); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 489 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 490 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 491 | Runtime::Current()->SetStatsEnabled(false); |
| 492 | } |
| 493 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 494 | std::set<mirror::ArtMethod*> visited_methods; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 495 | GetVisitedMethods(final_offset, &visited_methods); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 496 | |
| 497 | std::ostringstream os; |
| 498 | |
| 499 | os << StringPrintf("%cversion\n", kTraceTokenChar); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 500 | os << StringPrintf("%d\n", GetTraceVersion(clock_source_)); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 501 | os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false"); |
| 502 | if (UseThreadCpuClock()) { |
| 503 | if (UseWallClock()) { |
| 504 | os << StringPrintf("clock=dual\n"); |
| 505 | } else { |
| 506 | os << StringPrintf("clock=thread-cpu\n"); |
| 507 | } |
| 508 | } else { |
| 509 | os << StringPrintf("clock=wall\n"); |
| 510 | } |
| 511 | os << StringPrintf("elapsed-time-usec=%llu\n", elapsed); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 512 | size_t num_records = (final_offset - kTraceHeaderLength) / GetRecordSize(clock_source_); |
| 513 | os << StringPrintf("num-method-calls=%zd\n", num_records); |
Jeff Hao | 5ce4b17 | 2013-08-16 16:27:18 -0700 | [diff] [blame] | 514 | os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead_ns); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 515 | os << StringPrintf("vm=art\n"); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 516 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 517 | os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS)); |
| 518 | os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES)); |
| 519 | os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS)); |
| 520 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 521 | os << StringPrintf("%cthreads\n", kTraceTokenChar); |
| 522 | DumpThreadList(os); |
| 523 | os << StringPrintf("%cmethods\n", kTraceTokenChar); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 524 | DumpMethodList(os, visited_methods); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 525 | os << StringPrintf("%cend\n", kTraceTokenChar); |
| 526 | |
| 527 | std::string header(os.str()); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 528 | if (trace_file_.get() == NULL) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 529 | iovec iov[2]; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 530 | iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str())); |
| 531 | iov[0].iov_len = header.length(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 532 | iov[1].iov_base = buf_.get(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 533 | iov[1].iov_len = final_offset; |
| 534 | Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 535 | const bool kDumpTraceInfo = false; |
| 536 | if (kDumpTraceInfo) { |
| 537 | LOG(INFO) << "Trace sent:\n" << header; |
| 538 | DumpBuf(buf_.get(), final_offset, clock_source_); |
| 539 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 540 | } else { |
| 541 | if (!trace_file_->WriteFully(header.c_str(), header.length()) || |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 542 | !trace_file_->WriteFully(buf_.get(), final_offset)) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 543 | std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno))); |
| 544 | PLOG(ERROR) << detail; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 545 | ThrowRuntimeException("%s", detail.c_str()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 546 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 547 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 548 | } |
| 549 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 550 | void Trace::DexPcMoved(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 551 | const mirror::ArtMethod* method, uint32_t new_dex_pc) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 552 | // We're not recorded to listen to this kind of event, so complain. |
| 553 | LOG(ERROR) << "Unexpected dex PC event in tracing " << PrettyMethod(method) << " " << new_dex_pc; |
| 554 | }; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 555 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 556 | void Trace::MethodEntered(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 557 | const mirror::ArtMethod* method, uint32_t dex_pc) { |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 558 | uint32_t thread_clock_diff = 0; |
| 559 | uint32_t wall_clock_diff = 0; |
| 560 | ReadClocks(thread, &thread_clock_diff, &wall_clock_diff); |
| 561 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodEntered, |
| 562 | thread_clock_diff, wall_clock_diff); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | void Trace::MethodExited(Thread* thread, mirror::Object* this_object, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 566 | const mirror::ArtMethod* method, uint32_t dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 567 | const JValue& return_value) { |
| 568 | UNUSED(return_value); |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 569 | uint32_t thread_clock_diff = 0; |
| 570 | uint32_t wall_clock_diff = 0; |
| 571 | ReadClocks(thread, &thread_clock_diff, &wall_clock_diff); |
| 572 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodExited, |
| 573 | thread_clock_diff, wall_clock_diff); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 574 | } |
| 575 | |
Sebastien Hertz | 51db44a | 2013-11-19 10:00:29 +0100 | [diff] [blame] | 576 | void Trace::MethodUnwind(Thread* thread, mirror::Object* this_object, |
| 577 | const mirror::ArtMethod* method, uint32_t dex_pc) { |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 578 | uint32_t thread_clock_diff = 0; |
| 579 | uint32_t wall_clock_diff = 0; |
| 580 | ReadClocks(thread, &thread_clock_diff, &wall_clock_diff); |
| 581 | LogMethodTraceEvent(thread, method, instrumentation::Instrumentation::kMethodUnwind, |
| 582 | thread_clock_diff, wall_clock_diff); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 583 | } |
| 584 | |
| 585 | void Trace::ExceptionCaught(Thread* thread, const ThrowLocation& throw_location, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 586 | mirror::ArtMethod* catch_method, uint32_t catch_dex_pc, |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 587 | mirror::Throwable* exception_object) |
| 588 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 589 | LOG(ERROR) << "Unexpected exception caught event in tracing"; |
| 590 | } |
| 591 | |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 592 | void Trace::ReadClocks(Thread* thread, uint32_t* thread_clock_diff, uint32_t* wall_clock_diff) { |
| 593 | if (UseThreadCpuClock()) { |
| 594 | uint64_t clock_base = thread->GetTraceClockBase(); |
| 595 | if (UNLIKELY(clock_base == 0)) { |
| 596 | // First event, record the base time in the map. |
| 597 | uint64_t time = thread->GetCpuMicroTime(); |
| 598 | thread->SetTraceClockBase(time); |
| 599 | } else { |
| 600 | *thread_clock_diff = thread->GetCpuMicroTime() - clock_base; |
| 601 | } |
| 602 | } |
| 603 | if (UseWallClock()) { |
| 604 | *wall_clock_diff = MicroTime() - start_time_; |
| 605 | } |
| 606 | } |
| 607 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 608 | void Trace::LogMethodTraceEvent(Thread* thread, const mirror::ArtMethod* method, |
Jeff Hao | c1ff4b7 | 2013-08-19 11:33:10 -0700 | [diff] [blame] | 609 | instrumentation::Instrumentation::InstrumentationEvent event, |
| 610 | uint32_t thread_clock_diff, uint32_t wall_clock_diff) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 611 | // Advance cur_offset_ atomically. |
| 612 | int32_t new_offset; |
| 613 | int32_t old_offset; |
| 614 | do { |
| 615 | old_offset = cur_offset_; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 616 | new_offset = old_offset + GetRecordSize(clock_source_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 617 | if (new_offset > buffer_size_) { |
| 618 | overflow_ = true; |
| 619 | return; |
| 620 | } |
| 621 | } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0); |
| 622 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 623 | TraceAction action = kTraceMethodEnter; |
| 624 | switch (event) { |
| 625 | case instrumentation::Instrumentation::kMethodEntered: |
| 626 | action = kTraceMethodEnter; |
| 627 | break; |
| 628 | case instrumentation::Instrumentation::kMethodExited: |
| 629 | action = kTraceMethodExit; |
| 630 | break; |
| 631 | case instrumentation::Instrumentation::kMethodUnwind: |
| 632 | action = kTraceUnroll; |
| 633 | break; |
| 634 | default: |
| 635 | UNIMPLEMENTED(FATAL) << "Unexpected event: " << event; |
| 636 | } |
| 637 | |
| 638 | uint32_t method_value = EncodeTraceMethodAndAction(method, action); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 639 | |
| 640 | // Write data |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 641 | uint8_t* ptr = buf_.get() + old_offset; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 642 | Append2LE(ptr, thread->GetTid()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 643 | Append4LE(ptr + 2, method_value); |
| 644 | ptr += 6; |
| 645 | |
| 646 | if (UseThreadCpuClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 647 | Append4LE(ptr, thread_clock_diff); |
| 648 | ptr += 4; |
| 649 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 650 | if (UseWallClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 651 | Append4LE(ptr, wall_clock_diff); |
| 652 | } |
| 653 | } |
| 654 | |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 655 | void Trace::GetVisitedMethods(size_t buf_size, |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 656 | std::set<mirror::ArtMethod*>* visited_methods) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 657 | uint8_t* ptr = buf_.get() + kTraceHeaderLength; |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 658 | uint8_t* end = buf_.get() + buf_size; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 659 | |
| 660 | while (ptr < end) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 661 | uint32_t tmid = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 662 | mirror::ArtMethod* method = DecodeTraceMethodId(tmid); |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 663 | visited_methods->insert(method); |
| 664 | ptr += GetRecordSize(clock_source_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 668 | void Trace::DumpMethodList(std::ostream& os, const std::set<mirror::ArtMethod*>& visited_methods) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 669 | MethodHelper mh; |
Mathieu Chartier | 02e2511 | 2013-08-14 16:14:24 -0700 | [diff] [blame] | 670 | for (const auto& method : visited_methods) { |
Ian Rogers | 62d6c77 | 2013-02-27 08:32:07 -0800 | [diff] [blame] | 671 | mh.ChangeMethod(method); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 672 | os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method, |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 673 | PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(), |
Ian Rogers | d91d6d6 | 2013-09-25 20:26:14 -0700 | [diff] [blame] | 674 | mh.GetSignature().ToString().c_str(), mh.GetDeclaringClassSourceFile()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 675 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | static void DumpThread(Thread* t, void* arg) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 679 | std::ostream& os = *reinterpret_cast<std::ostream*>(arg); |
| 680 | std::string name; |
| 681 | t->GetThreadName(name); |
| 682 | os << t->GetTid() << "\t" << name << "\n"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | void Trace::DumpThreadList(std::ostream& os) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 686 | Thread* self = Thread::Current(); |
| 687 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 688 | MutexLock mu(self, *Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 689 | Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 690 | } |
| 691 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 692 | } // namespace art |