Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(871)

Unified Diff: content/renderer/devtools/devtools_cpu_throttler.cc

Issue 1977693002: Support CPU throttling on Windows (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: addressing comments Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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..277d863492523b8bacdac0771bc5521dbefb75a2 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,29 +123,46 @@ 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 defined(OS_WIN)
+ ::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 defined(OS_WIN)
+ ::ResumeThread(thread_handle.platform_handle());
#endif
}
void CPUThrottlingThread::Start() {
#ifdef USE_SIGNALS
InstallSignalHandler();
+#elif !defined(OS_WIN)
+ LOG(ERROR) << "CPU throttling is not supported."
#endif
if (!base::PlatformThread::Create(0, this, &throttling_thread_handle_)) {
LOG(ERROR) << "Failed to create throttling thread.";
}
}
+void CPUThrottlingThread::Sleep(base::TimeDelta duration) {
+#if defined(OS_WIN)
+ // 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) {}
+#else
+ base::PlatformThread::Sleep(duration);
+#endif
+}
+
void CPUThrottlingThread::Stop() {
cancellation_flag_.Set();
base::PlatformThread::Join(throttling_thread_handle_);
@@ -160,9 +178,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_);
}
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698