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