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