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"
21 #include "SessionData.h"
25 DriverSource::DriverSource(sem_t *senderSem, sem_t *startProfile) : mFifo(NULL), mSenderSem(senderSem), mStartProfile(startProfile), mBufferSize(0), mBufferFD(0), mLength(1) {
26 int driver_version = 0;
28 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
29 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
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"
42 ">> * Streamline", driver_version, PROTOCOL_VERSION);
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);
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.");
59 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
60 if (gSessionData->mCores == 0) {
61 gSessionData->mCores = 1;
64 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
65 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
70 DriverSource::~DriverSource() {
73 // Write zero for safety, as a zero should have already been written
74 writeDriver("/dev/gator/enable", "0");
76 // Calls event_buffer_release in the driver
82 bool 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);
90 void DriverSource::run() {
91 // Get the initial pointer to the collect buffer
92 char *collectBuffer = mFifo->start();
93 int bytesCollected = 0;
95 logg->logMessage("********** Profiling started **********");
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");
103 // open the buffer which calls userspace_buffer_open() in the driver
104 mBufferFD = open("/dev/gator/buffer", O_RDONLY);
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.");
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");
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");
124 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
125 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
129 logg->logMessage("Start the driver");
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.");
137 lseek(mBufferFD, 0, SEEK_SET);
139 sem_post(mStartProfile);
143 // This command will stall until data is received from the driver
144 // Calls event_buffer_read in the driver
146 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
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);
153 // return the total bytes written
154 logg->logMessage("Driver read of %d bytes", bytesCollected);
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");
163 collectBuffer = mFifo->write(bytesCollected);
164 } while (bytesCollected > 0);
166 logg->logMessage("Exit collect data loop");
169 void 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");
176 bool DriverSource::isDone() {
180 void DriverSource::write(Sender *sender) {
181 char *data = mFifo->read(&mLength);
183 sender->writeData(data, mLength, RESPONSE_APC_DATA);
188 int 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);
195 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
204 *value = strtol(data, &endptr, 10);
205 if (errno != 0 || *endptr != '\n') {
206 logg->logMessage("Invalid value in file %s", fullpath);
213 int 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);
220 const ssize_t bytes = read(fd, data, sizeof(data) - 1);
229 *value = strtoll(data, &endptr, 10);
230 if (errno != 0 || *endptr != '\n') {
231 logg->logMessage("Invalid value in file %s", fullpath);
238 int DriverSource::writeDriver(const char *fullpath, const char *data) {
239 int fd = open(fullpath, O_WRONLY);
243 if (::write(fd, data, strlen(data)) < 0) {
245 logg->logMessage("Opened but could not write to %s", fullpath);
252 int 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);
258 int 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);
264 int DriverSource::writeReadDriver(const char *path, int *value) {
265 if (writeDriver(path, *value) || readIntDriver(path, value)) {
271 int DriverSource::writeReadDriver(const char *path, int64_t *value) {
272 if (writeDriver(path, *value) || readInt64Driver(path, value)) {