blob: 34920cee92fb0d8eeeb123f606f4ea1e28357269 [file] [log] [blame]
Jon Medhurst15ce78d2014-04-10 09:02:02 +01001/**
Jon Medhurstb1d07442015-05-08 12:04:18 +01002 * Copyright (C) ARM Limited 2010-2015. All rights reserved.
Jon Medhurst15ce78d2014-04-10 09:02:02 +01003 *
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) {
Jon Medhurstb1d07442015-05-08 12:04:18 +010035 logg->logError("Error reading gator driver version");
Jon Medhurst15ce78d2014-04-10 09:02:02 +010036 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
Jon Medhurstb1d07442015-05-08 12:04:18 +010043 logg->logError(
Jon Medhurst15ce78d2014-04-10 09:02:02 +010044 "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 Medhurstb1d07442015-05-08 12:04:18 +010052 logg->logError(
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) {
Jon Medhurstb1d07442015-05-08 12:04:18 +010061 logg->logError("Driver already enabled, possibly a session is already in progress.");
Jon Medhurst15ce78d2014-04-10 09:02:02 +010062 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) {
Jon Medhurstb1d07442015-05-08 12:04:18 +010071 logg->logError("Unable to read the driver buffer size");
Jon Medhurst15ce78d2014-04-10 09:02:02 +010072 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 Medhurstb1d07442015-05-08 12:04:18 +0100102 // MonotonicStarted may not be not assigned yet
103 const uint64_t currTime = 0;//getTime() - gSessionData->mMonotonicStarted;
Jon Medhurste31266f2014-08-04 15:47:44 +0100104
Jon Medhurst96b56152014-10-30 18:01:15 +0000105 if (!readProcComms(currTime, mBuffer, &printb, &b1, &b2)) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100106 logg->logError("readProcComms failed");
Jon Medhurste31266f2014-08-04 15:47:44 +0100107 handleException();
108 }
109
Jon Medhurst96b56152014-10-30 18:01:15 +0000110 mBuffer->commit(currTime);
Jon Medhurste31266f2014-08-04 15:47:44 +0100111 mBuffer->setDone();
112}
113
114void *DriverSource::bootstrapThreadStatic(void *arg) {
115 static_cast<DriverSource *>(arg)->bootstrapThread();
116 return NULL;
117}
118
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100119void DriverSource::run() {
120 // Get the initial pointer to the collect buffer
121 char *collectBuffer = mFifo->start();
122 int bytesCollected = 0;
123
124 logg->logMessage("********** Profiling started **********");
125
126 // Set the maximum backtrace depth
127 if (writeReadDriver("/dev/gator/backtrace_depth", &gSessionData->mBacktraceDepth)) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100128 logg->logError("Unable to set the driver backtrace depth");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100129 handleException();
130 }
131
132 // open the buffer which calls userspace_buffer_open() in the driver
Jon Medhurst96b56152014-10-30 18:01:15 +0000133 mBufferFD = open("/dev/gator/buffer", O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100134 if (mBufferFD < 0) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100135 logg->logError("The gator driver did not set up properly. Please view the linux console or dmesg log for more information on the failure.");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100136 handleException();
137 }
138
139 // set the tick rate of the profiling timer
140 if (writeReadDriver("/dev/gator/tick", &gSessionData->mSampleRate) != 0) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100141 logg->logError("Unable to set the driver tick");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100142 handleException();
143 }
144
145 // notify the kernel of the response type
146 int response_type = gSessionData->mLocalCapture ? 0 : RESPONSE_APC_DATA;
147 if (writeDriver("/dev/gator/response_type", response_type)) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100148 logg->logError("Unable to write the response type");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100149 handleException();
150 }
151
152 // Set the live rate
153 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100154 logg->logError("Unable to set the driver live rate");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100155 handleException();
156 }
157
158 logg->logMessage("Start the driver");
159
160 // This command makes the driver start profiling by calling gator_op_start() in the driver
161 if (writeDriver("/dev/gator/enable", "1") != 0) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100162 logg->logError("The gator driver did not start properly. Please view the linux console or dmesg log for more information on the failure.");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100163 handleException();
164 }
165
166 lseek(mBufferFD, 0, SEEK_SET);
167
168 sem_post(mStartProfile);
169
Jon Medhurste31266f2014-08-04 15:47:44 +0100170 pthread_t bootstrapThreadID;
171 if (pthread_create(&bootstrapThreadID, NULL, bootstrapThreadStatic, this) != 0) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100172 logg->logError("Unable to start the gator_bootstrap thread");
Jon Medhurste31266f2014-08-04 15:47:44 +0100173 handleException();
174 }
175
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100176 // Collect Data
177 do {
178 // This command will stall until data is received from the driver
179 // Calls event_buffer_read in the driver
180 errno = 0;
181 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
182
183 // If read() returned due to an interrupt signal, re-read to obtain the last bit of collected data
184 if (bytesCollected == -1 && errno == EINTR) {
185 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
186 }
187
188 // return the total bytes written
189 logg->logMessage("Driver read of %d bytes", bytesCollected);
190
191 // In one shot mode, stop collection once all the buffers are filled
192 if (gSessionData->mOneShot && gSessionData->mSessionIsActive) {
193 if (bytesCollected == -1 || mFifo->willFill(bytesCollected)) {
Jon Medhurstb1d07442015-05-08 12:04:18 +0100194 logg->logMessage("One shot (gator.ko)");
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100195 child->endSession();
196 }
197 }
198 collectBuffer = mFifo->write(bytesCollected);
199 } while (bytesCollected > 0);
200
201 logg->logMessage("Exit collect data loop");
Jon Medhurste31266f2014-08-04 15:47:44 +0100202
203 pthread_join(bootstrapThreadID, NULL);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100204}
205
206void DriverSource::interrupt() {
207 // This command should cause the read() function in collect() to return and stop the driver from profiling
208 if (writeDriver("/dev/gator/enable", "0") != 0) {
209 logg->logMessage("Stopping kernel failed");
210 }
211}
212
213bool DriverSource::isDone() {
Jon Medhurste31266f2014-08-04 15:47:44 +0100214 return mLength <= 0 && (mBuffer == NULL || mBuffer->isDone());
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100215}
216
217void DriverSource::write(Sender *sender) {
218 char *data = mFifo->read(&mLength);
219 if (data != NULL) {
220 sender->writeData(data, mLength, RESPONSE_APC_DATA);
221 mFifo->release();
Jon Medhurste31266f2014-08-04 15:47:44 +0100222 // Assume the summary packet is in the first block received from the driver
223 gSessionData->mSentSummary = true;
224 }
225 if (mBuffer != NULL && !mBuffer->isDone()) {
226 mBuffer->write(sender);
227 if (mBuffer->isDone()) {
228 Buffer *buf = mBuffer;
229 mBuffer = NULL;
230 delete buf;
231 }
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100232 }
233}
234
235int DriverSource::readIntDriver(const char *fullpath, int *value) {
236 char data[40]; // Sufficiently large to hold any integer
Jon Medhurst96b56152014-10-30 18:01:15 +0000237 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100238 if (fd < 0) {
239 return -1;
240 }
241
242 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
243 close(fd);
244 if (bytes < 0) {
245 return -1;
246 }
247 data[bytes] = '\0';
248
249 char *endptr;
250 errno = 0;
251 *value = strtol(data, &endptr, 10);
252 if (errno != 0 || *endptr != '\n') {
253 logg->logMessage("Invalid value in file %s", fullpath);
254 return -1;
255 }
256
257 return 0;
258}
259
260int DriverSource::readInt64Driver(const char *fullpath, int64_t *value) {
261 char data[40]; // Sufficiently large to hold any integer
Jon Medhurst96b56152014-10-30 18:01:15 +0000262 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100263 if (fd < 0) {
264 return -1;
265 }
266
267 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
268 close(fd);
269 if (bytes < 0) {
270 return -1;
271 }
272 data[bytes] = '\0';
273
274 char *endptr;
275 errno = 0;
276 *value = strtoll(data, &endptr, 10);
Jon Medhurste31266f2014-08-04 15:47:44 +0100277 if (errno != 0 || (*endptr != '\n' && *endptr != '\0')) {
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100278 logg->logMessage("Invalid value in file %s", fullpath);
279 return -1;
280 }
281
282 return 0;
283}
284
285int DriverSource::writeDriver(const char *fullpath, const char *data) {
Jon Medhurst96b56152014-10-30 18:01:15 +0000286 int fd = open(fullpath, O_WRONLY | O_CLOEXEC);
Jon Medhurst15ce78d2014-04-10 09:02:02 +0100287 if (fd < 0) {
288 return -1;
289 }
290 if (::write(fd, data, strlen(data)) < 0) {
291 close(fd);
292 logg->logMessage("Opened but could not write to %s", fullpath);
293 return -1;
294 }
295 close(fd);
296 return 0;
297}
298
299int DriverSource::writeDriver(const char *path, int value) {
300 char data[40]; // Sufficiently large to hold any integer
301 snprintf(data, sizeof(data), "%d", value);
302 return writeDriver(path, data);
303}
304
305int DriverSource::writeDriver(const char *path, int64_t value) {
306 char data[40]; // Sufficiently large to hold any integer
307 snprintf(data, sizeof(data), "%" PRIi64, value);
308 return writeDriver(path, data);
309}
310
311int DriverSource::writeReadDriver(const char *path, int *value) {
312 if (writeDriver(path, *value) || readIntDriver(path, value)) {
313 return -1;
314 }
315 return 0;
316}
317
318int DriverSource::writeReadDriver(const char *path, int64_t *value) {
319 if (writeDriver(path, *value) || readInt64Driver(path, value)) {
320 return -1;
321 }
322 return 0;
323}