blob: 322639005132843ffcf3ea204bedde944b1922c0 [file] [log] [blame]
Alex Deymoab0d9762016-02-02 10:52:56 -08001//
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>
Alex Deymoab0d9762016-02-02 10:52:56 -080023#include <base/time/time.h>
24
25#include "update_engine/common/utils.h"
26
27namespace {
28
29// Cgroup container is created in update-engine's upstart script located at
30// /etc/init/update-engine.conf.
31const char kCGroupSharesPath[] = "/sys/fs/cgroup/cpu/update-engine/cpu.shares";
32
33} // namespace
34
35namespace chromeos_update_engine {
36
37CPULimiter::~CPULimiter() {
38 // Set everything back to normal on destruction.
39 CPULimiter::SetCpuShares(CpuShares::kNormal);
40}
41
42void 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
54void 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
64bool CPULimiter::SetCpuShares(CpuShares shares) {
65 // Short-circuit to avoid re-setting the shares.
66 if (shares_ == shares)
67 return true;
68
Kokoa Matsuda1f46f5b2024-10-18 16:00:05 +090069 std::string string_shares = std::format("{}", static_cast<int>(shares));
Alex Deymoab0d9762016-02-02 10:52:56 -080070 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;
Luis Hector Chavez696faf42016-05-20 23:14:32 -070078 LOG(INFO) << "CPU shares = " << static_cast<int>(shares_);
Alex Deymoab0d9762016-02-02 10:52:56 -080079 return true;
80}
81
82void CPULimiter::StopLimiterCallback() {
83 SetCpuShares(CpuShares::kNormal);
84 manage_shares_id_ = brillo::MessageLoop::kTaskIdNull;
85}
86
87} // namespace chromeos_update_engine