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

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: Clean up field trial logic 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 unified diff | Download patch
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 <mach/mach.h>
9
10 // The following was added to <mach/task_policy.h> after 10.8.
11 #ifndef TASK_OVERRIDE_QOS_POLICY
12
13 #define TASK_OVERRIDE_QOS_POLICY 9
14
15 typedef struct task_category_policy task_category_policy_data_t;
16 typedef struct task_category_policy* task_category_policy_t;
17
18 enum task_latency_qos {
19 LATENCY_QOS_TIER_UNSPECIFIED = 0x0,
20 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1),
21 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2),
22 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3),
23 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4),
24 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5),
25 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6)
26 };
27 typedef integer_t task_latency_qos_t;
28 enum task_throughput_qos {
29 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0,
30 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1),
31 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2),
32 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3),
33 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4),
34 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5),
35 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6),
36 };
37
38 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3
39 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3
40
41 typedef integer_t task_throughput_qos_t;
42
43 struct task_qos_policy {
44 task_latency_qos_t task_latency_qos_tier;
45 task_throughput_qos_t task_throughput_qos_tier;
46 };
47
48 typedef struct task_qos_policy* task_qos_policy_t;
49 #define TASK_QOS_POLICY_COUNT \
50 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t)))
51
52 #endif // TASK_OVERRIDE_QOS_POLICY
53
54 namespace base {
55
56 bool Process::CanBackgroundProcesses() {
57 return true;
58 }
59
60 bool Process::IsProcessBackgrounded(mach_port_t process_port) const {
61 // See SetProcessBackgrounded().
62 DCHECK(IsValid());
63
64 task_category_policy_data_t category_policy;
65 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT;
66 boolean_t get_default = FALSE;
67
68 kern_return_t result =
69 task_policy_get(process_port, TASK_CATEGORY_POLICY,
70 reinterpret_cast<task_policy_t>(&category_policy),
71 &task_info_count, &get_default);
72 DCHECK(result == KERN_SUCCESS);
73
74 if (result == KERN_SUCCESS && get_default == FALSE) {
75 return category_policy.role == TASK_BACKGROUND_APPLICATION;
76 }
77 return false;
78 }
79
80 bool Process::SetProcessBackgrounded(mach_port_t process_port,
81 bool background) {
82 DCHECK(IsValid());
83
84 if (!CanBackgroundProcesses() || process_port == TASK_NULL) {
85 return false;
86 }
87
88 struct task_category_policy task_category_policy;
89 task_category_policy.role =
90 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION;
91 kern_return_t result =
92 task_policy_set(process_port, TASK_CATEGORY_POLICY,
93 reinterpret_cast<task_policy_t>(&task_category_policy),
94 TASK_CATEGORY_POLICY_COUNT);
95 DCHECK(result == KERN_SUCCESS);
96
97 if (result != KERN_SUCCESS) {
98 return false;
99 } else if (!mac::IsOSMavericksOrLater()) {
100 return true;
101 }
102
103 // Latency QoS regulates timer throttling/accuracy. Select default tier
104 // on foreground because precise timer firing isn't needed
105 struct task_qos_policy qosinfo = {
106 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_LAUNCH_DEFAULT_TIER,
107 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER};
Robert Sesek 2015/03/12 16:00:23 nit: }; on its own line
shrike 2015/03/18 16:20:22 Done.
108 result = task_policy_set(process_port, TASK_OVERRIDE_QOS_POLICY,
109 (task_policy_t)&qosinfo, TASK_QOS_POLICY_COUNT);
Robert Sesek 2015/03/12 16:00:23 nit: reinterpret_cast
shrike 2015/03/18 16:20:22 Done.
110 DCHECK(result == KERN_SUCCESS);
111
112 return result == KERN_SUCCESS;
113 }
114
115 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698