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 MetricsLogManager::MetricsLogManager() : max_ongoing_log_store_size_(0) {} | 17 MetricsLogManager::MetricsLogManager() : current_log_type_(INITIAL_LOG), |
18 staged_log_type_(INITIAL_LOG), | |
19 max_ongoing_log_store_size_(0) {} | |
18 | 20 |
19 MetricsLogManager::~MetricsLogManager() {} | 21 MetricsLogManager::~MetricsLogManager() {} |
20 | 22 |
21 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log) { | 23 void MetricsLogManager::BeginLoggingWithLog(MetricsLogBase* log, |
24 LogType log_type) { | |
22 DCHECK(!current_log_.get()); | 25 DCHECK(!current_log_.get()); |
23 current_log_.reset(log); | 26 current_log_.reset(log); |
27 current_log_type_ = log_type; | |
24 } | 28 } |
25 | 29 |
26 void MetricsLogManager::StageCurrentLogForUpload() { | 30 void MetricsLogManager::FinishCurrentLog() { |
27 DCHECK(current_log_.get()); | 31 DCHECK(current_log_.get()); |
28 current_log_->CloseLog(); | 32 current_log_->CloseLog(); |
29 staged_log_.reset(current_log_.release()); | 33 std::string compressed_log; |
30 CompressStagedLog(); | 34 CompressCurrentLog(&compressed_log); |
35 if (!compressed_log.empty()) | |
36 StoreLog(compressed_log, current_log_type_); | |
37 current_log_.reset(); | |
38 } | |
39 | |
40 void MetricsLogManager::StageNextLogForUpload() { | |
41 // Prioritize initial logs for uploading. | |
42 LogType log_type = unsent_initial_logs_.empty() ? ONGOING_LOG : INITIAL_LOG; | |
Ilya Sherman
2012/02/29 01:25:41
nit: I'm not sure how much breaking out this varia
stuartmorgan
2012/02/29 13:26:15
Yep, this was an artifact of a previous iteration.
| |
43 std::vector<std::string>* source_list = (log_type == ONGOING_LOG) ? | |
44 &unsent_ongoing_logs_ : &unsent_initial_logs_; | |
45 DCHECK(!source_list->empty()); | |
46 DCHECK(compressed_staged_log_text_.empty()); | |
47 compressed_staged_log_text_ = source_list->back(); | |
Ilya Sherman
2012/02/29 01:25:41
Please use std::string::swap() here to avoid copyi
stuartmorgan
2012/02/29 13:26:15
Good tip; I wasn't familiar with swap(). (It's a b
| |
48 source_list->pop_back(); | |
31 } | 49 } |
32 | 50 |
33 bool MetricsLogManager::has_staged_log() const { | 51 bool MetricsLogManager::has_staged_log() const { |
34 return staged_log_.get() || !compressed_staged_log_text_.empty(); | 52 return !compressed_staged_log_text_.empty(); |
35 } | 53 } |
36 | 54 |
37 void MetricsLogManager::DiscardStagedLog() { | 55 void MetricsLogManager::DiscardStagedLog() { |
38 staged_log_.reset(); | |
39 compressed_staged_log_text_.clear(); | 56 compressed_staged_log_text_.clear(); |
40 } | 57 } |
41 | 58 |
42 void MetricsLogManager::DiscardCurrentLog() { | 59 void MetricsLogManager::DiscardCurrentLog() { |
43 current_log_->CloseLog(); | 60 current_log_->CloseLog(); |
44 current_log_.reset(); | 61 current_log_.reset(); |
45 } | 62 } |
46 | 63 |
47 void MetricsLogManager::PauseCurrentLog() { | 64 void MetricsLogManager::PauseCurrentLog() { |
48 DCHECK(!paused_log_.get()); | 65 DCHECK(!paused_log_.get()); |
49 paused_log_.reset(current_log_.release()); | 66 paused_log_.reset(current_log_.release()); |
50 } | 67 } |
51 | 68 |
52 void MetricsLogManager::ResumePausedLog() { | 69 void MetricsLogManager::ResumePausedLog() { |
53 DCHECK(!current_log_.get()); | 70 DCHECK(!current_log_.get()); |
54 current_log_.reset(paused_log_.release()); | 71 current_log_.reset(paused_log_.release()); |
55 } | 72 } |
56 | 73 |
57 void MetricsLogManager::StoreStagedLogAsUnsent(LogType log_type) { | 74 void MetricsLogManager::StoreStagedLogAsUnsent() { |
58 DCHECK(has_staged_log()); | 75 DCHECK(has_staged_log()); |
59 // If compressing the log failed, there's nothing to store. | 76 // If compressing the log failed, there's nothing to store. |
60 if (compressed_staged_log_text_.empty()) | 77 if (compressed_staged_log_text_.empty()) |
61 return; | 78 return; |
62 | 79 |
63 if (log_type == INITIAL_LOG) { | 80 StoreLog(compressed_staged_log_text_, staged_log_type_); |
64 unsent_initial_logs_.push_back(compressed_staged_log_text_); | |
65 } else { | |
66 // If it's too large, just note that and discard it. | |
67 if (max_ongoing_log_store_size_ && | |
68 compressed_staged_log_text_.length() > max_ongoing_log_store_size_) { | |
69 UMA_HISTOGRAM_COUNTS( | |
70 "UMA.Large Accumulated Log Not Persisted", | |
71 static_cast<int>(compressed_staged_log_text_.length())); | |
72 } else { | |
73 unsent_ongoing_logs_.push_back(compressed_staged_log_text_); | |
74 } | |
75 } | |
76 DiscardStagedLog(); | 81 DiscardStagedLog(); |
77 } | 82 } |
78 | 83 |
79 void MetricsLogManager::StageNextStoredLogForUpload() { | 84 void MetricsLogManager::StoreLog(const std::string& log_text, |
80 // Prioritize initial logs for uploading. | 85 LogType log_type) { |
81 std::vector<std::string>* source_list = unsent_initial_logs_.empty() ? | 86 if (log_type == INITIAL_LOG) { |
82 &unsent_ongoing_logs_ : &unsent_initial_logs_; | 87 unsent_initial_logs_.push_back(log_text); |
83 DCHECK(!source_list->empty()); | 88 } else { |
84 DCHECK(compressed_staged_log_text_.empty()); | 89 unsent_ongoing_logs_.push_back(log_text); |
85 compressed_staged_log_text_ = source_list->back(); | 90 } |
Ilya Sherman
2012/02/29 01:25:41
If we can avoid copying the log text by using std:
stuartmorgan
2012/02/29 13:26:15
The semantics of the StoreLog method become unfort
| |
86 source_list->pop_back(); | |
87 } | 91 } |
88 | 92 |
89 void MetricsLogManager::PersistUnsentLogs() { | 93 void MetricsLogManager::PersistUnsentLogs() { |
90 DCHECK(log_serializer_.get()); | 94 DCHECK(log_serializer_.get()); |
91 if (!log_serializer_.get()) | 95 if (!log_serializer_.get()) |
92 return; | 96 return; |
97 // Remove any ongoing logs that are over the serialization size limit. | |
98 if (max_ongoing_log_store_size_) { | |
99 for (std::vector<std::string>::iterator it = unsent_ongoing_logs_.begin(); | |
100 it != unsent_ongoing_logs_.end();) { | |
101 size_t log_size = it->length(); | |
102 if (log_size > max_ongoing_log_store_size_) { | |
103 UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", | |
104 static_cast<int>(log_size)); | |
105 it = unsent_ongoing_logs_.erase(it); | |
106 } else { | |
107 ++it; | |
108 } | |
109 } | |
110 } | |
93 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); | 111 log_serializer_->SerializeLogs(unsent_initial_logs_, INITIAL_LOG); |
94 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); | 112 log_serializer_->SerializeLogs(unsent_ongoing_logs_, ONGOING_LOG); |
95 } | 113 } |
96 | 114 |
97 void MetricsLogManager::LoadPersistedUnsentLogs() { | 115 void MetricsLogManager::LoadPersistedUnsentLogs() { |
98 DCHECK(log_serializer_.get()); | 116 DCHECK(log_serializer_.get()); |
99 if (!log_serializer_.get()) | 117 if (!log_serializer_.get()) |
100 return; | 118 return; |
101 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); | 119 log_serializer_->DeserializeLogs(INITIAL_LOG, &unsent_initial_logs_); |
102 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); | 120 log_serializer_->DeserializeLogs(ONGOING_LOG, &unsent_ongoing_logs_); |
103 } | 121 } |
104 | 122 |
105 void MetricsLogManager::CompressStagedLog() { | 123 void MetricsLogManager::CompressCurrentLog(std::string* compressed_log) { |
106 int text_size = staged_log_->GetEncodedLogSize(); | 124 int text_size = current_log_->GetEncodedLogSize(); |
107 std::string staged_log_text; | |
108 DCHECK_GT(text_size, 0); | 125 DCHECK_GT(text_size, 0); |
109 staged_log_->GetEncodedLog(WriteInto(&staged_log_text, text_size + 1), | 126 std::string log_text; |
110 text_size); | 127 current_log_->GetEncodedLog(WriteInto(&log_text, text_size + 1), text_size); |
111 | 128 |
112 bool success = Bzip2Compress(staged_log_text, &compressed_staged_log_text_); | 129 bool success = Bzip2Compress(log_text, compressed_log); |
113 if (success) { | 130 if (success) { |
114 // Allow security-conscious users to see all metrics logs that we send. | 131 // Allow security-conscious users to see all metrics logs that we send. |
115 DVLOG(1) << "METRICS LOG: " << staged_log_text; | 132 DVLOG(1) << "METRICS LOG: " << log_text; |
116 } else { | 133 } else { |
117 NOTREACHED() << "Failed to compress log for transmission."; | 134 NOTREACHED() << "Failed to compress log for transmission."; |
118 } | 135 } |
119 } | 136 } |
120 | 137 |
121 // static | 138 // static |
122 // This implementation is based on the Firefox MetricsService implementation. | 139 // This implementation is based on the Firefox MetricsService implementation. |
123 bool MetricsLogManager::Bzip2Compress(const std::string& input, | 140 bool MetricsLogManager::Bzip2Compress(const std::string& input, |
124 std::string* output) { | 141 std::string* output) { |
125 bz_stream stream = {0}; | 142 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. | 170 // TODO(jar): See if it would be better to do a CHECK() here. |
154 return false; | 171 return false; |
155 } | 172 } |
156 result = BZ2_bzCompressEnd(&stream); | 173 result = BZ2_bzCompressEnd(&stream); |
157 DCHECK(result == BZ_OK); | 174 DCHECK(result == BZ_OK); |
158 | 175 |
159 output->resize(stream.total_out_lo32); | 176 output->resize(stream.total_out_lo32); |
160 | 177 |
161 return true; | 178 return true; |
162 } | 179 } |
OLD | NEW |