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