Motr  M0
fl.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2014-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_BE
23 #include "lib/trace.h"
24 
25 #include "be/fl.h"
26 
27 #include "lib/assert.h" /* M0_PRE */
28 #include "lib/misc.h" /* ARRAY_SIZE */
29 #include "lib/arith.h" /* min_type */
30 
31 #include "motr/magic.h" /* M0_BE_ALLOC_FREE_LINK_MAGIC */
32 
33 #include "be/tx_credit.h" /* m0_be_tx_credit */
34 #include "be/alloc_internal.h" /* be_alloc_chunk */
35 
93 M0_BE_LIST_DESCR_DEFINE(fl, "m0_be_fl", static, struct be_alloc_chunk,
96 M0_BE_LIST_DEFINE(fl, static, struct be_alloc_chunk);
97 
98 static struct m0_be_list *be_fl_list(struct m0_be_fl *fl, unsigned long index)
99 {
101 
102  return &fl->bfl_free[index].bfs_list;
103 };
104 
105 static bool be_fl_list_is_empty(struct m0_be_fl *fl, unsigned long index)
106 {
107  return fl_be_list_is_empty(be_fl_list(fl, index));
108 }
109 
110 M0_INTERNAL void m0_be_fl_create(struct m0_be_fl *fl,
111  struct m0_be_tx *tx,
112  struct m0_be_seg *seg)
113 {
114  int i;
115 
116  for (i = 0; i < ARRAY_SIZE(fl->bfl_free); ++i)
117  fl_be_list_create(be_fl_list(fl, i), tx);
118 }
119 
120 M0_INTERNAL void m0_be_fl_destroy(struct m0_be_fl *fl, struct m0_be_tx *tx)
121 {
122  int i;
123 
124  for (i = 0; i < ARRAY_SIZE(fl->bfl_free); ++i)
125  fl_be_list_destroy(be_fl_list(fl, i), tx);
126 }
127 
128 static unsigned long be_fl_index_round_up(struct m0_be_fl *fl,
130 {
131  return min_type(unsigned long, M0_BE_FL_NR,
133 }
134 
135 static unsigned long
137  const struct be_alloc_chunk *chunk)
138 {
139  return min_type(unsigned long, M0_BE_FL_NR,
140  chunk->bac_size / M0_BE_FL_STEP);
141 }
142 
143 M0_INTERNAL bool m0_be_fl__invariant(struct m0_be_fl *fl)
144 {
145  return m0_forall(i, ARRAY_SIZE(fl->bfl_free),
146  m0_be_list_forall(fl, chunk, be_fl_list(fl, i),
147  _0C(be_fl_index_round_down_chunk(fl, chunk) ==
148  i)));
149 }
150 
151 M0_INTERNAL void m0_be_fl_add(struct m0_be_fl *fl,
152  struct m0_be_tx *tx,
153  struct be_alloc_chunk *chunk)
154 {
155  unsigned long index = be_fl_index_round_down_chunk(fl, chunk);
156 
158 
159  fl_be_tlink_create(chunk, tx);
160  fl_be_list_add(be_fl_list(fl, index), tx, chunk);
161 
163 }
164 
165 M0_INTERNAL void m0_be_fl_del(struct m0_be_fl *fl,
166  struct m0_be_tx *tx,
167  struct be_alloc_chunk *chunk)
168 {
169  unsigned long index = be_fl_index_round_down_chunk(fl, chunk);
170 
172 
173  fl_be_list_del(be_fl_list(fl, index), tx, chunk);
174  fl_be_tlink_destroy(chunk, tx);
176 }
177 
178 M0_INTERNAL struct be_alloc_chunk *m0_be_fl_pick(struct m0_be_fl *fl,
180 {
181  struct be_alloc_chunk *chunk;
182  struct be_alloc_chunk *iter;
183  unsigned long index;
184  struct m0_be_list *flist;
185  int i = 0;
186 
188 
189  for (index = be_fl_index_round_up(fl, size);
190  index < ARRAY_SIZE(fl->bfl_free); ++index) {
191  if (!be_fl_list_is_empty(fl, index))
192  break;
193  }
194 
195  flist = index < ARRAY_SIZE(fl->bfl_free) ? be_fl_list(fl, index) : NULL;
196  chunk = flist == NULL ? NULL : fl_be_list_head(flist);
197  if (index == M0_BE_FL_NR && chunk != NULL) {
198  chunk = NULL;
199  m0_be_list_for(fl, flist, iter) {
200  if (iter->bac_size > size &&
201  ergo(chunk != NULL,
202  chunk->bac_size > iter->bac_size)) {
203  chunk = iter;
204  }
205  ++i;
206  if (i >= M0_BE_FL_PICK_SCAN_LIMIT && chunk != NULL)
207  break;
209  }
210 
211  M0_LOG(M0_DEBUG, "chunk=%p size=%" PRIu64 " bac_size=%" PRIu64 " "
212  "index=%lu iterations=%u",
213  chunk, size, chunk ? chunk->bac_size : 0, index, i);
214  M0_POST(ergo(chunk != NULL, chunk->bac_size >= size));
215  return chunk;
216 }
217 
218 M0_INTERNAL void m0_be_fl_credit(struct m0_be_fl *fl,
219  enum m0_be_fl_op fl_op,
220  struct m0_be_tx_credit *accum)
221 {
222  size_t list_nr = ARRAY_SIZE(fl->bfl_free);
223 
226  "fl_op=%d", fl_op);
227  switch (fl_op) {
228  case M0_BFL_CREATE:
229  fl_be_list_credit(M0_BLO_CREATE, list_nr, accum);
230  case M0_BFL_DESTROY:
231  fl_be_list_credit(M0_BLO_DESTROY, list_nr, accum);
232  break;
233  case M0_BFL_ADD:
234  fl_be_list_credit(M0_BLO_TLINK_CREATE, 1, accum);
235  fl_be_list_credit(M0_BLO_ADD, 1, accum);
236  break;
237  case M0_BFL_DEL:
238  fl_be_list_credit(M0_BLO_DEL, 1, accum);
239  fl_be_list_credit(M0_BLO_TLINK_DESTROY, 1, accum);
240  break;
241  }
242 }
243 
244 
246 #undef M0_TRACE_SUBSYSTEM
247 
248 /*
249  * Local variables:
250  * c-indentation-style: "K&R"
251  * c-basic-offset: 8
252  * tab-width: 8
253  * fill-column: 80
254  * scroll-step: 1
255  * End:
256  */
257 /*
258  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
259  */
Definition: fl.h:54
#define M0_PRE(cond)
M0_INTERNAL void m0_be_fl_destroy(struct m0_be_fl *fl, struct m0_be_tx *tx)
Definition: fl.c:120
static bool be_fl_list_is_empty(struct m0_be_fl *fl, unsigned long index)
Definition: fl.c:105
#define NULL
Definition: misc.h:38
#define ergo(a, b)
Definition: misc.h:293
m0_bcount_t bac_size
#define M0_LOG(level,...)
Definition: trace.h:167
struct m0_be_fl_size bfl_free[M0_BE_FL_NR+1]
Definition: fl.h:76
#define min_type(t, a, b)
Definition: arith.h:76
uint64_t m0_bcount_t
Definition: types.h:77
Definition: fl.h:67
struct m0_be_list_link bac_linkage_free
M0_INTERNAL void m0_be_fl_create(struct m0_be_fl *fl, struct m0_be_tx *tx, struct m0_be_seg *seg)
Definition: fl.c:110
int i
Definition: dir.c:1033
#define PRIu64
Definition: types.h:58
M0_INTERNAL void m0_be_fl_credit(struct m0_be_fl *fl, enum m0_be_fl_op fl_op, struct m0_be_tx_credit *accum)
Definition: fl.c:218
Definition: fl.h:41
M0_INTERNAL bool m0_be_fl__invariant(struct m0_be_fl *fl)
Definition: fl.c:143
Allocator chunk.
M0_BE_LIST_DEFINE(fl, static, struct be_alloc_chunk)
uint64_t bac_magic_free
m0_be_fl_op
Definition: fl.h:50
#define M0_POST(cond)
static uint32_t list_nr(const struct m0_layout_enum *e)
Definition: list_enum.c:640
M0_INTERNAL struct be_alloc_chunk * m0_be_fl_pick(struct m0_be_fl *fl, m0_bcount_t size)
Definition: fl.c:178
struct m0_be_list bfs_list
Definition: fl.h:63
Definition: seg.h:66
static struct m0_be_list * be_fl_list(struct m0_be_fl *fl, unsigned long index)
Definition: fl.c:98
#define m0_forall(var, nr,...)
Definition: misc.h:112
M0_BE_LIST_DESCR_DEFINE(fl, "m0_be_fl", static, struct be_alloc_chunk, bac_linkage_free, bac_magic_free, M0_BE_ALLOC_FREE_LINK_MAGIC, M0_BE_ALLOC_FREE_MAGIC)
Definition: fl.h:53
M0_INTERNAL void m0_be_fl_add(struct m0_be_fl *fl, struct m0_be_tx *tx, struct be_alloc_chunk *chunk)
Definition: fl.c:151
m0_bcount_t size
Definition: di.c:39
#define _0C(exp)
Definition: assert.h:311
#define m0_be_list_endfor
Definition: list.h:369
#define m0_be_list_forall(name, var, head,...)
Definition: list.h:371
static unsigned long be_fl_index_round_up(struct m0_be_fl *fl, m0_bcount_t size)
Definition: fl.c:128
static struct m0_be_seg * seg
Definition: btree.c:40
#define M0_ASSERT_INFO(cond, fmt,...)
static unsigned long be_fl_index_round_down_chunk(struct m0_be_fl *fl, const struct be_alloc_chunk *chunk)
Definition: fl.c:136
M0_INTERNAL void m0_be_fl_del(struct m0_be_fl *fl, struct m0_be_tx *tx, struct be_alloc_chunk *chunk)
Definition: fl.c:165
#define M0_PRE_EX(cond)
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_POST_EX(cond)
static uint64_t m0_align(uint64_t val, uint64_t alignment)
Definition: arith.h:170
#define m0_be_list_for(name, head, obj)
Definition: list.h:358
Definition: tx.h:280