Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_RUNTIME_METRICS_REPORTER_H_ |
| 18 | #define ART_RUNTIME_METRICS_REPORTER_H_ |
| 19 | |
| 20 | #include "base/message_queue.h" |
| 21 | #include "base/metrics/metrics.h" |
| 22 | |
| 23 | #pragma clang diagnostic push |
| 24 | #pragma clang diagnostic error "-Wconversion" |
| 25 | |
| 26 | namespace art { |
| 27 | namespace metrics { |
| 28 | |
| 29 | // Defines the set of options for how metrics reporting happens. |
| 30 | struct ReportingConfig { |
| 31 | static ReportingConfig FromRuntimeArguments(const RuntimeArgumentMap& args); |
| 32 | |
| 33 | // Causes metrics to be written to the log, which makes them show up in logcat. |
| 34 | bool dump_to_logcat{false}; |
| 35 | |
| 36 | // If set, provides a file name to enable metrics logging to a file. |
| 37 | std::optional<std::string> dump_to_file; |
| 38 | |
| 39 | // Indicates whether to report the final state of metrics on shutdown. |
| 40 | // |
| 41 | // Note that reporting only happens if some output, such as logcat, is enabled. |
| 42 | bool report_metrics_on_shutdown{true}; |
| 43 | |
| 44 | // If set, metrics will be reported every time this many seconds elapses. |
| 45 | std::optional<unsigned int> periodic_report_seconds; |
| 46 | |
| 47 | // Returns whether any options are set that enables metrics reporting. |
| 48 | constexpr bool ReportingEnabled() const { return dump_to_logcat || dump_to_file.has_value(); } |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | // MetricsReporter handles periodically reporting ART metrics. |
| 52 | class MetricsReporter { |
| 53 | public: |
| 54 | // Creates a MetricsReporter instance that matches the options selected in ReportingConfig. |
| 55 | static std::unique_ptr<MetricsReporter> Create(ReportingConfig config, Runtime* runtime); |
| 56 | |
| 57 | ~MetricsReporter(); |
| 58 | |
| 59 | // Creates and runs the background reporting thread. |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame^] | 60 | void MaybeStartBackgroundThread(SessionData session_data); |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 61 | |
| 62 | // Sends a request to the background thread to shutdown. |
| 63 | void MaybeStopBackgroundThread(); |
| 64 | |
Eric Holk | b6dda5a | 2021-02-04 01:09:58 +0000 | [diff] [blame] | 65 | // Causes metrics to be reported so we can see a snapshot of the metrics after app startup |
| 66 | // completes. |
| 67 | void NotifyStartupCompleted(); |
| 68 | |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 69 | static constexpr const char* kBackgroundThreadName = "Metrics Background Reporting Thread"; |
| 70 | |
| 71 | private: |
| 72 | MetricsReporter(ReportingConfig config, Runtime* runtime); |
| 73 | |
| 74 | // The background reporting thread main loop. |
| 75 | void BackgroundThreadRun(); |
| 76 | |
| 77 | // Calls messages_.SetTimeout if needed. |
| 78 | void MaybeResetTimeout(); |
| 79 | |
| 80 | // Outputs the current state of the metrics to the destination set by config_. |
| 81 | void ReportMetrics() const; |
| 82 | |
| 83 | const ReportingConfig config_; |
| 84 | Runtime* runtime_; |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame^] | 85 | std::vector<std::unique_ptr<MetricsBackend>> backends_; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 86 | std::optional<std::thread> thread_; |
| 87 | |
| 88 | // A message indicating that the reporting thread should shut down. |
| 89 | struct ShutdownRequestedMessage {}; |
| 90 | |
Eric Holk | b6dda5a | 2021-02-04 01:09:58 +0000 | [diff] [blame] | 91 | // A message indicating that app startup has completed. |
| 92 | struct StartupCompletedMessage {}; |
| 93 | |
Eric Holk | c7ac91b | 2021-02-04 21:44:01 +0000 | [diff] [blame^] | 94 | // A message marking the beginning of a metrics logging session. |
| 95 | // |
| 96 | // The primary purpose of this is to pass the session metadata from the Runtime to the metrics |
| 97 | // backends. |
| 98 | struct BeginSessionMessage{ SessionData session_data; }; |
| 99 | |
| 100 | MessageQueue<ShutdownRequestedMessage, StartupCompletedMessage, BeginSessionMessage> messages_; |
Eric Holk | 480d981 | 2021-01-27 23:41:45 +0000 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | } // namespace metrics |
| 104 | } // namespace art |
| 105 | |
| 106 | #pragma clang diagnostic pop // -Wconversion |
| 107 | |
| 108 | #endif // ART_RUNTIME_METRICS_REPORTER_H_ |