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 #if defined(USE_SYSTEM_LIBBZ2) | 7 #if defined(USE_SYSTEM_LIBBZ2) |
8 #include <bzlib.h> | 8 #include <bzlib.h> |
9 #else | 9 #else |
10 #include "third_party/bzip2/bzlib.h" | 10 #include "third_party/bzip2/bzlib.h" |
11 #endif | 11 #endif |
12 | 12 |
13 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
14 #include "base/string_util.h" | 14 #include "base/string_util.h" |
15 #include "chrome/common/metrics/metrics_log_base.h" | 15 #include "chrome/common/metrics/metrics_log_base.h" |
16 | 16 |
| 17 namespace { |
| 18 |
| 19 // Used to keep track of discarded protobuf logs without having to track xml and |
| 20 // protobuf logs in separate lists. |
| 21 const char kDiscardedLog[] = "Log discarded"; |
| 22 |
| 23 } // anonymous namespace |
| 24 |
17 MetricsLogManager::MetricsLogManager() : max_ongoing_log_store_size_(0) {} | 25 MetricsLogManager::MetricsLogManager() : max_ongoing_log_store_size_(0) {} |
18 | 26 |
19 MetricsLogManager::~MetricsLogManager() {} | 27 MetricsLogManager::~MetricsLogManager() {} |
20 | 28 |
| 29 bool MetricsLogManager::SerializedLog::empty() const { |
| 30 DCHECK_EQ(xml.empty(), proto.empty()); |
| 31 return xml.empty(); |
| 32 } |
| 33 |
21 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log) { | 34 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log) { |
22 DCHECK(!current_log_.get()); | 35 DCHECK(!current_log_.get()); |
23 current_log_.reset(log); | 36 current_log_.reset(log); |
24 } | 37 } |
25 | 38 |
26 void MetricsLogManager::StageCurrentLogForUpload() { | 39 void MetricsLogManager::StageCurrentLogForUpload() { |
27 DCHECK(current_log_.get()); | 40 DCHECK(current_log_.get()); |
28 current_log_->CloseLog(); | 41 current_log_->CloseLog(); |
29 staged_log_.reset(current_log_.release()); | 42 staged_log_.reset(current_log_.release()); |
30 CompressStagedLog(); | 43 CompressStagedLog(); |
31 } | 44 } |
32 | 45 |
33 bool MetricsLogManager::has_staged_log() const { | 46 bool MetricsLogManager::has_staged_log() const { |
34 return staged_log_.get() || !compressed_staged_log_text_.empty(); | 47 return staged_log_.get() || !staged_log_text().empty(); |
| 48 } |
| 49 |
| 50 bool MetricsLogManager::has_staged_log_proto() const { |
| 51 return has_staged_log() && staged_log_text().proto != kDiscardedLog; |
35 } | 52 } |
36 | 53 |
37 void MetricsLogManager::DiscardStagedLog() { | 54 void MetricsLogManager::DiscardStagedLog() { |
38 staged_log_.reset(); | 55 staged_log_.reset(); |
39 compressed_staged_log_text_.clear(); | 56 staged_log_text_.xml.clear(); |
| 57 staged_log_text_.proto.clear(); |
| 58 } |
| 59 |
| 60 void MetricsLogManager::DiscardStagedLogProto() { |
| 61 staged_log_text_.proto = kDiscardedLog; |
40 } | 62 } |
41 | 63 |
42 void MetricsLogManager::DiscardCurrentLog() { | 64 void MetricsLogManager::DiscardCurrentLog() { |
43 current_log_->CloseLog(); | 65 current_log_->CloseLog(); |
44 current_log_.reset(); | 66 current_log_.reset(); |
45 } | 67 } |
46 | 68 |
47 void MetricsLogManager::PauseCurrentLog() { | 69 void MetricsLogManager::PauseCurrentLog() { |
48 DCHECK(!paused_log_.get()); | 70 DCHECK(!paused_log_.get()); |
49 paused_log_.reset(current_log_.release()); | 71 paused_log_.reset(current_log_.release()); |
50 } | 72 } |
51 | 73 |
52 void MetricsLogManager::ResumePausedLog() { | 74 void MetricsLogManager::ResumePausedLog() { |
53 DCHECK(!current_log_.get()); | 75 DCHECK(!current_log_.get()); |
54 current_log_.reset(paused_log_.release()); | 76 current_log_.reset(paused_log_.release()); |
55 } | 77 } |
56 | 78 |
57 void MetricsLogManager::StoreStagedLogAsUnsent(LogType log_type) { | 79 void MetricsLogManager::StoreStagedLogAsUnsent(LogType log_type) { |
58 DCHECK(has_staged_log()); | 80 DCHECK(has_staged_log()); |
| 81 |
59 // If compressing the log failed, there's nothing to store. | 82 // If compressing the log failed, there's nothing to store. |
60 if (compressed_staged_log_text_.empty()) | 83 if (staged_log_text().empty()) |
61 return; | 84 return; |
62 | 85 |
63 if (log_type == INITIAL_LOG) { | 86 if (log_type == INITIAL_LOG) { |
64 unsent_initial_logs_.push_back(compressed_staged_log_text_); | 87 unsent_initial_logs_.push_back(staged_log_text_); |
65 } else { | 88 } else { |
66 // If it's too large, just note that and discard it. | 89 // If it's too large, just note that and discard it. |
67 if (max_ongoing_log_store_size_ && | 90 if (max_ongoing_log_store_size_ && |
68 compressed_staged_log_text_.length() > max_ongoing_log_store_size_) { | 91 staged_log_text().xml.length() > max_ongoing_log_store_size_) { |
| 92 // TODO(isherman): We probably want a similar check for protobufs, but we |
| 93 // don't want to prevent XML upload just because the protobuf version is |
| 94 // too long. In practice, I'm pretty sure the XML version should always |
| 95 // be longer, or at least on the same order of magnitude in length. |
69 UMA_HISTOGRAM_COUNTS( | 96 UMA_HISTOGRAM_COUNTS( |
70 "UMA.Large Accumulated Log Not Persisted", | 97 "UMA.Large Accumulated Log Not Persisted", |
71 static_cast<int>(compressed_staged_log_text_.length())); | 98 static_cast<int>(staged_log_text().xml.length())); |
72 } else { | 99 } else { |
73 unsent_ongoing_logs_.push_back(compressed_staged_log_text_); | 100 unsent_ongoing_logs_.push_back(staged_log_text_); |
74 } | 101 } |
75 } | 102 } |
76 DiscardStagedLog(); | 103 DiscardStagedLog(); |
77 } | 104 } |
78 | 105 |
79 void MetricsLogManager::StageNextStoredLogForUpload() { | 106 void MetricsLogManager::StageNextStoredLogForUpload() { |
80 // Prioritize initial logs for uploading. | 107 // Prioritize initial logs for uploading. |
81 std::vector<std::string>* source_list = unsent_initial_logs_.empty() ? | 108 std::vector<SerializedLog>* source_list = |
82 &unsent_ongoing_logs_ : &unsent_initial_logs_; | 109 unsent_initial_logs_.empty() ? |
| 110 &unsent_ongoing_logs_ : |
| 111 &unsent_initial_logs_; |
83 DCHECK(!source_list->empty()); | 112 DCHECK(!source_list->empty()); |
84 DCHECK(compressed_staged_log_text_.empty()); | 113 DCHECK(staged_log_text().empty()); |
85 compressed_staged_log_text_ = source_list->back(); | 114 staged_log_text_ = source_list->back(); |
86 source_list->pop_back(); | 115 source_list->pop_back(); |
87 } | 116 } |
88 | 117 |
89 void MetricsLogManager::PersistUnsentLogs() { | 118 void MetricsLogManager::PersistUnsentLogs() { |
90 DCHECK(log_serializer_.get()); | 119 DCHECK(log_serializer_.get()); |
91 if (!log_serializer_.get()) | 120 if (!log_serializer_.get()) |
92 return; | 121 return; |
93 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); | 122 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); |
94 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); | 123 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); |
95 } | 124 } |
96 | 125 |
97 void MetricsLogManager::LoadPersistedUnsentLogs() { | 126 void MetricsLogManager::LoadPersistedUnsentLogs() { |
98 DCHECK(log_serializer_.get()); | 127 DCHECK(log_serializer_.get()); |
99 if (!log_serializer_.get()) | 128 if (!log_serializer_.get()) |
100 return; | 129 return; |
101 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); | 130 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); |
102 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); | 131 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); |
103 } | 132 } |
104 | 133 |
105 void MetricsLogManager::CompressStagedLog() { | 134 void MetricsLogManager::CompressStagedLog() { |
106 int text_size = staged_log_->GetEncodedLogSize(); | 135 int text_size = staged_log_->GetEncodedLogSizeXml(); |
107 std::string staged_log_text; | 136 std::string staged_log_text; |
108 DCHECK_GT(text_size, 0); | 137 DCHECK_GT(text_size, 0); |
109 staged_log_->GetEncodedLog(WriteInto(&staged_log_text, text_size + 1), | 138 staged_log_->GetEncodedLogXml(WriteInto(&staged_log_text, text_size + 1), |
110 text_size); | 139 text_size); |
111 | 140 |
112 bool success = Bzip2Compress(staged_log_text, &compressed_staged_log_text_); | 141 bool success = Bzip2Compress(staged_log_text, &staged_log_text_.xml); |
113 if (success) { | 142 if (success) { |
114 // Allow security-conscious users to see all metrics logs that we send. | 143 // Allow security-conscious users to see all metrics logs that we send. |
115 DVLOG(1) << "METRICS LOG: " << staged_log_text; | 144 DVLOG(1) << "METRICS LOG: " << staged_log_text; |
| 145 |
| 146 // Note that we only save the protobuf version if we succeeded in |
| 147 // compressing the XML, so that the two data streams are the same. |
| 148 staged_log_->GetEncodedLogProto(&staged_log_text_.proto); |
116 } else { | 149 } else { |
117 NOTREACHED() << "Failed to compress log for transmission."; | 150 NOTREACHED() << "Failed to compress log for transmission."; |
118 } | 151 } |
119 } | 152 } |
120 | 153 |
121 // static | 154 // static |
122 // This implementation is based on the Firefox MetricsService implementation. | 155 // This implementation is based on the Firefox MetricsService implementation. |
123 bool MetricsLogManager::Bzip2Compress(const std::string& input, | 156 bool MetricsLogManager::Bzip2Compress(const std::string& input, |
124 std::string* output) { | 157 std::string* output) { |
125 bz_stream stream = {0}; | 158 bz_stream stream = {0}; |
(...skipping 27 matching lines...) Expand all Loading... |
153 // TODO(jar): See if it would be better to do a CHECK() here. | 186 // TODO(jar): See if it would be better to do a CHECK() here. |
154 return false; | 187 return false; |
155 } | 188 } |
156 result = BZ2_bzCompressEnd(&stream); | 189 result = BZ2_bzCompressEnd(&stream); |
157 DCHECK(result == BZ_OK); | 190 DCHECK(result == BZ_OK); |
158 | 191 |
159 output->resize(stream.total_out_lo32); | 192 output->resize(stream.total_out_lo32); |
160 | 193 |
161 return true; | 194 return true; |
162 } | 195 } |
OLD | NEW |