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