1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
|
/* vim:set ts=4 sw=2 sts=2 et cin: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "HostRecordQueue.h"
#include "mozilla/glean/NetwerkDnsMetrics.h"
#include "nsQueryObject.h"
namespace mozilla {
namespace net {
void HostRecordQueue::InsertRecord(nsHostRecord* aRec,
nsIDNSService::DNSFlags aFlags,
const MutexAutoLock& aProofOfLock) {
if (aRec->isInList()) {
MOZ_DIAGNOSTIC_ASSERT(!mEvictionQ.contains(aRec),
"Already in eviction queue");
MOZ_DIAGNOSTIC_ASSERT(!mHighQ.contains(aRec), "Already in high queue");
MOZ_DIAGNOSTIC_ASSERT(!mMediumQ.contains(aRec), "Already in med queue");
MOZ_DIAGNOSTIC_ASSERT(!mLowQ.contains(aRec), "Already in low queue");
MOZ_DIAGNOSTIC_CRASH("Already on some other queue?");
}
switch (AddrHostRecord::GetPriority(aFlags)) {
case AddrHostRecord::DNS_PRIORITY_HIGH:
mHighQ.insertBack(aRec);
break;
case AddrHostRecord::DNS_PRIORITY_MEDIUM:
mMediumQ.insertBack(aRec);
break;
case AddrHostRecord::DNS_PRIORITY_LOW:
mLowQ.insertBack(aRec);
break;
}
mPendingCount++;
}
void HostRecordQueue::AddToEvictionQ(
nsHostRecord* aRec, uint32_t aMaxCacheEntries,
nsRefPtrHashtable<nsGenericHashKey<nsHostKey>, nsHostRecord>& aDB,
const MutexAutoLock& aProofOfLock) {
if (aRec->isInList()) {
bool inEvictionQ = mEvictionQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inEvictionQ, "Already in eviction queue");
bool inHighQ = mHighQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inHighQ, "Already in high queue");
bool inMediumQ = mMediumQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inMediumQ, "Already in med queue");
bool inLowQ = mLowQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inLowQ, "Already in low queue");
MOZ_DIAGNOSTIC_CRASH("Already on some other queue?");
// Bug 1678117 - it's not clear why this can happen, but let's fix it
// for release users.
aRec->remove();
if (inEvictionQ) {
MOZ_DIAGNOSTIC_ASSERT(mEvictionQSize > 0);
mEvictionQSize--;
} else if (inHighQ || inMediumQ || inLowQ) {
MOZ_DIAGNOSTIC_ASSERT(mPendingCount > 0);
mPendingCount--;
}
}
mEvictionQ.insertBack(aRec);
if (mEvictionQSize < aMaxCacheEntries) {
mEvictionQSize++;
} else {
// remove first element on mEvictionQ
RefPtr<nsHostRecord> head = mEvictionQ.popFirst();
aDB.Remove(*static_cast<nsHostKey*>(head.get()));
if (!head->negative) {
// record the age of the entry upon eviction.
TimeDuration age = TimeStamp::NowLoRes() - head->mValidStart;
if (aRec->IsAddrRecord()) {
glean::dns::cleanup_age.AccumulateRawDuration(age);
} else {
glean::dns::by_type_cleanup_age.AccumulateRawDuration(age);
}
if (head->CheckExpiration(TimeStamp::Now()) !=
nsHostRecord::EXP_EXPIRED) {
if (aRec->IsAddrRecord()) {
glean::dns::premature_eviction.AccumulateRawDuration(age);
} else {
glean::dns::by_type_premature_eviction.AccumulateRawDuration(age);
}
}
}
}
}
void HostRecordQueue::MoveToEvictionQueueTail(
nsHostRecord* aRec, const MutexAutoLock& aProofOfLock) {
bool inEvictionQ = mEvictionQ.contains(aRec);
if (!inEvictionQ) {
// Note: this function can be called when the record isn’t in the
// mEvictionQ. For example, if we immediately start a TTL lookup (see
// nsHostResolver::CompleteLookupLocked), the record may not be in
// mEvictionQ.
return;
}
aRec->remove();
mEvictionQ.insertBack(aRec);
}
void HostRecordQueue::MaybeRenewHostRecord(nsHostRecord* aRec,
const MutexAutoLock& aProofOfLock) {
if (!aRec->isInList()) {
return;
}
bool inEvictionQ = mEvictionQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(inEvictionQ, "Should be in eviction queue");
bool inHighQ = mHighQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inHighQ, "Already in high queue");
bool inMediumQ = mMediumQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inMediumQ, "Already in med queue");
bool inLowQ = mLowQ.contains(aRec);
MOZ_DIAGNOSTIC_ASSERT(!inLowQ, "Already in low queue");
// we're already on the eviction queue. This is a renewal
aRec->remove();
if (inEvictionQ) {
MOZ_DIAGNOSTIC_ASSERT(mEvictionQSize > 0);
mEvictionQSize--;
} else if (inHighQ || inMediumQ || inLowQ) {
MOZ_DIAGNOSTIC_ASSERT(mPendingCount > 0);
mPendingCount--;
}
}
void HostRecordQueue::FlushEvictionQ(
nsRefPtrHashtable<nsGenericHashKey<nsHostKey>, nsHostRecord>& aDB,
const MutexAutoLock& aProofOfLock) {
mEvictionQSize = 0;
// Clear the evictionQ and remove all its corresponding entries from
// the cache first
if (!mEvictionQ.isEmpty()) {
for (const RefPtr<nsHostRecord>& rec : mEvictionQ) {
rec->Cancel();
aDB.Remove(*static_cast<nsHostKey*>(rec));
}
mEvictionQ.clear();
}
}
void HostRecordQueue::MaybeRemoveFromQ(nsHostRecord* aRec,
const MutexAutoLock& aProofOfLock) {
if (!aRec->isInList()) {
return;
}
if (mHighQ.contains(aRec) || mMediumQ.contains(aRec) ||
mLowQ.contains(aRec)) {
mPendingCount--;
} else if (mEvictionQ.contains(aRec)) {
mEvictionQSize--;
} else {
MOZ_ASSERT(false, "record is in other queue");
}
aRec->remove();
}
void HostRecordQueue::MoveToAnotherPendingQ(nsHostRecord* aRec,
nsIDNSService::DNSFlags aFlags,
const MutexAutoLock& aProofOfLock) {
if (!(mHighQ.contains(aRec) || mMediumQ.contains(aRec) ||
mLowQ.contains(aRec))) {
MOZ_ASSERT(false, "record is not in the pending queue");
return;
}
aRec->remove();
// We just removed from pending queue. Insert record will
// increment this value again.
mPendingCount--;
InsertRecord(aRec, aFlags, aProofOfLock);
}
already_AddRefed<nsHostRecord> HostRecordQueue::Dequeue(
bool aHighQOnly, const MutexAutoLock& aProofOfLock) {
RefPtr<nsHostRecord> rec;
if (!mHighQ.isEmpty()) {
rec = mHighQ.popFirst();
} else if (!mMediumQ.isEmpty() && !aHighQOnly) {
rec = mMediumQ.popFirst();
} else if (!mLowQ.isEmpty() && !aHighQOnly) {
rec = mLowQ.popFirst();
}
if (rec) {
mPendingCount--;
}
return rec.forget();
}
void HostRecordQueue::ClearAll(
const std::function<void(nsHostRecord*)>& aCallback,
const MutexAutoLock& aProofOfLock) {
mPendingCount = 0;
auto clearPendingQ = [&](LinkedList<RefPtr<nsHostRecord>>& aPendingQ) {
if (aPendingQ.isEmpty()) {
return;
}
// loop through pending queue, erroring out pending lookups.
for (const RefPtr<nsHostRecord>& rec : aPendingQ) {
rec->Cancel();
aCallback(rec);
}
aPendingQ.clear();
};
clearPendingQ(mHighQ);
clearPendingQ(mMediumQ);
clearPendingQ(mLowQ);
mEvictionQSize = 0;
if (!mEvictionQ.isEmpty()) {
for (const RefPtr<nsHostRecord>& rec : mEvictionQ) {
rec->Cancel();
}
}
mEvictionQ.clear();
}
} // namespace net
} // namespace mozilla
|