1 /*
2  * Copyright (C) 2017 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 package com.android.internal.os;
17 
18 import android.util.ArrayMap;
19 
20 import java.util.Map;
21 
22 /**
23  * Container for Resource Power Manager states and their data.
24  * Values can be populated by the BatteryStatsService.fillLowPowerStats jni function.
25  */
26 public final class RpmStats {
27     public Map<String, PowerStatePlatformSleepState> mPlatformLowPowerStats = new ArrayMap<>();
28     public Map<String, PowerStateSubsystem> mSubsystemLowPowerStats = new ArrayMap<>();
29 
30     /**
31      * Finds the PowerStatePlatformSleepState with the given name (creating it if it doesn't exist),
32      * updates its timeMs and count, and returns it.
33      */
34     @SuppressWarnings("unused")
getAndUpdatePlatformState( String name, long timeMs, int count)35     public PowerStatePlatformSleepState getAndUpdatePlatformState(
36             String name, long timeMs, int count) {
37 
38         PowerStatePlatformSleepState e = mPlatformLowPowerStats.get(name);
39         if (e == null) {
40             e = new PowerStatePlatformSleepState();
41             mPlatformLowPowerStats.put(name, e);
42         }
43         e.mTimeMs = timeMs;
44         e.mCount = count;
45         return e;
46     }
47 
48     /**
49      * Returns the PowerStateSubsystem with the given name (creating it if it doesn't exist).
50      */
getSubsystem(String name)51     public PowerStateSubsystem getSubsystem(String name) {
52         PowerStateSubsystem e = mSubsystemLowPowerStats.get(name);
53         if (e == null) {
54             e = new PowerStateSubsystem();
55             mSubsystemLowPowerStats.put(name, e);
56         }
57         return e;
58     }
59 
60     /** Represents a subsystem state or a platform voter. */
61     public static class PowerStateElement {
62         public long mTimeMs; // totalTimeInMsecVotedForSinceBoot
63         public int mCount; // totalNumberOfTimesVotedSinceBoot
64 
PowerStateElement(long timeMs, int count)65         private PowerStateElement(long timeMs, int count) {
66             this.mTimeMs = timeMs;
67             this.mCount = count;
68         }
69     }
70 
71     /** Represents a PowerStatePlatformSleepState, per hardware/interfaces/power/1.0/types.hal */
72     public static class PowerStatePlatformSleepState {
73         public long mTimeMs; // residencyInMsecSinceBoot
74         public int mCount; // totalTransitions
75         public Map<String, PowerStateElement> mVoters = new ArrayMap<>(); // voters for this platform-level sleep state
76 
77         /**
78          * Updates (creating if necessary) the voter with the given name, with the given timeMs and
79          * count.
80          */
81         @SuppressWarnings("unused")
putVoter(String name, long timeMs, int count)82         public void putVoter(String name, long timeMs, int count) {
83             PowerStateElement e = mVoters.get(name);
84             if (e == null) {
85                 mVoters.put(name, new PowerStateElement(timeMs, count));
86             } else {
87                 e.mTimeMs = timeMs;
88                 e.mCount = count;
89             }
90         }
91     }
92 
93     /** Represents a PowerStateSubsystem, per hardware/interfaces/power/1.1/types.hal */
94     public static class PowerStateSubsystem {
95         public Map<String, PowerStateElement> mStates = new ArrayMap<>(); // sleep states supported by this susbsystem
96 
97         /**
98          * Updates (creating if necessary) the subsystem state with the given name, with the given
99          * timeMs and count.
100          */
101         @SuppressWarnings("unused")
putState(String name, long timeMs, int count)102         public void putState(String name, long timeMs, int count) {
103             PowerStateElement e = mStates.get(name);
104             if (e == null) {
105                 mStates.put(name, new PowerStateElement(timeMs, count));
106             } else {
107                 e.mTimeMs = timeMs;
108                 e.mCount = count;
109             }
110         }
111     }
112 }
113