xref: /aosp_15_r20/system/chre/host/common/include/chre_host/metrics_reporter.h (revision 84e339476a462649f82315436d70fd732297a399)
1 /*
2  * Copyright (C) 2023 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 CHRE_HOST_METRICS_REPORTER_H_
18 #define CHRE_HOST_METRICS_REPORTER_H_
19 
20 #include <aidl/android/frameworks/stats/IStats.h>
21 #include <chre_atoms_log.h>
22 #include <mutex>
23 
24 namespace android::chre {
25 
26 class MetricsReporter {
27  public:
28   MetricsReporter() = default;
29   ~MetricsReporter() = default;
30 
31   MetricsReporter(const MetricsReporter &) = delete;
32   MetricsReporter &operator=(const MetricsReporter &) = delete;
33 
34   /**
35    * Creates and reports CHRE vendor atom and send it to stats_client.
36    *
37    * @param atom the vendor atom to be reported
38    * @return true if the metric was successfully reported, false otherwise.
39    */
40   bool reportMetric(const aidl::android::frameworks::stats::VendorAtom &atom);
41 
42   /**
43    * Reports an AP Wakeup caused by a nanoapp.
44    *
45    * @return whether the operation was successful.
46    */
47   bool logApWakeupOccurred(uint64_t nanoappId);
48 
49   /**
50    * Reports a nanoapp load failed metric.
51    *
52    * @return whether the operation was successful.
53    */
54   bool logNanoappLoadFailed(
55       uint64_t nanoappId,
56       android::chre::Atoms::ChreHalNanoappLoadFailed::Type type,
57       android::chre::Atoms::ChreHalNanoappLoadFailed::Reason reason);
58 
59   /**
60    * Reports a PAL open failed metric.
61    *
62    * @return whether the operation was successful.
63    */
64   bool logPalOpenFailed(
65       android::chre::Atoms::ChrePalOpenFailed::ChrePalType pal,
66       android::chre::Atoms::ChrePalOpenFailed::Type type);
67 
68   /**
69    * Reports a event queue snapshot reported metric.
70    *
71    * @return whether the operation was successful.
72    */
73   bool logEventQueueSnapshotReported(int32_t snapshotChreGetTimeMs,
74                                      int32_t max_event_queue_size,
75                                      int32_t mean_event_queue_size,
76                                      int32_t num_dropped_events);
77 
78   /**
79    * Called when the binder dies for the stats service.
80    */
81   void onBinderDied();
82 
83  private:
84   /**
85    * Connects to the stats service or return nullptr if it cannot connect.
86    * This also adds a binder death handler to the service that will call
87    * onBinderDied on this.
88    *
89    * @return the stats service
90    */
91   std::shared_ptr<aidl::android::frameworks::stats::IStats> getStatsService();
92 
93   std::mutex mStatsServiceMutex;
94   std::shared_ptr<aidl::android::frameworks::stats::IStats> mStatsService =
95       nullptr;
96 };
97 
98 }  // namespace android::chre
99 
100 #endif  // CHRE_HOST_METRICS_REPORTER_H_