| 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/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
| 11 #include "chrome/common/metrics/metrics_log_base.h" | 12 #include "chrome/common/metrics/metrics_log_base.h" |
| 12 | 13 |
| 14 MetricsLogManager::SerializedLog::SerializedLog() {} |
| 15 MetricsLogManager::SerializedLog::~SerializedLog() {} |
| 16 |
| 17 bool MetricsLogManager::SerializedLog::IsEmpty() const { |
| 18 return log_text_.empty(); |
| 19 } |
| 20 |
| 21 void MetricsLogManager::SerializedLog::SwapLogText(std::string* log_text) { |
| 22 log_text_.swap(*log_text); |
| 23 if (log_text_.empty()) |
| 24 log_hash_.clear(); |
| 25 else |
| 26 log_hash_ = base::SHA1HashString(log_text_); |
| 27 } |
| 28 |
| 29 void MetricsLogManager::SerializedLog::Clear() { |
| 30 log_text_.clear(); |
| 31 log_hash_.clear(); |
| 32 } |
| 33 |
| 34 void MetricsLogManager::SerializedLog::Swap( |
| 35 MetricsLogManager::SerializedLog* other) { |
| 36 log_text_.swap(other->log_text_); |
| 37 log_hash_.swap(other->log_hash_); |
| 38 } |
| 39 |
| 13 MetricsLogManager::MetricsLogManager() | 40 MetricsLogManager::MetricsLogManager() |
| 14 : current_log_type_(NO_LOG), | 41 : current_log_type_(NO_LOG), |
| 15 paused_log_type_(NO_LOG), | 42 paused_log_type_(NO_LOG), |
| 16 staged_log_type_(NO_LOG), | 43 staged_log_type_(NO_LOG), |
| 17 max_ongoing_log_store_size_(0), | 44 max_ongoing_log_store_size_(0), |
| 18 last_provisional_store_index_(-1), | 45 last_provisional_store_index_(-1), |
| 19 last_provisional_store_type_(INITIAL_LOG) {} | 46 last_provisional_store_type_(INITIAL_LOG) {} |
| 20 | 47 |
| 21 MetricsLogManager::~MetricsLogManager() {} | 48 MetricsLogManager::~MetricsLogManager() {} |
| 22 | 49 |
| 23 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log, | 50 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log, |
| 24 LogType log_type) { | 51 LogType log_type) { |
| 25 DCHECK(log_type != NO_LOG); | 52 DCHECK(log_type != NO_LOG); |
| 26 DCHECK(!current_log_.get()); | 53 DCHECK(!current_log_.get()); |
| 27 current_log_.reset(log); | 54 current_log_.reset(log); |
| 28 current_log_type_ = log_type; | 55 current_log_type_ = log_type; |
| 29 } | 56 } |
| 30 | 57 |
| 31 void MetricsLogManager::FinishCurrentLog() { | 58 void MetricsLogManager::FinishCurrentLog() { |
| 32 DCHECK(current_log_.get()); | 59 DCHECK(current_log_.get()); |
| 33 DCHECK(current_log_type_ != NO_LOG); | 60 DCHECK(current_log_type_ != NO_LOG); |
| 34 current_log_->CloseLog(); | 61 current_log_->CloseLog(); |
| 35 std::string compressed_log; | 62 SerializedLog compressed_log; |
| 36 CompressCurrentLog(&compressed_log); | 63 CompressCurrentLog(&compressed_log); |
| 37 if (!compressed_log.empty()) | 64 if (!compressed_log.IsEmpty()) |
| 38 StoreLog(&compressed_log, current_log_type_, NORMAL_STORE); | 65 StoreLog(&compressed_log, current_log_type_, NORMAL_STORE); |
| 39 current_log_.reset(); | 66 current_log_.reset(); |
| 40 current_log_type_ = NO_LOG; | 67 current_log_type_ = NO_LOG; |
| 41 } | 68 } |
| 42 | 69 |
| 43 void MetricsLogManager::StageNextLogForUpload() { | 70 void MetricsLogManager::StageNextLogForUpload() { |
| 44 // Prioritize initial logs for uploading. | 71 // Prioritize initial logs for uploading. |
| 45 std::vector<std::string>* source_list = | 72 std::vector<SerializedLog>* source_list = |
| 46 unsent_initial_logs_.empty() ? &unsent_ongoing_logs_ | 73 unsent_initial_logs_.empty() ? &unsent_ongoing_logs_ |
| 47 : &unsent_initial_logs_; | 74 : &unsent_initial_logs_; |
| 48 LogType source_type = (source_list == &unsent_ongoing_logs_) ? ONGOING_LOG | 75 LogType source_type = (source_list == &unsent_ongoing_logs_) ? ONGOING_LOG |
| 49 : INITIAL_LOG; | 76 : INITIAL_LOG; |
| 50 // CHECK, rather than DCHECK, because swap()ing with an empty list causes | 77 // CHECK, rather than DCHECK, because swap()ing with an empty list causes |
| 51 // hard-to-identify crashes much later. | 78 // hard-to-identify crashes much later. |
| 52 CHECK(!source_list->empty()); | 79 CHECK(!source_list->empty()); |
| 53 DCHECK(staged_log_text_.empty()); | 80 DCHECK(staged_log_.IsEmpty()); |
| 54 DCHECK(staged_log_type_ == NO_LOG); | 81 DCHECK(staged_log_type_ == NO_LOG); |
| 55 staged_log_text_.swap(source_list->back()); | 82 staged_log_.Swap(&source_list->back()); |
| 56 staged_log_type_ = source_type; | 83 staged_log_type_ = source_type; |
| 57 source_list->pop_back(); | 84 source_list->pop_back(); |
| 58 | 85 |
| 59 // If the staged log was the last provisional store, clear that. | 86 // If the staged log was the last provisional store, clear that. |
| 60 if (last_provisional_store_index_ != -1) { | 87 if (last_provisional_store_index_ != -1) { |
| 61 if (source_type == last_provisional_store_type_ && | 88 if (source_type == last_provisional_store_type_ && |
| 62 static_cast<unsigned int>(last_provisional_store_index_) == | 89 static_cast<unsigned int>(last_provisional_store_index_) == |
| 63 source_list->size()) { | 90 source_list->size()) { |
| 64 last_provisional_store_index_ = -1; | 91 last_provisional_store_index_ = -1; |
| 65 } | 92 } |
| 66 } | 93 } |
| 67 } | 94 } |
| 68 | 95 |
| 69 bool MetricsLogManager::has_staged_log() const { | 96 bool MetricsLogManager::has_staged_log() const { |
| 70 return !staged_log_text().empty(); | 97 return !staged_log_.IsEmpty(); |
| 71 } | 98 } |
| 72 | 99 |
| 73 void MetricsLogManager::DiscardStagedLog() { | 100 void MetricsLogManager::DiscardStagedLog() { |
| 74 staged_log_text_.clear(); | 101 staged_log_.Clear(); |
| 75 staged_log_type_ = NO_LOG; | 102 staged_log_type_ = NO_LOG; |
| 76 } | 103 } |
| 77 | 104 |
| 78 void MetricsLogManager::DiscardCurrentLog() { | 105 void MetricsLogManager::DiscardCurrentLog() { |
| 79 current_log_->CloseLog(); | 106 current_log_->CloseLog(); |
| 80 current_log_.reset(); | 107 current_log_.reset(); |
| 81 current_log_type_ = NO_LOG; | 108 current_log_type_ = NO_LOG; |
| 82 } | 109 } |
| 83 | 110 |
| 84 void MetricsLogManager::PauseCurrentLog() { | 111 void MetricsLogManager::PauseCurrentLog() { |
| 85 DCHECK(!paused_log_.get()); | 112 DCHECK(!paused_log_.get()); |
| 86 DCHECK(paused_log_type_ == NO_LOG); | 113 DCHECK(paused_log_type_ == NO_LOG); |
| 87 paused_log_.reset(current_log_.release()); | 114 paused_log_.reset(current_log_.release()); |
| 88 paused_log_type_ = current_log_type_; | 115 paused_log_type_ = current_log_type_; |
| 89 current_log_type_ = NO_LOG; | 116 current_log_type_ = NO_LOG; |
| 90 } | 117 } |
| 91 | 118 |
| 92 void MetricsLogManager::ResumePausedLog() { | 119 void MetricsLogManager::ResumePausedLog() { |
| 93 DCHECK(!current_log_.get()); | 120 DCHECK(!current_log_.get()); |
| 94 DCHECK(current_log_type_ == NO_LOG); | 121 DCHECK(current_log_type_ == NO_LOG); |
| 95 current_log_.reset(paused_log_.release()); | 122 current_log_.reset(paused_log_.release()); |
| 96 current_log_type_ = paused_log_type_; | 123 current_log_type_ = paused_log_type_; |
| 97 paused_log_type_ = NO_LOG; | 124 paused_log_type_ = NO_LOG; |
| 98 } | 125 } |
| 99 | 126 |
| 100 void MetricsLogManager::StoreStagedLogAsUnsent(StoreType store_type) { | 127 void MetricsLogManager::StoreStagedLogAsUnsent(StoreType store_type) { |
| 101 DCHECK(has_staged_log()); | 128 DCHECK(has_staged_log()); |
| 102 | 129 |
| 103 // If compressing the log failed, there's nothing to store. | 130 // If compressing the log failed, there's nothing to store. |
| 104 if (staged_log_text_.empty()) | 131 if (staged_log_.IsEmpty()) |
| 105 return; | 132 return; |
| 106 | 133 |
| 107 StoreLog(&staged_log_text_, staged_log_type_, store_type); | 134 StoreLog(&staged_log_, staged_log_type_, store_type); |
| 108 DiscardStagedLog(); | 135 DiscardStagedLog(); |
| 109 } | 136 } |
| 110 | 137 |
| 111 void MetricsLogManager::StoreLog(std::string* log_text, | 138 void MetricsLogManager::StoreLog(SerializedLog* log, |
| 112 LogType log_type, | 139 LogType log_type, |
| 113 StoreType store_type) { | 140 StoreType store_type) { |
| 114 DCHECK(log_type != NO_LOG); | 141 DCHECK(log_type != NO_LOG); |
| 115 std::vector<std::string>* destination_list = | 142 std::vector<SerializedLog>* destination_list = |
| 116 (log_type == INITIAL_LOG) ? &unsent_initial_logs_ | 143 (log_type == INITIAL_LOG) ? &unsent_initial_logs_ |
| 117 : &unsent_ongoing_logs_; | 144 : &unsent_ongoing_logs_; |
| 118 destination_list->push_back(std::string()); | 145 destination_list->push_back(SerializedLog()); |
| 119 destination_list->back().swap(*log_text); | 146 destination_list->back().Swap(log); |
| 120 | 147 |
| 121 if (store_type == PROVISIONAL_STORE) { | 148 if (store_type == PROVISIONAL_STORE) { |
| 122 last_provisional_store_index_ = destination_list->size() - 1; | 149 last_provisional_store_index_ = destination_list->size() - 1; |
| 123 last_provisional_store_type_ = log_type; | 150 last_provisional_store_type_ = log_type; |
| 124 } | 151 } |
| 125 } | 152 } |
| 126 | 153 |
| 127 void MetricsLogManager::DiscardLastProvisionalStore() { | 154 void MetricsLogManager::DiscardLastProvisionalStore() { |
| 128 if (last_provisional_store_index_ == -1) | 155 if (last_provisional_store_index_ == -1) |
| 129 return; | 156 return; |
| 130 std::vector<std::string>* source_list = | 157 std::vector<SerializedLog>* source_list = |
| 131 (last_provisional_store_type_ == ONGOING_LOG) ? &unsent_ongoing_logs_ | 158 (last_provisional_store_type_ == ONGOING_LOG) ? &unsent_ongoing_logs_ |
| 132 : &unsent_initial_logs_; | 159 : &unsent_initial_logs_; |
| 133 DCHECK_LT(static_cast<unsigned int>(last_provisional_store_index_), | 160 DCHECK_LT(static_cast<unsigned int>(last_provisional_store_index_), |
| 134 source_list->size()); | 161 source_list->size()); |
| 135 source_list->erase(source_list->begin() + last_provisional_store_index_); | 162 source_list->erase(source_list->begin() + last_provisional_store_index_); |
| 136 last_provisional_store_index_ = -1; | 163 last_provisional_store_index_ = -1; |
| 137 } | 164 } |
| 138 | 165 |
| 139 void MetricsLogManager::PersistUnsentLogs() { | 166 void MetricsLogManager::PersistUnsentLogs() { |
| 140 DCHECK(log_serializer_.get()); | 167 DCHECK(log_serializer_.get()); |
| 141 if (!log_serializer_.get()) | 168 if (!log_serializer_.get()) |
| 142 return; | 169 return; |
| 143 // Remove any ongoing logs that are over the serialization size limit. | 170 // Remove any ongoing logs that are over the serialization size limit. |
| 144 if (max_ongoing_log_store_size_) { | 171 if (max_ongoing_log_store_size_) { |
| 145 for (std::vector<std::string>::iterator it = unsent_ongoing_logs_.begin(); | 172 for (std::vector<SerializedLog>::iterator it = unsent_ongoing_logs_.begin(); |
| 146 it != unsent_ongoing_logs_.end();) { | 173 it != unsent_ongoing_logs_.end();) { |
| 147 size_t log_size = it->length(); | 174 size_t log_size = it->log_text().length(); |
| 148 if (log_size > max_ongoing_log_store_size_) { | 175 if (log_size > max_ongoing_log_store_size_) { |
| 149 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", | 176 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
| 150 static_cast<int>(log_size)); | 177 static_cast<int>(log_size)); |
| 151 it = unsent_ongoing_logs_.erase(it); | 178 it = unsent_ongoing_logs_.erase(it); |
| 152 } else { | 179 } else { |
| 153 ++it; | 180 ++it; |
| 154 } | 181 } |
| 155 } | 182 } |
| 156 } | 183 } |
| 157 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); | 184 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); |
| 158 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); | 185 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); |
| 159 } | 186 } |
| 160 | 187 |
| 161 void MetricsLogManager::LoadPersistedUnsentLogs() { | 188 void MetricsLogManager::LoadPersistedUnsentLogs() { |
| 162 DCHECK(log_serializer_.get()); | 189 DCHECK(log_serializer_.get()); |
| 163 if (!log_serializer_.get()) | 190 if (!log_serializer_.get()) |
| 164 return; | 191 return; |
| 165 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); | 192 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); |
| 166 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); | 193 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); |
| 167 } | 194 } |
| 168 | 195 |
| 169 void MetricsLogManager::CompressCurrentLog(std::string* compressed_log) { | 196 void MetricsLogManager::CompressCurrentLog(SerializedLog* compressed_log) { |
| 170 current_log_->GetEncodedLog(compressed_log); | 197 std::string log_text; |
| 198 current_log_->GetEncodedLog(&log_text); |
| 199 compressed_log->SwapLogText(&log_text); |
| 171 } | 200 } |
| OLD | NEW |