blob: 7602b4d56ea61c93872f8bb4be6e53b0b47acd32 [file] [log] [blame]
Honglin Yu6adafcd2019-07-22 13:48:11 +10001// Copyright 2019 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#include "ml/request_metrics.h"
6
Qijiang Fan713061e2021-03-08 15:45:12 +09007#include <base/check.h>
charleszhao5a7050e2020-07-14 15:21:41 +10008#include <base/logging.h>
Honglin Yu6adafcd2019-07-22 13:48:11 +10009
10#include "ml/mojom/machine_learning_service.mojom.h"
11
12namespace ml {
13
14using chromeos::machine_learning::mojom::LoadModelResult;
15
Honglin Yu0b124182020-07-23 09:49:42 +100016RequestMetrics::RequestMetrics(const std::string& model_name,
17 const std::string& request_name)
charleszhao5a7050e2020-07-14 15:21:41 +100018 : name_base_(std::string(kGlobalMetricsPrefix) + model_name + "." +
19 request_name),
Honglin Yu0b124182020-07-23 09:49:42 +100020 initial_cpu_clock_(0),
Tom Hughes1d1c1922020-08-27 16:16:53 -070021 initial_memory_(0),
22 status_(Status::kNotStarted) {}
charleszhao5a7050e2020-07-14 15:21:41 +100023
24void RequestMetrics::StartRecordingPerformanceMetrics() {
Honglin Yu0b124182020-07-23 09:49:42 +100025 DCHECK(status_ == Status::kNotStarted);
26 // Get initial CPU clock in order to set the "zero" point of the CPU usage
27 // counter.
28 initial_cpu_clock_ = std::clock();
29 DCHECK(initial_cpu_clock_ != static_cast<std::clock_t>(-1));
charleszhao5a7050e2020-07-14 15:21:41 +100030 // Query memory usage.
31 size_t usage = 0;
32 if (!GetTotalProcessMemoryUsage(&usage)) {
33 LOG(DFATAL) << "Getting process memory usage failed.";
34 return;
35 }
36 initial_memory_ = static_cast<int64_t>(usage);
Honglin Yu0b124182020-07-23 09:49:42 +100037
38 status_ = Status::kRecording;
charleszhao5a7050e2020-07-14 15:21:41 +100039}
40
41void RequestMetrics::FinishRecordingPerformanceMetrics() {
Honglin Yu0b124182020-07-23 09:49:42 +100042 DCHECK(status_ == Status::kRecording);
43 status_ = Status::kFinished;
44 // Get CPU time by `clock()`.
45 const int64_t cpu_time_microsec = static_cast<int64_t>(
46 (std::clock() - initial_cpu_clock_) * 1000000.0 / CLOCKS_PER_SEC);
charleszhao5a7050e2020-07-14 15:21:41 +100047
48 // Memory usage
49 size_t usage = 0;
50 if (!GetTotalProcessMemoryUsage(&usage)) {
51 LOG(DFATAL) << "Getting process memory usage failed.";
52 return;
53 }
Tom Hughes1d1c1922020-08-27 16:16:53 -070054 const int64_t memory_usage_kb = static_cast<int64_t>(usage) - initial_memory_;
charleszhao5a7050e2020-07-14 15:21:41 +100055
56 metrics_library_.SendToUMA(name_base_ + kTotalMemoryDeltaSuffix,
Tom Hughes1d1c1922020-08-27 16:16:53 -070057 memory_usage_kb, kMemoryDeltaMinKb,
58 kMemoryDeltaMaxKb, kMemoryDeltaBuckets);
59 metrics_library_.SendToUMA(name_base_ + kCpuTimeSuffix, cpu_time_microsec,
60 kCpuTimeMinMicrosec, kCpuTimeMaxMicrosec,
charleszhao5a7050e2020-07-14 15:21:41 +100061 kCpuTimeBuckets);
62}
63
Honglin Yu6adafcd2019-07-22 13:48:11 +100064// Records in MachineLearningService.LoadModelResult rather than a
65// model-specific enum histogram because the model name is unknown.
66void RecordModelSpecificationErrorEvent() {
67 MetricsLibrary().SendEnumToUMA(
68 "MachineLearningService.LoadModelResult",
69 static_cast<int>(LoadModelResult::MODEL_SPEC_ERROR),
Andrew Moylan33b25c02019-01-29 09:40:01 +110070 static_cast<int>(LoadModelResult::kMaxValue) + 1);
Honglin Yu6adafcd2019-07-22 13:48:11 +100071}
72
Honglin Yu21616692021-05-14 11:20:22 +100073void RecordProcessErrorEvent(ProcessError error) {
74 MetricsLibrary().SendEnumToUMA("MachineLearningService.ProcessError",
75 static_cast<int>(error),
76 static_cast<int>(ProcessError::kMaxValue) + 1);
77}
78
79void RecordWorkerProcessExitStatus(int status) {
80 MetricsLibrary().SendSparseToUMA(
81 "MachineLearningService.WorkerProcessExitStatus", status);
82}
83
Honglin Yu6adafcd2019-07-22 13:48:11 +100084} // namespace ml