• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #ifndef LPERF_EVENTS_H
16 #define LPERF_EVENTS_H
17 
18 #include <functional>
19 #include <string>
20 #include <vector>
21 #include <linux/perf_event.h>
22 #include <poll.h>
23 
24 #include "lperf_event_record.h"
25 
26 namespace OHOS {
27 namespace HiviewDFX {
28 class LperfEvents {
29 public:
30     LperfEvents();
31     ~LperfEvents();
32 
33     int PrepareRecord();
34     int StartRecord();
35     bool StopRecord();
36 
37     void SetTid(const std::vector<int>& tids);
38     void SetTimeOut(int timeOut);
39     void SetSampleFrequency(unsigned int frequency);
40 
41     using ProcessRecordCB = std::function<void(LperfRecordSample& record)>;
42     void SetRecordCallBack(ProcessRecordCB recordCallBack);
43     void Clear();
44 
45 private:
46     struct MmapFd {
47         int fd = 0;
48         perf_event_mmap_page* mmapPage = nullptr;
49         uint8_t *buf = nullptr;
50         size_t bufSize = 0;
51         size_t dataSize = 0;
52 
53         perf_event_header header = {};
54     };
55 
56     bool PrepareFdEvents();
57     bool AddRecordThreads();
58     bool GetHeaderFromMmap(MmapFd& mmap);
59     void GetRecordFieldFromMmap(MmapFd& mmap, void* dest, size_t destSize, size_t pos, size_t size);
60     void GetRecordFromMmap(MmapFd& mmap);
61     void ReadRecordsFromMmaps();
62     bool PerfEventsEnable(bool enable);
63     bool RecordLoop();
64 
65     MmapFd lperfMmap_ = {};
66     ProcessRecordCB recordCallBack_;
67 
68     int lperfFd_ = -1;
69     int timeOut_ = 0;
70     unsigned int pageSize_ = 4096;
71     unsigned int mmapPages_ = 1024;
72     unsigned int sampleFreq_ = 0;
73     std::vector<int> tids_;
74     std::vector<struct pollfd> pollFds_;
75 };
76 } // namespace HiviewDFX
77 } // namespace OHOS
78 #endif // LPERF_EVENTS_H