Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / components / metrics / metrics_log_base.cc
1 // Copyright 2014 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 "components/metrics/metrics_log_base.h"
6
7 #include "base/metrics/histogram_base.h"
8 #include "base/metrics/histogram_samples.h"
9 #include "components/metrics/metrics_hashes.h"
10 #include "components/metrics/proto/histogram_event.pb.h"
11 #include "components/metrics/proto/system_profile.pb.h"
12 #include "components/metrics/proto/user_action_event.pb.h"
13
14 using base::Histogram;
15 using base::HistogramBase;
16 using base::HistogramSamples;
17 using base::SampleCountIterator;
18 using base::Time;
19 using base::TimeDelta;
20 using metrics::HistogramEventProto;
21 using metrics::SystemProfileProto;
22 using metrics::UserActionEventProto;
23
24 namespace metrics {
25 namespace {
26
27 // Any id less than 16 bytes is considered to be a testing id.
28 bool IsTestingID(const std::string& id) {
29   return id.size() < 16;
30 }
31
32 }  // namespace
33
34 MetricsLogBase::MetricsLogBase(const std::string& client_id,
35                                int session_id,
36                                LogType log_type,
37                                const std::string& version_string)
38     : num_events_(0),
39       locked_(false),
40       log_type_(log_type) {
41   DCHECK_NE(NO_LOG, log_type);
42   if (IsTestingID(client_id))
43     uma_proto_.set_client_id(0);
44   else
45     uma_proto_.set_client_id(Hash(client_id));
46
47   uma_proto_.set_session_id(session_id);
48   uma_proto_.mutable_system_profile()->set_build_timestamp(GetBuildTime());
49   uma_proto_.mutable_system_profile()->set_app_version(version_string);
50 }
51
52 MetricsLogBase::~MetricsLogBase() {}
53
54 // static
55 uint64 MetricsLogBase::Hash(const std::string& value) {
56   uint64 hash = metrics::HashMetricName(value);
57
58   // The following log is VERY helpful when folks add some named histogram into
59   // the code, but forgot to update the descriptive list of histograms.  When
60   // that happens, all we get to see (server side) is a hash of the histogram
61   // name.  We can then use this logging to find out what histogram name was
62   // being hashed to a given MD5 value by just running the version of Chromium
63   // in question with --enable-logging.
64   DVLOG(1) << "Metrics: Hash numeric [" << value << "]=[" << hash << "]";
65
66   return hash;
67 }
68
69 // static
70 int64 MetricsLogBase::GetBuildTime() {
71   static int64 integral_build_time = 0;
72   if (!integral_build_time) {
73     Time time;
74     const char* kDateTime = __DATE__ " " __TIME__ " GMT";
75     bool result = Time::FromString(kDateTime, &time);
76     DCHECK(result);
77     integral_build_time = static_cast<int64>(time.ToTimeT());
78   }
79   return integral_build_time;
80 }
81
82 // static
83 int64 MetricsLogBase::GetCurrentTime() {
84   return (base::TimeTicks::Now() - base::TimeTicks()).InSeconds();
85 }
86
87 void MetricsLogBase::CloseLog() {
88   DCHECK(!locked_);
89   locked_ = true;
90 }
91
92 void MetricsLogBase::GetEncodedLog(std::string* encoded_log) {
93   DCHECK(locked_);
94   uma_proto_.SerializeToString(encoded_log);
95 }
96
97 void MetricsLogBase::RecordUserAction(const std::string& key) {
98   DCHECK(!locked_);
99
100   UserActionEventProto* user_action = uma_proto_.add_user_action_event();
101   user_action->set_name_hash(Hash(key));
102   user_action->set_time(GetCurrentTime());
103
104   ++num_events_;
105 }
106
107 void MetricsLogBase::RecordHistogramDelta(const std::string& histogram_name,
108                                           const HistogramSamples& snapshot) {
109   DCHECK(!locked_);
110   DCHECK_NE(0, snapshot.TotalCount());
111
112   // We will ignore the MAX_INT/infinite value in the last element of range[].
113
114   HistogramEventProto* histogram_proto = uma_proto_.add_histogram_event();
115   histogram_proto->set_name_hash(Hash(histogram_name));
116   histogram_proto->set_sum(snapshot.sum());
117
118   for (scoped_ptr<SampleCountIterator> it = snapshot.Iterator(); !it->Done();
119        it->Next()) {
120     HistogramBase::Sample min;
121     HistogramBase::Sample max;
122     HistogramBase::Count count;
123     it->Get(&min, &max, &count);
124     HistogramEventProto::Bucket* bucket = histogram_proto->add_bucket();
125     bucket->set_min(min);
126     bucket->set_max(max);
127     bucket->set_count(count);
128   }
129
130   // Omit fields to save space (see rules in histogram_event.proto comments).
131   for (int i = 0; i < histogram_proto->bucket_size(); ++i) {
132     HistogramEventProto::Bucket* bucket = histogram_proto->mutable_bucket(i);
133     if (i + 1 < histogram_proto->bucket_size() &&
134         bucket->max() == histogram_proto->bucket(i + 1).min()) {
135       bucket->clear_max();
136     } else if (bucket->max() == bucket->min() + 1) {
137       bucket->clear_min();
138     }
139   }
140 }
141
142 }  // namespace metrics