summaryrefslogtreecommitdiff
path: root/powerstats/include/dataproviders/PowerStatsEnergyConsumer.h
blob: 349ca48953f8d51a2504b8a72c20aeb474cfff39 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#pragma once

#include <PowerStatsAidl.h>
#include "PowerStatsEnergyAttribution.h"

#include <utils/RefBase.h>

#include <map>
#include <set>

namespace aidl {
namespace android {
namespace hardware {
namespace power {
namespace stats {

using ::android::sp;

/**
 * PowerStatsEnergyConsumer is an energy consumer that can be represented as
 * EnergyConsumed = SUM_i(E_i) + SUM_j(C_j * T_j)
 * where E_i is the energy of channel i of the EnergyMeter and
 * where C_j is the coefficient (in mW) of state j and T_j is the total time (in ms) in state j
 *
 * Factory functions are provided to create PowerStatsEnergyConsumer of three varieties
 * 1. MeterAndEntityConsumer - number of channels is > 0, and there is at least one C_j != 0
 * 2. MeterConsumer - number of channels is > 0, and all C_j = 0
 * 3. EntityConsumer - number of channels is 0, and there is at least one C_j != 0
 */
class PowerStatsEnergyConsumer : public PowerStats::IEnergyConsumer {
  public:
    static sp<PowerStatsEnergyConsumer> createMeterConsumer(std::shared_ptr<PowerStats> p,
                                                            EnergyConsumerType type,
                                                            std::string name,
                                                            std::set<std::string> channelNames);
    static sp<PowerStatsEnergyConsumer> createEntityConsumer(
            std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
            std::string powerEntityName, std::map<std::string, int32_t> stateCoeffs);

    static sp<PowerStatsEnergyConsumer> createMeterAndEntityConsumer(
            std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
            std::set<std::string> channelNames, std::string powerEntityName,
            std::map<std::string, int32_t> stateCoeffs);

    static sp<PowerStatsEnergyConsumer> createMeterAndAttrConsumer(
            std::shared_ptr<PowerStats> p, EnergyConsumerType type, std::string name,
            std::set<std::string> channelNames, std::unordered_map<int32_t, std::string> paths,
            std::map<std::string, int32_t> stateCoeffs);

    std::pair<EnergyConsumerType, std::string> getInfo() override { return {kType, kName}; }

    std::optional<EnergyConsumerResult> getEnergyConsumed() override;

    std::string getConsumerName() override;
  private:
    PowerStatsEnergyConsumer(std::shared_ptr<PowerStats> p, EnergyConsumerType type,
                             std::string name, bool attr = false);
    bool addEnergyMeter(std::set<std::string> channelNames);
    bool addPowerEntity(std::string powerEntityName, std::map<std::string, int32_t> stateCoeffs);
    bool addAttribution(std::unordered_map<int32_t, std::string> paths,
                        std::map<std::string, int32_t> stateCoeffs);

    const EnergyConsumerType kType;
    const std::string kName;
    std::shared_ptr<PowerStats> mPowerStats;
    std::vector<int32_t> mChannelIds;
    int32_t mPowerEntityId;
    bool mWithAttribution;
    std::unordered_map<int32_t, std::string> mAttrInfoPath;
    PowerStatsEnergyAttribution mEnergyAttribution;
    // Snapshot of each uid's energy, uid_time_in_state and total energy from power meter
    // mUidTimeInStateSS: key = uid, val = {uid_time_in_state}
    // mUidEnergySS:      key = uid, val = {uid's energy(UWs)}
    // mTotalEnergySS:    total energy from power meter
    std::unordered_map<int32_t, std::vector<long>> mUidTimeInStateSS;
    std::unordered_map<int32_t, int64_t> mUidEnergySS;
    int64_t mTotalEnergySS = 0;
    std::map<int32_t, int32_t> mCoefficients;  // key = stateId, val = coefficients (mW)
};

}  // namespace stats
}  // namespace power
}  // namespace hardware
}  // namespace android
}  // namespace aidl