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