1 //
2 // Copyright (C) 2016 The Android Open Source Project
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 //
16
17 #include "update_engine/common/cpu_limiter.h"
18
19 #include <string>
20
21 #include <base/bind.h>
22 #include <base/logging.h>
23 #include <base/time/time.h>
24
25 #include "update_engine/common/utils.h"
26
27 namespace {
28
29 // Cgroup container is created in update-engine's upstart script located at
30 // /etc/init/update-engine.conf.
31 const char kCGroupSharesPath[] = "/sys/fs/cgroup/cpu/update-engine/cpu.shares";
32
33 } // namespace
34
35 namespace chromeos_update_engine {
36
~CPULimiter()37 CPULimiter::~CPULimiter() {
38 // Set everything back to normal on destruction.
39 CPULimiter::SetCpuShares(CpuShares::kNormal);
40 }
41
StartLimiter()42 void CPULimiter::StartLimiter() {
43 if (manage_shares_id_ != brillo::MessageLoop::kTaskIdNull) {
44 LOG(ERROR) << "Cpu shares timeout source hasn't been destroyed.";
45 StopLimiter();
46 }
47 manage_shares_id_ = brillo::MessageLoop::current()->PostDelayedTask(
48 FROM_HERE,
49 base::Bind(&CPULimiter::StopLimiterCallback, base::Unretained(this)),
50 base::TimeDelta::FromHours(2));
51 SetCpuShares(CpuShares::kLow);
52 }
53
StopLimiter()54 void CPULimiter::StopLimiter() {
55 if (manage_shares_id_ != brillo::MessageLoop::kTaskIdNull) {
56 // If the shares were never set and there isn't a message loop instance,
57 // we avoid calling CancelTask(), which otherwise would have been a no-op.
58 brillo::MessageLoop::current()->CancelTask(manage_shares_id_);
59 manage_shares_id_ = brillo::MessageLoop::kTaskIdNull;
60 }
61 SetCpuShares(CpuShares::kNormal);
62 }
63
SetCpuShares(CpuShares shares)64 bool CPULimiter::SetCpuShares(CpuShares shares) {
65 // Short-circuit to avoid re-setting the shares.
66 if (shares_ == shares)
67 return true;
68
69 std::string string_shares = std::format("{}", static_cast<int>(shares));
70 LOG(INFO) << "Setting cgroup cpu shares to " << string_shares;
71 if (!utils::WriteFile(
72 kCGroupSharesPath, string_shares.c_str(), string_shares.size())) {
73 LOG(ERROR) << "Failed to change cgroup cpu shares to " << string_shares
74 << " using " << kCGroupSharesPath;
75 return false;
76 }
77 shares_ = shares;
78 LOG(INFO) << "CPU shares = " << static_cast<int>(shares_);
79 return true;
80 }
81
StopLimiterCallback()82 void CPULimiter::StopLimiterCallback() {
83 SetCpuShares(CpuShares::kNormal);
84 manage_shares_id_ = brillo::MessageLoop::kTaskIdNull;
85 }
86
87 } // namespace chromeos_update_engine
88