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\n" |
|
rnephew (Reviews Here)
2016/03/18 22:21:27
Locally already changed to read:
" StopTracing <O
charliea (OOO until 10-5)
2016/03/21 15:19:15
Acknowledged.
| |
| 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 |
| 81 const std::string* mDumpPath = nullptr; | |
|
rnephew (Reviews Here)
2016/03/18 22:21:27
I know this is named wrong, but not sure what is r
charliea (OOO until 10-5)
2016/03/21 15:19:15
What do you think about the name "trace output fil
charliea (OOO until 10-5)
2016/03/21 15:19:15
This should go down with the other member variable
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 82 | |
| 80 void PrintSupportsExplicitClockSync() { | 83 void PrintSupportsExplicitClockSync() { |
| 81 std::cout << BattOrAgent::SupportsExplicitClockSync() << endl; | 84 std::cout << BattOrAgent::SupportsExplicitClockSync() << endl; |
| 82 } | 85 } |
| 83 | 86 |
| 84 // Logs the error and exits with an error code. | 87 // Logs the error and exits with an error code. |
| 85 void HandleError(battor::BattOrError error) { | 88 void HandleError(battor::BattOrError error) { |
| 86 if (error != BATTOR_ERROR_NONE) | 89 if (error != BATTOR_ERROR_NONE) |
| 87 LOG(FATAL) << "Fatal error when communicating with the BattOr: " | 90 LOG(FATAL) << "Fatal error when communicating with the BattOr: " |
| 88 << BattOrErrorToString(error); | 91 << BattOrErrorToString(error); |
| 89 } | 92 } |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 118 } | 121 } |
| 119 | 122 |
| 120 SetUp(path); | 123 SetUp(path); |
| 121 | 124 |
| 122 std::string cmd; | 125 std::string cmd; |
| 123 for (;;) { | 126 for (;;) { |
| 124 std::getline(std::cin, cmd); | 127 std::getline(std::cin, cmd); |
| 125 | 128 |
| 126 if (cmd == "StartTracing") { | 129 if (cmd == "StartTracing") { |
| 127 StartTracing(); | 130 StartTracing(); |
| 128 } else if (cmd == "StopTracing") { | 131 |
|
rnephew (Reviews Here)
2016/03/18 22:21:27
Already deleted this locally.
charliea (OOO until 10-5)
2016/03/21 15:19:16
Acknowledged.
| |
| 129 StopTracing(); | 132 } else if (cmd.find("StopTracing") != std::string::npos) { |
| 133 base::StringTokenizer tokenizer(cmd, " "); | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:16
Could you abstract the splitting of the command in
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 134 std::vector<std::string> tokens; | |
| 135 while (tokenizer.GetNext()) | |
| 136 tokens.push_back(tokenizer.token()); | |
| 137 | |
| 138 if (tokens.size() == 1 && tokens[0] == "StopTracing") { | |
| 139 // No path given. | |
| 140 StopTracing(); | |
| 141 } else if (tokens.size() == 2 && tokens[0] == "StopTracing") { | |
| 142 // Path given. | |
| 143 StopTracing(tokens[1]); | |
| 144 } else { | |
| 145 std::cout << "Invalid StopTracing command." << endl; | |
| 146 std::cout << kUsage << endl; | |
| 147 continue; | |
| 148 } | |
| 130 break; | 149 break; |
| 150 | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:15
nit: remove extra line
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 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) { |
| 134 base::StringTokenizer tokenizer(cmd, " "); | 154 base::StringTokenizer tokenizer(cmd, " "); |
| 135 | 155 |
| 136 std::vector<std::string> tokens; | 156 std::vector<std::string> tokens; |
| 137 while (tokenizer.GetNext()) | 157 while (tokenizer.GetNext()) |
| 138 tokens.push_back(tokenizer.token()); | 158 tokens.push_back(tokenizer.token()); |
| 139 | 159 |
| 140 if (tokens.size() != 2 || tokens[0] != "RecordClockSyncMarker") { | 160 if (tokens.size() != 2 || tokens[0] != "RecordClockSyncMarker") { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 | 208 |
| 189 void OnStartTracingComplete(BattOrError error) override { | 209 void OnStartTracingComplete(BattOrError error) override { |
| 190 if (error == BATTOR_ERROR_NONE) | 210 if (error == BATTOR_ERROR_NONE) |
| 191 std::cout << "Done." << endl; | 211 std::cout << "Done." << endl; |
| 192 else | 212 else |
| 193 HandleError(error); | 213 HandleError(error); |
| 194 | 214 |
| 195 done_.Signal(); | 215 done_.Signal(); |
| 196 } | 216 } |
| 197 | 217 |
| 218 void StopTracing(const std::string&path) { | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:16
nit: should be a space between & and path. Also, i
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 219 mDumpPath = &path; | |
| 220 StopTracing(); | |
| 221 } | |
| 222 | |
| 198 void StopTracing() { | 223 void StopTracing() { |
| 199 io_thread_.task_runner()->PostTask( | 224 io_thread_.task_runner()->PostTask( |
| 200 FROM_HERE, | 225 FROM_HERE, |
| 201 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get()))); | 226 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get()))); |
| 202 AwaitResult(); | 227 AwaitResult(); |
| 203 } | 228 } |
| 204 | 229 |
| 205 void OnStopTracingComplete(const std::string& trace, | 230 void OnStopTracingComplete(const std::string& trace, |
| 206 BattOrError error) override { | 231 BattOrError error) override { |
| 207 if (error == BATTOR_ERROR_NONE) { | 232 if (error == BATTOR_ERROR_NONE) { |
| 208 std::cout << trace; | 233 if (mDumpPath) { |
| 234 std::ofstream traceStream; | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:15
nit: variable names are underscore delimited (not
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 235 traceStream.open(*mDumpPath); | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:15
I think that the above two lines can be written as
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 236 traceStream << trace; | |
|
charliea (OOO until 10-5)
2016/03/21 15:19:16
Just about any time that you're opening and writin
rnephew (Reviews Here)
2016/03/21 16:40:27
Done.
| |
| 237 traceStream.close(); | |
| 238 } else { | |
| 239 std::cout << trace; | |
| 240 } | |
| 209 std::cout << "Done." << endl; | 241 std::cout << "Done." << endl; |
| 210 } else { | 242 } else { |
| 211 HandleError(error); | 243 HandleError(error); |
| 212 } | 244 } |
| 213 | 245 |
| 214 done_.Signal(); | 246 done_.Signal(); |
| 215 } | 247 } |
| 216 | 248 |
| 217 void RecordClockSyncMarker(const std::string& marker) { | 249 void RecordClockSyncMarker(const std::string& marker) { |
| 218 io_thread_.task_runner()->PostTask( | 250 io_thread_.task_runner()->PostTask( |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 279 }; | 311 }; |
| 280 | 312 |
| 281 } // namespace battor | 313 } // namespace battor |
| 282 | 314 |
| 283 int main(int argc, char* argv[]) { | 315 int main(int argc, char* argv[]) { |
| 284 base::AtExitManager exit_manager; | 316 base::AtExitManager exit_manager; |
| 285 base::CommandLine::Init(argc, argv); | 317 base::CommandLine::Init(argc, argv); |
| 286 battor::BattOrAgentBin bin; | 318 battor::BattOrAgentBin bin; |
| 287 return bin.Run(argc, argv); | 319 return bin.Run(argc, argv); |
| 288 } | 320 } |
| OLD | NEW |