blob: 7f299b646952c101c511d59f4985d0bc4fcf6775 [file] [log] [blame]
Jon Medhurst15ce78d2014-04-10 09:02:02 +01001/**
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
Jon Medhurst96b56152014-10-30 18:01:15 +00009// Define to get format macros from inttypes.h
Jon Medhurst15ce78d2014-04-10 09:02:02 +010010#define __STDC_FORMAT_MACROS
11
12#include "DriverSource.h"
13
14#include <fcntl.h>
15#include <inttypes.h>
Jon Medhurste31266f2014-08-04 15:47:44 +010016#include <sys/prctl.h>
Jon Medhurst15ce78d2014-04-10 09:02:02 +010017#include <unistd.h>
18
Jon Medhurste31266f2014-08-04 15:47:44 +010019#include "Buffer.h"
Jon Medhurst15ce78d2014-04-10 09:02:02 +010020#include "Child.h"
Jon Medhurste31266f2014-08-04 15:47:44 +010021#include "DynBuf.h"
Jon Medhurst15ce78d2014-04-10 09:02:02 +010022#include "Fifo.h"
23#include "Logging.h"
Jon Medhurste31266f2014-08-04 15:47:44 +010024#include "Proc.h"
Jon Medhurst15ce78d2014-04-10 09:02:02 +010025#include "Sender.h"
26#include "SessionData.h"
27
28extern Child *child;
29
Jon Medhurste31266f2014-08-04 15:47:44 +010030DriverSource::DriverSource(sem_t *senderSem, sem_t *startProfile) : mBuffer(NULL), mFifo(NULL), mSenderSem(senderSem), mStartProfile(startProfile), mBufferSize(0), mBufferFD(0), mLength(1) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +010031 int driver_version = 0;
32
Jon Medhurste31266f2014-08-04 15:47:44 +010033 mBuffer = new Buffer(0, FRAME_PERF_ATTRS, 4*1024*1024, senderSem);
Jon Medhurst15ce78d2014-04-10 09:02:02 +010034 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
35 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
36 handleException();
37 }
38
39 // Verify the driver version matches the daemon version
40 if (driver_version != PROTOCOL_VERSION) {
41 if ((driver_version > PROTOCOL_DEV) || (PROTOCOL_VERSION > PROTOCOL_DEV)) {
42 // One of the mismatched versions is development version
43 logg->logError(__FILE__, __LINE__,
44 "DEVELOPMENT BUILD MISMATCH: gator driver version \"%d\" is not in sync with gator daemon version \"%d\".\n"
45 ">> The following must be synchronized from engineering repository:\n"
46 ">> * gator driver\n"
47 ">> * gator daemon\n"
48 ">> * Streamline", driver_version, PROTOCOL_VERSION);
49 handleException();
50 } else {
51 // Release version mismatch
Jon Medhurste31266f2014-08-04 15:47:44 +010052 logg->logError(__FILE__, __LINE__,
Jon Medhurst15ce78d2014-04-10 09:02:02 +010053 "gator driver version \"%d\" is different than gator daemon version \"%d\".\n"
54 ">> Please upgrade the driver and daemon to the latest versions.", driver_version, PROTOCOL_VERSION);
55 handleException();
56 }
57 }
58
59 int enable = -1;
60 if (readIntDriver("/dev/gator/enable", &enable) != 0 || enable != 0) {
61 logg->logError(__FILE__, __LINE__, "Driver already enabled, possibly a session is already in progress.");
62 handleException();
63 }
64
65 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
66 if (gSessionData->mCores == 0) {
67 gSessionData->mCores = 1;
68 }
69
70 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
71 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
72 handleException();
73 }
74}
75
76DriverSource::~DriverSource() {
77 delete mFifo;
78
79 // Write zero for safety, as a zero should have already been written
80 writeDriver("/dev/gator/enable", "0");
81
82 // Calls event_buffer_release in the driver
83 if (mBufferFD) {
84 close(mBufferFD);
85 }
86}
87
88bool DriverSource::prepare() {
89 // Create user-space buffers, add 5 to the size to account for the 1-byte type and 4-byte length
90 logg->logMessage("Created %d MB collector buffer with a %d-byte ragged end", gSessionData->mTotalBufferSize, mBufferSize);
91 mFifo = new Fifo(mBufferSize + 5, gSessionData->mTotalBufferSize*1024*1024, mSenderSem);
92
93 return true;
94}
95
Jon Medhurste31266f2014-08-04 15:47:44 +010096void DriverSource::bootstrapThread() {
Jon Medhurst96b56152014-10-30 18:01:15 +000097 prctl(PR_SET_NAME, (unsigned long)&"gatord-proc", 0, 0, 0);
Jon Medhurste31266f2014-08-04 15:47:44 +010098
99 DynBuf printb;
100 DynBuf b1;
101 DynBuf b2;
Jon Medhurst96b56152014-10-30 18:01:15 +0000102 const uint64_t currTime = getTime();
Jon Medhurste31266f2014-08-04 15:47:44 +0100103
Jon Medhurst96b56152014-10-30 18:01:15 +0000104 if (!readProcComms(currTime, mBuffer, &printb, &b1, &b2)) {
105 logg->logError(__FILE__, __LINE__, "readProcComms failed");
Jon Medhurste31266f2014-08-04 15:47:44 +0100106 handleException();
107 }
108
Jon Medhurst96b56152014-10-30 18:01:15 +0000109 mBuffer->commit(currTime);
Jon Medhurste31266f2014-08-04 15:47:44 +0100110 mBuffer->setDone();
111}
112
113void *DriverSource::bootstrapThreadStatic(void *arg) {
114 static_cast<DriverSource *>(arg)->bootstrapThread();
115 return NULL;
116}
117
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100118void DriverSource::run() {
119 // Get the initial pointer to the collect buffer
120 char *collectBuffer = mFifo->start();
121 int bytesCollected = 0;
122
123 logg->logMessage("********** Profiling started **********");
124
125 // Set the maximum backtrace depth
126 if (writeReadDriver("/dev/gator/backtrace_depth", &gSessionData->mBacktraceDepth)) {
127 logg->logError(__FILE__, __LINE__, "Unable to set the driver backtrace depth");
128 handleException();
129 }
130
131 // open the buffer which calls userspace_buffer_open() in the driver
Jon Medhurst96b56152014-10-30 18:01:15 +0000132 mBufferFD = open("/dev/gator/buffer", O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100133 if (mBufferFD < 0) {
134 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.");
135 handleException();
136 }
137
138 // set the tick rate of the profiling timer
139 if (writeReadDriver("/dev/gator/tick", &gSessionData->mSampleRate) != 0) {
140 logg->logError(__FILE__, __LINE__, "Unable to set the driver tick");
141 handleException();
142 }
143
144 // notify the kernel of the response type
145 int response_type = gSessionData->mLocalCapture ? 0 : RESPONSE_APC_DATA;
146 if (writeDriver("/dev/gator/response_type", response_type)) {
147 logg->logError(__FILE__, __LINE__, "Unable to write the response type");
148 handleException();
149 }
150
151 // Set the live rate
152 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
153 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
154 handleException();
155 }
156
157 logg->logMessage("Start the driver");
158
159 // This command makes the driver start profiling by calling gator_op_start() in the driver
160 if (writeDriver("/dev/gator/enable", "1") != 0) {
161 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.");
162 handleException();
163 }
164
165 lseek(mBufferFD, 0, SEEK_SET);
166
167 sem_post(mStartProfile);
168
Jon Medhurste31266f2014-08-04 15:47:44 +0100169 pthread_t bootstrapThreadID;
170 if (pthread_create(&bootstrapThreadID, NULL, bootstrapThreadStatic, this) != 0) {
171 logg->logError(__FILE__, __LINE__, "Unable to start the gator_bootstrap thread");
172 handleException();
173 }
174
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100175 // Collect Data
176 do {
177 // This command will stall until data is received from the driver
178 // Calls event_buffer_read in the driver
179 errno = 0;
180 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
181
182 // If read() returned due to an interrupt signal, re-read to obtain the last bit of collected data
183 if (bytesCollected == -1 && errno == EINTR) {
184 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
185 }
186
187 // return the total bytes written
188 logg->logMessage("Driver read of %d bytes", bytesCollected);
189
190 // In one shot mode, stop collection once all the buffers are filled
191 if (gSessionData->mOneShot && gSessionData->mSessionIsActive) {
192 if (bytesCollected == -1 || mFifo->willFill(bytesCollected)) {
193 logg->logMessage("One shot");
194 child->endSession();
195 }
196 }
197 collectBuffer = mFifo->write(bytesCollected);
198 } while (bytesCollected > 0);
199
200 logg->logMessage("Exit collect data loop");
Jon Medhurste31266f2014-08-04 15:47:44 +0100201
202 pthread_join(bootstrapThreadID, NULL);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100203}
204
205void DriverSource::interrupt() {
206 // This command should cause the read() function in collect() to return and stop the driver from profiling
207 if (writeDriver("/dev/gator/enable", "0") != 0) {
208 logg->logMessage("Stopping kernel failed");
209 }
210}
211
212bool DriverSource::isDone() {
Jon Medhurste31266f2014-08-04 15:47:44 +0100213 return mLength <= 0 && (mBuffer == NULL || mBuffer->isDone());
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100214}
215
216void DriverSource::write(Sender *sender) {
217 char *data = mFifo->read(&mLength);
218 if (data != NULL) {
219 sender->writeData(data, mLength, RESPONSE_APC_DATA);
220 mFifo->release();
Jon Medhurste31266f2014-08-04 15:47:44 +0100221 // Assume the summary packet is in the first block received from the driver
222 gSessionData->mSentSummary = true;
223 }
224 if (mBuffer != NULL && !mBuffer->isDone()) {
225 mBuffer->write(sender);
226 if (mBuffer->isDone()) {
227 Buffer *buf = mBuffer;
228 mBuffer = NULL;
229 delete buf;
230 }
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100231 }
232}
233
234int DriverSource::readIntDriver(const char *fullpath, int *value) {
235 char data[40]; // Sufficiently large to hold any integer
Jon Medhurst96b56152014-10-30 18:01:15 +0000236 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100237 if (fd < 0) {
238 return -1;
239 }
240
241 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
242 close(fd);
243 if (bytes < 0) {
244 return -1;
245 }
246 data[bytes] = '\0';
247
248 char *endptr;
249 errno = 0;
250 *value = strtol(data, &endptr, 10);
251 if (errno != 0 || *endptr != '\n') {
252 logg->logMessage("Invalid value in file %s", fullpath);
253 return -1;
254 }
255
256 return 0;
257}
258
259int DriverSource::readInt64Driver(const char *fullpath, int64_t *value) {
260 char data[40]; // Sufficiently large to hold any integer
Jon Medhurst96b56152014-10-30 18:01:15 +0000261 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100262 if (fd < 0) {
263 return -1;
264 }
265
266 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
267 close(fd);
268 if (bytes < 0) {
269 return -1;
270 }
271 data[bytes] = '\0';
272
273 char *endptr;
274 errno = 0;
275 *value = strtoll(data, &endptr, 10);
Jon Medhurste31266f2014-08-04 15:47:44 +0100276 if (errno != 0 || (*endptr != '\n' && *endptr != '\0')) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100277 logg->logMessage("Invalid value in file %s", fullpath);
278 return -1;
279 }
280
281 return 0;
282}
283
284int DriverSource::writeDriver(const char *fullpath, const char *data) {
Jon Medhurst96b56152014-10-30 18:01:15 +0000285 int fd = open(fullpath, O_WRONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100286 if (fd < 0) {
287 return -1;
288 }
289 if (::write(fd, data, strlen(data)) < 0) {
290 close(fd);
291 logg->logMessage("Opened but could not write to %s", fullpath);
292 return -1;
293 }
294 close(fd);
295 return 0;
296}
297
298int DriverSource::writeDriver(const char *path, int value) {
299 char data[40]; // Sufficiently large to hold any integer
300 snprintf(data, sizeof(data), "%d", value);
301 return writeDriver(path, data);
302}
303
304int DriverSource::writeDriver(const char *path, int64_t value) {
305 char data[40]; // Sufficiently large to hold any integer
306 snprintf(data, sizeof(data), "%" PRIi64, value);
307 return writeDriver(path, data);
308}
309
310int DriverSource::writeReadDriver(const char *path, int *value) {
311 if (writeDriver(path, *value) || readIntDriver(path, value)) {
312 return -1;
313 }
314 return 0;
315}
316
317int DriverSource::writeReadDriver(const char *path, int64_t *value) {
318 if (writeDriver(path, *value) || readInt64Driver(path, value)) {
319 return -1;
320 }
321 return 0;
322}