Coverage Report

Created: 2018-09-25 14:53

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