12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- #include "update_engine/common/cpu_limiter.h"
- #include <string>
- #include <base/bind.h>
- #include <base/logging.h>
- #include <base/strings/string_number_conversions.h>
- #include <base/time/time.h>
- #include "update_engine/common/utils.h"
- namespace {
- const char kCGroupSharesPath[] = "/sys/fs/cgroup/cpu/update-engine/cpu.shares";
- }
- namespace chromeos_update_engine {
- CPULimiter::~CPULimiter() {
-
- CPULimiter::SetCpuShares(CpuShares::kNormal);
- }
- void CPULimiter::StartLimiter() {
- if (manage_shares_id_ != brillo::MessageLoop::kTaskIdNull) {
- LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
- StopLimiter();
- }
- manage_shares_id_ = brillo::MessageLoop::current()->PostDelayedTask(
- FROM_HERE,
- base::Bind(&CPULimiter::StopLimiterCallback, base::Unretained(this)),
- base::TimeDelta::FromHours(2));
- SetCpuShares(CpuShares::kLow);
- }
- void CPULimiter::StopLimiter() {
- if (manage_shares_id_ != brillo::MessageLoop::kTaskIdNull) {
-
-
- brillo::MessageLoop::current()->CancelTask(manage_shares_id_);
- manage_shares_id_ = brillo::MessageLoop::kTaskIdNull;
- }
- SetCpuShares(CpuShares::kNormal);
- }
- bool CPULimiter::SetCpuShares(CpuShares shares) {
-
- if (shares_ == shares)
- return true;
- std::string string_shares = base::IntToString(static_cast<int>(shares));
- LOG(INFO) << "Setting cgroup cpu shares to " << string_shares;
- if (!utils::WriteFile(
- kCGroupSharesPath, string_shares.c_str(), string_shares.size())) {
- LOG(ERROR) << "Failed to change cgroup cpu shares to " << string_shares
- << " using " << kCGroupSharesPath;
- return false;
- }
- shares_ = shares;
- LOG(INFO) << "CPU shares = " << static_cast<int>(shares_);
- return true;
- }
- void CPULimiter::StopLimiterCallback() {
- SetCpuShares(CpuShares::kNormal);
- manage_shares_id_ = brillo::MessageLoop::kTaskIdNull;
- }
- }
|