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 to get format macros from inttypes.h
10 #define __STDC_FORMAT_MACROS
12 #include "DriverSource.h"
16 #include <sys/prctl.h>
26 #include "SessionData.h"
30 DriverSource::DriverSource(sem_t *senderSem, sem_t *startProfile) : mBuffer(NULL), mFifo(NULL), mSenderSem(senderSem), mStartProfile(startProfile), mBufferSize(0), mBufferFD(0), mLength(1) {
31 int driver_version = 0;
33 mBuffer = new Buffer(0, FRAME_PERF_ATTRS, 4*1024*1024, senderSem);
34 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
35 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
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"
48 ">> * Streamline", driver_version, PROTOCOL_VERSION);
51 // Release version mismatch
52 logg->logError(__FILE__, __LINE__,
53 "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);
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.");
65 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
66 if (gSessionData->mCores == 0) {
67 gSessionData->mCores = 1;
70 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
71 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
76 DriverSource::~DriverSource() {
79 // Write zero for safety, as a zero should have already been written
80 writeDriver("/dev/gator/enable", "0");
82 // Calls event_buffer_release in the driver
88 bool 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);
96 void DriverSource::bootstrapThread() {
97 prctl(PR_SET_NAME, (unsigned long)&"gatord-proc", 0, 0, 0);
102 const uint64_t currTime = getTime();
104 if (!readProcComms(currTime, mBuffer, &printb, &b1, &b2)) {
105 logg->logError(__FILE__, __LINE__, "readProcComms failed");
109 mBuffer->commit(currTime);
113 void *DriverSource::bootstrapThreadStatic(void *arg) {
114 static_cast<DriverSource *>(arg)->bootstrapThread();
118 void DriverSource::run() {
119 // Get the initial pointer to the collect buffer
120 char *collectBuffer = mFifo->start();
121 int bytesCollected = 0;
123 logg->logMessage("********** Profiling started **********");
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");
131 // open the buffer which calls userspace_buffer_open() in the driver
132 mBufferFD = open("/dev/gator/buffer", O_RDONLY | O_CLOEXEC);
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.");
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");
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");
152 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
153 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
157 logg->logMessage("Start the driver");
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.");
165 lseek(mBufferFD, 0, SEEK_SET);
167 sem_post(mStartProfile);
169 pthread_t bootstrapThreadID;
170 if (pthread_create(&bootstrapThreadID, NULL, bootstrapThreadStatic, this) != 0) {
171 logg->logError(__FILE__, __LINE__, "Unable to start the gator_bootstrap thread");
177 // This command will stall until data is received from the driver
178 // Calls event_buffer_read in the driver
180 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
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);
187 // return the total bytes written
188 logg->logMessage("Driver read of %d bytes", bytesCollected);
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");
197 collectBuffer = mFifo->write(bytesCollected);
198 } while (bytesCollected > 0);
200 logg->logMessage("Exit collect data loop");
202 pthread_join(bootstrapThreadID, NULL);
205 void 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");
212 bool DriverSource::isDone() {
213 return mLength <= 0 && (mBuffer == NULL || mBuffer->isDone());
216 void DriverSource::write(Sender *sender) {
217 char *data = mFifo->read(&mLength);
219 sender->writeData(data, mLength, RESPONSE_APC_DATA);
221 // Assume the summary packet is in the first block received from the driver
222 gSessionData->mSentSummary = true;
224 if (mBuffer != NULL && !mBuffer->isDone()) {
225 mBuffer->write(sender);
226 if (mBuffer->isDone()) {
227 Buffer *buf = mBuffer;
234 int DriverSource::readIntDriver(const char *fullpath, int *value) {
235 char data[40]; // Sufficiently large to hold any integer
236 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
241 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
250 *value = strtol(data, &endptr, 10);
251 if (errno != 0 || *endptr != '\n') {
252 logg->logMessage("Invalid value in file %s", fullpath);
259 int DriverSource::readInt64Driver(const char *fullpath, int64_t *value) {
260 char data[40]; // Sufficiently large to hold any integer
261 const int fd = open(fullpath, O_RDONLY | O_CLOEXEC);
266 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
275 *value = strtoll(data, &endptr, 10);
276 if (errno != 0 || (*endptr != '\n' && *endptr != '\0')) {
277 logg->logMessage("Invalid value in file %s", fullpath);
284 int DriverSource::writeDriver(const char *fullpath, const char *data) {
285 int fd = open(fullpath, O_WRONLY | O_CLOEXEC);
289 if (::write(fd, data, strlen(data)) < 0) {
291 logg->logMessage("Opened but could not write to %s", fullpath);
298 int 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);
304 int 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);
310 int DriverSource::writeReadDriver(const char *path, int *value) {
311 if (writeDriver(path, *value) || readIntDriver(path, value)) {
317 int DriverSource::writeReadDriver(const char *path, int64_t *value) {
318 if (writeDriver(path, *value) || readInt64Driver(path, value)) {