]> git.proxmox.com Git - ceph.git/blob - ceph/src/os/bluestore/ZonedAllocator.h
update source to Ceph Pacific 16.2.2
[ceph.git] / ceph / src / os / bluestore / ZonedAllocator.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 //
5 // A simple allocator that just hands out space from the next empty zone. This
6 // is temporary, just to get the simplest append-only write workload to work.
7 //
8 // Copyright (C) 2020 Abutalib Aghayev
9 //
10
11 #ifndef CEPH_OS_BLUESTORE_ZONEDALLOCATOR_H
12 #define CEPH_OS_BLUESTORE_ZONEDALLOCATOR_H
13
14 #include <mutex>
15
16 #include "Allocator.h"
17 #include "common/ceph_mutex.h"
18 #include "include/btree_map.h"
19 #include "include/interval_set.h"
20 #include "include/mempool.h"
21 #include "bluestore_types.h"
22 #include "zoned_types.h"
23
24 class ZonedAllocator : public Allocator {
25 CephContext* cct;
26
27 // Currently only one thread at a time calls into ZonedAllocator due to
28 // atomic_alloc_and_submit_lock in BlueStore.cc, but we do locking anyway
29 // because eventually ZONE_APPEND support will land and
30 // atomic_alloc_and_submit_lock will be removed.
31 ceph::mutex lock = ceph::make_mutex("ZonedAllocator::lock");
32
33 std::atomic<int64_t> num_free; ///< total bytes in freelist
34 uint64_t size;
35 uint64_t block_size;
36 uint64_t zone_size;
37 uint64_t starting_zone_num;
38 uint64_t num_zones;
39 std::vector<zone_state_t> zone_states;
40
41 inline uint64_t get_offset(uint64_t zone_num) const {
42 return zone_num * zone_size + get_write_pointer(zone_num);
43 }
44
45 inline uint64_t get_write_pointer(uint64_t zone_num) const {
46 return zone_states[zone_num].get_write_pointer();
47 }
48
49 inline uint64_t get_remaining_space(uint64_t zone_num) const {
50 return zone_size - get_write_pointer(zone_num);
51 }
52
53 inline void advance_write_pointer(uint64_t zone_num, uint64_t want_size) {
54 zone_states[zone_num].increment_write_pointer(want_size);
55 }
56
57 inline bool fits(uint64_t want_size, uint64_t zone_num) const {
58 return want_size <= get_remaining_space(zone_num);
59 }
60
61 public:
62 ZonedAllocator(CephContext* cct, int64_t size, int64_t block_size,
63 const std::string& name);
64 ~ZonedAllocator() override;
65
66 const char *get_type() const override {
67 return "zoned";
68 }
69
70 int64_t allocate(
71 uint64_t want_size, uint64_t alloc_unit, uint64_t max_alloc_size,
72 int64_t hint, PExtentVector *extents) override;
73
74 void release(const interval_set<uint64_t>& release_set) override;
75
76 uint64_t get_free() override;
77
78 void dump() override;
79 void dump(std::function<void(uint64_t offset,
80 uint64_t length)> notify) override;
81
82 void zoned_set_zone_states(std::vector<zone_state_t> &&_zone_states) override;
83 bool zoned_get_zones_to_clean(std::deque<uint64_t> *zones_to_clean) override;
84
85 void init_add_free(uint64_t offset, uint64_t length) override;
86 void init_rm_free(uint64_t offset, uint64_t length) override;
87
88 void shutdown() override;
89 };
90
91 #endif