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: |
| 23 | enum class MojoConnectionEvent { |
| 24 | kBootstrapRequested = 0, |
| 25 | kBootstrapSucceeded, |
| 26 | kConnectionError, |
| 27 | kMax |
| 28 | }; |
| 29 | |
| 30 | Metrics(); |
| 31 | |
| 32 | // Starts periodic sampling of process metrics. |
| 33 | void StartCollectingProcessMetrics(); |
| 34 | |
| 35 | // Immediately samples & updates cumulative process metrics (i.e. peak RAM). |
| 36 | // This does not change how often metrics are reported, but might increase the |
| 37 | // accuracy of reported metrics. |
| 38 | // Clients can call this manually in contexts where they know that e.g. memory |
| 39 | // usage may have just increased, to help capture short-lived spikes that |
| 40 | // might be missed by periodic sampling. |
| 41 | void UpdateCumulativeMetricsNow(); |
| 42 | |
| 43 | void RecordMojoConnectionEvent(MojoConnectionEvent event); |
| 44 | |
| 45 | private: |
| 46 | // Fetches process metrics (e.g. RAM) and updates |cumulative_metrics|. |
| 47 | // If |record_current_metrics| is true, also logs current process metrics. |
| 48 | void UpdateAndRecordMetrics( |
| 49 | bool record_current_metrics, |
| 50 | chromeos_metrics::CumulativeMetrics* cumulative_metrics); |
| 51 | |
| 52 | const std::unique_ptr<base::ProcessMetrics> process_metrics_; |
| 53 | |
| 54 | MetricsLibrary metrics_library_; |
| 55 | |
| 56 | // Accumulator of process metrics (even across restarts). |
| 57 | std::unique_ptr<chromeos_metrics::CumulativeMetrics> cumulative_metrics_; |
| 58 | |
| 59 | DISALLOW_COPY_AND_ASSIGN(Metrics); |
| 60 | }; |
| 61 | |
| 62 | } // namespace ml |
| 63 | |
| 64 | #endif // ML_METRICS_H_ |