2 * Copyright (C) ARM Limited 2010-2015. 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 #define __STDC_FORMAT_MACROS
11 #include "UserSpaceSource.h"
14 #include <sys/prctl.h>
18 #include "DriverSource.h"
20 #include "SessionData.h"
24 UserSpaceSource::UserSpaceSource(sem_t *senderSem) : mBuffer(0, FRAME_BLOCK_COUNTER, gSessionData->mTotalBufferSize*1024*1024, senderSem) {
27 UserSpaceSource::~UserSpaceSource() {
30 bool UserSpaceSource::prepare() {
34 void UserSpaceSource::run() {
35 prctl(PR_SET_NAME, (unsigned long)&"gatord-counters", 0, 0, 0);
37 for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
38 gSessionData->usDrivers[i]->start();
41 int64_t monotonicStarted = 0;
42 while (monotonicStarted <= 0 && gSessionData->mSessionIsActive) {
45 if (gSessionData->perf.isSetup()) {
46 monotonicStarted = gSessionData->mMonotonicStarted;
48 if (DriverSource::readInt64Driver("/dev/gator/started", &monotonicStarted) == -1) {
49 logg->logError("Error reading gator driver start time");
52 gSessionData->mMonotonicStarted = monotonicStarted;
56 uint64_t nextTime = 0;
57 while (gSessionData->mSessionIsActive) {
58 const uint64_t currTime = getTime() - monotonicStarted;
59 // Sample ten times a second ignoring gSessionData->mSampleRate
60 nextTime += NS_PER_S/10;//gSessionData->mSampleRate;
61 if (nextTime < currTime) {
62 logg->logMessage("Too slow, currTime: %" PRIi64 " nextTime: %" PRIi64, currTime, nextTime);
66 if (mBuffer.eventHeader(currTime)) {
67 for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
68 gSessionData->usDrivers[i]->read(&mBuffer);
70 // Only check after writing all counters so that time and corresponding counters appear in the same frame
71 mBuffer.check(currTime);
74 if (gSessionData->mOneShot && gSessionData->mSessionIsActive && (mBuffer.bytesAvailable() <= 0)) {
75 logg->logMessage("One shot (counters)");
79 usleep((nextTime - currTime)/NS_PER_US);
85 void UserSpaceSource::interrupt() {
89 bool UserSpaceSource::isDone() {
90 return mBuffer.isDone();
93 void UserSpaceSource::write(Sender *sender) {
94 if (!mBuffer.isDone()) {
95 mBuffer.write(sender);