Chromium Code Reviews| 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/command_line.h" | |
| 8 #include "base/mac/mac_util.h" | |
| 9 #include "base/mac/mach_logging.h" | |
| 10 | |
| 11 #include <mach/mach.h> | |
| 12 | |
| 13 // The following was added to <mach/task_policy.h> after 10.8. | |
| 14 // TODO(shrike): Remove the TASK_OVERRIDE_QOS_POLICY ifndef once builders | |
| 15 // reach 10.9 or higher. | |
| 16 #ifndef TASK_OVERRIDE_QOS_POLICY | |
| 17 | |
| 18 #define TASK_OVERRIDE_QOS_POLICY 9 | |
| 19 | |
| 20 typedef struct task_category_policy task_category_policy_data_t; | |
| 21 typedef struct task_category_policy* task_category_policy_t; | |
| 22 | |
| 23 enum task_latency_qos { | |
| 24 LATENCY_QOS_TIER_UNSPECIFIED = 0x0, | |
| 25 LATENCY_QOS_TIER_0 = ((0xFF << 16) | 1), | |
| 26 LATENCY_QOS_TIER_1 = ((0xFF << 16) | 2), | |
| 27 LATENCY_QOS_TIER_2 = ((0xFF << 16) | 3), | |
| 28 LATENCY_QOS_TIER_3 = ((0xFF << 16) | 4), | |
| 29 LATENCY_QOS_TIER_4 = ((0xFF << 16) | 5), | |
| 30 LATENCY_QOS_TIER_5 = ((0xFF << 16) | 6) | |
| 31 }; | |
| 32 typedef integer_t task_latency_qos_t; | |
| 33 enum task_throughput_qos { | |
| 34 THROUGHPUT_QOS_TIER_UNSPECIFIED = 0x0, | |
| 35 THROUGHPUT_QOS_TIER_0 = ((0xFE << 16) | 1), | |
| 36 THROUGHPUT_QOS_TIER_1 = ((0xFE << 16) | 2), | |
| 37 THROUGHPUT_QOS_TIER_2 = ((0xFE << 16) | 3), | |
| 38 THROUGHPUT_QOS_TIER_3 = ((0xFE << 16) | 4), | |
| 39 THROUGHPUT_QOS_TIER_4 = ((0xFE << 16) | 5), | |
| 40 THROUGHPUT_QOS_TIER_5 = ((0xFE << 16) | 6), | |
| 41 }; | |
| 42 | |
| 43 #define LATENCY_QOS_LAUNCH_DEFAULT_TIER LATENCY_QOS_TIER_3 | |
| 44 #define THROUGHPUT_QOS_LAUNCH_DEFAULT_TIER THROUGHPUT_QOS_TIER_3 | |
| 45 | |
| 46 typedef integer_t task_throughput_qos_t; | |
| 47 | |
| 48 struct task_qos_policy { | |
| 49 task_latency_qos_t task_latency_qos_tier; | |
| 50 task_throughput_qos_t task_throughput_qos_tier; | |
| 51 }; | |
| 52 | |
| 53 typedef struct task_qos_policy* task_qos_policy_t; | |
| 54 #define TASK_QOS_POLICY_COUNT \ | |
| 55 ((mach_msg_type_number_t)(sizeof(struct task_qos_policy) / sizeof(integer_t))) | |
| 56 | |
| 57 #endif // TASK_OVERRIDE_QOS_POLICY | |
| 58 | |
| 59 namespace base { | |
| 60 | |
| 61 bool Process::CanBackgroundProcesses() { | |
| 62 return true; | |
| 63 } | |
| 64 | |
| 65 bool Process::IsProcessBackgrounded(mach_port_t task_port) const { | |
| 66 // See SetProcessBackgrounded(). | |
| 67 DCHECK(IsValid()); | |
| 68 DCHECK_NE(task_port, TASK_NULL); | |
| 69 | |
| 70 task_category_policy_data_t category_policy; | |
| 71 mach_msg_type_number_t task_info_count = TASK_CATEGORY_POLICY_COUNT; | |
| 72 boolean_t get_default = FALSE; | |
| 73 | |
| 74 kern_return_t result = | |
| 75 task_policy_get(task_port, TASK_CATEGORY_POLICY, | |
| 76 reinterpret_cast<task_policy_t>(&category_policy), | |
| 77 &task_info_count, &get_default); | |
| 78 MACH_LOG_IF(ERROR, result != KERN_SUCCESS, result) << | |
| 79 "task_policy_get TASK_CATEGORY_POLICY"; | |
| 80 | |
| 81 if (result == KERN_SUCCESS && get_default == FALSE) { | |
| 82 return category_policy.role == TASK_BACKGROUND_APPLICATION; | |
| 83 } | |
| 84 return false; | |
| 85 } | |
| 86 | |
| 87 bool Process::SetProcessBackgrounded(mach_port_t task_port, bool background) { | |
| 88 DCHECK(IsValid()); | |
| 89 DCHECK_NE(task_port, TASK_NULL); | |
| 90 | |
| 91 if (!CanBackgroundProcesses()) { | |
| 92 return false; | |
| 93 } else if (IsProcessBackgrounded(task_port) == background) { | |
| 94 return true; | |
| 95 } | |
| 96 | |
| 97 // When running the ExtensionApiNewTabTest.Tabs browser test don't allow | |
| 98 // process backgrounding (otherwise the test will time out). | |
| 99 const CommandLine* command_line = CommandLine::ForCurrentProcess(); | |
| 100 if (command_line->GetSwitchValueASCII("gtest_filter") == | |
| 101 "ExtensionApiNewTabTest.Tabs") { | |
|
gab
2015/05/28 13:58:09
That's not a very common way to disable things for
| |
| 102 return false; | |
| 103 } | |
| 104 | |
| 105 task_category_policy category_policy; | |
| 106 category_policy.role = | |
| 107 background ? TASK_BACKGROUND_APPLICATION : TASK_FOREGROUND_APPLICATION; | |
| 108 kern_return_t result = | |
| 109 task_policy_set(task_port, TASK_CATEGORY_POLICY, | |
| 110 reinterpret_cast<task_policy_t>(&category_policy), | |
| 111 TASK_CATEGORY_POLICY_COUNT); | |
| 112 | |
| 113 if (result != KERN_SUCCESS) { | |
| 114 MACH_LOG(ERROR, result) << "task_policy_set TASK_CATEGORY_POLICY"; | |
| 115 return false; | |
| 116 } else if (!mac::IsOSMavericksOrLater()) { | |
| 117 return true; | |
| 118 } | |
| 119 | |
| 120 // Latency QoS regulates timer throttling/accuracy. Select default tier | |
| 121 // on foreground because precise timer firing isn't needed. | |
| 122 struct task_qos_policy qos_policy = { | |
| 123 background ? LATENCY_QOS_TIER_5 : LATENCY_QOS_TIER_UNSPECIFIED, | |
| 124 background ? THROUGHPUT_QOS_TIER_5 : THROUGHPUT_QOS_TIER_UNSPECIFIED | |
| 125 }; | |
| 126 result = task_policy_set(task_port, TASK_OVERRIDE_QOS_POLICY, | |
| 127 reinterpret_cast<task_policy_t>(&qos_policy), | |
| 128 TASK_QOS_POLICY_COUNT); | |
| 129 if (result != KERN_SUCCESS) { | |
| 130 MACH_LOG(ERROR, result) << "task_policy_set TASK_OVERRIDE_QOS_POLICY"; | |
| 131 return false; | |
| 132 } | |
| 133 | |
| 134 return true; | |
| 135 } | |
| 136 | |
| 137 } // namespace base | |
| OLD | NEW |