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 _STATSTYPE_H_
18 #define _STATSTYPE_H_
19 
20 #include <perfstats_buffer.h>
21 
22 namespace android {
23 namespace pixel {
24 namespace perfstatsd {
25 
26 class StatsType : public RefBase {
27   public:
28     virtual void refresh() = 0;
29     virtual void setOptions(const std::string &, const std::string &) = 0;
dump(std::priority_queue<StatsData,std::vector<StatsData>,StatsdataCompare> * queue)30     void dump(std::priority_queue<StatsData, std::vector<StatsData>, StatsdataCompare> *queue) {
31         std::unique_lock<std::mutex> mlock(mMutex);
32         std::queue<StatsData> buffer = mBuffer.dump();
33         while (!buffer.empty()) {
34             queue->push(buffer.front());
35             buffer.pop();
36         }
37     }
bufferSize()38     size_t bufferSize() { return mBuffer.size(); }
setBufferSize(size_t size)39     void setBufferSize(size_t size) { mBuffer.setSize(size); }
bufferCount()40     size_t bufferCount() { return mBuffer.count(); }
41 
42   protected:
append(StatsData && data)43     void append(StatsData &&data) {
44         std::unique_lock<std::mutex> mlock(mMutex);
45         mBuffer.emplace(std::forward<StatsData>(data));
46     }
append(std::chrono::system_clock::time_point & time,std::string & content)47     void append(std::chrono::system_clock::time_point &time, std::string &content) {
48         StatsData data;
49         data.setTime(time);
50         data.setData(content);
51         append(std::move(data));
52     }
append(std::string & content)53     void append(std::string &content) {
54         std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
55         append(now, content);
56     }
57 
58   private:
59     PerfstatsBuffer mBuffer;
60     std::mutex mMutex;
61 };
62 
63 }  // namespace perfstatsd
64 }  // namespace pixel
65 }  // namespace android
66 
67 #endif /* _STATSTYPE_H_ */
68