blob: c7add89d5862d65b160b374f769aeeaa4c23b3f8 [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#ifndef UPDATE_ENGINE_COMMON_CPU_LIMITER_H_
18#define UPDATE_ENGINE_COMMON_CPU_LIMITER_H_
19
20#include <brillo/message_loops/message_loop.h>
21
22namespace chromeos_update_engine {
23
24// Cgroups cpu shares constants. 1024 is the default shares a standard process
25// gets and 2 is the minimum value. We set High as a value that gives the
26// update-engine 2x the cpu share of a standard process.
27enum class CpuShares : int {
28 kHigh = 2048,
29 kNormal = 1024,
30 kLow = 2,
31};
32
33// Sets the current process shares to |shares|. Returns true on
34// success, false otherwise.
35bool SetCpuShares(CpuShares shares);
36
37class CPULimiter {
38 public:
39 CPULimiter() = default;
40 ~CPULimiter();
41
42 // Sets the cpu shares to low and sets up timeout events to stop the limiter.
43 void StartLimiter();
44
45 // Resets the cpu shares to normal and destroys any scheduled timeout sources.
46 void StopLimiter();
47
48 // Sets the cpu shares to |shares|. This method can be user at any time, but
49 // if the limiter is not running, the shares won't be reset to normal.
50 bool SetCpuShares(CpuShares shares);
51
52 private:
53 // The cpu shares timeout source callback sets the current cpu shares to
54 // normal.
55 void StopLimiterCallback();
56
57 // Current cpu shares.
58 CpuShares shares_ = CpuShares::kNormal;
59
60 // The cpu shares management timeout task id.
61 brillo::MessageLoop::TaskId manage_shares_id_{
62 brillo::MessageLoop::kTaskIdNull};
63};
64
65} // namespace chromeos_update_engine
66
67#endif // UPDATE_ENGINE_COMMON_CPU_LIMITER_H_