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