1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/toolkit/components/telemetry/Telemetry.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,229 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#ifndef Telemetry_h__ 1.10 +#define Telemetry_h__ 1.11 + 1.12 +#include "mozilla/GuardObjects.h" 1.13 +#include "mozilla/TimeStamp.h" 1.14 +#include "mozilla/StartupTimeline.h" 1.15 +#include "nsTArray.h" 1.16 +#include "nsStringGlue.h" 1.17 + 1.18 +namespace base { 1.19 + class Histogram; 1.20 +} 1.21 + 1.22 +namespace mozilla { 1.23 +namespace Telemetry { 1.24 + 1.25 +#include "TelemetryHistogramEnums.h" 1.26 + 1.27 +enum TimerResolution { 1.28 + Millisecond, 1.29 + Microsecond 1.30 +}; 1.31 + 1.32 +/** 1.33 + * Initialize the Telemetry service on the main thread at startup. 1.34 + */ 1.35 +void Init(); 1.36 + 1.37 +/** 1.38 + * Adds sample to a histogram defined in TelemetryHistograms.h 1.39 + * 1.40 + * @param id - histogram id 1.41 + * @param sample - value to record. 1.42 + */ 1.43 +void Accumulate(ID id, uint32_t sample); 1.44 + 1.45 +/** 1.46 + * Adds a sample to a histogram defined in TelemetryHistograms.h. 1.47 + * This function is here to support telemetry measurements from Java, 1.48 + * where we have only names and not numeric IDs. You should almost 1.49 + * certainly be using the by-enum-id version instead of this one. 1.50 + * 1.51 + * @param name - histogram name 1.52 + * @param sample - value to record 1.53 + */ 1.54 +void Accumulate(const char* name, uint32_t sample); 1.55 + 1.56 +/** 1.57 + * Adds time delta in milliseconds to a histogram defined in TelemetryHistograms.h 1.58 + * 1.59 + * @param id - histogram id 1.60 + * @param start - start time 1.61 + * @param end - end time 1.62 + */ 1.63 +void AccumulateTimeDelta(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()); 1.64 + 1.65 +/** 1.66 + * Return a raw Histogram for direct manipulation for users who can not use Accumulate(). 1.67 + */ 1.68 +base::Histogram* GetHistogramById(ID id); 1.69 + 1.70 +/** 1.71 + * Those wrappers are needed because the VS versions we use do not support free 1.72 + * functions with default template arguments. 1.73 + */ 1.74 +template<TimerResolution res> 1.75 +struct AccumulateDelta_impl 1.76 +{ 1.77 + static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()); 1.78 +}; 1.79 + 1.80 +template<> 1.81 +struct AccumulateDelta_impl<Millisecond> 1.82 +{ 1.83 + static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) { 1.84 + Accumulate(id, static_cast<uint32_t>((end - start).ToMilliseconds())); 1.85 + } 1.86 +}; 1.87 + 1.88 +template<> 1.89 +struct AccumulateDelta_impl<Microsecond> 1.90 +{ 1.91 + static void compute(ID id, TimeStamp start, TimeStamp end = TimeStamp::Now()) { 1.92 + Accumulate(id, static_cast<uint32_t>((end - start).ToMicroseconds())); 1.93 + } 1.94 +}; 1.95 + 1.96 + 1.97 +template<ID id, TimerResolution res = Millisecond> 1.98 +class AutoTimer { 1.99 +public: 1.100 + AutoTimer(TimeStamp aStart = TimeStamp::Now() MOZ_GUARD_OBJECT_NOTIFIER_PARAM) 1.101 + : start(aStart) 1.102 + { 1.103 + MOZ_GUARD_OBJECT_NOTIFIER_INIT; 1.104 + } 1.105 + 1.106 + ~AutoTimer() { 1.107 + AccumulateDelta_impl<res>::compute(id, start); 1.108 + } 1.109 + 1.110 +private: 1.111 + const TimeStamp start; 1.112 + MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER 1.113 +}; 1.114 + 1.115 +template<ID id> 1.116 +class AutoCounter { 1.117 +public: 1.118 + AutoCounter(uint32_t counterStart = 0 MOZ_GUARD_OBJECT_NOTIFIER_PARAM) 1.119 + : counter(counterStart) 1.120 + { 1.121 + MOZ_GUARD_OBJECT_NOTIFIER_INIT; 1.122 + } 1.123 + 1.124 + ~AutoCounter() { 1.125 + Accumulate(id, counter); 1.126 + } 1.127 + 1.128 + // Prefix increment only, to encourage good habits. 1.129 + void operator++() { 1.130 + ++counter; 1.131 + } 1.132 + 1.133 + // Chaining doesn't make any sense, don't return anything. 1.134 + void operator+=(int increment) { 1.135 + counter += increment; 1.136 + } 1.137 + 1.138 +private: 1.139 + uint32_t counter; 1.140 + MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER 1.141 +}; 1.142 + 1.143 +/** 1.144 + * Indicates whether Telemetry recording is turned on. This is intended 1.145 + * to guard calls to Accumulate when the statistic being recorded is 1.146 + * expensive to compute. 1.147 + */ 1.148 +bool CanRecord(); 1.149 + 1.150 +/** 1.151 + * Records slow SQL statements for Telemetry reporting. 1.152 + * 1.153 + * @param statement - offending SQL statement to record 1.154 + * @param dbName - DB filename 1.155 + * @param delay - execution time in milliseconds 1.156 + */ 1.157 +void RecordSlowSQLStatement(const nsACString &statement, 1.158 + const nsACString &dbName, 1.159 + uint32_t delay); 1.160 + 1.161 +/** 1.162 + * Initialize I/O Reporting 1.163 + * Initially this only records I/O for files in the binary directory. 1.164 + * 1.165 + * @param aXreDir - XRE directory 1.166 + */ 1.167 +void InitIOReporting(nsIFile* aXreDir); 1.168 + 1.169 +/** 1.170 + * Set the profile directory. Once called, files in the profile directory will 1.171 + * be included in I/O reporting. We can't use the directory 1.172 + * service to obtain this information because it isn't running yet. 1.173 + */ 1.174 +void SetProfileDir(nsIFile* aProfD); 1.175 + 1.176 +/** 1.177 + * Called to inform Telemetry that startup has completed. 1.178 + */ 1.179 +void LeavingStartupStage(); 1.180 + 1.181 +/** 1.182 + * Called to inform Telemetry that shutdown is commencing. 1.183 + */ 1.184 +void EnteringShutdownStage(); 1.185 + 1.186 +/** 1.187 + * Thresholds for a statement to be considered slow, in milliseconds 1.188 + */ 1.189 +const uint32_t kSlowSQLThresholdForMainThread = 50; 1.190 +const uint32_t kSlowSQLThresholdForHelperThreads = 100; 1.191 + 1.192 +class ProcessedStack; 1.193 + 1.194 +/** 1.195 + * Record the main thread's call stack after it hangs. 1.196 + * 1.197 + * @param aDuration - Approximate duration of main thread hang, in seconds 1.198 + * @param aStack - Array of PCs from the hung call stack 1.199 + * @param aSystemUptime - System uptime at the time of the hang, in minutes 1.200 + * @param aFirefoxUptime - Firefox uptime at the time of the hang, in minutes 1.201 + */ 1.202 +#if defined(MOZ_ENABLE_PROFILER_SPS) 1.203 +void RecordChromeHang(uint32_t aDuration, 1.204 + ProcessedStack &aStack, 1.205 + int32_t aSystemUptime, 1.206 + int32_t aFirefoxUptime); 1.207 +#endif 1.208 + 1.209 +class ThreadHangStats; 1.210 + 1.211 +/** 1.212 + * Move a ThreadHangStats to Telemetry storage. Normally Telemetry queries 1.213 + * for active ThreadHangStats through BackgroundHangMonitor, but once a 1.214 + * thread exits, the thread's copy of ThreadHangStats needs to be moved to 1.215 + * inside Telemetry using this function. 1.216 + * 1.217 + * @param aStats ThreadHangStats to save; the data inside aStats 1.218 + * will be moved and aStats should be treated as 1.219 + * invalid after this function returns 1.220 + */ 1.221 +void RecordThreadHangStats(ThreadHangStats& aStats); 1.222 + 1.223 +/** 1.224 + * Record a failed attempt at locking the user's profile. 1.225 + * 1.226 + * @param aProfileDir The profile directory whose lock attempt failed 1.227 + */ 1.228 +void WriteFailedProfileLock(nsIFile* aProfileDir); 1.229 + 1.230 +} // namespace Telemetry 1.231 +} // namespace mozilla 1.232 +#endif // Telemetry_h__