blob: 1e2c294bf20bba0636b13b2fc4d71e3363465a1b [file] [log] [blame]
Andrew Moylan40ee4fc2018-08-24 15:46:09 +10001// Copyright 2018 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef ML_METRICS_H_
6#define ML_METRICS_H_
7
8#include <memory>
9
10#include <base/macros.h>
11#include <base/process/process_metrics.h>
12#include <metrics/cumulative_metrics.h>
13#include <metrics/metrics_library.h>
14
15namespace ml {
16
17// Performs UMA metrics logging for the ML Service daemon.
18// Periodically gathers some process metrics (e.g. memory) and cumulative
19// metrics (e.g. peak memory) itself.
20// Threading: Create and use on a single sequence.
21class Metrics {
22 public:
Honglin Yuca0caf82020-01-23 12:26:03 +110023 // These values are persisted to logs. Entries should not be renumbered and
24 // numeric values should never be reused.
Andrew Moylan79b34a42020-07-08 11:13:11 +100025 // `kMaxValue` must equal to the maximum value in this enum.
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100026 enum class MojoConnectionEvent {
27 kBootstrapRequested = 0,
Honglin Yuca0caf82020-01-23 12:26:03 +110028 kBootstrapSucceeded = 1,
Andrew Moylanb481af72020-07-09 15:22:00 +100029 kConnectionClosed = 2,
30 kMaxValue = kConnectionClosed
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100031 };
32
33 Metrics();
Qijiang Fan6bc59e12020-11-11 02:51:06 +090034 Metrics(const Metrics&) = delete;
35 Metrics& operator=(const Metrics&) = delete;
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100036
37 // Starts periodic sampling of process metrics.
38 void StartCollectingProcessMetrics();
39
40 // Immediately samples & updates cumulative process metrics (i.e. peak RAM).
41 // This does not change how often metrics are reported, but might increase the
42 // accuracy of reported metrics.
43 // Clients can call this manually in contexts where they know that e.g. memory
44 // usage may have just increased, to help capture short-lived spikes that
45 // might be missed by periodic sampling.
46 void UpdateCumulativeMetricsNow();
47
48 void RecordMojoConnectionEvent(MojoConnectionEvent event);
49
50 private:
Andrew Moylan79b34a42020-07-08 11:13:11 +100051 // Fetches process metrics (e.g. RAM) and updates `cumulative_metrics`.
52 // If `record_current_metrics` is true, also logs current process metrics.
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100053 void UpdateAndRecordMetrics(
54 bool record_current_metrics,
55 chromeos_metrics::CumulativeMetrics* cumulative_metrics);
56
57 const std::unique_ptr<base::ProcessMetrics> process_metrics_;
58
59 MetricsLibrary metrics_library_;
60
61 // Accumulator of process metrics (even across restarts).
62 std::unique_ptr<chromeos_metrics::CumulativeMetrics> cumulative_metrics_;
Andrew Moylan40ee4fc2018-08-24 15:46:09 +100063};
64
65} // namespace ml
66
67#endif // ML_METRICS_H_