1*e1997b9aSAndroid Build Coastguard Worker /* 2*e1997b9aSAndroid Build Coastguard Worker * Copyright (C) 2022 The Android Open Source Project 3*e1997b9aSAndroid Build Coastguard Worker * 4*e1997b9aSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*e1997b9aSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*e1997b9aSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*e1997b9aSAndroid Build Coastguard Worker * 8*e1997b9aSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*e1997b9aSAndroid Build Coastguard Worker * 10*e1997b9aSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*e1997b9aSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*e1997b9aSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*e1997b9aSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*e1997b9aSAndroid Build Coastguard Worker * limitations under the License. 15*e1997b9aSAndroid Build Coastguard Worker */ 16*e1997b9aSAndroid Build Coastguard Worker 17*e1997b9aSAndroid Build Coastguard Worker #include "StatsReporter.h" 18*e1997b9aSAndroid Build Coastguard Worker #include <android-base/logging.h> 19*e1997b9aSAndroid Build Coastguard Worker #include <stdlib.h> 20*e1997b9aSAndroid Build Coastguard Worker #include <string> 21*e1997b9aSAndroid Build Coastguard Worker #include <sys/stat.h> 22*e1997b9aSAndroid Build Coastguard Worker 23*e1997b9aSAndroid Build Coastguard Worker // Keep these constants in sync with those in OdsignStatsLogger.java. 24*e1997b9aSAndroid Build Coastguard Worker constexpr const char* kOdsignMetricsFile = "/data/misc/odsign/metrics/odsign-metrics.txt"; 25*e1997b9aSAndroid Build Coastguard Worker constexpr const char* kComposMetricName = "comp_os_artifacts_check_record"; 26*e1997b9aSAndroid Build Coastguard Worker constexpr const char* kOdsignMetricName = "odsign_record"; 27*e1997b9aSAndroid Build Coastguard Worker ~StatsReporter()28*e1997b9aSAndroid Build Coastguard WorkerStatsReporter::~StatsReporter() { 29*e1997b9aSAndroid Build Coastguard Worker if (comp_os_artifacts_check_record_ == nullptr && !odsign_record_enabled_) { 30*e1997b9aSAndroid Build Coastguard Worker LOG(INFO) << "Metrics report is empty"; 31*e1997b9aSAndroid Build Coastguard Worker 32*e1997b9aSAndroid Build Coastguard Worker // Remove the metrics file if any old version of the file already exists 33*e1997b9aSAndroid Build Coastguard Worker if (std::filesystem::remove(kOdsignMetricsFile) != 0 && 34*e1997b9aSAndroid Build Coastguard Worker !((errno = ENOENT) || errno == ENOTDIR)) { 35*e1997b9aSAndroid Build Coastguard Worker PLOG(ERROR) << "Could not remove already present file"; 36*e1997b9aSAndroid Build Coastguard Worker } 37*e1997b9aSAndroid Build Coastguard Worker return; 38*e1997b9aSAndroid Build Coastguard Worker } 39*e1997b9aSAndroid Build Coastguard Worker 40*e1997b9aSAndroid Build Coastguard Worker std::ofstream odsign_metrics_file_; 41*e1997b9aSAndroid Build Coastguard Worker odsign_metrics_file_.open(kOdsignMetricsFile, std::ios::trunc); 42*e1997b9aSAndroid Build Coastguard Worker if (!odsign_metrics_file_) { 43*e1997b9aSAndroid Build Coastguard Worker PLOG(ERROR) << "Could not open file: " << kOdsignMetricsFile; 44*e1997b9aSAndroid Build Coastguard Worker return; 45*e1997b9aSAndroid Build Coastguard Worker } 46*e1997b9aSAndroid Build Coastguard Worker if (chmod(kOdsignMetricsFile, 0644) != 0) { 47*e1997b9aSAndroid Build Coastguard Worker PLOG(ERROR) << "Could not set correct file permissions for " << kOdsignMetricsFile; 48*e1997b9aSAndroid Build Coastguard Worker return; 49*e1997b9aSAndroid Build Coastguard Worker } 50*e1997b9aSAndroid Build Coastguard Worker 51*e1997b9aSAndroid Build Coastguard Worker if (comp_os_artifacts_check_record_ != nullptr) { 52*e1997b9aSAndroid Build Coastguard Worker odsign_metrics_file_ << kComposMetricName << ' ' 53*e1997b9aSAndroid Build Coastguard Worker << comp_os_artifacts_check_record_->current_artifacts_ok << ' ' 54*e1997b9aSAndroid Build Coastguard Worker << comp_os_artifacts_check_record_->comp_os_pending_artifacts_exists 55*e1997b9aSAndroid Build Coastguard Worker << ' ' 56*e1997b9aSAndroid Build Coastguard Worker << comp_os_artifacts_check_record_->use_comp_os_generated_artifacts 57*e1997b9aSAndroid Build Coastguard Worker << '\n'; 58*e1997b9aSAndroid Build Coastguard Worker } 59*e1997b9aSAndroid Build Coastguard Worker 60*e1997b9aSAndroid Build Coastguard Worker if (odsign_record_enabled_) { 61*e1997b9aSAndroid Build Coastguard Worker odsign_metrics_file_ << kOdsignMetricName << ' ' << odsign_record_.status << '\n'; 62*e1997b9aSAndroid Build Coastguard Worker } 63*e1997b9aSAndroid Build Coastguard Worker 64*e1997b9aSAndroid Build Coastguard Worker odsign_metrics_file_.close(); 65*e1997b9aSAndroid Build Coastguard Worker if (!odsign_metrics_file_) { 66*e1997b9aSAndroid Build Coastguard Worker PLOG(ERROR) << "Failed to close the file"; 67*e1997b9aSAndroid Build Coastguard Worker } 68*e1997b9aSAndroid Build Coastguard Worker } 69*e1997b9aSAndroid Build Coastguard Worker GetOrCreateComposArtifactsCheckRecord()70*e1997b9aSAndroid Build Coastguard WorkerStatsReporter::CompOsArtifactsCheckRecord* StatsReporter::GetOrCreateComposArtifactsCheckRecord() { 71*e1997b9aSAndroid Build Coastguard Worker if (comp_os_artifacts_check_record_ == nullptr) { 72*e1997b9aSAndroid Build Coastguard Worker comp_os_artifacts_check_record_ = std::make_unique<CompOsArtifactsCheckRecord>(); 73*e1997b9aSAndroid Build Coastguard Worker } 74*e1997b9aSAndroid Build Coastguard Worker return comp_os_artifacts_check_record_.get(); 75*e1997b9aSAndroid Build Coastguard Worker } 76