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

Side by Side Diff: chrome/common/metrics_helpers.cc

Issue 2744003: Preparation CL for adding crash metrics UMA counters to ChromeFrame. Basicall... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 10 years, 6 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
Property Changes:
Added: svn:eol-style
+ LF
OLDNEW
(Empty)
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/common/metrics_helpers.h"
6
7 #if defined(USE_SYSTEM_LIBBZ2)
8 #include <bzlib.h>
9 #else
10 #include "third_party/bzip2/bzlib.h"
11 #endif
12
13 #include "base/base64.h"
14 #include "base/time.h"
15 #include "base/basictypes.h"
16 #include "base/file_util.h"
17 #include "base/md5.h"
18 #include "base/scoped_ptr.h"
19 #include "base/string_util.h"
20 #include "base/sys_info.h"
21 #include "base/utf_string_conversions.h"
22 #include "base/third_party/nspr/prtime.h"
23 #include "chrome/common/logging_chrome.h"
24 #include "googleurl/src/gurl.h"
25
26 #define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
27
28 using base::Time;
29 using base::TimeDelta;
30
31 // http://blogs.msdn.com/oldnewthing/archive/2004/10/25/247180.aspx
32 #if defined(OS_WIN)
33 extern "C" IMAGE_DOS_HEADER __ImageBase;
34 #endif
35
36 // static
37 std::string MetricsLogBase::version_extension_;
38
39 // libxml take xmlChar*, which is unsigned char*
40 inline const unsigned char* UnsignedChar(const char* input) {
41 return reinterpret_cast<const unsigned char*>(input);
42 }
43
44 MetricsLogBase::MetricsLogBase(const std::string& client_id, int session_id,
45 const std::string& version_string)
46 : start_time_(Time::Now()),
47 client_id_(client_id),
48 session_id_(IntToString(session_id)),
49 locked_(false),
50 buffer_(NULL),
51 writer_(NULL),
52 num_events_(0) {
53
54 buffer_ = xmlBufferCreate();
55 DCHECK(buffer_);
56
57 writer_ = xmlNewTextWriterMemory(buffer_, 0);
58 DCHECK(writer_);
59
60 int result = xmlTextWriterSetIndent(writer_, 2);
61 DCHECK_EQ(0, result);
62
63 StartElement("log");
64 WriteAttribute("clientid", client_id_);
65 WriteInt64Attribute("buildtime", GetBuildTime());
66 WriteAttribute("appversion", version_string);
67
68 DCHECK_GE(result, 0);
69 }
70
71 MetricsLogBase::~MetricsLogBase() {
72 if (writer_)
73 xmlFreeTextWriter(writer_);
74
75 if (buffer_)
76 xmlBufferFree(buffer_);
77 }
78
79 void MetricsLogBase::CloseLog() {
80 DCHECK(!locked_);
81 locked_ = true;
82
83 int result = xmlTextWriterEndDocument(writer_);
84 DCHECK_GE(result, 0);
85
86 result = xmlTextWriterFlush(writer_);
87 DCHECK_GE(result, 0);
88 }
89
90 int MetricsLogBase::GetEncodedLogSize() {
91 DCHECK(locked_);
92 return buffer_->use;
93 }
94
95 bool MetricsLogBase::GetEncodedLog(char* buffer, int buffer_size) {
96 DCHECK(locked_);
97 if (buffer_size < GetEncodedLogSize())
98 return false;
99
100 memcpy(buffer, buffer_->content, GetEncodedLogSize());
101 return true;
102 }
103
104 std::string MetricsLogBase::GetEncodedLogString() {
105 DCHECK(locked_);
106 return std::string(reinterpret_cast<char*>(buffer_->content));
107 }
108
109 int MetricsLogBase::GetElapsedSeconds() {
110 return static_cast<int>((Time::Now() - start_time_).InSeconds());
111 }
112
113 std::string MetricsLogBase::CreateHash(const std::string& value) {
114 MD5Context ctx;
115 MD5Init(&ctx);
116 MD5Update(&ctx, value.data(), value.length());
117
118 MD5Digest digest;
119 MD5Final(&digest, &ctx);
120
121 uint64 reverse_uint64;
122 // UMA only uses first 8 chars of hash. We use the above uint64 instead
123 // of a unsigned char[8] so that we don't run into strict aliasing issues
124 // in the LOG statement below when trying to interpret reverse as a uint64.
125 unsigned char* reverse = reinterpret_cast<unsigned char *>(&reverse_uint64);
126 DCHECK(arraysize(digest.a) >= sizeof(reverse_uint64));
127 for (size_t i = 0; i < sizeof(reverse_uint64); ++i)
128 reverse[i] = digest.a[sizeof(reverse_uint64) - i - 1];
129 // The following log is VERY helpful when folks add some named histogram into
130 // the code, but forgot to update the descriptive list of histograms. When
131 // that happens, all we get to see (server side) is a hash of the histogram
132 // name. We can then use this logging to find out what histogram name was
133 // being hashed to a given MD5 value by just running the version of Chromium
134 // in question with --enable-logging.
135 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
136 << reverse_uint64 << "]";
137 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
138 }
139
140 std::string MetricsLogBase::CreateBase64Hash(const std::string& string) {
141 std::string encoded_digest;
142 if (base::Base64Encode(CreateHash(string), &encoded_digest)) {
143 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
144 return encoded_digest;
145 }
146 return std::string();
147 }
148
149 void MetricsLogBase::RecordUserAction(const char* key) {
150 DCHECK(!locked_);
151
152 std::string command_hash = CreateBase64Hash(key);
153 if (command_hash.empty()) {
154 NOTREACHED() << "Unable generate encoded hash of command: " << key;
155 return;
156 }
157
158 OPEN_ELEMENT_FOR_SCOPE("uielement");
159 WriteAttribute("action", "command");
160 WriteAttribute("targetidhash", command_hash);
161
162 // TODO(jhughes): Properly track windows.
163 WriteIntAttribute("window", 0);
164 WriteCommonEventAttributes();
165
166 ++num_events_;
167 }
168
169 void MetricsLogBase::RecordLoadEvent(int window_id,
170 const GURL& url,
171 PageTransition::Type origin,
172 int session_index,
173 TimeDelta load_time) {
174 DCHECK(!locked_);
175
176 OPEN_ELEMENT_FOR_SCOPE("document");
177 WriteAttribute("action", "load");
178 WriteIntAttribute("docid", session_index);
179 WriteIntAttribute("window", window_id);
180 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
181
182 std::string origin_string;
183
184 switch (PageTransition::StripQualifier(origin)) {
185 // TODO(jhughes): Some of these mappings aren't right... we need to add
186 // some values to the server's enum.
187 case PageTransition::LINK:
188 case PageTransition::MANUAL_SUBFRAME:
189 origin_string = "link";
190 break;
191
192 case PageTransition::TYPED:
193 origin_string = "typed";
194 break;
195
196 case PageTransition::AUTO_BOOKMARK:
197 origin_string = "bookmark";
198 break;
199
200 case PageTransition::AUTO_SUBFRAME:
201 case PageTransition::RELOAD:
202 origin_string = "refresh";
203 break;
204
205 case PageTransition::GENERATED:
206 case PageTransition::KEYWORD:
207 origin_string = "global-history";
208 break;
209
210 case PageTransition::START_PAGE:
211 origin_string = "start-page";
212 break;
213
214 case PageTransition::FORM_SUBMIT:
215 origin_string = "form-submit";
216 break;
217
218 default:
219 NOTREACHED() << "Received an unknown page transition type: " <<
220 PageTransition::StripQualifier(origin);
221 }
222 if (!origin_string.empty())
223 WriteAttribute("origin", origin_string);
224
225 WriteCommonEventAttributes();
226
227 ++num_events_;
228 }
229
230 void MetricsLogBase::RecordWindowEvent(WindowEventType type,
231 int window_id,
232 int parent_id) {
233 DCHECK(!locked_);
234
235 OPEN_ELEMENT_FOR_SCOPE("window");
236 WriteAttribute("action", WindowEventTypeToString(type));
237 WriteAttribute("windowid", IntToString(window_id));
238 if (parent_id >= 0)
239 WriteAttribute("parent", IntToString(parent_id));
240 WriteCommonEventAttributes();
241
242 ++num_events_;
243 }
244
245 std::string MetricsLogBase::GetCurrentTimeString() {
246 return Uint64ToString(Time::Now().ToTimeT());
247 }
248
249 // These are the attributes that are common to every event.
250 void MetricsLogBase::WriteCommonEventAttributes() {
251 WriteAttribute("session", session_id_);
252 WriteAttribute("time", GetCurrentTimeString());
253 }
254
255 void MetricsLogBase::WriteAttribute(const std::string& name,
256 const std::string& value) {
257 DCHECK(!locked_);
258 DCHECK(!name.empty());
259
260 int result = xmlTextWriterWriteAttribute(writer_,
261 UnsignedChar(name.c_str()),
262 UnsignedChar(value.c_str()));
263 DCHECK_GE(result, 0);
264 }
265
266 void MetricsLogBase::WriteIntAttribute(const std::string& name, int value) {
267 WriteAttribute(name, IntToString(value));
268 }
269
270 void MetricsLogBase::WriteInt64Attribute(const std::string& name, int64 value) {
271 WriteAttribute(name, Int64ToString(value));
272 }
273
274 // static
275 const char* MetricsLogBase::WindowEventTypeToString(WindowEventType type) {
276 switch (type) {
277 case WINDOW_CREATE: return "create";
278 case WINDOW_OPEN: return "open";
279 case WINDOW_CLOSE: return "close";
280 case WINDOW_DESTROY: return "destroy";
281
282 default:
283 NOTREACHED();
284 return "unknown"; // Can't return NULL as this is used in a required
285 // attribute.
286 }
287 }
288
289 void MetricsLogBase::StartElement(const char* name) {
290 DCHECK(!locked_);
291 DCHECK(name);
292
293 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
294 DCHECK_GE(result, 0);
295 }
296
297 void MetricsLogBase::EndElement() {
298 DCHECK(!locked_);
299
300 int result = xmlTextWriterEndElement(writer_);
301 DCHECK_GE(result, 0);
302 }
303
304 // static
305 int64 MetricsLogBase::GetBuildTime() {
306 static int64 integral_build_time = 0;
307 if (!integral_build_time) {
308 Time time;
309 const char* kDateTime = __DATE__ " " __TIME__ " GMT";
310 bool result = Time::FromString(ASCIIToWide(kDateTime).c_str(), &time);
311 DCHECK(result);
312 integral_build_time = static_cast<int64>(time.ToTimeT());
313 }
314 return integral_build_time;
315 }
316
317 // TODO(JAR): A The following should really be part of the histogram class.
318 // Internal state is being needlessly exposed, and it would be hard to reuse
319 // this code. If we moved this into the Histogram class, then we could use
320 // the same infrastructure for logging StatsCounters, RatesCounters, etc.
321 void MetricsLogBase::RecordHistogramDelta(const Histogram& histogram,
322 const Histogram::SampleSet& snapshot) {
323 DCHECK(!locked_);
324 DCHECK_NE(0, snapshot.TotalCount());
325 snapshot.CheckSize(histogram);
326
327 // We will ignore the MAX_INT/infinite value in the last element of range[].
328
329 OPEN_ELEMENT_FOR_SCOPE("histogram");
330
331 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
332
333 WriteInt64Attribute("sum", snapshot.sum());
334 WriteInt64Attribute("sumsquares", snapshot.square_sum());
335
336 for (size_t i = 0; i < histogram.bucket_count(); i++) {
337 if (snapshot.counts(i)) {
338 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
339 WriteIntAttribute("min", histogram.ranges(i));
340 WriteIntAttribute("max", histogram.ranges(i + 1));
341 WriteIntAttribute("count", snapshot.counts(i));
342 }
343 }
344 }
345
346
347 // MetricsServiceBase
348 MetricsServiceBase::MetricsServiceBase()
349 : pending_log_(NULL),
350 pending_log_text_(),
351 current_log_(NULL),
352 logged_samples_() {
353 }
354
355 MetricsServiceBase::~MetricsServiceBase() {
356 if (pending_log_) {
357 delete pending_log_;
358 pending_log_ = NULL;
359 }
360 if (current_log_) {
361 delete current_log_;
362 current_log_ = NULL;
363 }
364 }
365
366 // This implementation is based on the Firefox MetricsService implementation.
367 bool MetricsServiceBase::Bzip2Compress(const std::string& input,
368 std::string* output) {
369 bz_stream stream = {0};
370 // As long as our input is smaller than the bzip2 block size, we should get
371 // the best compression. For example, if your input was 250k, using a block
372 // size of 300k or 500k should result in the same compression ratio. Since
373 // our data should be under 100k, using the minimum block size of 100k should
374 // allocate less temporary memory, but result in the same compression ratio.
375 int result = BZ2_bzCompressInit(&stream,
376 1, // 100k (min) block size
377 0, // quiet
378 0); // default "work factor"
379 if (result != BZ_OK) { // out of memory?
380 return false;
381 }
382
383 output->clear();
384
385 stream.next_in = const_cast<char*>(input.data());
386 stream.avail_in = static_cast<int>(input.size());
387 // NOTE: we don't need a BZ_RUN phase since our input buffer contains
388 // the entire input
389 do {
390 output->resize(output->size() + 1024);
391 stream.next_out = &((*output)[stream.total_out_lo32]);
392 stream.avail_out = static_cast<int>(output->size()) - stream.total_out_lo32;
393 result = BZ2_bzCompress(&stream, BZ_FINISH);
394 } while (result == BZ_FINISH_OK);
395 if (result != BZ_STREAM_END) // unknown failure?
396 return false;
397 result = BZ2_bzCompressEnd(&stream);
398 DCHECK(result == BZ_OK);
399
400 output->resize(stream.total_out_lo32);
401
402 return true;
403 }
404
405 void MetricsServiceBase::RecordCurrentHistograms() {
406 DCHECK(current_log_);
407
408 StatisticsRecorder::Histograms histograms;
409 StatisticsRecorder::GetHistograms(&histograms);
410 for (StatisticsRecorder::Histograms::iterator it = histograms.begin();
411 histograms.end() != it;
412 ++it) {
413 if ((*it)->flags() & Histogram::kUmaTargetedHistogramFlag)
414 // TODO(petersont): Only record historgrams if they are not precluded by
415 // the UMA response data.
416 // Bug http://code.google.com/p/chromium/issues/detail?id=2739.
417 RecordHistogram(**it);
418 }
419 }
420
421 void MetricsServiceBase::RecordHistogram(const Histogram& histogram) {
422 // Get up-to-date snapshot of sample stats.
423 Histogram::SampleSet snapshot;
424 histogram.SnapshotSample(&snapshot);
425
426 const std::string& histogram_name = histogram.histogram_name();
427
428 // Find the already sent stats, or create an empty set.
429 LoggedSampleMap::iterator it = logged_samples_.find(histogram_name);
430 Histogram::SampleSet* already_logged;
431 if (logged_samples_.end() == it) {
432 // Add new entry
433 already_logged = &logged_samples_[histogram.histogram_name()];
434 already_logged->Resize(histogram); // Complete initialization.
435 } else {
436 already_logged = &(it->second);
437 // Deduct any stats we've already logged from our snapshot.
438 snapshot.Subtract(*already_logged);
439 }
440
441 // snapshot now contains only a delta to what we've already_logged.
442
443 if (snapshot.TotalCount() > 0) {
444 current_log_->RecordHistogramDelta(histogram, snapshot);
445 // Add new data into our running total.
446 already_logged->Add(snapshot);
447 }
448 }
449
450 void MetricsServiceBase::DiscardPendingLog() {
451 if (pending_log_) { // Shutdown might have deleted it!
452 delete pending_log_;
453 pending_log_ = NULL;
454 }
455 pending_log_text_.clear();
456 }
457
OLDNEW
« chrome/browser/metrics/metrics_log.cc ('K') | « chrome/common/metrics_helpers.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698