2 * Copyright (C) ARM Limited 2010-2013. 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
18 #include "Collector.h"
19 #include "SessionData.h"
23 // Driver initialization independent of session settings
24 Collector::Collector() {
30 if (readIntDriver("/dev/gator/enable", &enable) != 0 || enable != 0) {
31 logg->logError(__FILE__, __LINE__, "Driver already enabled, possibly a session is already in progress.");
35 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
36 if (gSessionData->mCores == 0) {
37 gSessionData->mCores = 1;
41 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
42 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
47 Collector::~Collector() {
48 // Write zero for safety, as a zero should have already been written
49 writeDriver("/dev/gator/enable", "0");
51 // Calls event_buffer_release in the driver
57 void Collector::checkVersion() {
58 int driver_version = 0;
60 if (readIntDriver("/dev/gator/version", &driver_version) == -1) {
61 logg->logError(__FILE__, __LINE__, "Error reading gator driver version");
65 // Verify the driver version matches the daemon version
66 if (driver_version != PROTOCOL_VERSION) {
67 if ((driver_version > PROTOCOL_DEV) || (PROTOCOL_VERSION > PROTOCOL_DEV)) {
68 // One of the mismatched versions is development version
69 logg->logError(__FILE__, __LINE__,
70 "DEVELOPMENT BUILD MISMATCH: gator driver version \"%d\" is not in sync with gator daemon version \"%d\".\n"
71 ">> The following must be synchronized from engineering repository:\n"
74 ">> * Streamline", driver_version, PROTOCOL_VERSION);
77 // Release version mismatch
78 logg->logError(__FILE__, __LINE__,
79 "gator driver version \"%d\" is different than gator daemon version \"%d\".\n"
80 ">> Please upgrade the driver and daemon to the latest versions.", driver_version, PROTOCOL_VERSION);
86 void Collector::start() {
87 // Set the maximum backtrace depth
88 if (writeReadDriver("/dev/gator/backtrace_depth", &gSessionData->mBacktraceDepth)) {
89 logg->logError(__FILE__, __LINE__, "Unable to set the driver backtrace depth");
93 // open the buffer which calls userspace_buffer_open() in the driver
94 mBufferFD = open("/dev/gator/buffer", O_RDONLY);
96 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.");
100 // set the tick rate of the profiling timer
101 if (writeReadDriver("/dev/gator/tick", &gSessionData->mSampleRate) != 0) {
102 logg->logError(__FILE__, __LINE__, "Unable to set the driver tick");
106 // notify the kernel of the response type
107 int response_type = gSessionData->mLocalCapture ? 0 : RESPONSE_APC_DATA;
108 if (writeDriver("/dev/gator/response_type", response_type)) {
109 logg->logError(__FILE__, __LINE__, "Unable to write the response type");
114 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
115 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
119 logg->logMessage("Start the driver");
121 // This command makes the driver start profiling by calling gator_op_start() in the driver
122 if (writeDriver("/dev/gator/enable", "1") != 0) {
123 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.");
127 lseek(mBufferFD, 0, SEEK_SET);
130 // These commands should cause the read() function in collect() to return
131 void Collector::stop() {
132 // This will stop the driver from profiling
133 if (writeDriver("/dev/gator/enable", "0") != 0) {
134 logg->logMessage("Stopping kernel failed");
138 int Collector::collect(char* buffer) {
139 // Calls event_buffer_read in the driver
143 bytesRead = read(mBufferFD, buffer, mBufferSize);
145 // If read() returned due to an interrupt signal, re-read to obtain the last bit of collected data
146 if (bytesRead == -1 && errno == EINTR) {
147 bytesRead = read(mBufferFD, buffer, mBufferSize);
150 // return the total bytes written
151 logg->logMessage("Driver read of %d bytes", bytesRead);
155 int Collector::readIntDriver(const char* fullpath, int* value) {
156 FILE* file = fopen(fullpath, "r");
160 if (fscanf(file, "%u", value) != 1) {
162 logg->logMessage("Invalid value in file %s", fullpath);
169 int Collector::readInt64Driver(const char* fullpath, int64_t* value) {
170 FILE* file = fopen(fullpath, "r");
174 if (fscanf(file, "%" SCNi64, value) != 1) {
176 logg->logMessage("Invalid value in file %s", fullpath);
183 int Collector::writeDriver(const char* path, int value) {
184 char data[40]; // Sufficiently large to hold any integer
185 snprintf(data, sizeof(data), "%d", value);
186 return writeDriver(path, data);
189 int Collector::writeDriver(const char* path, int64_t value) {
190 char data[40]; // Sufficiently large to hold any integer
191 snprintf(data, sizeof(data), "%" PRIi64, value);
192 return writeDriver(path, data);
195 int Collector::writeDriver(const char* fullpath, const char* data) {
196 int fd = open(fullpath, O_WRONLY);
200 if (write(fd, data, strlen(data)) < 0) {
202 logg->logMessage("Opened but could not write to %s", fullpath);
209 int Collector::writeReadDriver(const char* path, int* value) {
210 if (writeDriver(path, *value) || readIntDriver(path, value)) {
216 int Collector::writeReadDriver(const char* path, int64_t* value) {
217 if (writeDriver(path, *value) || readInt64Driver(path, value)) {