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 | |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 19 | #include <fstream> |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 20 | #include <sys/uio.h> |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 21 | #include <sys/file.h> |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 22 | |
| 23 | #include "base/stl_util.h" |
| 24 | #include "base/unix_file/fd_file.h" |
| 25 | #include "class_linker.h" |
| 26 | #include "common_throws.h" |
| 27 | #include "debugger.h" |
| 28 | #include "dex_file-inl.h" |
| 29 | #include "instrumentation.h" |
| 30 | #include "mirror/art_method-inl.h" |
| 31 | #include "mirror/class-inl.h" |
| 32 | #include "mirror/dex_cache.h" |
| 33 | #include "mirror/object_array-inl.h" |
| 34 | #include "mirror/object-inl.h" |
| 35 | #include "object_utils.h" |
| 36 | #include "os.h" |
| 37 | #include "scoped_thread_state_change.h" |
| 38 | #include "ScopedLocalRef.h" |
| 39 | #include "thread.h" |
| 40 | #include "thread_list.h" |
Dave Allison | 4a7867b | 2014-01-30 17:44:12 -0800 | [diff] [blame] | 41 | |
| 42 | #ifdef HAVE_ANDROID_OS |
| 43 | #include "cutils/properties.h" |
| 44 | #endif |
| 45 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 46 | #if !defined(ART_USE_PORTABLE_COMPILER) |
| 47 | #include "entrypoints/quick/quick_entrypoints.h" |
| 48 | #endif |
| 49 | |
| 50 | namespace art { |
| 51 | |
| 52 | BackgroundMethodSamplingProfiler* BackgroundMethodSamplingProfiler::profiler_ = nullptr; |
| 53 | pthread_t BackgroundMethodSamplingProfiler::profiler_pthread_ = 0U; |
| 54 | volatile bool BackgroundMethodSamplingProfiler::shutting_down_ = false; |
| 55 | |
| 56 | |
| 57 | // TODO: this profiler runs regardless of the state of the machine. Maybe we should use the |
| 58 | // wakelock or something to modify the run characteristics. This can be done when we |
| 59 | // have some performance data after it's been used for a while. |
| 60 | |
| 61 | |
| 62 | // This is called from either a thread list traversal or from a checkpoint. Regardless |
| 63 | // of which caller, the mutator lock must be held. |
| 64 | static void GetSample(Thread* thread, void* arg) SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 65 | BackgroundMethodSamplingProfiler* profiler = |
| 66 | reinterpret_cast<BackgroundMethodSamplingProfiler*>(arg); |
| 67 | mirror::ArtMethod* method = thread->GetCurrentMethod(nullptr); |
| 68 | if (false && method == nullptr) { |
| 69 | LOG(INFO) << "No current method available"; |
| 70 | std::ostringstream os; |
| 71 | thread->Dump(os); |
| 72 | std::string data(os.str()); |
| 73 | LOG(INFO) << data; |
| 74 | } |
| 75 | profiler->RecordMethod(method); |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | // A closure that is called by the thread checkpoint code. |
| 81 | class SampleCheckpoint : public Closure { |
| 82 | public: |
| 83 | explicit SampleCheckpoint(BackgroundMethodSamplingProfiler* const profiler) : |
| 84 | profiler_(profiler) {} |
| 85 | |
| 86 | virtual void Run(Thread* thread) NO_THREAD_SAFETY_ANALYSIS { |
| 87 | Thread* self = Thread::Current(); |
| 88 | if (thread == nullptr) { |
| 89 | LOG(ERROR) << "Checkpoint with nullptr thread"; |
| 90 | return; |
| 91 | } |
| 92 | |
| 93 | // Grab the mutator lock (shared access). |
| 94 | ScopedObjectAccess soa(self); |
| 95 | |
| 96 | // Grab a sample. |
| 97 | GetSample(thread, this->profiler_); |
| 98 | |
| 99 | // And finally tell the barrier that we're done. |
| 100 | this->profiler_->GetBarrier().Pass(self); |
| 101 | } |
| 102 | |
| 103 | private: |
| 104 | BackgroundMethodSamplingProfiler* const profiler_; |
| 105 | }; |
| 106 | |
| 107 | bool BackgroundMethodSamplingProfiler::ShuttingDown(Thread* self) { |
| 108 | MutexLock mu(self, *Locks::profiler_lock_); |
| 109 | return shutting_down_; |
| 110 | } |
| 111 | |
| 112 | void* BackgroundMethodSamplingProfiler::RunProfilerThread(void* arg) { |
| 113 | Runtime* runtime = Runtime::Current(); |
| 114 | BackgroundMethodSamplingProfiler* profiler = |
| 115 | reinterpret_cast<BackgroundMethodSamplingProfiler*>(arg); |
| 116 | |
| 117 | // Add a random delay for the first time run so that we don't hammer the CPU |
| 118 | // with all profiles running at the same time. |
| 119 | const int kRandomDelayMaxSecs = 30; |
| 120 | const double kMaxBackoffSecs = 24*60*60; // Max backoff time. |
| 121 | |
| 122 | srand(MicroTime() * getpid()); |
| 123 | int startup_delay = rand() % kRandomDelayMaxSecs; // random delay for startup. |
| 124 | |
| 125 | |
| 126 | CHECK(runtime->AttachCurrentThread("Profiler", true, runtime->GetSystemThreadGroup(), |
| 127 | !runtime->IsCompiler())); |
| 128 | |
| 129 | Thread* self = Thread::Current(); |
| 130 | |
| 131 | while (true) { |
| 132 | if (ShuttingDown(self)) { |
| 133 | break; |
| 134 | } |
| 135 | |
| 136 | { |
| 137 | // wait until we need to run another profile |
| 138 | uint64_t delay_secs = profiler->period_s_ * profiler->backoff_factor_; |
| 139 | |
| 140 | // Add a startup delay to prevent all the profiles running at once. |
| 141 | delay_secs += startup_delay; |
| 142 | |
| 143 | // Immediate startup for benchmarking? |
| 144 | if (profiler->start_immediately_ && startup_delay > 0) { |
| 145 | delay_secs = 0; |
| 146 | } |
| 147 | |
| 148 | startup_delay = 0; |
| 149 | |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 150 | VLOG(profiler) << "Delaying profile start for " << delay_secs << " secs"; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 151 | MutexLock mu(self, profiler->wait_lock_); |
| 152 | profiler->period_condition_.TimedWait(self, delay_secs * 1000, 0); |
| 153 | |
| 154 | // Expand the backoff by its coefficient, but don't go beyond the max. |
| 155 | double new_backoff = profiler->backoff_factor_ * profiler->backoff_coefficient_; |
| 156 | if (new_backoff < kMaxBackoffSecs) { |
| 157 | profiler->backoff_factor_ = new_backoff; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if (ShuttingDown(self)) { |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | |
| 166 | uint64_t start_us = MicroTime(); |
Ian Rogers | 0f67847 | 2014-03-10 16:18:37 -0700 | [diff] [blame] | 167 | uint64_t end_us = start_us + profiler->duration_s_ * UINT64_C(1000000); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 168 | uint64_t now_us = start_us; |
| 169 | |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 170 | VLOG(profiler) << "Starting profiling run now for " << PrettyDuration((end_us - start_us) * 1000); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 171 | |
| 172 | |
| 173 | SampleCheckpoint check_point(profiler); |
| 174 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 175 | size_t valid_samples = 0; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 176 | while (now_us < end_us) { |
| 177 | if (ShuttingDown(self)) { |
| 178 | break; |
| 179 | } |
| 180 | |
| 181 | usleep(profiler->interval_us_); // Non-interruptible sleep. |
| 182 | |
| 183 | ThreadList* thread_list = runtime->GetThreadList(); |
| 184 | |
| 185 | profiler->profiler_barrier_->Init(self, 0); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 186 | size_t barrier_count = thread_list->RunCheckpointOnRunnableThreads(&check_point); |
| 187 | |
| 188 | // All threads are suspended, nothing to do. |
| 189 | if (barrier_count == 0) { |
| 190 | now_us = MicroTime(); |
| 191 | continue; |
| 192 | } |
| 193 | |
| 194 | valid_samples += barrier_count; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 195 | |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame^] | 196 | ScopedThreadStateChange tsc(self, kWaitingForCheckPointsToRun); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 197 | |
| 198 | // Wait for the barrier to be crossed by all runnable threads. This wait |
| 199 | // is done with a timeout so that we can detect problems with the checkpoint |
| 200 | // running code. We should never see this. |
| 201 | const uint32_t kWaitTimeoutMs = 10000; |
| 202 | const uint32_t kWaitTimeoutUs = kWaitTimeoutMs * 1000; |
| 203 | |
| 204 | uint64_t waitstart_us = MicroTime(); |
| 205 | // Wait for all threads to pass the barrier. |
| 206 | profiler->profiler_barrier_->Increment(self, barrier_count, kWaitTimeoutMs); |
| 207 | uint64_t waitend_us = MicroTime(); |
| 208 | uint64_t waitdiff_us = waitend_us - waitstart_us; |
| 209 | |
| 210 | // We should never get a timeout. If we do, it suggests a problem with the checkpoint |
| 211 | // code. Crash the process in this case. |
| 212 | CHECK_LT(waitdiff_us, kWaitTimeoutUs); |
| 213 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 214 | // Update the current time. |
| 215 | now_us = MicroTime(); |
| 216 | } |
| 217 | |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame^] | 218 | if (valid_samples > 0) { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 219 | // After the profile has been taken, write it out. |
| 220 | ScopedObjectAccess soa(self); // Acquire the mutator lock. |
| 221 | uint32_t size = profiler->WriteProfile(); |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 222 | VLOG(profiler) << "Profile size: " << size; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 223 | } |
| 224 | } |
| 225 | |
| 226 | LOG(INFO) << "Profiler shutdown"; |
| 227 | runtime->DetachCurrentThread(); |
| 228 | return nullptr; |
| 229 | } |
| 230 | |
| 231 | // Write out the profile file if we are generating a profile. |
| 232 | uint32_t BackgroundMethodSamplingProfiler::WriteProfile() { |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 233 | std::string full_name = profile_file_name_; |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 234 | VLOG(profiler) << "Saving profile to " << full_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 235 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 236 | int fd = open(full_name.c_str(), O_RDWR); |
| 237 | if (fd < 0) { |
| 238 | // Open failed. |
| 239 | LOG(ERROR) << "Failed to open profile file " << full_name; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 240 | return 0; |
| 241 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 242 | |
| 243 | // Lock the file for exclusive access. This will block if another process is using |
| 244 | // the file. |
| 245 | int err = flock(fd, LOCK_EX); |
| 246 | if (err < 0) { |
| 247 | LOG(ERROR) << "Failed to lock profile file " << full_name; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | // Read the previous profile. |
| 252 | profile_table_.ReadPrevious(fd); |
| 253 | |
| 254 | // Move back to the start of the file. |
| 255 | lseek(fd, 0, SEEK_SET); |
| 256 | |
| 257 | // Format the profile output and write to the file. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 258 | std::ostringstream os; |
| 259 | uint32_t num_methods = DumpProfile(os); |
| 260 | std::string data(os.str()); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 261 | const char *p = data.c_str(); |
| 262 | size_t length = data.length(); |
| 263 | size_t full_length = length; |
| 264 | do { |
| 265 | int n = ::write(fd, p, length); |
| 266 | p += n; |
| 267 | length -= n; |
| 268 | } while (length > 0); |
| 269 | |
| 270 | // Truncate the file to the new length. |
| 271 | ftruncate(fd, full_length); |
| 272 | |
| 273 | // Now unlock the file, allowing another process in. |
| 274 | err = flock(fd, LOCK_UN); |
| 275 | if (err < 0) { |
| 276 | LOG(ERROR) << "Failed to unlock profile file " << full_name; |
| 277 | } |
| 278 | |
| 279 | // Done, close the file. |
| 280 | ::close(fd); |
| 281 | |
| 282 | // Clean the profile for the next time. |
| 283 | CleanProfile(); |
| 284 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 285 | return num_methods; |
| 286 | } |
| 287 | |
| 288 | // Start a profile thread with the user-supplied arguments. |
| 289 | void BackgroundMethodSamplingProfiler::Start(int period, int duration, |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 290 | const std::string& profile_file_name, const std::string& procName, |
| 291 | int interval_us, |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 292 | double backoff_coefficient, bool startImmediately) { |
| 293 | Thread* self = Thread::Current(); |
| 294 | { |
| 295 | MutexLock mu(self, *Locks::profiler_lock_); |
| 296 | // Don't start two profiler threads. |
| 297 | if (profiler_ != nullptr) { |
| 298 | return; |
| 299 | } |
| 300 | } |
| 301 | |
Dave Allison | 4a7867b | 2014-01-30 17:44:12 -0800 | [diff] [blame] | 302 | // Only on target... |
| 303 | #ifdef HAVE_ANDROID_OS |
Dave Allison | 2d524dd | 2014-04-09 13:51:55 -0700 | [diff] [blame] | 304 | // Switch off profiler if the dalvik.vm.profiler property has value 0. |
| 305 | char buf[PROP_VALUE_MAX]; |
| 306 | property_get("dalvik.vm.profiler", buf, "0"); |
| 307 | if (strcmp(buf, "0") == 0) { |
| 308 | LOG(INFO) << "Profiler disabled. To enable setprop dalvik.vm.profiler 1"; |
| 309 | return; |
Dave Allison | 4a7867b | 2014-01-30 17:44:12 -0800 | [diff] [blame] | 310 | } |
| 311 | #endif |
| 312 | |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 313 | LOG(INFO) << "Starting profile with period " << period << "s, duration " << duration << |
| 314 | "s, interval " << interval_us << "us. Profile file " << profile_file_name; |
| 315 | |
| 316 | { |
| 317 | MutexLock mu(self, *Locks::profiler_lock_); |
| 318 | profiler_ = new BackgroundMethodSamplingProfiler(period, duration, profile_file_name, |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 319 | procName, |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 320 | backoff_coefficient, |
| 321 | interval_us, startImmediately); |
| 322 | |
| 323 | CHECK_PTHREAD_CALL(pthread_create, (&profiler_pthread_, nullptr, &RunProfilerThread, |
| 324 | reinterpret_cast<void*>(profiler_)), |
| 325 | "Profiler thread"); |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | |
| 330 | |
| 331 | void BackgroundMethodSamplingProfiler::Stop() { |
| 332 | BackgroundMethodSamplingProfiler* profiler = nullptr; |
| 333 | pthread_t profiler_pthread = 0U; |
| 334 | { |
| 335 | MutexLock trace_mu(Thread::Current(), *Locks::profiler_lock_); |
Wei Jin | 6a58691 | 2014-05-21 16:07:40 -0700 | [diff] [blame^] | 336 | CHECK(!shutting_down_); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 337 | profiler = profiler_; |
| 338 | shutting_down_ = true; |
| 339 | profiler_pthread = profiler_pthread_; |
| 340 | } |
| 341 | |
| 342 | // Now wake up the sampler thread if it sleeping. |
| 343 | { |
| 344 | MutexLock profile_mu(Thread::Current(), profiler->wait_lock_); |
| 345 | profiler->period_condition_.Signal(Thread::Current()); |
| 346 | } |
| 347 | // Wait for the sample thread to stop. |
| 348 | CHECK_PTHREAD_CALL(pthread_join, (profiler_pthread, nullptr), "profiler thread shutdown"); |
| 349 | |
| 350 | { |
| 351 | MutexLock mu(Thread::Current(), *Locks::profiler_lock_); |
| 352 | profiler_ = nullptr; |
| 353 | } |
| 354 | delete profiler; |
| 355 | } |
| 356 | |
| 357 | |
| 358 | void BackgroundMethodSamplingProfiler::Shutdown() { |
| 359 | Stop(); |
| 360 | } |
| 361 | |
| 362 | BackgroundMethodSamplingProfiler::BackgroundMethodSamplingProfiler(int period, int duration, |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 363 | const std::string& profile_file_name, |
| 364 | const std::string& process_name, |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 365 | double backoff_coefficient, int interval_us, bool startImmediately) |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 366 | : profile_file_name_(profile_file_name), process_name_(process_name), |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 367 | period_s_(period), start_immediately_(startImmediately), |
| 368 | interval_us_(interval_us), backoff_factor_(1.0), |
| 369 | backoff_coefficient_(backoff_coefficient), duration_s_(duration), |
| 370 | wait_lock_("Profile wait lock"), |
| 371 | period_condition_("Profile condition", wait_lock_), |
| 372 | profile_table_(wait_lock_), |
| 373 | profiler_barrier_(new Barrier(0)) { |
| 374 | // Populate the filtered_methods set. |
| 375 | // This is empty right now, but to add a method, do this: |
| 376 | // |
| 377 | // filtered_methods_.insert("void java.lang.Object.wait(long, int)"); |
| 378 | } |
| 379 | |
| 380 | // A method has been hit, record its invocation in the method map. |
| 381 | // The mutator_lock must be held (shared) when this is called. |
| 382 | void BackgroundMethodSamplingProfiler::RecordMethod(mirror::ArtMethod* method) { |
| 383 | if (method == nullptr) { |
| 384 | profile_table_.NullMethod(); |
| 385 | // Don't record a nullptr method. |
| 386 | return; |
| 387 | } |
| 388 | |
| 389 | mirror::Class* cls = method->GetDeclaringClass(); |
| 390 | if (cls != nullptr) { |
| 391 | if (cls->GetClassLoader() == nullptr) { |
| 392 | // Don't include things in the boot |
| 393 | profile_table_.BootMethod(); |
| 394 | return; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | bool is_filtered = false; |
| 399 | |
| 400 | MethodHelper mh(method); |
| 401 | if (strcmp(mh.GetName(), "<clinit>") == 0) { |
| 402 | // always filter out class init |
| 403 | is_filtered = true; |
| 404 | } |
| 405 | |
| 406 | // Filter out methods by name if there are any. |
| 407 | if (!is_filtered && filtered_methods_.size() > 0) { |
| 408 | std::string method_full_name = PrettyMethod(method); |
| 409 | |
| 410 | // Don't include specific filtered methods. |
| 411 | is_filtered = filtered_methods_.count(method_full_name) != 0; |
| 412 | } |
| 413 | |
| 414 | // Add to the profile table unless it is filtered out. |
| 415 | if (!is_filtered) { |
| 416 | profile_table_.Put(method); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | // Clean out any recordings for the method traces. |
| 421 | void BackgroundMethodSamplingProfiler::CleanProfile() { |
| 422 | profile_table_.Clear(); |
| 423 | } |
| 424 | |
| 425 | uint32_t BackgroundMethodSamplingProfiler::DumpProfile(std::ostream& os) { |
| 426 | return profile_table_.Write(os); |
| 427 | } |
| 428 | |
| 429 | // Profile Table. |
| 430 | // This holds a mapping of mirror::ArtMethod* to a count of how many times a sample |
| 431 | // hit it at the top of the stack. |
| 432 | ProfileSampleResults::ProfileSampleResults(Mutex& lock) : lock_(lock), num_samples_(0), |
| 433 | num_null_methods_(0), |
| 434 | num_boot_methods_(0) { |
| 435 | for (int i = 0; i < kHashSize; i++) { |
| 436 | table[i] = nullptr; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | ProfileSampleResults::~ProfileSampleResults() { |
| 441 | for (int i = 0; i < kHashSize; i++) { |
| 442 | delete table[i]; |
| 443 | } |
| 444 | } |
| 445 | |
| 446 | // Add a method to the profile table. If it the first time the method |
| 447 | // has been seen, add it with count=1, otherwise increment the count. |
| 448 | void ProfileSampleResults::Put(mirror::ArtMethod* method) { |
| 449 | lock_.Lock(Thread::Current()); |
| 450 | uint32_t index = Hash(method); |
| 451 | if (table[index] == nullptr) { |
| 452 | table[index] = new Map(); |
| 453 | } |
| 454 | Map::iterator i = table[index]->find(method); |
| 455 | if (i == table[index]->end()) { |
| 456 | (*table[index])[method] = 1; |
| 457 | } else { |
| 458 | i->second++; |
| 459 | } |
| 460 | num_samples_++; |
| 461 | lock_.Unlock(Thread::Current()); |
| 462 | } |
| 463 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 464 | // Write the profile table to the output stream. Also merge with the previous profile. |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 465 | uint32_t ProfileSampleResults::Write(std::ostream &os) { |
| 466 | ScopedObjectAccess soa(Thread::Current()); |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 467 | num_samples_ += previous_num_samples_; |
| 468 | num_null_methods_ += previous_num_null_methods_; |
| 469 | num_boot_methods_ += previous_num_boot_methods_; |
| 470 | |
Brian Carlstrom | 4d466a8 | 2014-05-08 19:05:29 -0700 | [diff] [blame] | 471 | VLOG(profiler) << "Profile: " << num_samples_ << "/" << num_null_methods_ << "/" << num_boot_methods_; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 472 | os << num_samples_ << "/" << num_null_methods_ << "/" << num_boot_methods_ << "\n"; |
| 473 | uint32_t num_methods = 0; |
| 474 | for (int i = 0 ; i < kHashSize; i++) { |
| 475 | Map *map = table[i]; |
| 476 | if (map != nullptr) { |
| 477 | for (const auto &meth_iter : *map) { |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 478 | mirror::ArtMethod *method = meth_iter.first; |
| 479 | std::string method_name = PrettyMethod(method); |
| 480 | |
| 481 | MethodHelper mh(method); |
| 482 | const DexFile::CodeItem* codeitem = mh.GetCodeItem(); |
| 483 | uint32_t method_size = 0; |
| 484 | if (codeitem != nullptr) { |
| 485 | method_size = codeitem->insns_size_in_code_units_; |
| 486 | } |
| 487 | uint32_t count = meth_iter.second; |
| 488 | |
| 489 | // Merge this profile entry with one from a previous run (if present). Also |
| 490 | // remove the previous entry. |
| 491 | PreviousProfile::iterator pi = previous_.find(method_name); |
| 492 | if (pi != previous_.end()) { |
| 493 | count += pi->second.count_; |
| 494 | previous_.erase(pi); |
| 495 | } |
| 496 | os << StringPrintf("%s/%u/%u\n", method_name.c_str(), count, method_size); |
| 497 | ++num_methods; |
| 498 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 499 | } |
| 500 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 501 | |
| 502 | // Now we write out the remaining previous methods. |
| 503 | for (PreviousProfile::iterator pi = previous_.begin(); pi != previous_.end(); ++pi) { |
| 504 | os << StringPrintf("%s/%u/%u\n", pi->first.c_str(), pi->second.count_, pi->second.method_size_); |
| 505 | ++num_methods; |
| 506 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 507 | return num_methods; |
| 508 | } |
| 509 | |
| 510 | void ProfileSampleResults::Clear() { |
| 511 | num_samples_ = 0; |
| 512 | num_null_methods_ = 0; |
| 513 | num_boot_methods_ = 0; |
| 514 | for (int i = 0; i < kHashSize; i++) { |
| 515 | delete table[i]; |
| 516 | table[i] = nullptr; |
| 517 | } |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 518 | previous_.clear(); |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 519 | } |
| 520 | |
| 521 | uint32_t ProfileSampleResults::Hash(mirror::ArtMethod* method) { |
Ian Rogers | ef7d42f | 2014-01-06 12:55:46 -0800 | [diff] [blame] | 522 | return (PointerToLowMemUInt32(method) >> 3) % kHashSize; |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 523 | } |
| 524 | |
Dave Allison | 39c3bfb | 2014-01-28 18:33:52 -0800 | [diff] [blame] | 525 | // Read a single line into the given string. Returns true if everything OK, false |
| 526 | // on EOF or error. |
| 527 | static bool ReadProfileLine(int fd, std::string& line) { |
| 528 | char buf[4]; |
| 529 | line.clear(); |
| 530 | while (true) { |
| 531 | int n = read(fd, buf, 1); // TODO: could speed this up but is it worth it? |
| 532 | if (n != 1) { |
| 533 | return false; |
| 534 | } |
| 535 | if (buf[0] == '\n') { |
| 536 | break; |
| 537 | } |
| 538 | line += buf[0]; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
| 543 | void ProfileSampleResults::ReadPrevious(int fd) { |
| 544 | // Reset counters. |
| 545 | previous_num_samples_ = previous_num_null_methods_ = previous_num_boot_methods_ = 0; |
| 546 | |
| 547 | std::string line; |
| 548 | |
| 549 | // The first line contains summary information. |
| 550 | if (!ReadProfileLine(fd, line)) { |
| 551 | return; |
| 552 | } |
| 553 | std::vector<std::string> summary_info; |
| 554 | Split(line, '/', summary_info); |
| 555 | if (summary_info.size() != 3) { |
| 556 | // Bad summary info. It should be count/nullcount/bootcount |
| 557 | return; |
| 558 | } |
| 559 | previous_num_samples_ = atoi(summary_info[0].c_str()); |
| 560 | previous_num_null_methods_ = atoi(summary_info[1].c_str()); |
| 561 | previous_num_boot_methods_ = atoi(summary_info[2].c_str()); |
| 562 | |
| 563 | // Now read each line until the end of file. Each line consists of 3 fields separated by / |
| 564 | while (true) { |
| 565 | if (!ReadProfileLine(fd, line)) { |
| 566 | break; |
| 567 | } |
| 568 | std::vector<std::string> info; |
| 569 | Split(line, '/', info); |
| 570 | if (info.size() != 3) { |
| 571 | // Malformed. |
| 572 | break; |
| 573 | } |
| 574 | std::string methodname = info[0]; |
| 575 | uint32_t count = atoi(info[1].c_str()); |
| 576 | uint32_t size = atoi(info[2].c_str()); |
| 577 | previous_[methodname] = PreviousValue(count, size); |
| 578 | } |
| 579 | } |
Dave Allison | 0aded08 | 2013-11-07 13:15:11 -0800 | [diff] [blame] | 580 | |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 581 | bool ProfileHelper::LoadProfileMap(ProfileMap& profileMap, const std::string& fileName) { |
| 582 | LOG(VERBOSE) << "reading profile file " << fileName; |
| 583 | struct stat st; |
| 584 | int err = stat(fileName.c_str(), &st); |
| 585 | if (err == -1) { |
| 586 | LOG(VERBOSE) << "not found"; |
| 587 | return false; |
| 588 | } |
| 589 | if (st.st_size == 0) { |
Dave Allison | 644789f | 2014-04-10 13:06:10 -0700 | [diff] [blame] | 590 | return false; // Empty profiles are invalid. |
Calin Juravle | 9dae5b4 | 2014-04-07 16:36:21 +0300 | [diff] [blame] | 591 | } |
| 592 | std::ifstream in(fileName.c_str()); |
| 593 | if (!in) { |
| 594 | LOG(VERBOSE) << "profile file " << fileName << " exists but can't be opened"; |
| 595 | LOG(VERBOSE) << "file owner: " << st.st_uid << ":" << st.st_gid; |
| 596 | LOG(VERBOSE) << "me: " << getuid() << ":" << getgid(); |
| 597 | LOG(VERBOSE) << "file permissions: " << std::oct << st.st_mode; |
| 598 | LOG(VERBOSE) << "errno: " << errno; |
| 599 | return false; |
| 600 | } |
| 601 | // The first line contains summary information. |
| 602 | std::string line; |
| 603 | std::getline(in, line); |
| 604 | if (in.eof()) { |
| 605 | return false; |
| 606 | } |
| 607 | std::vector<std::string> summary_info; |
| 608 | Split(line, '/', summary_info); |
| 609 | if (summary_info.size() != 3) { |
| 610 | // Bad summary info. It should be count/total/bootpath. |
| 611 | return false; |
| 612 | } |
| 613 | // This is the number of hits in all methods. |
| 614 | uint32_t total_count = 0; |
| 615 | for (int i = 0 ; i < 3; ++i) { |
| 616 | total_count += atoi(summary_info[i].c_str()); |
| 617 | } |
| 618 | |
| 619 | // Now read each line until the end of file. Each line consists of 3 fields separated by '/'. |
| 620 | // Store the info in descending order given by the most used methods. |
| 621 | typedef std::set<std::pair<int, std::vector<std::string>>> ProfileSet; |
| 622 | ProfileSet countSet; |
| 623 | while (!in.eof()) { |
| 624 | std::getline(in, line); |
| 625 | if (in.eof()) { |
| 626 | break; |
| 627 | } |
| 628 | std::vector<std::string> info; |
| 629 | Split(line, '/', info); |
| 630 | if (info.size() != 3) { |
| 631 | // Malformed. |
| 632 | break; |
| 633 | } |
| 634 | int count = atoi(info[1].c_str()); |
| 635 | countSet.insert(std::make_pair(-count, info)); |
| 636 | } |
| 637 | |
| 638 | uint32_t curTotalCount = 0; |
| 639 | ProfileSet::iterator end = countSet.end(); |
| 640 | const ProfileData* prevData = nullptr; |
| 641 | for (ProfileSet::iterator it = countSet.begin(); it != end ; it++) { |
| 642 | const std::string& methodname = it->second[0]; |
| 643 | uint32_t count = -it->first; |
| 644 | uint32_t size = atoi(it->second[2].c_str()); |
| 645 | double usedPercent = (count * 100.0) / total_count; |
| 646 | |
| 647 | curTotalCount += count; |
| 648 | // Methods with the same count should be part of the same top K percentage bucket. |
| 649 | double topKPercentage = (prevData != nullptr) && (prevData->GetCount() == count) |
| 650 | ? prevData->GetTopKUsedPercentage() |
| 651 | : 100 * static_cast<double>(curTotalCount) / static_cast<double>(total_count); |
| 652 | |
| 653 | // Add it to the profile map. |
| 654 | ProfileData curData = ProfileData(methodname, count, size, usedPercent, topKPercentage); |
| 655 | profileMap[methodname] = curData; |
| 656 | prevData = &curData; |
| 657 | } |
| 658 | return true; |
| 659 | } |
| 660 | |
| 661 | bool ProfileHelper::LoadTopKSamples(std::set<std::string>& topKSamples, const std::string& fileName, |
| 662 | double topKPercentage) { |
| 663 | ProfileMap profileMap; |
| 664 | bool loadOk = LoadProfileMap(profileMap, fileName); |
| 665 | if (!loadOk) { |
| 666 | return false; |
| 667 | } |
| 668 | ProfileMap::iterator end = profileMap.end(); |
| 669 | for (ProfileMap::iterator it = profileMap.begin(); it != end; it++) { |
| 670 | if (it->second.GetTopKUsedPercentage() < topKPercentage) { |
| 671 | topKSamples.insert(it->first); |
| 672 | } |
| 673 | } |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | } // namespace art |