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 | |
| 6 // library. This binary wrapper provides a means for non-C++ tracing | |
| 7 // controllers, such as Telemetry and Android Systrace, to issue high-level | |
| 8 // tracing commands to the BattOr.. | |
| 9 | |
| 5 #include <iostream> | 10 #include <iostream> |
| 6 | 11 |
| 12 #include "base/bind.h" | |
| 13 #include "base/memory/scoped_ptr.h" | |
| 14 #include "base/synchronization/waitable_event.h" | |
| 7 #include "tools/battor_agent/battor_agent.h" | 15 #include "tools/battor_agent/battor_agent.h" |
| 8 | 16 |
| 9 using std::cout; | 17 using std::cout; |
| 10 using std::endl; | 18 using std::endl; |
| 11 using std::string; | 19 using std::string; |
| 12 | 20 |
| 13 namespace { | 21 namespace { |
| 14 | 22 |
| 23 // An event used to signal that the BattOr Agent has finished executing its | |
| 24 // command. | |
| 25 base::WaitableEvent g_stop_tracing_complete_event(false, false); | |
| 26 | |
| 27 // A string that contains the trace that the BattOr has finished recording. | |
| 28 scoped_ptr<std::string> g_trace_output; | |
|
Zhen Wang
2015/10/30 16:46:44
Remove this?
charliea (OOO until 10-5)
2015/10/30 17:37:42
Ack. Sorry about that - not sure how that slipped
| |
| 29 | |
| 15 void PrintUsage() { | 30 void PrintUsage() { |
| 16 cout << "Usage: battor_agent <command> <arguments>" << endl << endl | 31 cout << "Usage: battor_agent <command> <arguments>" << endl << endl |
| 17 << "Commands:" << endl << endl | 32 << "Commands:" << endl << endl |
| 18 << " StartTracing <path>" << endl | 33 << " StartTracing <path>" << endl |
| 19 << " StopTracing <path>" << endl | 34 << " StopTracing <path>" << endl |
| 20 << " SupportsExplicitClockSync <path>" << endl | 35 << " SupportsExplicitClockSync" << endl |
| 21 << " RecordClockSyncMarker <path> <marker>" << endl | 36 << " RecordClockSyncMarker <path> <marker>" << endl |
| 22 << " IssueClockSyncMarker <path>" << endl | 37 << " IssueClockSyncMarker <path>" << endl |
| 23 << " Help" << endl; | 38 << " Help" << endl; |
| 24 } | 39 } |
| 25 | 40 |
| 26 // Retrieves argument argnum from the argument list and stores it into value, | 41 // Retrieves argument argnum from the argument list and stores it into value, |
| 27 // returning whether the operation was successful and printing the usage | 42 // returning whether the operation was successful and printing the usage |
| 28 // guidelines if it wasn't. | 43 // guidelines if it wasn't. |
| 29 bool GetArg(int argnum, int argc, char* argv[], string* value) { | 44 bool GetArg(int argnum, int argc, char* argv[], string* value) { |
| 30 if (argnum >= argc) { | 45 if (argnum >= argc) { |
| 31 PrintUsage(); | 46 PrintUsage(); |
| 32 return false; | 47 return false; |
| 33 } | 48 } |
| 34 | 49 |
| 35 *value = argv[argnum]; | 50 *value = argv[argnum]; |
| 36 return true; | 51 return true; |
| 37 } | 52 } |
| 38 | 53 |
| 54 void OnCommandComplete() { | |
| 55 g_stop_tracing_complete_event.Signal(); | |
| 56 } | |
| 57 | |
| 39 } // namespace | 58 } // namespace |
| 40 | 59 |
| 41 int main(int argc, char* argv[]) { | 60 int main(int argc, char* argv[]) { |
| 42 string cmd; | 61 string cmd; |
| 43 if (!GetArg(1, argc, argv, &cmd)) | 62 if (!GetArg(1, argc, argv, &cmd)) |
| 44 return 1; | 63 return 1; |
| 45 | 64 |
| 46 if (cmd == "StartTracing") { | 65 if (cmd == "StartTracing") { |
| 47 string path; | 66 string path; |
| 48 if (!GetArg(2, argc, argv, &path)) | 67 if (!GetArg(2, argc, argv, &path)) |
| 49 return 1; | 68 return 1; |
| 50 | 69 |
| 51 cout << "Calling StartTracing()" << endl; | 70 cout << "Calling StartTracing()" << endl; |
| 52 battor::BattOrAgent(path).StartTracing(); | 71 battor::BattOrAgent(path).StartTracing(); |
| 53 } else if (cmd == "StopTracing") { | 72 } else if (cmd == "StopTracing") { |
| 54 string path; | 73 string path; |
| 55 if (!GetArg(2, argc, argv, &path)) | 74 if (!GetArg(2, argc, argv, &path)) |
| 56 return 1; | 75 return 1; |
| 57 | 76 |
| 58 string out_trace; | |
| 59 cout << "Calling StopTracing()" << endl; | 77 cout << "Calling StopTracing()" << endl; |
| 60 battor::BattOrAgent(path).StopTracing(&out_trace); | 78 std::string trace_output; |
| 61 cout << out_trace << endl; | 79 battor::BattOrAgent(path) |
| 80 .StopTracing(&trace_output, base::Bind(&OnCommandComplete)); | |
| 81 g_stop_tracing_complete_event.Wait(); | |
| 82 cout << trace_output << endl; | |
| 62 } else if (cmd == "SupportsExplicitClockSync") { | 83 } else if (cmd == "SupportsExplicitClockSync") { |
| 63 cout << "Calling SupportsExplicitClockSync" << endl; | 84 cout << "Calling SupportsExplicitClockSync" << endl; |
| 64 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; | 85 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; |
| 65 } else if (cmd == "RecordClockSyncMarker") { | 86 } else if (cmd == "RecordClockSyncMarker") { |
| 66 string path, marker; | 87 string path, marker; |
| 67 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) | 88 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) |
| 68 return 1; | 89 return 1; |
| 69 | 90 |
| 70 cout << "Marker: " << marker << endl; | 91 cout << "Marker: " << marker << endl; |
| 71 cout << "Calling RecordClockSyncMarker()" << endl; | 92 cout << "Calling RecordClockSyncMarker()" << endl; |
| 72 battor::BattOrAgent(path).RecordClockSyncMarker(marker); | 93 // TODO: Write the time to STDOUT |
| 94 battor::BattOrAgent(path) | |
| 95 .RecordClockSyncMarker(marker, base::Bind(&OnCommandComplete)); | |
| 96 g_stop_tracing_complete_event.Wait(); | |
| 97 // TODO: Write the time to STDOUT | |
| 73 } else if (cmd == "IssueClockSyncMarker") { | 98 } else if (cmd == "IssueClockSyncMarker") { |
| 74 string path; | 99 string path; |
| 75 if (!GetArg(2, argc, argv, &path)) | 100 if (!GetArg(2, argc, argv, &path)) |
| 76 return 1; | 101 return 1; |
| 77 | 102 |
| 78 cout << "Calling IssueClockSyncMarker" << endl; | 103 cout << "Calling IssueClockSyncMarker" << endl; |
| 79 battor::BattOrAgent(path).IssueClockSyncMarker(); | 104 battor::BattOrAgent(path).IssueClockSyncMarker(); |
| 80 } else { | 105 } else { |
| 81 PrintUsage(); | 106 PrintUsage(); |
| 82 return 1; | 107 return 1; |
| 83 } | 108 } |
| 84 | 109 |
| 85 return 0; | 110 return 0; |
| 86 } | 111 } |
| OLD | NEW |