| 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 |
| 15 void PrintUsage() { | 27 void PrintUsage() { |
| 16 cout << "Usage: battor_agent <command> <arguments>" << endl << endl | 28 cout << "Usage: battor_agent <command> <arguments>" << endl << endl |
| 17 << "Commands:" << endl << endl | 29 << "Commands:" << endl << endl |
| 18 << " StartTracing <path>" << endl | 30 << " StartTracing <path>" << endl |
| 19 << " StopTracing <path>" << endl | 31 << " StopTracing <path>" << endl |
| 20 << " SupportsExplicitClockSync <path>" << endl | 32 << " SupportsExplicitClockSync" << endl |
| 21 << " RecordClockSyncMarker <path> <marker>" << endl | 33 << " RecordClockSyncMarker <path> <marker>" << endl |
| 22 << " IssueClockSyncMarker <path>" << endl | 34 << " IssueClockSyncMarker <path>" << endl |
| 23 << " Help" << endl; | 35 << " Help" << endl; |
| 24 } | 36 } |
| 25 | 37 |
| 26 // Retrieves argument argnum from the argument list and stores it into value, | 38 // Retrieves argument argnum from the argument list and stores it into value, |
| 27 // returning whether the operation was successful and printing the usage | 39 // returning whether the operation was successful and printing the usage |
| 28 // guidelines if it wasn't. | 40 // guidelines if it wasn't. |
| 29 bool GetArg(int argnum, int argc, char* argv[], string* value) { | 41 bool GetArg(int argnum, int argc, char* argv[], string* value) { |
| 30 if (argnum >= argc) { | 42 if (argnum >= argc) { |
| 31 PrintUsage(); | 43 PrintUsage(); |
| 32 return false; | 44 return false; |
| 33 } | 45 } |
| 34 | 46 |
| 35 *value = argv[argnum]; | 47 *value = argv[argnum]; |
| 36 return true; | 48 return true; |
| 37 } | 49 } |
| 38 | 50 |
| 51 void OnCommandComplete() { |
| 52 g_stop_tracing_complete_event.Signal(); |
| 53 } |
| 54 |
| 39 } // namespace | 55 } // namespace |
| 40 | 56 |
| 41 int main(int argc, char* argv[]) { | 57 int main(int argc, char* argv[]) { |
| 42 string cmd; | 58 string cmd; |
| 43 if (!GetArg(1, argc, argv, &cmd)) | 59 if (!GetArg(1, argc, argv, &cmd)) |
| 44 return 1; | 60 return 1; |
| 45 | 61 |
| 46 if (cmd == "StartTracing") { | 62 if (cmd == "StartTracing") { |
| 47 string path; | 63 string path; |
| 48 if (!GetArg(2, argc, argv, &path)) | 64 if (!GetArg(2, argc, argv, &path)) |
| 49 return 1; | 65 return 1; |
| 50 | 66 |
| 51 cout << "Calling StartTracing()" << endl; | 67 cout << "Calling StartTracing()" << endl; |
| 52 battor::BattOrAgent(path).StartTracing(); | 68 battor::BattOrAgent(path).StartTracing(); |
| 53 } else if (cmd == "StopTracing") { | 69 } else if (cmd == "StopTracing") { |
| 54 string path; | 70 string path; |
| 55 if (!GetArg(2, argc, argv, &path)) | 71 if (!GetArg(2, argc, argv, &path)) |
| 56 return 1; | 72 return 1; |
| 57 | 73 |
| 58 string out_trace; | |
| 59 cout << "Calling StopTracing()" << endl; | 74 cout << "Calling StopTracing()" << endl; |
| 60 battor::BattOrAgent(path).StopTracing(&out_trace); | 75 std::string trace_output; |
| 61 cout << out_trace << endl; | 76 battor::BattOrAgent(path) |
| 77 .StopTracing(&trace_output, base::Bind(&OnCommandComplete)); |
| 78 g_stop_tracing_complete_event.Wait(); |
| 79 cout << trace_output << endl; |
| 62 } else if (cmd == "SupportsExplicitClockSync") { | 80 } else if (cmd == "SupportsExplicitClockSync") { |
| 63 cout << "Calling SupportsExplicitClockSync" << endl; | 81 cout << "Calling SupportsExplicitClockSync" << endl; |
| 64 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; | 82 cout << battor::BattOrAgent::SupportsExplicitClockSync() << endl; |
| 65 } else if (cmd == "RecordClockSyncMarker") { | 83 } else if (cmd == "RecordClockSyncMarker") { |
| 66 string path, marker; | 84 string path, marker; |
| 67 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) | 85 if (!GetArg(2, argc, argv, &path) || !GetArg(3, argc, argv, &marker)) |
| 68 return 1; | 86 return 1; |
| 69 | 87 |
| 70 cout << "Marker: " << marker << endl; | 88 cout << "Marker: " << marker << endl; |
| 71 cout << "Calling RecordClockSyncMarker()" << endl; | 89 cout << "Calling RecordClockSyncMarker()" << endl; |
| 72 battor::BattOrAgent(path).RecordClockSyncMarker(marker); | 90 // TODO(charliea): Write the time to STDOUT |
| 91 battor::BattOrAgent(path) |
| 92 .RecordClockSyncMarker(marker, base::Bind(&OnCommandComplete)); |
| 93 g_stop_tracing_complete_event.Wait(); |
| 94 // TODO(charliea): Write the time to STDOUT |
| 73 } else if (cmd == "IssueClockSyncMarker") { | 95 } else if (cmd == "IssueClockSyncMarker") { |
| 74 string path; | 96 string path; |
| 75 if (!GetArg(2, argc, argv, &path)) | 97 if (!GetArg(2, argc, argv, &path)) |
| 76 return 1; | 98 return 1; |
| 77 | 99 |
| 78 cout << "Calling IssueClockSyncMarker" << endl; | 100 cout << "Calling IssueClockSyncMarker" << endl; |
| 79 battor::BattOrAgent(path).IssueClockSyncMarker(); | 101 battor::BattOrAgent(path).IssueClockSyncMarker(); |
| 80 } else { | 102 } else { |
| 81 PrintUsage(); | 103 PrintUsage(); |
| 82 return 1; | 104 return 1; |
| 83 } | 105 } |
| 84 | 106 |
| 85 return 0; | 107 return 0; |
| 86 } | 108 } |
| OLD | NEW |