• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 package com.android.server.cpu;
18 
19 import android.annotation.IntDef;
20 import android.util.IntArray;
21 
22 import java.lang.annotation.Retention;
23 import java.lang.annotation.RetentionPolicy;
24 
25 /** CPU availability monitoring config. */
26 public final class CpuAvailabilityMonitoringConfig {
27     /** Constant to monitor all cpusets. */
28     public static final int CPUSET_ALL = 1;
29 
30     /** Constant to monitor background cpusets. */
31     public static final int CPUSET_BACKGROUND = 2;
32 
33     @Retention(RetentionPolicy.SOURCE)
34     @IntDef(prefix = {"CPUSET_"}, value = {
35             CPUSET_ALL,
36             CPUSET_BACKGROUND
37     })
38     public @interface Cpuset {
39     }
40 
41     /**
42      * The CPUSET to monitor.
43      *
44      * <p>The value must be one of the {@code CPUSET_*} constants.
45      */
46     @Cpuset
47     public final int cpuset;
48 
49     /**
50      * CPU availability percent thresholds.
51      *
52      * <p>CPU availability change notifications are sent when the latest or last N seconds average
53      * CPU availability percent crosses any of these thresholds since the last notification.
54      */
55     private final IntArray mThresholds;
56 
getThresholds()57     public IntArray getThresholds() {
58         return mThresholds;
59     }
60 
61     /**
62      * Builder for the construction of {@link CpuAvailabilityMonitoringConfig} objects.
63      *
64      * <p>The builder must contain at least one threshold before calling {@link build}.
65      */
66     public static final class Builder {
67         private final int mCpuset;
68         private final IntArray mThresholds = new IntArray();
69 
Builder(int cpuset, int... thresholds)70         public Builder(int cpuset, int... thresholds) {
71             mCpuset = cpuset;
72             for (int threshold : thresholds) {
73                 addThreshold(threshold);
74             }
75         }
76 
77         /** Adds the given threshold to the builder object. */
addThreshold(int threshold)78         public Builder addThreshold(int threshold) {
79             if (mThresholds.indexOf(threshold) == -1) {
80                 mThresholds.add(threshold);
81             }
82             return this;
83         }
84 
85         /** Returns the {@link CpuAvailabilityMonitoringConfig} object. */
build()86         public CpuAvailabilityMonitoringConfig build() {
87             return new CpuAvailabilityMonitoringConfig(this);
88         }
89     }
90 
91     @Override
toString()92     public String toString() {
93         return "CpuAvailabilityMonitoringConfig{cpuset=" + toCpusetString(cpuset) + ", mThresholds="
94                 + mThresholds + ')';
95     }
96 
97     /** Returns the string equivalent of the provided cpuset. */
toCpusetString(int cpuset)98     public static String toCpusetString(int cpuset) {
99         switch (cpuset) {
100             case CPUSET_ALL:
101                 return "CPUSET_ALL";
102             case CPUSET_BACKGROUND:
103                 return "CPUSET_BACKGROUND";
104         }
105         return "Invalid cpuset: " + cpuset;
106     }
107 
CpuAvailabilityMonitoringConfig(Builder builder)108     private CpuAvailabilityMonitoringConfig(Builder builder) {
109         if (builder.mCpuset != CPUSET_ALL && builder.mCpuset != CPUSET_BACKGROUND) {
110             throw new IllegalStateException("Cpuset must be either CPUSET_ALL (" + CPUSET_ALL
111                     + ") or CPUSET_BACKGROUND (" + CPUSET_BACKGROUND + "). Builder contains "
112                     + builder.mCpuset);
113         }
114         if (builder.mThresholds.size() == 0) {
115             throw new IllegalStateException("Must provide at least one threshold");
116         }
117         this.cpuset = builder.mCpuset;
118         this.mThresholds = builder.mThresholds.clone();
119     }
120 }
121