1 /*
2  * Copyright (C) 2023 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.internal.os;
18 
19 import android.annotation.NonNull;
20 import android.util.SparseArray;
21 
22 import libcore.util.EmptyArray;
23 
24 import java.util.Arrays;
25 
26 /**
27  * CPU scaling policies: the policy IDs and corresponding supported scaling for those
28  * policies.
29  */
30 @android.ravenwood.annotation.RavenwoodKeepWholeClass
31 public class CpuScalingPolicies {
32     private final SparseArray<int[]> mCpusByPolicy;
33     private final SparseArray<int[]> mFreqsByPolicy;
34     private final int[] mPolicies;
35     private final int mScalingStepCount;
36 
CpuScalingPolicies(@onNull SparseArray<int[]> cpusByPolicy, @NonNull SparseArray<int[]> freqsByPolicy)37     public CpuScalingPolicies(@NonNull SparseArray<int[]> cpusByPolicy,
38             @NonNull SparseArray<int[]> freqsByPolicy) {
39         mCpusByPolicy = cpusByPolicy;
40         mFreqsByPolicy = freqsByPolicy;
41 
42         mPolicies = new int[cpusByPolicy.size()];
43         for (int i = 0; i < mPolicies.length; i++) {
44             mPolicies[i] = cpusByPolicy.keyAt(i);
45         }
46 
47         Arrays.sort(mPolicies);
48 
49         int count = 0;
50         for (int i = freqsByPolicy.size() - 1; i >= 0; i--) {
51             count += freqsByPolicy.valueAt(i).length;
52         }
53         mScalingStepCount = count;
54     }
55 
56     /**
57      * Returns available policies (aka clusters).
58      */
59     @NonNull
getPolicies()60     public int[] getPolicies() {
61         return mPolicies;
62     }
63 
64     /**
65      * CPUs covered by the specified policy.
66      */
67     @NonNull
getRelatedCpus(int policy)68     public int[] getRelatedCpus(int policy) {
69         return mCpusByPolicy.get(policy, EmptyArray.INT);
70     }
71 
72     /**
73      * Scaling frequencies supported for the specified policy.
74      */
75     @NonNull
getFrequencies(int policy)76     public int[] getFrequencies(int policy) {
77         return mFreqsByPolicy.get(policy, EmptyArray.INT);
78     }
79 
80     /**
81      * Returns the overall number of supported scaling steps: grand total of available frequencies
82      * across all scaling policies.
83      */
getScalingStepCount()84     public int getScalingStepCount() {
85         return mScalingStepCount;
86     }
87 
88     @Override
toString()89     public String toString() {
90         StringBuilder sb = new StringBuilder();
91         for (int policy : mPolicies) {
92             sb.append("policy").append(policy)
93                     .append("\n CPUs: ").append(Arrays.toString(mCpusByPolicy.get(policy)))
94                     .append("\n freqs: ").append(Arrays.toString(mFreqsByPolicy.get(policy)))
95                     .append("\n");
96         }
97         return sb.toString();
98     }
99 }
100