Roll src/third_party/skia b3eb687:c6f3e2c
[chromium-blink-merge.git] / base / process / process_linux.cc
blob59ee288f880135bc66d77f47d88478dd2a2aa993
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
3 // found in the LICENSE file.
5 #include "base/process/process.h"
7 #include <errno.h>
8 #include <sys/resource.h>
10 #include "base/files/file_util.h"
11 #include "base/lazy_instance.h"
12 #include "base/logging.h"
13 #include "base/strings/string_split.h"
14 #include "base/strings/stringprintf.h"
15 #include "base/synchronization/lock.h"
17 namespace base {
19 namespace {
21 const int kForegroundPriority = 0;
23 #if defined(OS_CHROMEOS)
24 // We are more aggressive in our lowering of background process priority
25 // for chromeos as we have much more control over other processes running
26 // on the machine.
28 // TODO(davemoore) Refactor this by adding support for higher levels to set
29 // the foregrounding / backgrounding process so we don't have to keep
30 // chrome / chromeos specific logic here.
31 const int kBackgroundPriority = 19;
32 const char kControlPath[] = "/sys/fs/cgroup/cpu%s/cgroup.procs";
33 const char kForeground[] = "/chrome_renderers/foreground";
34 const char kBackground[] = "/chrome_renderers/background";
35 const char kProcPath[] = "/proc/%d/cgroup";
37 struct CGroups {
38 // Check for cgroups files. ChromeOS supports these by default. It creates
39 // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups,
40 // one contains at most a single foreground renderer and the other contains
41 // all background renderers. This allows us to limit the impact of background
42 // renderers on foreground ones to a greater level than simple renicing.
43 bool enabled;
44 base::FilePath foreground_file;
45 base::FilePath background_file;
47 CGroups() {
48 foreground_file =
49 base::FilePath(base::StringPrintf(kControlPath, kForeground));
50 background_file =
51 base::FilePath(base::StringPrintf(kControlPath, kBackground));
52 base::FileSystemType foreground_type;
53 base::FileSystemType background_type;
54 enabled =
55 base::GetFileSystemType(foreground_file, &foreground_type) &&
56 base::GetFileSystemType(background_file, &background_type) &&
57 foreground_type == FILE_SYSTEM_CGROUP &&
58 background_type == FILE_SYSTEM_CGROUP;
62 base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER;
63 #else
64 const int kBackgroundPriority = 5;
65 #endif
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;
78 bool can_reraise_priority;
81 } // namespace
83 // static
84 bool Process::CanBackgroundProcesses() {
85 #if defined(OS_CHROMEOS)
86 if (cgroups.Get().enabled)
87 return true;
88 #endif
90 static LazyInstance<CheckForNicePermission> check_for_nice_permission =
91 LAZY_INSTANCE_INITIALIZER;
92 return check_for_nice_permission.Get().can_reraise_priority;
95 bool Process::IsProcessBackgrounded() const {
96 DCHECK(IsValid());
98 #if defined(OS_CHROMEOS)
99 if (cgroups.Get().enabled) {
100 std::string proc;
101 if (base::ReadFileToString(
102 base::FilePath(StringPrintf(kProcPath, process_)),
103 &proc)) {
104 std::vector<std::string> proc_parts;
105 base::SplitString(proc, ':', &proc_parts);
106 DCHECK(proc_parts.size() == 3);
107 bool ret = proc_parts[2] == std::string(kBackground);
108 return ret;
109 } else {
110 return false;
113 #endif
114 return GetPriority() == kBackgroundPriority;
117 bool Process::SetProcessBackgrounded(bool background) {
118 DCHECK(IsValid());
120 #if defined(OS_CHROMEOS)
121 if (cgroups.Get().enabled) {
122 std::string pid = StringPrintf("%d", process_);
123 const base::FilePath file =
124 background ?
125 cgroups.Get().background_file : cgroups.Get().foreground_file;
126 return base::WriteFile(file, pid.c_str(), pid.size()) > 0;
128 #endif // OS_CHROMEOS
130 if (!CanBackgroundProcesses())
131 return false;
133 int priority = background ? kBackgroundPriority : kForegroundPriority;
134 int result = setpriority(PRIO_PROCESS, process_, priority);
135 DPCHECK(result == 0);
136 return result == 0;
139 } // namespace base