1 // Copyright (c) 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "net/disk_cache/simple/post_doom_waiter.h"
6 
7 #include "base/bind.h"
8 #include "base/callback.h"
9 #include "net/disk_cache/simple/simple_histogram_macros.h"
10 
11 namespace disk_cache {
12 
SimplePostDoomWaiter()13 SimplePostDoomWaiter::SimplePostDoomWaiter() {}
14 
SimplePostDoomWaiter(base::OnceClosure to_run_post_doom)15 SimplePostDoomWaiter::SimplePostDoomWaiter(base::OnceClosure to_run_post_doom)
16     : time_queued(base::TimeTicks::Now()),
17       run_post_doom(std::move(to_run_post_doom)) {}
18 
19 SimplePostDoomWaiter::SimplePostDoomWaiter(SimplePostDoomWaiter&& other) =
20     default;
21 SimplePostDoomWaiter& SimplePostDoomWaiter::operator=(
22     SimplePostDoomWaiter&& other) = default;
~SimplePostDoomWaiter()23 SimplePostDoomWaiter::~SimplePostDoomWaiter() {}
24 
SimplePostDoomWaiterTable(net::CacheType cache_type)25 SimplePostDoomWaiterTable::SimplePostDoomWaiterTable(net::CacheType cache_type)
26     : cache_type_(cache_type) {}
27 SimplePostDoomWaiterTable::~SimplePostDoomWaiterTable() = default;
28 
OnDoomStart(uint64_t entry_hash)29 void SimplePostDoomWaiterTable::OnDoomStart(uint64_t entry_hash) {
30   DCHECK_EQ(0u, entries_pending_doom_.count(entry_hash));
31   entries_pending_doom_.insert(
32       std::make_pair(entry_hash, std::vector<SimplePostDoomWaiter>()));
33 }
34 
OnDoomComplete(uint64_t entry_hash)35 void SimplePostDoomWaiterTable::OnDoomComplete(uint64_t entry_hash) {
36   DCHECK_EQ(1u, entries_pending_doom_.count(entry_hash));
37   auto it = entries_pending_doom_.find(entry_hash);
38   std::vector<SimplePostDoomWaiter> to_handle_waiters;
39   to_handle_waiters.swap(it->second);
40   entries_pending_doom_.erase(it);
41 
42   SIMPLE_CACHE_UMA(COUNTS_1000, "NumOpsBlockedByPendingDoom", cache_type_,
43                    to_handle_waiters.size());
44 
45   for (SimplePostDoomWaiter& post_doom : to_handle_waiters) {
46     SIMPLE_CACHE_UMA(TIMES, "QueueLatency.PendingDoom", cache_type_,
47                      (base::TimeTicks::Now() - post_doom.time_queued));
48     std::move(post_doom.run_post_doom).Run();
49   }
50 }
51 
Find(uint64_t entry_hash)52 std::vector<SimplePostDoomWaiter>* SimplePostDoomWaiterTable::Find(
53     uint64_t entry_hash) {
54   auto doom_it = entries_pending_doom_.find(entry_hash);
55   if (doom_it != entries_pending_doom_.end())
56     return &doom_it->second;
57   else
58     return nullptr;
59 }
60 
61 }  // namespace disk_cache
62