| 6 |
| 7 | #include <errno.h> |
| 8 | #include <sys/resource.h> |
| 9 |
| 10 | #include "base/files/file_util.h" |
| 11 | #include "base/logging.h" |
| 12 | #include "base/posix/can_lower_nice_to.h" |
| 13 | #include "base/process/internal_linux.h" |
| 14 | #include "base/strings/string_number_conversions.h" |
| 15 | #include "base/strings/string_split.h" |
| 16 | #include "base/strings/stringprintf.h" |
| 17 | #include "base/synchronization/lock.h" |
| 18 | #include "base/threading/thread_restrictions.h" |
| 19 | #include "build/build_config.h" |
| 20 |
| 21 | namespace base { |
| 22 |
| 23 | namespace { |
| 24 |
| 25 | const int kForegroundPriority = 0; |
| 26 |
| 27 | #if defined(OS_CHROMEOS) |
| 28 | // We are more aggressive in our lowering of background process priority |
| 29 | // for chromeos as we have much more control over other processes running |
| 30 | // on the machine. |
| 31 | // |
| 32 | // TODO(davemoore) Refactor this by adding support for higher levels to set |
| 33 | // the foregrounding / backgrounding process so we don't have to keep |
| 34 | // chrome / chromeos specific logic here. |
| 35 | const int kBackgroundPriority = 19; |
| 36 | const char kControlPath[] = "/sys/fs/cgroup/cpu%s/cgroup.procs"; |
| 37 | const char kForeground[] = "/chrome_renderers/foreground"; |
| 38 | const char kBackground[] = "/chrome_renderers/background"; |
| 39 | const char kProcPath[] = "/proc/%d/cgroup"; |
| 40 |
| 41 | struct CGroups { |
| 42 | // Check for cgroups files. ChromeOS supports these by default. It creates |
| 43 | // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups, |
| 44 | // one contains at most a single foreground renderer and the other contains |
| 45 | // all background renderers. This allows us to limit the impact of background |
| 46 | // renderers on foreground ones to a greater level than simple renicing. |
| 47 | bool enabled; |
| 48 | base::FilePath foreground_file; |
| 49 | base::FilePath background_file; |
| 50 |
| 51 | CGroups() { |
| 52 | foreground_file = |
| 53 | base::FilePath(base::StringPrintf(kControlPath, kForeground)); |
| 54 | background_file = |
| 55 | base::FilePath(base::StringPrintf(kControlPath, kBackground)); |
| 56 | base::FileSystemType foreground_type; |
| 57 | base::FileSystemType background_type; |
| 58 | enabled = |
| 59 | base::GetFileSystemType(foreground_file, &foreground_type) && |
| 60 | base::GetFileSystemType(background_file, &background_type) && |
| 61 | foreground_type == FILE_SYSTEM_CGROUP && |
| 62 | background_type == FILE_SYSTEM_CGROUP; |
| 63 | } |
| 64 |
| 65 | static CGroups& Get() { |
| 66 | static auto& groups = *new CGroups; |
| 67 | return groups; |
| 68 | } |
| 69 | }; |
| 70 | #else |
| 71 | const int kBackgroundPriority = 5; |
| 72 | #endif // defined(OS_CHROMEOS) |
| 73 |
| 74 | } // namespace |
| 75 |
| 76 | Time Process::CreationTime() const { |
| 77 | int64_t start_ticks = is_current() |
| 78 | ? internal::ReadProcSelfStatsAndGetFieldAsInt64( |
| 79 | internal::VM_STARTTIME) |
| 80 | : internal::ReadProcStatsAndGetFieldAsInt64( |
| 81 | Pid(), internal::VM_STARTTIME); |
| 82 | if (!start_ticks) |
| 83 | return Time(); |
| 84 | TimeDelta start_offset = internal::ClockTicksToTimeDelta(start_ticks); |
| 85 | Time boot_time = internal::GetBootTime(); |
| 86 | if (boot_time.is_null()) |
| 87 | return Time(); |
| 88 | return Time(boot_time + start_offset); |
| 89 | } |
| 90 |
| 91 | // static |
| 92 | bool Process::CanBackgroundProcesses() { |
| 93 | #if defined(OS_CHROMEOS) |
| 94 | if (CGroups::Get().enabled) |
| 95 | return true; |
| 96 | #endif // defined(OS_CHROMEOS) |
| 97 |
| 98 | static const bool can_reraise_priority = |
| 99 | internal::CanLowerNiceTo(kForegroundPriority); |
| 100 | return can_reraise_priority; |
| 101 | } |
| 102 |
| 103 | bool Process::IsProcessBackgrounded() const { |
| 104 | DCHECK(IsValid()); |
| 105 |
| 106 | #if defined(OS_CHROMEOS) |
| 107 | if (CGroups::Get().enabled) { |
| 108 | // Used to allow reading the process priority from proc on thread launch. |
| 109 | base::ThreadRestrictions::ScopedAllowIO allow_io; |
| 110 | std::string proc; |
| 111 | if (base::ReadFileToString( |
| 112 | base::FilePath(StringPrintf(kProcPath, process_)), &proc)) { |
| 113 | return IsProcessBackgroundedCGroup(proc); |
| 114 | } |
| 115 | return false; |
| 116 | } |
| 117 | #endif // defined(OS_CHROMEOS) |
| 118 |
| 119 | return GetPriority() == kBackgroundPriority; |
| 120 | } |
| 121 |
| 122 | bool Process::SetProcessBackgrounded(bool background) { |
| 123 | DCHECK(IsValid()); |
| 124 |
| 125 | #if defined(OS_CHROMEOS) |
| 126 | if (CGroups::Get().enabled) { |
| 127 | std::string pid = NumberToString(process_); |
| 128 | const base::FilePath file = background ? CGroups::Get().background_file |
| 129 | : CGroups::Get().foreground_file; |
| 130 | return base::WriteFile(file, pid.c_str(), pid.size()) > 0; |
| 131 | } |
| 132 | #endif // defined(OS_CHROMEOS) |
| 133 |
| 134 | if (!CanBackgroundProcesses()) |
| 135 | return false; |
| 136 |
| 137 | int priority = background ? kBackgroundPriority : kForegroundPriority; |
| 138 | int result = setpriority(PRIO_PROCESS, process_, priority); |
| 139 | DPCHECK(result == 0); |
| 140 | return result == 0; |
| 141 | } |
| 142 |
| 143 | #if defined(OS_CHROMEOS) |
| 144 | bool IsProcessBackgroundedCGroup(const StringPiece& cgroup_contents) { |
| 145 | // The process can be part of multiple control groups, and for each cgroup |
| 146 | // hierarchy there's an entry in the file. We look for a control group |
| 147 | // named "/chrome_renderers/background" to determine if the process is |
| 148 | // backgrounded. crbug.com/548818. |
| 149 | std::vector<StringPiece> lines = SplitStringPiece( |
| 150 | cgroup_contents, "\n", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 151 | for (const auto& line : lines) { |
| 152 | std::vector<StringPiece> fields = |
| 153 | SplitStringPiece(line, ":", TRIM_WHITESPACE, SPLIT_WANT_ALL); |
| 154 | if (fields.size() != 3U) { |
| 155 | NOTREACHED(); |
| 156 | continue; |
| 157 | } |
| 158 | if (fields[2] == kBackground) |
| 159 | return true; |
| 160 | } |
| 161 |
| 162 | return false; |
| 163 | } |
| 164 | #endif // defined(OS_CHROMEOS) |
| 165 |
| 166 | #if defined(OS_CHROMEOS) |
| 167 | // Reads /proc/<pid>/status and returns the PID in its PID namespace. |
| 168 | // If the process is not in a PID namespace or /proc/<pid>/status does not |
| 169 | // report NSpid, kNullProcessId is returned. |
| 170 | ProcessId Process::GetPidInNamespace() const { |
| 171 | std::string status; |
| 172 | { |
| 173 | // Synchronously reading files in /proc does not hit the disk. |
| 174 | ThreadRestrictions::ScopedAllowIO allow_io; |
| 175 | FilePath status_file = |
| 176 | FilePath("/proc").Append(NumberToString(process_)).Append("status"); |
| 177 | if (!ReadFileToString(status_file, &status)) { |
| 178 | return kNullProcessId; |
| 179 | } |
| 180 | } |
| 181 |
| 182 | StringPairs pairs; |
| 183 | SplitStringIntoKeyValuePairs(status, ':', '\n', &pairs); |
| 184 | for (const auto& pair : pairs) { |
| 185 | const std::string& key = pair.first; |
| 186 | const std::string& value_str = pair.second; |
| 187 | if (key == "NSpid") { |
| 188 | std::vector<StringPiece> split_value_str = SplitStringPiece( |
| 189 | value_str, "\t", TRIM_WHITESPACE, SPLIT_WANT_NONEMPTY); |
| 190 | if (split_value_str.size() <= 1) { |
| 191 | return kNullProcessId; |
| 192 | } |
| 193 | int value; |
| 194 | // The last value in the list is the PID in the namespace. |
| 195 | if (!StringToInt(split_value_str.back(), &value)) { |
| 196 | NOTREACHED(); |
| 197 | return kNullProcessId; |
| 198 | } |
| 199 | return value; |
| 200 | } |
| 201 | } |
| 202 | return kNullProcessId; |
| 203 | } |
| 204 | #endif // defined(OS_CHROMEOS) |
| 205 |
| 206 | } // namespace base |
| 207 |