2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
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.
9 #include "PerfSource.h"
18 #include "PerfDriver.h"
20 #include "SessionData.h"
22 #define MS_PER_US 1000000
26 static bool sendTracepointFormat(Buffer *const buffer, const char *const name, DynBuf *const printb, DynBuf *const b) {
27 if (!printb->printf(EVENTS_PATH "/%s/format", name)) {
28 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
31 if (!b->read(printb->getBuf())) {
32 logg->logMessage("%s(%s:%i): DynBuf::read failed", __FUNCTION__, __FILE__, __LINE__);
35 buffer->format(b->getLength(), b->getBuf());
40 PerfSource::PerfSource(sem_t *senderSem, sem_t *startProfile) : mSummary(0, FRAME_SUMMARY, 1024, senderSem), mBuffer(0, FRAME_PERF_ATTRS, 4*1024*1024, senderSem), mCountersBuf(), mCountersGroup(&mCountersBuf), mMonitor(), mUEvent(), mSenderSem(senderSem), mStartProfile(startProfile), mInterruptFd(-1), mIsDone(false) {
41 long l = sysconf(_SC_PAGE_SIZE);
43 logg->logError(__FILE__, __LINE__, "Unable to obtain the page size");
46 gSessionData->mPageSize = static_cast<int>(l);
48 l = sysconf(_SC_NPROCESSORS_CONF);
50 logg->logError(__FILE__, __LINE__, "Unable to obtain the number of cores");
53 gSessionData->mCores = static_cast<int>(l);
56 PerfSource::~PerfSource() {
59 struct PrepareParallelArgs {
64 void *prepareParallel(void *arg) {
65 const PrepareParallelArgs *const args = (PrepareParallelArgs *)arg;
66 args->pg->prepareCPU(args->cpu);
70 bool PerfSource::prepare() {
75 long long schedSwitchId;
77 // Reread cpuinfo since cores may have changed since startup
78 gSessionData->readCpuInfo();
83 || !mMonitor.add(mUEvent.getFd())
85 || (schedSwitchId = PerfDriver::getTracepointId(SCHED_SWITCH, &printb)) < 0
86 || !sendTracepointFormat(&mBuffer, SCHED_SWITCH, &printb, &b1)
88 // Only want RAW but not IP on sched_switch and don't want TID on SAMPLE_ID
89 || !mCountersGroup.add(&mBuffer, 100/**/, PERF_TYPE_TRACEPOINT, schedSwitchId, 1, PERF_SAMPLE_RAW, PERF_GROUP_MMAP | PERF_GROUP_COMM | PERF_GROUP_TASK | PERF_GROUP_SAMPLE_ID_ALL | PERF_GROUP_PER_CPU)
91 // Only want TID and IP but not RAW on timer
92 || (gSessionData->mSampleRate > 0 && !gSessionData->mIsEBS && !mCountersGroup.add(&mBuffer, 99/**/, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK, 1000000000UL / gSessionData->mSampleRate, PERF_SAMPLE_TID | PERF_SAMPLE_IP, PERF_GROUP_PER_CPU))
94 || !gSessionData->perf.enable(&mCountersGroup, &mBuffer)
96 logg->logMessage("%s(%s:%i): perf setup failed, are you running Linux 3.4 or later?", __FUNCTION__, __FILE__, __LINE__);
100 if (!gSessionData->perf.summary(&mSummary)) {
101 logg->logMessage("%s(%s:%i): PerfDriver::summary failed", __FUNCTION__, __FILE__, __LINE__);
106 // Run prepareCPU in parallel as perf_event_open can take more than 1 sec in some cases
107 pthread_t threads[NR_CPUS];
108 PrepareParallelArgs args[NR_CPUS];
109 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
110 args[cpu].pg = &mCountersGroup;
112 if (pthread_create(&threads[cpu], NULL, prepareParallel, &args[cpu]) != 0) {
113 logg->logMessage("%s(%s:%i): pthread_create failed", __FUNCTION__, __FILE__, __LINE__);
117 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
118 if (pthread_join(threads[cpu], NULL) != 0) {
119 logg->logMessage("%s(%s:%i): pthread_join failed", __FUNCTION__, __FILE__, __LINE__);
126 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
127 numEvents += mCountersGroup.onlineCPU(cpu, false, &mBuffer, &mMonitor);
129 if (numEvents <= 0) {
130 logg->logMessage("%s(%s:%i): PerfGroup::onlineCPU failed on all cores", __FUNCTION__, __FILE__, __LINE__);
134 // Start events before reading proc to avoid race conditions
135 if (!mCountersGroup.start()) {
136 logg->logMessage("%s(%s:%i): PerfGroup::start failed", __FUNCTION__, __FILE__, __LINE__);
140 if (!readProc(&mBuffer, true, &printb, &b1, &b2, &b3)) {
141 logg->logMessage("%s(%s:%i): readProc failed", __FUNCTION__, __FILE__, __LINE__);
150 static const char CPU_DEVPATH[] = "/devices/system/cpu/cpu";
152 void PerfSource::run() {
155 if (pipe(pipefd) != 0) {
156 logg->logError(__FILE__, __LINE__, "pipe failed");
159 mInterruptFd = pipefd[1];
161 if (!mMonitor.add(pipefd[0])) {
162 logg->logError(__FILE__, __LINE__, "Monitor::add failed");
167 if (gSessionData->mLiveRate > 0) {
168 timeout = gSessionData->mLiveRate/MS_PER_US;
171 sem_post(mStartProfile);
173 while (gSessionData->mSessionIsActive) {
174 // +1 for uevents, +1 for pipe
175 struct epoll_event events[NR_CPUS + 2];
176 int ready = mMonitor.wait(events, ARRAY_LENGTH(events), timeout);
178 logg->logError(__FILE__, __LINE__, "Monitor::wait failed");
182 for (int i = 0; i < ready; ++i) {
183 if (events[i].data.fd == mUEvent.getFd()) {
184 if (!handleUEvent()) {
185 logg->logError(__FILE__, __LINE__, "PerfSource::handleUEvent failed");
192 // send a notification that data is ready
193 sem_post(mSenderSem);
195 // In one shot mode, stop collection once all the buffers are filled
196 // Assume timeout == 0 in this case
197 if (gSessionData->mOneShot && gSessionData->mSessionIsActive) {
198 logg->logMessage("%s(%s:%i): One shot", __FUNCTION__, __FILE__, __LINE__);
203 mCountersGroup.stop();
207 // send a notification that data is ready
208 sem_post(mSenderSem);
215 bool PerfSource::handleUEvent() {
217 if (!mUEvent.read(&result)) {
218 logg->logMessage("%s(%s:%i): UEvent::Read failed", __FUNCTION__, __FILE__, __LINE__);
222 if (strcmp(result.mSubsystem, "cpu") == 0) {
223 if (strncmp(result.mDevPath, CPU_DEVPATH, sizeof(CPU_DEVPATH) - 1) != 0) {
224 logg->logMessage("%s(%s:%i): Unexpected cpu DEVPATH format", __FUNCTION__, __FILE__, __LINE__);
229 int cpu = strtol(result.mDevPath + sizeof(CPU_DEVPATH) - 1, &endptr, 10);
230 if (errno != 0 || *endptr != '\0') {
231 logg->logMessage("%s(%s:%i): strtol failed", __FUNCTION__, __FILE__, __LINE__);
234 if (strcmp(result.mAction, "online") == 0) {
235 // Only call onlineCPU if prepareCPU succeeded
236 const bool result = mCountersGroup.prepareCPU(cpu) &&
237 mCountersGroup.onlineCPU(cpu, true, &mBuffer, &mMonitor);
240 } else if (strcmp(result.mAction, "offline") == 0) {
241 return mCountersGroup.offlineCPU(cpu);
248 void PerfSource::interrupt() {
249 if (mInterruptFd >= 0) {
251 // Write to the pipe to wake the monitor which will cause mSessionIsActive to be reread
252 if (::write(mInterruptFd, &c, sizeof(c)) != sizeof(c)) {
253 logg->logError(__FILE__, __LINE__, "write failed");
259 bool PerfSource::isDone () {
260 return mBuffer.isDone() && mIsDone && mCountersBuf.isEmpty();
263 void PerfSource::write (Sender *sender) {
264 if (!mSummary.isDone()) {
265 mSummary.write(sender);
266 gSessionData->mSentSummary = true;
268 if (!mBuffer.isDone()) {
269 mBuffer.write(sender);
271 if (!mCountersBuf.send(sender)) {
272 logg->logError(__FILE__, __LINE__, "PerfBuffer::send failed");