summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/disk_data_allocator.cc
blob: faffd17396e0b60f9e21f51f6c6ab798dd589e07 (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
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
// Copyright 2020 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 "third_party/blink/renderer/platform/disk_data_allocator.h"

#include <algorithm>
#include <utility>

#include "base/logging.h"
#include "base/threading/thread_restrictions.h"
#include "third_party/blink/renderer/platform/wtf/std_lib_extras.h"
#include "third_party/blink/renderer/platform/wtf/wtf.h"

namespace blink {

DiskDataAllocator::DiskDataAllocator()
    : free_chunks_size_(0), file_tail_(0), may_write_(false) {}

DiskDataAllocator::~DiskDataAllocator() = default;

bool DiskDataAllocator::may_write() {
  MutexLocker locker(mutex_);
  return may_write_;
}

void DiskDataAllocator::set_may_write_for_testing(bool may_write) {
  MutexLocker locker(mutex_);
  may_write_ = may_write;
}

DiskDataAllocator::Metadata DiskDataAllocator::FindChunk(size_t size) {
  // Try to reuse some space. Policy:
  // 1. Exact fit
  // 2. Worst fit
  Metadata chosen_chunk{-1, 0};

  size_t worst_fit_size = 0;
  for (const auto& chunk : free_chunks_) {
    size_t chunk_size = chunk.second;
    if (size == chunk_size) {
      chosen_chunk = {chunk.first, chunk.second};
      break;
    } else if (chunk_size > size && chunk_size > worst_fit_size) {
      chosen_chunk = {chunk.first, chunk.second};
      worst_fit_size = chunk.second;
    }
  }

  if (chosen_chunk.start_offset() != -1) {
    free_chunks_size_ -= size;
    free_chunks_.erase(chosen_chunk.start_offset());
    if (chosen_chunk.size() > size) {
      std::pair<int64_t, size_t> remainder_chunk = {
          chosen_chunk.start_offset() + size, chosen_chunk.size() - size};
      auto result = free_chunks_.insert(remainder_chunk);
      DCHECK(result.second);
      chosen_chunk.size_ = size;
    }
  } else {
    chosen_chunk = {file_tail_, size};
    file_tail_ += size;
  }

  return chosen_chunk;
}

void DiskDataAllocator::ReleaseChunk(const Metadata& metadata) {
  Metadata chunk = metadata;
  DCHECK(free_chunks_.find(chunk.start_offset()) == free_chunks_.end());

  auto lower_bound = free_chunks_.lower_bound(chunk.start_offset());
  DCHECK(free_chunks_.upper_bound(chunk.start_offset()) ==
         free_chunks_.lower_bound(chunk.start_offset()));
  if (lower_bound != free_chunks_.begin()) {
    // There is a chunk left.
    auto left = --lower_bound;
    // Can merge with the left chunk.
    int64_t left_chunk_end = left->first + left->second;
    DCHECK_LE(left_chunk_end, chunk.start_offset());
    if (left_chunk_end == chunk.start_offset()) {
      chunk = {left->first, left->second + chunk.size()};
      free_chunks_size_ -= left->second;
      free_chunks_.erase(left);
    }
  }

  auto right = free_chunks_.upper_bound(chunk.start_offset());
  if (right != free_chunks_.end()) {
    DCHECK_NE(right->first, chunk.start_offset());
    int64_t chunk_end = chunk.start_offset() + chunk.size();
    DCHECK_LE(chunk_end, right->first);
    if (right->first == chunk_end) {
      chunk = {chunk.start_offset(), chunk.size() + right->second};
      free_chunks_size_ -= right->second;
      free_chunks_.erase(right);
    }
  }

  auto result = free_chunks_.insert({chunk.start_offset(), chunk.size()});
  DCHECK(result.second);
  free_chunks_size_ += chunk.size();
}

std::unique_ptr<DiskDataAllocator::Metadata> DiskDataAllocator::Write(
    const void* data,
    size_t size) {
  Metadata chosen_chunk = {0, 0};

  {
    MutexLocker locker(mutex_);
    if (!may_write_)
      return nullptr;

    chosen_chunk = FindChunk(size);
  }  // Don't hold the lock during the actual Write().

  int size_int = static_cast<int>(size);
  const char* data_char = reinterpret_cast<const char*>(data);
  int written = DoWrite(chosen_chunk.start_offset(), data_char, size_int);

  MutexLocker locker(mutex_);
  if (size_int != written) {
    // Assume that the error is not transient. This can happen if the disk is
    // full for instance, in which case it is likely better not to try writing
    // later.
    may_write_ = false;
    return nullptr;
  }

#if DCHECK_IS_ON()
  allocated_chunks_.insert({chosen_chunk.start_offset(), chosen_chunk.size()});
#endif

  return std::unique_ptr<Metadata>(
      new Metadata(chosen_chunk.start_offset(), chosen_chunk.size()));
}

void DiskDataAllocator::Read(const Metadata& metadata, void* data) {
  DCHECK(IsMainThread());

  // Doesn't need locking as files support concurrent access, and we don't
  // update metadata.
  char* data_char = reinterpret_cast<char*>(data);
  DoRead(metadata.start_offset(), data_char, metadata.size());

#if DCHECK_IS_ON()
  {
    MutexLocker locker(mutex_);
    auto it = allocated_chunks_.find(metadata.start_offset());
    DCHECK(it != allocated_chunks_.end());
    DCHECK_EQ(metadata.size(), it->second);
  }
#endif
}

void DiskDataAllocator::Discard(std::unique_ptr<Metadata> metadata) {
  MutexLocker locker(mutex_);
  DCHECK(may_write_ || file_.IsValid());

#if DCHECK_IS_ON()
  auto it = allocated_chunks_.find(metadata->start_offset());
  DCHECK(it != allocated_chunks_.end());
  DCHECK_EQ(metadata->size(), it->second);
  allocated_chunks_.erase(it);
#endif

  ReleaseChunk(*metadata);
}

int DiskDataAllocator::DoWrite(int64_t offset, const char* data, int size) {
  int rv = file_.Write(offset, data, size);

  // No PCHECK(), since a file writing error is recoverable.
  if (rv != size) {
    LOG(ERROR) << "DISK: Cannot write to disk. written = " << rv << " "
               << base::File::ErrorToString(base::File::GetLastFileError());
  }
  return rv;
}

void DiskDataAllocator::DoRead(int64_t offset, char* data, int size) {
  // This happens on the main thread, which is typically not allowed. This is
  // fine as this is expected to happen rarely, and only be slow with memory
  // pressure, in which case writing to/reading from disk is better than
  // swapping out random parts of the memory. See crbug.com/1029320 for details.
  base::ScopedAllowBlocking allow_blocking;
  int rv = file_.Read(offset, data, size);
  // Can only crash, since we cannot continue without the data.
  PCHECK(rv == size) << "Likely file corruption.";
}

void DiskDataAllocator::ProvideTemporaryFile(base::File file) {
  MutexLocker locker(mutex_);
  DCHECK(IsMainThread());
  DCHECK(!file_.IsValid());
  DCHECK(!may_write_);

  file_ = std::move(file);
  may_write_ = file_.IsValid();
}

// static
DiskDataAllocator& DiskDataAllocator::Instance() {
  DEFINE_STATIC_LOCAL(DiskDataAllocator, instance, ());
  return instance;
}

// static
void DiskDataAllocator::Bind(
    mojo::PendingReceiver<mojom::blink::DiskAllocator> receiver) {
  DCHECK(!Instance().receiver_.is_bound());
  Instance().receiver_.Bind(std::move(receiver));
}

}  // namespace blink