OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/process.h" | 5 #include "base/process.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <sys/resource.h> | 8 #include <sys/resource.h> |
9 | 9 |
10 #include "base/file_util.h" | 10 #include "base/file_util.h" |
(...skipping 20 matching lines...) Expand all Loading... |
31 const char kBackground[] = "/chrome_renderers/background"; | 31 const char kBackground[] = "/chrome_renderers/background"; |
32 const char kProcPath[] = "/proc/%d/cgroup"; | 32 const char kProcPath[] = "/proc/%d/cgroup"; |
33 | 33 |
34 struct CGroups { | 34 struct CGroups { |
35 // Check for cgroups files. ChromeOS supports these by default. It creates | 35 // Check for cgroups files. ChromeOS supports these by default. It creates |
36 // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups, | 36 // a cgroup mount in /sys/fs/cgroup and then configures two cpu task groups, |
37 // one contains at most a single foreground renderer and the other contains | 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 | 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. | 39 // renderers on foreground ones to a greater level than simple renicing. |
40 bool enabled; | 40 bool enabled; |
41 FilePath foreground_file; | 41 base::FilePath foreground_file; |
42 FilePath background_file; | 42 base::FilePath background_file; |
43 | 43 |
44 CGroups() { | 44 CGroups() { |
45 foreground_file = FilePath(StringPrintf(kControlPath, kForeground)); | 45 foreground_file = base::FilePath(StringPrintf(kControlPath, kForeground)); |
46 background_file = FilePath(StringPrintf(kControlPath, kBackground)); | 46 background_file = base::FilePath(StringPrintf(kControlPath, kBackground)); |
47 file_util::FileSystemType foreground_type; | 47 file_util::FileSystemType foreground_type; |
48 file_util::FileSystemType background_type; | 48 file_util::FileSystemType background_type; |
49 enabled = | 49 enabled = |
50 file_util::GetFileSystemType(foreground_file, &foreground_type) && | 50 file_util::GetFileSystemType(foreground_file, &foreground_type) && |
51 file_util::GetFileSystemType(background_file, &background_type) && | 51 file_util::GetFileSystemType(background_file, &background_type) && |
52 foreground_type == file_util::FILE_SYSTEM_CGROUP && | 52 foreground_type == file_util::FILE_SYSTEM_CGROUP && |
53 background_type == file_util::FILE_SYSTEM_CGROUP; | 53 background_type == file_util::FILE_SYSTEM_CGROUP; |
54 } | 54 } |
55 }; | 55 }; |
56 | 56 |
57 base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER; | 57 base::LazyInstance<CGroups> cgroups = LAZY_INSTANCE_INITIALIZER; |
58 #else | 58 #else |
59 const int kBackgroundPriority = 5; | 59 const int kBackgroundPriority = 5; |
60 #endif | 60 #endif |
61 } | 61 } |
62 | 62 |
63 namespace base { | 63 namespace base { |
64 | 64 |
65 bool Process::IsProcessBackgrounded() const { | 65 bool Process::IsProcessBackgrounded() const { |
66 DCHECK(process_); | 66 DCHECK(process_); |
67 | 67 |
68 #if defined(OS_CHROMEOS) | 68 #if defined(OS_CHROMEOS) |
69 if (cgroups.Get().enabled) { | 69 if (cgroups.Get().enabled) { |
70 std::string proc; | 70 std::string proc; |
71 if (file_util::ReadFileToString( | 71 if (file_util::ReadFileToString( |
72 FilePath(StringPrintf(kProcPath, process_)), | 72 base::FilePath(StringPrintf(kProcPath, process_)), |
73 &proc)) { | 73 &proc)) { |
74 std::vector<std::string> proc_parts; | 74 std::vector<std::string> proc_parts; |
75 base::SplitString(proc, ':', &proc_parts); | 75 base::SplitString(proc, ':', &proc_parts); |
76 DCHECK(proc_parts.size() == 3); | 76 DCHECK(proc_parts.size() == 3); |
77 bool ret = proc_parts[2] == std::string(kBackground); | 77 bool ret = proc_parts[2] == std::string(kBackground); |
78 return ret; | 78 return ret; |
79 } else { | 79 } else { |
80 return false; | 80 return false; |
81 } | 81 } |
82 } | 82 } |
83 #endif | 83 #endif |
84 return GetPriority() == kBackgroundPriority; | 84 return GetPriority() == kBackgroundPriority; |
85 } | 85 } |
86 | 86 |
87 bool Process::SetProcessBackgrounded(bool background) { | 87 bool Process::SetProcessBackgrounded(bool background) { |
88 DCHECK(process_); | 88 DCHECK(process_); |
89 | 89 |
90 #if defined(OS_CHROMEOS) | 90 #if defined(OS_CHROMEOS) |
91 if (cgroups.Get().enabled) { | 91 if (cgroups.Get().enabled) { |
92 std::string pid = StringPrintf("%d", process_); | 92 std::string pid = StringPrintf("%d", process_); |
93 const FilePath file = | 93 const base::FilePath file = |
94 background ? | 94 background ? |
95 cgroups.Get().background_file : cgroups.Get().foreground_file; | 95 cgroups.Get().background_file : cgroups.Get().foreground_file; |
96 return file_util::WriteFile(file, pid.c_str(), pid.size()) > 0; | 96 return file_util::WriteFile(file, pid.c_str(), pid.size()) > 0; |
97 } | 97 } |
98 #endif // OS_CHROMEOS | 98 #endif // OS_CHROMEOS |
99 | 99 |
100 if (!CanBackgroundProcesses()) | 100 if (!CanBackgroundProcesses()) |
101 return false; | 101 return false; |
102 | 102 |
103 int priority = background ? kBackgroundPriority : kForegroundPriority; | 103 int priority = background ? kBackgroundPriority : kForegroundPriority; |
(...skipping 22 matching lines...) Expand all Loading... |
126 if (cgroups.Get().enabled) | 126 if (cgroups.Get().enabled) |
127 return true; | 127 return true; |
128 #endif | 128 #endif |
129 | 129 |
130 static LazyInstance<CheckForNicePermission> check_for_nice_permission = | 130 static LazyInstance<CheckForNicePermission> check_for_nice_permission = |
131 LAZY_INSTANCE_INITIALIZER; | 131 LAZY_INSTANCE_INITIALIZER; |
132 return check_for_nice_permission.Get().can_reraise_priority; | 132 return check_for_nice_permission.Get().can_reraise_priority; |
133 } | 133 } |
134 | 134 |
135 } // namespace base | 135 } // namespace base |
OLD | NEW |