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

Side by Side 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 nit. Created 5 years, 6 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 unified diff | Download patch
« no previous file with comments | « base/process/process.h ('k') | base/process/process_posix.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/process/process.h"
6
7 #include "base/mac/mac_util.h"
8 #include "base/mac/mach_logging.h"
9
10 #include <mach/mach.h>
11
12 // The following was added to <mach/task_policy.h> after 10.8.
13 // TODO(shrike): Remove the TASK_OVERRIDE_QOS_POLICY ifndef once builders
14 // reach 10.9 or higher.
15 #ifndef TASK_OVERRIDE_QOS_POLICY
16
17 #define TASK_OVERRIDE_QOS_POLICY 9
18
19 typedef struct task_category_policy task_category_policy_data_t;
20 typedef struct task_category_policy* task_category_policy_t;
21
22 enum task_latency_qos {
23 LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
24 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
25 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
26 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
27 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
28 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
29 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
30 };
31 typedef integer_t task_latency_qos_t;
32 enum task_throughput_qos {
33 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
34 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
35 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
36 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
37 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
38 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
39 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
40 };
41
42 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
43 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
44
45 typedef integer_t task_throughput_qos_t;
46
47 struct task_qos_policy {
48 task_latency_qos_t task_latency_qos_tier;
49 task_throughput_qos_t task_throughput_qos_tier;
50 };
51
52 typedef struct task_qos_policy* task_qos_policy_t;
53 #define TASK_QOS_POLICY_COUNT \
54 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
55
56 #endif // TASK_OVERRIDE_QOS_POLICY
57
58 namespace base {
59
60 bool Process::CanBackgroundProcesses() {
61 return true;
62 }
63
64 bool Process::IsProcessBackgrounded(mach_port_t task_port) const {
65 // See SetProcessBackgrounded().
66 DCHECK(IsValid());
67 DCHECK_NE(task_port, TASK_NULL);
68
69 task_category_policy_data_t category_policy;
70 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
71 boolean_t get_default = FALSE;
72
73 kern_return_t result =
74 task_policy_get(task_port, TASK_CATEGORY_POLICY,
75 reinterpret_cast<task_policy_t>(&category_policy),
76 &task_info_count, &get_default);
77 MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result) <<
78 "task_policy_get TASK_CATEGORY_POLICY";
79
80 if (result == KERN_SUCCESS && get_default == FALSE) {
81 return category_policy.role == TASK_BACKGROUND_APPLICATION;
82 }
83 return false;
84 }
85
86 bool Process::SetProcessBackgrounded(mach_port_t task_port, bool background) {
87 DCHECK(IsValid());
88 DCHECK_NE(task_port, TASK_NULL);
89
90 if (!CanBackgroundProcesses()) {
91 return false;
92 } else if (IsProcessBackgrounded(task_port) == background) {
93 return true;
94 }
95
96 task_category_policy category_policy;
97 category_policy.role =
98 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
99 kern_return_t result =
100 task_policy_set(task_port, TASK_CATEGORY_POLICY,
101 reinterpret_cast<task_policy_t>(&category_policy),
102 TASK_CATEGORY_POLICY_COUNT);
103
104 if (result != KERN_SUCCESS) {
105 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY";
106 return false;
107 } else if (!mac::IsOSMavericksOrLater()) {
108 return true;
109 }
110
111 // Latency QoS regulates timer throttling/accuracy. Select default tier
112 // on foreground because precise timer firing isn't needed.
113 struct task_qos_policy qos_policy = {
114 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED,
115 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED
116 };
117 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY,
118 reinterpret_cast<task_policy_t>(&qos_policy),
119 TASK_QOS_POLICY_COUNT);
120 if (result != KERN_SUCCESS) {
121 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY";
122 return false;
123 }
124
125 return true;
126 }
127
128 } // namespace base
OLDNEW
« no previous file with comments | « base/process/process.h ('k') | base/process/process_posix.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698