Motr  M0
base.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2015-2020 Seagate Technology LLC and/or its Affiliates
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * For any questions about this software or licensing,
17  * please email opensource@seagate.com or cortx-questions@seagate.com.
18  *
19  */
20 
21 
22 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
23 
24 #include "lib/trace.h"
25 #include "ut/ut.h"
26 #include "addb2/addb2.h"
27 
28 #include "addb2/ut/common.h"
29 
30 static const struct m0_addb2_mach_ops null_ops = {
31 };
32 
38 static void init_fini(void)
39 {
40  struct m0_addb2_mach *m;
41 
43  M0_UT_ASSERT(m != NULL);
45 }
46 
47 enum {
48  SKIPME = 0xbebebebebe4ebebe,
49  END = 0xbcbcbcbcbcbcb7bc
50 };
51 
52 static bool trace_eq(const struct m0_addb2_trace *t0,
53  const struct m0_addb2_trace *t1)
54 {
55  return t0->tr_nr == t1->tr_nr &&
56  m0_forall(i, t1->tr_nr,
57  t0->tr_body[i] == t1->tr_body[i] ||
58  t0->tr_body[i] == SKIPME);
59 }
60 
61 enum {
62  LABEL_ID_0 = 0x17,
63 };
64 
65 const uint64_t payload[] = {
66  0x7472756374206d30,
67  0x5f61646462325f74,
68  0x726163652073686f,
69  0x756c646265203d20,
70  0x7b0a09092e74725f,
71  0x6e72203d20332c0a,
72  0x09092e74725f626f,
73  0x6479203d20287569,
74  0x3131303030303030,
75  0x303030303030207c,
76  0x204c4142454c5f49,
77  0x445f302c202f2a20,
78  0x505553482031372c,
79  0x2031202a2f0a0909,
80  0x094c4142454c5f56,
81  0x414c55455f302c20,
82  0x2020202020202020,
83  0x2020202020202020,
84  0x2f2a207061796c6f,
85  0x6164202a2f0a0909,
86  0x0930783066303030,
87  0x3030303030303030
88 };
89 
90 static struct m0_addb2_trace *shouldbe;
91 
92 static int check_submit(const struct m0_addb2_mach *mach,
93  struct m0_addb2_trace *trace)
94 {
95  int nr;
96 
97  for (nr = 0; shouldbe->tr_body[nr] != END; ++nr)
98  ;
99  shouldbe->tr_nr = nr;
100  M0_UT_ASSERT(trace_eq(shouldbe, trace));
101  M0_UT_ASSERT(submitted == 0);
102  ++ submitted;
103  return 0;
104 }
105 
111 static void push_pop(void)
112 {
113  struct m0_addb2_mach *m;
114 
115  shouldbe = &(struct m0_addb2_trace) {
116  .tr_body = (uint64_t[]){
117  0x1100000000000000 | LABEL_ID_0, /* PUSH 17, 1 */
118  SKIPME, /* time-stamp */
119  payload[0], /* payload */
120  0xf000000000000000 | LABEL_ID_0, /* POP */
121  SKIPME, /* time-stamp */
122  END
123  }
124  };
125 
126  m = mach_set(&check_submit);
129  mach_put(m);
130 }
131 
138 static void push0_pop(void)
139 {
140  struct m0_addb2_mach *m;
141 
142  shouldbe = &(struct m0_addb2_trace) {
143  .tr_body = (uint64_t[]){
144  0x1000000000000000 | LABEL_ID_0, /* PUSH 17, 0 */
145  SKIPME, /* time-stamp */
146  0xf000000000000000 | LABEL_ID_0, /* POP */
147  SKIPME, /* time-stamp */
148  END
149  }
150  };
151 
152  m = mach_set(&check_submit);
155  mach_put(m);
156 }
157 
163 static void push5_pop(void)
164 {
165  struct m0_addb2_mach *m;
166 
167  shouldbe = &(struct m0_addb2_trace) {
168  .tr_body = (uint64_t[]){
169  0x1500000000000000 | LABEL_ID_0, /* PUSH 17, 5 */
170  SKIPME, /* time-stamp */
171  payload[3],
172  payload[4],
173  payload[5],
174  payload[6],
175  payload[7],
176  0xf000000000000000 | LABEL_ID_0, /* POP */
177  SKIPME, /* time-stamp */
178  END
179  }
180  };
181 
182  m = mach_set(&check_submit);
185  mach_put(m);
186 }
187 
193 static void pushN_popN(void)
194 {
195  struct m0_addb2_mach *m;
196 
197  shouldbe = &(struct m0_addb2_trace) {
198  .tr_body = (uint64_t[]){
199  0x1100000000000000 | (LABEL_ID_0 + 0), /* PUSH 17, 5 */
200  SKIPME, /* time-stamp */
201  payload[2],
202  0x1000000000000000 | (LABEL_ID_0 + 2), /* PUSH 19, 0 */
203  SKIPME, /* time-stamp */
204  0x1300000000000000 | (LABEL_ID_0 + 3), /* PUSH 1a, 3 */
205  SKIPME, /* time-stamp */
206  payload[0],
207  payload[1],
208  payload[2],
209  0xf000000000000000 | (LABEL_ID_0 + 3), /* POP */
210  SKIPME, /* time-stamp */
211  0xf000000000000000 | (LABEL_ID_0 + 2), /* POP */
212  SKIPME, /* time-stamp */
213  0xf000000000000000 | (LABEL_ID_0 + 0), /* POP */
214  SKIPME, /* time-stamp */
215  END
216  }
217  };
218 
219  m = mach_set(&check_submit);
220  m0_addb2_push(LABEL_ID_0 + 0, 1, payload + 2);
221  m0_addb2_push(LABEL_ID_0 + 2, 0, NULL);
226  mach_put(m);
227 }
228 
233 static void add(void)
234 {
235  struct m0_addb2_mach *m;
236 
237  shouldbe = &(struct m0_addb2_trace) {
238  .tr_body = (uint64_t[]){
239  0x2100000000000000 | LABEL_ID_0, /* DATA 17, 1 */
240  SKIPME, /* time-stamp */
241  payload[0], /* payload */
242  END
243  }
244  };
245 
246  m = mach_set(&check_submit);
248  mach_put(m);
249 }
250 
255 static void add_var(void)
256 {
257  struct m0_addb2_mach *m;
258  int i;
259 
260  shouldbe = &(struct m0_addb2_trace) {
261  .tr_body = (uint64_t[]){
262  0x1100000000000000 | (LABEL_ID_0 + 0), /* PUSH 17, 1 */
263  SKIPME, /* time-stamp */
264  payload[0],
265  0x1100000000000000 | (LABEL_ID_0 + 1), /* PUSH 18, 1 */
266  SKIPME, /* time-stamp */
267  payload[1],
268  0x2000000000000000 | (LABEL_ID_0 + 0), /* DATA 17, 0 */
269  SKIPME, /* time-stamp */
270  0x2100000000000000 | (LABEL_ID_0 + 1), /* DATA 18, 1 */
271  SKIPME, /* time-stamp */
272  payload[1],
273  0x2200000000000000 | (LABEL_ID_0 + 2), /* DATA 19, 2 */
274  SKIPME, /* time-stamp */
275  payload[2],
276  payload[3],
277  0x2300000000000000 | (LABEL_ID_0 + 3), /* DATA 1a, 3 */
278  SKIPME, /* time-stamp */
279  payload[3],
280  payload[4],
281  payload[5],
282  0xf000000000000000 | (LABEL_ID_0 + 1), /* POP */
283  SKIPME, /* time-stamp */
284  0xf000000000000000 | (LABEL_ID_0 + 0), /* POP */
285  SKIPME, /* time-stamp */
286  END
287  }
288  };
289 
290  m = mach_set(&check_submit);
292  m0_addb2_push(LABEL_ID_0 + 1, 1, payload + 1);
293  for (i = 0; i < 4; ++i)
297  mach_put(m);
298 }
299 
300 static int added;
301 static bool enough;
302 static int total;
303 static int keep;
304 static int used;
305 static const struct m0_addb2_trace *busy[1000];
306 
307 static int full_submit(const struct m0_addb2_mach *mach,
308  struct m0_addb2_trace *trace)
309 {
310  int i;
311 
312  ++ submitted;
313  M0_UT_ASSERT(trace->tr_nr % 3 == 0);
314  for (i = 0; i < trace->tr_nr; i += 3) {
315  M0_UT_ASSERT(trace->tr_body[i] ==
316  (0x2100000000000000 | (total + i / 3)));
317  /* trace->tr_body[i + 1] is the time-stamp. */
318  M0_UT_ASSERT(trace->tr_body[i + 2] == payload[0]);
319  }
320  total += trace->tr_nr / 3;
322  enough = (submitted > 10);
323  if (keep) {
325  busy[used++] = trace;
326  }
327  return keep;
328 }
329 
334 static void full(void)
335 {
336  struct m0_addb2_mach *m;
337 
338  m = mach_set(&full_submit);
339  total = 0;
340  keep = 0;
341  for (enough = false, added = 0; !enough; ++added)
343  mach_put(m);
344 }
345 
346 static bool idled = false;
347 
348 void idle_idle(const struct m0_addb2_mach *mach)
349 {
350  M0_UT_ASSERT(used == 0);
351  idled = true;
352 }
353 
357 static void stop_idle(void)
358 {
359  struct m0_addb2_mach *m;
360 
361  idle = &idle_idle;
362  m = mach_set(&full_submit);
363  total = 0;
364  keep = +1;
365  used = 0;
366  for (enough = false, added = 0; !enough; ++added)
368  M0_UT_ASSERT(used > 0);
370  while (used > 0)
373  mach_put(m);
374 }
375 
376 static uint64_t found;
377 static unsigned depth;
378 
379 static int sensor_submit(const struct m0_addb2_mach *mach,
380  struct m0_addb2_trace *trace)
381 {
382  int i;
383 
384  ++ submitted;
385  for (i = 0; i < depth; ++ i)
386  M0_UT_ASSERT(trace->tr_body[2 * i] == /* PUSH 17 + i, 0 */
387  (0x1000000000000000 | (LABEL_ID_0 + i)));
388  M0_UT_ASSERT(trace->tr_body[2 * i] != /* PUSH 17 + i, 0 */
389  (0x1000000000000000 | (LABEL_ID_0 + i)));
390  for (; i < trace->tr_nr; ++ i) {
391  if (trace->tr_body[i] == SENSOR_MARKER)
392  found = trace->tr_body[i + 1];
393  }
394  return 0;
395 }
396 
402 static void sensor_depth(void)
403 {
404  struct m0_addb2_mach *m;
405  struct m0_addb2_sensor s;
406 
407  seq = 1;
408  sensor_finalised = false;
409  found = 0;
410  submitted = 0;
411 
412  M0_SET0(&s);
414  for (depth = 0; depth < M0_ADDB2_LABEL_MAX; ++depth)
416  m0_addb2_sensor_add(&s, LABEL_ID_0 + 5, 2, -1, &sensor_ops);
417  fill_one(m);
418 
419  while (depth > 0) {
422 
423  fill_one(m);
424  -- depth;
425  fill_one(m);
426  }
427  mach_put(m);
428  M0_UT_ASSERT(found == seq);
429 }
430 
432  .ts_name = "addb2-base",
433  .ts_init = NULL,
434  .ts_fini = NULL,
435  .ts_tests = {
436  { "init-fini", &init_fini },
437  { "push-pop", &push_pop },
438  { "push0-pop", &push0_pop },
439  { "push5-pop", &push5_pop },
440  { "push^N-pop^N", &pushN_popN },
441  { "add", &add },
442  { "add-var", &add_var },
443  { "full", &full },
444  { "stop-idle", &stop_idle },
445  { "sensor-depth", &sensor_depth },
446  { NULL, NULL }
447  }
448 };
449 
450 #undef M0_TRACE_SUBSYSTEM
451 
452 /*
453  * Local variables:
454  * c-indentation-style: "K&R"
455  * c-basic-offset: 8
456  * tab-width: 8
457  * fill-column: 80
458  * scroll-step: 1
459  * End:
460  */
static size_t nr
Definition: dump.c:1505
#define NULL
Definition: misc.h:38
static struct m0_addb2_mach * m
Definition: consumer.c:38
static void sensor_depth(void)
Definition: base.c:402
static void full(void)
Definition: base.c:334
void m0_addb2_mach_fini(struct m0_addb2_mach *mach)
Definition: addb2.c:560
bool sensor_finalised
Definition: common.c:98
static void push0_pop(void)
Definition: base.c:138
static struct m0_addb2_mach * mach
Definition: storage.c:42
static void pushN_popN(void)
Definition: base.c:193
static bool enough
Definition: base.c:301
#define M0_SET0(obj)
Definition: misc.h:64
Definition: ut.h:77
static const struct m0_addb2_trace * busy[1000]
Definition: base.c:305
static bool idled
Definition: base.c:346
int submitted
Definition: common.c:30
void mach_put(struct m0_addb2_mach *m)
Definition: common.c:89
int i
Definition: dir.c:1033
static unsigned depth
Definition: base.c:377
void m0_addb2_trace_done(const struct m0_addb2_trace *ctrace)
Definition: addb2.c:650
static int sensor_submit(const struct m0_addb2_mach *mach, struct m0_addb2_trace *trace)
Definition: base.c:379
void m0_addb2_push(uint64_t id, int n, const uint64_t *value)
Definition: addb2.c:412
static const struct m0_addb2_mach_ops null_ops
Definition: base.c:30
void(* idle)(const struct m0_addb2_mach *mach)
Definition: common.c:34
static bool trace_eq(const struct m0_addb2_trace *t0, const struct m0_addb2_trace *t1)
Definition: base.c:52
static void push5_pop(void)
Definition: base.c:163
const uint64_t SENSOR_MARKER
Definition: common.c:96
Definition: base.c:49
void m0_addb2_mach_stop(struct m0_addb2_mach *mach)
Definition: addb2.c:630
static int keep
Definition: base.c:303
static void add_var(void)
Definition: base.c:255
void m0_addb2_sensor_add(struct m0_addb2_sensor *s, uint64_t id, unsigned nr, int idx, const struct m0_addb2_sensor_ops *ops)
Definition: addb2.c:480
Definition: base.c:48
static void push_pop(void)
Definition: base.c:111
static void t0(int self)
Definition: chan.c:39
void m0_addb2_add(uint64_t id, int n, const uint64_t *value)
Definition: addb2.c:466
static int added
Definition: base.c:300
static int full_submit(const struct m0_addb2_mach *mach, struct m0_addb2_trace *trace)
Definition: base.c:307
struct m0_addb2_mach * mach_set(int(*s)(const struct m0_addb2_mach *, struct m0_addb2_trace *))
Definition: common.c:65
void m0_addb2_pop(uint64_t id)
Definition: addb2.c:440
static int used
Definition: base.c:304
static void init_fini(void)
Definition: base.c:38
#define m0_forall(var, nr,...)
Definition: misc.h:112
const char * ts_name
Definition: ut.h:99
static const struct m0_addb2_sensor_ops sensor_ops
Definition: counter.c:41
static void stop_idle(void)
Definition: base.c:357
static void t1(int n)
Definition: mutex.c:48
static uint64_t found
Definition: base.c:376
static int total
Definition: base.c:302
static int check_submit(const struct m0_addb2_mach *mach, struct m0_addb2_trace *trace)
Definition: base.c:92
static struct m0_addb2_trace * shouldbe
Definition: base.c:90
uint64_t tr_nr
Definition: addb2.h:442
int fill_one(struct m0_addb2_mach *m)
Definition: common.c:117
static struct m0_addb2_source * s
Definition: consumer.c:39
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
uint64_t seq
Definition: common.c:97
void idle_idle(const struct m0_addb2_mach *mach)
Definition: base.c:348
static void add(void)
Definition: base.c:233
struct m0_addb2_mach * m0_addb2_mach_init(const struct m0_addb2_mach_ops *ops, void *cookie)
Definition: addb2.c:521
const uint64_t payload[]
Definition: base.c:65
struct m0_ut_suite addb2_base_ut
Definition: base.c:431
uint64_t * tr_body
Definition: addb2.h:443