OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process/process.h" | 5 #include "base/process/process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 | 9 |
10 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
11 #include "base/lazy_instance.h" | 11 #include "base/lazy_instance.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/strings/string_split.h" | 13 #include "base/strings/string_split.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/synchronization/lock.h" | 15 #include "base/synchronization/lock.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 | 18 |
19 namespace { | 19 namespace { |
| 20 |
20 const int kForegroundPriority = 0; | 21 const int kForegroundPriority = 0; |
21 | 22 |
22 #if defined(OS_CHROMEOS) | 23 #if defined(OS_CHROMEOS) |
23 // We are more aggressive in our lowering of background process priority | 24 // We are more aggressive in our lowering of background process priority |
24 // for chromeos as we have much more control over other processes running | 25 // for chromeos as we have much more control over other processes running |
25 // on the machine. | 26 // on the machine. |
26 // | 27 // |
27 // TODO(davemoore) Refactor this by adding support for higher levels to set | 28 // TODO(davemoore) Refactor this by adding support for higher levels to set |
28 // the foregrounding / backgrounding process so we don't have to keep | 29 // the foregrounding / backgrounding process so we don't have to keep |
29 // chrome / chromeos specific logic here. | 30 // chrome / chromeos specific logic here. |
(...skipping 25 matching lines...) Expand all Loading... |
55 base::GetFileSystemType(background_file, &background_type) && | 56 base::GetFileSystemType(background_file, &background_type) && |
56 foreground_type == FILE_SYSTEM_CGROUP && | 57 foreground_type == FILE_SYSTEM_CGROUP && |
57 background_type == FILE_SYSTEM_CGROUP; | 58 background_type == FILE_SYSTEM_CGROUP; |
58 } | 59 } |
59 }; | 60 }; |
60 | 61 |
61 base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER; | 62 base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER; |
62 #else | 63 #else |
63 const int kBackgroundPriority = 5; | 64 const int kBackgroundPriority = 5; |
64 #endif | 65 #endif |
| 66 |
| 67 struct CheckForNicePermission { |
| 68 CheckForNicePermission() : can_reraise_priority(false) { |
| 69 // We won't be able to raise the priority if we don't have the right rlimit. |
| 70 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. |
| 71 struct rlimit rlim; |
| 72 if ((getrlimit(RLIMIT_NICE, &rlim) == 0) && |
| 73 (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) { |
| 74 can_reraise_priority = true; |
| 75 } |
| 76 }; |
| 77 |
| 78 bool can_reraise_priority; |
| 79 }; |
| 80 |
| 81 } // namespace |
| 82 |
| 83 // static |
| 84 bool Process::CanBackgroundProcesses() { |
| 85 #if defined(OS_CHROMEOS) |
| 86 if (cgroups.Get().enabled) |
| 87 return true; |
| 88 #endif |
| 89 |
| 90 static LazyInstance<CheckForNicePermission> check_for_nice_permission = |
| 91 LAZY_INSTANCE_INITIALIZER; |
| 92 return check_for_nice_permission.Get().can_reraise_priority; |
65 } | 93 } |
66 | 94 |
67 bool Process::IsProcessBackgrounded() const { | 95 bool Process::IsProcessBackgrounded() const { |
68 DCHECK(process_); | 96 DCHECK(IsValid()); |
69 | 97 |
70 #if defined(OS_CHROMEOS) | 98 #if defined(OS_CHROMEOS) |
71 if (cgroups.Get().enabled) { | 99 if (cgroups.Get().enabled) { |
72 std::string proc; | 100 std::string proc; |
73 if (base::ReadFileToString( | 101 if (base::ReadFileToString( |
74 base::FilePath(StringPrintf(kProcPath, process_)), | 102 base::FilePath(StringPrintf(kProcPath, process_)), |
75 &proc)) { | 103 &proc)) { |
76 std::vector<std::string> proc_parts; | 104 std::vector<std::string> proc_parts; |
77 base::SplitString(proc, ':', &proc_parts); | 105 base::SplitString(proc, ':', &proc_parts); |
78 DCHECK(proc_parts.size() == 3); | 106 DCHECK(proc_parts.size() == 3); |
79 bool ret = proc_parts[2] == std::string(kBackground); | 107 bool ret = proc_parts[2] == std::string(kBackground); |
80 return ret; | 108 return ret; |
81 } else { | 109 } else { |
82 return false; | 110 return false; |
83 } | 111 } |
84 } | 112 } |
85 #endif | 113 #endif |
86 return GetPriority() == kBackgroundPriority; | 114 return GetPriority() == kBackgroundPriority; |
87 } | 115 } |
88 | 116 |
89 bool Process::SetProcessBackgrounded(bool background) { | 117 bool Process::SetProcessBackgrounded(bool background) { |
90 DCHECK(process_); | 118 DCHECK(IsValid()); |
91 | 119 |
92 #if defined(OS_CHROMEOS) | 120 #if defined(OS_CHROMEOS) |
93 if (cgroups.Get().enabled) { | 121 if (cgroups.Get().enabled) { |
94 std::string pid = StringPrintf("%d", process_); | 122 std::string pid = StringPrintf("%d", process_); |
95 const base::FilePath file = | 123 const base::FilePath file = |
96 background ? | 124 background ? |
97 cgroups.Get().background_file : cgroups.Get().foreground_file; | 125 cgroups.Get().background_file : cgroups.Get().foreground_file; |
98 return base::WriteFile(file, pid.c_str(), pid.size()) > 0; | 126 return base::WriteFile(file, pid.c_str(), pid.size()) > 0; |
99 } | 127 } |
100 #endif // OS_CHROMEOS | 128 #endif // OS_CHROMEOS |
101 | 129 |
102 if (!CanBackgroundProcesses()) | 130 if (!CanBackgroundProcesses()) |
103 return false; | 131 return false; |
104 | 132 |
105 int priority = background ? kBackgroundPriority : kForegroundPriority; | 133 int priority = background ? kBackgroundPriority : kForegroundPriority; |
106 int result = setpriority(PRIO_PROCESS, process_, priority); | 134 int result = setpriority(PRIO_PROCESS, process_, priority); |
107 DPCHECK(result == 0); | 135 DPCHECK(result == 0); |
108 return result == 0; | 136 return result == 0; |
109 } | 137 } |
110 | 138 |
111 struct CheckForNicePermission { | |
112 CheckForNicePermission() : can_reraise_priority(false) { | |
113 // We won't be able to raise the priority if we don't have the right rlimit. | |
114 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. | |
115 struct rlimit rlim; | |
116 if ((getrlimit(RLIMIT_NICE, &rlim) == 0) && | |
117 (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) { | |
118 can_reraise_priority = true; | |
119 } | |
120 }; | |
121 | |
122 bool can_reraise_priority; | |
123 }; | |
124 | |
125 // static | |
126 bool Process::CanBackgroundProcesses() { | |
127 #if defined(OS_CHROMEOS) | |
128 if (cgroups.Get().enabled) | |
129 return true; | |
130 #endif | |
131 | |
132 static LazyInstance<CheckForNicePermission> check_for_nice_permission = | |
133 LAZY_INSTANCE_INITIALIZER; | |
134 return check_for_nice_permission.Get().can_reraise_priority; | |
135 } | |
136 | |
137 } // namespace base | 139 } // namespace base |
OLD | NEW |