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 #pragma once
18 
19 #include <PowerStats.h>
20 
21 #include <random>
22 
23 namespace aidl {
24 namespace android {
25 namespace hardware {
26 namespace power {
27 namespace stats {
28 
29 class FakeStateResidencyDataProvider : public PowerStats::IStateResidencyDataProvider {
30   public:
FakeStateResidencyDataProvider(const std::string & name,std::vector<State> states)31     FakeStateResidencyDataProvider(const std::string& name, std::vector<State> states)
32         : mName(name), mStates(states) {
33         for (const auto& state : mStates) {
34             StateResidency r;
35             r.id = state.id;
36             r.totalTimeInStateMs = 0;
37             r.totalStateEntryCount = 0;
38             r.lastEntryTimestampMs = 0;
39             mResidencies.push_back(r);
40         }
41     }
42     ~FakeStateResidencyDataProvider() = default;
43 
44     // Methods from PowerStats::IStateResidencyDataProvider
getStateResidencies(std::unordered_map<std::string,std::vector<StateResidency>> * residencies)45     bool getStateResidencies(
46             std::unordered_map<std::string, std::vector<StateResidency>>* residencies) override {
47         for (auto& residency : mResidencies) {
48             mFakeStateResidency.update(&residency);
49         }
50 
51         residencies->emplace(mName, mResidencies);
52         return true;
53     }
54 
getInfo()55     std::unordered_map<std::string, std::vector<State>> getInfo() override {
56         return {{mName, mStates}};
57     }
58 
59   private:
60     class FakeStateResidency {
61       public:
FakeStateResidency()62         FakeStateResidency() : mDistribution(1, 100) {}
update(StateResidency * residency)63         void update(StateResidency* residency) {
64             // generates number in the range 1..100
65             auto randNum = std::bind(mDistribution, mGenerator);
66 
67             residency->totalTimeInStateMs += randNum() * 100;
68             residency->totalStateEntryCount += randNum();
69             residency->lastEntryTimestampMs += randNum() * 100;
70         }
71 
72       private:
73         std::default_random_engine mGenerator;
74         std::uniform_int_distribution<int> mDistribution;
75     };
76 
77     const std::string mName;
78     const std::vector<State> mStates;
79     FakeStateResidency mFakeStateResidency;
80     std::vector<StateResidency> mResidencies;
81 };
82 
83 }  // namespace stats
84 }  // namespace power
85 }  // namespace hardware
86 }  // namespace android
87 }  // namespace aidl