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 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 21 | #include "class_linker.h" |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 22 | #include "debugger.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 23 | #include "dex_cache.h" |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame^] | 24 | #include "instrumentation.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 25 | #if !defined(ART_USE_LLVM_COMPILER) |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 26 | #include "oat/runtime/oat_support_entrypoints.h" |
Ian Rogers | 776ac1f | 2012-04-13 23:36:36 -0700 | [diff] [blame] | 27 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 28 | #include "object_utils.h" |
| 29 | #include "os.h" |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 30 | #include "scoped_thread_state_change.h" |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 31 | #include "thread.h" |
Ian Rogers | 57b86d4 | 2012-03-27 16:05:41 -0700 | [diff] [blame] | 32 | #include "thread_list.h" |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 33 | |
| 34 | namespace art { |
| 35 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 36 | // File format: |
| 37 | // header |
| 38 | // record 0 |
| 39 | // record 1 |
| 40 | // ... |
| 41 | // |
| 42 | // Header format: |
| 43 | // u4 magic ('SLOW') |
| 44 | // u2 version |
| 45 | // u2 offset to data |
| 46 | // u8 start date/time in usec |
| 47 | // u2 record size in bytes (version >= 2 only) |
| 48 | // ... padding to 32 bytes |
| 49 | // |
| 50 | // Record format v1: |
| 51 | // u1 thread ID |
| 52 | // u4 method ID | method action |
| 53 | // u4 time delta since start, in usec |
| 54 | // |
| 55 | // Record format v2: |
| 56 | // u2 thread ID |
| 57 | // u4 method ID | method action |
| 58 | // u4 time delta since start, in usec |
| 59 | // |
| 60 | // Record format v3: |
| 61 | // u2 thread ID |
| 62 | // u4 method ID | method action |
| 63 | // u4 time delta since start, in usec |
| 64 | // u4 wall time since start, in usec (when clock == "dual" only) |
| 65 | // |
| 66 | // 32 bits of microseconds is 70 minutes. |
| 67 | // |
| 68 | // All values are stored in little-endian order. |
| 69 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 70 | static const uint32_t kTraceMethodActionMask = 0x03; // two bits |
| 71 | static const char kTraceTokenChar = '*'; |
| 72 | static const uint16_t kTraceHeaderLength = 32; |
| 73 | static const uint32_t kTraceMagicValue = 0x574f4c53; |
| 74 | static const uint16_t kTraceVersionSingleClock = 2; |
| 75 | static const uint16_t kTraceVersionDualClock = 3; |
| 76 | static const uint16_t kTraceRecordSizeSingleClock = 10; // using v2 |
| 77 | static const uint16_t kTraceRecordSizeDualClock = 14; // using v3 with two timestamps |
| 78 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 79 | static ProfilerClockSource gDefaultTraceClockSource = kProfilerClockSourceDual; |
| 80 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 81 | static inline uint32_t TraceMethodId(uint32_t methodValue) { |
| 82 | return (methodValue & ~kTraceMethodActionMask); |
| 83 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 84 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 85 | static inline uint32_t TraceMethodCombine(uint32_t method, uint8_t traceEvent) { |
| 86 | return (method | traceEvent); |
| 87 | } |
| 88 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 89 | void Trace::SetDefaultClockSource(ProfilerClockSource clock_source) { |
| 90 | gDefaultTraceClockSource = clock_source; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 93 | bool Trace::UseThreadCpuClock() { |
| 94 | #if defined(HAVE_POSIX_CLOCKS) |
| 95 | return clock_source_ != kProfilerClockSourceWall; |
| 96 | #else |
| 97 | return false; |
| 98 | #endif |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 99 | } |
| 100 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 101 | bool Trace::UseWallClock() { |
| 102 | #if defined(HAVE_POSIX_CLOCKS) |
| 103 | return clock_source_ != kProfilerClockSourceThreadCpu; |
| 104 | #else |
| 105 | return true; |
| 106 | #endif |
| 107 | } |
| 108 | |
| 109 | static void MeasureClockOverhead(Trace* trace) { |
| 110 | if (trace->UseThreadCpuClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 111 | ThreadCpuMicroTime(); |
| 112 | } |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 113 | if (trace->UseWallClock()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 114 | MicroTime(); |
| 115 | } |
| 116 | } |
| 117 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 118 | static uint32_t GetClockOverhead(Trace* trace) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 119 | uint64_t start = ThreadCpuMicroTime(); |
| 120 | |
| 121 | for (int i = 4000; i > 0; i--) { |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 122 | MeasureClockOverhead(trace); |
| 123 | MeasureClockOverhead(trace); |
| 124 | MeasureClockOverhead(trace); |
| 125 | MeasureClockOverhead(trace); |
| 126 | MeasureClockOverhead(trace); |
| 127 | MeasureClockOverhead(trace); |
| 128 | MeasureClockOverhead(trace); |
| 129 | MeasureClockOverhead(trace); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | uint64_t elapsed = ThreadCpuMicroTime() - start; |
| 133 | return uint32_t (elapsed / 32); |
| 134 | } |
| 135 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 136 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 137 | static void Append2LE(uint8_t* buf, uint16_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 138 | *buf++ = (uint8_t) val; |
| 139 | *buf++ = (uint8_t) (val >> 8); |
| 140 | } |
| 141 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 142 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 143 | static void Append4LE(uint8_t* buf, uint32_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 144 | *buf++ = (uint8_t) val; |
| 145 | *buf++ = (uint8_t) (val >> 8); |
| 146 | *buf++ = (uint8_t) (val >> 16); |
| 147 | *buf++ = (uint8_t) (val >> 24); |
| 148 | } |
| 149 | |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 150 | // TODO: put this somewhere with the big-endian equivalent used by JDWP. |
| 151 | static void Append8LE(uint8_t* buf, uint64_t val) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 152 | *buf++ = (uint8_t) val; |
| 153 | *buf++ = (uint8_t) (val >> 8); |
| 154 | *buf++ = (uint8_t) (val >> 16); |
| 155 | *buf++ = (uint8_t) (val >> 24); |
| 156 | *buf++ = (uint8_t) (val >> 32); |
| 157 | *buf++ = (uint8_t) (val >> 40); |
| 158 | *buf++ = (uint8_t) (val >> 48); |
| 159 | *buf++ = (uint8_t) (val >> 56); |
| 160 | } |
| 161 | |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 162 | Trace::Trace(File* trace_file, int buffer_size, int flags) |
| 163 | : trace_file_(trace_file), buf_(new uint8_t[buffer_size]()), flags_(flags), |
| 164 | clock_source_(gDefaultTraceClockSource), overflow_(false), |
| 165 | buffer_size_(buffer_size), start_time_(0), trace_version_(0), record_size_(0), cur_offset_(0) { |
| 166 | } |
| 167 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 168 | void Trace::Start(const char* trace_filename, int trace_fd, int buffer_size, int flags, bool direct_to_ddms) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 169 | if (Runtime::Current()->IsMethodTracingActive()) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 170 | LOG(INFO) << "Trace already in progress, ignoring this request"; |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 171 | return; |
| 172 | } |
| 173 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 174 | Runtime::Current()->GetThreadList()->SuspendAll(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 175 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 176 | // Open trace file if not going directly to ddms. |
| 177 | File* trace_file = NULL; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 178 | if (!direct_to_ddms) { |
| 179 | if (trace_fd < 0) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 180 | trace_file = OS::OpenFile(trace_filename, true); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 181 | } else { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 182 | trace_file = OS::FileFromFd("tracefile", trace_fd); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 183 | } |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 184 | if (trace_file == NULL) { |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 185 | PLOG(ERROR) << "Unable to open trace file '" << trace_filename << "'"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 186 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", |
| 187 | StringPrintf("Unable to open trace file '%s'", trace_filename).c_str()); |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 188 | Runtime::Current()->GetThreadList()->ResumeAll(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 192 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 193 | // Create Trace object. |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 194 | Trace* tracer(new Trace(trace_file, buffer_size, flags)); |
| 195 | |
| 196 | // Enable count of allocs if specified in the flags. |
| 197 | if ((flags && kTraceCountAllocs) != 0) { |
| 198 | Runtime::Current()->SetStatsEnabled(true); |
| 199 | } |
| 200 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 201 | Runtime::Current()->EnableMethodTracing(tracer); |
| 202 | tracer->BeginTracing(); |
| 203 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 204 | Runtime::Current()->GetThreadList()->ResumeAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | void Trace::Stop() { |
| 208 | if (!Runtime::Current()->IsMethodTracingActive()) { |
| 209 | LOG(INFO) << "Trace stop requested, but no trace currently running"; |
| 210 | return; |
| 211 | } |
| 212 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 213 | Runtime::Current()->GetThreadList()->SuspendAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 214 | |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame^] | 215 | Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 216 | Runtime::Current()->DisableMethodTracing(); |
| 217 | |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 218 | Runtime::Current()->GetThreadList()->ResumeAll(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 219 | } |
| 220 | |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 221 | void Trace::Shutdown() { |
| 222 | if (!Runtime::Current()->IsMethodTracingActive()) { |
| 223 | LOG(INFO) << "Trace shutdown requested, but no trace currently running"; |
| 224 | return; |
| 225 | } |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame^] | 226 | Runtime::Current()->GetInstrumentation()->GetTrace()->FinishTracing(); |
jeffhao | b5e8185 | 2012-03-12 11:15:45 -0700 | [diff] [blame] | 227 | Runtime::Current()->DisableMethodTracing(); |
| 228 | } |
| 229 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 230 | void Trace::BeginTracing() { |
| 231 | // Set the start time of tracing. |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 232 | start_time_ = MicroTime(); |
| 233 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 234 | // Set trace version and record size. |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 235 | if (UseThreadCpuClock() && UseWallClock()) { |
| 236 | trace_version_ = kTraceVersionDualClock; |
| 237 | record_size_ = kTraceRecordSizeDualClock; |
| 238 | } else { |
| 239 | trace_version_ = kTraceVersionSingleClock; |
| 240 | record_size_ = kTraceRecordSizeSingleClock; |
| 241 | } |
| 242 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 243 | // Set up the beginning of the trace. |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 244 | memset(buf_.get(), 0, kTraceHeaderLength); |
| 245 | Append4LE(buf_.get(), kTraceMagicValue); |
| 246 | Append2LE(buf_.get() + 4, trace_version_); |
| 247 | Append2LE(buf_.get() + 6, kTraceHeaderLength); |
| 248 | Append8LE(buf_.get() + 8, start_time_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 249 | if (trace_version_ >= kTraceVersionDualClock) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 250 | Append2LE(buf_.get() + 16, record_size_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 251 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 252 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 253 | // Update current offset. |
| 254 | cur_offset_ = kTraceHeaderLength; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 255 | |
| 256 | // Install all method tracing stubs. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame^] | 257 | Runtime::Current()->GetInstrumentation()->InstallStubs(); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 258 | } |
| 259 | |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 260 | void Trace::FinishTracing() { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 261 | // Uninstall all method tracing stubs. |
jeffhao | 725a957 | 2012-11-13 18:20:12 -0800 | [diff] [blame^] | 262 | Runtime::Current()->GetInstrumentation()->UninstallStubs(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 263 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 264 | // Compute elapsed time. |
| 265 | uint64_t elapsed = MicroTime() - start_time_; |
| 266 | |
| 267 | size_t final_offset = cur_offset_; |
Elliott Hughes | e119a36 | 2012-05-22 17:37:06 -0700 | [diff] [blame] | 268 | uint32_t clock_overhead = GetClockOverhead(this); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 269 | |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 270 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 271 | Runtime::Current()->SetStatsEnabled(false); |
| 272 | } |
| 273 | |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 274 | GetVisitedMethods(final_offset); |
| 275 | |
| 276 | std::ostringstream os; |
| 277 | |
| 278 | os << StringPrintf("%cversion\n", kTraceTokenChar); |
| 279 | os << StringPrintf("%d\n", trace_version_); |
| 280 | os << StringPrintf("data-file-overflow=%s\n", overflow_ ? "true" : "false"); |
| 281 | if (UseThreadCpuClock()) { |
| 282 | if (UseWallClock()) { |
| 283 | os << StringPrintf("clock=dual\n"); |
| 284 | } else { |
| 285 | os << StringPrintf("clock=thread-cpu\n"); |
| 286 | } |
| 287 | } else { |
| 288 | os << StringPrintf("clock=wall\n"); |
| 289 | } |
| 290 | os << StringPrintf("elapsed-time-usec=%llu\n", elapsed); |
Elliott Hughes | ad6c9c3 | 2012-01-19 17:39:12 -0800 | [diff] [blame] | 291 | os << StringPrintf("num-method-calls=%zd\n", (final_offset - kTraceHeaderLength) / record_size_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 292 | os << StringPrintf("clock-call-overhead-nsec=%d\n", clock_overhead); |
| 293 | os << StringPrintf("vm=art\n"); |
jeffhao | 0791adc | 2012-04-04 11:14:32 -0700 | [diff] [blame] | 294 | if ((flags_ & kTraceCountAllocs) != 0) { |
| 295 | os << StringPrintf("alloc-count=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_OBJECTS)); |
| 296 | os << StringPrintf("alloc-size=%d\n", Runtime::Current()->GetStat(KIND_ALLOCATED_BYTES)); |
| 297 | os << StringPrintf("gc-count=%d\n", Runtime::Current()->GetStat(KIND_GC_INVOCATIONS)); |
| 298 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 299 | os << StringPrintf("%cthreads\n", kTraceTokenChar); |
| 300 | DumpThreadList(os); |
| 301 | os << StringPrintf("%cmethods\n", kTraceTokenChar); |
| 302 | DumpMethodList(os); |
| 303 | os << StringPrintf("%cend\n", kTraceTokenChar); |
| 304 | |
| 305 | std::string header(os.str()); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 306 | if (trace_file_.get() == NULL) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 307 | iovec iov[2]; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 308 | iov[0].iov_base = reinterpret_cast<void*>(const_cast<char*>(header.c_str())); |
| 309 | iov[0].iov_len = header.length(); |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 310 | iov[1].iov_base = buf_.get(); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 311 | iov[1].iov_len = final_offset; |
| 312 | Dbg::DdmSendChunkV(CHUNK_TYPE("MPSE"), iov, 2); |
| 313 | } else { |
| 314 | if (!trace_file_->WriteFully(header.c_str(), header.length()) || |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 315 | !trace_file_->WriteFully(buf_.get(), final_offset)) { |
Elliott Hughes | 7b9d996 | 2012-04-20 18:48:18 -0700 | [diff] [blame] | 316 | std::string detail(StringPrintf("Trace data write failed: %s", strerror(errno))); |
| 317 | PLOG(ERROR) << detail; |
| 318 | Thread::Current()->ThrowNewException("Ljava/lang/RuntimeException;", detail.c_str()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 319 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 320 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 321 | } |
| 322 | |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 323 | void Trace::LogMethodTraceEvent(Thread* self, const AbstractMethod* method, Trace::TraceEvent event) { |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 324 | if (thread_clock_base_map_.find(self) == thread_clock_base_map_.end()) { |
| 325 | uint64_t time = ThreadCpuMicroTime(); |
Elliott Hughes | a0e1806 | 2012-04-13 15:59:59 -0700 | [diff] [blame] | 326 | thread_clock_base_map_.Put(self, time); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | // Advance cur_offset_ atomically. |
| 330 | int32_t new_offset; |
| 331 | int32_t old_offset; |
| 332 | do { |
| 333 | old_offset = cur_offset_; |
| 334 | new_offset = old_offset + record_size_; |
| 335 | if (new_offset > buffer_size_) { |
| 336 | overflow_ = true; |
| 337 | return; |
| 338 | } |
| 339 | } while (android_atomic_release_cas(old_offset, new_offset, &cur_offset_) != 0); |
| 340 | |
| 341 | uint32_t method_value = TraceMethodCombine(reinterpret_cast<uint32_t>(method), event); |
| 342 | |
| 343 | // Write data |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 344 | uint8_t* ptr = buf_.get() + old_offset; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 345 | Append2LE(ptr, self->GetTid()); |
| 346 | Append4LE(ptr + 2, method_value); |
| 347 | ptr += 6; |
| 348 | |
| 349 | if (UseThreadCpuClock()) { |
| 350 | uint64_t thread_clock_base = thread_clock_base_map_.find(self)->second; |
| 351 | uint32_t thread_clock_diff = ThreadCpuMicroTime() - thread_clock_base; |
| 352 | Append4LE(ptr, thread_clock_diff); |
| 353 | ptr += 4; |
| 354 | } |
| 355 | |
| 356 | if (UseWallClock()) { |
| 357 | uint32_t wall_clock_diff = MicroTime() - start_time_; |
| 358 | Append4LE(ptr, wall_clock_diff); |
| 359 | } |
| 360 | } |
| 361 | |
| 362 | void Trace::GetVisitedMethods(size_t end_offset) { |
jeffhao | 2692b57 | 2011-12-16 15:42:28 -0800 | [diff] [blame] | 363 | uint8_t* ptr = buf_.get() + kTraceHeaderLength; |
| 364 | uint8_t* end = buf_.get() + end_offset; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 365 | |
| 366 | while (ptr < end) { |
| 367 | uint32_t method_value = ptr[2] | (ptr[3] << 8) | (ptr[4] << 16) | (ptr[5] << 24); |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 368 | AbstractMethod* method = reinterpret_cast<AbstractMethod*>(TraceMethodId(method_value)); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 369 | visited_methods_.insert(method); |
| 370 | ptr += record_size_; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | void Trace::DumpMethodList(std::ostream& os) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 375 | typedef std::set<const AbstractMethod*>::const_iterator It; // TODO: C++0x auto |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 376 | for (It it = visited_methods_.begin(); it != visited_methods_.end(); ++it) { |
Mathieu Chartier | 66f1925 | 2012-09-18 08:57:04 -0700 | [diff] [blame] | 377 | const AbstractMethod* method = *it; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 378 | MethodHelper mh(method); |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 379 | os << StringPrintf("%p\t%s\t%s\t%s\t%s\n", method, |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 380 | PrettyDescriptor(mh.GetDeclaringClassDescriptor()).c_str(), mh.GetName(), |
Ian Rogers | 0399dde | 2012-06-06 17:09:28 -0700 | [diff] [blame] | 381 | mh.GetSignature().c_str(), mh.GetDeclaringClassSourceFile()); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 382 | } |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 383 | } |
| 384 | |
| 385 | static void DumpThread(Thread* t, void* arg) { |
Elliott Hughes | ffb465f | 2012-03-01 18:46:05 -0800 | [diff] [blame] | 386 | std::ostream& os = *reinterpret_cast<std::ostream*>(arg); |
| 387 | std::string name; |
| 388 | t->GetThreadName(name); |
| 389 | os << t->GetTid() << "\t" << name << "\n"; |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | void Trace::DumpThreadList(std::ostream& os) { |
Ian Rogers | 81d425b | 2012-09-27 16:03:43 -0700 | [diff] [blame] | 393 | Thread* self = Thread::Current(); |
| 394 | Locks::thread_list_lock_->AssertNotHeld(self); |
| 395 | MutexLock mu(self, *Locks::thread_list_lock_); |
jeffhao | a9ef3fd | 2011-12-13 18:33:43 -0800 | [diff] [blame] | 396 | Runtime::Current()->GetThreadList()->ForEach(DumpThread, &os); |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 397 | } |
| 398 | |
jeffhao | e343b76 | 2011-12-05 16:36:44 -0800 | [diff] [blame] | 399 | } // namespace art |