1 /*
2  * Copyright (C) 2019 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 _PERFSTATS_BUFFER_H_
18 #define _PERFSTATS_BUFFER_H_
19 
20 #include <dirent.h>
21 #include <inttypes.h>
22 #include <pthread.h>
23 #include <time.h>
24 
25 #include <chrono>
26 #include <iomanip>
27 #include <list>
28 #include <memory>
29 #include <mutex>
30 #include <queue>
31 #include <regex>
32 #include <sstream>
33 #include <string>
34 #include <thread>
35 #include <unordered_map>
36 #include <vector>
37 
38 #include <android-base/file.h>
39 #include <android-base/logging.h>
40 #include <android-base/parseint.h>
41 #include <utils/RefBase.h>
42 
43 namespace android {
44 namespace pixel {
45 namespace perfstatsd {
46 
47 constexpr auto operator""_KiB(unsigned long long const num) {
48     return num * 1024;
49 }
50 
51 class StatsData {
52   public:
getTime()53     std::chrono::system_clock::time_point getTime() const { return mTime; }
getData()54     std::string getData() const { return mData; }
setTime(std::chrono::system_clock::time_point & time)55     void setTime(std::chrono::system_clock::time_point &time) { mTime = time; }
setData(std::string & data)56     void setData(std::string &data) { mData = data; }
57 
58   private:
59     std::chrono::system_clock::time_point mTime;
60     std::string mData;
61 };
62 
63 class PerfstatsBuffer {
64   public:
size()65     size_t size() { return mBufferSize; }
count()66     size_t count() { return mStorage.size(); }
67 
setSize(size_t size)68     void setSize(size_t size) { mBufferSize = size; }
69     void emplace(StatsData &&data);
70     const std::queue<StatsData> &dump(void);
71 
72   private:
73     size_t mBufferSize = 0U;
74     std::queue<StatsData> mStorage;
75 };
76 
77 struct StatsdataCompare {
78     // sort time in ascending order
operatorStatsdataCompare79     bool operator()(const StatsData &a, const StatsData &b) const {
80         return a.getTime() > b.getTime();
81     }
82 };
83 
84 }  // namespace perfstatsd
85 }  // namespace pixel
86 }  // namespace android
87 
88 #endif /*  _PERFSTATS_BUFFER_H_ */
89