• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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.power.stats.processor;
18 
19 import android.util.IntArray;
20 
21 import com.android.internal.os.PowerStats;
22 import com.android.server.power.stats.format.EnergyConsumerPowerStatsLayout;
23 
24 import java.util.List;
25 
26 class CustomEnergyConsumerPowerStatsProcessor extends PowerStatsProcessor {
27     private static final EnergyConsumerPowerStatsLayout sLayout =
28             new EnergyConsumerPowerStatsLayout();
29     private long[] mTmpDeviceStatsArray;
30     private long[] mTmpUidStatsArray;
31     private PowerEstimationPlan mPlan;
32 
33     @Override
finish(PowerComponentAggregatedPowerStats stats, long timestampMs)34     void finish(PowerComponentAggregatedPowerStats stats, long timestampMs) {
35         PowerStats.Descriptor descriptor = stats.getPowerStatsDescriptor();
36         mTmpDeviceStatsArray = new long[descriptor.statsArrayLength];
37         mTmpUidStatsArray = new long[descriptor.uidStatsArrayLength];
38         if (mPlan == null) {
39             mPlan = new PowerEstimationPlan(stats.getConfig());
40         }
41 
42         computeDevicePowerEstimates(stats);
43 
44         IntArray uids = stats.getActiveUids();
45         if (uids.size() != 0) {
46             computeUidPowerEstimates(stats, uids);
47         }
48     }
49 
computeDevicePowerEstimates(PowerComponentAggregatedPowerStats stats)50     private void computeDevicePowerEstimates(PowerComponentAggregatedPowerStats stats) {
51         for (int i = mPlan.deviceStateEstimations.size() - 1; i >= 0; i--) {
52             DeviceStateEstimation estimation = mPlan.deviceStateEstimations.get(i);
53             if (!stats.getDeviceStats(mTmpDeviceStatsArray, estimation.stateValues)) {
54                 continue;
55             }
56 
57             sLayout.setDevicePowerEstimate(mTmpDeviceStatsArray,
58                     uCtoMah(sLayout.getConsumedEnergy(mTmpDeviceStatsArray, 0)));
59             stats.setDeviceStats(estimation.stateValues, mTmpDeviceStatsArray);
60         }
61     }
62 
computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats, IntArray uids)63     private void computeUidPowerEstimates(PowerComponentAggregatedPowerStats stats,
64             IntArray uids) {
65         for (int i = mPlan.uidStateEstimates.size() - 1; i >= 0; i--) {
66             UidStateEstimate uidStateEstimate = mPlan.uidStateEstimates.get(i);
67             List<UidStateProportionalEstimate> proportionalEstimates =
68                     uidStateEstimate.proportionalEstimates;
69             for (int j = proportionalEstimates.size() - 1; j >= 0; j--) {
70                 UidStateProportionalEstimate proportionalEstimate = proportionalEstimates.get(j);
71                 for (int k = uids.size() - 1; k >= 0; k--) {
72                     int uid = uids.get(k);
73                     if (stats.getUidStats(mTmpUidStatsArray, uid,
74                             proportionalEstimate.stateValues)) {
75                         double power = uCtoMah(sLayout.getUidConsumedEnergy(mTmpUidStatsArray, 0));
76                         if (power != 0) {
77                             sLayout.setUidPowerEstimate(mTmpUidStatsArray, power);
78                             stats.setUidStats(uid, proportionalEstimate.stateValues,
79                                     mTmpUidStatsArray);
80                         }
81                     }
82                 }
83             }
84         }
85     }
86 }
87