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