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