Bertrand SIMONNET | 52e5b99 | 2015-08-10 15:18:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 16 | |
| 17 | #include "uploader/metrics_log.h" |
| 18 | |
| 19 | #include <string> |
| 20 | |
Bertrand SIMONNET | 0586504 | 2015-12-15 12:33:01 -0800 | [diff] [blame] | 21 | #include <base/files/file_util.h> |
| 22 | |
Bertrand SIMONNET | 4b915ae | 2015-07-28 15:38:14 -0700 | [diff] [blame] | 23 | #include "uploader/proto/system_profile.pb.h" |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 24 | #include "uploader/system_profile_setter.h" |
| 25 | |
| 26 | // We use default values for the MetricsLogBase constructor as the setter will |
| 27 | // override them. |
| 28 | MetricsLog::MetricsLog() |
| 29 | : MetricsLogBase("", 0, metrics::MetricsLogBase::ONGOING_LOG, "") { |
| 30 | } |
| 31 | |
Bertrand SIMONNET | 0586504 | 2015-12-15 12:33:01 -0800 | [diff] [blame] | 32 | bool MetricsLog::LoadFromFile(const base::FilePath& saved_log) { |
| 33 | std::string encoded_log; |
| 34 | if (!base::ReadFileToString(saved_log, &encoded_log)) { |
| 35 | LOG(ERROR) << "Failed to read the metrics log backup from " |
| 36 | << saved_log.value(); |
| 37 | return false; |
| 38 | } |
| 39 | |
| 40 | if (!uma_proto()->ParseFromString(encoded_log)) { |
| 41 | LOG(ERROR) << "Failed to parse log from " << saved_log.value() |
| 42 | << ", deleting the log"; |
| 43 | base::DeleteFile(saved_log, false); |
| 44 | uma_proto()->Clear(); |
| 45 | return false; |
| 46 | } |
| 47 | |
| 48 | VLOG(1) << uma_proto()->histogram_event_size() << " histograms loaded from " |
| 49 | << saved_log.value(); |
| 50 | |
| 51 | return true; |
| 52 | } |
| 53 | |
| 54 | bool MetricsLog::SaveToFile(const base::FilePath& path) { |
| 55 | std::string encoded_log; |
| 56 | GetEncodedLog(&encoded_log); |
| 57 | |
| 58 | if (static_cast<int>(encoded_log.size()) != |
| 59 | base::WriteFile(path, encoded_log.data(), encoded_log.size())) { |
| 60 | LOG(ERROR) << "Failed to persist the current log to " << path.value(); |
| 61 | return false; |
| 62 | } |
| 63 | return true; |
| 64 | } |
| 65 | |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 66 | void MetricsLog::IncrementUserCrashCount(unsigned int count) { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 67 | metrics::SystemProfileProto::Stability* stability( |
| 68 | uma_proto()->mutable_system_profile()->mutable_stability()); |
| 69 | int current = stability->other_user_crash_count(); |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 70 | stability->set_other_user_crash_count(current + count); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 73 | void MetricsLog::IncrementKernelCrashCount(unsigned int count) { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 74 | metrics::SystemProfileProto::Stability* stability( |
| 75 | uma_proto()->mutable_system_profile()->mutable_stability()); |
| 76 | int current = stability->kernel_crash_count(); |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 77 | stability->set_kernel_crash_count(current + count); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 80 | void MetricsLog::IncrementUncleanShutdownCount(unsigned int count) { |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 81 | metrics::SystemProfileProto::Stability* stability( |
| 82 | uma_proto()->mutable_system_profile()->mutable_stability()); |
| 83 | int current = stability->unclean_system_shutdown_count(); |
Bertrand SIMONNET | 6b8629a | 2015-11-18 13:46:33 -0800 | [diff] [blame] | 84 | stability->set_unclean_system_shutdown_count(current + count); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 85 | } |
| 86 | |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 87 | bool MetricsLog::PopulateSystemProfile(SystemProfileSetter* profile_setter) { |
Bertrand SIMONNET | f1aa372 | 2016-01-22 13:15:54 -0800 | [diff] [blame] | 88 | CHECK(profile_setter); |
Bertrand SIMONNET | 1f14655 | 2015-08-19 18:13:02 -0700 | [diff] [blame] | 89 | return profile_setter->Populate(uma_proto()); |
Bertrand SIMONNET | 46b49da | 2014-06-25 14:38:07 -0700 | [diff] [blame] | 90 | } |