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

Unified Diff: base/process/process_mac.cc

Issue 989703002: Add support for backgrounding processes on the Mac (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix compile issue, wrap process backgrounding in an experiment Created 5 years, 9 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
Index: base/process/process_mac.cc
diff --git a/base/process/process_mac.cc b/base/process/process_mac.cc
new file mode 100644
index 0000000000000000000000000000000000000000..5fa98a6ce88ad7afbe358850429f168a7d89d0e7
--- /dev/null
+++ b/base/process/process_mac.cc
@@ -0,0 +1,112 @@
+// Copyright (c) 2015 The Chromium Authors. All rights reserved.
Robert Sesek 2015/03/10 18:27:43 nit: no (c)
shrike 2015/03/11 21:38:21 Done (but FYI I just copy/pasted from process_linu
Robert Sesek 2015/03/12 16:00:23 Yup, the copyright policy changed over time.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "base/process/process.h"
Robert Sesek 2015/03/10 18:27:43 nit: blank line after
shrike 2015/03/11 21:38:21 I added the blank line (what is the rule)?
Robert Sesek 2015/03/12 16:00:23 http://google-styleguide.googlecode.com/svn/trunk/
shrike 2015/03/18 16:20:22 Thank you.
+#include "base/mac/mac_util.h"
+#include <mach/mach.h>
+
+// The following was added to <mach/task_policy.h> after 10.8
Robert Sesek 2015/03/10 18:27:43 nit: punctuation
shrike 2015/03/11 21:38:21 Done.
+#ifndef TASK_OVERRIDE_QOS_POLICY
+
+#define TASK_OVERRIDE_QOS_POLICY 9
+
+typedef struct task_category_policy task_category_policy_data_t;
+typedef struct task_category_policy* task_category_policy_t;
+
+enum task_latency_qos {
+ LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
+ LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
+ LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
+ LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
+ LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
+ LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
+ LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
+};
+typedef integer_t task_latency_qos_t;
+enum task_throughput_qos {
+ THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
+ THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
+ THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
+ THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
+ THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
+ THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
+ THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
+};
+
+#define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
+#define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
+
+typedef integer_t task_throughput_qos_t;
+
+struct task_qos_policy {
+ task_latency_qos_t task_latency_qos_tier;
+ task_throughput_qos_t task_throughput_qos_tier;
+};
+
+typedef struct task_qos_policy* task_qos_policy_t;
+#define TASK_QOS_POLICY_COUNT \
+ ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
+
+#endif // TASK_OVERRIDE_QOS_POLICY
+
+namespace base {
+
+bool Process::CanBackgroundProcesses() {
+ return true;
Robert Sesek 2015/03/10 18:27:43 This isn't true unconditionally. This should proba
shrike 2015/03/11 21:38:21 You can foreground/background a task on prior vers
Robert Sesek 2015/03/12 16:00:23 Ah, my mistake. I missed that in the 10.6 kernel.
+}
+
+bool Process::IsProcessBackgrounded(mach_port_t process_port) const {
+ // See SetProcessBackgrounded().
+ DCHECK(IsValid());
+
+ task_category_policy_data_t category_policy;
+ mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
+ boolean_t get_default = FALSE;
+
+ kern_return_t result = task_policy_get(process_port, TASK_CATEGORY_POLICY,
+ (task_policy_t)&category_policy,
Robert Sesek 2015/03/10 18:27:43 C-style casts are banned by Google style guide. Us
shrike 2015/03/11 21:38:21 Done.
+ &task_info_count, &get_default);
+ DPCHECK(result == KERN_SUCCESS);
Robert Sesek 2015/03/10 18:27:43 This will fail on older kernels. PCHECK is also no
shrike 2015/03/11 21:38:21 Looks like I borrowed some code from process_linux
Robert Sesek 2015/03/12 16:00:23 I suggested you look at mach_logging.h because we
shrike 2015/03/18 16:20:22 Thank you. I haven't done a lot of kernel work so
+
+ if (result == KERN_SUCCESS && get_default == FALSE) {
+ return category_policy.role == TASK_BACKGROUND_APPLICATION;
+ }
+ return false;
+}
+
+bool Process::SetProcessBackgrounded(mach_port_t process_port,
+ bool background) {
+ DCHECK(IsValid());
+
+ if (!CanBackgroundProcesses() || process_port == 0) {
Robert Sesek 2015/03/10 18:27:43 TASK_NULL instead of 0.
shrike 2015/03/11 21:38:21 Done. I know that TASK_NULL is 0 but I had a reaso
+ return false;
+ }
+
+ struct task_category_policy taskCatPolicy;
Robert Sesek 2015/03/10 18:27:43 Naming: use under_score variable names and spell o
shrike 2015/03/11 21:38:21 Done.
+ taskCatPolicy.role =
+ background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
+ kern_return_t result = task_policy_set(process_port, TASK_CATEGORY_POLICY,
+ (thread_policy_t)&taskCatPolicy,
+ TASK_CATEGORY_POLICY_COUNT);
+ DPCHECK(result == KERN_SUCCESS);
+
+ if (result != KERN_SUCCESS) {
+ return false;
+ } else if (!mac::IsOSMavericksOrLater()) {
+ return true;
+ }
+
+ // Latency QoS regulates timer throttling/accuracy. Select default tier
+ // on foreground because precise timer firing isn't needed
+ struct task_qos_policy qosinfo = {
+ background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_LAUNCH_DEFAULT_TIER,
+ background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER};
+ result = task_policy_set(process_port, TASK_OVERRIDE_QOS_POLICY,
+ (task_policy_t)&qosinfo, TASK_QOS_POLICY_COUNT);
+ DPCHECK(result == KERN_SUCCESS);
+
+ return result == KERN_SUCCESS;
+}
+
+} // namespace base

Powered by Google App Engine
This is Rietveld 408576698