aboutsummaryrefslogtreecommitdiff
path: root/daemon/MaliVideoDriver.cpp
blob: 18b413b01a37dd981792a67fb63bae02c11c4e73 (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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/**
 * Copyright (C) ARM Limited 2014. 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.
 */

#include "MaliVideoDriver.h"

#include <unistd.h>

#include "Buffer.h"
#include "Counter.h"
#include "Logging.h"
#include "SessionData.h"

// From instr/src/mve_instr_comm_protocol.h
typedef enum mve_instr_configuration_type {
	MVE_INSTR_RAW         = 1 << 0,
	MVE_INSTR_COUNTERS    = 1 << 1,
	MVE_INSTR_EVENTS      = 1 << 2,
	MVE_INSTR_ACTIVITIES  = 1 << 3,

	// Raw always pushed regardless
	MVE_INSTR_PULL        = 1 << 12,
	// Raw always unpacked regardless
	MVE_INSTR_PACKED_COMM = 1 << 13,
	// Don’t send ACKt response
	MVE_INSTR_NO_AUTO_ACK   = 1 << 14,
} mve_instr_configuration_type_t;

static const char COUNTER[] = "ARM_Mali-V500_cnt";
static const char EVENT[] = "ARM_Mali-V500_evn";
static const char ACTIVITY[] = "ARM_Mali-V500_act";

class MaliVideoCounter {
public:
	MaliVideoCounter(MaliVideoCounter *next, const char *name, const MaliVideoCounterType type, const int id) : mNext(next), mName(name), mType(type), mId(id), mKey(getEventKey()), mEnabled(false) {
	}

	~MaliVideoCounter() {
		delete mName;
	}

	MaliVideoCounter *getNext() const { return mNext; }
	const char *getName() const { return mName; }
	MaliVideoCounterType getType() const { return mType; }
	int getId() const { return mId; }
	int getKey() const { return mKey; }
	bool isEnabled() const { return mEnabled; }
	void setEnabled(const bool enabled) { mEnabled = enabled; }

private:
	MaliVideoCounter *const mNext;
	const char *const mName;
	const MaliVideoCounterType mType;
	// Mali Video id
	const int mId;
	// Streamline key
	const int mKey;
	bool mEnabled;
};

MaliVideoDriver::MaliVideoDriver() : mCounters(NULL), mActivityCount(0) {
}

MaliVideoDriver::~MaliVideoDriver() {
	while (mCounters != NULL) {
		MaliVideoCounter *counter = mCounters;
		mCounters = counter->getNext();
		delete counter;
	}
}

void MaliVideoDriver::setup(mxml_node_t *const xml) {
	// hwmon does not currently work with perf
	if (gSessionData->perf.isSetup()) {
		return;
	}

	mxml_node_t *node = xml;
	while (true) {
		node = mxmlFindElement(node, xml, "event", NULL, NULL, MXML_DESCEND);
		if (node == NULL) {
			break;
		}
		const char *counter = mxmlElementGetAttr(node, "counter");
		if (counter == NULL) {
			// Ignore
		} else if (strncmp(counter, COUNTER, sizeof(COUNTER) - 1) == 0) {
			const int i = strtol(counter + sizeof(COUNTER) - 1, NULL, 10);
			mCounters = new MaliVideoCounter(mCounters, strdup(counter), MVCT_COUNTER, i);
		} else if (strncmp(counter, EVENT, sizeof(EVENT) - 1) == 0) {
			const int i = strtol(counter + sizeof(EVENT) - 1, NULL, 10);
			mCounters = new MaliVideoCounter(mCounters, strdup(counter), MVCT_EVENT, i);
		} else if (strcmp(counter, ACTIVITY) == 0) {
			mCounters = new MaliVideoCounter(mCounters, strdup(ACTIVITY), MVCT_ACTIVITY, 0);
			mActivityCount = 0;
			while (true) {
				char buf[32];
				snprintf(buf, sizeof(buf), "activity%i", mActivityCount + 1);
				if (mxmlElementGetAttr(node, buf) == NULL) {
					break;
				}
				++mActivityCount;
			}
		}
	}
}

MaliVideoCounter *MaliVideoDriver::findCounter(const Counter &counter) const {
	for (MaliVideoCounter *maliVideoCounter = mCounters; maliVideoCounter != NULL; maliVideoCounter = maliVideoCounter->getNext()) {
		if (strcmp(maliVideoCounter->getName(), counter.getType()) == 0) {
			return maliVideoCounter;
		}
	}

	return NULL;
}

bool MaliVideoDriver::claimCounter(const Counter &counter) const {
	return findCounter(counter) != NULL;
}

bool MaliVideoDriver::countersEnabled() const {
	for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
		if (counter->isEnabled()) {
			return true;
		}
	}
	return false;
}

