| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/common/metrics/metrics_log_manager.h" | 5 #include "chrome/common/metrics/metrics_log_manager.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 | 8 |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/sha1.h" | 10 #include "base/sha1.h" |
| 11 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 12 #include "base/timer/elapsed_timer.h" |
| 12 #include "chrome/common/metrics/metrics_log_base.h" | 13 #include "chrome/common/metrics/metrics_log_base.h" |
| 13 | 14 |
| 14 MetricsLogManager::SerializedLog::SerializedLog() {} | 15 MetricsLogManager::SerializedLog::SerializedLog() {} |
| 15 MetricsLogManager::SerializedLog::~SerializedLog() {} | 16 MetricsLogManager::SerializedLog::~SerializedLog() {} |
| 16 | 17 |
| 17 bool MetricsLogManager::SerializedLog::IsEmpty() const { | 18 bool MetricsLogManager::SerializedLog::IsEmpty() const { |
| 18 return log_text_.empty(); | 19 return log_text_.empty(); |
| 19 } | 20 } |
| 20 | 21 |
| 21 void MetricsLogManager::SerializedLog::SwapLogText(std::string* log_text) { | 22 void MetricsLogManager::SerializedLog::SwapLogText(std::string* log_text) { |
| 22 log_text_.swap(*log_text); | 23 log_text_.swap(*log_text); |
| 23 if (log_text_.empty()) | 24 if (log_text_.empty()) |
| 24 log_hash_.clear(); | 25 log_hash_.clear(); |
| 25 else | 26 else |
| 26 log_hash_ = base::SHA1HashString(log_text_); | 27 log_hash_ = base::SHA1HashString(log_text_); |
| 27 } | 28 } |
| 28 | 29 |
| 29 void MetricsLogManager::SerializedLog::Clear() { | 30 void MetricsLogManager::SerializedLog::Clear() { |
| 30 log_text_.clear(); | 31 log_text_.clear(); |
| 31 log_hash_.clear(); | 32 log_hash_.clear(); |
| 32 } | 33 } |
| 33 | 34 |
| 34 void MetricsLogManager::SerializedLog::Swap( | 35 void MetricsLogManager::SerializedLog::Swap( |
| 35 MetricsLogManager::SerializedLog* other) { | 36 MetricsLogManager::SerializedLog* other) { |
| 36 log_text_.swap(other->log_text_); | 37 log_text_.swap(other->log_text_); |
| 37 log_hash_.swap(other->log_hash_); | 38 log_hash_.swap(other->log_hash_); |
| 38 } | 39 } |
| 39 | 40 |
| 40 MetricsLogManager::MetricsLogManager() | 41 MetricsLogManager::MetricsLogManager() |
| 41 : current_log_type_(MetricsLogBase::NO_LOG), | 42 : unsent_logs_loaded_(false), |
| 43 current_log_type_(MetricsLogBase::NO_LOG), |
| 42 paused_log_type_(MetricsLogBase::NO_LOG), | 44 paused_log_type_(MetricsLogBase::NO_LOG), |
| 43 staged_log_type_(MetricsLogBase::NO_LOG), | 45 staged_log_type_(MetricsLogBase::NO_LOG), |
| 44 max_ongoing_log_store_size_(0), | 46 max_ongoing_log_store_size_(0), |
| 45 last_provisional_store_index_(-1), | 47 last_provisional_store_index_(-1), |
| 46 last_provisional_store_type_(MetricsLogBase::INITIAL_LOG) {} | 48 last_provisional_store_type_(MetricsLogBase::INITIAL_LOG) {} |
| 47 | 49 |
| 48 MetricsLogManager::~MetricsLogManager() {} | 50 MetricsLogManager::~MetricsLogManager() {} |
| 49 | 51 |
| 50 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log, | 52 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log, |
| 51 LogType log_type) { | 53 LogType log_type) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 160 DCHECK_LT(static_cast<unsigned int>(last_provisional_store_index_), | 162 DCHECK_LT(static_cast<unsigned int>(last_provisional_store_index_), |
| 161 source_list->size()); | 163 source_list->size()); |
| 162 source_list->erase(source_list->begin() + last_provisional_store_index_); | 164 source_list->erase(source_list->begin() + last_provisional_store_index_); |
| 163 last_provisional_store_index_ = -1; | 165 last_provisional_store_index_ = -1; |
| 164 } | 166 } |
| 165 | 167 |
| 166 void MetricsLogManager::PersistUnsentLogs() { | 168 void MetricsLogManager::PersistUnsentLogs() { |
| 167 DCHECK(log_serializer_.get()); | 169 DCHECK(log_serializer_.get()); |
| 168 if (!log_serializer_.get()) | 170 if (!log_serializer_.get()) |
| 169 return; | 171 return; |
| 172 DCHECK(unsent_logs_loaded_); |
| 173 if (!unsent_logs_loaded_) |
| 174 return; |
| 175 |
| 176 base::ElapsedTimer timer; |
| 170 // Remove any ongoing logs that are over the serialization size limit. | 177 // Remove any ongoing logs that are over the serialization size limit. |
| 171 if (max_ongoing_log_store_size_) { | 178 if (max_ongoing_log_store_size_) { |
| 172 for (std::vector<SerializedLog>::iterator it = unsent_ongoing_logs_.begin(); | 179 for (std::vector<SerializedLog>::iterator it = unsent_ongoing_logs_.begin(); |
| 173 it != unsent_ongoing_logs_.end();) { | 180 it != unsent_ongoing_logs_.end();) { |
| 174 size_t log_size = it->log_text().length(); | 181 size_t log_size = it->log_text().length(); |
| 175 if (log_size > max_ongoing_log_store_size_) { | 182 if (log_size > max_ongoing_log_store_size_) { |
| 176 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", | 183 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
| 177 static_cast<int>(log_size)); | 184 static_cast<int>(log_size)); |
| 178 it = unsent_ongoing_logs_.erase(it); | 185 it = unsent_ongoing_logs_.erase(it); |
| 179 } else { | 186 } else { |
| 180 ++it; | 187 ++it; |
| 181 } | 188 } |
| 182 } | 189 } |
| 183 } | 190 } |
| 184 log_serializer_->SerializeLogs(unsent_initial_logs_, | 191 log_serializer_->SerializeLogs(unsent_initial_logs_, |
| 185 MetricsLogBase::INITIAL_LOG); | 192 MetricsLogBase::INITIAL_LOG); |
| 186 log_serializer_->SerializeLogs(unsent_ongoing_logs_, | 193 log_serializer_->SerializeLogs(unsent_ongoing_logs_, |
| 187 MetricsLogBase::ONGOING_LOG); | 194 MetricsLogBase::ONGOING_LOG); |
| 195 UMA_HISTOGRAM_TIMES("UMA.StoreLogsTime", timer.Elapsed()); |
| 188 } | 196 } |
| 189 | 197 |
| 190 void MetricsLogManager::LoadPersistedUnsentLogs() { | 198 void MetricsLogManager::LoadPersistedUnsentLogs() { |
| 191 DCHECK(log_serializer_.get()); | 199 DCHECK(log_serializer_.get()); |
| 192 if (!log_serializer_.get()) | 200 if (!log_serializer_.get()) |
| 193 return; | 201 return; |
| 202 |
| 203 base::ElapsedTimer timer; |
| 194 log_serializer_->DeserializeLogs(MetricsLogBase::INITIAL_LOG, | 204 log_serializer_->DeserializeLogs(MetricsLogBase::INITIAL_LOG, |
| 195 &unsent_initial_logs_); | 205 &unsent_initial_logs_); |
| 196 log_serializer_->DeserializeLogs(MetricsLogBase::ONGOING_LOG, | 206 log_serializer_->DeserializeLogs(MetricsLogBase::ONGOING_LOG, |
| 197 &unsent_ongoing_logs_); | 207 &unsent_ongoing_logs_); |
| 208 UMA_HISTOGRAM_TIMES("UMA.LoadLogsTime", timer.Elapsed()); |
| 209 |
| 210 unsent_logs_loaded_ = true; |
| 198 } | 211 } |
| 199 | 212 |
| 200 void MetricsLogManager::CompressCurrentLog(SerializedLog* compressed_log) { | 213 void MetricsLogManager::CompressCurrentLog(SerializedLog* compressed_log) { |
| 201 std::string log_text; | 214 std::string log_text; |
| 202 current_log_->GetEncodedLog(&log_text); | 215 current_log_->GetEncodedLog(&log_text); |
| 203 compressed_log->SwapLogText(&log_text); | 216 compressed_log->SwapLogText(&log_text); |
| 204 } | 217 } |
| OLD | NEW |