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 #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 
22 namespace 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.
27 enum class CpuShares : int {
28   kHigh = 2048,
29   kNormal = 1024,
30   kLow = 2,
31 };
32 
33 class 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_
64