gator: Version 5.21.1
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / UserSpaceSource.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2015. 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 #define __STDC_FORMAT_MACROS
10
11 #include "UserSpaceSource.h"
12
13 #include <inttypes.h>
14 #include <sys/prctl.h>
15 #include <unistd.h>
16
17 #include "Child.h"
18 #include "DriverSource.h"
19 #include "Logging.h"
20 #include "SessionData.h"
21
22 extern Child *child;
23
24 UserSpaceSource::UserSpaceSource(sem_t *senderSem) : mBuffer(0, FRAME_BLOCK_COUNTER, gSessionData->mTotalBufferSize*1024*1024, senderSem) {
25 }
26
27 UserSpaceSource::~UserSpaceSource() {
28 }
29
30 bool UserSpaceSource::prepare() {
31         return true;
32 }
33
34 void UserSpaceSource::run() {
35         prctl(PR_SET_NAME, (unsigned long)&"gatord-counters", 0, 0, 0);
36
37         for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
38                 gSessionData->usDrivers[i]->start();
39         }
40
41         int64_t monotonicStarted = 0;
42         while (monotonicStarted <= 0 && gSessionData->mSessionIsActive) {
43                 usleep(10);
44
45                 if (gSessionData->perf.isSetup()) {
46                         monotonicStarted = gSessionData->mMonotonicStarted;
47                 } else {
48                         if (DriverSource::readInt64Driver("/dev/gator/started", &monotonicStarted) == -1) {
49                                 logg->logError("Error reading gator driver start time");
50                                 handleException();
51                         }
52                         gSessionData->mMonotonicStarted = monotonicStarted;
53                 }
54         }
55
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);
63                         nextTime = currTime;
64                 }
65
66                 if (mBuffer.eventHeader(currTime)) {
67                         for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
68                                 gSessionData->usDrivers[i]->read(&mBuffer);
69                         }
70                         // Only check after writing all counters so that time and corresponding counters appear in the same frame
71                         mBuffer.check(currTime);
72                 }
73
74                 if (gSessionData->mOneShot && gSessionData->mSessionIsActive && (mBuffer.bytesAvailable() <= 0)) {
75                         logg->logMessage("One shot (counters)");
76                         child->endSession();
77                 }
78
79                 usleep((nextTime - currTime)/NS_PER_US);
80         }
81
82         mBuffer.setDone();
83 }
84
85 void UserSpaceSource::interrupt() {
86         // Do nothing
87 }
88
89 bool UserSpaceSource::isDone() {
90         return mBuffer.isDone();
91 }
92
93 void UserSpaceSource::write(Sender *sender) {
94         if (!mBuffer.isDone()) {
95                 mBuffer.write(sender);
96         }
97 }