Motr  M0
common.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 "fd/fd_internal.h"
24 #include "fd/ut/common.h"
25 #include "ut/ut.h"
26 #include "lib/memory.h" /* m0_alloc() */
27 #include "lib/errno.h" /* ENOME M */
28 #include "lib/arith.h" /* min_type() */
29 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_LIB
31 #include "lib/trace.h" /* M0_ERR() */
32 
33 M0_INTERNAL void fd_ut_symm_tree_create(struct m0_fd_tree *tree,
34  enum tree_gen_type tg_type,
35  uint64_t *child_nr, uint64_t depth)
36 {
37  int rc;
38  uint32_t i;
39 
40  M0_UT_ASSERT(tree != NULL);
41  M0_UT_ASSERT(M0_IN(tg_type, (TG_DETERM, TG_RANDOM)));
42  M0_UT_ASSERT(child_nr != NULL);
43 
44  if (tg_type == TG_RANDOM)
46  rc = m0_fd__tree_root_create(tree, child_nr[0]);
47  M0_UT_ASSERT(rc == 0);
48 
49  for (i = 1; i < depth; ++i) {
50  rc = fd_ut_tree_level_populate(tree, child_nr[i], i, TA_SYMM);
51  M0_UT_ASSERT(rc == 0);
52  }
54 }
55 
56 M0_INTERNAL void fd_ut_children_populate(uint64_t *children, uint32_t depth)
57 {
58  uint32_t i;
59  uint32_t j;
61 
62  seed = m0_time_now();
63  for (i = 0; i < depth; ++i) {
65  children[i] = TUA_CHILD_NR_MAX - j;
66  }
67 }
68 
69 M0_INTERNAL int fd_ut_tree_level_populate(struct m0_fd_tree *tree,
70  uint64_t max_children,
71  uint16_t level, enum tree_attr ta)
72 {
73  struct m0_fd__tree_cursor cursor;
74  struct m0_fd_tree_node **node;
75  uint64_t children;
76  int rc;
77 
78  M0_UT_ASSERT(tree != NULL);
79  M0_UT_ASSERT(tree->ft_root != NULL);
80  M0_UT_ASSERT(level <= tree->ft_depth);
81  M0_UT_ASSERT(ergo(level == tree->ft_depth, max_children == 0));
82 
83  rc = m0_fd__tree_cursor_init(&cursor, tree, level);
84  M0_UT_ASSERT(rc == 0);
85  do {
86  node = m0_fd__tree_cursor_get(&cursor);
87  *node = m0_alloc(sizeof node[0][0]);
88  if (*node == NULL) {
89  rc = -ENOMEM;
90  goto err;
91  }
92  children = ta == TA_SYMM ? max_children :
93  fd_ut_random_cnt_get(max_children);
94  /* To ensure better randomization for asymmetric tree */
95  max_children = ta == TA_ASYMM && max_children > 1 ?
96  max_children - 1 : max_children;
97  rc = m0_fd__tree_node_init(tree, *node, children, &cursor);
98  if (rc != 0)
99  goto err;
100  } while (m0_fd__tree_cursor_next(&cursor));
101  return 0;
102 err:
103  if (*node != NULL) {
104  m0_fd__tree_node_fini(tree, *node);
105  m0_free(*node);
106  *node = NULL;
107  }
108  --cursor.ftc_child_abs_idx;
109  tree->ft_depth = cursor.ftc_child_abs_idx > - 1 ? cursor.ftc_depth :
110  cursor.ftc_depth - 1;
111  return rc;
112 }
113 
114 M0_INTERNAL int fd_ut_tree_init(struct m0_fd_tree *tree, uint64_t tree_depth)
115 {
116  M0_PRE(tree != NULL);
117  M0_PRE(tree_depth < M0_CONF_PVER_HEIGHT);
118 
119  tree->ft_depth = tree_depth;
120  tree->ft_cnt = 0;
121  tree->ft_root = m0_alloc(sizeof tree->ft_root[0]);
122  if (tree->ft_root == NULL)
123  goto err;
124  return 0;
125 err:
126  tree->ft_depth = 0;
127  return -ENOMEM;
128 }
129 
130 M0_INTERNAL void fd_ut_symm_tree_get(struct m0_fd_tree *tree,
131  uint64_t *children_nr)
132 {
133  struct m0_fd__tree_cursor cursor;
134  struct m0_fd_tree_node *node;
135  uint64_t depth;
136  uint64_t i;
137  uint64_t min = 0;
138  int rc;
139 
141  M0_UT_ASSERT(children_nr != NULL);
142 
143  for (i = 0; i < M0_CONF_PVER_HEIGHT; ++i)
144  children_nr[i] = i >= tree->ft_depth;
145 
146  for (depth = 0; depth < tree->ft_depth; ++depth) {
147  rc = m0_fd__tree_cursor_init(&cursor, tree, depth);
148  M0_UT_ASSERT(rc == 0);
149  do {
150  node = *(m0_fd__tree_cursor_get(&cursor));
151  if (node->ftn_abs_idx == 0)
152  min = node->ftn_child_nr;
153  min = min_type(uint64_t, min, node->ftn_child_nr);
154  children_nr[depth] = min;
155  } while (m0_fd__tree_cursor_next(&cursor));
156  }
157 }
158 
159 M0_INTERNAL uint64_t fd_ut_random_cnt_get(uint64_t max_cnt)
160 {
161  m0_time_t seed;
162  uint64_t cnt;
163 
164  seed = m0_time_now();
165  cnt = m0_rnd(max_cnt, &seed);
166  return max_cnt - cnt;
167 }
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
M0_INTERNAL void fd_ut_symm_tree_get(struct m0_fd_tree *tree, uint64_t *children_nr)
Definition: common.c:130
tree_gen_type
Definition: common.h:61
#define ergo(a, b)
Definition: misc.h:293
uint64_t m0_time_t
Definition: time.h:37
enum m0_trace_level level
Definition: trace.c:111
M0_INTERNAL void fd_ut_children_populate(uint64_t *children, uint32_t depth)
Definition: common.c:56
static struct net_test_cmd_node * node
Definition: commands.c:72
uint16_t ft_depth
Definition: fd.h:201
int32_t ftc_child_abs_idx
Definition: fd_internal.h:41
M0_INTERNAL bool m0_fd__tree_invariant(const struct m0_fd_tree *tree)
Definition: fd_tree.c:202
M0_INTERNAL int m0_fd__tree_cursor_next(struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:142
#define min_type(t, a, b)
Definition: arith.h:76
Definition: common.h:71
M0_INTERNAL int m0_fd__tree_root_create(struct m0_fd_tree *tree, uint64_t root_children)
Definition: fd_tree.c:181
int i
Definition: dir.c:1033
static unsigned depth
Definition: base.c:377
M0_INTERNAL uint64_t m0_rnd(uint64_t max, uint64_t *seed)
Definition: misc.c:115
Definition: cnt.h:36
uint16_t ftc_depth
Definition: fd_internal.h:35
m0_time_t m0_time_now(void)
Definition: time.c:134
void * m0_alloc(size_t size)
Definition: memory.c:126
tree_attr
Definition: common.h:68
M0_INTERNAL void fd_ut_symm_tree_create(struct m0_fd_tree *tree, enum tree_gen_type tg_type, uint64_t *child_nr, uint64_t depth)
Definition: common.c:33
uint64_t ft_cnt
Definition: fd.h:207
static long long min(long long a, long long b)
Definition: crate.c:191
struct m0_fd_tree_node * ft_root
Definition: fd.h:203
M0_INTERNAL int m0_fd__tree_node_init(struct m0_fd_tree *tree, struct m0_fd_tree_node *node, uint16_t child_nr, const struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:31
M0_INTERNAL int fd_ut_tree_level_populate(struct m0_fd_tree *tree, uint64_t max_children, uint16_t level, enum tree_attr ta)
Definition: common.c:69
M0_INTERNAL void m0_fd__tree_node_fini(struct m0_fd_tree *tree, struct m0_fd_tree_node *node)
Definition: fd_tree.c:53
M0_INTERNAL uint64_t fd_ut_random_cnt_get(uint64_t max_cnt)
Definition: common.c:159
Definition: fd.h:199
void m0_free(void *data)
Definition: memory.c:146
M0_INTERNAL int fd_ut_tree_init(struct m0_fd_tree *tree, uint64_t tree_depth)
Definition: common.c:114
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL int m0_fd__tree_cursor_init(struct m0_fd__tree_cursor *cursor, const struct m0_fd_tree *tree, uint16_t depth)
Definition: fd_tree.c:62
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL struct m0_fd_tree_node ** m0_fd__tree_cursor_get(struct m0_fd__tree_cursor *cursor)
Definition: fd_tree.c:136