Motr  M0
sys.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 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
31 #include "lib/trace.h"
32 #include "lib/finject.h"
33 #include "lib/semaphore.h"
34 #include "lib/thread.h" /* m0_thread_tls */
35 #include "lib/misc.h" /* m0_forall, M0_SET0 */
36 #include "ut/ut.h"
37 #include "addb2/sys.h"
38 
39 #include "addb2/ut/common.h"
40 
41 static const struct m0_addb2_config noqueue = {
42  .co_buffer_size = 4096,
43  .co_buffer_min = 0,
44  .co_buffer_max = 1,
45  .co_queue_max = 0,
46  .co_pool_min = 1,
47  .co_pool_max = 1
48 };
49 
50 static const struct m0_addb2_config queue = {
51  .co_buffer_size = 4096,
52  .co_buffer_min = 0,
53  .co_buffer_max = 1,
54  .co_queue_max = 100,
55  .co_pool_min = 1,
56  .co_pool_max = 1
57 };
58 
59 static void init_fini(void)
60 {
61  struct m0_addb2_sys *s;
62  int result;
63 
64  result = m0_addb2_sys_init(&s, &noqueue);
65  M0_UT_ASSERT(result == 0);
66  M0_UT_ASSERT(s != NULL);
68 }
69 
70 static void mach_1(void)
71 {
72  struct m0_addb2_mach *m;
73  struct m0_addb2_sys *s;
74  int result;
75 
76  result = m0_addb2_sys_init(&s, &noqueue);
77  M0_UT_ASSERT(result == 0);
78  M0_UT_ASSERT(s != NULL);
79  m = m0_addb2_sys_get(s);
80  M0_UT_ASSERT(m != NULL);
83 }
84 
85 static void mach_toomany(void)
86 {
87  struct m0_addb2_mach *m0;
88  struct m0_addb2_mach *m1;
89  struct m0_addb2_sys *s;
90  int result;
91 
92  result = m0_addb2_sys_init(&s, &noqueue);
93  M0_UT_ASSERT(result == 0);
94  M0_UT_ASSERT(s != NULL);
96  M0_UT_ASSERT(m0 != NULL);
97  m1 = m0_addb2_sys_get(s);
98  M0_UT_ASSERT(m1 == NULL);
101 }
102 
103 static void mach_cache(void)
104 {
105  struct m0_addb2_mach *m0;
106  struct m0_addb2_mach *m1;
107  struct m0_addb2_sys *s;
108  int result;
109 
110  result = m0_addb2_sys_init(&s, &noqueue);
111  M0_UT_ASSERT(result == 0);
112  M0_UT_ASSERT(s != NULL);
113  m0 = m0_addb2_sys_get(s);
114  M0_UT_ASSERT(m0 != NULL);
116  m1 = m0_addb2_sys_get(s);
117  M0_UT_ASSERT(m1 == m0);
118  m0_addb2_sys_put(s, m1);
120 }
121 
122 enum { N = 17 };
123 
124 static void mach_cache_N(void)
125 {
126  struct m0_addb2_sys *s;
127  struct m0_addb2_config conf = noqueue;
128  struct m0_addb2_mach *m[N];
129  struct m0_addb2_mach *mmm;
130  int i;
131  int result;
132 
133  /* check various pool sizes. */
134  for (i = 0; i < N; ++i) {
135  int j;
136 
137  conf.co_pool_max = i;
138  conf.co_pool_min = i;
139  result = m0_addb2_sys_init(&s, &conf);
140  M0_UT_ASSERT(result == 0);
141  M0_UT_ASSERT(s != NULL);
142  for (j = 0; j < i; ++j) {
143  m[j] = m0_addb2_sys_get(s);
144  M0_UT_ASSERT(m[j] != NULL);
145  M0_UT_ASSERT(m0_forall(k, j, m[k] != m[j]));
146  }
147  mmm = m0_addb2_sys_get(s);
148  M0_UT_ASSERT(mmm == NULL);
149  for (j = 0; j < i; ++j) {
150  int t;
151 
152  m0_addb2_sys_put(s, m[j]);
153  /*
154  * Machines from m[0] to m[j] were released back into
155  * the pool. Reacquire them and check that the same
156  * machines are returned.
157  */
158  for (t = 0; t <= j; ++t) {
159  mmm = m0_addb2_sys_get(s);
160  M0_UT_ASSERT(mmm != NULL);
161  M0_UT_ASSERT(m0_exists(k, j + 1, mmm == m[k]));
162  }
163  mmm = m0_addb2_sys_get(s);
164  M0_UT_ASSERT(mmm == NULL);
165  for (t = 0; t <= j; ++t)
166  m0_addb2_sys_put(s, m[t]);
167  }
169  }
170 }
171 
172 static void _add(const struct m0_addb2_config *conf, unsigned nr)
173 {
174  struct m0_addb2_sys *s;
175  struct m0_addb2_mach *orig;
176  struct m0_addb2_mach *m;
177  struct m0_thread_tls *tls = m0_thread_tls();
178  int i;
179  int result;
180 
181  result = m0_addb2_sys_init(&s, conf);
182  M0_UT_ASSERT(result == 0);
183  M0_UT_ASSERT(s != NULL);
184  m = m0_addb2_sys_get(s);
185  M0_UT_ASSERT(m != NULL);
186  orig = tls->tls_addb2_mach;
187  tls->tls_addb2_mach = m;
188  for (i = 0; i < nr; ++i)
189  M0_ADDB2_ADD(10 + i, 9, 8, 7, 6 + i, 5, 4, 3 - i, 2, 1, 0);
190  tls->tls_addb2_mach = orig;
191  m0_addb2_sys_put(s, m);
193 }
194 
195 static void add_loop(const struct m0_addb2_config *conf)
196 {
197  _add(conf, 1);
198  _add(conf, 10);
199  _add(conf, 100);
200  _add(conf, 1000);
201 }
202 
203 static void noqueue_add(void)
204 {
205  add_loop(&noqueue);
206 }
207 
208 static void queue_add(void)
209 {
210  add_loop(&queue);
211 }
212 
213 extern void (*m0_addb2__sys_submit_trap)(struct m0_addb2_sys *sys,
214  struct m0_addb2_trace_obj *obj);
215 extern void (*m0_addb2__sys_ast_trap)(struct m0_addb2_sys *sys);
216 
217 static unsigned sys_submitted;
218 static void submit_trap(struct m0_addb2_sys *sys,
219  struct m0_addb2_trace_obj *obj)
220 {
221  ++sys_submitted;
222 }
223 
224 static struct m0_semaphore ast_wait;
225 static void ast_trap(struct m0_addb2_sys *sys)
226 {
228 }
229 
230 static void sm_add(void)
231 {
232  struct m0_addb2_sys *s;
233  struct m0_addb2_mach *m;
234  struct m0_addb2_config longqueue = queue;
235  struct m0_addb2_mach *orig;
236  struct m0_thread_tls *tls = m0_thread_tls();
237  int result;
238 
239  longqueue.co_queue_max = 1000000;
243  sys_submitted = 0;
244  m0_fi_enable("sys_submit", "trap");
245  m0_fi_enable("sys_ast", "trap");
246 
247  result = m0_addb2_sys_init(&s, &longqueue);
248  M0_UT_ASSERT(result == 0);
249  M0_UT_ASSERT(s != NULL);
251  m = m0_addb2_sys_get(s);
252  M0_UT_ASSERT(m != NULL);
253  orig = tls->tls_addb2_mach;
254  tls->tls_addb2_mach = m;
255 
256  while (sys_submitted == 0)
257  M0_ADDB2_ADD(1132); /* FW */
258 
261 
262  tls->tls_addb2_mach = orig;
263  m0_addb2_sys_put(s, m);
265 
266  m0_fi_disable("sys_submit", "trap");
267  m0_fi_disable("sys_ast", "trap");
269 }
270 
272  .ts_name = "addb2-sys",
273  .ts_init = NULL,
274  .ts_fini = NULL,
275  .ts_tests = {
276  { "init-fini", &init_fini, "Nikita" },
277  { "mach-1", &mach_1, "Nikita" },
278  { "mach-toomany", &mach_toomany, "Nikita" },
279  { "mach-cache", &mach_cache, "Nikita" },
280  { "mach-cache-N", &mach_cache_N, "Nikita" },
281  { "noqueue-add", &noqueue_add, "Nikita" },
282  { "queue-add", &queue_add, "Nikita" },
283  { "sm-add", &sm_add, "Nikita" },
284  { NULL, NULL }
285  }
286 };
287 
288 #undef M0_TRACE_SUBSYSTEM
289 
292 /*
293  * Local variables:
294  * c-indentation-style: "K&R"
295  * c-basic-offset: 8
296  * tab-width: 8
297  * fill-column: 80
298  * scroll-step: 1
299  * End:
300  */
301 /*
302  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
303  */
static const struct m0_addb2_config queue
Definition: sys.c:50
static void ast_trap(struct m0_addb2_sys *sys)
Definition: sys.c:225
static size_t nr
Definition: dump.c:1505
#define NULL
Definition: misc.h:38
static struct m0_addb2_mach * m
Definition: consumer.c:38
void m0_addb2_sys_sm_start(struct m0_addb2_sys *sys)
Definition: sys.c:377
struct m0_addb2_mach * tls_addb2_mach
Definition: thread.h:68
Definition: conf.py:1
#define m0_exists(var, nr,...)
Definition: misc.h:134
M0_ADDB2_ADD(M0_AVI_FS_CREATE, new_fid.f_container, new_fid.f_key, mode, rc)
Definition: ut.h:77
static struct foo * obj
Definition: tlist.c:302
void(* m0_addb2__sys_submit_trap)(struct m0_addb2_sys *sys, struct m0_addb2_trace_obj *obj)
Definition: sys.c:469
M0_INTERNAL struct m0_thread_tls * m0_thread_tls(void)
Definition: kthread.c:67
Definition: beck.c:80
int i
Definition: dir.c:1033
static unsigned sys_submitted
Definition: sys.c:217
void m0_addb2_sys_fini(struct m0_addb2_sys *sys)
Definition: sys.c:214
void(* m0_addb2__sys_ast_trap)(struct m0_addb2_sys *sys)
Definition: sys.c:545
M0_INTERNAL void m0_fi_disable(const char *fp_func, const char *fp_tag)
Definition: finject.c:485
static void m0_fi_enable(const char *func, const char *tag)
Definition: finject.h:276
int m0_addb2_sys_init(struct m0_addb2_sys **out, const struct m0_addb2_config *conf)
Definition: sys.c:176
static void mach_1(void)
Definition: sys.c:70
static struct m0_thread t[8]
Definition: service_ut.c:1230
static void mach_cache(void)
Definition: sys.c:103
static void _add(const struct m0_addb2_config *conf, unsigned nr)
Definition: sys.c:172
Definition: instance.h:80
unsigned co_queue_max
Definition: sys.h:118
M0_INTERNAL int m0_semaphore_init(struct m0_semaphore *semaphore, unsigned value)
Definition: semaphore.c:38
static __thread struct m0_thread_tls * tls
Definition: uthread.c:66
static const struct m0_addb2_config noqueue
Definition: sys.c:41
static void submit_trap(struct m0_addb2_sys *sys, struct m0_addb2_trace_obj *obj)
Definition: sys.c:218
static void sm_add(void)
Definition: sys.c:230
static struct m0_semaphore ast_wait
Definition: sys.c:224
#define m0_forall(var, nr,...)
Definition: misc.h:112
const char * ts_name
Definition: ut.h:99
struct m0_addb2_mach * m0_addb2_sys_get(struct m0_addb2_sys *sys)
Definition: sys.c:272
M0_INTERNAL void m0_semaphore_fini(struct m0_semaphore *semaphore)
Definition: semaphore.c:45
struct m0_ut_suite addb2_sys_ut
Definition: sys.c:271
static void add_loop(const struct m0_addb2_config *conf)
Definition: sys.c:195
static void queue_add(void)
Definition: sys.c:208
static void mach_toomany(void)
Definition: sys.c:85
void m0_addb2_sys_put(struct m0_addb2_sys *sys, struct m0_addb2_mach *m)
Definition: sys.c:295
M0_INTERNAL void m0_semaphore_down(struct m0_semaphore *semaphore)
Definition: semaphore.c:49
M0_INTERNAL void m0_semaphore_up(struct m0_semaphore *semaphore)
Definition: semaphore.c:65
static void init_fini(void)
Definition: sys.c:59
static void noqueue_add(void)
Definition: sys.c:203
unsigned co_buffer_size
Definition: sys.h:103
static struct m0_addb2_source * s
Definition: consumer.c:39
Definition: sys.c:122
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static void mach_cache_N(void)
Definition: sys.c:124