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

Side by Side Diff: chrome/browser/metrics/metrics_log_serializer.cc

Issue 9562007: Add a minimum byte count to metrics log serialization (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Change limit terminology, |start| decrement logic Created 8 years, 7 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 | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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/browser/metrics/metrics_log_serializer.h" 5 #include "chrome/browser/metrics/metrics_log_serializer.h"
6 6
7 #include "base/base64.h" 7 #include "base/base64.h"
8 #include "base/md5.h" 8 #include "base/md5.h"
9 #include "base/metrics/histogram.h" 9 #include "base/metrics/histogram.h"
10 #include "chrome/browser/browser_process.h" 10 #include "chrome/browser/browser_process.h"
(...skipping 12 matching lines...) Expand all
23 // send during a this or future sessions. Note that each log may be pretty 23 // send during a this or future sessions. Note that each log may be pretty
24 // large, as presumably the related "initial" log wasn't sent (probably nothing 24 // large, as presumably the related "initial" log wasn't sent (probably nothing
25 // was, as the user was probably off-line). As a result, the log probably kept 25 // was, as the user was probably off-line). As a result, the log probably kept
26 // accumulating while the "initial" log was stalled, and couldn't be sent. As a 26 // accumulating while the "initial" log was stalled, and couldn't be sent. As a
27 // result, we don't want to save too many of these mega-logs. 27 // result, we don't want to save too many of these mega-logs.
28 // A "standard shutdown" will create a small log, including just the data that 28 // A "standard shutdown" will create a small log, including just the data that
29 // was not yet been transmitted, and that is normal (to have exactly one 29 // was not yet been transmitted, and that is normal (to have exactly one
30 // ongoing_log_ at startup). 30 // ongoing_log_ at startup).
31 const size_t kMaxOngoingLogsPersisted = 8; 31 const size_t kMaxOngoingLogsPersisted = 8;
32 32
33 // The number of bytes each of initial and ongoing logs that must be reached
34 // before the count-based limits above will be enforced. This ensures that a
35 // reasonable amount of history will be stored even if there is a long series
36 // of very small logs.
37 const size_t kMinStorageBytesPerLogType = 300000;
38
33 // We append (2) more elements to persisted lists: the size of the list and a 39 // We append (2) more elements to persisted lists: the size of the list and a
34 // checksum of the elements. 40 // checksum of the elements.
35 const size_t kChecksumEntryCount = 2; 41 const size_t kChecksumEntryCount = 2;
36 42
37 // TODO(isherman): Remove this histogram once it's confirmed that there are no 43 // TODO(isherman): Remove this histogram once it's confirmed that there are no
38 // encoding failures for protobuf logs. 44 // encoding failures for protobuf logs.
39 enum LogStoreStatus { 45 enum LogStoreStatus {
40 STORE_SUCCESS, // Successfully presisted log. 46 STORE_SUCCESS, // Successfully presisted log.
41 ENCODE_FAIL, // Failed to encode log. 47 ENCODE_FAIL, // Failed to encode log.
42 COMPRESS_FAIL, // Failed to compress log. 48 COMPRESS_FAIL, // Failed to compress log.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
87 pref_proto = prefs::kMetricsOngoingLogsProto; 93 pref_proto = prefs::kMetricsOngoingLogsProto;
88 max_store_count = kMaxOngoingLogsPersisted; 94 max_store_count = kMaxOngoingLogsPersisted;
89 break; 95 break;
90 default: 96 default:
91 NOTREACHED(); 97 NOTREACHED();
92 return; 98 return;
93 }; 99 };
94 100
95 // Write the XML version. 101 // Write the XML version.
96 ListPrefUpdate update_xml(local_state, pref_xml); 102 ListPrefUpdate update_xml(local_state, pref_xml);
97 WriteLogsToPrefList(logs, true, max_store_count, update_xml.Get()); 103 WriteLogsToPrefList(logs, true, max_store_count, kMinStorageBytesPerLogType,
104 update_xml.Get());
98 105
99 // Write the protobuf version. 106 // Write the protobuf version.
100 ListPrefUpdate update_proto(local_state, pref_proto); 107 ListPrefUpdate update_proto(local_state, pref_proto);
101 WriteLogsToPrefList(logs, false, max_store_count, update_proto.Get()); 108 WriteLogsToPrefList(logs, false, max_store_count, kMinStorageBytesPerLogType,
109 update_proto.Get());
102 } 110 }
103 111
104 void MetricsLogSerializer::DeserializeLogs( 112 void MetricsLogSerializer::DeserializeLogs(
105 MetricsLogManager::LogType log_type, 113 MetricsLogManager::LogType log_type,
106 std::vector<MetricsLogManager::SerializedLog>* logs) { 114 std::vector<MetricsLogManager::SerializedLog>* logs) {
107 DCHECK(logs); 115 DCHECK(logs);
108 PrefService* local_state = g_browser_process->local_state(); 116 PrefService* local_state = g_browser_process->local_state();
109 DCHECK(local_state); 117 DCHECK(local_state);
110 118
111 const char* pref_xml; 119 const char* pref_xml;
(...skipping 12 matching lines...) Expand all
124 // In order to try to keep the data sent to both servers roughly in sync, 132 // In order to try to keep the data sent to both servers roughly in sync,
125 // only read the protobuf data if we read the XML data successfully. 133 // only read the protobuf data if we read the XML data successfully.
126 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); 134 ReadLogsFromPrefList(*unsent_logs_proto, false, logs);
127 } 135 }
128 } 136 }
129 137
130 // static 138 // static
131 void MetricsLogSerializer::WriteLogsToPrefList( 139 void MetricsLogSerializer::WriteLogsToPrefList(
132 const std::vector<MetricsLogManager::SerializedLog>& local_list, 140 const std::vector<MetricsLogManager::SerializedLog>& local_list,
133 bool is_xml, 141 bool is_xml,
134 size_t max_list_size, 142 size_t list_length_limit,
143 size_t byte_limit,
135 base::ListValue* list) { 144 base::ListValue* list) {
145 // One of the limit arguments must be non-zero.
146 DCHECK(list_length_limit > 0 || byte_limit > 0);
147
136 list->Clear(); 148 list->Clear();
149 if (local_list.size() == 0)
150 return;
151
137 size_t start = 0; 152 size_t start = 0;
138 if (local_list.size() > max_list_size) 153 // If there are too many logs, keep the most recent logs up to the length
139 start = local_list.size() - max_list_size; 154 // limit, and at least to the minimum number of bytes.
140 DCHECK_LE(start, local_list.size()); 155 if (local_list.size() > list_length_limit) {
141 if (local_list.size() <= start) 156 start = local_list.size();
142 return; 157 size_t bytes_used = 0;
158 for (std::vector<MetricsLogManager::SerializedLog>::const_reverse_iterator
159 it = local_list.rbegin(); it != local_list.rend(); ++it) {
160 // TODO(isherman): Always uses XML length so both formats of a given log
161 // will be saved; switch to proto once that's the primary format.
162 size_t log_size = it->xml.length();
163 if (bytes_used >= byte_limit &&
164 (local_list.size() - start) >= list_length_limit)
jar (doing other things) 2012/05/15 18:27:40 You probably want ">" rather than ">=" now. Curre
stuartmorgan 2012/05/15 19:02:58 No, because it's an &&. To satisfy both conditions
165 break;
166 bytes_used += log_size;
167 --start;
168 }
169 }
170 DCHECK_LT(start, local_list.size());
143 171
144 // Store size at the beginning of the list. 172 // Store size at the beginning of the list.
145 list->Append(Value::CreateIntegerValue(local_list.size() - start)); 173 list->Append(Value::CreateIntegerValue(local_list.size() - start));
146 174
147 base::MD5Context ctx; 175 base::MD5Context ctx;
148 base::MD5Init(&ctx); 176 base::MD5Init(&ctx);
149 std::string encoded_log; 177 std::string encoded_log;
150 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = 178 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it =
151 local_list.begin() + start; 179 local_list.begin() + start;
152 it != local_list.end(); ++it) { 180 it != local_list.end(); ++it) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
236 if (!valid) { 264 if (!valid) {
237 local_list->clear(); 265 local_list->clear();
238 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); 266 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml);
239 } 267 }
240 if (recovered_md5 != base::MD5DigestToBase16(digest)) { 268 if (recovered_md5 != base::MD5DigestToBase16(digest)) {
241 local_list->clear(); 269 local_list->clear();
242 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); 270 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml);
243 } 271 }
244 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); 272 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml);
245 } 273 }
OLDNEW
« no previous file with comments | « chrome/browser/metrics/metrics_log_serializer.h ('k') | chrome/browser/metrics/metrics_log_serializer_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698