| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file provides a thin binary wrapper around the BattOr Agent | 5 // This file provides a thin binary wrapper around the BattOr Agent |
| 6 // library. This binary wrapper provides a means for non-C++ tracing | 6 // library. This binary wrapper provides a means for non-C++ tracing |
| 7 // controllers, such as Telemetry and Android Systrace, to issue high-level | 7 // controllers, such as Telemetry and Android Systrace, to issue high-level |
| 8 // tracing commands to the BattOr through an interactive shell. | 8 // tracing commands to the BattOr through an interactive shell. |
| 9 // | 9 // |
| 10 // Example usage of how an external trace controller might use this binary: | 10 // Example usage of how an external trace controller might use this binary: |
| (...skipping 123 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 } | 134 } |
| 135 | 135 |
| 136 // Performs any setup necessary for the BattOr binary to run. | 136 // Performs any setup necessary for the BattOr binary to run. |
| 137 void SetUp(const std::string& path) { | 137 void SetUp(const std::string& path) { |
| 138 base::Thread::Options io_thread_options; | 138 base::Thread::Options io_thread_options; |
| 139 io_thread_options.message_loop_type = base::MessageLoopForIO::TYPE_IO; | 139 io_thread_options.message_loop_type = base::MessageLoopForIO::TYPE_IO; |
| 140 if (!io_thread_.StartWithOptions(io_thread_options)) { | 140 if (!io_thread_.StartWithOptions(io_thread_options)) { |
| 141 ExitFromThreadStartFailure(kIoThreadName); | 141 ExitFromThreadStartFailure(kIoThreadName); |
| 142 } | 142 } |
| 143 | 143 |
| 144 // Block until the creation of the BattOrAgent is complete. This doesn't |
| 145 // seem necessary because we're posting the creation to the IO thread |
| 146 // before posting any commands, so we're guaranteed that the creation |
| 147 // will happen first. However, the crashes that happen without this sync |
| 148 // mechanism in place say otherwise. |
| 149 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 150 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 144 io_thread_.task_runner()->PostTask( | 151 io_thread_.task_runner()->PostTask( |
| 145 FROM_HERE, | 152 FROM_HERE, |
| 146 base::Bind(&BattOrAgentBin::CreateAgent, base::Unretained(this), path, | 153 base::Bind(&BattOrAgentBin::CreateAgent, base::Unretained(this), path, |
| 147 base::ThreadTaskRunnerHandle::Get())); | 154 base::ThreadTaskRunnerHandle::Get(), &done)); |
| 155 done.Wait(); |
| 148 } | 156 } |
| 149 | 157 |
| 150 // Performs any cleanup necessary after the BattOr binary is done running. | 158 // Performs any cleanup necessary after the BattOr binary is done running. |
| 151 void TearDown() { | 159 void TearDown() { |
| 152 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, | 160 base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC, |
| 153 base::WaitableEvent::InitialState::NOT_SIGNALED); | 161 base::WaitableEvent::InitialState::NOT_SIGNALED); |
| 154 io_thread_.task_runner()->PostTask( | 162 io_thread_.task_runner()->PostTask( |
| 155 FROM_HERE, base::Bind(&BattOrAgentBin::DeleteAgent, | 163 FROM_HERE, base::Bind(&BattOrAgentBin::DeleteAgent, |
| 156 base::Unretained(this), &done)); | 164 base::Unretained(this), &done)); |
| 157 done.Wait(); | 165 done.Wait(); |
| (...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 HandleError(error); | 273 HandleError(error); |
| 266 | 274 |
| 267 PostRunNextCommand(); | 275 PostRunNextCommand(); |
| 268 } | 276 } |
| 269 | 277 |
| 270 // Postable task for creating the BattOrAgent. Because the BattOrAgent has | 278 // Postable task for creating the BattOrAgent. Because the BattOrAgent has |
| 271 // uber thread safe dependencies, all interactions with it, including creating | 279 // uber thread safe dependencies, all interactions with it, including creating |
| 272 // and deleting it, MUST happen on the IO thread. | 280 // and deleting it, MUST happen on the IO thread. |
| 273 void CreateAgent( | 281 void CreateAgent( |
| 274 const std::string& path, | 282 const std::string& path, |
| 275 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner) { | 283 scoped_refptr<base::SingleThreadTaskRunner> ui_thread_task_runner, |
| 284 base::WaitableEvent* done) { |
| 276 // In Chrome, we already have a file thread running. Because the Chrome | 285 // In Chrome, we already have a file thread running. Because the Chrome |
| 277 // serial library relies on having it available, we have to spin up our own. | 286 // serial library relies on having it available, we have to spin up our own. |
| 278 if (!file_thread_.Start()) | 287 if (!file_thread_.Start()) |
| 279 ExitFromThreadStartFailure(kFileThreadName); | 288 ExitFromThreadStartFailure(kFileThreadName); |
| 280 | 289 |
| 281 agent_.reset(new BattOrAgent(path, this, file_thread_.task_runner(), | 290 agent_.reset(new BattOrAgent(path, this, file_thread_.task_runner(), |
| 282 ui_thread_task_runner)); | 291 ui_thread_task_runner)); |
| 292 done->Signal(); |
| 283 } | 293 } |
| 284 | 294 |
| 285 // Postable task for deleting the BattOrAgent. See the comment for | 295 // Postable task for deleting the BattOrAgent. See the comment for |
| 286 // CreateAgent() above regarding why this is necessary. | 296 // CreateAgent() above regarding why this is necessary. |
| 287 void DeleteAgent(base::WaitableEvent* done) { | 297 void DeleteAgent(base::WaitableEvent* done) { |
| 288 agent_.reset(); | 298 agent_.reset(); |
| 289 done->Signal(); | 299 done->Signal(); |
| 290 } | 300 } |
| 291 | 301 |
| 292 private: | 302 private: |
| (...skipping 14 matching lines...) Expand all Loading... |
| 307 }; | 317 }; |
| 308 | 318 |
| 309 } // namespace battor | 319 } // namespace battor |
| 310 | 320 |
| 311 int main(int argc, char* argv[]) { | 321 int main(int argc, char* argv[]) { |
| 312 base::AtExitManager exit_manager; | 322 base::AtExitManager exit_manager; |
| 313 base::CommandLine::Init(argc, argv); | 323 base::CommandLine::Init(argc, argv); |
| 314 battor::BattOrAgentBin bin; | 324 battor::BattOrAgentBin bin; |
| 315 return bin.Run(argc, argv); | 325 return bin.Run(argc, argv); |
| 316 } | 326 } |
| OLD | NEW |