Bug 1773770: Part 12 - Remove XPCOM Module infrastructure. r=xpcom-reviewers,nika
[gecko.git] / netwerk / cache2 / CacheFileUtils.h
blob5e4a97d9cba7e7ef3a7f01bdee4298b175b30978
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef CacheFileUtils__h__
6 #define CacheFileUtils__h__
8 #include "nsError.h"
9 #include "nsCOMPtr.h"
10 #include "nsString.h"
11 #include "nsTArray.h"
12 #include "mozilla/Mutex.h"
13 #include "mozilla/StaticMutex.h"
14 #include "mozilla/TimeStamp.h"
16 class nsILoadContextInfo;
18 namespace mozilla {
19 namespace net {
20 namespace CacheFileUtils {
22 extern const char* kAltDataKey;
24 already_AddRefed<nsILoadContextInfo> ParseKey(const nsACString& aKey,
25 nsACString* aIdEnhance = nullptr,
26 nsACString* aURISpec = nullptr);
28 void AppendKeyPrefix(nsILoadContextInfo* aInfo, nsACString& _retval);
30 void AppendTagWithValue(nsACString& aTarget, char const aTag,
31 const nsACString& aValue);
33 nsresult KeyMatchesLoadContextInfo(const nsACString& aKey,
34 nsILoadContextInfo* aInfo, bool* _retval);
36 class ValidityPair {
37 public:
38 ValidityPair(uint32_t aOffset, uint32_t aLen);
40 ValidityPair& operator=(const ValidityPair& aOther) = default;
42 // Returns true when two pairs can be merged, i.e. they do overlap or the one
43 // ends exactly where the other begins.
44 bool CanBeMerged(const ValidityPair& aOther) const;
46 // Returns true when aOffset is placed anywhere in the validity interval or
47 // exactly after its end.
48 bool IsInOrFollows(uint32_t aOffset) const;
50 // Returns true when this pair has lower offset than the other pair. In case
51 // both pairs have the same offset it returns true when this pair has a
52 // shorter length.
53 bool LessThan(const ValidityPair& aOther) const;
55 // Merges two pair into one.
56 void Merge(const ValidityPair& aOther);
58 uint32_t Offset() const { return mOffset; }
59 uint32_t Len() const { return mLen; }
61 private:
62 uint32_t mOffset;
63 uint32_t mLen;
66 class ValidityMap {
67 public:
68 // Prints pairs in the map into log.
69 void Log() const;
71 // Returns number of pairs in the map.
72 uint32_t Length() const;
74 // Adds a new pair to the map. It keeps the pairs ordered and merges pairs
75 // when possible.
76 void AddPair(uint32_t aOffset, uint32_t aLen);
78 // Removes all pairs from the map.
79 void Clear();
81 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
83 ValidityPair& operator[](uint32_t aIdx);
85 private:
86 nsTArray<ValidityPair> mMap;
89 class DetailedCacheHitTelemetry {
90 public:
91 enum ERecType { HIT = 0, MISS = 1 };
93 static void AddRecord(ERecType aType, TimeStamp aLoadStart);
95 private:
96 class HitRate {
97 public:
98 HitRate();
100 void AddRecord(ERecType aType);
101 // Returns the bucket index that the current hit rate falls into according
102 // to the given aNumOfBuckets.
103 uint32_t GetHitRateBucket(uint32_t aNumOfBuckets) const;
104 uint32_t Count();
105 void Reset();
107 private:
108 uint32_t mHitCnt = 0;
109 uint32_t mMissCnt = 0;
112 // Group the hits and misses statistics by cache files count ranges (0-5000,
113 // 5001-10000, ... , 95001- )
114 static const uint32_t kRangeSize = 5000;
115 static const uint32_t kNumOfRanges = 20;
117 // Use the same ranges to report an average hit rate. Report the hit rates
118 // (and reset the counters) every kTotalSamplesReportLimit samples.
119 static const uint32_t kTotalSamplesReportLimit = 1000;
121 // Report hit rate for a given cache size range only if it contains
122 // kHitRateSamplesReportLimit or more samples. This limit should avoid
123 // reporting a biased statistics.
124 static const uint32_t kHitRateSamplesReportLimit = 500;
126 // All hit rates are accumulated in a single telemetry probe, so to use
127 // a sane number of enumerated values the hit rate is divided into buckets
128 // instead of using a percent value. This constant defines number of buckets
129 // that we divide the hit rates into. I.e. we'll report ranges 0%-5%, 5%-10%,
130 // 10-%15%, ...
131 static const uint32_t kHitRateBuckets = 20;
133 // Protects sRecordCnt, sHitStats and Telemetry::Accumulated() calls.
134 static StaticMutex sLock MOZ_UNANNOTATED;
136 // Counter of samples that is compared against kTotalSamplesReportLimit.
137 static uint32_t sRecordCnt;
139 // Hit rate statistics for every cache size range.
140 static HitRate sHRStats[kNumOfRanges];
143 class CachePerfStats {
144 public:
145 // perfStatTypes in displayRcwnStats() in toolkit/content/aboutNetworking.js
146 // must match EDataType
147 enum EDataType {
148 IO_OPEN = 0,
149 IO_READ = 1,
150 IO_WRITE = 2,
151 ENTRY_OPEN = 3,
152 LAST = 4
155 static void AddValue(EDataType aType, uint32_t aValue, bool aShortOnly);
156 static uint32_t GetAverage(EDataType aType, bool aFiltered);
157 static uint32_t GetStdDev(EDataType aType, bool aFiltered);
158 static bool IsCacheSlow();
159 static void GetSlowStats(uint32_t* aSlow, uint32_t* aNotSlow);
161 private:
162 // This class computes average and standard deviation, it returns an
163 // arithmetic avg and stddev until total number of values reaches mWeight.
164 // Then it returns modified moving average computed as follows:
166 // avg = (1-a)*avg + a*value
167 // avgsq = (1-a)*avgsq + a*value^2
168 // stddev = sqrt(avgsq - avg^2)
170 // where
171 // avgsq is an average of the square of the values
172 // a = 1 / weight
173 class MMA {
174 public:
175 MMA(uint32_t aTotalWeight, bool aFilter);
177 void AddValue(uint32_t aValue);
178 uint32_t GetAverage();
179 uint32_t GetStdDev();
181 private:
182 uint64_t mSum;
183 uint64_t mSumSq;
184 uint32_t mCnt;
185 uint32_t mWeight;
186 bool mFilter;
189 class PerfData {
190 public:
191 PerfData();
193 void AddValue(uint32_t aValue, bool aShortOnly);
194 uint32_t GetAverage(bool aFiltered);
195 uint32_t GetStdDev(bool aFiltered);
197 private:
198 // Contains filtered data (i.e. times when we think the cache and disk was
199 // not busy) for a longer time.
200 MMA mFilteredAvg;
202 // Contains unfiltered average of few recent values.
203 MMA mShortAvg;
206 static StaticMutex sLock MOZ_UNANNOTATED;
208 static PerfData sData[LAST];
209 static uint32_t sCacheSlowCnt;
210 static uint32_t sCacheNotSlowCnt;
213 void FreeBuffer(void* aBuf);
215 nsresult ParseAlternativeDataInfo(const char* aInfo, int64_t* _offset,
216 nsACString* _type);
218 void BuildAlternativeDataInfo(const char* aInfo, int64_t aOffset,
219 nsACString& _retval);
221 class CacheFileLock final {
222 public:
223 NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CacheFileLock)
224 CacheFileLock() = default;
226 mozilla::Mutex& Lock() { return mLock; }
228 private:
229 ~CacheFileLock() = default;
231 mozilla::Mutex mLock MOZ_UNANNOTATED{"CacheFile.mLock"};
234 } // namespace CacheFileUtils
235 } // namespace net
236 } // namespace mozilla
238 #endif