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