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"
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"
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
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";
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.
44 base::FilePath foreground_file
;
45 base::FilePath background_file
;
49 base::FilePath(base::StringPrintf(kControlPath
, kForeground
));
51 base::FilePath(base::StringPrintf(kControlPath
, kBackground
));
52 base::FileSystemType foreground_type
;
53 base::FileSystemType background_type
;
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
;
64 const int kBackgroundPriority
= 5;
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.
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
;
84 bool Process::CanBackgroundProcesses() {
85 #if defined(OS_CHROMEOS)
86 if (cgroups
.Get().enabled
)
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 {
98 #if defined(OS_CHROMEOS)
99 if (cgroups
.Get().enabled
) {
101 if (base::ReadFileToString(
102 base::FilePath(StringPrintf(kProcPath
, process_
)),
104 std::vector
<std::string
> proc_parts
;
105 base::SplitString(proc
, ':', &proc_parts
);
106 DCHECK_EQ(proc_parts
.size(), 3u);
107 bool ret
= proc_parts
[2] == std::string(kBackground
);
114 return GetPriority() == kBackgroundPriority
;
117 bool Process::SetProcessBackgrounded(bool background
) {
120 #if defined(OS_CHROMEOS)
121 if (cgroups
.Get().enabled
) {
122 std::string pid
= StringPrintf("%d", process_
);
123 const base::FilePath file
=
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())
133 int priority
= background
? kBackgroundPriority
: kForegroundPriority
;
134 int result
= setpriority(PRIO_PROCESS
, process_
, priority
);
135 DPCHECK(result
== 0);