Dave Allison | 0aded08 | 2013-11-07 13:15:11 -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 | */ |
| 16 | |
| 17 | #include "profiler.h" |
| 18 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 19 | #include <sys/file.h> |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 20 | #include <sys/stat.h> |
| 21 | #include <sys/uio.h> |
| 22 | |
| 23 | #include <fstream> |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 24 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 25 | #include "art_method-inl.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 26 | #include "base/stl_util.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 27 | #include "base/time_utils.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 28 | #include "base/unix_file/fd_file.h" |
| 29 | #include "class_linker.h" |
| 30 | #include "common_throws.h" |
| 31 | #include "debugger.h" |
| 32 | #include "dex_file-inl.h" |
| 33 | #include "instrumentation.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 34 | #include "mirror/class-inl.h" |
| 35 | #include "mirror/dex_cache.h" |
| 36 | #include "mirror/object_array-inl.h" |
| 37 | #include "mirror/object-inl.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 38 | #include "os.h" |
| 39 | #include "scoped_thread_state_change.h" |
| 40 | #include "ScopedLocalRef.h" |
| 41 | #include "thread.h" |
| 42 | #include "thread_list.h" |
Vladimir Marko | 80afd02 | 2015-05-19 18:08:00 +0100 | [diff] [blame] | 43 | #include "utils.h" |
Dave Allison | 4a7867b | 2014-01-30 17:44:12 -0800 | [diff] [blame] | 44 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 45 | #include "entrypoints/quick/quick_entrypoints.h" |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 46 | |
| 47 | namespace art { |
| 48 | |
| 49 | BackgroundMethodSamplingProfiler* BackgroundMethodSamplingProfiler::profiler_ = nullptr; |
| 50 | pthread_t BackgroundMethodSamplingProfiler::profiler_pthread_ = 0U; |
| 51 | volatile bool BackgroundMethodSamplingProfiler::shutting_down_ = false; |
| 52 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 53 | // TODO: this profiler runs regardless of the state of the machine. Maybe we should use the |
| 54 | // wakelock or something to modify the run characteristics. This can be done when we |
| 55 | // have some performance data after it's been used for a while. |
| 56 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 57 | // Walk through the method within depth of max_depth_ on the Java stack |
| 58 | class BoundedStackVisitor : public StackVisitor { |
| 59 | public: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 60 | BoundedStackVisitor(std::vector<std::pair<ArtMethod*, uint32_t>>* stack, |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 61 | Thread* thread, uint32_t max_depth) |
| 62 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) |
Nicolas Geoffray | 8e5bd18 | 2015-05-06 11:34:34 +0100 | [diff] [blame] | 63 | : StackVisitor(thread, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames), |
| 64 | stack_(stack), |
| 65 | max_depth_(max_depth), |
| 66 | depth_(0) {} |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 67 | |
| 68 | bool VisitFrame() SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 69 | ArtMethod* m = GetMethod(); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 70 | if (m->IsRuntimeMethod()) { |
| 71 | return true; |
| 72 | } |
| 73 | uint32_t dex_pc_ = GetDexPc(); |
| 74 | stack_->push_back(std::make_pair(m, dex_pc_)); |
| 75 | ++depth_; |
| 76 | if (depth_ < max_depth_) { |
| 77 | return true; |
| 78 | } else { |
| 79 | return false; |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 84 | std::vector<std::pair<ArtMethod*, uint32_t>>* stack_; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 85 | const uint32_t max_depth_; |
| 86 | uint32_t depth_; |
| 87 | }; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 88 | |
| 89 | // This is called from either a thread list traversal or from a checkpoint. Regardless |
| 90 | // of which caller, the mutator lock must be held. |
| 91 | static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 92 | BackgroundMethodSamplingProfiler* profiler = |
| 93 | reinterpret_cast<BackgroundMethodSamplingProfiler*>(arg); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 94 | const ProfilerOptions profile_options = profiler->GetProfilerOptions(); |
| 95 | switch (profile_options.GetProfileType()) { |
| 96 | case kProfilerMethod: { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 97 | ArtMethod* method = thread->GetCurrentMethod(nullptr); |
Ian Rogers | cf7f191 | 2014-10-22 22:06:39 -0700 | [diff] [blame] | 98 | if ((false) && method == nullptr) { |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 99 | LOG(INFO) << "No current method available"; |
| 100 | std::ostringstream os; |
| 101 | thread->Dump(os); |
| 102 | std::string data(os.str()); |
| 103 | LOG(INFO) << data; |
| 104 | } |
| 105 | profiler->RecordMethod(method); |
| 106 | break; |
| 107 | } |
| 108 | case kProfilerBoundedStack: { |
| 109 | std::vector<InstructionLocation> stack; |
| 110 | uint32_t max_depth = profile_options.GetMaxStackDepth(); |
| 111 | BoundedStackVisitor bounded_stack_visitor(&stack, thread, max_depth); |
| 112 | bounded_stack_visitor.WalkStack(); |
| 113 | profiler->RecordStack(stack); |
| 114 | break; |
| 115 | } |
| 116 | default: |
| 117 | LOG(INFO) << "This profile type is not implemented."; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 118 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 121 | // A closure that is called by the thread checkpoint code. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 122 | class SampleCheckpoint FINAL : public Closure { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 123 | public: |
| 124 | explicit SampleCheckpoint(BackgroundMethodSamplingProfiler* const profiler) : |
| 125 | profiler_(profiler) {} |
| 126 | |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 127 | void Run(Thread* thread) OVERRIDE { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 128 | Thread* self = Thread::Current(); |
| 129 | if (thread == nullptr) { |
| 130 | LOG(ERROR) << "Checkpoint with nullptr thread"; |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | // Grab the mutator lock (shared access). |
| 135 | ScopedObjectAccess soa(self); |
| 136 | |
| 137 | // Grab a sample. |
| 138 | GetSample(thread, this->profiler_); |
| 139 | |
| 140 | // And finally tell the barrier that we're done. |
| 141 | this->profiler_->GetBarrier().Pass(self); |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | BackgroundMethodSamplingProfiler* const profiler_; |
| 146 | }; |
| 147 | |
| 148 | bool BackgroundMethodSamplingProfiler::ShuttingDown(Thread* self) { |
| 149 | MutexLock mu(self, *Locks::profiler_lock_); |
| 150 | return shutting_down_; |
| 151 | } |
| 152 | |
| 153 | void* BackgroundMethodSamplingProfiler::RunProfilerThread(void* arg) { |
| 154 | Runtime* runtime = Runtime::Current(); |
| 155 | BackgroundMethodSamplingProfiler* profiler = |
| 156 | reinterpret_cast<BackgroundMethodSamplingProfiler*>(arg); |
| 157 | |
| 158 | // Add a random delay for the first time run so that we don't hammer the CPU |
| 159 | // with all profiles running at the same time. |
| 160 | const int kRandomDelayMaxSecs = 30; |
| 161 | const double kMaxBackoffSecs = 24*60*60; // Max backoff time. |
| 162 | |
| 163 | srand(MicroTime() * getpid()); |
| 164 | int startup_delay = rand() % kRandomDelayMaxSecs; // random delay for startup. |
| 165 | |
| 166 | |
| 167 | CHECK(runtime->AttachCurrentThread("Profiler", true, runtime->GetSystemThreadGroup(), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 168 | !runtime->IsAotCompiler())); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 169 | |
| 170 | Thread* self = Thread::Current(); |
| 171 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 172 | double backoff = 1.0; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 173 | while (true) { |
| 174 | if (ShuttingDown(self)) { |
| 175 | break; |
| 176 | } |
| 177 | |
| 178 | { |
| 179 | // wait until we need to run another profile |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 180 | uint64_t delay_secs = profiler->options_.GetPeriodS() * backoff; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 181 | |
| 182 | // Add a startup delay to prevent all the profiles running at once. |
| 183 | delay_secs += startup_delay; |
| 184 | |
| 185 | // Immediate startup for benchmarking? |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 186 | if (profiler->options_.GetStartImmediately() && startup_delay > 0) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 187 | delay_secs = 0; |
| 188 | } |
| 189 | |
| 190 | startup_delay = 0; |
| 191 | |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 192 | VLOG(profiler) << "Delaying profile start for " << delay_secs << " secs"; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 193 | MutexLock mu(self, profiler->wait_lock_); |
| 194 | profiler->period_condition_.TimedWait(self, delay_secs * 1000, 0); |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 195 | // We were either signaled by Stop or timedout, in either case ignore the timed out result. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 196 | |
| 197 | // Expand the backoff by its coefficient, but don't go beyond the max. |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 198 | backoff = std::min(backoff * profiler->options_.GetBackoffCoefficient(), kMaxBackoffSecs); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | if (ShuttingDown(self)) { |
| 202 | break; |
| 203 | } |
| 204 | |
| 205 | |
| 206 | uint64_t start_us = MicroTime(); |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 207 | uint64_t end_us = start_us + profiler->options_.GetDurationS() * UINT64_C(1000000); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 208 | uint64_t now_us = start_us; |
| 209 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 210 | VLOG(profiler) << "Starting profiling run now for " |
| 211 | << PrettyDuration((end_us - start_us) * 1000); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 212 | |
| 213 | SampleCheckpoint check_point(profiler); |
| 214 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 215 | size_t valid_samples = 0; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 216 | while (now_us < end_us) { |
| 217 | if (ShuttingDown(self)) { |
| 218 | break; |
| 219 | } |
| 220 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 221 | usleep(profiler->options_.GetIntervalUs()); // Non-interruptible sleep. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 222 | |
| 223 | ThreadList* thread_list = runtime->GetThreadList(); |
| 224 | |
| 225 | profiler->profiler_barrier_->Init(self, 0); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 226 | size_t barrier_count = thread_list->RunCheckpointOnRunnableThreads(&check_point); |
| 227 | |
| 228 | // All threads are suspended, nothing to do. |
| 229 | if (barrier_count == 0) { |
| 230 | now_us = MicroTime(); |
| 231 | continue; |
| 232 | } |
| 233 | |
| 234 | valid_samples += barrier_count; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 235 | |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame] | 236 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 237 | |
| 238 | // Wait for the barrier to be crossed by all runnable threads. This wait |
| 239 | // is done with a timeout so that we can detect problems with the checkpoint |
| 240 | // running code. We should never see this. |
| 241 | const uint32_t kWaitTimeoutMs = 10000; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 242 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 243 | // Wait for all threads to pass the barrier. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 244 | bool timed_out = profiler->profiler_barrier_->Increment(self, barrier_count, kWaitTimeoutMs); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 245 | |
| 246 | // We should never get a timeout. If we do, it suggests a problem with the checkpoint |
| 247 | // code. Crash the process in this case. |
Ian Rogers | 7b078e8 | 2014-09-10 14:44:24 -0700 | [diff] [blame] | 248 | CHECK(!timed_out); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 249 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 250 | // Update the current time. |
| 251 | now_us = MicroTime(); |
| 252 | } |
| 253 | |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame] | 254 | if (valid_samples > 0) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 255 | // After the profile has been taken, write it out. |
| 256 | ScopedObjectAccess soa(self); // Acquire the mutator lock. |
| 257 | uint32_t size = profiler->WriteProfile(); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 258 | VLOG(profiler) << "Profile size: " << size; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 259 | } |
| 260 | } |
| 261 | |
| 262 | LOG(INFO) << "Profiler shutdown"; |
| 263 | runtime->DetachCurrentThread(); |
| 264 | return nullptr; |
| 265 | } |
| 266 | |
| 267 | // Write out the profile file if we are generating a profile. |
| 268 | uint32_t BackgroundMethodSamplingProfiler::WriteProfile() { |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 269 | std::string full_name = output_filename_; |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 270 | VLOG(profiler) << "Saving profile to " << full_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 271 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 272 | int fd = open(full_name.c_str(), O_RDWR); |
| 273 | if (fd < 0) { |
| 274 | // Open failed. |
| 275 | LOG(ERROR) << "Failed to open profile file " << full_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 276 | return 0; |
| 277 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 278 | |
| 279 | // Lock the file for exclusive access. This will block if another process is using |
| 280 | // the file. |
| 281 | int err = flock(fd, LOCK_EX); |
| 282 | if (err < 0) { |
| 283 | LOG(ERROR) << "Failed to lock profile file " << full_name; |
| 284 | return 0; |
| 285 | } |
| 286 | |
| 287 | // Read the previous profile. |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 288 | profile_table_.ReadPrevious(fd, options_.GetProfileType()); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 289 | |
| 290 | // Move back to the start of the file. |
| 291 | lseek(fd, 0, SEEK_SET); |
| 292 | |
| 293 | // Format the profile output and write to the file. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 294 | std::ostringstream os; |
| 295 | uint32_t num_methods = DumpProfile(os); |
| 296 | std::string data(os.str()); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 297 | const char *p = data.c_str(); |
| 298 | size_t length = data.length(); |
| 299 | size_t full_length = length; |
| 300 | do { |
| 301 | int n = ::write(fd, p, length); |
| 302 | p += n; |
| 303 | length -= n; |
| 304 | } while (length > 0); |
| 305 | |
| 306 | // Truncate the file to the new length. |
Elliott Hughes | 06f08e4 | 2015-05-12 21:25:36 -0700 | [diff] [blame] | 307 | if (ftruncate(fd, full_length) == -1) { |
| 308 | LOG(ERROR) << "Failed to truncate profile file " << full_name; |
| 309 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 310 | |
| 311 | // Now unlock the file, allowing another process in. |
| 312 | err = flock(fd, LOCK_UN); |
| 313 | if (err < 0) { |
| 314 | LOG(ERROR) << "Failed to unlock profile file " << full_name; |
| 315 | } |
| 316 | |
| 317 | // Done, close the file. |
| 318 | ::close(fd); |
| 319 | |
| 320 | // Clean the profile for the next time. |
| 321 | CleanProfile(); |
| 322 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 323 | return num_methods; |
| 324 | } |
| 325 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 326 | bool BackgroundMethodSamplingProfiler::Start( |
| 327 | const std::string& output_filename, const ProfilerOptions& options) { |
| 328 | if (!options.IsEnabled()) { |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 329 | return false; |
| 330 | } |
| 331 | |
| 332 | CHECK(!output_filename.empty()); |
| 333 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 334 | Thread* self = Thread::Current(); |
| 335 | { |
| 336 | MutexLock mu(self, *Locks::profiler_lock_); |
| 337 | // Don't start two profiler threads. |
| 338 | if (profiler_ != nullptr) { |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 339 | return true; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 343 | LOG(INFO) << "Starting profiler using output file: " << output_filename |
| 344 | << " and options: " << options; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 345 | { |
| 346 | MutexLock mu(self, *Locks::profiler_lock_); |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 347 | profiler_ = new BackgroundMethodSamplingProfiler(output_filename, options); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 348 | |
| 349 | CHECK_PTHREAD_CALL(pthread_create, (&profiler_pthread_, nullptr, &RunProfilerThread, |
| 350 | reinterpret_cast<void*>(profiler_)), |
| 351 | "Profiler thread"); |
| 352 | } |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 353 | return true; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 354 | } |
| 355 | |
| 356 | |
| 357 | |
| 358 | void BackgroundMethodSamplingProfiler::Stop() { |
| 359 | BackgroundMethodSamplingProfiler* profiler = nullptr; |
| 360 | pthread_t profiler_pthread = 0U; |
| 361 | { |
| 362 | MutexLock trace_mu(Thread::Current(), *Locks::profiler_lock_); |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame] | 363 | CHECK(!shutting_down_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 364 | profiler = profiler_; |
| 365 | shutting_down_ = true; |
| 366 | profiler_pthread = profiler_pthread_; |
| 367 | } |
| 368 | |
| 369 | // Now wake up the sampler thread if it sleeping. |
| 370 | { |
| 371 | MutexLock profile_mu(Thread::Current(), profiler->wait_lock_); |
| 372 | profiler->period_condition_.Signal(Thread::Current()); |
| 373 | } |
| 374 | // Wait for the sample thread to stop. |
| 375 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profiler thread shutdown"); |
| 376 | |
| 377 | { |
| 378 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 379 | profiler_ = nullptr; |
| 380 | } |
| 381 | delete profiler; |
| 382 | } |
| 383 | |
| 384 | |
| 385 | void BackgroundMethodSamplingProfiler::Shutdown() { |
| 386 | Stop(); |
| 387 | } |
| 388 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 389 | BackgroundMethodSamplingProfiler::BackgroundMethodSamplingProfiler( |
| 390 | const std::string& output_filename, const ProfilerOptions& options) |
| 391 | : output_filename_(output_filename), |
| 392 | options_(options), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 393 | wait_lock_("Profile wait lock"), |
| 394 | period_condition_("Profile condition", wait_lock_), |
| 395 | profile_table_(wait_lock_), |
| 396 | profiler_barrier_(new Barrier(0)) { |
| 397 | // Populate the filtered_methods set. |
| 398 | // This is empty right now, but to add a method, do this: |
| 399 | // |
| 400 | // filtered_methods_.insert("void java.lang.Object.wait(long, int)"); |
| 401 | } |
| 402 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 403 | // Filter out methods the profiler doesn't want to record. |
| 404 | // We require mutator lock since some statistics will be updated here. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 405 | bool BackgroundMethodSamplingProfiler::ProcessMethod(ArtMethod* method) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 406 | if (method == nullptr) { |
| 407 | profile_table_.NullMethod(); |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 408 | // Don't record a null method. |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 409 | return false; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | mirror::Class* cls = method->GetDeclaringClass(); |
| 413 | if (cls != nullptr) { |
| 414 | if (cls->GetClassLoader() == nullptr) { |
| 415 | // Don't include things in the boot |
| 416 | profile_table_.BootMethod(); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 417 | return false; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 418 | } |
| 419 | } |
| 420 | |
| 421 | bool is_filtered = false; |
| 422 | |
Mathieu Chartier | bfd9a43 | 2014-05-21 17:43:44 -0700 | [diff] [blame] | 423 | if (strcmp(method->GetName(), "<clinit>") == 0) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 424 | // always filter out class init |
| 425 | is_filtered = true; |
| 426 | } |
| 427 | |
| 428 | // Filter out methods by name if there are any. |
| 429 | if (!is_filtered && filtered_methods_.size() > 0) { |
| 430 | std::string method_full_name = PrettyMethod(method); |
| 431 | |
| 432 | // Don't include specific filtered methods. |
| 433 | is_filtered = filtered_methods_.count(method_full_name) != 0; |
| 434 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 435 | return !is_filtered; |
| 436 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 437 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 438 | // A method has been hit, record its invocation in the method map. |
| 439 | // The mutator_lock must be held (shared) when this is called. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 440 | void BackgroundMethodSamplingProfiler::RecordMethod(ArtMethod* method) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 441 | // Add to the profile table unless it is filtered out. |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 442 | if (ProcessMethod(method)) { |
| 443 | profile_table_.Put(method); |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | // Record the current bounded stack into sampling results. |
| 448 | void BackgroundMethodSamplingProfiler::RecordStack(const std::vector<InstructionLocation>& stack) { |
| 449 | if (stack.size() == 0) { |
| 450 | return; |
| 451 | } |
| 452 | // Get the method on top of the stack. We use this method to perform filtering. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 453 | ArtMethod* method = stack.front().first; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 454 | if (ProcessMethod(method)) { |
| 455 | profile_table_.PutStack(stack); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 456 | } |
| 457 | } |
| 458 | |
| 459 | // Clean out any recordings for the method traces. |
| 460 | void BackgroundMethodSamplingProfiler::CleanProfile() { |
| 461 | profile_table_.Clear(); |
| 462 | } |
| 463 | |
| 464 | uint32_t BackgroundMethodSamplingProfiler::DumpProfile(std::ostream& os) { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 465 | return profile_table_.Write(os, options_.GetProfileType()); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | // Profile Table. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 469 | // This holds a mapping of ArtMethod* to a count of how many times a sample |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 470 | // hit it at the top of the stack. |
| 471 | ProfileSampleResults::ProfileSampleResults(Mutex& lock) : lock_(lock), num_samples_(0), |
| 472 | num_null_methods_(0), |
| 473 | num_boot_methods_(0) { |
| 474 | for (int i = 0; i < kHashSize; i++) { |
| 475 | table[i] = nullptr; |
| 476 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 477 | method_context_table = nullptr; |
| 478 | stack_trie_root_ = nullptr; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | ProfileSampleResults::~ProfileSampleResults() { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 482 | Clear(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 483 | } |
| 484 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 485 | // Add a method to the profile table. If it's the first time the method |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 486 | // has been seen, add it with count=1, otherwise increment the count. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 487 | void ProfileSampleResults::Put(ArtMethod* method) { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 488 | MutexLock mu(Thread::Current(), lock_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 489 | uint32_t index = Hash(method); |
| 490 | if (table[index] == nullptr) { |
| 491 | table[index] = new Map(); |
| 492 | } |
| 493 | Map::iterator i = table[index]->find(method); |
| 494 | if (i == table[index]->end()) { |
| 495 | (*table[index])[method] = 1; |
| 496 | } else { |
| 497 | i->second++; |
| 498 | } |
| 499 | num_samples_++; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 502 | // Add a bounded stack to the profile table. Only the count of the method on |
| 503 | // top of the frame will be increased. |
| 504 | void ProfileSampleResults::PutStack(const std::vector<InstructionLocation>& stack) { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 505 | MutexLock mu(Thread::Current(), lock_); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 506 | ScopedObjectAccess soa(Thread::Current()); |
| 507 | if (stack_trie_root_ == nullptr) { |
| 508 | // The root of the stack trie is a dummy node so that we don't have to maintain |
| 509 | // a collection of tries. |
| 510 | stack_trie_root_ = new StackTrieNode(); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 511 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 512 | |
| 513 | StackTrieNode* current = stack_trie_root_; |
| 514 | if (stack.size() == 0) { |
| 515 | current->IncreaseCount(); |
| 516 | return; |
| 517 | } |
| 518 | |
| 519 | for (std::vector<InstructionLocation>::const_reverse_iterator iter = stack.rbegin(); |
| 520 | iter != stack.rend(); ++iter) { |
| 521 | InstructionLocation inst_loc = *iter; |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 522 | ArtMethod* method = inst_loc.first; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 523 | if (method == nullptr) { |
| 524 | // skip null method |
| 525 | continue; |
| 526 | } |
| 527 | uint32_t dex_pc = inst_loc.second; |
| 528 | uint32_t method_idx = method->GetDexMethodIndex(); |
| 529 | const DexFile* dex_file = method->GetDeclaringClass()->GetDexCache()->GetDexFile(); |
| 530 | MethodReference method_ref(dex_file, method_idx); |
| 531 | StackTrieNode* child = current->FindChild(method_ref, dex_pc); |
| 532 | if (child != nullptr) { |
| 533 | current = child; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 534 | } else { |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 535 | uint32_t method_size = 0; |
| 536 | const DexFile::CodeItem* codeitem = method->GetCodeItem(); |
| 537 | if (codeitem != nullptr) { |
| 538 | method_size = codeitem->insns_size_in_code_units_; |
| 539 | } |
| 540 | StackTrieNode* new_node = new StackTrieNode(method_ref, dex_pc, method_size, current); |
| 541 | current->AppendChild(new_node); |
| 542 | current = new_node; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 543 | } |
| 544 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 545 | |
| 546 | if (current != stack_trie_root_ && current->GetCount() == 0) { |
| 547 | // Insert into method_context table; |
| 548 | if (method_context_table == nullptr) { |
| 549 | method_context_table = new MethodContextMap(); |
| 550 | } |
| 551 | MethodReference method = current->GetMethod(); |
| 552 | MethodContextMap::iterator i = method_context_table->find(method); |
| 553 | if (i == method_context_table->end()) { |
| 554 | TrieNodeSet* node_set = new TrieNodeSet(); |
| 555 | node_set->insert(current); |
| 556 | (*method_context_table)[method] = node_set; |
| 557 | } else { |
| 558 | TrieNodeSet* node_set = i->second; |
| 559 | node_set->insert(current); |
| 560 | } |
| 561 | } |
| 562 | current->IncreaseCount(); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 563 | num_samples_++; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 564 | } |
| 565 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 566 | // Write the profile table to the output stream. Also merge with the previous profile. |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 567 | uint32_t ProfileSampleResults::Write(std::ostream& os, ProfileDataType type) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 568 | ScopedObjectAccess soa(Thread::Current()); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 569 | num_samples_ += previous_num_samples_; |
| 570 | num_null_methods_ += previous_num_null_methods_; |
| 571 | num_boot_methods_ += previous_num_boot_methods_; |
| 572 | |
Calin Juravle | c1b643c | 2014-05-30 23:44:11 +0100 | [diff] [blame] | 573 | VLOG(profiler) << "Profile: " |
| 574 | << num_samples_ << "/" << num_null_methods_ << "/" << num_boot_methods_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 575 | os << num_samples_ << "/" << num_null_methods_ << "/" << num_boot_methods_ << "\n"; |
| 576 | uint32_t num_methods = 0; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 577 | if (type == kProfilerMethod) { |
| 578 | for (int i = 0 ; i < kHashSize; i++) { |
| 579 | Map *map = table[i]; |
| 580 | if (map != nullptr) { |
| 581 | for (const auto &meth_iter : *map) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 582 | ArtMethod *method = meth_iter.first; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 583 | std::string method_name = PrettyMethod(method); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 584 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 585 | const DexFile::CodeItem* codeitem = method->GetCodeItem(); |
| 586 | uint32_t method_size = 0; |
| 587 | if (codeitem != nullptr) { |
| 588 | method_size = codeitem->insns_size_in_code_units_; |
| 589 | } |
| 590 | uint32_t count = meth_iter.second; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 591 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 592 | // Merge this profile entry with one from a previous run (if present). Also |
| 593 | // remove the previous entry. |
| 594 | PreviousProfile::iterator pi = previous_.find(method_name); |
| 595 | if (pi != previous_.end()) { |
| 596 | count += pi->second.count_; |
| 597 | previous_.erase(pi); |
| 598 | } |
| 599 | os << StringPrintf("%s/%u/%u\n", method_name.c_str(), count, method_size); |
| 600 | ++num_methods; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 601 | } |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 602 | } |
| 603 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 604 | } else if (type == kProfilerBoundedStack) { |
| 605 | if (method_context_table != nullptr) { |
| 606 | for (const auto &method_iter : *method_context_table) { |
| 607 | MethodReference method = method_iter.first; |
| 608 | TrieNodeSet* node_set = method_iter.second; |
| 609 | std::string method_name = PrettyMethod(method.dex_method_index, *(method.dex_file)); |
| 610 | uint32_t method_size = 0; |
| 611 | uint32_t total_count = 0; |
| 612 | PreviousContextMap new_context_map; |
| 613 | for (const auto &trie_node_i : *node_set) { |
| 614 | StackTrieNode* node = trie_node_i; |
| 615 | method_size = node->GetMethodSize(); |
| 616 | uint32_t count = node->GetCount(); |
| 617 | uint32_t dexpc = node->GetDexPC(); |
| 618 | total_count += count; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 619 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 620 | StackTrieNode* current = node->GetParent(); |
| 621 | // We go backward on the trie to retrieve context and dex_pc until the dummy root. |
| 622 | // The format of the context is "method_1@pc_1@method_2@pc_2@..." |
| 623 | std::vector<std::string> context_vector; |
| 624 | while (current != nullptr && current->GetParent() != nullptr) { |
| 625 | context_vector.push_back(StringPrintf("%s@%u", |
| 626 | PrettyMethod(current->GetMethod().dex_method_index, *(current->GetMethod().dex_file)).c_str(), |
| 627 | current->GetDexPC())); |
| 628 | current = current->GetParent(); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 629 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 630 | std::string context_sig = Join(context_vector, '@'); |
| 631 | new_context_map[std::make_pair(dexpc, context_sig)] = count; |
| 632 | } |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 633 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 634 | PreviousProfile::iterator pi = previous_.find(method_name); |
| 635 | if (pi != previous_.end()) { |
| 636 | total_count += pi->second.count_; |
| 637 | PreviousContextMap* previous_context_map = pi->second.context_map_; |
| 638 | if (previous_context_map != nullptr) { |
| 639 | for (const auto &context_i : *previous_context_map) { |
| 640 | uint32_t count = context_i.second; |
| 641 | PreviousContextMap::iterator ci = new_context_map.find(context_i.first); |
| 642 | if (ci == new_context_map.end()) { |
| 643 | new_context_map[context_i.first] = count; |
| 644 | } else { |
| 645 | ci->second += count; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 646 | } |
| 647 | } |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 648 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 649 | delete previous_context_map; |
| 650 | previous_.erase(pi); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 651 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 652 | // We write out profile data with dex pc and context information in the following format: |
| 653 | // "method/total_count/size/[pc_1:count_1:context_1#pc_2:count_2:context_2#...]". |
| 654 | std::vector<std::string> context_count_vector; |
| 655 | for (const auto &context_i : new_context_map) { |
| 656 | context_count_vector.push_back(StringPrintf("%u:%u:%s", context_i.first.first, |
| 657 | context_i.second, context_i.first.second.c_str())); |
| 658 | } |
| 659 | os << StringPrintf("%s/%u/%u/[%s]\n", method_name.c_str(), total_count, |
| 660 | method_size, Join(context_count_vector, '#').c_str()); |
| 661 | ++num_methods; |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 662 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 663 | } |
| 664 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 665 | |
| 666 | // Now we write out the remaining previous methods. |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 667 | for (const auto &pi : previous_) { |
| 668 | if (type == kProfilerMethod) { |
| 669 | os << StringPrintf("%s/%u/%u\n", pi.first.c_str(), pi.second.count_, pi.second.method_size_); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 670 | } else if (type == kProfilerBoundedStack) { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 671 | os << StringPrintf("%s/%u/%u/[", pi.first.c_str(), pi.second.count_, pi.second.method_size_); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 672 | PreviousContextMap* previous_context_map = pi.second.context_map_; |
| 673 | if (previous_context_map != nullptr) { |
| 674 | std::vector<std::string> context_count_vector; |
| 675 | for (const auto &context_i : *previous_context_map) { |
| 676 | context_count_vector.push_back(StringPrintf("%u:%u:%s", context_i.first.first, |
| 677 | context_i.second, context_i.first.second.c_str())); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 678 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 679 | os << Join(context_count_vector, '#'); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 680 | } |
| 681 | os << "]\n"; |
| 682 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 683 | ++num_methods; |
| 684 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 685 | return num_methods; |
| 686 | } |
| 687 | |
| 688 | void ProfileSampleResults::Clear() { |
| 689 | num_samples_ = 0; |
| 690 | num_null_methods_ = 0; |
| 691 | num_boot_methods_ = 0; |
| 692 | for (int i = 0; i < kHashSize; i++) { |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 693 | delete table[i]; |
| 694 | table[i] = nullptr; |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 695 | } |
| 696 | if (stack_trie_root_ != nullptr) { |
| 697 | stack_trie_root_->DeleteChildren(); |
| 698 | delete stack_trie_root_; |
| 699 | stack_trie_root_ = nullptr; |
| 700 | if (method_context_table != nullptr) { |
| 701 | delete method_context_table; |
| 702 | method_context_table = nullptr; |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 703 | } |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 704 | } |
| 705 | for (auto &pi : previous_) { |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 706 | if (pi.second.context_map_ != nullptr) { |
| 707 | delete pi.second.context_map_; |
| 708 | pi.second.context_map_ = nullptr; |
| 709 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 710 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 711 | previous_.clear(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 712 | } |
| 713 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame^] | 714 | uint32_t ProfileSampleResults::Hash(ArtMethod* method) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 715 | return (PointerToLowMemUInt32(method) >> 3) % kHashSize; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 716 | } |
| 717 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 718 | // Read a single line into the given string. Returns true if everything OK, false |
| 719 | // on EOF or error. |
| 720 | static bool ReadProfileLine(int fd, std::string& line) { |
| 721 | char buf[4]; |
| 722 | line.clear(); |
| 723 | while (true) { |
| 724 | int n = read(fd, buf, 1); // TODO: could speed this up but is it worth it? |
| 725 | if (n != 1) { |
| 726 | return false; |
| 727 | } |
| 728 | if (buf[0] == '\n') { |
| 729 | break; |
| 730 | } |
| 731 | line += buf[0]; |
| 732 | } |
| 733 | return true; |
| 734 | } |
| 735 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 736 | void ProfileSampleResults::ReadPrevious(int fd, ProfileDataType type) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 737 | // Reset counters. |
| 738 | previous_num_samples_ = previous_num_null_methods_ = previous_num_boot_methods_ = 0; |
| 739 | |
| 740 | std::string line; |
| 741 | |
| 742 | // The first line contains summary information. |
| 743 | if (!ReadProfileLine(fd, line)) { |
| 744 | return; |
| 745 | } |
| 746 | std::vector<std::string> summary_info; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 747 | Split(line, '/', &summary_info); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 748 | if (summary_info.size() != 3) { |
| 749 | // Bad summary info. It should be count/nullcount/bootcount |
| 750 | return; |
| 751 | } |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 752 | previous_num_samples_ = strtoul(summary_info[0].c_str(), nullptr, 10); |
| 753 | previous_num_null_methods_ = strtoul(summary_info[1].c_str(), nullptr, 10); |
| 754 | previous_num_boot_methods_ = strtoul(summary_info[2].c_str(), nullptr, 10); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 755 | |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 756 | // Now read each line until the end of file. Each line consists of 3 or 4 fields separated by / |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 757 | while (true) { |
| 758 | if (!ReadProfileLine(fd, line)) { |
| 759 | break; |
| 760 | } |
| 761 | std::vector<std::string> info; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 762 | Split(line, '/', &info); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 763 | if (info.size() != 3 && info.size() != 4) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 764 | // Malformed. |
| 765 | break; |
| 766 | } |
| 767 | std::string methodname = info[0]; |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 768 | uint32_t total_count = strtoul(info[1].c_str(), nullptr, 10); |
| 769 | uint32_t size = strtoul(info[2].c_str(), nullptr, 10); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 770 | PreviousContextMap* context_map = nullptr; |
| 771 | if (type == kProfilerBoundedStack && info.size() == 4) { |
| 772 | context_map = new PreviousContextMap(); |
| 773 | std::string context_counts_str = info[3].substr(1, info[3].size() - 2); |
| 774 | std::vector<std::string> context_count_pairs; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 775 | Split(context_counts_str, '#', &context_count_pairs); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 776 | for (uint32_t i = 0; i < context_count_pairs.size(); ++i) { |
| 777 | std::vector<std::string> context_count; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 778 | Split(context_count_pairs[i], ':', &context_count); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 779 | if (context_count.size() == 2) { |
| 780 | // Handles the situtation when the profile file doesn't contain context information. |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 781 | uint32_t dexpc = strtoul(context_count[0].c_str(), nullptr, 10); |
| 782 | uint32_t count = strtoul(context_count[1].c_str(), nullptr, 10); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 783 | (*context_map)[std::make_pair(dexpc, "")] = count; |
| 784 | } else { |
| 785 | // Handles the situtation when the profile file contains context information. |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 786 | uint32_t dexpc = strtoul(context_count[0].c_str(), nullptr, 10); |
| 787 | uint32_t count = strtoul(context_count[1].c_str(), nullptr, 10); |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 788 | std::string context = context_count[2]; |
| 789 | (*context_map)[std::make_pair(dexpc, context)] = count; |
| 790 | } |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 791 | } |
| 792 | } |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 793 | previous_[methodname] = PreviousValue(total_count, size, context_map); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 794 | } |
| 795 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 796 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 797 | bool ProfileFile::LoadFile(const std::string& fileName) { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 798 | LOG(VERBOSE) << "reading profile file " << fileName; |
| 799 | struct stat st; |
| 800 | int err = stat(fileName.c_str(), &st); |
| 801 | if (err == -1) { |
| 802 | LOG(VERBOSE) << "not found"; |
| 803 | return false; |
| 804 | } |
| 805 | if (st.st_size == 0) { |
Dave Allison | 644789f | 2014-04-10 13:06:10 -0700 | [diff] [blame] | 806 | return false; // Empty profiles are invalid. |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 807 | } |
| 808 | std::ifstream in(fileName.c_str()); |
| 809 | if (!in) { |
| 810 | LOG(VERBOSE) << "profile file " << fileName << " exists but can't be opened"; |
| 811 | LOG(VERBOSE) << "file owner: " << st.st_uid << ":" << st.st_gid; |
| 812 | LOG(VERBOSE) << "me: " << getuid() << ":" << getgid(); |
| 813 | LOG(VERBOSE) << "file permissions: " << std::oct << st.st_mode; |
| 814 | LOG(VERBOSE) << "errno: " << errno; |
| 815 | return false; |
| 816 | } |
| 817 | // The first line contains summary information. |
| 818 | std::string line; |
| 819 | std::getline(in, line); |
| 820 | if (in.eof()) { |
| 821 | return false; |
| 822 | } |
| 823 | std::vector<std::string> summary_info; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 824 | Split(line, '/', &summary_info); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 825 | if (summary_info.size() != 3) { |
Calin Juravle | 19477a8 | 2014-06-06 12:24:21 +0100 | [diff] [blame] | 826 | // Bad summary info. It should be total/null/boot. |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 827 | return false; |
| 828 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 829 | // This is the number of hits in all profiled methods (without null or boot methods) |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 830 | uint32_t total_count = strtoul(summary_info[0].c_str(), nullptr, 10); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 831 | |
| 832 | // Now read each line until the end of file. Each line consists of 3 fields separated by '/'. |
| 833 | // Store the info in descending order given by the most used methods. |
| 834 | typedef std::set<std::pair<int, std::vector<std::string>>> ProfileSet; |
| 835 | ProfileSet countSet; |
| 836 | while (!in.eof()) { |
| 837 | std::getline(in, line); |
| 838 | if (in.eof()) { |
| 839 | break; |
| 840 | } |
| 841 | std::vector<std::string> info; |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 842 | Split(line, '/', &info); |
Wei Jin | a93b0bb | 2014-06-09 16:19:15 -0700 | [diff] [blame] | 843 | if (info.size() != 3 && info.size() != 4) { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 844 | // Malformed. |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 845 | return false; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 846 | } |
| 847 | int count = atoi(info[1].c_str()); |
| 848 | countSet.insert(std::make_pair(-count, info)); |
| 849 | } |
| 850 | |
| 851 | uint32_t curTotalCount = 0; |
| 852 | ProfileSet::iterator end = countSet.end(); |
| 853 | const ProfileData* prevData = nullptr; |
| 854 | for (ProfileSet::iterator it = countSet.begin(); it != end ; it++) { |
| 855 | const std::string& methodname = it->second[0]; |
| 856 | uint32_t count = -it->first; |
Wei Jin | f21f0a9 | 2014-06-27 17:44:18 -0700 | [diff] [blame] | 857 | uint32_t size = strtoul(it->second[2].c_str(), nullptr, 10); |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 858 | double usedPercent = (count * 100.0) / total_count; |
| 859 | |
| 860 | curTotalCount += count; |
| 861 | // Methods with the same count should be part of the same top K percentage bucket. |
| 862 | double topKPercentage = (prevData != nullptr) && (prevData->GetCount() == count) |
| 863 | ? prevData->GetTopKUsedPercentage() |
| 864 | : 100 * static_cast<double>(curTotalCount) / static_cast<double>(total_count); |
| 865 | |
| 866 | // Add it to the profile map. |
| 867 | ProfileData curData = ProfileData(methodname, count, size, usedPercent, topKPercentage); |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 868 | profile_map_[methodname] = curData; |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 869 | prevData = &curData; |
| 870 | } |
| 871 | return true; |
| 872 | } |
| 873 | |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 874 | bool ProfileFile::GetProfileData(ProfileFile::ProfileData* data, const std::string& method_name) { |
| 875 | ProfileMap::iterator i = profile_map_.find(method_name); |
| 876 | if (i == profile_map_.end()) { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 877 | return false; |
| 878 | } |
Calin Juravle | bb0b53f | 2014-05-23 17:33:29 +0100 | [diff] [blame] | 879 | *data = i->second; |
| 880 | return true; |
| 881 | } |
| 882 | |
| 883 | bool ProfileFile::GetTopKSamples(std::set<std::string>& topKSamples, double topKPercentage) { |
| 884 | ProfileMap::iterator end = profile_map_.end(); |
| 885 | for (ProfileMap::iterator it = profile_map_.begin(); it != end; it++) { |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 886 | if (it->second.GetTopKUsedPercentage() < topKPercentage) { |
| 887 | topKSamples.insert(it->first); |
| 888 | } |
| 889 | } |
| 890 | return true; |
| 891 | } |
| 892 | |
Wei Jin | 445220d | 2014-06-20 15:56:53 -0700 | [diff] [blame] | 893 | StackTrieNode* StackTrieNode::FindChild(MethodReference method, uint32_t dex_pc) { |
| 894 | if (children_.size() == 0) { |
| 895 | return nullptr; |
| 896 | } |
| 897 | // Create a dummy node for searching. |
| 898 | StackTrieNode* node = new StackTrieNode(method, dex_pc, 0, nullptr); |
| 899 | std::set<StackTrieNode*, StackTrieNodeComparator>::iterator i = children_.find(node); |
| 900 | delete node; |
| 901 | return (i == children_.end()) ? nullptr : *i; |
| 902 | } |
| 903 | |
| 904 | void StackTrieNode::DeleteChildren() { |
| 905 | for (auto &child : children_) { |
| 906 | if (child != nullptr) { |
| 907 | child->DeleteChildren(); |
| 908 | delete child; |
| 909 | } |
| 910 | } |
| 911 | } |
| 912 | |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 913 | } // namespace art |