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