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
251
|
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// This file provides a thin binary wrapper around the BattOr Agent
// library. This binary wrapper provides a means for non-C++ tracing
// controllers, such as Telemetry and Android Systrace, to issue high-level
// tracing commands to the BattOr..
#include <stdint.h>
#include <iostream>
#include "base/at_exit.h"
#include "base/bind.h"
#include "base/bind_helpers.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/threading/thread.h"
#include "tools/battor_agent/battor_agent.h"
#include "tools/battor_agent/battor_error.h"
#include "tools/battor_agent/battor_finder.h"
using std::cout;
using std::endl;
namespace {
const char kIoThreadName[] = "BattOr IO Thread";
const char kFileThreadName[] = "BattOr File Thread";
const char kUiThreadName[] = "BattOr UI Thread";
const int32_t kBattOrCommandTimeoutSeconds = 10;
void PrintUsage() {
cout << "Usage: battor_agent <command> <arguments>" << endl
<< endl
<< "Commands:" << endl
<< endl
<< " StartTracing <path>" << endl
<< " StopTracing <path>" << endl
<< " SupportsExplicitClockSync" << endl
<< " RecordClockSyncMarker <path> <marker>" << endl
<< " IssueClockSyncMarker <path>" << endl
<< " Help" << endl;
}
void PrintSupportsExplicitClockSync() {
cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl;
}
// Retrieves argument argnum from the argument list, or an empty string if the
// argument doesn't exist.
std::string GetArg(int argnum, int argc, char* argv[]) {
if (argnum >= argc) {
return std::string();
}
return argv[argnum];
}
// Checks if an error occurred and, if it did, prints the error and exits
// with an error code.
void CheckError(battor::BattOrError error) {
if (error != battor::BATTOR_ERROR_NONE)
LOG(FATAL) << "Fatal error when communicating with the BattOr: " << error;
}
// Prints an error message and exits due to a required thread failing to start.
void ExitFromThreadStartFailure(const std::string& thread_name) {
LOG(FATAL) << "Failed to start " << thread_name;
}
} // namespace
namespace battor {
// Wrapper class containing all state necessary for an independent binary to
// use a BattOrAgent to communicate with a BattOr.
class BattOrAgentBin : public BattOrAgent::Listener {
public:
BattOrAgentBin()
: done_(false, false),
io_thread_(kIoThreadName),
file_thread_(kFileThreadName),
ui_thread_(kUiThreadName) {}
~BattOrAgentBin() { DCHECK(!agent_); }
// Runs the BattOr binary and returns the exit code.
int Run(int argc, char* argv[]) {
std::string cmd = GetArg(1, argc, argv);
if (cmd.empty()) {
PrintUsage();
exit(1);
}
// SupportsExplicitClockSync doesn't need to use the serial connection, so
// handle it separately.
if (cmd == "SupportsExplicitClockSync") {
PrintSupportsExplicitClockSync();
return 0;
}
std::string path = GetArg(2, argc, argv);
// If no path is specified, see if we can find a BattOr and use that.
if (path.empty())
path = BattOrFinder::FindBattOr();
// If we don't have any BattOr to use, exit.
if (path.empty()) {
cout << "Unable to find a BattOr, and no explicit BattOr path was "
"specified."
<< endl;
exit(1);
}
SetUp(path);
if (cmd == "StartTracing") {
StartTracing();
} else if (cmd == "StopTracing") {
StopTracing();
} else if (cmd == "RecordClockSyncMarker") {
// TODO(charliea): Write RecordClockSyncMarker.
} else if (cmd == "IssueClockSyncMarker") {
// TODO(charliea): Write IssueClockSyncMarker.
} else {
TearDown();
PrintUsage();
return 1;
}
TearDown();
return 0;
}
// Performs any setup necessary for the BattOr binary to run.
void SetUp(const std::string& path) {
// TODO(charliea): Investigate whether it's possible to replace this
// separate thread with a combination of MessageLoopForIO and RunLoop.
base::Thread::Options io_thread_options;
io_thread_options.message_loop_type = base::MessageLoopForIO::TYPE_IO;
if (!io_thread_.StartWithOptions(io_thread_options)) {
ExitFromThreadStartFailure(kIoThreadName);
}
io_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&BattOrAgentBin::CreateAgent, base::Unretained(this), path));
CheckError(AwaitResult());
}
// Performs any cleanup necessary after the BattOr binary is done running.
void TearDown() {
io_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&BattOrAgentBin::DeleteAgent, base::Unretained(this)));
CheckError(AwaitResult());
}
void StartTracing() {
io_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&BattOrAgent::StartTracing, base::Unretained(agent_.get())));
CheckError(AwaitResult());
}
void OnStartTracingComplete(BattOrError error) override {
error_ = error;
done_.Signal();
}
void StopTracing() {
io_thread_.task_runner()->PostTask(
FROM_HERE,
base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get())));
CheckError(AwaitResult());
}
void OnStopTracingComplete(const std::string& trace,
BattOrError error) override {
error_ = error;
if (error == BATTOR_ERROR_NONE)
cout << trace << endl;
done_.Signal();
}
// Postable task for creating the BattOrAgent. Because the BattOrAgent has
// uber thread safe dependencies, all interactions with it, including creating
// and deleting it, MUST happen on the IO thread.
void CreateAgent(const std::string& path) {
// In Chrome, we already have file and UI threads running. Because the
// Chrome serial libraries rely on having those threads available, we have
// to spin up our own because we're in a separate binary.
if (!file_thread_.Start())
ExitFromThreadStartFailure(kFileThreadName);
base::Thread::Options ui_thread_options;
ui_thread_options.message_loop_type = base::MessageLoopForIO::TYPE_UI;
if (!ui_thread_.StartWithOptions(ui_thread_options)) {
ExitFromThreadStartFailure(kUiThreadName);
}
agent_.reset(new BattOrAgent(path, this, file_thread_.task_runner(),
ui_thread_.task_runner()));
error_ = BATTOR_ERROR_NONE;
done_.Signal();
}
// Postable task for deleting the BattOrAgent. See the comment for
// CreateAgent() above regarding why this is necessary.
void DeleteAgent() {
agent_.reset(nullptr);
error_ = BATTOR_ERROR_NONE;
done_.Signal();
}
// Waits until the previously executed command has finished executing.
BattOrError AwaitResult() {
if (!done_.TimedWait(
base::TimeDelta::FromSeconds(kBattOrCommandTimeoutSeconds)))
return BATTOR_ERROR_TIMEOUT;
return error_;
}
private:
// Event signaled when an async task has finished executing.
base::WaitableEvent done_;
// The error from the last async command that finished.
BattOrError error_;
// Threads needed for serial communication.
base::Thread io_thread_;
base::Thread file_thread_;
base::Thread ui_thread_;
// The agent capable of asynchronously communicating with the BattOr.
scoped_ptr<BattOrAgent> agent_;
};
} // namespace battor
int main(int argc, char* argv[]) {
base::AtExitManager exit_manager;
battor::BattOrAgentBin bin;
return bin.Run(argc, argv);
}
|