blob: 5fad583f7bd095661d213d1f3d8f113b3bf63743 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001/**
2 * Copyright (C) ARM Limited 2013-2014. All rights reserved.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 */
8
9#include "PerfBuffer.h"
10
11#include <sys/ioctl.h>
12#include <sys/mman.h>
13
14#include "Buffer.h"
15#include "Logging.h"
16#include "Sender.h"
17#include "SessionData.h"
18
19PerfBuffer::PerfBuffer() {
20 for (int cpu = 0; cpu < ARRAY_LENGTH(mBuf); ++cpu) {
21 mBuf[cpu] = MAP_FAILED;
22 mDiscard[cpu] = false;
23 }
24}
25
26PerfBuffer::~PerfBuffer() {
27 for (int cpu = ARRAY_LENGTH(mBuf) - 1; cpu >= 0; --cpu) {
28 if (mBuf[cpu] != MAP_FAILED) {
29 munmap(mBuf[cpu], gSessionData->mPageSize + BUF_SIZE);
30 }
31 }
32}
33
34bool PerfBuffer::useFd(const int cpu, const int fd, const int groupFd) {
35 if (fd == groupFd) {
36 if (mBuf[cpu] != MAP_FAILED) {
37 logg->logMessage("%s(%s:%i): cpu %i already online or not correctly cleaned up", __FUNCTION__, __FILE__, __LINE__, cpu);
38 return false;
39 }
40
41 // The buffer isn't mapped yet
42 mBuf[cpu] = mmap(NULL, gSessionData->mPageSize + BUF_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
43 if (mBuf[cpu] == MAP_FAILED) {
44 logg->logMessage("%s(%s:%i): mmap failed", __FUNCTION__, __FILE__, __LINE__);
45 return false;
46 }
47
48 // Check the version
49 struct perf_event_mmap_page *pemp = static_cast<struct perf_event_mmap_page *>(mBuf[cpu]);
50 if (pemp->compat_version != 0) {
51 logg->logMessage("%s(%s:%i): Incompatible perf_event_mmap_page compat_version", __FUNCTION__, __FILE__, __LINE__);
52 return false;
53 }
54 } else {
55 if (mBuf[cpu] == MAP_FAILED) {
56 logg->logMessage("%s(%s:%i): cpu already online or not correctly cleaned up", __FUNCTION__, __FILE__, __LINE__);
57 return false;
58 }
59
60 if (ioctl(fd, PERF_EVENT_IOC_SET_OUTPUT, groupFd) < 0) {
61 logg->logMessage("%s(%s:%i): ioctl failed", __FUNCTION__, __FILE__, __LINE__);
62 return false;
63 }
64 }
65
66 return true;
67}
68
69void PerfBuffer::discard(const int cpu) {
70 if (mBuf[cpu] != MAP_FAILED) {
71 mDiscard[cpu] = true;
72 }
73}
74
75bool PerfBuffer::isEmpty() {
76 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
77 if (mBuf[cpu] != MAP_FAILED) {
78 // Take a snapshot of the positions
79 struct perf_event_mmap_page *pemp = static_cast<struct perf_event_mmap_page *>(mBuf[cpu]);
80 const __u64 head = pemp->data_head;
81 const __u64 tail = pemp->data_tail;
82
83 if (head != tail) {
84 return false;
85 }
86 }
87 }
88
89 return true;
90}
91
92bool PerfBuffer::send(Sender *const sender) {
93 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
94 if (mBuf[cpu] == MAP_FAILED) {
95 continue;
96 }
97
98 // Take a snapshot of the positions
99 struct perf_event_mmap_page *pemp = static_cast<struct perf_event_mmap_page *>(mBuf[cpu]);
100 const __u64 head = pemp->data_head;
101 const __u64 tail = pemp->data_tail;
102
103 if (head > tail) {
104 const uint8_t *const b = static_cast<uint8_t *>(mBuf[cpu]) + gSessionData->mPageSize;
105 const int offset = gSessionData->mLocalCapture ? 1 : 0;
106 unsigned char header[7];
107 header[0] = RESPONSE_APC_DATA;
108 Buffer::writeLEInt(header + 1, head - tail + sizeof(header) - 5);
109 // Should use real packing functions
110 header[5] = FRAME_PERF;
111 header[6] = cpu;
112
113 // Write header
114 sender->writeData(reinterpret_cast<const char *>(&header) + offset, sizeof(header) - offset, RESPONSE_APC_DATA);
115
116 // Write data
117 if ((head & ~BUF_MASK) == (tail & ~BUF_MASK)) {
118 // Not wrapped
119 sender->writeData(reinterpret_cast<const char *>(b + (tail & BUF_MASK)), head - tail, RESPONSE_APC_DATA);
120 } else {
121 // Wrapped
122 sender->writeData(reinterpret_cast<const char *>(b + (tail & BUF_MASK)), BUF_SIZE - (tail & BUF_MASK), RESPONSE_APC_DATA);
123 sender->writeData(reinterpret_cast<const char *>(b), head & BUF_MASK, RESPONSE_APC_DATA);
124 }
125
126 // Update tail with the data read
127 pemp->data_tail = head;
128 }
129
130 if (mDiscard[cpu]) {
131 munmap(mBuf[cpu], gSessionData->mPageSize + BUF_SIZE);
132 mBuf[cpu] = MAP_FAILED;
133 mDiscard[cpu] = false;
134 logg->logMessage("%s(%s:%i): Unmaped cpu %i", __FUNCTION__, __FILE__, __LINE__, cpu);
135 }
136 }
137
138 return true;
139}