• 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 
35 }  // namespace stats
36 }  // namespace frameworks
37 }  // namespace android
38 }  // namespace aidl
39 
40 namespace aidl {
41 namespace android {
42 namespace hardware {
43 namespace vibrator {
44 
45 class StatsBase {
46   public:
47     using VendorAtom = ::aidl::android::frameworks::stats::VendorAtom;
48 
49     StatsBase(const std::string &instance);
50     ~StatsBase();
51 
52     void debug(int fd);
53 
54   protected:
55     std::vector<int32_t> mWaveformCounts;
56     std::vector<int32_t> mDurationCounts;
57     std::vector<int32_t> mMinLatencies;
58     std::vector<int32_t> mMaxLatencies;
59     std::vector<int32_t> mLatencyTotals;
60     std::vector<int32_t> mLatencyCounts;
61     std::vector<int32_t> mErrorCounts;
62     std::mutex mDataAccess;
63 
64   private:
65     void runReporterThread();
66     void reportVendorAtomAsync(const VendorAtom &atom);
67     void uploadDiagnostics();
68     void waitForStatsService() const;
69     void drainAtomQueue();
70 
71     void uploadPlaycountAtoms();
72     void uploadLatencyAtoms();
73     void uploadErrorAtoms();
74 
75     void clearData(std::vector<int32_t> *data);
76 
77     VendorAtom vibratorPlaycountAtom();
78     VendorAtom vibratorLatencyAtom();
79     VendorAtom vibratorErrorAtom();
80 
81     std::thread mReporterThread;
82     std::vector<VendorAtom> mAtomQueue;
83     std::mutex mAtomQueueAccess;
84     std::condition_variable mAtomQueueUpdated;
85     bool mTerminateReporterThread = false;
86 
87     const std::string kStatsInstanceName;
88 };
89 
90 }  // namespace vibrator
91 }  // namespace hardware
92 }  // namespace android
93 }  // namespace aidl
94