summaryrefslogtreecommitdiffstats
path: root/src/tools/immutable_object_cache/SimplePolicy.h
blob: 671cbd518991003180f55d0b5fe882c4396b37d6 (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_CACHE_SIMPLE_POLICY_H
#define CEPH_CACHE_SIMPLE_POLICY_H

#include "common/ceph_context.h"
#include "common/ceph_mutex.h"
#include "include/lru.h"
#include "Policy.h"

#include <unordered_map>
#include <string>

namespace ceph {
namespace immutable_obj_cache {

class SimplePolicy : public Policy {
 public:
  SimplePolicy(CephContext *cct, uint64_t block_num, uint64_t max_inflight,
               double watermark);
  ~SimplePolicy();

  cache_status_t lookup_object(std::string file_name);
  cache_status_t get_status(std::string file_name);

  void update_status(std::string file_name,
                     cache_status_t new_status,
                     uint64_t size = 0);

  int evict_entry(std::string file_name);

  void get_evict_list(std::list<std::string>* obj_list);

  uint64_t get_free_size();
  uint64_t get_promoting_entry_num();
  uint64_t get_promoted_entry_num();
  std::string get_evict_entry();

 private:
  cache_status_t alloc_entry(std::string file_name);

  class Entry : public LRUObject {
   public:
    cache_status_t status;
    Entry() : status(OBJ_CACHE_NONE) {}
    std::string file_name;
    uint64_t size;
  };

  CephContext* cct;
  double m_watermark;
  uint64_t m_max_inflight_ops;
  uint64_t m_max_cache_size;
  std::atomic<uint64_t> inflight_ops = 0;

  std::unordered_map<std::string, Entry*> m_cache_map;
  ceph::shared_mutex m_cache_map_lock =
    ceph::make_shared_mutex("rbd::cache::SimplePolicy::m_cache_map_lock");

  std::atomic<uint64_t> m_cache_size;

  LRU m_promoted_lru;
};

}  // namespace immutable_obj_cache
}  // namespace ceph
#endif  // CEPH_CACHE_SIMPLE_POLICY_H