OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "chrome/browser/chromeos/power/freezer_cgroup_process_manager.h" | 5 #include "chrome/browser/chromeos/power/freezer_cgroup_process_manager.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/files/file_util.h" | 9 #include "base/files/file_util.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
| 11 #include "base/strings/stringprintf.h" |
11 | 12 |
12 namespace chromeos { | 13 namespace chromeos { |
13 | 14 |
14 namespace { | 15 namespace { |
15 const char kFreezerStatePath[] = | 16 const char kFreezerPath[] = "/sys/fs/cgroup/freezer/chrome_renderers"; |
16 "/sys/fs/cgroup/freezer/chrome_renderers/freezer.state"; | 17 const char kToBeFrozen[] = "to_be_frozen"; |
| 18 const char kFreezerState[] = "freezer.state"; |
| 19 const char kCgroupProcs[] = "cgroup.procs"; |
| 20 |
17 const char kFreezeCommand[] = "FROZEN"; | 21 const char kFreezeCommand[] = "FROZEN"; |
18 const char kThawCommand[] = "THAWED"; | 22 const char kThawCommand[] = "THAWED"; |
19 | 23 |
20 } // namespace | 24 } // namespace |
21 | 25 |
22 FreezerCgroupProcessManager::FreezerCgroupProcessManager() | 26 FreezerCgroupProcessManager::FreezerCgroupProcessManager() |
23 : state_path_(base::FilePath(kFreezerStatePath)), | 27 : default_control_path_(base::FilePath(kFreezerPath).Append(kCgroupProcs)), |
24 enabled_(base::PathIsWritable(state_path_)) { | 28 to_be_frozen_control_path_(base::FilePath(kFreezerPath) |
| 29 .AppendASCII(kToBeFrozen) |
| 30 .AppendASCII(kCgroupProcs)), |
| 31 to_be_frozen_state_path_(base::FilePath(kFreezerPath) |
| 32 .AppendASCII(kToBeFrozen) |
| 33 .AppendASCII(kFreezerState)), |
| 34 enabled_(base::PathIsWritable(default_control_path_) && |
| 35 base::PathIsWritable(to_be_frozen_control_path_) && |
| 36 base::PathIsWritable(to_be_frozen_state_path_)) { |
25 if (!enabled_) { | 37 if (!enabled_) { |
26 LOG(WARNING) << "Cgroup freezer does not exist or is not writable. " | 38 LOG(WARNING) << "Cgroup freezer does not exist or is not writable. " |
27 << "Unable to freeze renderer processes."; | 39 << "Unable to freeze renderer processes."; |
28 } | 40 } |
29 } | 41 } |
30 | 42 |
31 FreezerCgroupProcessManager::~FreezerCgroupProcessManager() { | 43 FreezerCgroupProcessManager::~FreezerCgroupProcessManager() { |
32 } | 44 } |
33 | 45 |
| 46 void FreezerCgroupProcessManager::SetShouldFreezeRenderer( |
| 47 base::ProcessHandle handle, |
| 48 bool frozen) { |
| 49 std::string pid = base::StringPrintf("%d", handle); |
| 50 |
| 51 WriteCommandToFile( |
| 52 pid, frozen ? to_be_frozen_control_path_ : default_control_path_); |
| 53 } |
| 54 |
34 bool FreezerCgroupProcessManager::FreezeRenderers() { | 55 bool FreezerCgroupProcessManager::FreezeRenderers() { |
35 if (!enabled_) { | 56 if (!enabled_) { |
36 LOG(ERROR) << "Attempting to freeze renderers when the freezer cgroup is " | 57 LOG(ERROR) << "Attempting to freeze renderers when the freezer cgroup is " |
37 << "not available."; | 58 << "not available."; |
38 return false; | 59 return false; |
39 } | 60 } |
40 | 61 |
41 return WriteCommandToStateFile(kFreezeCommand); | 62 return WriteCommandToFile(kFreezeCommand, to_be_frozen_state_path_); |
42 } | 63 } |
43 | 64 |
44 bool FreezerCgroupProcessManager::ThawRenderers() { | 65 bool FreezerCgroupProcessManager::ThawRenderers() { |
45 if (!enabled_) { | 66 if (!enabled_) { |
46 LOG(ERROR) << "Attempting to thaw renderers when the freezer cgroup is not " | 67 LOG(ERROR) << "Attempting to thaw renderers when the freezer cgroup is not " |
47 << "available."; | 68 << "available."; |
48 return false; | 69 return false; |
49 } | 70 } |
50 | 71 |
51 return WriteCommandToStateFile(kThawCommand); | 72 return WriteCommandToFile(kThawCommand, to_be_frozen_state_path_); |
52 } | 73 } |
53 | 74 |
54 bool FreezerCgroupProcessManager::CanFreezeRenderers() { | 75 bool FreezerCgroupProcessManager::CanFreezeRenderers() { |
55 return enabled_; | 76 return enabled_; |
56 } | 77 } |
57 | 78 |
58 bool FreezerCgroupProcessManager::WriteCommandToStateFile( | 79 bool FreezerCgroupProcessManager::WriteCommandToFile( |
59 const std::string& command) { | 80 const std::string& command, |
60 int bytes = base::WriteFile(state_path_, command.c_str(), command.size()); | 81 const base::FilePath& file) { |
| 82 int bytes = base::WriteFile(file, command.c_str(), command.size()); |
61 if (bytes == -1) { | 83 if (bytes == -1) { |
62 PLOG(ERROR) << "Writing " << command << " to " << state_path_.value() | 84 PLOG(ERROR) << "Writing " << command << " to " << file.value() << " failed"; |
63 << " failed"; | |
64 return false; | 85 return false; |
65 } else if (bytes != static_cast<int>(command.size())) { | 86 } else if (bytes != static_cast<int>(command.size())) { |
66 LOG(ERROR) << "Only wrote " << bytes << " byte(s) when writing " | 87 LOG(ERROR) << "Only wrote " << bytes << " byte(s) when writing " << command |
67 << command << " to " << state_path_.value(); | 88 << " to " << file.value(); |
68 return false; | 89 return false; |
69 } | 90 } |
70 return true; | 91 return true; |
71 } | 92 } |
72 | 93 |
73 } // namespace chromeos | 94 } // namespace chromeos |
OLD | NEW |