2 * Copyright (C) ARM Limited 2010-2014. All rights reserved.
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.
9 #define __STDC_FORMAT_MACROS
11 #include "DriverSource.h"
15 #include <sys/prctl.h>
25 #include "SessionData.h"
29 DriverSource::DriverSource(sem_t *senderSem, sem_t *startProfile) : mBuffer(NULL), mFifo(NULL), mSenderSem(senderSem), mStartProfile(startProfile), mBufferSize(0), mBufferFD(0), mLength(1) {
30 int driver_version = 0;
32 mBuffer = new Buffer(0, FRAME_PERF_ATTRS, 4*1024*1024, senderSem);
33 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
34 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
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"
47 ">> * Streamline", driver_version, PROTOCOL_VERSION);
50 // Release version mismatch
51 logg->logError(__FILE__, __LINE__,
52 "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);
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.");
64 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
65 if (gSessionData->mCores == 0) {
66 gSessionData->mCores = 1;
69 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
70 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
75 DriverSource::~DriverSource() {
78 // Write zero for safety, as a zero should have already been written
79 writeDriver("/dev/gator/enable", "0");
81 // Calls event_buffer_release in the driver
87 bool 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);
95 void DriverSource::bootstrapThread() {
96 prctl(PR_SET_NAME, (unsigned long)&"gatord-bootstrap", 0, 0, 0);
103 if (!readProc(mBuffer, false, &printb, &b1, &b2, &b3)) {
104 logg->logMessage("%s(%s:%i): readProc failed", __FUNCTION__, __FILE__, __LINE__);
112 void *DriverSource::bootstrapThreadStatic(void *arg) {
113 static_cast<DriverSource *>(arg)->bootstrapThread();
117 void DriverSource::run() {
118 // Get the initial pointer to the collect buffer
119 char *collectBuffer = mFifo->start();
120 int bytesCollected = 0;
122 logg->logMessage("********** Profiling started **********");
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");
130 // open the buffer which calls userspace_buffer_open() in the driver
131 mBufferFD = open("/dev/gator/buffer", O_RDONLY);
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.");
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");
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");
151 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
152 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
156 logg->logMessage("Start the driver");
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.");
164 lseek(mBufferFD, 0, SEEK_SET);
166 sem_post(mStartProfile);
168 pthread_t bootstrapThreadID;
169 if (pthread_create(&bootstrapThreadID, NULL, bootstrapThreadStatic, this) != 0) {
170 logg->logError(__FILE__, __LINE__, "Unable to start the gator_bootstrap thread");
176 // This command will stall until data is received from the driver
177 // Calls event_buffer_read in the driver
179 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
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);
186 // return the total bytes written
187 logg->logMessage("Driver read of %d bytes", bytesCollected);
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");
196 collectBuffer = mFifo->write(bytesCollected);
197 } while (bytesCollected > 0);
199 logg->logMessage("Exit collect data loop");
201 pthread_join(bootstrapThreadID, NULL);
204 void 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");
211 bool DriverSource::isDone() {
212 return mLength <= 0 && (mBuffer == NULL || mBuffer->isDone());
215 void DriverSource::write(Sender *sender) {
216 char *data = mFifo->read(&mLength);
218 sender->writeData(data, mLength, RESPONSE_APC_DATA);
220 // Assume the summary packet is in the first block received from the driver
221 gSessionData->mSentSummary = true;
223 if (mBuffer != NULL && !mBuffer->isDone()) {
224 mBuffer->write(sender);
225 if (mBuffer->isDone()) {
226 Buffer *buf = mBuffer;
233 int 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);
240 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
249 *value = strtol(data, &endptr, 10);
250 if (errno != 0 || *endptr != '\n') {
251 logg->logMessage("Invalid value in file %s", fullpath);
258 int 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);
265 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
274 *value = strtoll(data, &endptr, 10);
275 if (errno != 0 || (*endptr != '\n' && *endptr != '\0')) {
276 logg->logMessage("Invalid value in file %s", fullpath);
283 int DriverSource::writeDriver(const char *fullpath, const char *data) {
284 int fd = open(fullpath, O_WRONLY);
288 if (::write(fd, data, strlen(data)) < 0) {
290 logg->logMessage("Opened but could not write to %s", fullpath);
297 int 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);
303 int 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);
309 int DriverSource::writeReadDriver(const char *path, int *value) {
310 if (writeDriver(path, *value) || readIntDriver(path, value)) {
316 int DriverSource::writeReadDriver(const char *path, int64_t *value) {
317 if (writeDriver(path, *value) || readInt64Driver(path, value)) {