Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(23)

Side by Side Diff: tools/battor_agent/battor_agent_bin.cc

Issue 1819573002: [Battor] Add ability to dump trace log to file. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Charlies review Created 4 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
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"
charliea (OOO until 10-5) 2016/03/22 17:45:26 nit: "Optional" -> "optional" to match <marker> be
rnephew (Reviews Here) 2016/03/22 18:05:46 Done.
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) {
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
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) {
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) {
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
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
213 void StopTracing(const std::string& path) {
214 trace_output_file_ = &path;
charliea (OOO until 10-5) 2016/03/22 17:45:26 This is a scary line - you're taking the address o
rnephew (Reviews Here) 2016/03/22 18:05:46 Done.
215 StopTracing();
216 }
217
198 void StopTracing() { 218 void StopTracing() {
199 io_thread_.task_runner()->PostTask( 219 io_thread_.task_runner()->PostTask(
200 FROM_HERE, 220 FROM_HERE,
201 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get()))); 221 base::Bind(&BattOrAgent::StopTracing, base::Unretained(agent_.get())));
202 AwaitResult(); 222 AwaitResult();
203 } 223 }
204 224
205 void OnStopTracingComplete(const std::string& trace, 225 void OnStopTracingComplete(const std::string& trace,
206 BattOrError error) override { 226 BattOrError error) override {
207 if (error == BATTOR_ERROR_NONE) { 227 if (error == BATTOR_ERROR_NONE) {
208 std::cout << trace; 228 if (trace_output_file_) {
charliea (OOO until 10-5) 2016/03/22 17:45:26 If we do the above, this will have to be changed t
rnephew (Reviews Here) 2016/03/22 18:05:46 Done.
229 std::ofstream trace_stream(*trace_output_file_);
230 if (!trace_stream.is_open()) {
231 std::cout << "Tracing output file could not be opened." << endl;
232 exit(1);
233 }
234 trace_stream << trace;
235 trace_stream.close();
236 } else {
237 std::cout << trace;
238 }
209 std::cout << "Done." << endl; 239 std::cout << "Done." << endl;
210 } else { 240 } else {
211 HandleError(error); 241 HandleError(error);
212 } 242 }
213 243
214 done_.Signal(); 244 done_.Signal();
215 } 245 }
216 246
217 void RecordClockSyncMarker(const std::string& marker) { 247 void RecordClockSyncMarker(const std::string& marker) {
218 io_thread_.task_runner()->PostTask( 248 io_thread_.task_runner()->PostTask(
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 // Event signaled when an async task has finished executing. 299 // Event signaled when an async task has finished executing.
270 base::WaitableEvent done_; 300 base::WaitableEvent done_;
271 301
272 // Threads needed for serial communication. 302 // Threads needed for serial communication.
273 base::Thread io_thread_; 303 base::Thread io_thread_;
274 base::Thread file_thread_; 304 base::Thread file_thread_;
275 base::Thread ui_thread_; 305 base::Thread ui_thread_;
276 306
277 // The agent capable of asynchronously communicating with the BattOr. 307 // The agent capable of asynchronously communicating with the BattOr.
278 scoped_ptr<BattOrAgent> agent_; 308 scoped_ptr<BattOrAgent> agent_;
309
310 const std::string* trace_output_file_ = nullptr;
charliea (OOO until 10-5) 2016/03/22 17:45:26 As noted above, I think it makes sense to change t
rnephew (Reviews Here) 2016/03/22 18:05:46 Done.
311
279 }; 312 };
280 313
281 } // namespace battor 314 } // namespace battor
282 315
283 int main(int argc, char* argv[]) { 316 int main(int argc, char* argv[]) {
284 base::AtExitManager exit_manager; 317 base::AtExitManager exit_manager;
285 base::CommandLine::Init(argc, argv); 318 base::CommandLine::Init(argc, argv);
286 battor::BattOrAgentBin bin; 319 battor::BattOrAgentBin bin;
287 return bin.Run(argc, argv); 320 return bin.Run(argc, argv);
288 } 321 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698