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