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_number_conversions.h"
14 #include "base/strings/string_split.h"
15 #include "base/strings/stringprintf.h"
16 #include "base/synchronization/lock.h"
22 const int kForegroundPriority
= 0;
24 #if defined(OS_CHROMEOS)
25 // We are more aggressive in our lowering of background process priority
26 // for chromeos as we have much more control over other processes running
29 // TODO(davemoore) Refactor this by adding support for higher levels to set
30 // the foregrounding / backgrounding process so we don't have to keep
31 // chrome / chromeos specific logic here.
32 const int kBackgroundPriority
= 19;
33 const char kControlPath
[] = "/sys/fs/cgroup/cpu%s/cgroup.procs";
34 const char kForeground
[] = "/chrome_renderers/foreground";
35 const char kBackground
[] = "/chrome_renderers/background";
36 const char kProcPath
[] = "/proc/%d/cgroup";
39 // Check for cgroups files. ChromeOS supports these by default. It creates
40 // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups,
41 // one contains at most a single foreground renderer and the other contains
42 // all background renderers. This allows us to limit the impact of background
43 // renderers on foreground ones to a greater level than simple renicing.
45 base::FilePath foreground_file
;
46 base::FilePath background_file
;
50 base::FilePath(base::StringPrintf(kControlPath
, kForeground
));
52 base::FilePath(base::StringPrintf(kControlPath
, kBackground
));
53 base::FileSystemType foreground_type
;
54 base::FileSystemType background_type
;
56 base::GetFileSystemType(foreground_file
, &foreground_type
) &&
57 base::GetFileSystemType(background_file
, &background_type
) &&
58 foreground_type
== FILE_SYSTEM_CGROUP
&&
59 background_type
== FILE_SYSTEM_CGROUP
;
63 base::LazyInstance
<CGroups
> cgroups
= LAZY_INSTANCE_INITIALIZER
;
65 const int kBackgroundPriority
= 5;
68 struct CheckForNicePermission
{
69 CheckForNicePermission() : can_reraise_priority(false) {
70 // We won't be able to raise the priority if we don't have the right rlimit.
71 // The limit may be adjusted in /etc/security/limits.conf for PAM systems.
73 if ((getrlimit(RLIMIT_NICE
, &rlim
) == 0) &&
74 (20 - kForegroundPriority
) <= static_cast<int>(rlim
.rlim_cur
)) {
75 can_reraise_priority
= true;
79 bool can_reraise_priority
;
85 bool Process::CanBackgroundProcesses() {
86 #if defined(OS_CHROMEOS)
87 if (cgroups
.Get().enabled
)
91 static LazyInstance
<CheckForNicePermission
> check_for_nice_permission
=
92 LAZY_INSTANCE_INITIALIZER
;
93 return check_for_nice_permission
.Get().can_reraise_priority
;
96 bool Process::IsProcessBackgrounded() const {
99 #if defined(OS_CHROMEOS)
100 if (cgroups
.Get().enabled
) {
102 if (base::ReadFileToString(
103 base::FilePath(StringPrintf(kProcPath
, process_
)),
105 std::vector
<std::string
> proc_parts
;
106 base::SplitString(proc
, ':', &proc_parts
);
107 DCHECK_EQ(proc_parts
.size(), 3u);
108 bool ret
= proc_parts
[2] == std::string(kBackground
);
115 return GetPriority() == kBackgroundPriority
;
118 bool Process::SetProcessBackgrounded(bool background
) {
121 #if defined(OS_CHROMEOS)
122 if (cgroups
.Get().enabled
) {
123 std::string pid
= IntToString(process_
);
124 const base::FilePath file
=
126 cgroups
.Get().background_file
: cgroups
.Get().foreground_file
;
127 return base::WriteFile(file
, pid
.c_str(), pid
.size()) > 0;
129 #endif // OS_CHROMEOS
131 if (!CanBackgroundProcesses())
134 int priority
= background
? kBackgroundPriority
: kForegroundPriority
;
135 int result
= setpriority(PRIO_PROCESS
, process_
, priority
);
136 DPCHECK(result
== 0);