aboutsummaryrefslogtreecommitdiff
path: root/daemon/UserSpaceSource.cpp
blob: f58f828e6e2ebafee9ae2d8c31d0400253f8472e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/**
 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2 as
 * published by the Free Software Foundation.
 */

#define __STDC_FORMAT_MACROS

#include "UserSpaceSource.h"

#include <inttypes.h>
#include <sys/prctl.h>
#include <unistd.h>

#include "Child.h"
#include "DriverSource.h"
#include "Logging.h"
#include "SessionData.h"

extern Child *child;

UserSpaceSource::UserSpaceSource(sem_t *senderSem) : mBuffer(0, FRAME_BLOCK_COUNTER, gSessionData->mTotalBufferSize*1024*1024, senderSem) {
}

UserSpaceSource::~UserSpaceSource() {
}

bool UserSpaceSource::prepare() {
	return true;
}

void UserSpaceSource::run() {
	prctl(PR_SET_NAME, (unsigned long)&"gatord-counters", 0, 0, 0);

	for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
		gSessionData->usDrivers[i]->start();
	}

	int64_t monotonicStarted = 0;
	while (monotonicStarted <= 0 && gSessionData->mSessionIsActive) {
		usleep(10);

		if (gSessionData->perf.isSetup()) {
			monotonicStarted = gSessionData->mMonotonicStarted;
		} else {
			if (DriverSource::readInt64Driver("/dev/gator/started", &monotonicStarted) == -1) {
				logg->logError("Error reading gator driver start time");
				handleException();
			}
			gSessionData->mMonotonicStarted = monotonicStarted;
		}
	}

	uint64_t nextTime = 0;
	while (gSessionData->mSessionIsActive) {
		const uint64_t currTime = getTime() - monotonicStarted;
		// Sample ten times a second ignoring gSessionData->mSampleRate
		nextTime += NS_PER_S/10;//gSessionData->mSampleRate;
		if (nextTime < currTime) {
			logg->logMessage("Too slow, currTime: %" PRIi64 " nextTime: %" PRIi64, currTime, nextTime);
			nextTime = currTime;
		}

		if (mBuffer.eventHeader(currTime)) {
			for (int i = 0; i < ARRAY_LENGTH(gSessionData->usDrivers); ++i) {
				gSessionData->usDrivers[i]->read(&mBuffer);
			}
			// Only check after writing all counters so that time and corresponding counters appear in the same frame
			mBuffer.check(currTime);
		}

		if (gSessionData->mOneShot && gSessionData->mSessionIsActive && (mBuffer.bytesAvailable() <= 0)) {
			logg->logMessage("One shot (counters)");
			child->endSession();
		}

		usleep((nextTime - currTime)/NS_PER_US);
	}

	mBuffer.setDone();
}

void UserSpaceSource::interrupt() {
	// Do nothing
}

bool UserSpaceSource::isDone() {
	return mBuffer.isDone();
}

void UserSpaceSource::write(Sender *sender) {
	if (!mBuffer.isDone()) {
		mBuffer.write(sender);
	}
}