cache_entry.cpp 7.04 KB
Newer Older
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "cache_entry.hh"
#include <mutex>

#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#define FMT_HEADER_ONLY
#include "spdlog/spdlog.h"

#include "gpu_cache.hh"

namespace kvc2 {

bool ConcurrentControlUnit::can_desert() {
  if (ref_count.load() == 0 && dirty.load() == false) {
    tc.reset();
    return true;
  } else {
    return false;
  }
}
void ConcurrentControlUnit::debug() {
  SPDLOG_DEBUG("ref count {}, dirty {}, {}", ref_count.load(), dirty.load(), tc.debug());
}

CacheBlockEntry::~CacheBlockEntry() {
  if (data != nullptr && manager && manager->pool) {
    SPDLOG_WARN("Free {} when destruct", data);
    free_on_cpu();
  }
}

bool CacheBlockEntry::alloc_on_cpu() {
  assert(data == nullptr);
  data = manager->pool->alloc(size);
  if (data == nullptr) {
    manager->evict_for_cpu_cache();
    data = manager->pool->alloc(size);
    if (data == nullptr) {
      SPDLOG_ERROR("Not enough memory for Block Cache");
      return false;
    }
  }
  return true;
}

void CacheBlockEntry::free_on_cpu() {
  manager->pool->free(data, size);
  data = nullptr;
}

bool CacheBlockEntry::alloc_on_cpu_no_lock() {
  if (data == nullptr) {
    if (alloc_on_cpu() == false) {
      return false;
    }
  }
  return true;
}

bool CacheBlockEntry::inc_ref_or_alloc_on_cpu() {
  std::lock_guard<CacheBlockEntry::MutexT> lg(lock);
  if (data == nullptr) {
    if (alloc_on_cpu()) {
      cpu_cc.ref_count.fetch_add(1);
      return true;
    } else {
      return false;
    }
  } else {
    cpu_cc.ref_count.fetch_add(1);
    return true;
  }
}

std::unique_lock<CacheBlockEntry::MutexT> CacheBlockEntry::try_lock() {
  return std::unique_lock<CacheBlockEntry::MutexT>(lock, std::try_to_lock);
}

std::lock_guard<CacheBlockEntry::MutexT> CacheBlockEntry::lock_guard() {
  return std::lock_guard<CacheBlockEntry::MutexT>(lock);
}

void CacheBlockEntry::debug() {
  SPDLOG_DEBUG(
      "CacheBlockEntry: disk[{:4},{:7}], with key {}, hash {:016x}, data: {}, ref_count: {}, size: {}, cpu tc: {}, "
      "in page cache: {}, gpu ref count:{}, gpu tc: {}",
      layer, idx, with_key, hash, data, cpu_cc.ref_count.load(), size, cpu_cc.tc.debug(), manager != nullptr,
      gpu_cc.ref_count.load(), gpu_cc.tc.debug());
}

CacheBlockEntryCollector::CacheBlockEntryCollector(std::function<void(CacheBlockEntry*)> exit_fn) : exit_fn(exit_fn) {}

CacheBlockEntryCollector::~CacheBlockEntryCollector() {
  // SPDLOG_DEBUG("Collector Destruct");
  for (auto& e : entries) {
    exit_fn(e);
  }
}

void CacheBlockEntry::io_with(async_store::IODealer* dealer, IO_Helper<CacheBlockEntry>& io_helper,
                              async_store::ArrayStore* store, size_t layer, size_t index, IOOption option) {
  bool write;

  auto& batch_promise = io_helper.batch_promise;

  switch (option) {
    case IO_Read: {
      write = false;
      if (io_helper.absorb_tc(this, cpu_cc.tc)) {
        // need read
      } else {
        return;
      }
      break;
    }
    case IO_ForceRead: {
      // Not change
      write = false;
      break;
    }
    case IO_ForceWrite: {
      // Not change
      write = true;
      break;
    }
    case IO_Write: {
      write = true;
      break;
    }
    default: {
      assert(0);
    }
  }
  io_helper.new_task();
  this->layer = layer;
  this->idx = index;

  auto req = std::make_shared<async_store::IORequest>();
  req->store = store;
  req->data = data;
  req->index = index;
  req->write = write;
  req->need_promise = true;
  req->promise = &batch_promise;

  SPDLOG_TRACE("Submitting {}", async_store::request_to_string(req.get()));
  dealer->enqueue(std::move(req));
}

CacheEntryManager::CacheEntryManager(CacheEntryManagerConfig config) : config(config) {}

void CacheEntryManager::evict_for_cpu_cache() {
  size_t count = 0;
  evict(
      [&count](const BlockPtr& block) {
        // here we assume each with gpu must resides on cpu
        if (block->data != nullptr && block->cpu_cc.can_desert() &&
            block->gpu_cc.can_desert() /*For now If A Cache Entry Block is on GPU, it must on cpu. */) {
          block->free_on_cpu();
          count += 1;
          return true;
        } else {
          return false;
        }
      },
      [&count, this]() {
        return false;
        // return count == this->config.evict_count;
      });
}

void CacheEntryManager::insert(BlockPtr entry) {
  assert(entry->with_key);
  assert(key_entry_map.count(entry->hash) == 0);
  usage_list.push_front(entry);
  key_entry_map[entry->hash] = usage_list.begin();
}

CacheEntryManager::BlockPtr CacheEntryManager::access(const Key& key) {
  auto it = key_entry_map.at(key);
  auto entry = *it;
  usage_list.erase(it);
  usage_list.push_front(entry);
  key_entry_map[key] = usage_list.begin();
  return entry;
}

// void CacheEntryManager::remove(const Key& key) {
//   auto it = key_entry_map[key];
//   usage_list.erase(it);
//   key_entry_map.erase(key);
// }

void CacheEntryManager::evict(std::function<bool(const BlockPtr&)> filter, std::function<bool()> stop_condition) {
  auto evict_count = 0;
  auto inspect_count = 0;

  std::lock_guard<std::mutex> lg(lock);
  for (auto it = usage_list.rbegin(); it != usage_list.rend();) {
    inspect_count += 1;
    // SPDLOG_DEBUG("Map Size {}, List Size {}, Evicted {} blocks, Inspected {}, {}", key_entry_map.size(),
    //              usage_list.size(), evict_count, inspect_count, pool->debug());
    // (*it)->debug();
    if (stop_condition())
      break;
    auto entry_ul = (*it)->try_lock();
    if (entry_ul.owns_lock() == false) {
      ++it;  // Ensure iterator advances when locking fails
      continue;
    }
    if (filter(*it)) {
      // SPDLOG_DEBUG("Evicting {}", fmt::ptr(it->get()));
      evict_count++;
      if ((*it)->with_key)
        key_entry_map.erase((*it)->hash);
      it = decltype(it)(usage_list.erase(std::next(it).base()));  // Use base() to adjust for reverse iterator
    } else {
      ++it;  // Ensure iterator advances when filter fails
    }
  }

  if (evict_count > 0) {
    SPDLOG_DEBUG("Map Size {}, List Size {}, Evicted {} blocks, Inspected {}, {}", key_entry_map.size(),
                 usage_list.size(), evict_count, inspect_count, pool->debug());
  }
}

CacheEntryManager::BlockPtr CacheEntryManager::get(bool& is_new, size_t size, std::optional<Key> key) {
  std::unique_lock<std::mutex> ul(lock);
  if (key.has_value()) {
    if (key_entry_map.count(key.value())) {
      is_new = false;
      return access(key.value());
    } else {
      auto entry = std::make_shared<CacheBlockEntry>();
      entry->with_key = true;
      entry->hash = key.value();
      entry->size = size;
      entry->manager = this;
      insert(entry);
      is_new = true;
      return entry;
    }
  } else {
    auto entry = std::make_shared<CacheBlockEntry>();
    entry->with_key = false;
    entry->size = size;
    entry->manager = this;
    is_new = true;
    return entry;
  }
}

void CacheEntryManager::debug() {
  fmt::print("Cache Manager: {} entries\n", key_entry_map.size());
  pool->debug();
  fmt::print("Layer 0 Entries in Order\n", key_entry_map.size());
  for (auto& it : usage_list) {
    if (it->layer == 0)
      it->debug();
  }
}

};  // namespace kvc2