gator: Version 5.19
[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 #define NS_PER_US 1000
20
21 extern Child *child;
22
23 UserSpaceSource::UserSpaceSource(sem_t *senderSem) : mBuffer(0, FRAME_BLOCK_COUNTER, gSessionData->mTotalBufferSize*1024*1024, senderSem) {
24 }
25
26 UserSpaceSource::~UserSpaceSource() {
27 }
28
29 bool UserSpaceSource::prepare() {
30         return true;
31 }
32
33 void UserSpaceSource::run() {
34         prctl(PR_SET_NAME, (unsigned long)&"gatord-counters", 0, 0, 0);
35
36         gSessionData->hwmon.start();
37         gSessionData->fsDriver.start();
38
39         int64_t monotonic_started = 0;
40         while (monotonic_started <= 0) {
41                 usleep(10);
42
43                 if (DriverSource::readInt64Driver("/dev/gator/started", &monotonic_started) == -1) {
44                         logg->logError(__FILE__, __LINE__, "Error reading gator driver start time");
45                         handleException();
46                 }
47         }
48
49         uint64_t next_time = 0;
50         while (gSessionData->mSessionIsActive) {
51                 const uint64_t curr_time = getTime() - monotonic_started;
52                 // Sample ten times a second ignoring gSessionData->mSampleRate
53                 next_time += NS_PER_S/10;//gSessionData->mSampleRate;
54                 if (next_time < curr_time) {
55                         logg->logMessage("Too slow, curr_time: %lli next_time: %lli", curr_time, next_time);
56                         next_time = curr_time;
57                 }
58
59                 if (mBuffer.eventHeader(curr_time)) {
60                         gSessionData->hwmon.read(&mBuffer);
61                         gSessionData->fsDriver.read(&mBuffer);
62                         // Only check after writing all counters so that time and corresponding counters appear in the same frame
63                         mBuffer.check(curr_time);
64                 }
65
66                 if (mBuffer.bytesAvailable() <= 0) {
67                         logg->logMessage("One shot (counters)");
68                         child->endSession();
69                 }
70
71                 usleep((next_time - curr_time)/NS_PER_US);
72         }
73
74         mBuffer.setDone();
75 }
76
77 void UserSpaceSource::interrupt() {
78         // Do nothing
79 }
80
81 bool UserSpaceSource::isDone() {
82         return mBuffer.isDone();
83 }
84
85 void UserSpaceSource::write(Sender *sender) {
86         if (!mBuffer.isDone()) {
87                 mBuffer.write(sender);
88         }
89 }