Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "platform/globals.h" | |
| 6 #if defined(TARGET_OS_ANDROID) | |
| 7 | |
| 8 #include "vm/profiler.h" | |
| 9 #include "vm/signal_handler.h" | |
| 10 | |
| 11 namespace dart { | |
| 12 | |
| 13 DECLARE_FLAG(bool, profile); | |
| 14 | |
| 15 static void ProfileSignalAction(int signal, siginfo_t* info, void* context_) { | |
| 16 if (signal != SIGPROF) { | |
| 17 return; | |
| 18 } | |
| 19 ucontext_t* context = reinterpret_cast<ucontext_t*>(context_); | |
| 20 mcontext_t mcontext = context->uc_mcontext; | |
| 21 Isolate* isolate = Isolate::Current(); | |
| 22 if (isolate == NULL) { | |
| 23 return; | |
| 24 } | |
| 25 { | |
| 26 ScopedMutexLock profiler_data_lock(isolate->profiler_data_mutex()); | |
| 27 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
| 28 if (profiler_data == NULL) { | |
| 29 return; | |
| 30 } | |
| 31 | |
| 32 uintptr_t stack_lower; | |
| 33 uintptr_t stack_upper; | |
| 34 bool r = isolate->GetStackBounds(&stack_lower, &stack_upper); | |
| 35 ASSERT(r); | |
| 36 uintptr_t PC = SignalHandler::GetProgramCounter(mcontext); | |
| 37 uintptr_t FP = SignalHandler::GetFramePointer(mcontext); | |
| 38 uintptr_t SP = SignalHandler::GetStackPointer(mcontext); | |
| 39 stack_lower = SignalHandler::GetStackPointer(mcontext); | |
| 40 int64_t sample_time = OS::GetCurrentTimeMicros(); | |
| 41 profiler_data->SampledAt(sample_time); | |
| 42 CollectSample(profiler_data, PC, FP, SP, stack_lower, stack_upper); | |
| 43 } | |
| 44 ProfilerManager::ScheduleIsolate(isolate); | |
| 45 } | |
| 46 | |
| 47 | |
| 48 int64_t ProfilerManager::SampleAndRescheduleIsolates(int64_t current_time) { | |
| 49 if (isolates_size_ == 0) { | |
| 50 return 0; | |
| 51 } | |
| 52 static const int64_t max_time = 0x7fffffffffffffffLL; | |
| 53 int64_t lowest = max_time; | |
| 54 for (intptr_t i = 0; i < isolates_size_; i++) { | |
| 55 Isolate* isolate = isolates_[i]; | |
| 56 ScopedMutexLock isolate_lock(isolate->profiler_data_mutex()); | |
| 57 IsolateProfilerData* profiler_data = isolate->profiler_data(); | |
| 58 ASSERT(profiler_data != NULL); | |
| 59 if (profiler_data->ShouldSample(current_time)) { | |
| 60 pthread_kill(profiler_data->thread_id(), SIGPROF); | |
| 61 RemoveIsolate(i); | |
| 62 i--; // Remove moves the last element into i, this decrement cancels | |
| 63 // the increment in the for loop. | |
|
siva
2013/11/11 03:51:54
Seems hacky why not use a while loop
while (i < is
Cutch
2013/11/18 20:48:54
Done.
| |
| 64 continue; | |
| 65 } | |
| 66 if (profiler_data->CanExpire()) { | |
| 67 int64_t isolate_time_left = | |
| 68 profiler_data->TimeUntilExpiration(current_time); | |
| 69 if (isolate_time_left < 0) { | |
| 70 continue; | |
| 71 } | |
| 72 if (isolate_time_left < lowest) { | |
| 73 lowest = isolate_time_left; | |
| 74 } | |
| 75 } | |
| 76 } | |
| 77 if (isolates_size_ == 0) { | |
| 78 return 0; | |
| 79 } | |
| 80 if (lowest == max_time) { | |
| 81 return 0; | |
| 82 } | |
| 83 ASSERT(lowest != max_time); | |
| 84 ASSERT(lowest > 0); | |
| 85 return lowest; | |
| 86 } | |
| 87 | |
| 88 | |
| 89 void ProfilerManager::ThreadMain(uword parameters) { | |
| 90 ASSERT(initialized_); | |
| 91 ASSERT(FLAG_profile); | |
| 92 SignalHandler::Install(ProfileSignalAction); | |
| 93 ScopedMonitorLock lock(monitor_); | |
| 94 while (!shutdown_) { | |
| 95 int64_t current_time = OS::GetCurrentTimeMicros(); | |
| 96 int64_t next_sample = SampleAndRescheduleIsolates(current_time); | |
| 97 lock.WaitMicros(next_sample); | |
| 98 } | |
| 99 } | |
| 100 | |
| 101 | |
| 102 } // namespace dart | |
| 103 | |
| 104 #endif // defined(TARGET_OS_ANDROID) | |
| OLD | NEW |