1 /**
2 * Copyright (C) ARM Limited 2010-2013. 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 */
9 #define __STDC_FORMAT_MACROS
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include <errno.h>
16 #include <sys/time.h>
17 #include <inttypes.h>
18 #include "Collector.h"
19 #include "SessionData.h"
20 #include "Logging.h"
21 #include "Sender.h"
23 // Driver initialization independent of session settings
24 Collector::Collector() {
25 mBufferFD = 0;
27 checkVersion();
29 int enable = -1;
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.");
32 handleException();
33 }
35 readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
36 if (gSessionData->mCores == 0) {
37 gSessionData->mCores = 1;
38 }
40 mBufferSize = 0;
41 if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
42 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
43 handleException();
44 }
45 }
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
52 if (mBufferFD) {
53 close(mBufferFD);
54 }
55 }
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");
62 handleException();
63 }
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"
72 ">> * gator driver\n"
73 ">> * gator daemon\n"
74 ">> * Streamline", driver_version, PROTOCOL_VERSION);
75 handleException();
76 } else {
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);
81 handleException();
82 }
83 }
84 }
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");
90 handleException();
91 }
93 // open the buffer which calls userspace_buffer_open() in the driver
94 mBufferFD = open("/dev/gator/buffer", O_RDONLY);
95 if (mBufferFD < 0) {
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.");
97 handleException();
98 }
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");
103 handleException();
104 }
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");
110 handleException();
111 }
113 // Set the live rate
114 if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
115 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
116 handleException();
117 }
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.");
124 handleException();
125 }
127 lseek(mBufferFD, 0, SEEK_SET);
128 }
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");
135 }
136 }
138 int Collector::collect(char* buffer) {
139 // Calls event_buffer_read in the driver
140 int bytesRead;
142 errno = 0;
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);
148 }
150 // return the total bytes written
151 logg->logMessage("Driver read of %d bytes", bytesRead);
152 return bytesRead;
153 }
155 int Collector::readIntDriver(const char* fullpath, int* value) {
156 FILE* file = fopen(fullpath, "r");
157 if (file == NULL) {
158 return -1;
159 }
160 if (fscanf(file, "%u", value) != 1) {
161 fclose(file);
162 logg->logMessage("Invalid value in file %s", fullpath);
163 return -1;
164 }
165 fclose(file);
166 return 0;
167 }
169 int Collector::readInt64Driver(const char* fullpath, int64_t* value) {
170 FILE* file = fopen(fullpath, "r");
171 if (file == NULL) {
172 return -1;
173 }
174 if (fscanf(file, "%" SCNi64, value) != 1) {
175 fclose(file);
176 logg->logMessage("Invalid value in file %s", fullpath);
177 return -1;
178 }
179 fclose(file);
180 return 0;
181 }
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);
187 }
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);
193 }
195 int Collector::writeDriver(const char* fullpath, const char* data) {
196 int fd = open(fullpath, O_WRONLY);
197 if (fd < 0) {
198 return -1;
199 }
200 if (write(fd, data, strlen(data)) < 0) {
201 close(fd);
202 logg->logMessage("Opened but could not write to %s", fullpath);
203 return -1;
204 }
205 close(fd);
206 return 0;
207 }
209 int Collector::writeReadDriver(const char* path, int* value) {
210 if (writeDriver(path, *value) || readIntDriver(path, value)) {
211 return -1;
212 }
213 return 0;
214 }
216 int Collector::writeReadDriver(const char* path, int64_t* value) {
217 if (writeDriver(path, *value) || readInt64Driver(path, value)) {
218 return -1;
219 }
220 return 0;
221 }