OLD | NEW |
---|---|
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" |
11 #include "chrome/browser/prefs/pref_service.h" | 11 #include "chrome/browser/prefs/pref_service.h" |
12 #include "chrome/browser/prefs/scoped_user_pref_update.h" | 12 #include "chrome/browser/prefs/scoped_user_pref_update.h" |
13 #include "chrome/common/pref_names.h" | 13 #include "chrome/common/pref_names.h" |
14 | 14 |
15 namespace { | 15 namespace { |
16 | 16 |
17 // The number of "initial" logs we're willing to save, and hope to send during | 17 // The number of bytes each of initial and ongoing logs that will be stored. |
18 // a future Chrome session. Initial logs contain crash stats, and are pretty | 18 const size_t kMaxStorageBytesPerLogType = 300000; |
19 // small. | |
20 const size_t kMaxInitialLogsPersisted = 20; | |
21 | |
22 // The number of ongoing logs we're willing to save persistently, and hope to | |
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 | |
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 | |
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 | |
29 // was not yet been transmitted, and that is normal (to have exactly one | |
30 // ongoing_log_ at startup). | |
31 const size_t kMaxOngoingLogsPersisted = 8; | |
32 | 19 |
33 // We append (2) more elements to persisted lists: the size of the list and a | 20 // We append (2) more elements to persisted lists: the size of the list and a |
34 // checksum of the elements. | 21 // checksum of the elements. |
35 const size_t kChecksumEntryCount = 2; | 22 const size_t kChecksumEntryCount = 2; |
36 | 23 |
37 // TODO(isherman): Remove this histogram once it's confirmed that there are no | 24 // TODO(isherman): Remove this histogram once it's confirmed that there are no |
38 // encoding failures for protobuf logs. | 25 // encoding failures for protobuf logs. |
39 enum LogStoreStatus { | 26 enum LogStoreStatus { |
40 STORE_SUCCESS, // Successfully presisted log. | 27 STORE_SUCCESS, // Successfully presisted log. |
41 ENCODE_FAIL, // Failed to encode log. | 28 ENCODE_FAIL, // Failed to encode log. |
(...skipping 26 matching lines...) Expand all Loading... | |
68 | 55 |
69 MetricsLogSerializer::~MetricsLogSerializer() {} | 56 MetricsLogSerializer::~MetricsLogSerializer() {} |
70 | 57 |
71 void MetricsLogSerializer::SerializeLogs( | 58 void MetricsLogSerializer::SerializeLogs( |
72 const std::vector<MetricsLogManager::SerializedLog>& logs, | 59 const std::vector<MetricsLogManager::SerializedLog>& logs, |
73 MetricsLogManager::LogType log_type) { | 60 MetricsLogManager::LogType log_type) { |
74 PrefService* local_state = g_browser_process->local_state(); | 61 PrefService* local_state = g_browser_process->local_state(); |
75 DCHECK(local_state); | 62 DCHECK(local_state); |
76 const char* pref_xml = NULL; | 63 const char* pref_xml = NULL; |
77 const char* pref_proto = NULL; | 64 const char* pref_proto = NULL; |
78 size_t max_store_count = 0; | |
79 switch (log_type) { | 65 switch (log_type) { |
80 case MetricsLogManager::INITIAL_LOG: | 66 case MetricsLogManager::INITIAL_LOG: |
81 pref_xml = prefs::kMetricsInitialLogsXml; | 67 pref_xml = prefs::kMetricsInitialLogsXml; |
82 pref_proto = prefs::kMetricsInitialLogsProto; | 68 pref_proto = prefs::kMetricsInitialLogsProto; |
83 max_store_count = kMaxInitialLogsPersisted; | |
84 break; | 69 break; |
85 case MetricsLogManager::ONGOING_LOG: | 70 case MetricsLogManager::ONGOING_LOG: |
86 pref_xml = prefs::kMetricsOngoingLogsXml; | 71 pref_xml = prefs::kMetricsOngoingLogsXml; |
87 pref_proto = prefs::kMetricsOngoingLogsProto; | 72 pref_proto = prefs::kMetricsOngoingLogsProto; |
88 max_store_count = kMaxOngoingLogsPersisted; | |
89 break; | 73 break; |
90 default: | 74 default: |
91 NOTREACHED(); | 75 NOTREACHED(); |
92 return; | 76 return; |
93 }; | 77 }; |
94 | 78 |
95 // Write the XML version. | 79 // Write the XML version. |
96 ListPrefUpdate update_xml(local_state, pref_xml); | 80 ListPrefUpdate update_xml(local_state, pref_xml); |
97 WriteLogsToPrefList(logs, true, max_store_count, update_xml.Get()); | 81 WriteLogsToPrefList(logs, true, kMaxStorageBytesPerLogType, update_xml.Get()); |
98 | 82 |
99 // Write the protobuf version. | 83 // Write the protobuf version. |
100 ListPrefUpdate update_proto(local_state, pref_proto); | 84 ListPrefUpdate update_proto(local_state, pref_proto); |
101 WriteLogsToPrefList(logs, false, max_store_count, update_proto.Get()); | 85 WriteLogsToPrefList(logs, false, kMaxStorageBytesPerLogType, |
86 update_proto.Get()); | |
102 } | 87 } |
103 | 88 |
104 void MetricsLogSerializer::DeserializeLogs( | 89 void MetricsLogSerializer::DeserializeLogs( |
105 MetricsLogManager::LogType log_type, | 90 MetricsLogManager::LogType log_type, |
106 std::vector<MetricsLogManager::SerializedLog>* logs) { | 91 std::vector<MetricsLogManager::SerializedLog>* logs) { |
107 DCHECK(logs); | 92 DCHECK(logs); |
108 PrefService* local_state = g_browser_process->local_state(); | 93 PrefService* local_state = g_browser_process->local_state(); |
109 DCHECK(local_state); | 94 DCHECK(local_state); |
110 | 95 |
111 const char* pref_xml; | 96 const char* pref_xml; |
(...skipping 12 matching lines...) Expand all Loading... | |
124 // In order to try to keep the data sent to both servers roughly in sync, | 109 // 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. | 110 // only read the protobuf data if we read the XML data successfully. |
126 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); | 111 ReadLogsFromPrefList(*unsent_logs_proto, false, logs); |
127 } | 112 } |
128 } | 113 } |
129 | 114 |
130 // static | 115 // static |
131 void MetricsLogSerializer::WriteLogsToPrefList( | 116 void MetricsLogSerializer::WriteLogsToPrefList( |
132 const std::vector<MetricsLogManager::SerializedLog>& local_list, | 117 const std::vector<MetricsLogManager::SerializedLog>& local_list, |
133 bool is_xml, | 118 bool is_xml, |
134 size_t max_list_size, | 119 size_t max_bytes, |
135 base::ListValue* list) { | 120 base::ListValue* list) { |
136 list->Clear(); | 121 list->Clear(); |
137 size_t start = 0; | 122 |
138 if (local_list.size() > max_list_size) | 123 // Keep the most recent logs, up to the size limit. |
jar (doing other things)
2012/05/05 01:09:31
Although your proposal makes a lot of sense for mo
| |
139 start = local_list.size() - max_list_size; | 124 size_t start = local_list.size(); |
140 DCHECK_LE(start, local_list.size()); | 125 size_t bytes_used = 0; |
141 if (local_list.size() <= start) | 126 std::set<size_t> skip_indexes; |
127 for (std::vector<MetricsLogManager::SerializedLog>::const_reverse_iterator | |
128 it = local_list.rbegin(); it != local_list.rend(); ++it) { | |
129 // TODO(isherman): Always uses XML length so both formats of a given log | |
130 // will be saved; switch to proto once that's the primary format. | |
131 size_t log_size = it->xml.length(); | |
132 // If a log is individually larger than the max, skip it. | |
133 if (log_size > max_bytes) { | |
134 DLOG(WARNING) << "Individual UMA log was discarded as too large: " | |
135 << log_size << " bytes"; | |
136 skip_indexes.insert(start - 1); | |
137 } else { | |
138 bytes_used += log_size; | |
139 if (bytes_used > max_bytes) | |
140 break; | |
141 } | |
142 --start; | |
143 } | |
144 if (local_list.size() == 0 || | |
145 skip_indexes.size() == local_list.size() - start) | |
142 return; | 146 return; |
147 DCHECK_LT(start, local_list.size()); | |
143 | 148 |
144 // Store size at the beginning of the list. | 149 // Store size at the beginning of the list. |
145 list->Append(Value::CreateIntegerValue(local_list.size() - start)); | 150 list->Append(Value::CreateIntegerValue(local_list.size() - start - |
151 skip_indexes.size())); | |
146 | 152 |
147 base::MD5Context ctx; | 153 base::MD5Context ctx; |
148 base::MD5Init(&ctx); | 154 base::MD5Init(&ctx); |
149 std::string encoded_log; | 155 std::string encoded_log; |
156 size_t i = start; | |
150 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = | 157 for (std::vector<MetricsLogManager::SerializedLog>::const_iterator it = |
151 local_list.begin() + start; | 158 local_list.begin() + start; it != local_list.end(); ++it, ++i) { |
152 it != local_list.end(); ++it) { | 159 if (skip_indexes.count(i)) |
160 continue; | |
153 const std::string& value = is_xml ? it->xml : it->proto; | 161 const std::string& value = is_xml ? it->xml : it->proto; |
154 // We encode the compressed log as Value::CreateStringValue() expects to | 162 // We encode the compressed log as Value::CreateStringValue() expects to |
155 // take a valid UTF8 string. | 163 // take a valid UTF8 string. |
156 if (!base::Base64Encode(value, &encoded_log)) { | 164 if (!base::Base64Encode(value, &encoded_log)) { |
157 MakeStoreStatusHistogram(ENCODE_FAIL); | 165 MakeStoreStatusHistogram(ENCODE_FAIL); |
158 list->Clear(); | 166 list->Clear(); |
159 return; | 167 return; |
160 } | 168 } |
161 base::MD5Update(&ctx, encoded_log); | 169 base::MD5Update(&ctx, encoded_log); |
162 list->Append(Value::CreateStringValue(encoded_log)); | 170 list->Append(Value::CreateStringValue(encoded_log)); |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
236 if (!valid) { | 244 if (!valid) { |
237 local_list->clear(); | 245 local_list->clear(); |
238 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); | 246 return MakeRecallStatusHistogram(CHECKSUM_STRING_CORRUPTION, is_xml); |
239 } | 247 } |
240 if (recovered_md5 != base::MD5DigestToBase16(digest)) { | 248 if (recovered_md5 != base::MD5DigestToBase16(digest)) { |
241 local_list->clear(); | 249 local_list->clear(); |
242 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); | 250 return MakeRecallStatusHistogram(CHECKSUM_CORRUPTION, is_xml); |
243 } | 251 } |
244 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); | 252 return MakeRecallStatusHistogram(RECALL_SUCCESS, is_xml); |
245 } | 253 } |
OLD | NEW |