1 : /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is mozilla.org code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * The Mozilla Foundation <http://www.mozilla.org/>.
19 : * Portions created by the Initial Developer are Copyright (C) 2011
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Taras Glek <tglek@mozilla.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #ifndef Telemetry_h__
40 : #define Telemetry_h__
41 :
42 : #include "mozilla/GuardObjects.h"
43 : #include "mozilla/TimeStamp.h"
44 : #include "mozilla/StartupTimeline.h"
45 :
46 : namespace base {
47 : class Histogram;
48 : }
49 :
50 : namespace mozilla {
51 : namespace Telemetry {
52 :
53 : enum ID {
54 : #define HISTOGRAM(name, a, b, c, d, e) name,
55 :
56 : #include "TelemetryHistograms.h"
57 :
58 : #undef HISTOGRAM
59 : HistogramCount
60 : };
61 :
62 : /**
63 : * Initialize the Telemetry service on the main thread at startup.
64 : */
65 : void Init();
66 :
67 : /**
68 : * Adds sample to a histogram defined in TelemetryHistograms.h
69 : *
70 : * @param id - histogram id
71 : * @param sample - value to record.
72 : */
73 : void Accumulate(ID id, PRUint32 sample);
74 :
75 : /**
76 : * Adds time delta in milliseconds to a histogram defined in TelemetryHistograms.h
77 : *
78 : * @param id - histogram id
79 : * @param start - start time
80 : * @param end - end time
81 : */
82 : void AccumulateTimeDelta(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now());
83 :
84 : /**
85 : * Return a raw Histogram for direct manipulation for users who can not use Accumulate().
86 : */
87 : base::Histogram* GetHistogramById(ID id);
88 :
89 : template<ID id>
90 : class AutoTimer {
91 : public:
92 136553 : AutoTimer(TimeStamp aStart = TimeStamp::Now() MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
93 136553 : : start(aStart)
94 : {
95 136553 : MOZ_GUARD_OBJECT_NOTIFIER_INIT;
96 136553 : }
97 :
98 136553 : ~AutoTimer() {
99 136553 : AccumulateTimeDelta(id, start);
100 136553 : }
101 :
102 : private:
103 : const TimeStamp start;
104 : MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
105 : };
106 :
107 : template<ID id>
108 : class AutoCounter {
109 : public:
110 0 : AutoCounter(PRUint32 counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
111 0 : : counter(counterStart)
112 : {
113 0 : MOZ_GUARD_OBJECT_NOTIFIER_INIT;
114 0 : }
115 :
116 0 : ~AutoCounter() {
117 0 : Accumulate(id, counter);
118 0 : }
119 :
120 : // Prefix increment only, to encourage good habits.
121 0 : void operator++() {
122 0 : ++counter;
123 0 : }
124 :
125 : // Chaining doesn't make any sense, don't return anything.
126 : void operator+=(int increment) {
127 : counter += increment;
128 : }
129 :
130 : private:
131 : PRUint32 counter;
132 : MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
133 : };
134 :
135 : /**
136 : * Indicates whether Telemetry recording is turned on. This is intended
137 : * to guard calls to Accumulate when the statistic being recorded is
138 : * expensive to compute.
139 : */
140 : bool CanRecord();
141 :
142 : /**
143 : * Records slow SQL statements for Telemetry reporting.
144 : * For privacy reasons, only prepared statements are reported.
145 : *
146 : * @param statement - offending SQL statement to record
147 : * @param dbName - DB filename; reporting is only done for whitelisted DBs
148 : * @param delay - execution time in milliseconds
149 : */
150 : void RecordSlowSQLStatement(const nsACString &statement,
151 : const nsACString &dbName,
152 : PRUint32 delay);
153 :
154 : /**
155 : * Threshold for a statement to be considered slow, in milliseconds
156 : */
157 : const PRUint32 kSlowStatementThreshold = 100;
158 :
159 : } // namespace Telemetry
160 : } // namespace mozilla
161 : #endif // Telemetry_h__
|