blob: 9f69596dcb4a5f05fcac48cf3395f75bb6f0f49d [file] [log] [blame]
[email protected]3e55e212011-03-24 19:45:021// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]276aa6a2009-10-29 17:43:442// 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]3e55e212011-03-24 19:45:0210#include "base/file_util.h"
[email protected]8a420802011-12-02 16:14:4611#include "base/lazy_instance.h"
[email protected]276aa6a2009-10-29 17:43:4412#include "base/logging.h"
[email protected]3e55e212011-03-24 19:45:0213#include "base/stringprintf.h"
[email protected]8a420802011-12-02 16:14:4614#include "base/string_split.h"
15#include "base/synchronization/lock.h"
[email protected]3e55e212011-03-24 19:45:0216
[email protected]8a420802011-12-02 16:14:4617namespace {
18const int kForegroundPriority = 0;
[email protected]276aa6a2009-10-29 17:43:4419
[email protected]4d641082011-02-09 21:27:2820#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]8a420802011-12-02 16:14:4624//
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.
28const int kBackgroundPriority = 19;
29const char kControlPath[] = "/tmp/cgroup/cpu%s/cgroup.procs";
30const char kForeground[] = "/chrome_renderers/foreground";
31const char kBackground[] = "/chrome_renderers/background";
32const char kProcPath[] = "/proc/%d/cgroup";
33
34struct CGroups {
35 // Check for cgroups files. ChromeOS supports these by default. It creates
36 // a cgroup mount in /tmp/cgroup and then configures two cpu task groups,
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;
41 FilePath foreground_file;
42 FilePath background_file;
43
44 CGroups() {
45 foreground_file = FilePath(StringPrintf(kControlPath, kForeground));
46 background_file = FilePath(StringPrintf(kControlPath, kBackground));
47 file_util::FileSystemType foreground_type;
48 file_util::FileSystemType background_type;
49 enabled =
50 file_util::GetFileSystemType(foreground_file, &foreground_type) &&
51 file_util::GetFileSystemType(background_file, &background_type) &&
52 foreground_type == file_util::FILE_SYSTEM_CGROUP &&
53 background_type == file_util::FILE_SYSTEM_CGROUP;
54 }
55};
56
57base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER;
[email protected]4d641082011-02-09 21:27:2858#else
[email protected]8a420802011-12-02 16:14:4659const int kBackgroundPriority = 5;
[email protected]4d641082011-02-09 21:27:2860#endif
[email protected]8a420802011-12-02 16:14:4661}
62
63namespace base {
[email protected]f0c7a262010-01-13 00:45:5464
[email protected]276aa6a2009-10-29 17:43:4465bool Process::IsProcessBackgrounded() const {
66 DCHECK(process_);
[email protected]8a420802011-12-02 16:14:4667
68#if defined(OS_CHROMEOS)
69 if (cgroups.Get().enabled) {
70 std::string proc;
71 if (file_util::ReadFileToString(
72 FilePath(StringPrintf(kProcPath, process_)),
73 &proc)) {
74 std::vector<std::string> proc_parts;
75 base::SplitString(proc, ':', &proc_parts);
76 DCHECK(proc_parts.size() == 3);
77 bool ret = proc_parts[2] == std::string(kBackground);
78 return ret;
79 } else {
80 return false;
81 }
82 }
83#endif
84 return GetPriority() == kBackgroundPriority;
[email protected]276aa6a2009-10-29 17:43:4485}
86
87bool Process::SetProcessBackgrounded(bool background) {
88 DCHECK(process_);
89
[email protected]3e55e212011-03-24 19:45:0290#if defined(OS_CHROMEOS)
[email protected]8a420802011-12-02 16:14:4691 if (cgroups.Get().enabled) {
92 std::string pid = StringPrintf("%d", process_);
93 const FilePath file =
94 background ?
95 cgroups.Get().background_file : cgroups.Get().foreground_file;
96 return file_util::WriteFile(file, pid.c_str(), pid.size()) > 0;
[email protected]3e55e212011-03-24 19:45:0297 }
98#endif // OS_CHROMEOS
99
[email protected]8a420802011-12-02 16:14:46100 if (!CanBackgroundProcesses())
101 return false;
102
103 int priority = background ? kBackgroundPriority : kForegroundPriority;
104 int result = setpriority(PRIO_PROCESS, process_, priority);
105 DPCHECK(result == 0);
106 return result == 0;
107}
108
109struct CheckForNicePermission {
[email protected]73c7c832011-12-22 22:34:58110 CheckForNicePermission() : can_reraise_priority(false) {
[email protected]276aa6a2009-10-29 17:43:44111 // We won't be able to raise the priority if we don't have the right rlimit.
112 // The limit may be adjusted in /etc/security/limits.conf for PAM systems.
113 struct rlimit rlim;
[email protected]8a420802011-12-02 16:14:46114 if ((getrlimit(RLIMIT_NICE, &rlim) == 0) &&
115 (20 - kForegroundPriority) <= static_cast<int>(rlim.rlim_cur)) {
116 can_reraise_priority = true;
[email protected]276aa6a2009-10-29 17:43:44117 }
[email protected]8a420802011-12-02 16:14:46118 };
[email protected]73c7c832011-12-22 22:34:58119
120 bool can_reraise_priority;
[email protected]8a420802011-12-02 16:14:46121};
122
123// static
124bool Process::CanBackgroundProcesses() {
125#if defined(OS_CHROMEOS)
126 if (cgroups.Get().enabled)
[email protected]276aa6a2009-10-29 17:43:44127 return true;
[email protected]8a420802011-12-02 16:14:46128#endif
129
130 static LazyInstance<CheckForNicePermission> check_for_nice_permission =
131 LAZY_INSTANCE_INITIALIZER;
132 return check_for_nice_permission.Get().can_reraise_priority;
[email protected]276aa6a2009-10-29 17:43:44133}
134
135} // namespace base