blob: a9cd1f381d7172fd239ac39af4cd15ae390f9e49 [file] [log] [blame]
Eric Holk480d9812021-01-27 23:41:45 +00001/*
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
26namespace art {
27namespace metrics {
28
29// Defines the set of options for how metrics reporting happens.
30struct 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 Holk480d9812021-01-27 23:41:45 +000049};
50
51// MetricsReporter handles periodically reporting ART metrics.
52class 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 Holkc7ac91b2021-02-04 21:44:01 +000060 void MaybeStartBackgroundThread(SessionData session_data);
Eric Holk480d9812021-01-27 23:41:45 +000061
62 // Sends a request to the background thread to shutdown.
63 void MaybeStopBackgroundThread();
64
Eric Holkb6dda5a2021-02-04 01:09:58 +000065 // Causes metrics to be reported so we can see a snapshot of the metrics after app startup
66 // completes.
67 void NotifyStartupCompleted();
68
Eric Holk480d9812021-01-27 23:41:45 +000069 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 Holkc7ac91b2021-02-04 21:44:01 +000085 std::vector<std::unique_ptr<MetricsBackend>> backends_;
Eric Holk480d9812021-01-27 23:41:45 +000086 std::optional<std::thread> thread_;
87
88 // A message indicating that the reporting thread should shut down.
89 struct ShutdownRequestedMessage {};
90
Eric Holkb6dda5a2021-02-04 01:09:58 +000091 // A message indicating that app startup has completed.
92 struct StartupCompletedMessage {};
93
Eric Holkc7ac91b2021-02-04 21:44:01 +000094 // 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 Holk480d9812021-01-27 23:41:45 +0000101};
102
103} // namespace metrics
104} // namespace art
105
106#pragma clang diagnostic pop // -Wconversion
107
108#endif // ART_RUNTIME_METRICS_REPORTER_H_