1 /*
2  * Copyright (C) 2020 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 #pragma once
18 
19 #include <PowerStatsAidl.h>
20 #include "PowerStatsEnergyAttribution.h"
21 
22 #include <utils/RefBase.h>
23 
24 #include <map>
25 #include <set>
26 
27 namespace aidl {
28 namespace android {
29 namespace hardware {
30 namespace power {
31 namespace stats {
32 
33 using ::android::sp;
34 
35 /**
36  * PowerStatsEnergyConsumer is an energy consumer that can be represented as
37  * EnergyConsumed = SUM_i(E_i) + SUM_j(C_j * T_j)
38  * where E_i is the energy of channel i of the EnergyMeter and
39  * where C_j is the coefficient (in mW) of state j and T_j is the total time (in ms) in state j
40  *
41  * Factory functions are provided to create PowerStatsEnergyConsumer of three varieties
42  * 1. MeterAndEntityConsumer - number of channels is > 0, and there is at least one C_j != 0
43  * 2. MeterConsumer - number of channels is > 0, and all C_j = 0
44  * 3. EntityConsumer - number of channels is 0, and there is at least one C_j != 0
45  */
46 class PowerStatsEnergyConsumer : public PowerStats::IEnergyConsumer {
47   public:
48     static std::unique_ptr<PowerStatsEnergyConsumer> createMeterConsumer(
49             std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
50             std::set<std::string> channelNames);
51     static std::unique_ptr<PowerStatsEnergyConsumer> createEntityConsumer(
52             std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
53             std::string powerEntityName, std::map<std::string, int32_t> stateCoeffs);
54 
55     static std::unique_ptr<PowerStatsEnergyConsumer> createMeterAndEntityConsumer(
56             std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
57             std::set<std::string> channelNames, std::string powerEntityName,
58             std::map<std::string, int32_t> stateCoeffs);
59 
60     static std::unique_ptr<PowerStatsEnergyConsumer> createMeterAndAttrConsumer(
61             std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
62             std::set<std::string> channelNames, std::unordered_map<int32_t, std::string> paths,
63             std::map<std::string, int32_t> stateCoeffs);
64 
getInfo()65     std::pair<EnergyConsumerType, std::string> getInfo() override { return {kType, kName}; }
66 
67     std::optional<EnergyConsumerResult> getEnergyConsumed() override;
68 
69     std::string getConsumerName() override;
70 
71   private:
72     PowerStatsEnergyConsumer(std::shared_ptr<PowerStats> p, EnergyConsumerType type,
73                              std::string name, bool attr = false);
74     bool addEnergyMeter(std::set<std::string> channelNames);
75     bool addPowerEntity(std::string powerEntityName, std::map<std::string, int32_t> stateCoeffs);
76     bool addAttribution(std::unordered_map<int32_t, std::string> paths,
77                         std::map<std::string, int32_t> stateCoeffs);
78 
79     const EnergyConsumerType kType;
80     const std::string kName;
81     std::shared_ptr<PowerStats> mPowerStats;
82     std::vector<int32_t> mChannelIds;
83     int32_t mPowerEntityId;
84     bool mWithAttribution;
85     std::unordered_map<int32_t, std::string> mAttrInfoPath;
86     PowerStatsEnergyAttribution mEnergyAttribution;
87     // Snapshot of each uid's energy, uid_time_in_state and total energy from power meter
88     // mUidTimeInStateSS: key = uid, val = {uid_time_in_state}
89     // mUidEnergySS:      key = uid, val = {uid's energy(UWs)}
90     // mTotalEnergySS:    total energy from power meter
91     std::unordered_map<int32_t, std::vector<long>> mUidTimeInStateSS;
92     std::unordered_map<int32_t, int64_t> mUidEnergySS;
93     int64_t mTotalEnergySS = 0;
94     std::map<int32_t, int32_t> mCoefficients;  // key = stateId, val = coefficients (mW)
95 };
96 
97 }  // namespace stats
98 }  // namespace power
99 }  // namespace hardware
100 }  // namespace android
101 }  // namespace aidl
102