Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 1 | // 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 Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 7 | #include <base/check.h> |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 8 | #include <base/logging.h> |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 9 | |
| 10 | #include "ml/mojom/machine_learning_service.mojom.h" |
| 11 | |
| 12 | namespace ml { |
| 13 | |
| 14 | using chromeos::machine_learning::mojom::LoadModelResult; |
| 15 | |
Honglin Yu | 0b12418 | 2020-07-23 09:49:42 +1000 | [diff] [blame] | 16 | RequestMetrics::RequestMetrics(const std::string& model_name, |
| 17 | const std::string& request_name) |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 18 | : name_base_(std::string(kGlobalMetricsPrefix) + model_name + "." + |
| 19 | request_name), |
Honglin Yu | 0b12418 | 2020-07-23 09:49:42 +1000 | [diff] [blame] | 20 | initial_cpu_clock_(0), |
Tom Hughes | 1d1c192 | 2020-08-27 16:16:53 -0700 | [diff] [blame] | 21 | initial_memory_(0), |
| 22 | status_(Status::kNotStarted) {} |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 23 | |
| 24 | void RequestMetrics::StartRecordingPerformanceMetrics() { |
Honglin Yu | 0b12418 | 2020-07-23 09:49:42 +1000 | [diff] [blame] | 25 | 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)); |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 30 | // 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 Yu | 0b12418 | 2020-07-23 09:49:42 +1000 | [diff] [blame] | 37 | |
| 38 | status_ = Status::kRecording; |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | void RequestMetrics::FinishRecordingPerformanceMetrics() { |
Honglin Yu | 0b12418 | 2020-07-23 09:49:42 +1000 | [diff] [blame] | 42 | 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); |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 47 | |
| 48 | // Memory usage |
| 49 | size_t usage = 0; |
| 50 | if (!GetTotalProcessMemoryUsage(&usage)) { |
| 51 | LOG(DFATAL) << "Getting process memory usage failed."; |
| 52 | return; |
| 53 | } |
Tom Hughes | 1d1c192 | 2020-08-27 16:16:53 -0700 | [diff] [blame] | 54 | const int64_t memory_usage_kb = static_cast<int64_t>(usage) - initial_memory_; |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 55 | |
| 56 | metrics_library_.SendToUMA(name_base_ + kTotalMemoryDeltaSuffix, |
Tom Hughes | 1d1c192 | 2020-08-27 16:16:53 -0700 | [diff] [blame] | 57 | memory_usage_kb, kMemoryDeltaMinKb, |
| 58 | kMemoryDeltaMaxKb, kMemoryDeltaBuckets); |
| 59 | metrics_library_.SendToUMA(name_base_ + kCpuTimeSuffix, cpu_time_microsec, |
| 60 | kCpuTimeMinMicrosec, kCpuTimeMaxMicrosec, |
charleszhao | 5a7050e | 2020-07-14 15:21:41 +1000 | [diff] [blame] | 61 | kCpuTimeBuckets); |
| 62 | } |
| 63 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 64 | // Records in MachineLearningService.LoadModelResult rather than a |
| 65 | // model-specific enum histogram because the model name is unknown. |
| 66 | void RecordModelSpecificationErrorEvent() { |
| 67 | MetricsLibrary().SendEnumToUMA( |
| 68 | "MachineLearningService.LoadModelResult", |
| 69 | static_cast<int>(LoadModelResult::MODEL_SPEC_ERROR), |
Andrew Moylan | 33b25c0 | 2019-01-29 09:40:01 +1100 | [diff] [blame] | 70 | static_cast<int>(LoadModelResult::kMaxValue) + 1); |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 71 | } |
| 72 | |
Honglin Yu | 2161669 | 2021-05-14 11:20:22 +1000 | [diff] [blame] | 73 | void RecordProcessErrorEvent(ProcessError error) { |
| 74 | MetricsLibrary().SendEnumToUMA("MachineLearningService.ProcessError", |
| 75 | static_cast<int>(error), |
| 76 | static_cast<int>(ProcessError::kMaxValue) + 1); |
| 77 | } |
| 78 | |
| 79 | void RecordWorkerProcessExitStatus(int status) { |
| 80 | MetricsLibrary().SendSparseToUMA( |
| 81 | "MachineLearningService.WorkerProcessExitStatus", status); |
| 82 | } |
| 83 | |
Honglin Yu | 6adafcd | 2019-07-22 13:48:11 +1000 | [diff] [blame] | 84 | } // namespace ml |