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