Motr  M0
bitmap.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 "ut/ut.h"
24 #include "lib/ub.h"
25 #include "lib/bitmap.h"
26 #include "lib/assert.h"
27 #include "lib/misc.h" /* m0_forall */
28 
29 enum {
31 };
32 
33 static void test_bitmap_copy(void)
34 {
35  struct m0_bitmap src;
36  struct m0_bitmap dst;
37  size_t dst_nr;
38  size_t i;
39  int n;
40 
42  for (i = 0; i < UT_BITMAP_SIZE; i += 3)
43  m0_bitmap_set(&src, i, true);
44 
45  for (n = 1; n < 3; ++n) {
46  /* n == 1: equal sized, n == 2: dst size is bigger */
47  dst_nr = n * UT_BITMAP_SIZE;
48  M0_UT_ASSERT(m0_bitmap_init(&dst, dst_nr) == 0);
49  for (i = 1; i < dst_nr; i += 2)
50  m0_bitmap_set(&dst, i, true);
51 
53  for (i = 0; i < UT_BITMAP_SIZE; ++i)
55  m0_bitmap_get(&dst, i));
56  for (; i < dst_nr; ++i)
59  }
61 }
62 
63 void test_bitmap(void)
64 {
65  struct m0_bitmap bm;
66  size_t idx;
67 
70  M0_UT_ASSERT(bm.b_words != NULL);
71 
72  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
73  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) == false);
74  }
75 
76  m0_bitmap_set(&bm, 0, true);
77  M0_UT_ASSERT(m0_bitmap_get(&bm, 0) == true);
78  M0_UT_ASSERT(m0_bitmap_ffs(&bm) == 0);
79  m0_bitmap_set(&bm, 0, false);
80 
81  m0_bitmap_set(&bm, 1, true);
82  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
83  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) == (idx == 1));
84  }
85  M0_UT_ASSERT(m0_bitmap_ffs(&bm) == 1);
86 
87  m0_bitmap_set(&bm, 2, true);
88  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
89  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) == (idx == 1 || idx == 2));
90  }
91  M0_UT_ASSERT(m0_bitmap_ffs(&bm) == 1);
92 
93  m0_bitmap_set(&bm, 64, true);
94  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
95  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) ==
96  (idx == 1 || idx == 2 || idx == 64));
97  }
98  M0_UT_ASSERT(m0_bitmap_ffs(&bm) == 1);
99 
100  m0_bitmap_set(&bm, 2, false);
101  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
102  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) ==
103  (idx == 1 || idx == 64));
104  }
105 
106  m0_bitmap_set(&bm, 1, false);
107  M0_UT_ASSERT(m0_bitmap_ffs(&bm) == 64);
108  m0_bitmap_set(&bm, 64, false);
109  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx) {
110  M0_UT_ASSERT(m0_bitmap_get(&bm, idx) == false);
111  }
112 
113  m0_bitmap_fini(&bm);
114  M0_UT_ASSERT(bm.b_nr == 0);
115  M0_UT_ASSERT(bm.b_words == NULL);
116 
118 }
119 
121 {
122  struct m0_bitmap in_bm;
123  struct m0_bitmap out_bm;
124  struct m0_bitmap_onwire ow_bm;
125 
127  M0_UT_ASSERT(in_bm.b_nr == UT_BITMAP_SIZE);
128  M0_UT_ASSERT(in_bm.b_words != NULL);
129 
130  m0_bitmap_set(&in_bm, 1, true);
131  m0_bitmap_set(&in_bm, 7, true);
132  m0_bitmap_set(&in_bm, 64, true);
133 
135  m0_bitmap_store(&in_bm, &ow_bm);
137  m0_bitmap_load(&ow_bm, &out_bm);
138 
140  m0_bitmap_get(&out_bm, i) == m0_bitmap_get(&in_bm, i)));
141 
142  m0_bitmap_fini(&in_bm);
143  m0_bitmap_onwire_fini(&ow_bm);
144  m0_bitmap_fini(&out_bm);
145 }
146 
147 enum {
148  UB_ITER = 100000
149 };
150 
151 static struct m0_bitmap ub_bm;
152 
153 static int ub_init(const char *opts M0_UNUSED)
154 {
156 }
157 
158 static void ub_fini(void)
159 {
161 }
162 
163 static void ub_set0(int i)
164 {
165  size_t idx;
166 
167  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx)
168  m0_bitmap_set(&ub_bm, idx, false);
169 }
170 
171 static void ub_set1(int i)
172 {
173  size_t idx;
174 
175  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx)
176  m0_bitmap_set(&ub_bm, idx, true);
177 }
178 
179 static void ub_get(int i)
180 {
181  size_t idx;
182 
183  for (idx = 0; idx < UT_BITMAP_SIZE; ++idx)
184  m0_bitmap_get(&ub_bm, idx);
185 }
186 
188  .us_name = "bitmap-ub",
189  .us_init = ub_init,
190  .us_fini = ub_fini,
191  .us_run = {
192  { .ub_name = "set0",
193  .ub_iter = UB_ITER,
194  .ub_round = ub_set0 },
195  { .ub_name = "set1",
196  .ub_iter = UB_ITER,
197  .ub_round = ub_set1 },
198  { .ub_name = "get",
199  .ub_iter = UB_ITER,
200  .ub_round = ub_get },
201  { .ub_name = NULL }
202  }
203 };
204 
205 /*
206  * Local variables:
207  * c-indentation-style: "K&R"
208  * c-basic-offset: 8
209  * tab-width: 8
210  * fill-column: 80
211  * scroll-step: 1
212  * End:
213  */
M0_INTERNAL int m0_bitmap_init(struct m0_bitmap *map, size_t nr)
Definition: bitmap.c:86
static void ub_fini(void)
Definition: bitmap.c:158
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_bitmap_fini(struct m0_bitmap *map)
Definition: bitmap.c:97
static struct m0_bufvec dst
Definition: xform.c:61
static struct m0_bitmap ub_bm
Definition: bitmap.c:151
static void test_bitmap_copy(void)
Definition: bitmap.c:33
static int ub_init(const char *opts M0_UNUSED)
Definition: bitmap.c:153
M0_INTERNAL void m0_bitmap_store(const struct m0_bitmap *im_map, struct m0_bitmap_onwire *ow_map)
Definition: bitmap.c:200
int i
Definition: dir.c:1033
M0_INTERNAL void m0_bitmap_onwire_fini(struct m0_bitmap_onwire *ow_map)
Definition: bitmap.c:192
M0_INTERNAL int m0_bitmap_onwire_init(struct m0_bitmap_onwire *ow_map, size_t nr)
Definition: bitmap.c:182
const char * us_name
Definition: ub.h:76
static void ub_set1(int i)
Definition: bitmap.c:171
M0_INTERNAL void m0_bitmap_set(struct m0_bitmap *map, size_t idx, bool val)
Definition: bitmap.c:139
M0_INTERNAL int m0_bitmap_ffs(const struct m0_bitmap *map)
Definition: bitmap.c:112
M0_INTERNAL void m0_bitmap_copy(struct m0_bitmap *dst, const struct m0_bitmap *src)
Definition: bitmap.c:158
#define m0_forall(var, nr,...)
Definition: misc.h:112
uint64_t n
Definition: fops.h:107
uint64_t * b_words
Definition: bitmap.h:46
struct m0_ub_set m0_bitmap_ub
Definition: bitmap.c:187
M0_INTERNAL bool m0_bitmap_get(const struct m0_bitmap *map, size_t idx)
Definition: bitmap.c:105
static void ub_get(int i)
Definition: bitmap.c:179
size_t b_nr
Definition: bitmap.h:44
M0_INTERNAL void m0_bitmap_load(const struct m0_bitmap_onwire *ow_map, struct m0_bitmap *im_map)
Definition: bitmap.c:213
void test_bitmap(void)
Definition: bitmap.c:63
struct m0_pdclust_src_addr src
Definition: fd.c:108
Definition: ub.h:74
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void ub_set0(int i)
Definition: bitmap.c:163
void test_bitmap_onwire(void)
Definition: bitmap.c:120
#define M0_UNUSED
Definition: misc.h:380