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"
15 #include <sys/prctl.h>
16 #include <sys/resource.h>
17 #include <sys/syscall.h>
18 #include <sys/types.h>
24 #include "PerfDriver.h"
26 #include "SessionData.h"
28 #ifndef SCHED_RESET_ON_FORK
29 #define SCHED_RESET_ON_FORK 0x40000000
34 static bool sendTracepointFormat(const uint64_t currTime, Buffer *const buffer, const char *const name, DynBuf *const printb, DynBuf *const b) {
35 if (!printb->printf(EVENTS_PATH "/%s/format", name)) {
36 logg->logMessage("%s(%s:%i): DynBuf::printf failed", __FUNCTION__, __FILE__, __LINE__);
39 if (!b->read(printb->getBuf())) {
40 logg->logMessage("%s(%s:%i): DynBuf::read failed", __FUNCTION__, __FILE__, __LINE__);
43 buffer->format(currTime, b->getLength(), b->getBuf());
48 static void *syncFunc(void *arg)
51 int64_t nextTime = gSessionData->mMonotonicStarted;
55 prctl(PR_SET_NAME, (unsigned long)&"gatord-sync", 0, 0, 0);
57 // Mask all signals so that this thread will not be woken up
60 if (sigfillset(&set) != 0) {
61 logg->logError(__FILE__, __LINE__, "sigfillset failed");
64 if ((err = pthread_sigmask(SIG_SETMASK, &set, NULL)) != 0) {
65 logg->logError(__FILE__, __LINE__, "pthread_sigmask failed");
71 if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) {
72 logg->logError(__FILE__, __LINE__, "clock_gettime failed");
75 const int64_t currTime = ts.tv_sec * NS_PER_S + ts.tv_nsec;
77 // Wake up once a second
80 // Always sleep more than 1 ms, hopefully things will line up better next time
81 const int64_t sleepTime = max(nextTime - currTime, (int64_t)(NS_PER_MS + 1));
82 ts.tv_sec = sleepTime/NS_PER_S;
83 ts.tv_nsec = sleepTime % NS_PER_S;
85 err = nanosleep(&ts, NULL);
87 fprintf(stderr, "clock_nanosleep failed: %s\n", strerror(err));
95 static long getMaxCoreNum() {
96 DIR *dir = opendir("/sys/devices/system/cpu");
98 logg->logError(__FILE__, __LINE__, "Unable to determine the number of cores on the target, opendir failed");
102 long maxCoreNum = -1;
103 struct dirent *dirent;
104 while ((dirent = readdir(dir)) != NULL) {
105 if (strncmp(dirent->d_name, "cpu", 3) == 0) {
108 long coreNum = strtol(dirent->d_name + 3, &endptr, 10);
109 if ((errno == 0) && (*endptr == '\0') && (coreNum >= maxCoreNum)) {
110 maxCoreNum = coreNum + 1;
116 if (maxCoreNum < 1) {
117 logg->logError(__FILE__, __LINE__, "Unable to determine the number of cores on the target, no cpu# directories found");
121 if (maxCoreNum >= NR_CPUS) {
122 logg->logError(__FILE__, __LINE__, "Too many cores on the target, please increase NR_CPUS in Config.h");
129 PerfSource::PerfSource(sem_t *senderSem, sem_t *startProfile) : mSummary(0, FRAME_SUMMARY, 1024, senderSem), mBuffer(0, FRAME_PERF_ATTRS, 1024*1024, senderSem), mCountersBuf(), mCountersGroup(&mCountersBuf), mIdleGroup(&mCountersBuf), mMonitor(), mUEvent(), mSenderSem(senderSem), mStartProfile(startProfile), mInterruptFd(-1), mIsDone(false) {
130 long l = sysconf(_SC_PAGE_SIZE);
132 logg->logError(__FILE__, __LINE__, "Unable to obtain the page size");
135 gSessionData->mPageSize = static_cast<int>(l);
136 gSessionData->mCores = static_cast<int>(getMaxCoreNum());
139 PerfSource::~PerfSource() {
142 bool PerfSource::prepare() {
145 long long schedSwitchId;
148 const uint64_t currTime = getTime();
150 // Reread cpuinfo since cores may have changed since startup
151 gSessionData->readCpuInfo();
156 || !mMonitor.add(mUEvent.getFd())
158 || (schedSwitchId = PerfDriver::getTracepointId(SCHED_SWITCH, &printb)) < 0
159 || !sendTracepointFormat(currTime, &mBuffer, SCHED_SWITCH, &printb, &b1)
161 || (cpuIdleId = PerfDriver::getTracepointId(CPU_IDLE, &printb)) < 0
162 || !sendTracepointFormat(currTime, &mBuffer, CPU_IDLE, &printb, &b1)
164 // Only want RAW but not IP on sched_switch and don't want TID on SAMPLE_ID
165 || !mCountersGroup.add(currTime, &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)
166 || !mIdleGroup.add(currTime, &mBuffer, 101/**/, PERF_TYPE_TRACEPOINT, cpuIdleId, 1, PERF_SAMPLE_RAW, PERF_GROUP_PER_CPU)
168 // Only want TID and IP but not RAW on timer
169 || (gSessionData->mSampleRate > 0 && !gSessionData->mIsEBS && !mCountersGroup.add(currTime, &mBuffer, 102/**/, PERF_TYPE_SOFTWARE, PERF_COUNT_SW_CPU_CLOCK, 1000000000UL / gSessionData->mSampleRate, PERF_SAMPLE_TID | PERF_SAMPLE_IP, PERF_GROUP_PER_CPU))
171 || !gSessionData->perf.enable(currTime, &mCountersGroup, &mBuffer)
173 logg->logMessage("%s(%s:%i): perf setup failed, are you running Linux 3.4 or later?", __FUNCTION__, __FILE__, __LINE__);
177 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
178 const int result = mCountersGroup.prepareCPU(cpu, &mMonitor);
179 if ((result != PG_SUCCESS) && (result != PG_CPU_OFFLINE)) {
180 logg->logError(__FILE__, __LINE__, "PerfGroup::prepareCPU on mCountersGroup failed");
184 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
185 const int result = mIdleGroup.prepareCPU(cpu, &mMonitor);
186 if ((result != PG_SUCCESS) && (result != PG_CPU_OFFLINE)) {
187 logg->logError(__FILE__, __LINE__, "PerfGroup::prepareCPU on mIdleGroup failed");
193 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
194 numEvents += mCountersGroup.onlineCPU(currTime, cpu, false, &mBuffer);
196 for (int cpu = 0; cpu < gSessionData->mCores; ++cpu) {
197 numEvents += mIdleGroup.onlineCPU(currTime, cpu, false, &mBuffer);
199 if (numEvents <= 0) {
200 logg->logMessage("%s(%s:%i): PerfGroup::onlineCPU failed on all cores", __FUNCTION__, __FILE__, __LINE__);
204 // Send the summary right before the start so that the monotonic delta is close to the start time
205 if (!gSessionData->perf.summary(&mSummary)) {
206 logg->logError(__FILE__, __LINE__, "PerfDriver::summary failed", __FUNCTION__, __FILE__, __LINE__);
210 // Start the timer thread to used to sync perf and monotonic raw times
211 pthread_t syncThread;
212 if (pthread_create(&syncThread, NULL, syncFunc, NULL)) {
213 logg->logError(__FILE__, __LINE__, "pthread_create failed", __FUNCTION__, __FILE__, __LINE__);
216 struct sched_param param;
217 param.sched_priority = sched_get_priority_max(SCHED_FIFO);
218 if (pthread_setschedparam(syncThread, SCHED_FIFO | SCHED_RESET_ON_FORK, ¶m) != 0) {
219 logg->logError(__FILE__, __LINE__, "pthread_setschedparam failed");
223 mBuffer.commit(currTime);
228 struct ProcThreadArgs {
234 void *procFunc(void *arg) {
237 const ProcThreadArgs *const args = (ProcThreadArgs *)arg;
239 prctl(PR_SET_NAME, (unsigned long)&"gatord-proc", 0, 0, 0);
241 // Gator runs at a high priority, reset the priority to the default
242 if (setpriority(PRIO_PROCESS, syscall(__NR_gettid), 0) == -1) {
243 logg->logError(__FILE__, __LINE__, "setpriority failed");
247 if (!readProcMaps(args->mCurrTime, args->mBuffer, &printb, &b)) {
248 logg->logError(__FILE__, __LINE__, "readProcMaps failed");
251 args->mBuffer->commit(args->mCurrTime);
253 if (!readKallsyms(args->mCurrTime, args->mBuffer, &args->mIsDone)) {
254 logg->logError(__FILE__, __LINE__, "readKallsyms failed");
257 args->mBuffer->commit(args->mCurrTime);
262 static const char CPU_DEVPATH[] = "/devices/system/cpu/cpu";
264 void PerfSource::run() {
266 pthread_t procThread;
267 ProcThreadArgs procThreadArgs;
274 const uint64_t currTime = getTime();
276 // Start events before reading proc to avoid race conditions
277 if (!mCountersGroup.start() || !mIdleGroup.start()) {
278 logg->logError(__FILE__, __LINE__, "PerfGroup::start failed", __FUNCTION__, __FILE__, __LINE__);
282 if (!readProcComms(currTime, &mBuffer, &printb, &b1, &b2)) {
283 logg->logError(__FILE__, __LINE__, "readProcComms failed");
286 mBuffer.commit(currTime);
288 // Postpone reading kallsyms as on android adb gets too backed up and data is lost
289 procThreadArgs.mBuffer = &mBuffer;
290 procThreadArgs.mCurrTime = currTime;
291 procThreadArgs.mIsDone = false;
292 if (pthread_create(&procThread, NULL, procFunc, &procThreadArgs)) {
293 logg->logError(__FILE__, __LINE__, "pthread_create failed", __FUNCTION__, __FILE__, __LINE__);
298 if (pipe_cloexec(pipefd) != 0) {
299 logg->logError(__FILE__, __LINE__, "pipe failed");
302 mInterruptFd = pipefd[1];
304 if (!mMonitor.add(pipefd[0])) {
305 logg->logError(__FILE__, __LINE__, "Monitor::add failed");
310 if (gSessionData->mLiveRate > 0) {
311 timeout = gSessionData->mLiveRate/NS_PER_MS;
314 sem_post(mStartProfile);
316 while (gSessionData->mSessionIsActive) {
317 // +1 for uevents, +1 for pipe
318 struct epoll_event events[NR_CPUS + 2];
319 int ready = mMonitor.wait(events, ARRAY_LENGTH(events), timeout);
321 logg->logError(__FILE__, __LINE__, "Monitor::wait failed");
324 const uint64_t currTime = getTime();
326 for (int i = 0; i < ready; ++i) {
327 if (events[i].data.fd == mUEvent.getFd()) {
328 if (!handleUEvent(currTime)) {
329 logg->logError(__FILE__, __LINE__, "PerfSource::handleUEvent failed");
336 // send a notification that data is ready
337 sem_post(mSenderSem);
339 // In one shot mode, stop collection once all the buffers are filled
340 // Assume timeout == 0 in this case
341 if (gSessionData->mOneShot && gSessionData->mSessionIsActive) {
342 logg->logMessage("%s(%s:%i): One shot", __FUNCTION__, __FILE__, __LINE__);
347 procThreadArgs.mIsDone = true;
348 pthread_join(procThread, NULL);
350 mCountersGroup.stop();
354 // send a notification that data is ready
355 sem_post(mSenderSem);
362 bool PerfSource::handleUEvent(const uint64_t currTime) {
364 if (!mUEvent.read(&result)) {
365 logg->logMessage("%s(%s:%i): UEvent::Read failed", __FUNCTION__, __FILE__, __LINE__);
369 if (strcmp(result.mSubsystem, "cpu") == 0) {
370 if (strncmp(result.mDevPath, CPU_DEVPATH, sizeof(CPU_DEVPATH) - 1) != 0) {
371 logg->logMessage("%s(%s:%i): Unexpected cpu DEVPATH format", __FUNCTION__, __FILE__, __LINE__);
376 int cpu = strtol(result.mDevPath + sizeof(CPU_DEVPATH) - 1, &endptr, 10);
377 if (errno != 0 || *endptr != '\0') {
378 logg->logMessage("%s(%s:%i): strtol failed", __FUNCTION__, __FILE__, __LINE__);
382 if (cpu >= gSessionData->mCores) {
383 logg->logError(__FILE__, __LINE__, "Only %i cores are expected but core %i reports %s", gSessionData->mCores, cpu, result.mAction);
387 if (strcmp(result.mAction, "online") == 0) {
388 mBuffer.onlineCPU(currTime, currTime - gSessionData->mMonotonicStarted, cpu);
389 // Only call onlineCPU if prepareCPU succeeded
391 int err = mCountersGroup.prepareCPU(cpu, &mMonitor);
392 if (err == PG_CPU_OFFLINE) {
394 } else if (err == PG_SUCCESS) {
395 if (mCountersGroup.onlineCPU(currTime, cpu, true, &mBuffer)) {
396 err = mIdleGroup.prepareCPU(cpu, &mMonitor);
397 if (err == PG_CPU_OFFLINE) {
399 } else if (err == PG_SUCCESS) {
400 if (mIdleGroup.onlineCPU(currTime, cpu, true, &mBuffer)) {
406 mBuffer.commit(currTime);
408 gSessionData->readCpuInfo();
409 gSessionData->perf.coreName(currTime, &mSummary, cpu);
410 mSummary.commit(currTime);
412 } else if (strcmp(result.mAction, "offline") == 0) {
413 const bool result = mCountersGroup.offlineCPU(cpu) && mIdleGroup.offlineCPU(cpu);
414 mBuffer.offlineCPU(currTime, currTime - gSessionData->mMonotonicStarted, cpu);
422 void PerfSource::interrupt() {
423 if (mInterruptFd >= 0) {
425 // Write to the pipe to wake the monitor which will cause mSessionIsActive to be reread
426 if (::write(mInterruptFd, &c, sizeof(c)) != sizeof(c)) {
427 logg->logError(__FILE__, __LINE__, "write failed");
433 bool PerfSource::isDone () {
434 return mBuffer.isDone() && mIsDone && mCountersBuf.isEmpty();
437 void PerfSource::write (Sender *sender) {
438 if (!mSummary.isDone()) {
439 mSummary.write(sender);
440 gSessionData->mSentSummary = true;
442 if (!mBuffer.isDone()) {
443 mBuffer.write(sender);
445 if (!mCountersBuf.send(sender)) {
446 logg->logError(__FILE__, __LINE__, "PerfBuffer::send failed");