Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 1 | // 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 | |
| 15 | namespace 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. |
| 21 | class Metrics { |
| 22 | public: |
Honglin Yu | ca0caf8 | 2020-01-23 12:26:03 +1100 | [diff] [blame^] | 23 | // These values are persisted to logs. Entries should not be renumbered and |
| 24 | // numeric values should never be reused. |
| 25 | // |kMaxValue| must equal to the maximum value in this enum. |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 26 | enum class MojoConnectionEvent { |
| 27 | kBootstrapRequested = 0, |
Honglin Yu | ca0caf8 | 2020-01-23 12:26:03 +1100 | [diff] [blame^] | 28 | kBootstrapSucceeded = 1, |
| 29 | kConnectionError = 2, |
| 30 | kMaxValue = kConnectionError |
Andrew Moylan | 40ee4fc | 2018-08-24 15:46:09 +1000 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | Metrics(); |
| 34 | |
| 35 | // Starts periodic sampling of process metrics. |
| 36 | void StartCollectingProcessMetrics(); |
| 37 | |
| 38 | // Immediately samples & updates cumulative process metrics (i.e. peak RAM). |
| 39 | // This does not change how often metrics are reported, but might increase the |
| 40 | // accuracy of reported metrics. |
| 41 | // Clients can call this manually in contexts where they know that e.g. memory |
| 42 | // usage may have just increased, to help capture short-lived spikes that |
| 43 | // might be missed by periodic sampling. |
| 44 | void UpdateCumulativeMetricsNow(); |
| 45 | |
| 46 | void RecordMojoConnectionEvent(MojoConnectionEvent event); |
| 47 | |
| 48 | private: |
| 49 | // Fetches process metrics (e.g. RAM) and updates |cumulative_metrics|. |
| 50 | // If |record_current_metrics| is true, also logs current process metrics. |
| 51 | void UpdateAndRecordMetrics( |
| 52 | bool record_current_metrics, |
| 53 | chromeos_metrics::CumulativeMetrics* cumulative_metrics); |
| 54 | |
| 55 | const std::unique_ptr<base::ProcessMetrics> process_metrics_; |
| 56 | |
| 57 | MetricsLibrary metrics_library_; |
| 58 | |
| 59 | // Accumulator of process metrics (even across restarts). |
| 60 | std::unique_ptr<chromeos_metrics::CumulativeMetrics> cumulative_metrics_; |
| 61 | |
| 62 | DISALLOW_COPY_AND_ASSIGN(Metrics); |
| 63 | }; |
| 64 | |
| 65 | } // namespace ml |
| 66 | |
| 67 | #endif // ML_METRICS_H_ |