Chromium Code Reviews| 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 15 matching lines...) Expand all Loading... | |
| 26 // sync end timestamp and sends the subprocess the StopTracing message via | 26 // sync end timestamp and sends the subprocess the StopTracing message via |
| 27 // STDIN | 27 // STDIN |
| 28 // 9) PowerTracingAgent continues to read trace output lines from STDOUT until | 28 // 9) PowerTracingAgent continues to read trace output lines from STDOUT until |
| 29 // the binary exits with an exit code of 1 (indicating failure) or the | 29 // the binary exits with an exit code of 1 (indicating failure) or the |
| 30 // 'Done.' line is printed to STDOUT, signaling the last line of the trace | 30 // 'Done.' line is printed to STDOUT, signaling the last line of the trace |
| 31 // 10) PowerTracingAgent returns the battery trace to the Telemetry trace | 31 // 10) PowerTracingAgent returns the battery trace to the Telemetry trace |
| 32 // controller | 32 // controller |
| 33 | 33 |
| 34 #include <stdint.h> | 34 #include <stdint.h> |
| 35 | 35 |
| 36 #include <fstream> | |
| 36 #include <iostream> | 37 #include <iostream> |
| 37 | 38 |
| 38 #include "base/at_exit.h" | 39 #include "base/at_exit.h" |
| 39 #include "base/bind.h" | 40 #include "base/bind.h" |
| 40 #include "base/bind_helpers.h" | 41 #include "base/bind_helpers.h" |
| 41 #include "base/command_line.h" | 42 #include "base/command_line.h" |
| 42 #include "base/location.h" | 43 #include "base/location.h" |
| 43 #include "base/logging.h" | 44 #include "base/logging.h" |
| 44 #include "base/strings/string_tokenizer.h" | 45 #include "base/strings/string_tokenizer.h" |
| 45 #include "base/strings/utf_string_conversions.h" | 46 #include "base/strings/utf_string_conversions.h" |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 63 "Start the battor_agent shell with:\n" | 64 "Start the battor_agent shell with:\n" |
| 64 "\n" | 65 "\n" |
| 65 " battor_agent <switches>\n" | 66 " battor_agent <switches>\n" |
| 66 "\n" | 67 "\n" |
| 67 "Switches: \n" | 68 "Switches: \n" |
| 68 " --battor-path=<path> Uses the specified BattOr path.\n" | 69 " --battor-path=<path> Uses the specified BattOr path.\n" |
| 69 "\n" | 70 "\n" |
| 70 "Once in the shell, you can issue the following commands:\n" | 71 "Once in the shell, you can issue the following commands:\n" |
| 71 "\n" | 72 "\n" |
| 72 " StartTracing\n" | 73 " StartTracing\n" |
| 73 " StopTracing\n" | 74 " StopTracing <optional file path>\n" |
| 74 " SupportsExplicitClockSync\n" | 75 " SupportsExplicitClockSync\n" |
| 75 " RecordClockSyncMarker <marker>\n" | 76 " RecordClockSyncMarker <marker>\n" |
| 76 " Exit\n" | 77 " Exit\n" |
| 77 " Help\n" | 78 " Help\n" |
| 78 "\n"; | 79 "\n"; |
| 79 | 80 |
| 80 void PrintSupportsExplicitClockSync() { | 81 void PrintSupportsExplicitClockSync() { |
| 81 std::cout << BattOrAgent::SupportsExplicitClockSync() << endl; | 82 std::cout << BattOrAgent::SupportsExplicitClockSync() << endl; |
| 82 } | 83 } |
| 83 | 84 |
| 84 // Logs the error and exits with an error code. | 85 // Logs the error and exits with an error code. |
| 85 void HandleError(battor::BattOrError error) { | 86 void HandleError(battor::BattOrError error) { |
| 86 if (error != BATTOR_ERROR_NONE) | 87 if (error != BATTOR_ERROR_NONE) |
| 87 LOG(FATAL) << "Fatal error when communicating with the BattOr: " | 88 LOG(FATAL) << "Fatal error when communicating with the BattOr: " |
| 88 << BattOrErrorToString(error); | 89 << BattOrErrorToString(error); |
| 89 } | 90 } |
| 90 | 91 |
| 91 // Prints an error message and exits due to a required thread failing to start. | 92 // Prints an error message and exits due to a required thread failing to start. |
| 92 void ExitFromThreadStartFailure(const std::string& thread_name) { | 93 void ExitFromThreadStartFailure(const std::string& thread_name) { |
| 93 LOG(FATAL) << "Failed to start " << thread_name; | 94 LOG(FATAL) << "Failed to start " << thread_name; |
| 94 } | 95 } |
| 95 | 96 |
| 97 std::vector<std::string> TokenizeCommand(std::string cmd) { | |
|
alexandermont
2016/03/23 22:56:53
The name of this function could be more general, l
rnephew (Reviews Here)
2016/03/24 00:11:26
This will only ever be used to tokenize command in
charliea (OOO until 10-5)
2016/03/24 15:05:21
I don't have any strong opinion here. I'm fine wit
rnephew (Reviews Here)
2016/03/24 15:15:47
Switched to String
| |
| 98 base::StringTokenizer tokenizer(cmd, " "); | |
| 99 std::vector<std::string> tokens; | |
| 100 while (tokenizer.GetNext()) | |
| 101 tokens.push_back(tokenizer.token()); | |
| 102 return tokens; | |
| 103 } | |
| 104 | |
| 96 } // namespace | 105 } // namespace |
| 97 | 106 |
| 98 // Wrapper class containing all state necessary for an independent binary to | 107 // Wrapper class containing all state necessary for an independent binary to |
| 99 // use a BattOrAgent to communicate with a BattOr. | 108 // use a BattOrAgent to communicate with a BattOr. |
| 100 class BattOrAgentBin : public BattOrAgent::Listener { | 109 class BattOrAgentBin : public BattOrAgent::Listener { |
| 101 public: | 110 public: |
| 102 BattOrAgentBin() | 111 BattOrAgentBin() |
| 103 : done_(false, false), | 112 : done_(false, false), |
| 104 io_thread_(kIoThreadName), | 113 io_thread_(kIoThreadName), |
| 105 file_thread_(kFileThreadName), | 114 file_thread_(kFileThreadName), |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 118 } | 127 } |
| 119 | 128 |
| 120 SetUp(path); | 129 SetUp(path); |
| 121 | 130 |
| 122 std::string cmd; | 131 std::string cmd; |
| 123 for (;;) { | 132 for (;;) { |
| 124 std::getline(std::cin, cmd); | 133 std::getline(std::cin, cmd); |
| 125 | 134 |
| 126 if (cmd == "StartTracing") { | 135 if (cmd == "StartTracing") { |
| 127 StartTracing(); | 136 StartTracing(); |
| 128 } else if (cmd == "StopTracing") { | 137 } else if (cmd.find("StopTracing") != std::string::npos) { |
|
alexandermont
2016/03/23 22:56:53
This just tests if "StopTracing" is in the string
rnephew (Reviews Here)
2016/03/24 00:11:26
Later it does make sure the first word is STopTrac
| |
| 129 StopTracing(); | 138 std::vector<std::string> tokens = TokenizeCommand(cmd); |
| 139 if (tokens.size() == 1 && tokens[0] == "StopTracing") { | |
| 140 // No path given. | |
| 141 StopTracing(); | |
| 142 } else if (tokens.size() == 2 && tokens[0] == "StopTracing") { | |
| 143 // Path given. | |
| 144 StopTracing(tokens[1]); | |
| 145 } else { | |
| 146 std::cout << "Invalid StopTracing command." << endl; | |
| 147 std::cout << kUsage << endl; | |
| 148 continue; | |
| 149 } | |
| 130 break; | 150 break; |
| 131 } else if (cmd == "SupportsExplicitClockSync") { | 151 } else if (cmd == "SupportsExplicitClockSync") { |
| 132 PrintSupportsExplicitClockSync(); | 152 PrintSupportsExplicitClockSync(); |
| 133 } else if (cmd.find("RecordClockSyncMarker") != std::string::npos) { | 153 } else if (cmd.find("RecordClockSyncMarker") != std::string::npos) { |
|
alexandermont
2016/03/23 22:56:53
Same here; this just tests if "RecordClockSyncMark
rnephew (Reviews Here)
2016/03/24 00:11:26
Same as above.
| |
| 134 base::StringTokenizer tokenizer(cmd, " "); | 154 std::vector<std::string> tokens = TokenizeCommand(cmd); |
| 135 | |
| 136 std::vector<std::string> tokens; | |
| 137 while (tokenizer.GetNext()) | |
| 138 tokens.push_back(tokenizer.token()); | |
| 139 | |
| 140 if (tokens.size() != 2 || tokens[0] != "RecordClockSyncMarker") { | 155 if (tokens.size() != 2 || tokens[0] != "RecordClockSyncMarker") { |
| 141 std::cout << "Invalid RecordClockSyncMarker command." << endl; | 156 std::cout << "Invalid RecordClockSyncMarker command." << endl; |
| 142 std::cout << kUsage << endl; | 157 std::cout << kUsage << endl; |
| 143 continue; | 158 continue; |
| 144 } | 159 } |
| 145 | 160 |
| 146 RecordClockSyncMarker(tokens[1]); | 161 RecordClockSyncMarker(tokens[1]); |
| 147 } else if (cmd == "Exit") { | 162 } else if (cmd == "Exit") { |
| 148 break; | 163 break; |
| 149 } else { | 164 } else { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 | 203 |
| 189 void OnStartTracingComplete(BattOrError error) override { | 204 void OnStartTracingComplete(BattOrError error) override { |
| 190 if (error == BATTOR_ERROR_NONE) | 205 if (error == BATTOR_ERROR_NONE) |
| 191 std::cout << "Done." << endl; | 206 std::cout << "Done." << endl; |
| 192 else | 207 else |
| 193 HandleError(error); | 208 HandleError(error); |
| 194 | 209 |
| 195 done_.Signal(); | 210 done_.Signal(); |
| 196 } | 211 } |
| 197 | 212 |
| 198 void StopTracing() { | 213 void StopTracing(const std::string& path = "") { |
| 214 trace_output_file_ = path; | |
| 199 io_thread_.task_runner()->PostTask( | 215 io_thread_.task_runner()->PostTask( |
| 200 FROM_HERE, | 216 FROM_HERE, |
| 201 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get()))); | 217 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get()))); |
| 202 AwaitResult(); | 218 AwaitResult(); |
|
charliea (OOO until 10-5)
2016/03/24 15:05:20
After AwaitResult(), I'd do:
trace_output_file_ =
rnephew (Reviews Here)
2016/03/24 15:15:47
Done.
| |
| 203 } | 219 } |
| 204 | 220 |
| 205 void OnStopTracingComplete(const std::string& trace, | 221 void OnStopTracingComplete(const std::string& trace, |
| 206 BattOrError error) override { | 222 BattOrError error) override { |
| 207 if (error == BATTOR_ERROR_NONE) { | 223 if (error == BATTOR_ERROR_NONE) { |
| 208 std::cout << trace; | 224 if (trace_output_file_.empty()) { |
| 225 std::cout << trace; | |
| 226 } | |
| 227 else { | |
| 228 std::ofstream trace_stream(trace_output_file_); | |
| 229 if (!trace_stream.is_open()) { | |
| 230 std::cout << "Tracing output file could not be opened." << endl; | |
| 231 exit(1); | |
| 232 } | |
| 233 trace_stream << trace; | |
| 234 trace_stream.close(); | |
| 235 } | |
| 209 std::cout << "Done." << endl; | 236 std::cout << "Done." << endl; |
| 210 } else { | 237 } else { |
| 211 HandleError(error); | 238 HandleError(error); |
| 212 } | 239 } |
| 213 | 240 |
| 214 done_.Signal(); | 241 done_.Signal(); |
| 215 } | 242 } |
| 216 | 243 |
| 217 void RecordClockSyncMarker(const std::string& marker) { | 244 void RecordClockSyncMarker(const std::string& marker) { |
| 218 io_thread_.task_runner()->PostTask( | 245 io_thread_.task_runner()->PostTask( |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 269 // Event signaled when an async task has finished executing. | 296 // Event signaled when an async task has finished executing. |
| 270 base::WaitableEvent done_; | 297 base::WaitableEvent done_; |
| 271 | 298 |
| 272 // Threads needed for serial communication. | 299 // Threads needed for serial communication. |
| 273 base::Thread io_thread_; | 300 base::Thread io_thread_; |
| 274 base::Thread file_thread_; | 301 base::Thread file_thread_; |
| 275 base::Thread ui_thread_; | 302 base::Thread ui_thread_; |
| 276 | 303 |
| 277 // The agent capable of asynchronously communicating with the BattOr. | 304 // The agent capable of asynchronously communicating with the BattOr. |
| 278 scoped_ptr<BattOrAgent> agent_; | 305 scoped_ptr<BattOrAgent> agent_; |
| 306 | |
| 307 std::string trace_output_file_; | |
| 308 | |
| 279 }; | 309 }; |
| 280 | 310 |
| 281 } // namespace battor | 311 } // namespace battor |
| 282 | 312 |
| 283 int main(int argc, char* argv[]) { | 313 int main(int argc, char* argv[]) { |
| 284 base::AtExitManager exit_manager; | 314 base::AtExitManager exit_manager; |
| 285 base::CommandLine::Init(argc, argv); | 315 base::CommandLine::Init(argc, argv); |
| 286 battor::BattOrAgentBin bin; | 316 battor::BattOrAgentBin bin; |
| 287 return bin.Run(argc, argv); | 317 return bin.Run(argc, argv); |
| 288 } | 318 } |
| OLD | NEW |