[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 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.h" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <sys/resource.h> |
| 9 | |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 10 | #include "base/file_util.h" |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 11 | #include "base/lazy_instance.h" |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 12 | #include "base/logging.h" |
[email protected] | 5ae0b763e | 2013-02-07 23:01:39 | [diff] [blame] | 13 | #include "base/strings/string_split.h" |
[email protected] | d1a5a2f | 2013-06-10 21:17:40 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 16 | |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 17 | namespace { |
| 18 | const int kForegroundPriority = 0; |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 19 | |
[email protected] | 4d64108 | 2011-02-09 21:27:28 | [diff] [blame] | 20 | #if defined(OS_CHROMEOS) |
| 21 | // We are more aggressive in our lowering of background process priority |
| 22 | // for chromeos as we have much more control over other processes running |
| 23 | // on the machine. |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 24 | // |
| 25 | // TODO(davemoore) Refactor this by adding support for higher levels to set |
| 26 | // the foregrounding / backgrounding process so we don't have to keep |
| 27 | // chrome / chromeos specific logic here. |
| 28 | const int kBackgroundPriority = 19; |
[email protected] | f9b950e | 2013-01-26 03:48:38 | [diff] [blame] | 29 | const char kControlPath[] = "/sys/fs/cgroup/cpu%s/cgroup.procs"; |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 30 | const char kForeground[] = "/chrome_renderers/foreground"; |
| 31 | const char kBackground[] = "/chrome_renderers/background"; |
| 32 | const char kProcPath[] = "/proc/%d/cgroup"; |
| 33 | |
| 34 | struct CGroups { |
| 35 | // Check for cgroups files. ChromeOS supports these by default. It creates |
[email protected] | f9b950e | 2013-01-26 03:48:38 | [diff] [blame] | 36 | // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups, |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 37 | // one contains at most a single foreground renderer and the other contains |
| 38 | // all background renderers. This allows us to limit the impact of background |
| 39 | // renderers on foreground ones to a greater level than simple renicing. |
| 40 | bool enabled; |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 41 | base::FilePath foreground_file; |
| 42 | base::FilePath background_file; |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 43 | |
| 44 | CGroups() { |
[email protected] | 7d3cbc9 | 2013-03-18 22:33:04 | [diff] [blame] | 45 | foreground_file = |
| 46 | base::FilePath(base::StringPrintf(kControlPath, kForeground)); |
| 47 | background_file = |
| 48 | base::FilePath(base::StringPrintf(kControlPath, kBackground)); |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 49 | file_util::FileSystemType foreground_type; |
| 50 | file_util::FileSystemType background_type; |
| 51 | enabled = |
| 52 | file_util::GetFileSystemType(foreground_file, &foreground_type) && |
| 53 | file_util::GetFileSystemType(background_file, &background_type) && |
| 54 | foreground_type == file_util::FILE_SYSTEM_CGROUP && |
| 55 | background_type == file_util::FILE_SYSTEM_CGROUP; |
| 56 | } |
| 57 | }; |
| 58 | |
| 59 | base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER; |
[email protected] | 4d64108 | 2011-02-09 21:27:28 | [diff] [blame] | 60 | #else |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 61 | const int kBackgroundPriority = 5; |
[email protected] | 4d64108 | 2011-02-09 21:27:28 | [diff] [blame] | 62 | #endif |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | namespace base { |
[email protected] | f0c7a26 | 2010-01-13 00:45:54 | [diff] [blame] | 66 | |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 67 | bool Process::IsProcessBackgrounded() const { |
| 68 | DCHECK(process_); |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 69 | |
| 70 | #if defined(OS_CHROMEOS) |
| 71 | if (cgroups.Get().enabled) { |
| 72 | std::string proc; |
| 73 | if (file_util::ReadFileToString( |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 74 | base::FilePath(StringPrintf(kProcPath, process_)), |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 75 | &proc)) { |
| 76 | std::vector<std::string> proc_parts; |
| 77 | base::SplitString(proc, ':', &proc_parts); |
| 78 | DCHECK(proc_parts.size() == 3); |
| 79 | bool ret = proc_parts[2] == std::string(kBackground); |
| 80 | return ret; |
| 81 | } else { |
| 82 | return false; |
| 83 | } |
| 84 | } |
| 85 | #endif |
| 86 | return GetPriority() == kBackgroundPriority; |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | bool Process::SetProcessBackgrounded(bool background) { |
| 90 | DCHECK(process_); |
| 91 | |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 92 | #if defined(OS_CHROMEOS) |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 93 | if (cgroups.Get().enabled) { |
| 94 | std::string pid = StringPrintf("%d", process_); |
[email protected] | aaa6df4 | 2013-02-17 19:36:03 | [diff] [blame] | 95 | const base::FilePath file = |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 96 | background ? |
| 97 | cgroups.Get().background_file : cgroups.Get().foreground_file; |
| 98 | return file_util::WriteFile(file, pid.c_str(), pid.size()) > 0; |
[email protected] | 3e55e21 | 2011-03-24 19:45:02 | [diff] [blame] | 99 | } |
| 100 | #endif // OS_CHROMEOS |
| 101 | |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 102 | if (!CanBackgroundProcesses()) |
| 103 | return false; |
| 104 | |
| 105 | int priority = background ? kBackgroundPriority : kForegroundPriority; |
| 106 | int result = setpriority(PRIO_PROCESS, process_, priority); |
| 107 | DPCHECK(result == 0); |
| 108 | return result == 0; |
| 109 | } |
| 110 | |
| 111 | struct CheckForNicePermission { |
[email protected] | 73c7c83 | 2011-12-22 22:34:58 | [diff] [blame] | 112 | CheckForNicePermission() : can_reraise_priority(false) { |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 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; |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 116 | if ((getrlimit(RLIMIT_NICE, &rlim) == 0) && |
| 117 | (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) { |
| 118 | can_reraise_priority = true; |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 119 | } |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 120 | }; |
[email protected] | 73c7c83 | 2011-12-22 22:34:58 | [diff] [blame] | 121 | |
| 122 | bool can_reraise_priority; |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | // static |
| 126 | bool Process::CanBackgroundProcesses() { |
| 127 | #if defined(OS_CHROMEOS) |
| 128 | if (cgroups.Get().enabled) |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 129 | return true; |
[email protected] | 8a42080 | 2011-12-02 16:14:46 | [diff] [blame] | 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; |
[email protected] | 276aa6a | 2009-10-29 17:43:44 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | } // namespace base |