gator: Version 5.20
[firefly-linux-kernel-4.4.55.git] / tools / gator / daemon / DriverSource.cpp
1 /**
2  * Copyright (C) ARM Limited 2010-2014. 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  */
8
9 // Define to get format macros from inttypes.h
10 #define __STDC_FORMAT_MACROS
11
12 #include "DriverSource.h"
13
14 #include <fcntl.h>
15 #include <inttypes.h>
16 #include <sys/prctl.h>
17 #include <unistd.h>
18
19 #include "Buffer.h"
20 #include "Child.h"
21 #include "DynBuf.h"
22 #include "Fifo.h"
23 #include "Logging.h"
24 #include "Proc.h"
25 #include "Sender.h"
26 #include "SessionData.h"
27
28 extern Child *child;
29
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;
32
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");
36                 handleException();
37         }
38
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"
46                                 ">> * gator driver\n"
47                                 ">> * gator daemon\n"
48                                 ">> * Streamline", driver_version, PROTOCOL_VERSION);
49                         handleException();
50                 } else {
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);
55                         handleException();
56                 }
57         }
58
59         int enable = -1;
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.");
62                 handleException();
63         }
64
65         readIntDriver("/dev/gator/cpu_cores", &gSessionData->mCores);
66         if (gSessionData->mCores == 0) {
67                 gSessionData->mCores = 1;
68         }
69
70         if (readIntDriver("/dev/gator/buffer_size", &mBufferSize) || mBufferSize <= 0) {
71                 logg->logError(__FILE__, __LINE__, "Unable to read the driver buffer size");
72                 handleException();
73         }
74 }
75
76 DriverSource::~DriverSource() {
77         delete mFifo;
78
79         // Write zero for safety, as a zero should have already been written
80         writeDriver("/dev/gator/enable", "0");
81
82         // Calls event_buffer_release in the driver
83         if (mBufferFD) {
84                 close(mBufferFD);
85         }
86 }
87
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);
92
93         return true;
94 }
95
96 void DriverSource::bootstrapThread() {
97         prctl(PR_SET_NAME, (unsigned long)&"gatord-proc", 0, 0, 0);
98
99         DynBuf printb;
100         DynBuf b1;
101         DynBuf b2;
102         const uint64_t currTime = getTime();
103
104         if (!readProcComms(currTime, mBuffer, &printb, &b1, &b2)) {
105                 logg->logError(__FILE__, __LINE__, "readProcComms failed");
106                 handleException();
107         }
108
109         mBuffer->commit(currTime);
110         mBuffer->setDone();
111 }
112
113 void *DriverSource::bootstrapThreadStatic(void *arg) {
114         static_cast<DriverSource *>(arg)->bootstrapThread();
115         return NULL;
116 }
117
118 void DriverSource::run() {
119         // Get the initial pointer to the collect buffer
120         char *collectBuffer = mFifo->start();
121         int bytesCollected = 0;
122
123         logg->logMessage("********** Profiling started **********");
124
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");
128                 handleException();
129         }
130
131         // open the buffer which calls userspace_buffer_open() in the driver
132         mBufferFD = open("/dev/gator/buffer", O_RDONLY | O_CLOEXEC);
133         if (mBufferFD < 0) {
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.");
135                 handleException();
136         }
137
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");
141                 handleException();
142         }
143
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");
148                 handleException();
149         }
150
151         // Set the live rate
152         if (writeReadDriver("/dev/gator/live_rate", &gSessionData->mLiveRate)) {
153                 logg->logError(__FILE__, __LINE__, "Unable to set the driver live rate");
154                 handleException();
155         }
156
157         logg->logMessage("Start the driver");
158
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.");
162                 handleException();
163         }
164
165         lseek(mBufferFD, 0, SEEK_SET);
166
167         sem_post(mStartProfile);
168
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");
172                 handleException();
173         }
174
175         // Collect Data
176         do {
177                 // This command will stall until data is received from the driver
178                 // Calls event_buffer_read in the driver
179                 errno = 0;
180                 bytesCollected = read(mBufferFD, collectBuffer, mBufferSize);
181
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);
185                 }
186
187                 // return the total bytes written
188                 logg->logMessage("Driver read of %d bytes", bytesCollected);
189
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");
194                                 child->endSession();
195                         }
196                 }
197                 collectBuffer = mFifo->write(bytesCollected);
198         } while (bytesCollected > 0);
199
200         logg->logMessage("Exit collect data loop");
201
202         pthread_join(bootstrapThreadID, NULL);
203 }
204
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");
209         }
210 }
211
212 bool DriverSource::isDone() {
213         return mLength <= 0 && (mBuffer == NULL || mBuffer->isDone());
214 }
215
216 void DriverSource::write(Sender *sender) {
217         char *data = mFifo->read(&mLength);
218         if (data != NULL) {
219                 sender->writeData(data, mLength, RESPONSE_APC_DATA);
220                 mFifo->release();
221                 // Assume the summary packet is in the first block received from the driver
222                 gSessionData->mSentSummary = true;
223         }
224         if (mBuffer != NULL && !mBuffer->isDone()) {
225                 mBuffer->write(sender);
226                 if (mBuffer->isDone()) {
227                         Buffer *buf = mBuffer;
228                         mBuffer = NULL;
229                         delete buf;
230                 }
231         }
232 }
233
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);
237         if (fd < 0) {
238                 return -1;
239         }
240
241         const ssize_t bytes = read(fd, data, sizeof(data) - 1);
242         close(fd);
243         if (bytes < 0) {
244                 return -1;
245         }
246         data[bytes] = '\0';
247
248         char *endptr;
249         errno = 0;
250         *value = strtol(data, &endptr, 10);
251         if (errno != 0 || *endptr != '\n') {
252                 logg->logMessage("Invalid value in file %s", fullpath);
253                 return -1;
254         }
255
256         return 0;
257 }
258
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);
262         if (fd < 0) {
263                 return -1;
264         }
265
266         const ssize_t bytes = read(fd, data, sizeof(data) - 1);
267         close(fd);
268         if (bytes < 0) {
269                 return -1;
270         }
271         data[bytes] = '\0';
272
273         char *endptr;
274         errno = 0;
275         *value = strtoll(data, &endptr, 10);
276         if (errno != 0 || (*endptr != '\n' && *endptr != '\0')) {
277                 logg->logMessage("Invalid value in file %s", fullpath);
278                 return -1;
279         }
280
281         return 0;
282 }
283
284 int DriverSource::writeDriver(const char *fullpath, const char *data) {
285         int fd = open(fullpath, O_WRONLY | O_CLOEXEC);
286         if (fd < 0) {
287                 return -1;
288         }
289         if (::write(fd, data, strlen(data)) < 0) {
290                 close(fd);
291                 logg->logMessage("Opened but could not write to %s", fullpath);
292                 return -1;
293         }
294         close(fd);
295         return 0;
296 }
297
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);
302 }
303
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);
308 }
309
310 int DriverSource::writeReadDriver(const char *path, int *value) {
311         if (writeDriver(path, *value) || readIntDriver(path, value)) {
312                 return -1;
313         }
314         return 0;
315 }
316
317 int DriverSource::writeReadDriver(const char *path, int64_t *value) {
318         if (writeDriver(path, *value) || readInt64Driver(path, value)) {
319                 return -1;
320         }
321         return 0;
322 }