OLD | NEW |
---|---|
(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 #ifndef TASK_OVERRIDE_QOS_POLICY | |
14 | |
15 #define TASK_OVERRIDE_QOS_POLICY 9 | |
16 | |
17 typedef struct task_category_policy task_category_policy_data_t; | |
18 typedef struct task_category_policy* task_category_policy_t; | |
19 | |
20 enum task_latency_qos { | |
21 LATENCY_QOS_TIER_UNSPECIFIED = 0x0, | |
22 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1), | |
23 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2), | |
24 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3), | |
25 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4), | |
26 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5), | |
27 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6) | |
28 }; | |
29 typedef integer_t task_latency_qos_t; | |
30 enum task_throughput_qos { | |
31 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0, | |
32 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1), | |
33 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2), | |
34 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3), | |
35 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4), | |
36 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5), | |
37 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6), | |
38 }; | |
39 | |
40 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3 | |
41 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3 | |
42 | |
43 typedef integer_t task_throughput_qos_t; | |
44 | |
45 struct task_qos_policy { | |
46 task_latency_qos_t task_latency_qos_tier; | |
47 task_throughput_qos_t task_throughput_qos_tier; | |
48 }; | |
49 | |
50 typedef struct task_qos_policy* task_qos_policy_t; | |
51 #define TASK_QOS_POLICY_COUNT \ | |
52 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t))) | |
53 | |
54 #endif // TASK_OVERRIDE_QOS_POLICY | |
55 | |
56 namespace base { | |
57 | |
58 bool Process::CanBackgroundProcesses() { | |
59 return true; | |
60 } | |
61 | |
62 bool Process::IsProcessBackgrounded(mach_port_t task_port) const { | |
63 // See SetProcessBackgrounded(). | |
64 DCHECK(IsValid()); | |
65 | |
66 task_category_policy_data_t category_policy; | |
67 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT; | |
68 boolean_t get_default = FALSE; | |
69 | |
70 kern_return_t result = | |
71 task_policy_get(task_port, TASK_CATEGORY_POLICY, | |
72 reinterpret_cast<task_policy_t>(&category_policy), | |
73 &task_info_count, &get_default); | |
74 MACH_DCHECK(result == KERN_SUCCESS, result); | |
Robert Sesek
2015/03/13 04:57:01
It may be more helpful to log this, rather than ju
shrike
2015/03/18 16:20:22
Thank you. Done.
| |
75 | |
76 if (result == KERN_SUCCESS && get_default == FALSE) { | |
77 return category_policy.role == TASK_BACKGROUND_APPLICATION; | |
78 } | |
79 return false; | |
80 } | |
81 | |
82 bool Process::SetProcessBackgrounded(mach_port_t task_port, bool background) { | |
83 DCHECK(IsValid()); | |
84 | |
85 if (!CanBackgroundProcesses() || task_port == TASK_NULL) { | |
86 return false; | |
87 } | |
88 | |
89 struct task_category_policy task_category_policy; | |
Robert Sesek
2015/03/13 04:57:01
This function isn't consistently typed or named wi
shrike
2015/03/18 16:20:23
Done.
| |
90 task_category_policy.role = | |
91 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION; | |
92 kern_return_t result = | |
93 task_policy_set(task_port, TASK_CATEGORY_POLICY, | |
94 reinterpret_cast<task_policy_t>(&task_category_policy), | |
95 TASK_CATEGORY_POLICY_COUNT); | |
96 MACH_DCHECK(result == KERN_SUCCESS, result); | |
97 | |
98 if (result != KERN_SUCCESS) { | |
99 return false; | |
Robert Sesek
2015/03/13 04:57:01
Here, you could just do:
MACH_LOG(ERROR, result)
shrike
2015/03/18 16:20:22
Done.
| |
100 } else if (!mac::IsOSMavericksOrLater()) { | |
101 return true; | |
102 } | |
103 | |
104 // Latency QoS regulates timer throttling/accuracy. Select default tier | |
105 // on foreground because precise timer firing isn't needed | |
Robert Sesek
2015/03/13 04:57:01
nit: punctuation
shrike
2015/03/18 16:20:22
(Not used to ending my comments with punctuation).
| |
106 struct task_qos_policy qosinfo = { | |
Robert Sesek
2015/03/13 04:57:01
naming: qos_policy
shrike
2015/03/18 16:20:23
Done.
| |
107 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_LAUNCH_DEFAULT_TIER, | |
108 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER | |
109 }; | |
110 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY, | |
111 reinterpret_cast<task_policy_t>(&qosinfo), | |
112 TASK_QOS_POLICY_COUNT); | |
113 MACH_DCHECK(result == KERN_SUCCESS, result); | |
Robert Sesek
2015/03/13 04:57:01
MACH_LOG_IF as above, or restructure:
if (result
shrike
2015/03/18 16:20:23
Done.
| |
114 | |
115 return result == KERN_SUCCESS; | |
116 } | |
117 | |
118 } // namespace base | |
OLD | NEW |