]> git.proxmox.com Git - ceph.git/blob - ceph/src/osd/ECUtil.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / osd / ECUtil.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2013 Inktank Storage, Inc.
7 *
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef ECUTIL_H
16 #define ECUTIL_H
17
18 #include <map>
19 #include <set>
20
21 #include "include/memory.h"
22 #include "erasure-code/ErasureCodeInterface.h"
23 #include "include/buffer_fwd.h"
24 #include "include/assert.h"
25 #include "include/encoding.h"
26 #include "common/Formatter.h"
27
28 namespace ECUtil {
29
30 const uint64_t CHUNK_ALIGNMENT = 64;
31 const uint64_t CHUNK_INFO = 8;
32 const uint64_t CHUNK_PADDING = 8;
33 const uint64_t CHUNK_OVERHEAD = 16; // INFO + PADDING
34
35 class stripe_info_t {
36 const uint64_t stripe_width;
37 const uint64_t chunk_size;
38 public:
39 stripe_info_t(uint64_t stripe_size, uint64_t stripe_width)
40 : stripe_width(stripe_width),
41 chunk_size(stripe_width / stripe_size) {
42 assert(stripe_width % stripe_size == 0);
43 }
44 bool logical_offset_is_stripe_aligned(uint64_t logical) const {
45 return (logical % stripe_width) == 0;
46 }
47 uint64_t get_stripe_width() const {
48 return stripe_width;
49 }
50 uint64_t get_chunk_size() const {
51 return chunk_size;
52 }
53 uint64_t logical_to_prev_chunk_offset(uint64_t offset) const {
54 return (offset / stripe_width) * chunk_size;
55 }
56 uint64_t logical_to_next_chunk_offset(uint64_t offset) const {
57 return ((offset + stripe_width - 1)/ stripe_width) * chunk_size;
58 }
59 uint64_t logical_to_prev_stripe_offset(uint64_t offset) const {
60 return offset - (offset % stripe_width);
61 }
62 uint64_t logical_to_next_stripe_offset(uint64_t offset) const {
63 return ((offset % stripe_width) ?
64 (offset - (offset % stripe_width) + stripe_width) :
65 offset);
66 }
67 uint64_t aligned_logical_offset_to_chunk_offset(uint64_t offset) const {
68 assert(offset % stripe_width == 0);
69 return (offset / stripe_width) * chunk_size;
70 }
71 uint64_t aligned_chunk_offset_to_logical_offset(uint64_t offset) const {
72 assert(offset % chunk_size == 0);
73 return (offset / chunk_size) * stripe_width;
74 }
75 pair<uint64_t, uint64_t> aligned_offset_len_to_chunk(
76 pair<uint64_t, uint64_t> in) const {
77 return make_pair(
78 aligned_logical_offset_to_chunk_offset(in.first),
79 aligned_logical_offset_to_chunk_offset(in.second));
80 }
81 pair<uint64_t, uint64_t> offset_len_to_stripe_bounds(
82 pair<uint64_t, uint64_t> in) const {
83 uint64_t off = logical_to_prev_stripe_offset(in.first);
84 uint64_t len = logical_to_next_stripe_offset(
85 (in.first - off) + in.second);
86 return make_pair(off, len);
87 }
88 };
89
90 int decode(
91 const stripe_info_t &sinfo,
92 ErasureCodeInterfaceRef &ec_impl,
93 map<int, bufferlist> &to_decode,
94 bufferlist *out);
95
96 int decode(
97 const stripe_info_t &sinfo,
98 ErasureCodeInterfaceRef &ec_impl,
99 map<int, bufferlist> &to_decode,
100 map<int, bufferlist*> &out);
101
102 int encode(
103 const stripe_info_t &sinfo,
104 ErasureCodeInterfaceRef &ec_impl,
105 bufferlist &in,
106 const set<int> &want,
107 map<int, bufferlist> *out);
108
109 class HashInfo {
110 uint64_t total_chunk_size = 0;
111 vector<uint32_t> cumulative_shard_hashes;
112
113 // purely ephemeral, represents the size once all in-flight ops commit
114 uint64_t projected_total_chunk_size = 0;
115 public:
116 HashInfo() {}
117 explicit HashInfo(unsigned num_chunks) :
118 cumulative_shard_hashes(num_chunks, -1) {}
119 void append(uint64_t old_size, map<int, bufferlist> &to_append);
120 void clear() {
121 total_chunk_size = 0;
122 cumulative_shard_hashes = vector<uint32_t>(
123 cumulative_shard_hashes.size(),
124 -1);
125 }
126 void encode(bufferlist &bl) const;
127 void decode(bufferlist::iterator &bl);
128 void dump(Formatter *f) const;
129 static void generate_test_instances(list<HashInfo*>& o);
130 uint32_t get_chunk_hash(int shard) const {
131 assert((unsigned)shard < cumulative_shard_hashes.size());
132 return cumulative_shard_hashes[shard];
133 }
134 uint64_t get_total_chunk_size() const {
135 return total_chunk_size;
136 }
137 uint64_t get_projected_total_chunk_size() const {
138 return projected_total_chunk_size;
139 }
140 uint64_t get_total_logical_size(const stripe_info_t &sinfo) const {
141 return get_total_chunk_size() *
142 (sinfo.get_stripe_width()/sinfo.get_chunk_size());
143 }
144 uint64_t get_projected_total_logical_size(const stripe_info_t &sinfo) const {
145 return get_projected_total_chunk_size() *
146 (sinfo.get_stripe_width()/sinfo.get_chunk_size());
147 }
148 void set_projected_total_logical_size(
149 const stripe_info_t &sinfo,
150 uint64_t logical_size) {
151 assert(sinfo.logical_offset_is_stripe_aligned(logical_size));
152 projected_total_chunk_size = sinfo.aligned_logical_offset_to_chunk_offset(
153 logical_size);
154 }
155 void set_total_chunk_size_clear_hash(uint64_t new_chunk_size) {
156 cumulative_shard_hashes.clear();
157 total_chunk_size = new_chunk_size;
158 }
159 bool has_chunk_hash() const {
160 return !cumulative_shard_hashes.empty();
161 }
162 void update_to(const HashInfo &rhs) {
163 auto ptcs = projected_total_chunk_size;
164 *this = rhs;
165 projected_total_chunk_size = ptcs;
166 }
167 };
168
169 typedef ceph::shared_ptr<HashInfo> HashInfoRef;
170
171 bool is_hinfo_key_string(const string &key);
172 const string &get_hinfo_key();
173
174 }
175 WRITE_CLASS_ENCODER(ECUtil::HashInfo)
176 #endif