aboutsummaryrefslogtreecommitdiff
path: root/daemon/Collector.cpp
blob: c4133475838abf9c61f6bf48ab59afcfa56b2830 (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
/**
 * Copyright (C) ARM Limited 2010-2011. 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.
 */

typedef unsigned long long uint64_t;
#include <fcntl.h>
#include <malloc.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/time.h>
#include "Collector.h"
#include "SessionData.h"
#include "Logging.h"
#include "ConfigurationXML.h"

extern void handleException();

// Driver initialization independent of session settings
Collector::Collector() {
	bufferFD = 0;

	checkVersion();

	readIntDriver("cpu_cores", &gSessionData.mCores);
	if (gSessionData.mCores == 0) {
		gSessionData.mCores = 1;
	}

	if (writeDriver("buffer_size", 256*1024)) {
		logg->logError(__FILE__, __LINE__, "Unable to set the cpu buffer settings");
		handleException();
	}

	readIntDriver("buffer_size", &bufferSize);
	if (bufferSize <= 0) {
		logg->logError(__FILE__, __LINE__, "bufferSize %d is invalid", bufferSize);
		handleException();
	}

	getCoreName();

	// populate performance counter session data
	new ConfigurationXML();
}

Collector::~Collector() {
	// Write zero for safety, as a zero should have already been written
	writeDriver("enable", "0");

	// Calls event_buffer_release in the driver
	if (bufferFD) {
		close(bufferFD);
	}
}

void Collector::enablePerfCounters() {
	for (int i=0; i<MAX_PERFORMANCE_COUNTERS; i++) {
		char base[256];
		char text[256];
		if (!gSessionData.mPerfCounterEnabled[i]) {
			continue;
		}
		snprintf(base, sizeof(base), "events/%s", gSessionData.mPerfCounterType[i]);
		snprintf(text, sizeof(text), "%s/event", base);
		writeDriver(text, gSessionData.mPerfCounterEvent[i]);
		snprintf(text, sizeof(text), "%s/key", base);
		readIntDriver(text, &gSessionData.mPerfCounterKey[i]);
		if (gSessionData.mPerfCounterEBSCapable[i]) {
			snprintf(text, sizeof(text), "%s/count", base);
			if (writeReadDriver(text, &gSessionData.mPerfCounterCount[i]))
				gSessionData.mPerfCounterCount[i] = 0;
			if (gSessionData.mPerfCounterCount[i] > 0)
				logg->logMessage("EBS enabled for %s with a count of %d", gSessionData.mPerfCounterName[i], gSessionData.mPerfCounterCount[i]);
		}
		snprintf(text, sizeof(text), "%s/enabled", base);
		if (writeReadDriver(text, &gSessionData.mPerfCounterEnabled[i])) {
			gSessionData.mPerfCounterEnabled[i] = 0;
		}
	}
}

void Collector::checkVersion() {
	int driver_version = 0;

	if (readIntDriver("version", &driver_version) == -1) {
		logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
		handleException();
	}

	// Verify the driver version matches the daemon version
	if (driver_version != PROTOCOL_VERSION) {
		if ((driver_version > PROTOCOL_DEV) || (PROTOCOL_VERSION > PROTOCOL_DEV)) {
			// One of the mismatched versions is development version
			logg->logError(__FILE__, __LINE__,
				"DEVELOPMENT BUILD MISMATCH: gator driver version \"%d\" is not in sync with gator daemon version \"%d\".\n"
				">> The following must be synchronized from engineering repository:\n"
				">> * gator driver\n"
				">> * gator daemon\n"
				">> * Streamline", driver_version, PROTOCOL_VERSION);
			handleException();
		} else {
			// Release version mismatch
			logg->logError(__FILE__, __LINE__, 
				"gator driver version \"%d\" is different than gator daemon version \"%d\".\n"
				">> Please upgrade the driver and daemon to the latest versions.", driver_version, PROTOCOL_VERSION);
			handleException();
		}
	}
}

void Collector::start() {
	// Set the maximum backtrace depth
	if (writeReadDriver("backtrace_depth", &gSessionData.mBacktraceDepth)) {
		logg->logError(__FILE__, __LINE__, "Unable to set the driver backtrace depth");
		handleException();
	}

	// open the buffer which calls userspace_buffer_open() in the driver
	char* fullpath = resolvePath("buffer");
	bufferFD = open(fullpath, O_RDONLY);
	if (bufferFD < 0) {
		logg->logError(__FILE__, __LINE__, "The gator driver did not set up properly. Please view the linux console or dmesg log for more information on the failure.");
		handleException();
	}

	// set the tick rate of the profiling timer
	if (writeReadDriver("tick", &gSessionData.mSampleRate) != 0) {
		logg->logError(__FILE__, __LINE__, "Unable to set the driver tick");
		handleException();
	}

	// set the frequency of syncing data, i.e. once per second
	int streaming = (int)!gSessionData.mOneShot;
	if (writeReadDriver("streaming", &streaming) != 0) {
		logg->logError(__FILE__, __LINE__, "Unable to set streaming");
		handleException();
	}

	logg->logMessage("Start the driver");

	// This command makes the driver start profiling by calling gator_op_start() in the driver
	if (writeDriver("enable", "1") != 0) {
		logg->logError(__FILE__, __LINE__, "The gator driver did not start properly. Please view the linux console or dmesg log for more information on the failure.");
		handleException();
	}

	lseek(bufferFD, 0, SEEK_SET);
}

// These commands should cause the read() function in collect() to return
void Collector::stop() {
	// This will stop the driver from profiling
	if (writeDriver("enable", "0") != 0) {
		logg->logMessage("Stopping kernel failed");
	}
}

int Collector::collect(char* buffer) {
	// Calls event_buffer_read in the driver
	int bytesRead = read(bufferFD, buffer, bufferSize);

	// If read() returned due to an interrupt signal, re-read to obtain the last bit of collected data
	if (bytesRead == -1 && errno == EINTR) {
		bytesRead = read(bufferFD, buffer, bufferSize);
	}

	logg->logMessage("Driver read of %d bytes", bytesRead);

	return bytesRead;
}

void Collector::getCoreName() {
	char temp[256];
	strncpy(gSessionData.mCoreName, "unknown", sizeof(gSessionData.mCoreName));

	FILE* f = fopen("/proc/cpuinfo", "r");	
	if (f == NULL) {
		logg->logMessage("Error opening /proc/cpuinfo\n"
			"The core name in the captured xml file will be 'unknown'.");
		return;
	}

	while (fgets(temp, sizeof(temp), f)) {
		if (strlen(temp) > 0)
			temp[strlen(temp) - 1] = 0;	// Replace the line feed with a null

		if (strstr(temp, "Hardware") != 0) {
			char* position = strchr(temp, ':');
			if (position == NULL || (unsigned int)(position - temp) + 2 >= strlen(temp)) {
				logg->logMessage("Unknown format of /proc/cpuinfo\n"
					"The core name in the captured xml file will be 'unknown'.");
				return;
			}
			strncpy(gSessionData.mCoreName, (char *)((int)position + 2), sizeof(gSessionData.mCoreName));
			fclose(f);
			return;
		}
	}

	logg->logMessage("Could not determine core name from /proc/cpuinfo\n"
		"The core name in the captured xml file will be 'unknown'.");
	fclose(f);
}

char* Collector::resolvePath(const char* file) {
	static char fullpath[128];
	snprintf(fullpath, sizeof(fullpath), "/dev/gator/%s", file);
	return fullpath;
}

int Collector::readIntDriver(const char* path, int* value) {
	char* fullpath = resolvePath(path);
	FILE* file = fopen(fullpath, "r");
	if (file == NULL) {
		return -1;
	}
	if (fscanf(file, "%u", value) != 1) {
		fclose(file);
		logg->logMessage(__FILE__, __LINE__, "Invalid value in file %s", fullpath);
		return -1;
	}
	fclose(file);
	return 0;
}

int Collector::writeDriver(const char* path, int value) {
	char data[40];
	snprintf(data, sizeof(data), "%d", value);
	return writeDriver(path, data);
}

int Collector::writeDriver(const char* path, const char* data) {
	char* fullpath = resolvePath(path);
	int fd = open(fullpath, O_WRONLY);
	if (fd < 0) {
		return -1;
	}
	if (write(fd, data, strlen(data)) < 0) {
		close(fd);
		logg->logMessage("Opened but could not write to %s", fullpath);
		return -1;
	}
	close(fd);
	return 0;
}

int Collector::writeReadDriver(const char* path, int* value) {
	if (writeDriver(path, *value) || readIntDriver(path, value)) {
		return -1;
	}
	return 0;
}