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