blob: e6d7331ef48859d81dc67221bb1933659e2d05ec [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
Alex Deymoab0d9762016-02-02 10:52:56 -080033class CPULimiter {
34 public:
35 CPULimiter() = default;
36 ~CPULimiter();
37
38 // Sets the cpu shares to low and sets up timeout events to stop the limiter.
39 void StartLimiter();
40
41 // Resets the cpu shares to normal and destroys any scheduled timeout sources.
42 void StopLimiter();
43
44 // Sets the cpu shares to |shares|. This method can be user at any time, but
45 // if the limiter is not running, the shares won't be reset to normal.
46 bool SetCpuShares(CpuShares shares);
47
48 private:
49 // The cpu shares timeout source callback sets the current cpu shares to
50 // normal.
51 void StopLimiterCallback();
52
53 // Current cpu shares.
54 CpuShares shares_ = CpuShares::kNormal;
55
56 // The cpu shares management timeout task id.
57 brillo::MessageLoop::TaskId manage_shares_id_{
58 brillo::MessageLoop::kTaskIdNull};
59};
60
61} // namespace chromeos_update_engine
62
63#endif // UPDATE_ENGINE_COMMON_CPU_LIMITER_H_