summaryrefslogtreecommitdiff
path: root/power/stats/1.0/default/PowerStats.h
blob: d67fb3508fe613d8753ee1e7db0af9cc2c41d600 (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
102
103
104
105
106
107
108
109
110
111
112
/*
 * Copyright (C) 2018 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.
 */

#ifndef ANDROID_HARDWARE_POWERSTATS_V1_0_POWERSTATS_H
#define ANDROID_HARDWARE_POWERSTATS_V1_0_POWERSTATS_H

#include <android/hardware/power/stats/1.0/IPowerStats.h>
#include <fmq/MessageQueue.h>
#include <hidl/MQDescriptor.h>
#include <hidl/Status.h>
#include <unordered_map>

namespace android {
namespace hardware {
namespace power {
namespace stats {
namespace V1_0 {
namespace implementation {

using ::android::hardware::hidl_vec;
using ::android::hardware::kSynchronizedReadWrite;
using ::android::hardware::Return;
using ::android::hardware::Void;
using ::android::hardware::power::stats::V1_0::EnergyData;
using ::android::hardware::power::stats::V1_0::PowerEntityInfo;
using ::android::hardware::power::stats::V1_0::PowerEntityStateInfo;
using ::android::hardware::power::stats::V1_0::PowerEntityStateResidencyData;
using ::android::hardware::power::stats::V1_0::PowerEntityStateResidencyResult;
using ::android::hardware::power::stats::V1_0::PowerEntityStateSpace;
using ::android::hardware::power::stats::V1_0::PowerEntityType;
using ::android::hardware::power::stats::V1_0::RailInfo;
using ::android::hardware::power::stats::V1_0::Status;

typedef MessageQueue<EnergyData, kSynchronizedReadWrite> MessageQueueSync;
struct RailData {
    std::string devicePath;
    uint32_t index;
    std::string subsysName;
    uint32_t samplingRate;
};

struct OnDeviceMmt {
    std::mutex mLock;
    bool hwEnabled;
    std::vector<std::string> devicePaths;
    std::map<std::string, RailData> railsInfo;
    std::vector<EnergyData> reading;
    std::unique_ptr<MessageQueueSync> fmqSynchronized;
};

class IStateResidencyDataProvider {
   public:
    virtual ~IStateResidencyDataProvider() = default;
    virtual bool getResults(
            std::unordered_map<uint32_t, PowerEntityStateResidencyResult>& results) = 0;
    virtual std::vector<PowerEntityStateSpace> getStateSpaces() = 0;
};

struct PowerStats : public IPowerStats {
   public:
    PowerStats();
    uint32_t addPowerEntity(const std::string& name, PowerEntityType type);
    void addStateResidencyDataProvider(std::shared_ptr<IStateResidencyDataProvider> p);
    // Methods from ::android::hardware::power::stats::V1_0::IPowerStats follow.
    Return<void> getRailInfo(getRailInfo_cb _hidl_cb) override;
    Return<void> getEnergyData(const hidl_vec<uint32_t>& railIndices,
                               getEnergyData_cb _hidl_cb) override;
    Return<void> streamEnergyData(uint32_t timeMs, uint32_t samplingRate,
                                  streamEnergyData_cb _hidl_cb) override;
    Return<void> getPowerEntityInfo(getPowerEntityInfo_cb _hidl_cb) override;
    Return<void> getPowerEntityStateInfo(const hidl_vec<uint32_t>& powerEntityIds,
                                         getPowerEntityStateInfo_cb _hidl_cb) override;
    Return<void> getPowerEntityStateResidencyData(
        const hidl_vec<uint32_t>& powerEntityIds,
        getPowerEntityStateResidencyData_cb _hidl_cb) override;

    // Methods from ::android::hidl::base::V1_0::IBase follow.
    Return<void> debug(const hidl_handle& fd, const hidl_vec<hidl_string>& args) override;

   private:
    OnDeviceMmt mPm;
    void findIioPowerMonitorNodes();
    size_t parsePowerRails();
    int parseIioEnergyNode(std::string devName);
    Status parseIioEnergyNodes();
    std::vector<PowerEntityInfo> mPowerEntityInfos;
    std::unordered_map<uint32_t, PowerEntityStateSpace> mPowerEntityStateSpaces;
    std::unordered_map<uint32_t, std::shared_ptr<IStateResidencyDataProvider>>
            mStateResidencyDataProviders;
};

}  // namespace implementation
}  // namespace V1_0
}  // namespace stats
}  // namespace power
}  // namespace hardware
}  // namespace android

#endif  // ANDROID_HARDWARE_POWERSTATS_V1_0_POWERSTATS_H