blob: 2a0239f7c348f3d0649bc89f956b2f82e5aacb86 [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 "PerfGroup.h"
10
11#include <errno.h>
12#include <string.h>
13#include <sys/ioctl.h>
14#include <sys/syscall.h>
15#include <unistd.h>
16
17#include "Buffer.h"
18#include "Logging.h"
19#include "Monitor.h"
20#include "PerfBuffer.h"
21#include "SessionData.h"
22
23#define DEFAULT_PEA_ARGS(pea, additionalSampleType) \
24 pea.size = sizeof(pea); \
25 /* Emit time, read_format below, group leader id, and raw tracepoint info */ \
26 pea.sample_type = (gSessionData->perf.getLegacySupport() \
27 ? PERF_SAMPLE_TID | PERF_SAMPLE_IP | PERF_SAMPLE_TIME | PERF_SAMPLE_READ | PERF_SAMPLE_ID \
28 : PERF_SAMPLE_TIME | PERF_SAMPLE_READ | PERF_SAMPLE_IDENTIFIER ) | additionalSampleType; \
29 /* Emit emit value in group format */ \
30 pea.read_format = PERF_FORMAT_ID | PERF_FORMAT_GROUP; \
31 /* start out disabled */ \
32 pea.disabled = 1; \
33 /* have a sampling interrupt happen when we cross the wakeup_watermark boundary */ \
34 pea.watermark = 1; \
35 /* Be conservative in flush size as only one buffer set is monitored */ \
36 pea.wakeup_watermark = 3 * BUF_SIZE / 4
37
38static int sys_perf_event_open(struct perf_event_attr *const attr, const pid_t pid, const int cpu, const int group_fd, const unsigned long flags) {
39 return syscall(__NR_perf_event_open, attr, pid, cpu, group_fd, flags);
40}
41
42PerfGroup::PerfGroup(PerfBuffer *const pb) : mPb(pb) {
43 memset(&mAttrs, 0, sizeof(mAttrs));
44 memset(&mPerCpu, 0, sizeof(mPerCpu));
45 memset(&mKeys, -1, sizeof(mKeys));
46 memset(&mFds, -1, sizeof(mFds));
47}
48
49PerfGroup::~PerfGroup() {
50 for (int pos = ARRAY_LENGTH(mFds) - 1; pos >= 0; --pos) {
51 if (mFds[pos] >= 0) {
52 close(mFds[pos]);
53 }
54 }
55}
56
57bool PerfGroup::add(Buffer *const buffer, const int key, const __u32 type, const __u64 config, const __u64 sample, const __u64 sampleType, const int flags) {
58 int i;
59 for (i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
60 if (mKeys[i] < 0) {
61 break;
62 }
63 }
64
65 if (i >= ARRAY_LENGTH(mKeys)) {
66 logg->logMessage("%s(%s:%i): Too many counters", __FUNCTION__, __FILE__, __LINE__);
67 return false;
68 }
69
70 DEFAULT_PEA_ARGS(mAttrs[i], sampleType);
71 mAttrs[i].type = type;
72 mAttrs[i].config = config;
73 mAttrs[i].sample_period = sample;
74 // always be on the CPU but only a group leader can be pinned
75 mAttrs[i].pinned = (i == 0 ? 1 : 0);
76 mAttrs[i].mmap = (flags & PERF_GROUP_MMAP ? 1 : 0);
77 mAttrs[i].comm = (flags & PERF_GROUP_COMM ? 1 : 0);
78 mAttrs[i].freq = (flags & PERF_GROUP_FREQ ? 1 : 0);
79 mAttrs[i].task = (flags & PERF_GROUP_TASK ? 1 : 0);
80 mAttrs[i].sample_id_all = (flags & PERF_GROUP_SAMPLE_ID_ALL ? 1 : 0);
81 mPerCpu[i] = (flags & PERF_GROUP_PER_CPU);
82
83 mKeys[i] = key;
84
85 buffer->pea(&mAttrs[i], key);
86
87 return true;
88}
89
90bool PerfGroup::prepareCPU(const int cpu) {
91 logg->logMessage("%s(%s:%i): Onlining cpu %i", __FUNCTION__, __FILE__, __LINE__, cpu);
92
93 for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
94 if (mKeys[i] < 0) {
95 continue;
96 }
97
98 if ((cpu != 0) && !mPerCpu[i]) {
99 continue;
100 }
101
102 const int offset = i * gSessionData->mCores;
103 if (mFds[cpu + offset] >= 0) {
104 logg->logMessage("%s(%s:%i): cpu already online or not correctly cleaned up", __FUNCTION__, __FILE__, __LINE__);
105 return false;
106 }
107
108 logg->logMessage("%s(%s:%i): perf_event_open cpu: %i type: %lli config: %lli sample: %lli sample_type: 0x%llx pinned: %i mmap: %i comm: %i freq: %i task: %i sample_id_all: %i", __FUNCTION__, __FILE__, __LINE__, cpu, (long long)mAttrs[i].type, (long long)mAttrs[i].config, (long long)mAttrs[i].sample_period, (long long)mAttrs[i].sample_type, mAttrs[i].pinned, mAttrs[i].mmap, mAttrs[i].comm, mAttrs[i].freq, mAttrs[i].task, mAttrs[i].sample_id_all);
109 mFds[cpu + offset] = sys_perf_event_open(&mAttrs[i], -1, cpu, i == 0 ? -1 : mFds[cpu], i == 0 ? 0 : PERF_FLAG_FD_OUTPUT);
110 if (mFds[cpu + offset] < 0) {
111 logg->logMessage("%s(%s:%i): failed %s", __FUNCTION__, __FILE__, __LINE__, strerror(errno));
112 continue;
113 }
114
115 if (!mPb->useFd(cpu, mFds[cpu + offset], mFds[cpu])) {
116 logg->logMessage("%s(%s:%i): PerfBuffer::useFd failed", __FUNCTION__, __FILE__, __LINE__);
117 return false;
118 }
119 }
120
121 return true;
122}
123
124int PerfGroup::onlineCPU(const int cpu, const bool start, Buffer *const buffer, Monitor *const monitor) {
125 __u64 ids[ARRAY_LENGTH(mKeys)];
126 int coreKeys[ARRAY_LENGTH(mKeys)];
127 int idCount = 0;
128
129 for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
130 const int fd = mFds[cpu + i * gSessionData->mCores];
131 if (fd < 0) {
132 continue;
133 }
134
135 coreKeys[idCount] = mKeys[i];
136 if (!gSessionData->perf.getLegacySupport() && ioctl(fd, PERF_EVENT_IOC_ID, &ids[idCount]) != 0 &&
137 // Workaround for running 32-bit gatord on 64-bit systems, kernel patch in the works
138 ioctl(fd, (PERF_EVENT_IOC_ID & ~IOCSIZE_MASK) | (8 << _IOC_SIZESHIFT), &ids[idCount]) != 0) {
139 logg->logMessage("%s(%s:%i): ioctl failed", __FUNCTION__, __FILE__, __LINE__);
140 return false;
141 }
142 ++idCount;
143 }
144
145 if (!monitor->add(mFds[cpu])) {
146 logg->logMessage("%s(%s:%i): Monitor::add failed", __FUNCTION__, __FILE__, __LINE__);
147 return false;
148 }
149
150 if (!gSessionData->perf.getLegacySupport()) {
151 buffer->keys(idCount, ids, coreKeys);
152 } else {
153 char buf[1024];
154 ssize_t bytes = read(mFds[cpu], buf, sizeof(buf));
155 if (bytes < 0) {
156 logg->logMessage("read failed");
157 return false;
158 }
159 buffer->keysOld(idCount, coreKeys, bytes, buf);
160 }
161
162 if (start) {
163 for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
164 int offset = i * gSessionData->mCores + cpu;
165 if (mFds[offset] >= 0 && ioctl(mFds[offset], PERF_EVENT_IOC_ENABLE) < 0) {
166 logg->logMessage("%s(%s:%i): ioctl failed", __FUNCTION__, __FILE__, __LINE__);
167 return false;
168 }
169 }
170 }
171
172 return idCount;
173}
174
175bool PerfGroup::offlineCPU(const int cpu) {
176 logg->logMessage("%s(%s:%i): Offlining cpu %i", __FUNCTION__, __FILE__, __LINE__, cpu);
177
178 for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
179 int offset = i * gSessionData->mCores + cpu;
180 if (mFds[offset] >= 0 && ioctl(mFds[offset], PERF_EVENT_IOC_DISABLE) < 0) {
181 logg->logMessage("%s(%s:%i): ioctl failed", __FUNCTION__, __FILE__, __LINE__);
182 return false;
183 }
184 }
185
186 // Mark the buffer so that it will be released next time it's read
187 mPb->discard(cpu);
188
189 for (int i = 0; i < ARRAY_LENGTH(mKeys); ++i) {
190 if (mKeys[i] < 0) {
191 continue;
192 }
193
194 int offset = i * gSessionData->mCores + cpu;
195 if (mFds[offset] >= 0) {
196 close(mFds[offset]);
197 mFds[offset] = -1;
198 }
199 }
200
201 return true;
202}
203
204bool PerfGroup::start() {
205 for (int pos = 0; pos < ARRAY_LENGTH(mFds); ++pos) {
206 if (mFds[pos] >= 0 && ioctl(mFds[pos], PERF_EVENT_IOC_ENABLE) < 0) {
207 logg->logMessage("%s(%s:%i): ioctl failed", __FUNCTION__, __FILE__, __LINE__);
208 goto fail;
209 }
210 }
211
212 return true;
213
214 fail:
215 stop();
216
217 return false;
218}
219
220void PerfGroup::stop() {
221 for (int pos = ARRAY_LENGTH(mFds) - 1; pos >= 0; --pos) {
222 if (mFds[pos] >= 0) {
223 ioctl(mFds[pos], PERF_EVENT_IOC_DISABLE);
224 }
225 }
226}