Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef Telemetry_h__ |
michael@0 | 7 | #define Telemetry_h__ |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/GuardObjects.h" |
michael@0 | 10 | #include "mozilla/TimeStamp.h" |
michael@0 | 11 | #include "mozilla/StartupTimeline.h" |
michael@0 | 12 | #include "nsTArray.h" |
michael@0 | 13 | #include "nsStringGlue.h" |
michael@0 | 14 | |
michael@0 | 15 | namespace base { |
michael@0 | 16 | class Histogram; |
michael@0 | 17 | } |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace Telemetry { |
michael@0 | 21 | |
michael@0 | 22 | #include "TelemetryHistogramEnums.h" |
michael@0 | 23 | |
michael@0 | 24 | enum TimerResolution { |
michael@0 | 25 | Millisecond, |
michael@0 | 26 | Microsecond |
michael@0 | 27 | }; |
michael@0 | 28 | |
michael@0 | 29 | /** |
michael@0 | 30 | * Initialize the Telemetry service on the main thread at startup. |
michael@0 | 31 | */ |
michael@0 | 32 | void Init(); |
michael@0 | 33 | |
michael@0 | 34 | /** |
michael@0 | 35 | * Adds sample to a histogram defined in TelemetryHistograms.h |
michael@0 | 36 | * |
michael@0 | 37 | * @param id - histogram id |
michael@0 | 38 | * @param sample - value to record. |
michael@0 | 39 | */ |
michael@0 | 40 | void Accumulate(ID id, uint32_t sample); |
michael@0 | 41 | |
michael@0 | 42 | /** |
michael@0 | 43 | * Adds a sample to a histogram defined in TelemetryHistograms.h. |
michael@0 | 44 | * This function is here to support telemetry measurements from Java, |
michael@0 | 45 | * where we have only names and not numeric IDs. You should almost |
michael@0 | 46 | * certainly be using the by-enum-id version instead of this one. |
michael@0 | 47 | * |
michael@0 | 48 | * @param name - histogram name |
michael@0 | 49 | * @param sample - value to record |
michael@0 | 50 | */ |
michael@0 | 51 | void Accumulate(const char* name, uint32_t sample); |
michael@0 | 52 | |
michael@0 | 53 | /** |
michael@0 | 54 | * Adds time delta in milliseconds to a histogram defined in TelemetryHistograms.h |
michael@0 | 55 | * |
michael@0 | 56 | * @param id - histogram id |
michael@0 | 57 | * @param start - start time |
michael@0 | 58 | * @param end - end time |
michael@0 | 59 | */ |
michael@0 | 60 | void AccumulateTimeDelta(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()); |
michael@0 | 61 | |
michael@0 | 62 | /** |
michael@0 | 63 | * Return a raw Histogram for direct manipulation for users who can not use Accumulate(). |
michael@0 | 64 | */ |
michael@0 | 65 | base::Histogram* GetHistogramById(ID id); |
michael@0 | 66 | |
michael@0 | 67 | /** |
michael@0 | 68 | * Those wrappers are needed because the VS versions we use do not support free |
michael@0 | 69 | * functions with default template arguments. |
michael@0 | 70 | */ |
michael@0 | 71 | template<TimerResolution res> |
michael@0 | 72 | struct AccumulateDelta_impl |
michael@0 | 73 | { |
michael@0 | 74 | static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()); |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | template<> |
michael@0 | 78 | struct AccumulateDelta_impl<Millisecond> |
michael@0 | 79 | { |
michael@0 | 80 | static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) { |
michael@0 | 81 | Accumulate(id, static_cast<uint32_t>((end - start).ToMilliseconds())); |
michael@0 | 82 | } |
michael@0 | 83 | }; |
michael@0 | 84 | |
michael@0 | 85 | template<> |
michael@0 | 86 | struct AccumulateDelta_impl<Microsecond> |
michael@0 | 87 | { |
michael@0 | 88 | static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) { |
michael@0 | 89 | Accumulate(id, static_cast<uint32_t>((end - start).ToMicroseconds())); |
michael@0 | 90 | } |
michael@0 | 91 | }; |
michael@0 | 92 | |
michael@0 | 93 | |
michael@0 | 94 | template<ID id, TimerResolution res = Millisecond> |
michael@0 | 95 | class AutoTimer { |
michael@0 | 96 | public: |
michael@0 | 97 | AutoTimer(TimeStamp aStart = TimeStamp::Now() MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
michael@0 | 98 | : start(aStart) |
michael@0 | 99 | { |
michael@0 | 100 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | ~AutoTimer() { |
michael@0 | 104 | AccumulateDelta_impl<res>::compute(id, start); |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | private: |
michael@0 | 108 | const TimeStamp start; |
michael@0 | 109 | MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
michael@0 | 110 | }; |
michael@0 | 111 | |
michael@0 | 112 | template<ID id> |
michael@0 | 113 | class AutoCounter { |
michael@0 | 114 | public: |
michael@0 | 115 | AutoCounter(uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) |
michael@0 | 116 | : counter(counterStart) |
michael@0 | 117 | { |
michael@0 | 118 | MOZ_GUARD_OBJECT_NOTIFIER_INIT; |
michael@0 | 119 | } |
michael@0 | 120 | |
michael@0 | 121 | ~AutoCounter() { |
michael@0 | 122 | Accumulate(id, counter); |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | // Prefix increment only, to encourage good habits. |
michael@0 | 126 | void operator++() { |
michael@0 | 127 | ++counter; |
michael@0 | 128 | } |
michael@0 | 129 | |
michael@0 | 130 | // Chaining doesn't make any sense, don't return anything. |
michael@0 | 131 | void operator+=(int increment) { |
michael@0 | 132 | counter += increment; |
michael@0 | 133 | } |
michael@0 | 134 | |
michael@0 | 135 | private: |
michael@0 | 136 | uint32_t counter; |
michael@0 | 137 | MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER |
michael@0 | 138 | }; |
michael@0 | 139 | |
michael@0 | 140 | /** |
michael@0 | 141 | * Indicates whether Telemetry recording is turned on. This is intended |
michael@0 | 142 | * to guard calls to Accumulate when the statistic being recorded is |
michael@0 | 143 | * expensive to compute. |
michael@0 | 144 | */ |
michael@0 | 145 | bool CanRecord(); |
michael@0 | 146 | |
michael@0 | 147 | /** |
michael@0 | 148 | * Records slow SQL statements for Telemetry reporting. |
michael@0 | 149 | * |
michael@0 | 150 | * @param statement - offending SQL statement to record |
michael@0 | 151 | * @param dbName - DB filename |
michael@0 | 152 | * @param delay - execution time in milliseconds |
michael@0 | 153 | */ |
michael@0 | 154 | void RecordSlowSQLStatement(const nsACString &statement, |
michael@0 | 155 | const nsACString &dbName, |
michael@0 | 156 | uint32_t delay); |
michael@0 | 157 | |
michael@0 | 158 | /** |
michael@0 | 159 | * Initialize I/O Reporting |
michael@0 | 160 | * Initially this only records I/O for files in the binary directory. |
michael@0 | 161 | * |
michael@0 | 162 | * @param aXreDir - XRE directory |
michael@0 | 163 | */ |
michael@0 | 164 | void InitIOReporting(nsIFile* aXreDir); |
michael@0 | 165 | |
michael@0 | 166 | /** |
michael@0 | 167 | * Set the profile directory. Once called, files in the profile directory will |
michael@0 | 168 | * be included in I/O reporting. We can't use the directory |
michael@0 | 169 | * service to obtain this information because it isn't running yet. |
michael@0 | 170 | */ |
michael@0 | 171 | void SetProfileDir(nsIFile* aProfD); |
michael@0 | 172 | |
michael@0 | 173 | /** |
michael@0 | 174 | * Called to inform Telemetry that startup has completed. |
michael@0 | 175 | */ |
michael@0 | 176 | void LeavingStartupStage(); |
michael@0 | 177 | |
michael@0 | 178 | /** |
michael@0 | 179 | * Called to inform Telemetry that shutdown is commencing. |
michael@0 | 180 | */ |
michael@0 | 181 | void EnteringShutdownStage(); |
michael@0 | 182 | |
michael@0 | 183 | /** |
michael@0 | 184 | * Thresholds for a statement to be considered slow, in milliseconds |
michael@0 | 185 | */ |
michael@0 | 186 | const uint32_t kSlowSQLThresholdForMainThread = 50; |
michael@0 | 187 | const uint32_t kSlowSQLThresholdForHelperThreads = 100; |
michael@0 | 188 | |
michael@0 | 189 | class ProcessedStack; |
michael@0 | 190 | |
michael@0 | 191 | /** |
michael@0 | 192 | * Record the main thread's call stack after it hangs. |
michael@0 | 193 | * |
michael@0 | 194 | * @param aDuration - Approximate duration of main thread hang, in seconds |
michael@0 | 195 | * @param aStack - Array of PCs from the hung call stack |
michael@0 | 196 | * @param aSystemUptime - System uptime at the time of the hang, in minutes |
michael@0 | 197 | * @param aFirefoxUptime - Firefox uptime at the time of the hang, in minutes |
michael@0 | 198 | */ |
michael@0 | 199 | #if defined(MOZ_ENABLE_PROFILER_SPS) |
michael@0 | 200 | void RecordChromeHang(uint32_t aDuration, |
michael@0 | 201 | ProcessedStack &aStack, |
michael@0 | 202 | int32_t aSystemUptime, |
michael@0 | 203 | int32_t aFirefoxUptime); |
michael@0 | 204 | #endif |
michael@0 | 205 | |
michael@0 | 206 | class ThreadHangStats; |
michael@0 | 207 | |
michael@0 | 208 | /** |
michael@0 | 209 | * Move a ThreadHangStats to Telemetry storage. Normally Telemetry queries |
michael@0 | 210 | * for active ThreadHangStats through BackgroundHangMonitor, but once a |
michael@0 | 211 | * thread exits, the thread's copy of ThreadHangStats needs to be moved to |
michael@0 | 212 | * inside Telemetry using this function. |
michael@0 | 213 | * |
michael@0 | 214 | * @param aStats ThreadHangStats to save; the data inside aStats |
michael@0 | 215 | * will be moved and aStats should be treated as |
michael@0 | 216 | * invalid after this function returns |
michael@0 | 217 | */ |
michael@0 | 218 | void RecordThreadHangStats(ThreadHangStats& aStats); |
michael@0 | 219 | |
michael@0 | 220 | /** |
michael@0 | 221 | * Record a failed attempt at locking the user's profile. |
michael@0 | 222 | * |
michael@0 | 223 | * @param aProfileDir The profile directory whose lock attempt failed |
michael@0 | 224 | */ |
michael@0 | 225 | void WriteFailedProfileLock(nsIFile* aProfileDir); |
michael@0 | 226 | |
michael@0 | 227 | } // namespace Telemetry |
michael@0 | 228 | } // namespace mozilla |
michael@0 | 229 | #endif // Telemetry_h__ |