summaryrefslogtreecommitdiffstats
path: root/net/disk_cache/eviction.cc
blob: 11ff969f3357ecfad90d05f382b2578fce445221 (plain)
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
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/disk_cache/eviction.h"

#include "base/logging.h"
#include "base/message_loop.h"
#include "base/string_util.h"
#include "base/time.h"
#include "net/disk_cache/backend_impl.h"
#include "net/disk_cache/entry_impl.h"
#include "net/disk_cache/trace.h"

using base::Time;

namespace {

const int kCleanUpMargin = 1024 * 1024;

int LowWaterAdjust(int high_water) {
  if (high_water < kCleanUpMargin)
    return 0;

  return high_water - kCleanUpMargin;
}

}  // namespace

namespace disk_cache {

void Eviction::Init(BackendImpl* backend) {
  // We grab a bunch of info from the backend to make the code a little cleaner
  // when we're actually doing work.
  backend_ = backend;
  rankings_ = &backend->rankings_;
  header_ = &backend_->data_->header;
  max_size_ = LowWaterAdjust(backend_->max_size_);
}

void Eviction::TrimCache(bool empty) {
  Trace("*** Trim Cache ***");
  if (backend_->disabled_)
    return;

  Time start = Time::Now();
  Rankings::ScopedRankingsBlock node(rankings_);
  Rankings::ScopedRankingsBlock next(rankings_,
      rankings_->GetPrev(node.get(), Rankings::NO_USE));
  DCHECK(next.get());
  int target_size = empty ? 0 : max_size_;
  int deleted = 0;
  while (header_->num_bytes > target_size && next.get()) {
    node.reset(next.release());
    next.reset(rankings_->GetPrev(node.get(), Rankings::NO_USE));
    if (!node->Data()->pointer || empty) {
      // This entry is not being used by anybody.
      EntryImpl* entry;
      bool dirty;
      if (backend_->NewEntry(Addr(node->Data()->contents), &entry, &dirty)) {
        Trace("NewEntry failed on Trim 0x%x", node->address().value());
        continue;
      }

      if (node->Data()->pointer) {
        entry = EntryImpl::Update(entry);
      }
      ReportTrimTimes(entry);
      entry->Doom();
      entry->Release();
      if (!empty)
        backend_->OnEvent(Stats::TRIM_ENTRY);
      if (++deleted == 4 && !empty) {
#if defined(OS_WIN)
        MessageLoop::current()->PostTask(FROM_HERE,
            factory_.NewRunnableMethod(&Eviction::TrimCache, false));
        break;
#endif
      }
    }
  }

  UMA_HISTOGRAM_TIMES(L"DiskCache.TotalTrimTime", Time::Now() - start);
  Trace("*** Trim Cache end ***");
  return;
}

void Eviction::UpdateRank(EntryImpl* entry, bool modified) {
  rankings_->UpdateRank(entry->rankings(), modified, GetListForEntry(entry));
}

void Eviction::OnOpenEntry(EntryImpl* entry) {
}

void Eviction::OnCreateEntry(EntryImpl* entry) {
  rankings_->Insert(entry->rankings(), true, GetListForEntry(entry));
}

void Eviction::OnDoomEntry(EntryImpl* entry) {
  rankings_->Remove(entry->rankings(), GetListForEntry(entry));
}

void Eviction::OnDestroyEntry(EntryImpl* entry) {
}

void Eviction::ReportTrimTimes(EntryImpl* entry) {
  static bool first_time = true;
  if (first_time) {
    first_time = false;
    std::wstring name(StringPrintf(L"DiskCache.TrimAge_%d",
                                   header_->experiment));
    static Histogram counter(name.c_str(), 1, 10000, 50);
    counter.SetFlags(kUmaTargetedHistogramFlag);
    counter.Add((Time::Now() - entry->GetLastUsed()).InHours());
  }
}

Rankings::List Eviction::GetListForEntry(EntryImpl* entry) {
  return Rankings::NO_USE;
}

}  // namespace disk_cache