blob: eb0b5be0982a199f48970465130d40f35b668609 [file] [log] [blame]
initial.commit09911bf2008-07-26 23:55:291// Copyright 2008, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8// * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14// * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#include "chrome/browser/metrics_log.h"
31
32#include "base/file_util.h"
33#include "base/file_version_info.h"
34#include "base/md5.h"
35#include "base/scoped_ptr.h"
36#include "base/string_util.h"
37#include "chrome/browser/autocomplete/autocomplete.h"
38#include "chrome/browser/browser_process.h"
39#include "chrome/common/env_util.h"
40#include "chrome/common/logging_chrome.h"
41#include "chrome/common/pref_names.h"
42#include "chrome/common/pref_service.h"
[email protected]46072d42008-07-28 14:49:3543#include "googleurl/src/gurl.h"
initial.commit09911bf2008-07-26 23:55:2944#include "net/base/base64.h"
45
46#define OPEN_ELEMENT_FOR_SCOPE(name) ScopedElement scoped_element(this, name)
47
48// libxml take xmlChar*, which is unsigned char*
49inline const unsigned char* UnsignedChar(const char* input) {
50 return reinterpret_cast<const unsigned char*>(input);
51}
52
53// static
54void MetricsLog::RegisterPrefs(PrefService* local_state) {
55 local_state->RegisterListPref(prefs::kStabilityPluginStats);
56 local_state->RegisterBooleanPref(prefs::kMetricsIsRecording, true);
57}
58
59MetricsLog::MetricsLog(const std::string& client_id, int session_id)
60 : start_time_(Time::Now()),
61 num_events_(0),
62 locked_(false),
63 buffer_(NULL),
64 writer_(NULL),
65 client_id_(client_id),
66 session_id_(IntToString(session_id)) {
67
68 buffer_ = xmlBufferCreate();
69 DCHECK(buffer_);
70
71 writer_ = xmlNewTextWriterMemory(buffer_, 0);
72 DCHECK(writer_);
73
74 int result = xmlTextWriterSetIndent(writer_, 2);
75 DCHECK_EQ(0, result);
76
77 StartElement("log");
78 WriteAttribute("clientid", client_id_);
79
80 DCHECK_GE(result, 0);
81}
82
83MetricsLog::~MetricsLog() {
84 if (writer_)
85 xmlFreeTextWriter(writer_);
86
87 if (buffer_)
88 xmlBufferFree(buffer_);
89}
90
91void MetricsLog::CloseLog() {
92 DCHECK(!locked_);
93 locked_ = true;
94
95 int result = xmlTextWriterEndDocument(writer_);
96 DCHECK(result >= 0);
97
98 result = xmlTextWriterFlush(writer_);
99 DCHECK(result >= 0);
100}
101
102int MetricsLog::GetEncodedLogSize() {
103 DCHECK(locked_);
104 return buffer_->use;
105}
106
107bool MetricsLog::GetEncodedLog(char* buffer, int buffer_size) {
108 DCHECK(locked_);
109 if (buffer_size < GetEncodedLogSize())
110 return false;
111
112 memcpy(buffer, buffer_->content, GetEncodedLogSize());
113 return true;
114}
115
116int MetricsLog::GetElapsedSeconds() {
117 return static_cast<int>((Time::Now() - start_time_).InSeconds());
118}
119
120std::string MetricsLog::CreateHash(const std::string& value) {
121 MD5Context ctx;
122 MD5Init(&ctx);
123 MD5Update(&ctx, value.data(), value.length());
124
125 MD5Digest digest;
126 MD5Final(&digest, &ctx);
127
128 unsigned char reverse[8]; // UMA only uses first 8 chars of hash.
129 DCHECK(arraysize(digest.a) >= arraysize(reverse));
130 for (int i = 0; i < arraysize(reverse); ++i)
131 reverse[i] = digest.a[arraysize(reverse) - i - 1];
132 LOG(INFO) << "Metrics: Hash numeric [" << value << "]=["
133 << *reinterpret_cast<const uint64*>(&reverse[0]) << "]";
134 return std::string(reinterpret_cast<char*>(digest.a), arraysize(digest.a));
135}
136
137std::string MetricsLog::CreateBase64Hash(const std::string& string) {
138 std::string encoded_digest;
[email protected]a9bb6f692008-07-30 16:40:10139 if (net::Base64Encode(CreateHash(string), &encoded_digest)) {
initial.commit09911bf2008-07-26 23:55:29140 DLOG(INFO) << "Metrics: Hash [" << encoded_digest << "]=[" << string << "]";
141 return encoded_digest;
initial.commit09911bf2008-07-26 23:55:29142 }
[email protected]a9bb6f692008-07-30 16:40:10143 return std::string();
initial.commit09911bf2008-07-26 23:55:29144}
145
146void MetricsLog::RecordUserAction(const wchar_t* key) {
147 DCHECK(!locked_);
148
149 std::string command_hash = CreateBase64Hash(WideToUTF8(key));
150 if (command_hash.empty()) {
151 NOTREACHED() << "Unable generate encoded hash of command: " << key;
152 return;
153 }
154
155 StartElement("uielement");
156 WriteAttribute("action", "command");
157 WriteAttribute("targetidhash", command_hash);
158
159 // TODO(jhughes): Properly track windows.
160 WriteIntAttribute("window", 0);
161 WriteCommonEventAttributes();
162 EndElement();
163
164 ++num_events_;
165}
166
167void MetricsLog::RecordLoadEvent(int window_id,
168 const GURL& url,
169 PageTransition::Type origin,
170 int session_index,
171 TimeDelta load_time) {
172 DCHECK(!locked_);
173
174 StartElement("document");
175 WriteAttribute("action", "load");
176 WriteIntAttribute("docid", session_index);
177 WriteIntAttribute("window", window_id);
178 WriteAttribute("loadtime", Int64ToString(load_time.InMilliseconds()));
179
180 std::string origin_string;
181
182 switch (PageTransition::StripQualifier(origin)) {
183 // TODO(jhughes): Some of these mappings aren't right... we need to add
184 // some values to the server's enum.
185 case PageTransition::LINK:
186 case PageTransition::MANUAL_SUBFRAME:
187 origin_string = "link";
188 break;
189
190 case PageTransition::TYPED:
191 origin_string = "typed";
192 break;
193
194 case PageTransition::AUTO_BOOKMARK:
195 origin_string = "bookmark";
196 break;
197
198 case PageTransition::AUTO_SUBFRAME:
199 case PageTransition::RELOAD:
200 origin_string = "refresh";
201 break;
202
203 case PageTransition::GENERATED:
204 origin_string = "global-history";
205 break;
206
207 case PageTransition::START_PAGE:
208 origin_string = "start-page";
209 break;
210
211 case PageTransition::FORM_SUBMIT:
212 origin_string = "form-submit";
213 break;
214
215 default:
216 NOTREACHED() << "Received an unknown page transition type: " <<
217 PageTransition::StripQualifier(origin);
218 }
219 if (!origin_string.empty())
220 WriteAttribute("origin", origin_string);
221
222 WriteCommonEventAttributes();
223 EndElement();
224
225 ++num_events_;
226}
227
228// static
229const char* MetricsLog::WindowEventTypeToString(WindowEventType type) {
230 switch (type) {
231 case WINDOW_CREATE:
232 return "create";
233
234 case WINDOW_OPEN:
235 return "open";
236
237 case WINDOW_CLOSE:
238 return "close";
239
240 case WINDOW_DESTROY:
241 return "destroy";
242
243 default:
244 NOTREACHED();
245 return "unknown";
246 }
247}
248
249void MetricsLog::RecordWindowEvent(WindowEventType type,
250 int window_id,
251 int parent_id) {
252 DCHECK(!locked_);
253
254 StartElement("window");
255 WriteAttribute("action", WindowEventTypeToString(type));
256 WriteAttribute("windowid", IntToString(window_id));
257 if (parent_id >= 0)
258 WriteAttribute("parent", IntToString(parent_id));
259 WriteCommonEventAttributes();
260 EndElement();
261
262 ++num_events_;
263}
264
265std::string MetricsLog::GetCurrentTimeString() {
266 return Uint64ToString(Time::Now().ToTimeT());
267}
268
269// These are the attributes that are common to every event.
270void MetricsLog::WriteCommonEventAttributes() {
271 WriteAttribute("session", session_id_);
272 WriteAttribute("time", GetCurrentTimeString());
273}
274
275void MetricsLog::WriteAttribute(const std::string& name,
276 const std::string& value) {
277 DCHECK(!locked_);
278 DCHECK(!name.empty());
279
280 int result = xmlTextWriterWriteAttribute(writer_,
281 UnsignedChar(name.c_str()),
282 UnsignedChar(value.c_str()));
283 DCHECK_GE(result, 0);
284}
285
286void MetricsLog::WriteIntAttribute(const std::string& name, int value) {
287 WriteAttribute(name, IntToString(value));
288}
289
290void MetricsLog::WriteInt64Attribute(const std::string& name, int64 value) {
291 WriteAttribute(name, Int64ToString(value));
292}
293
294void MetricsLog::StartElement(const char* name) {
295 DCHECK(!locked_);
296 DCHECK(name);
297
298 int result = xmlTextWriterStartElement(writer_, UnsignedChar(name));
299 DCHECK_GE(result, 0);
300}
301
302void MetricsLog::EndElement() {
303 DCHECK(!locked_);
304
305 int result = xmlTextWriterEndElement(writer_);
306 DCHECK_GE(result, 0);
307}
308
309std::string MetricsLog::GetVersionString() const {
310 scoped_ptr<FileVersionInfo> version_info(
311 FileVersionInfo::CreateFileVersionInfoForCurrentModule());
312 if (version_info.get()) {
313 std::string version = WideToUTF8(version_info->product_version());
314 if (!version_info->is_official_build())
315 version.append("-devel");
316 return version;
317 } else {
318 NOTREACHED() << "Unable to retrieve version string.";
319 }
320
321 return std::string();
322}
323
324std::string MetricsLog::GetInstallDate() const {
325 PrefService* pref = g_browser_process->local_state();
326 if (pref) {
327 return WideToUTF8(pref->GetString(prefs::kMetricsClientIDTimestamp));
328 } else {
329 NOTREACHED();
330 return "0";
331 }
332}
333
334void MetricsLog::WriteStabilityElement() {
335 DCHECK(!locked_);
336
337 PrefService* pref = g_browser_process->local_state();
338 DCHECK(pref);
339
340 // Get stability attributes out of Local State, zeroing out stored values.
341 // NOTE: This could lead to some data loss if this report isn't successfully
342 // sent, but that's true for all the metrics.
343
344 StartElement("stability");
345
346 WriteIntAttribute("launchcount",
347 pref->GetInteger(prefs::kStabilityLaunchCount));
348 pref->SetInteger(prefs::kStabilityLaunchCount, 0);
349 WriteIntAttribute("crashcount",
350 pref->GetInteger(prefs::kStabilityCrashCount));
351 pref->SetInteger(prefs::kStabilityCrashCount, 0);
[email protected]8e674e42008-07-30 16:05:48352 WriteIntAttribute("incompleteshutdowncount",
353 pref->GetInteger(
354 prefs::kStabilityIncompleteSessionEndCount));
355 pref->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0);
initial.commit09911bf2008-07-26 23:55:29356 WriteIntAttribute("pageloadcount",
357 pref->GetInteger(prefs::kStabilityPageLoadCount));
358 pref->SetInteger(prefs::kStabilityPageLoadCount, 0);
359 WriteIntAttribute("renderercrashcount",
360 pref->GetInteger(prefs::kStabilityRendererCrashCount));
361 pref->SetInteger(prefs::kStabilityRendererCrashCount, 0);
362 WriteIntAttribute("rendererhangcount",
363 pref->GetInteger(prefs::kStabilityRendererHangCount));
364 pref->SetInteger(prefs::kStabilityRendererHangCount, 0);
[email protected]e73c01972008-08-13 00:18:24365 WriteIntAttribute("breakpadregistrationok",
366 pref->GetInteger(prefs::kStabilityBreakpadRegistrationSuccess));
367 pref->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0);
368 WriteIntAttribute("breakpadregistrationfail",
369 pref->GetInteger(prefs::kStabilityBreakpadRegistrationFail));
370 pref->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0);
371 WriteIntAttribute("debuggerpresent",
372 pref->GetInteger(prefs::kStabilityDebuggerPresent));
373 pref->SetInteger(prefs::kStabilityDebuggerPresent, 0);
374 WriteIntAttribute("debuggernotpresent",
375 pref->GetInteger(prefs::kStabilityDebuggerNotPresent));
376 pref->SetInteger(prefs::kStabilityDebuggerNotPresent, 0);
initial.commit09911bf2008-07-26 23:55:29377
378 // Uptime is stored as a string, since there's no int64 in Value/JSON.
379 WriteAttribute("uptimesec",
380 WideToUTF8(pref->GetString(prefs::kStabilityUptimeSec)));
381 pref->SetString(prefs::kStabilityUptimeSec, L"0");
382
383 // Now log plugin stability info
384 const ListValue* plugin_stats_list = pref->GetList(
385 prefs::kStabilityPluginStats);
386 if (plugin_stats_list) {
387 StartElement("plugins");
388 for (ListValue::const_iterator iter = plugin_stats_list->begin();
389 iter != plugin_stats_list->end(); ++iter) {
390 if (!(*iter)->IsType(Value::TYPE_DICTIONARY)) {
391 NOTREACHED();
392 continue;
393 }
394 DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*iter);
395
396 std::wstring plugin_path;
397 plugin_dict->GetString(prefs::kStabilityPluginPath, &plugin_path);
398 plugin_path = file_util::GetFilenameFromPath(plugin_path);
399 if (plugin_path.empty()) {
400 NOTREACHED();
401 continue;
402 }
403
404 StartElement("pluginstability");
405 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(plugin_path)));
406
407 int launches = 0;
408 plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches);
409 WriteIntAttribute("launchcount", launches);
410
411 int instances = 0;
412 plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances);
413 WriteIntAttribute("instancecount", instances);
414
415 int crashes = 0;
416 plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes);
417 WriteIntAttribute("crashcount", crashes);
418 EndElement();
419 }
420
421 pref->ClearPref(prefs::kStabilityPluginStats);
422 EndElement();
423 }
424
425 EndElement();
426}
427
428void MetricsLog::WritePluginList(
429 const std::vector<WebPluginInfo>& plugin_list) {
430 DCHECK(!locked_);
431
432 StartElement("plugins");
433
434 for (std::vector<WebPluginInfo>::const_iterator iter = plugin_list.begin();
435 iter != plugin_list.end(); ++iter) {
436 StartElement("plugin");
437
438 // Plugin name and filename are hashed for the privacy of those
439 // testing unreleased new extensions.
440 WriteAttribute("name", CreateBase64Hash(WideToUTF8((*iter).name)));
441 std::wstring filename = file_util::GetFilenameFromPath((*iter).file);
442 WriteAttribute("filename", CreateBase64Hash(WideToUTF8(filename)));
443
444 WriteAttribute("version", WideToUTF8((*iter).version));
445
446 EndElement();
447 }
448
449 EndElement();
450}
451
452void MetricsLog::RecordEnvironment(
453 const std::vector<WebPluginInfo>& plugin_list,
454 const DictionaryValue* profile_metrics) {
455 DCHECK(!locked_);
456
457 PrefService* pref = g_browser_process->local_state();
458
459 StartElement("profile");
460 WriteCommonEventAttributes();
461
462 StartElement("install");
463 WriteAttribute("installdate", GetInstallDate());
464 WriteIntAttribute("buildid", 0); // means that we're using appversion instead
465 WriteAttribute("appversion", GetVersionString());
466 EndElement();
467
468 WritePluginList(plugin_list);
469
470 WriteStabilityElement();
471
472 {
473 OPEN_ELEMENT_FOR_SCOPE("cpu");
474 WriteAttribute("arch", env_util::GetCPUArchitecture());
475 }
476
477 {
478 OPEN_ELEMENT_FOR_SCOPE("security");
479 WriteIntAttribute("rendereronsboxdesktop",
480 pref->GetInteger(prefs::kSecurityRendererOnSboxDesktop));
481 pref->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0);
482
483 WriteIntAttribute("rendererondefaultdesktop",
484 pref->GetInteger(prefs::kSecurityRendererOnDefaultDesktop));
485 pref->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0);
486 }
487
488 {
489 OPEN_ELEMENT_FOR_SCOPE("memory");
490 WriteIntAttribute("mb", env_util::GetPhysicalMemoryMB());
491 }
492
493 {
494 OPEN_ELEMENT_FOR_SCOPE("os");
495 WriteAttribute("name",
496 env_util::GetOperatingSystemName());
497 WriteAttribute("version",
498 env_util::GetOperatingSystemVersion());
499 }
500
501 {
502 OPEN_ELEMENT_FOR_SCOPE("display");
503 int width = 0;
504 int height = 0;
505 env_util::GetPrimaryDisplayDimensions(&width, &height);
506 WriteIntAttribute("xsize", width);
507 WriteIntAttribute("ysize", height);
508 WriteIntAttribute("screens", env_util::GetDisplayCount());
509 }
510
511 {
512 OPEN_ELEMENT_FOR_SCOPE("bookmarks");
513 int num_bookmarks_on_bookmark_bar =
514 pref->GetInteger(prefs::kNumBookmarksOnBookmarkBar);
515 int num_folders_on_bookmark_bar =
516 pref->GetInteger(prefs::kNumFoldersOnBookmarkBar);
517 int num_bookmarks_in_other_bookmarks_folder =
518 pref->GetInteger(prefs::kNumBookmarksInOtherBookmarkFolder);
519 int num_folders_in_other_bookmarks_folder =
520 pref->GetInteger(prefs::kNumFoldersInOtherBookmarkFolder);
521 {
522 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
523 WriteAttribute("name", "full-tree");
524 WriteIntAttribute("foldercount",
525 num_folders_on_bookmark_bar + num_folders_in_other_bookmarks_folder);
526 WriteIntAttribute("itemcount",
527 num_bookmarks_on_bookmark_bar +
528 num_bookmarks_in_other_bookmarks_folder);
529 }
530 {
531 OPEN_ELEMENT_FOR_SCOPE("bookmarklocation");
532 WriteAttribute("name", "toolbar");
533 WriteIntAttribute("foldercount", num_folders_on_bookmark_bar);
534 WriteIntAttribute("itemcount", num_bookmarks_on_bookmark_bar);
535 }
536 }
537
538 {
539 OPEN_ELEMENT_FOR_SCOPE("keywords");
540 WriteIntAttribute("count", pref->GetInteger(prefs::kNumKeywords));
541 }
542
543 if (profile_metrics)
544 WriteAllProfilesMetrics(*profile_metrics);
545
546 EndElement(); // profile
547}
548
549void MetricsLog::WriteAllProfilesMetrics(
550 const DictionaryValue& all_profiles_metrics) {
551 const std::wstring profile_prefix(prefs::kProfilePrefix);
552 for (DictionaryValue::key_iterator i = all_profiles_metrics.begin_keys();
553 i != all_profiles_metrics.end_keys(); ++i) {
554 const std::wstring& key_name = *i;
555 if (key_name.compare(0, profile_prefix.size(), profile_prefix) == 0) {
556 DictionaryValue* profile;
557 if (all_profiles_metrics.GetDictionary(key_name, &profile))
558 WriteProfileMetrics(key_name.substr(profile_prefix.size()), *profile);
559 }
560 }
561}
562
563void MetricsLog::WriteProfileMetrics(const std::wstring& profileidhash,
564 const DictionaryValue& profile_metrics) {
565 OPEN_ELEMENT_FOR_SCOPE("userprofile");
566 WriteAttribute("profileidhash", WideToUTF8(profileidhash));
567 for (DictionaryValue::key_iterator i = profile_metrics.begin_keys();
568 i != profile_metrics.end_keys(); ++i) {
569 Value* value;
570 if (profile_metrics.Get(*i, &value)) {
571 DCHECK(*i != L"id");
572 switch (value->GetType()) {
573 case Value::TYPE_STRING: {
574 std::wstring string_value;
575 if (value->GetAsString(&string_value)) {
576 OPEN_ELEMENT_FOR_SCOPE("profileparam");
577 WriteAttribute("name", WideToUTF8(*i));
578 WriteAttribute("value", WideToUTF8(string_value));
579 }
580 break;
581 }
582
583 case Value::TYPE_BOOLEAN: {
584 bool bool_value;
585 if (value->GetAsBoolean(&bool_value)) {
586 OPEN_ELEMENT_FOR_SCOPE("profileparam");
587 WriteAttribute("name", WideToUTF8(*i));
588 WriteIntAttribute("value", bool_value ? 1 : 0);
589 }
590 break;
591 }
592
593 case Value::TYPE_INTEGER: {
594 int int_value;
595 if (value->GetAsInteger(&int_value)) {
596 OPEN_ELEMENT_FOR_SCOPE("profileparam");
597 WriteAttribute("name", WideToUTF8(*i));
598 WriteIntAttribute("value", int_value);
599 }
600 break;
601 }
602
603 default:
604 NOTREACHED();
605 break;
606 }
607 }
608 }
609}
610
611void MetricsLog::RecordOmniboxOpenedURL(const AutocompleteLog& log) {
612 DCHECK(!locked_);
613
614 StartElement("uielement");
615 WriteAttribute("action", "autocomplete");
616 WriteAttribute("targetidhash", "");
617 // TODO(kochi): Properly track windows.
618 WriteIntAttribute("window", 0);
619 WriteCommonEventAttributes();
620
621 StartElement("autocomplete");
622
623 WriteIntAttribute("typedlength", static_cast<int>(log.text.length()));
624 WriteIntAttribute("completedlength",
625 static_cast<int>(log.inline_autocompleted_length));
626 WriteIntAttribute("selectedindex", static_cast<int>(log.selected_index));
627
628 for (AutocompleteResult::const_iterator i(log.result.begin());
629 i != log.result.end(); ++i) {
630 StartElement("autocompleteitem");
631 if (i->provider)
632 WriteAttribute("provider", i->provider->name());
633 WriteIntAttribute("relevance", i->relevance);
634 WriteIntAttribute("isstarred", i->starred ? 1 : 0);
635 EndElement(); // autocompleteitem
636 }
637 EndElement(); // autocomplete
638 EndElement(); // uielement
639
640 ++num_events_;
641}
642
643// TODO(JAR): A The following should really be part of the histogram class.
644// Internal state is being needlessly exposed, and it would be hard to reuse
645// this code. If we moved this into the Histogram class, then we could use
646// the same infrastructure for logging StatsCounters, RatesCounters, etc.
647void MetricsLog::RecordHistogramDelta(const Histogram& histogram,
648 const Histogram::SampleSet& snapshot) {
649 DCHECK(!locked_);
650 DCHECK(0 != snapshot.TotalCount());
651 snapshot.CheckSize(histogram);
652
653 // We will ignore the MAX_INT/infinite value in the last element of range[].
654
655 OPEN_ELEMENT_FOR_SCOPE("histogram");
656
657 WriteAttribute("name", CreateBase64Hash(histogram.histogram_name()));
658
659 WriteInt64Attribute("sum", snapshot.sum());
660 WriteInt64Attribute("sumsquares", snapshot.square_sum());
661
662 for (size_t i = 0; i < histogram.bucket_count(); i++) {
663 if (snapshot.counts(i)) {
664 OPEN_ELEMENT_FOR_SCOPE("histogrambucket");
665 WriteIntAttribute("min", histogram.ranges(i));
666 WriteIntAttribute("max", histogram.ranges(i + 1));
667 WriteIntAttribute("count", snapshot.counts(i));
668 }
669 }
670}