Motr  M0
pool.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2015-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 
23 #include "lib/memory.h"
24 #include "lib/misc.h"
25 #include "ut/ut.h"
26 
27 #include "be/op.h"
28 #include "be/pool.h"
29 
30 enum {
33 };
34 
35 #define BE_UT_POOL_MAGIC 0xdeadbeef
36 #define BE_UT_POOL_MAGIC1 UINT64_MAX
37 #define BE_UT_POOL_MAGIC2 UINT64_MAX
38 #define BE_UT_POOL_MAGIC3 UINT64_MAX
39 
42  uint64_t bupi_magic1;
44  uint64_t bupi_magic2;
45  uint64_t bupi_pool_magic;
46  uint64_t bupi_magic3;
47 };
48 
49 M0_BE_POOL_DESCR_DEFINE(ut, "be_pool UT", static, struct be_ut_pool_item,
50  bupi_pool_item, bupi_pool_magic, BE_UT_POOL_MAGIC);
51 M0_BE_POOL_DEFINE(ut, static, struct be_ut_pool_item);
52 
54 {
55  item->bupi_magic1 = BE_UT_POOL_MAGIC1;
56  item->bupi_magic2 = BE_UT_POOL_MAGIC2;
57  item->bupi_magic3 = BE_UT_POOL_MAGIC3;
58  item->bupi_in_pool = false;
59 }
60 
61 static void be_ut_pool_items_check(struct be_ut_pool_item *items,
62  int nr)
63 {
64  int i;
65 
66  for (i = 0; i < nr; ++i) {
67  M0_UT_ASSERT(items[i].bupi_magic1 == BE_UT_POOL_MAGIC1);
68  M0_UT_ASSERT(items[i].bupi_magic2 == BE_UT_POOL_MAGIC2);
69  M0_UT_ASSERT(items[i].bupi_magic3 == BE_UT_POOL_MAGIC3);
70  }
71 }
72 
73 static void be_ut_pool_op_cb(struct m0_be_op *op, void *param)
74 {
75  struct be_ut_pool_item *item;
76 
77  item = *(struct be_ut_pool_item **)param;
78  M0_UT_ASSERT(item != NULL && item->bupi_in_pool == true);
79 }
80 
81 static void be_ut_pool_usecase(int item_nr, int q_size)
82 {
83  struct m0_be_pool pool = {};
84  struct be_ut_pool_item *items;
85  struct be_ut_pool_item **reqs;
86  struct be_ut_pool_item *item;
87  struct m0_be_op *ops;
88  int i;
89  int rc;
90 
91  struct m0_be_pool_cfg cfg = {
92  .bplc_q_size = q_size,
93  };
94 
95  M0_UT_ASSERT(q_size <= item_nr);
96 
97  rc = ut_be_pool_init(&pool, &cfg);
98  M0_UT_ASSERT(rc == 0);
99 
100  M0_ALLOC_ARR(items, item_nr);
101  M0_UT_ASSERT(items != NULL);
102  M0_ALLOC_ARR(reqs, item_nr + q_size);
103  M0_UT_ASSERT(reqs != NULL);
104  M0_ALLOC_ARR(ops, q_size);
105  M0_UT_ASSERT(ops != NULL);
106 
107  for (i = 0; i < item_nr; ++i) {
108  be_ut_pool_item_init(&items[i]);
109  ut_be_pool_add(&pool, &items[i]);
110  items[i].bupi_in_pool = true;
111  }
112  be_ut_pool_items_check(items, item_nr);
113 
114  for (i = 0; i < item_nr; ++i) {
115  M0_BE_OP_SYNC(op, ut_be_pool_get(&pool, &reqs[i], &op));
116  M0_UT_ASSERT(reqs[i] != NULL);
117  M0_UT_ASSERT(reqs[i]->bupi_in_pool);
118  reqs[i]->bupi_in_pool = false;
119  }
120  be_ut_pool_items_check(items, item_nr);
121 
122  for (i = 0; i < q_size; ++i) {
123  m0_be_op_init(&ops[i]);
125  &reqs[item_nr + i], M0_BOS_DONE);
126  ut_be_pool_get(&pool, &reqs[item_nr + i], &ops[i]);
127  }
128 
129  for (i = 0; i < item_nr; ++i) {
130  reqs[i]->bupi_in_pool = true;
131  ut_be_pool_put(&pool, reqs[i]);
132  }
133  be_ut_pool_items_check(items, item_nr);
134 
135  for (i = 0; i < q_size; ++i) {
136  m0_be_op_wait(&ops[i]);
137  M0_UT_ASSERT(reqs[item_nr + i] != NULL);
138  m0_be_op_fini(&ops[i]);
139  }
140  for (i = 0; i < q_size; ++i)
141  ut_be_pool_put(&pool, reqs[item_nr + i]);
142  be_ut_pool_items_check(items, item_nr);
143 
144  for (i = 0; i < item_nr; ++i) {
145  item = ut_be_pool_del(&pool);
146  M0_UT_ASSERT(item != NULL);
147  M0_UT_ASSERT(item->bupi_in_pool);
148  item->bupi_in_pool = false;
149  }
150  item = ut_be_pool_del(&pool);
151  M0_UT_ASSERT(item == NULL);
152  M0_UT_ASSERT(m0_forall(i, item_nr, !items[i].bupi_in_pool));
153 
154  ut_be_pool_fini(&pool);
155  be_ut_pool_items_check(items, item_nr);
156 
157  m0_free(ops);
158  m0_free(reqs);
159  m0_free(items);
160 }
161 
163 {
166 }
167 
168 /*
169  * Local variables:
170  * c-indentation-style: "K&R"
171  * c-basic-offset: 8
172  * tab-width: 8
173  * fill-column: 80
174  * scroll-step: 1
175  * End:
176  */
177 /*
178  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
179  */
static size_t nr
Definition: dump.c:1505
#define M0_ALLOC_ARR(arr, nr)
Definition: memory.h:84
#define BE_UT_POOL_MAGIC3
Definition: pool.c:38
uint64_t bupi_magic1
Definition: pool.c:42
#define NULL
Definition: misc.h:38
#define BE_UT_POOL_MAGIC
Definition: pool.c:35
#define M0_BE_OP_SYNC(op_obj, action)
Definition: op.h:190
static void be_ut_pool_usecase(int item_nr, int q_size)
Definition: pool.c:81
M0_INTERNAL void m0_be_op_callback_set(struct m0_be_op *op, m0_be_op_cb_t cb, void *param, enum m0_be_op_state state)
Definition: op.c:239
static struct m0_rpc_item * item
Definition: item.c:56
uint64_t bupi_magic3
Definition: pool.c:46
op
Definition: libdemo.c:64
int i
Definition: dir.c:1033
static void be_ut_pool_item_init(struct be_ut_pool_item *item)
Definition: pool.c:53
void m0_be_ut_pool_usecase(void)
Definition: pool.c:162
static void be_ut_pool_items_check(struct be_ut_pool_item *items, int nr)
Definition: pool.c:61
static void be_ut_pool_op_cb(struct m0_be_op *op, void *param)
Definition: pool.c:73
unsigned bplc_q_size
Definition: pool.h:45
static struct m0_pool pool
Definition: iter_ut.c:58
#define m0_forall(var, nr,...)
Definition: misc.h:112
struct m0_be_pool_item bupi_pool_item
Definition: pool.c:43
M0_BE_POOL_DESCR_DEFINE(ut, "be_pool UT", static, struct be_ut_pool_item, bupi_pool_item, bupi_pool_magic, BE_UT_POOL_MAGIC)
M0_BE_POOL_DEFINE(ut, static, struct be_ut_pool_item)
bool bupi_in_pool
Definition: pool.c:41
M0_INTERNAL void m0_be_op_fini(struct m0_be_op *op)
Definition: stubs.c:92
Definition: op.h:59
struct m0_fom_ops ops
Definition: io_foms.c:623
Definition: op.h:74
M0_INTERNAL void m0_be_op_init(struct m0_be_op *op)
Definition: stubs.c:87
void m0_free(void *data)
Definition: memory.c:146
uint64_t bupi_pool_magic
Definition: pool.c:45
#define BE_UT_POOL_MAGIC1
Definition: pool.c:36
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46
uint64_t bupi_magic2
Definition: pool.c:44
M0_INTERNAL void m0_be_op_wait(struct m0_be_op *op)
Definition: stubs.c:96
#define BE_UT_POOL_MAGIC2
Definition: pool.c:37