blob: f78ec6b7ce413648d6b3d8df8e6295b9f37d3fb2 [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>
15#include <unistd.h>
16
17#include "Child.h"
18#include "Fifo.h"
19#include "Logging.h"
20#include "Sender.h"
21#include "SessionData.h"
22
23extern Child *child;
24
25DriverSource::DriverSource(sem_t *senderSem, sem_t *startProfile) : mFifo(NULL), mSenderSem(senderSem), mStartProfile(startProfile), mBufferSize(0), mBufferFD(0), mLength(1) {
26 int driver_version = 0;
27
28 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
29 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
30 handleException();
31 }
32
33 // Verify the driver version matches the daemon version
34 if (driver_version != PROTOCOL_VERSION) {
35 if ((driver_version > PROTOCOL_DEV) || (PROTOCOL_VERSION > PROTOCOL_DEV)) {
36 // One of the mismatched versions is development version
37 logg->logError(__FILE__, __LINE__,
38 "DEVELOPMENT BUILD MISMATCH: gator driver version \"%d\" is not in sync with gator daemon version \"%d\".\n"
39 ">> The following must be synchronized from engineering repository:\n"
40 ">> * gator driver\n"
41 ">> * gator daemon\n"
42 ">> * Streamline", driver_version, PROTOCOL_VERSION);
43 handleException();
44 } else {
45 // Release version mismatch
46 logg->logError(__FILE__, __LINE__,
47 "gator driver version \"%d\" is different than gator daemon version \"%d\".\n"
48 ">> Please upgrade the driver and daemon to the latest versions.", driver_version, PROTOCOL_VERSION);
49 handleException();
50 }
51 }
52
53 int enable = -1;
54 if (readIntDriver("/dev/gator/enable", &enable) != 0 || enable != 0) {
55 logg->logError(__FILE__, __LINE__, "Driver already enabled, possibly a session is already in progress.");
56 handleException();
57 }
58
59 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
60 if (gSessionData->mCores == 0) {
61 gSessionData->mCores = 1;
62 }
63
64 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
65 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
66 handleException();
67 }
68}
69
70DriverSource::~DriverSource() {
71 delete mFifo;
72
73 // Write zero for safety, as a zero should have already been written
74 writeDriver("/dev/gator/enable", "0");
75
76 // Calls event_buffer_release in the driver
77 if (mBufferFD) {
78 close(mBufferFD);
79 }
80}
81
82bool DriverSource::prepare() {
83 // Create user-space buffers, add 5 to the size to account for the 1-byte type and 4-byte length
84 logg->logMessage("Created %d MB collector buffer with a %d-byte ragged end", gSessionData->mTotalBufferSize, mBufferSize);
85 mFifo = new Fifo(mBufferSize + 5, gSessionData->mTotalBufferSize*1024*1024, mSenderSem);
86
87 return true;
88}
89
90void DriverSource::run() {
91 // Get the initial pointer to the collect buffer
92 char *collectBuffer = mFifo->start();
93 int bytesCollected = 0;
94
95 logg->logMessage("********** Profiling started **********");
96
97 // Set the maximum backtrace depth
98 if (writeReadDriver("/dev/gator/backtrace_depth", &gSessionData->mBacktraceDepth)) {
99 logg->logError(__FILE__, __LINE__, "Unable to set the driver backtrace depth");
100 handleException();
101 }
102
103 // open the buffer which calls userspace_buffer_open() in the driver
104 mBufferFD = open("/dev/gator/buffer", O_RDONLY);
105 if (mBufferFD < 0) {
106 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.");
107 handleException();
108 }
109
110 // set the tick rate of the profiling timer
111 if (writeReadDriver("/dev/gator/tick", &gSessionData->mSampleRate) != 0) {
112 logg->logError(__FILE__, __LINE__, "Unable to set the driver tick");
113 handleException();
114 }
115
116 // notify the kernel of the response type
117 int response_type = gSessionData->mLocalCapture ? 0 : RESPONSE_APC_DATA;
118 if (writeDriver("/dev/gator/response_type", response_type)) {
119 logg->logError(__FILE__, __LINE__, "Unable to write the response type");
120 handleException();
121 }
122
123 // Set the live rate
124 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
125 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
126 handleException();
127 }
128
129 logg->logMessage("Start the driver");
130
131 // This command makes the driver start profiling by calling gator_op_start() in the driver
132 if (writeDriver("/dev/gator/enable", "1") != 0) {
133 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.");
134 handleException();
135 }
136
137 lseek(mBufferFD, 0, SEEK_SET);
138
139 sem_post(mStartProfile);
140
141 // Collect Data
142 do {
143 // This command will stall until data is received from the driver
144 // Calls event_buffer_read in the driver
145 errno = 0;
146 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
147
148 // If read() returned due to an interrupt signal, re-read to obtain the last bit of collected data
149 if (bytesCollected == -1 && errno == EINTR) {
150 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
151 }
152
153 // return the total bytes written
154 logg->logMessage("Driver read of %d bytes", bytesCollected);
155
156 // In one shot mode, stop collection once all the buffers are filled
157 if (gSessionData->mOneShot && gSessionData->mSessionIsActive) {
158 if (bytesCollected == -1 || mFifo->willFill(bytesCollected)) {
159 logg->logMessage("One shot");
160 child->endSession();
161 }
162 }
163 collectBuffer = mFifo->write(bytesCollected);
164 } while (bytesCollected > 0);
165
166 logg->logMessage("Exit collect data loop");
167}
168
169void DriverSource::interrupt() {
170 // This command should cause the read() function in collect() to return and stop the driver from profiling
171 if (writeDriver("/dev/gator/enable", "0") != 0) {
172 logg->logMessage("Stopping kernel failed");
173 }
174}
175
176bool DriverSource::isDone() {
177 return mLength <= 0;
178}
179
180void DriverSource::write(Sender *sender) {
181 char *data = mFifo->read(&mLength);
182 if (data != NULL) {
183 sender->writeData(data, mLength, RESPONSE_APC_DATA);
184 mFifo->release();
185 }
186}
187
188int DriverSource::readIntDriver(const char *fullpath, int *value) {
189 char data[40]; // Sufficiently large to hold any integer
190 const int fd = open(fullpath, O_RDONLY);
191 if (fd < 0) {
192 return -1;
193 }
194
195 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
196 close(fd);
197 if (bytes < 0) {
198 return -1;
199 }
200 data[bytes] = '\0';
201
202 char *endptr;
203 errno = 0;
204 *value = strtol(data, &endptr, 10);
205 if (errno != 0 || *endptr != '\n') {
206 logg->logMessage("Invalid value in file %s", fullpath);
207 return -1;
208 }
209
210 return 0;
211}
212
213int DriverSource::readInt64Driver(const char *fullpath, int64_t *value) {
214 char data[40]; // Sufficiently large to hold any integer
215 const int fd = open(fullpath, O_RDONLY);
216 if (fd < 0) {
217 return -1;
218 }
219
220 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
221 close(fd);
222 if (bytes < 0) {
223 return -1;
224 }
225 data[bytes] = '\0';
226
227 char *endptr;
228 errno = 0;
229 *value = strtoll(data, &endptr, 10);
230 if (errno != 0 || *endptr != '\n') {
231 logg->logMessage("Invalid value in file %s", fullpath);
232 return -1;
233 }
234
235 return 0;
236}
237
238int DriverSource::writeDriver(const char *fullpath, const char *data) {
239 int fd = open(fullpath, O_WRONLY);
240 if (fd < 0) {
241 return -1;
242 }
243 if (::write(fd, data, strlen(data)) < 0) {
244 close(fd);
245 logg->logMessage("Opened but could not write to %s", fullpath);
246 return -1;
247 }
248 close(fd);
249 return 0;
250}
251
252int DriverSource::writeDriver(const char *path, int value) {
253 char data[40]; // Sufficiently large to hold any integer
254 snprintf(data, sizeof(data), "%d", value);
255 return writeDriver(path, data);
256}
257
258int DriverSource::writeDriver(const char *path, int64_t value) {
259 char data[40]; // Sufficiently large to hold any integer
260 snprintf(data, sizeof(data), "%" PRIi64, value);
261 return writeDriver(path, data);
262}
263
264int DriverSource::writeReadDriver(const char *path, int *value) {
265 if (writeDriver(path, *value) || readIntDriver(path, value)) {
266 return -1;
267 }
268 return 0;
269}
270
271int DriverSource::writeReadDriver(const char *path, int64_t *value) {
272 if (writeDriver(path, *value) || readInt64Driver(path, value)) {
273 return -1;
274 }
275 return 0;
276}