void MaliVideoDriver::resetCounters() {
	for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
		counter->setEnabled(false);
	}
}

void MaliVideoDriver::setupCounter(Counter &counter) {
	MaliVideoCounter *const maliVideoCounter = findCounter(counter);
	if (maliVideoCounter == NULL) {
		counter.setEnabled(false);
		return;
	}
	maliVideoCounter->setEnabled(true);
	counter.setKey(maliVideoCounter->getKey());
}

int MaliVideoDriver::writeCounters(mxml_node_t *root) const {
	if (access("/dev/mv500", F_OK) != 0) {
		return 0;
	}

	int count = 0;
	for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
		mxml_node_t *node = mxmlNewElement(root, "counter");
		mxmlElementSetAttr(node, "name", counter->getName());
		++count;
	}

	return count;
}

void MaliVideoDriver::marshalEnable(const MaliVideoCounterType type, char *const buf, const size_t bufsize, int &pos) {
	// size
	int numEnabled = 0;
	for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
		if (counter->isEnabled() && (counter->getType() == type)) {
			++numEnabled;
		}
	}
	Buffer::packInt(buf, bufsize, pos, numEnabled*sizeof(uint32_t));
	for (MaliVideoCounter * counter = mCounters; counter != NULL; counter = counter->getNext()) {
		if (counter->isEnabled() && (counter->getType() == type)) {
			Buffer::packInt(buf, bufsize, pos, counter->getId());
		}
	}
}

bool MaliVideoDriver::start(const int mveUds) {
	char buf[256];
	int pos = 0;

	// code - MVE_INSTR_STARTUP
	buf[pos++] = 'C';
	buf[pos++] = 'L';
	buf[pos++] = 'N';
	buf[pos++] = 'T';
	// size
	Buffer::packInt(buf, sizeof(buf), pos, sizeof(uint32_t));
	// client_version_number
	Buffer::packInt(buf, sizeof(buf), pos, 1);

	// code - MVE_INSTR_CONFIGURE
	buf[pos++] = 'C';
	buf[pos++] = 'N';
	buf[pos++] = 'F';
	buf[pos++] = 'G';
	// size
	Buffer::packInt(buf, sizeof(buf), pos, 5*sizeof(uint32_t));
	// configuration
	Buffer::packInt(buf, sizeof(buf), pos, MVE_INSTR_COUNTERS | MVE_INSTR_EVENTS | MVE_INSTR_ACTIVITIES | MVE_INSTR_PACKED_COMM);
	// communication_protocol_version
	Buffer::packInt(buf, sizeof(buf), pos, 1);
	// data_protocol_version
	Buffer::packInt(buf, sizeof(buf), pos, 1);
	// sample_rate - convert samples/second to ms/sample
	Buffer::packInt(buf, sizeof(buf), pos, 1000/gSessionData->mSampleRate);
	// live_rate - convert ns/flush to ms/flush
	Buffer::packInt(buf, sizeof(buf), pos, gSessionData->mLiveRate/1000000);

	// code - MVE_INSTR_ENABLE_COUNTERS
	buf[pos++] = 'C';
	buf[pos++] = 'F';
	buf[pos++] = 'G';
	buf[pos++] = 'c';
	marshalEnable(MVCT_COUNTER, buf, sizeof(buf), pos);

	// code - MVE_INSTR_ENABLE_EVENTS
	buf[pos++] = 'C';
	buf[pos++] = 'F';
	buf[pos++] = 'G';
	buf[pos++] = 'e';
	marshalEnable(MVCT_EVENT, buf, sizeof(buf), pos);

	/*
	// code - MVE_INSTR_ENABLE_ACTIVITIES
	buf[pos++] = 'C';
	buf[pos++] = 'F';
	buf[pos++] = 'G';
	buf[pos++] = 'a';
	// size
	Buffer::packInt(buf, sizeof(buf), pos, mActivityCount*sizeof(uint32_t));
	for (int i = 0; i < mActivityCount; ++i) {
		// activity_id
		Buffer::packInt(buf, sizeof(buf), pos, i);
	}
	*/

	int written = 0;
	while (written < pos) {
		size_t bytes = ::write(mveUds, buf + written, pos - written);
		if (bytes <= 0) {
			logg->logMessage("%s(%s:%i): write failed", __FUNCTION__, __FILE__, __LINE__);
			return false;
		}
		written += bytes;
	}

	return true;
}