-
Notifications
You must be signed in to change notification settings - Fork 11
/
burrow.cpp
250 lines (204 loc) · 6.12 KB
/
burrow.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "interface.hpp"
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include "json/writer.h"
#include "json/value.h"
#ifndef DEBUG
// otherwise we will get complaints about assert()ed variables being unused
#pragma GCC diagnostic ignored "-Wunused-variable"
#endif
#ifdef ANDROID
#include <android/log.h>
#ifndef LOGI
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, "burrow", __VA_ARGS__);
#endif
#ifndef LOGE
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, "burrow", __VA_ARGS__);
#endif
#ifndef LOGD
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, "burrow", __VA_ARGS__);
#endif
#else
#ifndef LOGI
#define LOGI(...) do { fprintf( stdout, __VA_ARGS__); } while(0)
#endif
#ifndef LOGE
#define LOGE(...) do { fprintf( stderr, __VA_ARGS__ ); } while(0)
#endif
#ifndef LOGD
#define LOGD(...) do { \
fprintf( stderr, "burrow %s:%d: ", __FILE__, __LINE__ ); \
fprintf( stderr, __VA_ARGS__ ); \
} while(0)
#endif
#endif
static bool monitor(const int maxCpu, std::string const& process, std::string const& outDir, int timeout)
{
bool success = false;
Json::Value ferret;
Json::Value cpus;
for( int cpu = 0; cpu <= maxCpu; ++cpu )
{
cpus.append( cpu );
}
ferret["cpus"] = cpus;
Json::Value process_names;
process_names.append( process );
ferret["process_names"] = process_names;
/* We use a pipe to obtain "live" status information from Ferret.
*/
int fds[2];
if( pipe( fds ) < 0 )
{
perror( "pipe" );
exit( 1 );
}
/* We cannot function without the Ferret collector. The collector writes to the pipe
* and we read the status (see later).
*/
ferret["required"] = true;
ferret["status_fd"] = fds[1];
ferret["output_dir"] = outDir;
ferret["poll_timeout"] = timeout;
Json::Value cfg;
cfg["ferret"] = ferret;
Collection c( cfg );
success = c.initialize();
if( success )
{
bool finished = false;
int status, last_status = -99;
c.start();
while( !finished )
{
/* Read integer status values from the collector.
*
* -1 indicates a timeout or other error, yielding no data.
* 1 indicates that data capture is progressing.
* 0 indicates that the named process has exited.
*/
int recvd = read( fds[0], &status, sizeof(status) );
assert( recvd == sizeof(status) );
if( status != last_status )
{
last_status = status;
if( status == -1 )
{
LOGE( "ERROR: timed out waiting for '%s'\n", process.c_str() );
finished = true;
success = false;
}
else if( status == 0 )
{
LOGI( "INFO: process '%s' has finished.\n", process.c_str() );
finished = true;
}
else
{
LOGI( "INFO: Process '%s' found, logging in progress.\n", process.c_str() );
}
}
}
c.stop();
}
close( fds[0] );
close( fds[1] );
if( success )
{
/* Dump the JSON formatted results for standardised result parsing.
*/
Json::Value results = c.results();
Json::StyledWriter writer;
std::string data = writer.write( results );
LOGI( "Results:\n%s", data.c_str() );
}
return success;
}
void usage(int status)
{
static const char message[] =
"usage: burrow [-h] -c <max CPU number> PROGRAM\n\n"
"Burrow (+Ferret) measures the True CPU Load of PROGRAM.\n"
"\n"
"The burrow tool runs on a Linux/Android target, extracting "
"data for subsequent analysis by Ferret.\n"
"\n"
"This is achieved by polling the /proc and /sys filesystems to "
"determine the CPU utilisation, the\n"
"CPU occupancy of each thread in PROGRAM, and the CPU frequency of all monitored CPUs.\n"
"Specifically, the "
"/sys/devices/system/cpu/cpu[cpu_nr]/cpufreq/scaling_cur_freq and the\n"
"/proc/[pid]/task/*/stat files are polled at a rate of twice _SC_CLK_TCK and "
"recorded in file \nferret_run_0.data.\n"
"\n"
" -h Display this message\n"
" -c Maximum CPU number to monitor.\n"
" -o Output file path (Linux default '.', required for Android).\n"
" -t Maximum time (integer seconds, default 30) to wait for PROGRAM to appear.\n"
"\n";
LOGE( message );
exit( status );
}
int main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
int c;
int maxCpu = -1;
int timeout = 30;
std::string outputPath;
while( ( c = getopt(argc, argv, "hc:o:t:") ) != -1 )
{
switch( c )
{
case 'h':
usage( 0 );
break;
case 'c':
maxCpu = (int) atoi( optarg );
break;
case 'o':
outputPath = optarg;
break;
case 't':
timeout = (int) atoi( optarg );
break;
default:
usage( 2 );
break;
}
}
#ifdef ANDROID
if( outputPath.size() == 0 )
{
LOGE( "ERROR: an output path is required.\n" );
usage( 2 );
}
#endif
if( timeout < 1 )
{
LOGE( "ERROR: positive, non-zero timeouts are required.\n" );
usage( 2 );
}
if( maxCpu < 0 )
{
LOGE( "ERROR: maximum CPU number is required.\n" );
usage( 2 );
}
if( optind >= argc )
{
LOGE( "ERROR: missing executable\n" );
usage( 2 );
}
if( argc - optind > 1 )
{
LOGE( "ERROR: too many executables\n" );
usage( 2 );
}
std::string processName = argv[optind];
bool success = monitor( maxCpu, processName, outputPath, timeout );
return success ? 0 : 1;
}