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 // 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 } | |
93 | |
94 task_category_policy category_policy; | |
95 category_policy.role = | |
96 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION; | |
97 kern_return_t result = | |
98 task_policy_set(task_port, TASK_CATEGORY_POLICY, | |
99 reinterpret_cast<task_policy_t>(&category_policy), | |
100 TASK_CATEGORY_POLICY_COUNT); | |
101 | |
102 if (result != KERN_SUCCESS) { | |
103 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY"; | |
104 return false; | |
105 } else if (!mac::IsOSMavericksOrLater()) { | |
106 return true; | |
107 } | |
108 | |
109 // Latency QoS regulates timer throttling/accuracy. Select default tier | |
110 // on foreground because precise timer firing isn't needed. | |
111 struct task_qos_policy qos_policy = { | |
112 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED, | |
113 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED | |
114 }; | |
115 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY, | |
116 reinterpret_cast<task_policy_t>(&qos_policy), | |
117 TASK_QOS_POLICY_COUNT); | |
118 if (result != KERN_SUCCESS) { | |
119 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY"; | |
120 return false; | |
121 } | |
122 | |
123 return true; | |
124 } | |
125 | |
126 } // namespace base | |
OLD | NEW |