| 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 #include <memory> | 5 #include <memory> |
| 6 #include <set> | 6 #include <set> |
| 7 #include <string> | 7 #include <string> |
| 8 #include <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "apps/benchmark/event.h" | 10 #include "apps/benchmark/event.h" |
| 11 #include "apps/benchmark/measurements.h" | 11 #include "apps/benchmark/measurements.h" |
| 12 #include "apps/benchmark/run_args.h" | 12 #include "apps/benchmark/run_args.h" |
| 13 #include "apps/benchmark/trace_collector_client.h" | 13 #include "apps/benchmark/trace_collector_client.h" |
| 14 #include "base/bind.h" | 14 #include "base/bind.h" |
| 15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/strings/string_split.h" | 18 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 19 #include "base/strings/string_util.h" |
| 20 #include "base/time/time.h" | 20 #include "base/time/time.h" |
| 21 #include "base/trace_event/trace_event.h" | 21 #include "base/trace_event/trace_event.h" |
| 22 #include "mojo/application/application_runner_chromium.h" | 22 #include "mojo/environment/scoped_chromium_init.h" |
| 23 #include "mojo/public/c/system/main.h" | 23 #include "mojo/public/c/system/main.h" |
| 24 #include "mojo/public/cpp/application/application_delegate.h" | 24 #include "mojo/public/cpp/application/application_impl_base.h" |
| 25 #include "mojo/public/cpp/application/application_impl.h" | |
| 26 #include "mojo/public/cpp/application/connect.h" | 25 #include "mojo/public/cpp/application/connect.h" |
| 26 #include "mojo/public/cpp/application/run_application.h" |
| 27 #include "mojo/public/cpp/bindings/interface_handle.h" | 27 #include "mojo/public/cpp/bindings/interface_handle.h" |
| 28 #include "mojo/public/interfaces/application/service_provider.mojom.h" | 28 #include "mojo/public/interfaces/application/service_provider.mojom.h" |
| 29 #include "mojo/services/tracing/interfaces/tracing.mojom.h" | 29 #include "mojo/services/tracing/interfaces/tracing.mojom.h" |
| 30 | 30 |
| 31 namespace benchmark { | 31 namespace benchmark { |
| 32 namespace { | 32 namespace { |
| 33 | 33 |
| 34 class BenchmarkApp : public mojo::ApplicationDelegate, | 34 class BenchmarkApp : public mojo::ApplicationImplBase, |
| 35 public TraceCollectorClient::Receiver { | 35 public TraceCollectorClient::Receiver { |
| 36 public: | 36 public: |
| 37 BenchmarkApp() {} | 37 BenchmarkApp() {} |
| 38 ~BenchmarkApp() override {} | 38 ~BenchmarkApp() override {} |
| 39 | 39 |
| 40 // mojo:ApplicationDelegate: | 40 // mojo:ApplicationImplBase: |
| 41 void Initialize(mojo::ApplicationImpl* app) override { | 41 void OnInitialize() override { |
| 42 // Parse command-line arguments. | 42 // Parse command-line arguments. |
| 43 if (!GetRunArgs(app->args(), &args_)) { | 43 if (!GetRunArgs(args(), &args_)) { |
| 44 LOG(ERROR) << "Failed to parse the input arguments."; | 44 LOG(ERROR) << "Failed to parse the input arguments."; |
| 45 mojo::ApplicationImpl::Terminate(); | 45 mojo::TerminateApplication(MOJO_RESULT_INVALID_ARGUMENT); |
| 46 return; | 46 return; |
| 47 } | 47 } |
| 48 | 48 |
| 49 // Don't compute the categories string if all categories should be traced. | 49 // Don't compute the categories string if all categories should be traced. |
| 50 std::string categories_str; | 50 std::string categories_str; |
| 51 if (args_.write_output_file) { | 51 if (args_.write_output_file) { |
| 52 categories_str = "*"; | 52 categories_str = "*"; |
| 53 } else { | 53 } else { |
| 54 categories_str = ComputeCategoriesStr(); | 54 categories_str = ComputeCategoriesStr(); |
| 55 } | 55 } |
| 56 | 56 |
| 57 // Connect to trace collector, which will fetch the trace events produced by | 57 // Connect to trace collector, which will fetch the trace events produced by |
| 58 // the app being benchmarked. | 58 // the app being benchmarked. |
| 59 tracing::TraceCollectorPtr trace_collector; | 59 tracing::TraceCollectorPtr trace_collector; |
| 60 mojo::ConnectToService(app->shell(), "mojo:tracing", | 60 mojo::ConnectToService(shell(), "mojo:tracing", GetProxy(&trace_collector)); |
| 61 GetProxy(&trace_collector)); | |
| 62 trace_collector_client_.reset( | 61 trace_collector_client_.reset( |
| 63 new TraceCollectorClient(this, trace_collector.Pass())); | 62 new TraceCollectorClient(this, trace_collector.Pass())); |
| 64 trace_collector_client_->Start(categories_str); | 63 trace_collector_client_->Start(categories_str); |
| 65 | 64 |
| 66 // Start tracing the application with 1 sec of delay. | 65 // Start tracing the application with 1 sec of delay. |
| 67 base::MessageLoop::current()->PostDelayedTask( | 66 base::MessageLoop::current()->PostDelayedTask( |
| 68 FROM_HERE, base::Bind(&BenchmarkApp::StartTracedApplication, | 67 FROM_HERE, base::Bind(&BenchmarkApp::StartTracedApplication, |
| 69 base::Unretained(this), app), | 68 base::Unretained(this)), |
| 70 base::TimeDelta::FromSeconds(1)); | 69 base::TimeDelta::FromSeconds(1)); |
| 71 } | 70 } |
| 72 | 71 |
| 73 // Computes the string of trace categories we want to collect: a union of all | 72 // Computes the string of trace categories we want to collect: a union of all |
| 74 // categories targeted in measurements. | 73 // categories targeted in measurements. |
| 75 std::string ComputeCategoriesStr() { | 74 std::string ComputeCategoriesStr() { |
| 76 std::set<std::string> category_set; | 75 std::set<std::string> category_set; |
| 77 for (const Measurement& measurement : args_.measurements) { | 76 for (const Measurement& measurement : args_.measurements) { |
| 78 std::vector<std::string> categories = | 77 std::vector<std::string> categories = |
| 79 base::SplitString(measurement.target_event.categories, ",", | 78 base::SplitString(measurement.target_event.categories, ",", |
| 80 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); | 79 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL); |
| 81 category_set.insert(categories.begin(), categories.end()); | 80 category_set.insert(categories.begin(), categories.end()); |
| 82 } | 81 } |
| 83 std::vector<std::string> unique_categories(category_set.begin(), | 82 std::vector<std::string> unique_categories(category_set.begin(), |
| 84 category_set.end()); | 83 category_set.end()); |
| 85 return base::JoinString(unique_categories, ","); | 84 return base::JoinString(unique_categories, ","); |
| 86 } | 85 } |
| 87 | 86 |
| 88 void StartTracedApplication(mojo::ApplicationImpl* app) { | 87 void StartTracedApplication() { |
| 89 // Record the time origin for measurements just before connecting to the app | 88 // Record the time origin for measurements just before connecting to the app |
| 90 // being benchmarked. | 89 // being benchmarked. |
| 91 time_origin_ = base::TimeTicks::FromInternalValue(MojoGetTimeTicksNow()); | 90 time_origin_ = base::TimeTicks::FromInternalValue(MojoGetTimeTicksNow()); |
| 92 app->shell()->ConnectToApplication( | 91 shell()->ConnectToApplication(args_.app, GetProxy(&traced_app_connection_), |
| 93 args_.app, GetProxy(&traced_app_connection_), nullptr); | 92 nullptr); |
| 94 | 93 |
| 95 // Post task to stop tracing when the time is up. | 94 // Post task to stop tracing when the time is up. |
| 96 base::MessageLoop::current()->PostDelayedTask( | 95 base::MessageLoop::current()->PostDelayedTask( |
| 97 FROM_HERE, | 96 FROM_HERE, |
| 98 base::Bind(&BenchmarkApp::StopTracing, base::Unretained(this)), | 97 base::Bind(&BenchmarkApp::StopTracing, base::Unretained(this)), |
| 99 args_.duration); | 98 args_.duration); |
| 100 } | 99 } |
| 101 | 100 |
| 102 void StopTracing() { | 101 void StopTracing() { |
| 103 // Request the trace collector to send back the data. When the data is ready | 102 // Request the trace collector to send back the data. When the data is ready |
| (...skipping 11 matching lines...) Expand all Loading... |
| 115 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); | 114 base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE); |
| 116 trace_file.WriteAtCurrentPos(trace_data.data(), trace_data.size()); | 115 trace_file.WriteAtCurrentPos(trace_data.data(), trace_data.size()); |
| 117 printf("wrote trace file at: %s\n", | 116 printf("wrote trace file at: %s\n", |
| 118 args_.output_file_path.value().c_str()); | 117 args_.output_file_path.value().c_str()); |
| 119 } | 118 } |
| 120 | 119 |
| 121 // Parse trace events. | 120 // Parse trace events. |
| 122 std::vector<Event> events; | 121 std::vector<Event> events; |
| 123 if (!GetEvents(trace_data, &events)) { | 122 if (!GetEvents(trace_data, &events)) { |
| 124 LOG(ERROR) << "Failed to parse the trace data"; | 123 LOG(ERROR) << "Failed to parse the trace data"; |
| 125 mojo::ApplicationImpl::Terminate(); | 124 mojo::TerminateApplication(MOJO_RESULT_UNKNOWN); |
| 126 return; | 125 return; |
| 127 } | 126 } |
| 128 | 127 |
| 129 // Calculate and print the results. | 128 // Calculate and print the results. |
| 130 bool succeeded = true; | 129 bool succeeded = true; |
| 131 Measurements measurements(events, time_origin_); | 130 Measurements measurements(events, time_origin_); |
| 132 for (const Measurement& measurement : args_.measurements) { | 131 for (const Measurement& measurement : args_.measurements) { |
| 133 double result = measurements.Measure(measurement); | 132 double result = measurements.Measure(measurement); |
| 134 if (result >= 0.0) { | 133 if (result >= 0.0) { |
| 135 printf("measurement: %s %lf\n", measurement.spec.c_str(), result); | 134 printf("measurement: %s %lf\n", measurement.spec.c_str(), result); |
| 136 } else { | 135 } else { |
| 137 succeeded = false; | 136 succeeded = false; |
| 138 printf("measurement: %s FAILED\n", measurement.spec.c_str()); | 137 printf("measurement: %s FAILED\n", measurement.spec.c_str()); |
| 139 } | 138 } |
| 140 } | 139 } |
| 141 | 140 |
| 142 // Scripts that run benchmarks can pick this up as a signal that the run | 141 // Scripts that run benchmarks can pick this up as a signal that the run |
| 143 // succeeded, as shell exit code is 0 even if an app exits early due to an | 142 // succeeded, as shell exit code is 0 even if an app exits early due to an |
| 144 // error. | 143 // error. |
| 145 if (succeeded) { | 144 if (succeeded) { |
| 146 printf("benchmark succeeded\n"); | 145 printf("benchmark succeeded\n"); |
| 147 } else { | 146 } else { |
| 148 printf("some measurements failed\n"); | 147 printf("some measurements failed\n"); |
| 149 } | 148 } |
| 150 mojo::ApplicationImpl::Terminate(); | 149 mojo::TerminateApplication(MOJO_RESULT_OK); |
| 151 } | 150 } |
| 152 | 151 |
| 153 private: | 152 private: |
| 154 RunArgs args_; | 153 RunArgs args_; |
| 155 mojo::InterfaceHandle<mojo::ServiceProvider> traced_app_connection_; | 154 mojo::InterfaceHandle<mojo::ServiceProvider> traced_app_connection_; |
| 156 scoped_ptr<TraceCollectorClient> trace_collector_client_; | 155 scoped_ptr<TraceCollectorClient> trace_collector_client_; |
| 157 base::TimeTicks time_origin_; | 156 base::TimeTicks time_origin_; |
| 158 | 157 |
| 159 DISALLOW_COPY_AND_ASSIGN(BenchmarkApp); | 158 DISALLOW_COPY_AND_ASSIGN(BenchmarkApp); |
| 160 }; | 159 }; |
| 161 } // namespace | 160 } // namespace |
| 162 } // namespace benchmark | 161 } // namespace benchmark |
| 163 | 162 |
| 164 MojoResult MojoMain(MojoHandle application_request) { | 163 MojoResult MojoMain(MojoHandle application_request) { |
| 165 mojo::ApplicationRunnerChromium runner(new benchmark::BenchmarkApp); | 164 mojo::ScopedChromiumInit init; |
| 166 auto ret = runner.Run(application_request); | 165 benchmark::BenchmarkApp benchmark_app; |
| 166 auto ret = mojo::RunApplication(application_request, &benchmark_app); |
| 167 fflush(nullptr); | 167 fflush(nullptr); |
| 168 return ret; | 168 return ret; |
| 169 } | 169 } |
| OLD | NEW |