Motr  M0
list.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
24 #include "lib/trace.h"
25 
26 #include "be/list.h"
27 
28 #include "ut/ut.h" /* M0_UT_ASSERT */
29 
30 #include "lib/misc.h" /* M0_SET0 */
31 
32 #include "be/ut/helper.h" /* m0_be_ut_backend_init */
33 
34 /* -------------------------------------------------------------------------
35  * Descriptors and stuff
36  * ------------------------------------------------------------------------- */
37 enum {
38  TEST_MAGIC = 0x331fefefefefe177,
39  TEST_LINK_MAGIC = 0x331acacacacac277
40 };
41 
42 struct test {
43  uint64_t t_magic;
45  int t_payload;
46 };
47 
48 M0_BE_LIST_DESCR_DEFINE(test, "test:m0-be-list", static, struct test,
49  t_linkage, t_magic, TEST_MAGIC, TEST_LINK_MAGIC);
50 M0_BE_LIST_DEFINE(test, static, struct test);
51 
52 /* -------------------------------------------------------------------------
53  * List construction test
54  * ------------------------------------------------------------------------- */
55 
56 static void check(struct m0_be_list *list, struct m0_be_seg *seg);
57 M0_UNUSED static void print(struct m0_be_list *list);
58 
59 M0_INTERNAL void m0_be_ut_list(void)
60 {
61  enum { SHIFT = 0 };
62  /* Following variables are static to reduce kernel stack consumption. */
63  static struct m0_be_tx_credit cred_add = {};
64  static struct m0_be_list *list;
65  static struct m0_be_ut_backend ut_be;
66  static struct m0_be_ut_seg ut_seg;
67  static struct m0_be_seg *seg;
68  static struct test *elem[10];
69  int i;
70 
71  M0_ENTRY();
72 
73  M0_SET0(&ut_be);
74  /* Init BE. */
76  m0_be_ut_seg_init(&ut_seg, &ut_be, 1ULL << 16);
77  seg = ut_seg.bus_seg;
78 
80  for (i = 0; i < ARRAY_SIZE(elem); ++i)
81  M0_BE_UT_ALLOC_PTR(&ut_be, &ut_seg, elem[i]);
82 
83  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
84  test_be_list_credit(M0_BLO_CREATE, 1, &cred),
85  test_be_list_create(list, tx));
86 
87  /* Perform some operations over the list. */
88 
89  for (i = 0; i < ARRAY_SIZE(elem); ++i) {
90  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
91  test_be_list_credit(M0_BLO_TLINK_CREATE, 1, &cred),
92  test_be_tlink_create(elem[i], tx));
93  }
94  /* add */
95  test_be_list_credit(M0_BLO_ADD, 1, &cred_add);
96  for (i = 0; i < ARRAY_SIZE(elem); ++i) {
97  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
98  cred = M0_BE_TX_CREDIT_PTR(elem[i]),
99  (elem[i]->t_payload = i,
100  M0_BE_TX_CAPTURE_PTR(seg, tx, elem[i])));
101 
102  if (i < ARRAY_SIZE(elem) / 2) {
103  if (i % 2 == 0) {
104  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
105  cred = cred_add,
106  test_be_list_add(list, tx, elem[i]));
107  } else {
108  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
109  cred = cred_add,
110  test_be_list_add_tail(list, tx, elem[i]));
111  }
112  } else {
113  if (i % 2 == 0) {
114  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
115  cred = cred_add,
116  test_be_list_add_after(list, tx,
117  elem[i - 1], elem[i]));
118  } else {
119  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
120  cred = cred_add,
121  test_be_list_add_before(list, tx,
122  elem[i - 1], elem[i]));
123  }
124  }
125  }
126 
127  /* delete */
128  for (i = 0; i < ARRAY_SIZE(elem); ++i) {
129  if (!M0_IN(i, (0, 2, 7, 9)))
130  continue;
131 
132  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
133  test_be_list_credit(M0_BLO_DEL, 1, &cred),
134  test_be_list_del(list, tx, elem[i]));
135  }
136 
137  /* Reload segment and check data. */
139 
140  check(list, seg);
141 
142  for (i = 0; i < ARRAY_SIZE(elem); ++i) {
143  if (M0_IN(i, (0, 2, 7, 9)))
144  continue;
145 
146  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
147  test_be_list_credit(M0_BLO_DEL, 1, &cred),
148  test_be_list_del(list, tx, elem[i]));
149  }
150 
151  for (i = 0; i < ARRAY_SIZE(elem); ++i) {
152  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
153  test_be_list_credit(M0_BLO_TLINK_DESTROY, 1, &cred),
154  test_be_tlink_destroy(elem[i], tx));
155  }
156  M0_BE_UT_TRANSACT(&ut_be, tx, cred,
157  test_be_list_credit(M0_BLO_DESTROY, 1, &cred),
158  test_be_list_destroy(list, tx));
159 
160  for (i = 0; i < ARRAY_SIZE(elem); ++i)
161  M0_BE_UT_FREE_PTR(&ut_be, &ut_seg, elem[i]);
163 
166 
167  M0_LEAVE();
168 }
169 
170 /* -------------------------------------------------------------------------
171  * List reloading test
172  * ------------------------------------------------------------------------- */
173 
174 static void check(struct m0_be_list *list, struct m0_be_seg *seg)
175 {
176  struct test *test;
177  int expected[] = { 5, 8, 6, 4, 1, 3 };
178  int i = 0;
179 
184 }
185 
186 M0_UNUSED static void print(struct m0_be_list *list)
187 {
188  struct test *test;
189 
190  M0_LOG(M0_DEBUG, "----------");
192  M0_LOG(M0_DEBUG, "-- %d", test->t_payload);
194 }
195 
196 #undef M0_TRACE_SUBSYSTEM
197 
200 /*
201  * Local variables:
202  * c-indentation-style: "K&R"
203  * c-basic-offset: 8
204  * tab-width: 8
205  * fill-column: 80
206  * scroll-step: 1
207  * End:
208  */
209 /*
210  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
211  */
void m0_be_ut_seg_fini(struct m0_be_ut_seg *ut_seg)
Definition: stubs.c:267
#define M0_BE_TX_CREDIT_PTR(ptr)
Definition: tx_credit.h:98
static struct m0_list list
Definition: list.c:144
void m0_be_ut_seg_reload(struct m0_be_ut_seg *ut_seg)
Definition: stubs.c:272
int t_payload
Definition: list.c:45
#define M0_LOG(level,...)
Definition: trace.h:167
M0_LEAVE()
struct m0_be_seg * bus_seg
Definition: helper.h:119
uint64_t t_magic
Definition: tlist.h:296
M0_BE_LIST_DEFINE(test, static, struct test)
#define M0_BE_TX_CAPTURE_PTR(seg, tx, ptr)
Definition: tx.h:505
struct m0_be_ut_seg ut_seg
Definition: ad.c:73
void m0_be_ut_seg_init(struct m0_be_ut_seg *ut_seg, struct m0_be_ut_backend *ut_be, m0_bcount_t size)
Definition: stubs.c:256
#define M0_SET0(obj)
Definition: misc.h:64
M0_BE_LIST_DESCR_DEFINE(test, "test:m0-be-list", static, struct test, t_linkage, t_magic, TEST_MAGIC, TEST_LINK_MAGIC)
static void check(struct m0_be_list *list, struct m0_be_seg *seg)
Definition: list.c:174
struct tpool_test test[]
Definition: thread_pool.c:45
#define M0_ENTRY(...)
Definition: trace.h:170
static int expected
Definition: locality.c:102
int i
Definition: dir.c:1033
static M0_UNUSED void print(struct m0_be_list *list)
Definition: list.c:186
struct m0_be_ut_backend ut_be
Definition: ad.c:72
#define M0_BE_UT_FREE_PTR(ut_be, ut_seg, ptr)
Definition: helper.h:151
Definition: tx.c:251
void m0_be_ut_backend_init(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:238
uint64_t t_magic
Definition: list.c:43
Definition: seg.h:66
struct m0_be_list_link t_linkage
Definition: list.c:44
#define M0_BE_UT_TRANSACT(__ut_be, __tx, __cred, __credit_func, __action_func)
Definition: helper.h:159
Definition: list.c:42
void m0_be_ut_backend_fini(struct m0_be_ut_backend *ut_be)
Definition: stubs.c:242
#define m0_be_list_endfor
Definition: list.h:369
static struct m0_be_seg * seg
Definition: btree.c:40
M0_INTERNAL void m0_be_ut_list(void)
Definition: list.c:59
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
#define m0_be_list_for(name, head, obj)
Definition: list.h:358
#define M0_BE_UT_ALLOC_PTR(ut_be, ut_seg, ptr)
Definition: helper.h:147
#define M0_UNUSED
Definition: misc.h:380