 Chromium Code Reviews
 Chromium Code Reviews Issue 1977693002:
  Support CPU throttling on Windows  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master
    
  
    Issue 1977693002:
  Support CPU throttling on Windows  (Closed) 
  Base URL: https://chromium.googlesource.com/chromium/src.git@master| Index: content/renderer/devtools/devtools_cpu_throttler.cc | 
| diff --git a/content/renderer/devtools/devtools_cpu_throttler.cc b/content/renderer/devtools/devtools_cpu_throttler.cc | 
| index 68f37d052523721509ef0078c7bd89af33ed6fa8..7f379af40483769ea68b6ed0d19af85d48eb8524 100644 | 
| --- a/content/renderer/devtools/devtools_cpu_throttler.cc | 
| +++ b/content/renderer/devtools/devtools_cpu_throttler.cc | 
| @@ -12,7 +12,7 @@ | 
| #if defined(OS_POSIX) | 
| #include <signal.h> | 
| -#define USE_SIGNALS | 
| +#define USE_SIGNALS 1 | 
| #endif | 
| using base::subtle::Atomic32; | 
| @@ -37,6 +37,7 @@ class CPUThrottlingThread final : public base::PlatformThread::Delegate { | 
| static void SuspendThread(base::PlatformThreadHandle thread_handle); | 
| static void ResumeThread(base::PlatformThreadHandle thread_handle); | 
| + static void Sleep(base::TimeDelta duration); | 
| #ifdef USE_SIGNALS | 
| void InstallSignalHandler(); | 
| @@ -122,17 +123,21 @@ void CPUThrottlingThread::HandleSignal(int signal) { | 
| // static | 
| void CPUThrottlingThread::SuspendThread( | 
| base::PlatformThreadHandle thread_handle) { | 
| -#ifdef USE_SIGNALS | 
| +#if defined(USE_SIGNALS) | 
| Release_Store(&suspended_, 1); | 
| pthread_kill(thread_handle.platform_handle(), SIGUSR2); | 
| +#elif OS_WIN | 
| 
caseq
2016/05/13 01:49:07
defined(OS_WIN)?
 
alph
2016/05/13 21:00:59
Done.
 | 
| + ::SuspendThread(thread_handle.platform_handle()); | 
| #endif | 
| } | 
| // static | 
| void CPUThrottlingThread::ResumeThread( | 
| base::PlatformThreadHandle thread_handle) { | 
| -#ifdef USE_SIGNALS | 
| +#if defined(USE_SIGNALS) | 
| Release_Store(&suspended_, 0); | 
| +#elif OS_WIN | 
| 
caseq
2016/05/13 01:49:07
ditto
 
alph
2016/05/13 21:00:59
Done.
 | 
| + ::ResumeThread(thread_handle.platform_handle()); | 
| #endif | 
| } | 
| @@ -145,6 +150,17 @@ void CPUThrottlingThread::Start() { | 
| } | 
| } | 
| +void CPUThrottlingThread::Sleep(base::TimeDelta duration) { | 
| +#if defined(USE_SIGNALS) | 
| + base::PlatformThread::Sleep(duration); | 
| +#elif OS_WIN | 
| 
caseq
2016/05/13 01:49:07
ditto
 
alph
2016/05/13 21:00:59
Done.
 | 
| + // We cannot rely on ::Sleep function as it's precision is not enough for | 
| + // the purpose. Could be up to 16ms jitter. | 
| + base::TimeTicks wakeup_time = base::TimeTicks::Now() + duration; | 
| + while (base::TimeTicks::Now() < wakeup_time) {} | 
| +#endif | 
| 
caseq
2016/05/13 01:49:07
What do we do #else?
 
alph
2016/05/13 21:00:59
I don't want it to break the build if CPU throttli
 | 
| +} | 
| + | 
| void CPUThrottlingThread::Stop() { | 
| cancellation_flag_.Set(); | 
| base::PlatformThread::Join(throttling_thread_handle_); | 
| @@ -160,9 +176,9 @@ void CPUThrottlingThread::Throttle() { | 
| base::TimeDelta::FromMicroseconds(static_cast<int>(quant_time_us / rate)); | 
| base::TimeDelta sleep_duration = | 
| base::TimeDelta::FromMicroseconds(quant_time_us) - run_duration; | 
| - base::PlatformThread::Sleep(run_duration); | 
| + Sleep(run_duration); | 
| SuspendThread(throttled_thread_handle_); | 
| - base::PlatformThread::Sleep(sleep_duration); | 
| + Sleep(sleep_duration); | 
| ResumeThread(throttled_thread_handle_); | 
| } |