Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(421)

Side by Side Diff: components/metrics/persisted_logs.cc

Issue 2358223002: Add log date to the metrics log (Closed)
Patch Set: add deprectaed comments Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « components/metrics/persisted_logs.h ('k') | components/metrics/persisted_logs_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 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 "components/metrics/persisted_logs.h" 5 #include "components/metrics/persisted_logs.h"
6 6
7 #include <memory>
7 #include <string> 8 #include <string>
9 #include <utility>
8 10
9 #include "base/base64.h" 11 #include "base/base64.h"
10 #include "base/md5.h" 12 #include "base/md5.h"
11 #include "base/metrics/histogram_macros.h" 13 #include "base/metrics/histogram_macros.h"
12 #include "base/sha1.h" 14 #include "base/sha1.h"
15 #include "base/strings/string_number_conversions.h"
13 #include "base/timer/elapsed_timer.h" 16 #include "base/timer/elapsed_timer.h"
14 #include "components/prefs/pref_service.h" 17 #include "components/prefs/pref_service.h"
15 #include "components/prefs/scoped_user_pref_update.h" 18 #include "components/prefs/scoped_user_pref_update.h"
16 #include "third_party/zlib/google/compression_utils.h" 19 #include "third_party/zlib/google/compression_utils.h"
17 20
18 namespace metrics { 21 namespace metrics {
19 22
20 namespace { 23 namespace {
21 24
25 const char kLogHashKey[] = "hash";
26 const char kLogTimestampKey[] = "timestamp";
27 const char kLogDataKey[] = "data";
28
22 PersistedLogs::LogReadStatus MakeRecallStatusHistogram( 29 PersistedLogs::LogReadStatus MakeRecallStatusHistogram(
23 PersistedLogs::LogReadStatus status) { 30 PersistedLogs::LogReadStatus status) {
24 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs", 31 UMA_HISTOGRAM_ENUMERATION("PrefService.PersistentLogRecallProtobufs",
25 status, PersistedLogs::END_RECALL_STATUS); 32 status, PersistedLogs::END_RECALL_STATUS);
26 return status; 33 return status;
27 } 34 }
28 35
29 // Reads the value at |index| from |list_value| as a string and Base64-decodes 36 // Reads the value at |index| from |list_value| as a string and Base64-decodes
30 // it into |result|. Returns true on success. 37 // it into |result|. Returns true on success.
31 bool ReadBase64String(const base::ListValue& list_value, 38 bool ReadBase64String(const base::ListValue& list_value,
32 size_t index, 39 size_t index,
33 std::string* result) { 40 std::string* result) {
34 std::string base64_result; 41 std::string base64_result;
35 if (!list_value.GetString(index, &base64_result)) 42 if (!list_value.GetString(index, &base64_result))
36 return false; 43 return false;
37 return base::Base64Decode(base64_result, result); 44 return base::Base64Decode(base64_result, result);
38 } 45 }
39 46
40 // Base64-encodes |str| and appends the result to |list_value|. 47 std::string EncodeToBase64(const std::string& to_convert) {
41 void AppendBase64String(const std::string& str, base::ListValue* list_value) { 48 std::string base64_result;
42 std::string base64_str; 49 base::Base64Encode(to_convert, &base64_result);
43 base::Base64Encode(str, &base64_str); 50 return base64_result;
44 list_value->AppendString(base64_str); 51 }
52
53 std::string DecodeFromBase64(const std::string& to_convert) {
54 std::string result;
55 base::Base64Decode(to_convert, &result);
56 return result;
45 } 57 }
46 58
47 } // namespace 59 } // namespace
48 60
49 void PersistedLogs::LogHashPair::Init(const std::string& log_data) { 61 void PersistedLogs::LogInfo::Init(const std::string& log_data,
62 const std::string& log_timestamp) {
50 DCHECK(!log_data.empty()); 63 DCHECK(!log_data.empty());
51 64
52 if (!compression::GzipCompress(log_data, &compressed_log_data)) { 65 if (!compression::GzipCompress(log_data, &compressed_log_data)) {
53 NOTREACHED(); 66 NOTREACHED();
54 return; 67 return;
55 } 68 }
56 69
57 UMA_HISTOGRAM_PERCENTAGE( 70 UMA_HISTOGRAM_PERCENTAGE(
58 "UMA.ProtoCompressionRatio", 71 "UMA.ProtoCompressionRatio",
59 static_cast<int>(100 * compressed_log_data.size() / log_data.size())); 72 static_cast<int>(100 * compressed_log_data.size() / log_data.size()));
60 73
61 hash = base::SHA1HashString(log_data); 74 hash = base::SHA1HashString(log_data);
75 timestamp = log_timestamp;
62 } 76 }
63 77
64 PersistedLogs::PersistedLogs(PrefService* local_state, 78 PersistedLogs::PersistedLogs(PrefService* local_state,
65 const char* pref_name, 79 const char* pref_name,
80 const char* outdated_pref_name,
66 size_t min_log_count, 81 size_t min_log_count,
67 size_t min_log_bytes, 82 size_t min_log_bytes,
68 size_t max_log_size) 83 size_t max_log_size)
69 : local_state_(local_state), 84 : local_state_(local_state),
70 pref_name_(pref_name), 85 pref_name_(pref_name),
86 outdated_pref_name_(outdated_pref_name),
71 min_log_count_(min_log_count), 87 min_log_count_(min_log_count),
72 min_log_bytes_(min_log_bytes), 88 min_log_bytes_(min_log_bytes),
73 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)), 89 max_log_size_(max_log_size != 0 ? max_log_size : static_cast<size_t>(-1)),
74 staged_log_index_(-1) { 90 staged_log_index_(-1) {
75 DCHECK(local_state_); 91 DCHECK(local_state_);
76 // One of the limit arguments must be non-zero. 92 // One of the limit arguments must be non-zero.
77 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0); 93 DCHECK(min_log_count_ > 0 || min_log_bytes_ > 0);
78 } 94 }
79 95
80 PersistedLogs::~PersistedLogs() {} 96 PersistedLogs::~PersistedLogs() {}
81 97
82 void PersistedLogs::SerializeLogs() const { 98 void PersistedLogs::SerializeLogs() const {
83 ListPrefUpdate update(local_state_, pref_name_); 99 ListPrefUpdate update(local_state_, pref_name_);
84 WriteLogsToPrefList(update.Get()); 100 WriteLogsToPrefList(update.Get());
101
102 // After writing all the logs to the new pref remove old outdated pref.
103 // TODO(gayane): Remove when all users are migrated. crbug.com/649440
104 if (local_state_->HasPrefPath(outdated_pref_name_))
105 local_state_->ClearPref(outdated_pref_name_);
85 } 106 }
86 107
87 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() { 108 PersistedLogs::LogReadStatus PersistedLogs::DeserializeLogs() {
109 // TODO(gayane): Remove the code for reading logs from outdated pref when all
110 // users are migrated. crbug.com/649440
111 if (local_state_->HasPrefPath(outdated_pref_name_)) {
112 return ReadLogsFromOldFormatPrefList(
113 *local_state_->GetList(outdated_pref_name_));
114 }
88 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_)); 115 return ReadLogsFromPrefList(*local_state_->GetList(pref_name_));
89 } 116 }
90 117
91 void PersistedLogs::StoreLog(const std::string& log_data) { 118 void PersistedLogs::StoreLog(const std::string& log_data) {
92 list_.push_back(LogHashPair()); 119 list_.push_back(LogInfo());
93 list_.back().Init(log_data); 120 list_.back().Init(log_data, base::Int64ToString(base::Time::Now().ToTimeT()));
94 } 121 }
95 122
96 void PersistedLogs::StageLog() { 123 void PersistedLogs::StageLog() {
97 // CHECK, rather than DCHECK, because swap()ing with an empty list causes 124 // CHECK, rather than DCHECK, because swap()ing with an empty list causes
98 // hard-to-identify crashes much later. 125 // hard-to-identify crashes much later.
99 CHECK(!list_.empty()); 126 CHECK(!list_.empty());
100 DCHECK(!has_staged_log()); 127 DCHECK(!has_staged_log());
101 staged_log_index_ = list_.size() - 1; 128 staged_log_index_ = list_.size() - 1;
102 DCHECK(has_staged_log()); 129 DCHECK(has_staged_log());
103 } 130 }
104 131
105 void PersistedLogs::DiscardStagedLog() { 132 void PersistedLogs::DiscardStagedLog() {
106 DCHECK(has_staged_log()); 133 DCHECK(has_staged_log());
107 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size()); 134 DCHECK_LT(static_cast<size_t>(staged_log_index_), list_.size());
108 list_.erase(list_.begin() + staged_log_index_); 135 list_.erase(list_.begin() + staged_log_index_);
109 staged_log_index_ = -1; 136 staged_log_index_ = -1;
110 } 137 }
111 138
139 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList(
140 const base::ListValue& list_value) {
141 if (list_value.empty())
142 return MakeRecallStatusHistogram(LIST_EMPTY);
143
144 const size_t log_count = list_value.GetSize();
145
146 DCHECK(list_.empty());
147 list_.resize(log_count);
148
149 for (size_t i = 0; i < log_count; ++i) {
150 const base::DictionaryValue* dict;
151 if (!list_value.GetDictionary(i, &dict) ||
152 !dict->GetString(kLogDataKey, &list_[i].compressed_log_data) ||
153 !dict->GetString(kLogHashKey, &list_[i].hash)) {
154 list_.clear();
155 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
156 }
157
158 list_[i].compressed_log_data =
159 DecodeFromBase64(list_[i].compressed_log_data);
160 list_[i].hash = DecodeFromBase64(list_[i].hash);
161 // Ignoring the success of this step as timestamp might not be there for
162 // older logs.
163 // NOTE: Should be added to the check with other fields once migration is
164 // over.
165 dict->GetString(kLogTimestampKey, &list_[i].timestamp);
166 }
167
168 return MakeRecallStatusHistogram(RECALL_SUCCESS);
169 }
170
112 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const { 171 void PersistedLogs::WriteLogsToPrefList(base::ListValue* list_value) const {
113 list_value->Clear(); 172 list_value->Clear();
114 173
115 // Keep the most recent logs which are smaller than |max_log_size_|. 174 // Keep the most recent logs which are smaller than |max_log_size_|.
116 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before 175 // We keep at least |min_log_bytes_| and |min_log_count_| of logs before
117 // discarding older logs. 176 // discarding older logs.
118 size_t start = list_.size(); 177 size_t start = list_.size();
119 size_t saved_log_count = 0; 178 size_t saved_log_count = 0;
120 size_t bytes_used = 0; 179 size_t bytes_used = 0;
121 for (; start > 0; --start) { 180 for (; start > 0; --start) {
(...skipping 11 matching lines...) Expand all
133 int dropped_logs_num = start - 1; 192 int dropped_logs_num = start - 1;
134 193
135 for (size_t i = start; i < list_.size(); ++i) { 194 for (size_t i = start; i < list_.size(); ++i) {
136 size_t log_size = list_[i].compressed_log_data.length(); 195 size_t log_size = list_[i].compressed_log_data.length();
137 if (log_size > max_log_size_) { 196 if (log_size > max_log_size_) {
138 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", 197 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted",
139 static_cast<int>(log_size)); 198 static_cast<int>(log_size));
140 dropped_logs_num++; 199 dropped_logs_num++;
141 continue; 200 continue;
142 } 201 }
143 AppendBase64String(list_[i].compressed_log_data, list_value); 202 std::unique_ptr<base::DictionaryValue> dict_value(
144 AppendBase64String(list_[i].hash, list_value); 203 new base::DictionaryValue);
204 dict_value->SetString(kLogHashKey, EncodeToBase64(list_[i].hash));
205 dict_value->SetString(kLogDataKey,
206 EncodeToBase64(list_[i].compressed_log_data));
207 dict_value->SetString(kLogTimestampKey, list_[i].timestamp);
208 list_value->Append(std::move(dict_value));
145 } 209 }
146 if (dropped_logs_num > 0) 210 if (dropped_logs_num > 0)
147 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num); 211 UMA_HISTOGRAM_COUNTS("UMA.UnsentLogs.Dropped", dropped_logs_num);
148 } 212 }
149 213
150 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromPrefList( 214 PersistedLogs::LogReadStatus PersistedLogs::ReadLogsFromOldFormatPrefList(
151 const base::ListValue& list_value) { 215 const base::ListValue& list_value) {
152 if (list_value.empty()) 216 if (list_value.empty())
153 return MakeRecallStatusHistogram(LIST_EMPTY); 217 return MakeRecallStatusHistogram(LIST_EMPTY);
154 218
155 // For each log, there's two entries in the list (the data and the hash). 219 // For each log, there's two entries in the list (the data and the hash).
156 DCHECK_EQ(0U, list_value.GetSize() % 2); 220 DCHECK_EQ(0U, list_value.GetSize() % 2);
157 const size_t log_count = list_value.GetSize() / 2; 221 const size_t log_count = list_value.GetSize() / 2;
158 222
159 // Resize |list_| ahead of time, so that values can be decoded directly into 223 // Resize |list_| ahead of time, so that values can be decoded directly into
160 // the elements of the list. 224 // the elements of the list.
161 DCHECK(list_.empty()); 225 DCHECK(list_.empty());
162 list_.resize(log_count); 226 list_.resize(log_count);
163 227
164 for (size_t i = 0; i < log_count; ++i) { 228 for (size_t i = 0; i < log_count; ++i) {
165 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) || 229 if (!ReadBase64String(list_value, i * 2, &list_[i].compressed_log_data) ||
166 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) { 230 !ReadBase64String(list_value, i * 2 + 1, &list_[i].hash)) {
167 list_.clear(); 231 list_.clear();
168 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION); 232 return MakeRecallStatusHistogram(LOG_STRING_CORRUPTION);
169 } 233 }
170 } 234 }
171 235
172 return MakeRecallStatusHistogram(RECALL_SUCCESS); 236 return MakeRecallStatusHistogram(RECALL_SUCCESS);
173 } 237 }
174 238
175 } // namespace metrics 239 } // namespace metrics
OLDNEW
« no previous file with comments | « components/metrics/persisted_logs.h ('k') | components/metrics/persisted_logs_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698