Motr  M0
bytecount.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2022 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 "ut/ut.h"
24 #include "lib/ub.h"
25 #include "lib/misc.h" /* M0_SET0 */
26 #include "lib/memory.h"
27 #include "be/ut/helper.h"
28 #include "be/seg.h"
29 #include "cob/cob.h"
30 #include "lib/locality.h"
31 
32 enum {
33  KEY_VAL_NR = 10,
34 };
35 
36 static struct m0_cob_domain_id id = { 42 };
37 static struct m0_be_ut_backend ut_be;
38 static struct m0_sm_group *grp;
39 static struct m0_cob_domain *dom;
40 static struct m0_cob *cob;
43 
44 
45 static int ut_init(void)
46 {
47  return 0;
48 }
49 
50 static int ut_fini(void)
51 {
52  return 0;
53 }
54 
55 static void ut_tx_open(struct m0_be_tx *tx, struct m0_be_tx_credit *credit)
56 {
57  int rc;
58 
59  m0_be_ut_tx_init(tx, &ut_be);
60  m0_be_tx_prep(tx, credit);
61  rc = m0_be_tx_open_sync(tx);
62  M0_UT_ASSERT(rc == 0);
64 }
65 
66 static void test_cob_dom_create(void)
67 {
68  struct m0_be_seg *seg0;
69  int rc;
70 
71  M0_SET0(&ut_be);
74 
76  rc = m0_cob_domain_create(&dom, grp, &id, &ut_be.but_dom, seg0); /*XXX*/
77  M0_UT_ASSERT(rc == 0);
78 
81 }
82 
83 static void test_init(void)
84 {
85  struct m0_be_seg *seg0;
86  int rc;
87 
88  M0_SET0(&ut_be);
90 
92 
94  M0_UT_ASSERT(rc == 0);
95 }
96 
97 static void test_fini(void)
98 {
99  int rc;
100 
101  m0_free(cob); /* Free m0_cob struct allocated in test_init() */
104  M0_UT_ASSERT(rc == 0);
105 
107 }
108 
109 void test_insert(void)
110 {
111  struct m0_be_tx tx_;
112  struct m0_be_tx *tx = &tx_;
113  int rc;
114  struct m0_be_tx_credit accum = {};
115  int i;
116 
117  rc = m0_cob_alloc(dom, &cob);
118  M0_UT_ASSERT(rc == 0);
119 
120  /* Add credits for insertion operation */
121  for (i = 0; i < KEY_VAL_NR; i++) {
123  }
124 
125  /* Populate keys and records */
126  for (i = 0; i < KEY_VAL_NR; i++) {
127  bckey[i].cbk_pfid = M0_FID_TINIT('k', 1, i);
128  bckey[i].cbk_user_id = i;
129  bcrec[i].cbr_bytecount = 100 + (i * 10);
130  bcrec[i].cbr_cob_objects = 10 + i;
131  }
132 
133  ut_tx_open(tx, &accum);
134 
135  /* Insert keys and records in bytecount btree */
136  for (i = 0; i < KEY_VAL_NR; i++) {
137  rc = m0_cob_bc_insert(cob, &bckey[i], &bcrec[i], tx);
138  M0_UT_ASSERT(rc == 0);
139  }
140 
142  m0_be_tx_fini(tx);
143 }
144 
146 {
147  int i;
148  int rc;
149  uint32_t count;
150  struct m0_cob_bckey dump_keys[KEY_VAL_NR];
151  struct m0_cob_bcrec dump_recs[KEY_VAL_NR];
152  struct m0_buf *keys = NULL;
153  struct m0_buf *recs = NULL;
154  struct m0_fid temp_fid;
155  struct m0_cob_bckey *kcurr;
156  struct m0_cob_bcrec *rcurr;
157 
158  rc = m0_cob_bc_entries_dump(cob->co_dom, &keys, &recs, &count);
159  M0_UT_ASSERT(rc == 0);
161 
162  kcurr = (struct m0_cob_bckey *)keys->b_addr;
163  rcurr = (struct m0_cob_bcrec *)recs->b_addr;
164 
165  for (i =0; i < count; i++) {
166  memcpy(&dump_keys[i], kcurr, sizeof(struct m0_cob_bckey));
167  memcpy(&dump_recs[i], rcurr, sizeof(struct m0_cob_bcrec));
168 
169  kcurr++;
170  rcurr++;
171 
172  temp_fid = M0_FID_TINIT('k', 1, i);
173  M0_UT_ASSERT(m0_fid_eq(&dump_keys[i].cbk_pfid, &temp_fid));
174  M0_UT_ASSERT(dump_keys[i].cbk_user_id == i);
175  M0_UT_ASSERT(dump_recs[i].cbr_bytecount == 100 + (i * 10));
176  M0_UT_ASSERT(dump_recs[i].cbr_cob_objects == 10 + i);
177  }
178 }
179 
180 void test_iterator(void)
181 {
182  struct m0_cob_bc_iterator it;
183  int rc;
184  /* Make sure fid is valid. */
185  struct m0_fid fid = M0_FID_TINIT('k', 1, 0);
186 
187  rc = m0_cob_bc_iterator_init(cob, &it, &fid, 0);
188  M0_UT_ASSERT(rc == 0);
190  M0_UT_ASSERT(rc == 0);
192  M0_UT_ASSERT(rc == 0);
194 }
195 
196 void test_lookup(void)
197 {
198  int rc;
199  int i;
200  struct m0_cob_bckey dummy_key = {};
201  struct m0_cob_bcrec out_rec = {};
202 
207  for (i = 0; i < KEY_VAL_NR; i++) {
208  rc = m0_cob_bc_lookup(cob, &bckey[i], &out_rec);
209  M0_UT_ASSERT(rc == 0);
212  out_rec.cbr_cob_objects);
213  }
214 
219  dummy_key.cbk_pfid = M0_FID_TINIT('k', 0, 1);
220  dummy_key.cbk_user_id = 0;
221  rc = m0_cob_bc_lookup(cob, &dummy_key, &out_rec);
222  M0_UT_ASSERT(rc != 0);
223 }
224 
225 void test_update(void)
226 {
227  struct m0_be_tx tx_;
228  struct m0_be_tx *tx = &tx_;
229  int rc;
230  struct m0_be_tx_credit accum = {};
231  int i;
232  struct m0_cob_bcrec out_rec = {};
233 
234  /* Add credits for update operation */
235  for (i = 0; i < KEY_VAL_NR; i++) {
237  }
238 
239  /* Populate new record values */
240  for (i = 0; i < KEY_VAL_NR; i++) {
241  bcrec[i].cbr_bytecount = 200 + i;
242  bcrec[i].cbr_cob_objects = 20 + i;
243  }
244 
245  ut_tx_open(tx, &accum);
246 
247  /* Update records of keys in bytecount btree created in test_insert() */
248  for (i = 0; i < KEY_VAL_NR; i++) {
249  rc = m0_cob_bc_update(cob, &bckey[i], &bcrec[i], tx);
250  M0_UT_ASSERT(rc == 0);
251  }
252 
257  for (i = 0; i < KEY_VAL_NR; i++) {
258  rc = m0_cob_bc_lookup(cob, &bckey[i], &out_rec);
259  M0_UT_ASSERT(rc == 0);
262  out_rec.cbr_cob_objects);
263  }
264 
266  m0_be_tx_fini(tx);
267 }
268 
270  .ts_name = "bytecount-ut",
271  .ts_init = ut_init,
272  .ts_fini = ut_fini,
273  .ts_tests = {
274  { "cob-dom-create", test_cob_dom_create },
275  { "cob-dom-init", test_init },
276  { "bc-tree-insert", test_insert },
277  { "bc-entries-dump", test_entries_dump },
278  { "bc-tree-iterator", test_iterator },
279  { "bc-tree-lookup", test_lookup },
280  { "bc-tree-update", test_update },
281  { "cob-dom-fini", test_fini },
282  { NULL, NULL }
283  }
284 };
285 
286 
287 /*
288  * Local variables:
289  * c-indentation-style: "K&R"
290  * c-basic-offset: 8
291  * tab-width: 8
292  * fill-column: 80
293  * scroll-step: 1
294  * End:
295  */
Definition: cob.h:581
M0_INTERNAL int m0_be_ut_backend_init_cfg(struct m0_be_ut_backend *ut_be, const struct m0_be_domain_cfg *cfg, bool mkfs)
Definition: stubs.c:231
M0_INTERNAL int m0_cob_bc_iterator_next(struct m0_cob_bc_iterator *it)
Definition: cob.c:467
uint64_t cbk_user_id
Definition: cob.h:517
#define NULL
Definition: misc.h:38
static void test_fini(void)
Definition: bytecount.c:97
m0_be_tx_state
Definition: tx.h:214
void * b_addr
Definition: buf.h:39
static struct m0_be_ut_backend ut_be
Definition: bytecount.c:37
static struct m0_sm_group * grp
Definition: bytecount.c:38
struct m0_cob_domain * co_dom
Definition: cob.h:582
M0_INTERNAL void m0_be_tx_fini(struct m0_be_tx *tx)
Definition: stubs.c:163
void m0_cob_domain_fini(struct m0_cob_domain *dom)
Definition: cob.c:726
static int ut_fini(void)
Definition: bytecount.c:50
M0_INTERNAL struct m0_be_seg * m0_be_domain_seg0_get(struct m0_be_domain *dom)
Definition: domain.c:466
static struct m0_be_emap_cursor it
Definition: extmap.c:46
struct m0_ut_suite bytecount_ut
Definition: bytecount.c:269
M0_INTERNAL void m0_be_tx_prep(struct m0_be_tx *tx, const struct m0_be_tx_credit *credit)
Definition: stubs.c:175
void test_update(void)
Definition: bytecount.c:225
M0_INTERNAL int m0_cob_bc_insert(struct m0_cob *cob, struct m0_cob_bckey *bc_key, struct m0_cob_bcrec *bc_val, struct m0_be_tx *tx)
Definition: cob.c:1140
int m0_cob_domain_create(struct m0_cob_domain **dom, struct m0_sm_group *grp, const struct m0_cob_domain_id *cdid, struct m0_be_domain *bedom, struct m0_be_seg *seg)
Definition: cob.c:840
void test_lookup(void)
Definition: bytecount.c:196
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL void m0_cob_bc_iterator_fini(struct m0_cob_bc_iterator *it)
Definition: cob.c:489
Definition: ut.h:77
static struct m0_cob_domain_id id
Definition: bytecount.c:36
static m0_bcount_t count
Definition: xcode.c:167
static struct m0_cob_domain * dom
Definition: bytecount.c:39
struct m0_fid fid
Definition: di.c:46
M0_INTERNAL void m0_cob_tx_credit(struct m0_cob_domain *dom, enum m0_cob_op optype, struct m0_be_tx_credit *accum)
Definition: cob.c:2281
Definition: buf.h:37
int i
Definition: dir.c:1033
struct m0_fid cbk_pfid
Definition: cob.h:516
M0_INTERNAL int m0_cob_bc_update(struct m0_cob *cob, struct m0_cob_bckey *bc_key, struct m0_cob_bcrec *bc_val, struct m0_be_tx *tx)
Definition: cob.c:1161
#define M0_FID_TINIT(type, container, key)
Definition: fid.h:90
static struct m0_cob * cob
Definition: bytecount.c:40
static void test_init(void)
Definition: bytecount.c:83
void test_entries_dump(void)
Definition: bytecount.c:145
int m0_cob_domain_destroy(struct m0_cob_domain *dom, struct m0_sm_group *grp, struct m0_be_domain *bedom)
Definition: cob.c:871
void test_iterator(void)
Definition: bytecount.c:180
M0_INTERNAL int m0_be_tx_open_sync(struct m0_be_tx *tx)
Definition: stubs.c:199
void test_insert(void)
Definition: bytecount.c:109
struct m0_cob_bckey bckey[KEY_VAL_NR]
Definition: bytecount.c:41
static void test_cob_dom_create(void)
Definition: bytecount.c:66
struct m0_sm_group * m0_be_ut_backend_sm_group_lookup(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:277
M0_INTERNAL int m0_cob_bc_iterator_init(struct m0_cob *cob, struct m0_cob_bc_iterator *it, const struct m0_fid *pver_fid, uint64_t user_id)
Definition: cob.c:420
Definition: seg.h:66
static int ut_init(void)
Definition: bytecount.c:45
struct m0_be_domain but_dom
Definition: helper.h:47
int m0_cob_domain_init(struct m0_cob_domain *dom, struct m0_be_seg *seg)
Definition: cob.c:708
static void ut_tx_open(struct m0_be_tx *tx, struct m0_be_tx_credit *credit)
Definition: bytecount.c:55
M0_INTERNAL bool m0_fid_eq(const struct m0_fid *fid0, const struct m0_fid *fid1)
Definition: fid.c:164
const char * ts_name
Definition: ut.h:99
Definition: fid.h:38
uint64_t cbr_bytecount
Definition: cob.h:522
M0_INTERNAL int m0_cob_bc_lookup(struct m0_cob *cob, struct m0_cob_bckey *bc_key, struct m0_cob_bcrec *bc_rec)
Definition: cob.c:1120
struct m0_cob_bcrec bcrec[KEY_VAL_NR]
Definition: bytecount.c:42
M0_INTERNAL int m0_cob_bc_iterator_get(struct m0_cob_bc_iterator *it)
Definition: cob.c:443
void m0_be_ut_tx_init(struct m0_be_tx *tx, struct m0_be_ut_backend *ut_be)
Definition: stubs.c:286
static struct m0_be_seg * seg0
Definition: active_record.c:31
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
uint64_t cbr_cob_objects
Definition: cob.h:523
M0_INTERNAL int m0_cob_alloc(struct m0_cob_domain *dom, struct m0_cob **out)
Definition: cob.c:1100
M0_INTERNAL int m0_cob_bc_entries_dump(struct m0_cob_domain *cdom, struct m0_buf **out_keys, struct m0_buf **out_recs, uint32_t *out_count)
Definition: cob.c:496
void m0_free(void *data)
Definition: memory.c:146
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_be_tx_close_sync(struct m0_be_tx *tx)
Definition: stubs.c:205
Definition: tx.h:280