• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 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 #ifndef CPP_EVS_MANAGER_1_1_STATS_STATSCOLLECTOR_H_
18 #define CPP_EVS_MANAGER_1_1_STATS_STATSCOLLECTOR_H_
19 
20 #include "CameraUsageStats.h"
21 #include "IStatsCollector.h"
22 #include "LooperWrapper.h"
23 #include "VirtualCamera.h"
24 
25 #include <android-base/chrono_utils.h>
26 #include <android-base/logging.h>
27 #include <android-base/result.h>
28 #include <android/hardware/automotive/evs/1.1/types.h>
29 #include <utils/Mutex.h>
30 
31 #include <deque>
32 #include <thread>  // NOLINT
33 #include <unordered_map>
34 #include <vector>
35 
36 namespace android::automotive::evs::V1_1::implementation {
37 
38 enum CollectionEvent {
39     INIT = 0,
40     PERIODIC,
41     CUSTOM_START,
42     CUSTOM_END,
43     TERMINATED,
44 
45     LAST_EVENT,
46 };
47 
48 struct CollectionRecord {
49     // Latest statistics collection
50     CameraUsageStatsRecord latest = {};
51 
52     // History of collected statistics records
53     std::deque<CameraUsageStatsRecord> history;
54 };
55 
56 struct CollectionInfo {
57     // Collection interval between two subsequent collections
58     std::chrono::nanoseconds interval = 0ns;
59 
60     // The maximum number of records this collection stores
61     size_t maxCacheSize = 0;
62 
63     // Time when the latest collection was done
64     nsecs_t lastCollectionTime = 0;
65 
66     // Collected statistics records per instances
67     std::unordered_map<std::string, CollectionRecord> records;
68 };
69 
70 // Statistic collector for camera usage statistics.
71 // Statistics are not collected until |startCollection|.
72 class StatsCollector : public IStatsCollector, public MessageHandler {
73 public:
StatsCollector()74     StatsCollector() :
75           mLooper(new LooperWrapper()),
76           mCurrentCollectionEvent(CollectionEvent::INIT),
77           mPeriodicCollectionInfo({}),
78           mCustomCollectionInfo({}) {}
79 
80     virtual ~StatsCollector();
81 
82     android::base::Result<void> startCollection() override;
83 
84     // Starts collecting CameraUsageStarts during a given duration at a given
85     // interval.
86     android::base::Result<void> startCustomCollection(std::chrono::nanoseconds interval,
87                                                       std::chrono::nanoseconds duration)
88             EXCLUDES(mMutex) override;
89 
90     // Stops current custom collection and shows the result from the device with
91     // a given unique id.  If this is "all", all results will be returned.
92     android::base::Result<std::string> stopCustomCollection(std::string id = "")
93             EXCLUDES(mMutex) override;
94 
95     // Registers HalCamera object to monitor
96     android::base::Result<void> registerClientToMonitor(const android::sp<HalCamera>& camera)
97             EXCLUDES(mMutex) override;
98 
99     // Unregister HalCamera object.
100     android::base::Result<void> unregisterClientToMonitor(const std::string& id)
101             EXCLUDES(mMutex) override;
102 
103     // Returns a map that contains the latest statistics pulled from
104     // currently active clients.
105     std::unordered_map<std::string, std::string> toString(const char* indent = "") override;
106 
107 private:
108     // Mutex to protect records
109     mutable Mutex mMutex;
110 
111     // Looper to message the collection thread
112     android::sp<LooperWrapper> mLooper;
113 
114     // Background thread to pull stats from the clients
115     std::thread mCollectionThread;
116 
117     // Current state of the monitor
118     CollectionEvent mCurrentCollectionEvent GUARDED_BY(mMutex);
119 
120     // Periodic collection information
121     CollectionInfo mPeriodicCollectionInfo GUARDED_BY(mMutex);
122 
123     // A collection during the custom period the user sets
124     CollectionInfo mCustomCollectionInfo GUARDED_BY(mMutex);
125 
126     // A list of HalCamera objects to monitor
127     std::unordered_map<std::string, android::wp<HalCamera>> mClientsToMonitor GUARDED_BY(mMutex);
128 
129     // Handles the messages from the looper
130     void handleMessage(const Message& message) override;
131 
132     // Handles each CollectionEvent
133     android::base::Result<void> handleCollectionEvent(CollectionEvent event, CollectionInfo* info)
134             EXCLUDES(mMutex);
135 
136     // Pulls the statistics from each active HalCamera objects and generates the
137     // records
138     android::base::Result<void> collectLocked(CollectionInfo* info) REQUIRES(mMutex);
139 
140     // Returns a string corresponding to a given collection event.
141     std::string collectionEventToString(const CollectionEvent& event) const;
142 };
143 
144 }  // namespace android::automotive::evs::V1_1::implementation
145 
146 #endif  // CPP_EVS_MANAGER_1_1_STATS_STATSCOLLECTOR_H_
147