• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #include <utils/SystemClock.h>
19 
20 #include <cinttypes>
21 #include <mutex>
22 #include <thread>
23 #include <vector>
24 
25 /* Forward declaration to speed-up build and avoid build errors. Clients of this
26  * library force to use C++11 std, when AIDL auto-generated code uses features
27  * from more recent C++ version. */
28 namespace aidl {
29 namespace android {
30 namespace frameworks {
31 namespace stats {
32 
33 class VendorAtom;
34 class IStats;
35 
36 }  // namespace stats
37 }  // namespace frameworks
38 }  // namespace android
39 }  // namespace aidl
40 
41 namespace aidl {
42 namespace android {
43 namespace hardware {
44 namespace vibrator {
45 
46 class StatsBase {
47   public:
48     using VendorAtom = ::aidl::android::frameworks::stats::VendorAtom;
49     using IStats = ::aidl::android::frameworks::stats::IStats;
50 
51     StatsBase(const std::string &instance);
52     ~StatsBase();
53 
54     void debug(int fd);
55 
56   protected:
57     std::vector<int32_t> mWaveformCounts;
58     std::vector<int32_t> mDurationCounts;
59     std::vector<int32_t> mMinLatencies;
60     std::vector<int32_t> mMaxLatencies;
61     std::vector<int32_t> mLatencyTotals;
62     std::vector<int32_t> mLatencyCounts;
63     std::vector<int32_t> mErrorCounts;
64     std::mutex mDataAccess;
65 
66   private:
67     void runReporterThread();
68     void reportVendorAtomAsync(const VendorAtom &atom);
69     void uploadDiagnostics();
70     std::shared_ptr<IStats> waitForStatsService() const;
71     void drainAtomQueue();
72 
73     void uploadPlaycountAtoms();
74     void uploadLatencyAtoms();
75     void uploadErrorAtoms();
76 
77     void clearData(std::vector<int32_t> *data);
78 
79     VendorAtom vibratorPlaycountAtom();
80     VendorAtom vibratorLatencyAtom();
81     VendorAtom vibratorErrorAtom();
82 
83     std::thread mReporterThread;
84     std::vector<VendorAtom> mAtomQueue;
85     std::mutex mAtomQueueAccess;
86     std::condition_variable mAtomQueueUpdated;
87     bool mTerminateReporterThread = false;
88 
89     const std::string kStatsInstanceName;
90 };
91 
92 }  // namespace vibrator
93 }  // namespace hardware
94 }  // namespace android
95 }  // namespace aidl
96