• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #pragma once
2 
3 /*
4  * Copyright (C) 2021 The Android Open Source Project
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #include <aidl/android/hardware/power/WorkDuration.h>
20 
21 #include <chrono>
22 #include <vector>
23 
24 namespace aidl {
25 namespace google {
26 namespace hardware {
27 namespace power {
28 namespace impl {
29 namespace pixel {
30 
31 using ::aidl::android::hardware::power::WorkDuration;
32 
33 struct WorkDurationBatch {
WorkDurationBatchWorkDurationBatch34     WorkDurationBatch(const std::vector<WorkDuration> &workDurations,
35                       std::chrono::nanoseconds targetDuration)
36         : workDurations(workDurations), targetDuration(targetDuration) {}
37     const std::vector<WorkDuration> workDurations;
38     const std::chrono::nanoseconds targetDuration;
39 };
40 
41 struct WorkDurationFeatures {
42     std::chrono::nanoseconds averageDuration;
43     std::chrono::nanoseconds maxDuration;
44     uint32_t numMissedDeadlines;
45     uint32_t numDurations;
46 
47     bool operator==(const WorkDurationFeatures &other) const {
48         return averageDuration == other.averageDuration && maxDuration == other.maxDuration &&
49                numMissedDeadlines == other.numMissedDeadlines && numDurations == other.numDurations;
50     }
51 };
52 
53 class WorkDurationProcessor {
54   public:
55     bool ReportWorkDurations(const std::vector<WorkDuration> &workDurations,
56                              std::chrono::nanoseconds targetDuration);
57 
58     WorkDurationFeatures GetFeatures();
59 
60     // True if ReportWorkDurations has been called since GetFeatures was last called.
61     bool HasWorkDurations();
62 
63   private:
64     // The work durations reported since GetFeatures() was last called.
65     // Ordered from least recent to most recent.
66     std::vector<WorkDurationBatch> mWorkDurationBatches;
67     // Guards reading/writing mWorkDurationBatches.
68     std::mutex mMutex;
69 };
70 
71 }  // namespace pixel
72 }  // namespace impl
73 }  // namespace power
74 }  // namespace hardware
75 }  // namespace google
76 }  // namespace aidl
77