summaryrefslogtreecommitdiff
path: root/tools/gator/daemon/StreamlineSetup.cpp
blob: 303e95994d9c44e1f13df8764074e86dbb67e2fb (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/**
 * Copyright (C) ARM Limited 2011-2016. 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 "StreamlineSetup.h"

#include "Buffer.h"
#include "CapturedXML.h"
#include "ConfigurationXML.h"
#include "Driver.h"
#include "EventsXML.h"
#include "Logging.h"
#include "OlySocket.h"
#include "OlyUtility.h"
#include "Sender.h"
#include "SessionData.h"

static const char TAG_SESSION[] = "session";
static const char TAG_REQUEST[] = "request";
static const char TAG_CONFIGURATIONS[] = "configurations";

static const char ATTR_TYPE[]           = "type";
static const char VALUE_EVENTS[]        = "events";
static const char VALUE_CONFIGURATION[] = "configuration";
static const char VALUE_COUNTERS[]      = "counters";
static const char VALUE_CAPTURED[]      = "captured";
static const char VALUE_DEFAULTS[]      = "defaults";

StreamlineSetup::StreamlineSetup(OlySocket* s) {
	bool ready = false;
	char* data = NULL;
	int type;

	mSocket = s;

	// Receive commands from Streamline (master)
	while (!ready) {
		// receive command over socket
		gSessionData.mWaitingOnCommand = true;
		data = readCommand(&type);

		// parse and handle data
		switch (type) {
			case COMMAND_REQUEST_XML:
				handleRequest(data);
				break;
			case COMMAND_DELIVER_XML:
				handleDeliver(data);
				break;
			case COMMAND_APC_START:
				logg.logMessage("Received apc start request");
				ready = true;
				break;
			case COMMAND_APC_STOP:
				logg.logMessage("Received apc stop request before apc start request");
				exit(0);
				break;
			case COMMAND_DISCONNECT:
				logg.logMessage("Received disconnect command");
				exit(0);
				break;
			case COMMAND_PING:
				logg.logMessage("Received ping command");
				sendData(NULL, 0, RESPONSE_ACK);
				break;
			default:
				logg.logError("Target error: Unknown command type, %d", type);
				handleException();
		}

		free(data);
	}

	if (gSessionData.mCountersError != NULL) {
		logg.logError("%s", gSessionData.mCountersError);
		handleException();
	}
}

StreamlineSetup::~StreamlineSetup() {
}

char* StreamlineSetup::readCommand(int* command) {
	unsigned char header[5];
	char* data;
	int response;

	// receive type and length
	response = mSocket->receiveNBytes((char*)&header, sizeof(header));

	// After receiving a single byte, we are no longer waiting on a command
	gSessionData.mWaitingOnCommand = false;

	if (response < 0) {
		logg.logError("Target error: Unexpected socket disconnect");
		handleException();
	}

	const char type = header[0];
	const int length = (header[1] << 0) | (header[2] << 8) | (header[3] << 16) | (header[4] << 24);

	// add artificial limit
	if ((length < 0) || length > 1024 * 1024) {
		logg.logError("Target error: Invalid length received, %d", length);
		handleException();
	}

	// allocate memory to contain the xml file, size of zero returns a zero size object
	data = (char*)calloc(length + 1, 1);
	if (data == NULL) {
		logg.logError("Unable to allocate memory for xml");
		handleException();
	}

	// receive data
	response = mSocket->receiveNBytes(data, length);
	if (response < 0) {
		logg.logError("Target error: Unexpected socket disconnect");
		handleException();
	}

	// null terminate the data for string parsing
	if (length > 0) {
		data[length] = 0;
	}

	*command = type;
	return data;
}

void StreamlineSetup::handleRequest(char* xml) {
	mxml_node_t *tree, *node;
	const char * attr = NULL;

	tree = mxmlLoadString(NULL, xml, MXML_NO_CALLBACK);
	node = mxmlFindElement(tree, tree, TAG_REQUEST, ATTR_TYPE, NULL, MXML_DESCEND_FIRST);
	if (node) {
		attr = mxmlElementGetAttr(node, ATTR_TYPE);
	}
	if (attr && strcmp(attr, VALUE_EVENTS) == 0) {
		sendEvents();
		logg.logMessage("Sent events xml response");
	} else if (attr && strcmp(attr, VALUE_CONFIGURATION) == 0) {
		sendConfiguration();
		logg.logMessage("Sent configuration xml response");
	} else if (attr && strcmp(attr, VALUE_COUNTERS) == 0) {
		sendCounters();
		logg.logMessage("Sent counters xml response");
	} else if (attr && strcmp(attr, VALUE_CAPTURED) == 0) {
		CapturedXML capturedXML;
		char* capturedText = capturedXML.getXML(false);
		sendData(capturedText, strlen(capturedText), RESPONSE_XML);
		free(capturedText);
		logg.logMessage("Sent captured xml response");
	} else if (attr && strcmp(attr, VALUE_DEFAULTS) == 0) {
		sendDefaults();
		logg.logMessage("Sent default configuration xml response");
	} else {
		char error[] = "Unknown request";
		sendData(error, strlen(error), RESPONSE_NAK);
		logg.logMessage("Received unknown request:\n%s", xml);
	}

	mxmlDelete(tree);
}

void StreamlineSetup::handleDeliver(char* xml) {
	mxml_node_t *tree;

	// Determine xml type
	tree = mxmlLoadString(NULL, xml, MXML_NO_CALLBACK);
	if (mxmlFindElement(tree, tree, TAG_SESSION, NULL, NULL, MXML_DESCEND_FIRST)) {
		// Session XML
		gSessionData.parseSessionXML(xml);
		sendData(NULL, 0, RESPONSE_ACK);
		logg.logMessage("Received session xml");
	} else if (mxmlFindElement(tree, tree, TAG_CONFIGURATIONS, NULL, NULL, MXML_DESCEND_FIRST)) {
		// Configuration XML
		writeConfiguration(xml);
		sendData(NULL, 0, RESPONSE_ACK);
		logg.logMessage("Received configuration xml");
	} else {
		// Unknown XML
		logg.logMessage("Received unknown XML delivery type");
		sendData(NULL, 0, RESPONSE_NAK);
	}

	mxmlDelete(tree);
}

void StreamlineSetup::sendData(const char* data, uint32_t length, char type) {
	unsigned char header[5];
	header[0] = type;
	Buffer::writeLEInt(header + 1, length);
	mSocket->send((char*)&header, sizeof(header));
	mSocket->send(data, length);
}

void StreamlineSetup::sendEvents() {
	EventsXML eventsXML;
	char* string = eventsXML.getXML();
	sendString(string, RESPONSE_XML);
	free(string);
}

void StreamlineSetup::sendConfiguration() {
	ConfigurationXML xml;

	const char* string = xml.getConfigurationXML();
	sendData(string, strlen(string), RESPONSE_XML);
}

void StreamlineSetup::sendDefaults() {
	// Send the config built into the binary
	char* xml = ConfigurationXML::getDefaultConfigurationXml();
	size_t size = strlen(xml);

	// Artificial size restriction
	if (size > 1024*1024) {
		logg.logError("Corrupt default configuration file");
		handleException();
	}

	sendData(xml, size, RESPONSE_XML);
	free(xml);
}

void StreamlineSetup::sendCounters() {
	mxml_node_t *xml;
	mxml_node_t *counters;

	xml = mxmlNewXML("1.0");
	counters = mxmlNewElement(xml, "counters");
	int count = 0;
	for (Driver *driver = Driver::getHead(); driver != NULL; driver = driver->getNext()) {
		count += driver->writeCounters(counters);
	}

	if (count == 0) {
		logg.logError("No counters found, this could be because /dev/gator/events can not be read or because perf is not working correctly");
		handleException();
	}

	mxml_node_t *setup = mxmlNewElement(counters, "setup_warnings");
	mxmlNewText(setup, 0, logg.getSetup());

	if (gSessionData.mSharedData->mClustersAccurate) {
		for (int cluster = 0; cluster < gSessionData.mSharedData->mClusterCount; ++cluster) {
			mxml_node_t *node = mxmlNewElement(counters, "cluster");
			mxmlElementSetAttrf(node, "id", "%i", cluster);
			mxmlElementSetAttr(node, "name", gSessionData.mSharedData->mClusters[cluster]->getPmncName());
		}
		for (int cpu = 0; cpu < gSessionData.mCores; ++cpu) {
			if (gSessionData.mSharedData->mClusterIds[cpu] >= 0) {
				mxml_node_t *node = mxmlNewElement(counters, "cpu");
				mxmlElementSetAttrf(node, "id", "%i", cpu);
				mxmlElementSetAttrf(node, "cluster", "%i", gSessionData.mSharedData->mClusterIds[cpu]);
			}
		}
	}

	char* string = mxmlSaveAllocString(xml, mxmlWhitespaceCB);
	mxmlDelete(xml);

	sendString(string, RESPONSE_XML);
	free(string);
}

void StreamlineSetup::writeConfiguration(char* xml) {
	char path[PATH_MAX];

	ConfigurationXML::getPath(path);

	if (writeToDisk(path, xml) < 0) {
		logg.logError("Error writing %s\nPlease verify write permissions to this path.", path);
		handleException();
	}

	// Re-populate gSessionData with the configuration, as it has now changed
	{ ConfigurationXML configuration; }

	if (gSessionData.mCountersError != NULL) {
		logg.logError("%s", gSessionData.mCountersError);
		handleException();
	}
}