gator: Version 5.18
[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_S ((uint64_t)1000000000)
20 #define NS_PER_US 1000
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         gSessionData->hwmon.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                 struct timespec ts;
52 #ifndef CLOCK_MONOTONIC_RAW
53                 // Android doesn't have this defined but it was added in Linux 2.6.28
54 #define CLOCK_MONOTONIC_RAW 4
55 #endif
56                 if (clock_gettime(CLOCK_MONOTONIC_RAW, &ts) != 0) {
57                         logg->logError(__FILE__, __LINE__, "Failed to get uptime");
58                         handleException();
59                 }
60                 const uint64_t curr_time = (NS_PER_S*ts.tv_sec + ts.tv_nsec) - monotonic_started;
61                 // Sample ten times a second ignoring gSessionData->mSampleRate
62                 next_time += NS_PER_S/10;//gSessionData->mSampleRate;
63                 if (next_time < curr_time) {
64                         logg->logMessage("Too slow, curr_time: %lli next_time: %lli", curr_time, next_time);
65                         next_time = curr_time;
66                 }
67
68                 if (mBuffer.eventHeader(curr_time)) {
69                         gSessionData->hwmon.read(&mBuffer);
70                         // Only check after writing all counters so that time and corresponding counters appear in the same frame
71                         mBuffer.check(curr_time);
72                 }
73
74                 if (mBuffer.bytesAvailable() <= 0) {
75                         logg->logMessage("One shot (counters)");
76                         child->endSession();
77                 }
78
79                 usleep((next_time - curr_time)/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 }