OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2008 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/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/stringprintf.h" |
| 13 |
| 14 #if defined(OS_CHROMEOS) |
| 15 static bool use_cgroups = false; |
| 16 static bool cgroups_inited = false; |
| 17 static const char kForegroundTasks[] = |
| 18 "/tmp/cgroup/cpu/chrome_renderers/foreground/tasks"; |
| 19 static const char kBackgroundTasks[] = |
| 20 "/tmp/cgroup/cpu/chrome_renderers/background/tasks"; |
| 21 static FilePath foreground_tasks; |
| 22 static FilePath background_tasks; |
| 23 #endif |
11 | 24 |
12 namespace base { | 25 namespace base { |
13 | 26 |
14 #if defined(OS_CHROMEOS) | 27 #if defined(OS_CHROMEOS) |
15 // We are more aggressive in our lowering of background process priority | 28 // We are more aggressive in our lowering of background process priority |
16 // for chromeos as we have much more control over other processes running | 29 // for chromeos as we have much more control over other processes running |
17 // on the machine. | 30 // on the machine. |
18 const int kPriorityAdjustment = 19; | 31 const int kPriorityAdjustment = 19; |
19 #else | 32 #else |
20 const int kPriorityAdjustment = 5; | 33 const int kPriorityAdjustment = 5; |
21 #endif | 34 #endif |
22 | 35 |
23 bool Process::IsProcessBackgrounded() const { | 36 bool Process::IsProcessBackgrounded() const { |
24 DCHECK(process_); | 37 DCHECK(process_); |
25 return saved_priority_ == kUnsetProcessPriority; | 38 return saved_priority_ == kUnsetProcessPriority; |
26 } | 39 } |
27 | 40 |
28 bool Process::SetProcessBackgrounded(bool background) { | 41 bool Process::SetProcessBackgrounded(bool background) { |
29 DCHECK(process_); | 42 DCHECK(process_); |
30 | 43 |
| 44 #if defined(OS_CHROMEOS) |
| 45 // Check for cgroups files. ChromeOS supports these by default. It creates |
| 46 // a cgroup mount in /tmp/cgroup and then configures two cpu task groups, |
| 47 // one contains at most a single foreground renderer and the other contains |
| 48 // all background renderers. This allows us to limit the impact of background |
| 49 // renderers on foreground ones to a greater level than simple renicing. |
| 50 if (!cgroups_inited) { |
| 51 cgroups_inited = true; |
| 52 foreground_tasks = FilePath(kForegroundTasks); |
| 53 background_tasks = FilePath(kBackgroundTasks); |
| 54 file_util::FileSystemType foreground_type; |
| 55 file_util::FileSystemType background_type; |
| 56 use_cgroups = |
| 57 file_util::GetFileSystemType(foreground_tasks, &foreground_type) && |
| 58 file_util::GetFileSystemType(background_tasks, &background_type) && |
| 59 foreground_type == file_util::FILE_SYSTEM_CGROUP && |
| 60 background_type == file_util::FILE_SYSTEM_CGROUP; |
| 61 } |
| 62 |
| 63 if (use_cgroups) { |
| 64 if (background) { |
| 65 std::string pid = StringPrintf("%d", process_); |
| 66 if (file_util::WriteFile(background_tasks, pid.c_str(), pid.size()) > 0) { |
| 67 // With cgroups there's no real notion of priority as an int, but this |
| 68 // will ensure we only move renderers back to the foreground group |
| 69 // if we've ever put them in the background one. |
| 70 saved_priority_ = 0; |
| 71 return true; |
| 72 } else { |
| 73 return false; |
| 74 } |
| 75 } else { |
| 76 if (saved_priority_ == kUnsetProcessPriority) { |
| 77 // Can't restore if we were never backgrounded. |
| 78 return false; |
| 79 } |
| 80 std::string pid = StringPrintf("%d", process_); |
| 81 if (file_util::WriteFile(foreground_tasks, pid.c_str(), pid.size()) > 0) { |
| 82 saved_priority_ = kUnsetProcessPriority; |
| 83 return true; |
| 84 } else { |
| 85 return false; |
| 86 } |
| 87 } |
| 88 } |
| 89 #endif // OS_CHROMEOS |
| 90 |
31 if (background) { | 91 if (background) { |
32 // We won't be able to raise the priority if we don't have the right rlimit. | 92 // We won't be able to raise the priority if we don't have the right rlimit. |
33 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. | 93 // The limit may be adjusted in /etc/security/limits.conf for PAM systems. |
34 struct rlimit rlim; | 94 struct rlimit rlim; |
35 if (getrlimit(RLIMIT_NICE, &rlim) != 0) { | 95 if (getrlimit(RLIMIT_NICE, &rlim) != 0) { |
36 // Call to getrlimit failed, don't background. | 96 // Call to getrlimit failed, don't background. |
37 return false; | 97 return false; |
38 } | 98 } |
39 errno = 0; | 99 errno = 0; |
40 int current_priority = GetPriority(); | 100 int current_priority = GetPriority(); |
(...skipping 24 matching lines...) Expand all Loading... |
65 } | 125 } |
66 int result = setpriority(PRIO_PROCESS, process_, saved_priority_); | 126 int result = setpriority(PRIO_PROCESS, process_, saved_priority_); |
67 // If we can't restore something has gone terribly wrong. | 127 // If we can't restore something has gone terribly wrong. |
68 DPCHECK(result == 0); | 128 DPCHECK(result == 0); |
69 saved_priority_ = kUnsetProcessPriority; | 129 saved_priority_ = kUnsetProcessPriority; |
70 return true; | 130 return true; |
71 } | 131 } |
72 } | 132 } |
73 | 133 |
74 } // namespace base | 134 } // namespace base |
OLD | NEW |