Motr  M0
common.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 "ut/ut.h"
25 #include "lib/finject.h"
26 #include "lib/thread.h"
27 #include "addb2/addb2.h"
28 #include "addb2/ut/common.h"
29 
30 int submitted = 0;
31 
32 int (*submit)(const struct m0_addb2_mach *mach,
33  struct m0_addb2_trace *trace);
34 void (*idle)(const struct m0_addb2_mach *mach);
35 
36 static int test_submit(struct m0_addb2_mach *mach,
37  struct m0_addb2_trace_obj *obj)
38 {
39  return (*submit)(mach, &obj->o_tr);
40 }
41 
42 static void test_idle(struct m0_addb2_mach *mach)
43 {
44  if (idle != NULL)
45  (*idle)(mach);
46 }
47 
48 static const struct m0_addb2_mach_ops test_mach_ops = {
50  .apo_idle = &test_idle
51 };
52 
53 extern struct m0_addb2_mach *(*m0_addb2__mach)(void);
54 static struct m0_thread *main_thread;
55 static struct m0_addb2_mach *__mach;
56 
57 static struct m0_addb2_mach *getmach(void)
58 {
59  if (m0_thread_self() == main_thread)
60  return __mach;
61  else
62  return m0_thread_tls()->tls_addb2_mach;
63 }
64 
65 struct m0_addb2_mach *mach_set(int (*s)(const struct m0_addb2_mach *,
66  struct m0_addb2_trace *))
67 {
68  submitted = 0;
69  submit = s;
74  m0_fi_enable("mach", "surrogate-mach");
75  return __mach;
76 }
77 
78 void mach_fini(struct m0_addb2_mach *m)
79 {
80  m0_fi_disable("mach", "surrogate-mach");
81  __mach = NULL;
84  submit = NULL;
85  idle = NULL;
86  submitted = 0;
87 }
88 
89 void mach_put(struct m0_addb2_mach *m)
90 {
92  mach_fini(m);
93 }
94 
95 
96 const uint64_t SENSOR_MARKER = 0x5555555555555555;
97 uint64_t seq = 0;
99 
100 static void snapshot(struct m0_addb2_sensor *s, uint64_t *area)
101 {
102  area[0] = SENSOR_MARKER;
103  area[1] = ++seq;
104 }
105 
106 static void sensor_fini(struct m0_addb2_sensor *s)
107 {
109  sensor_finalised = true;
110 }
111 
113  .so_snapshot = &snapshot,
114  .so_fini = &sensor_fini
115 };
116 
118 {
119  unsigned sofar = submitted;
120  int issued;
121 
122  for (issued = 0; submitted == sofar; ++ issued)
123  M0_ADDB2_ADD(42, 1, 2, 3, 4, 5, 6);
124  return issued;
125 }
126 
127 bool valeq(const struct m0_addb2_value *v0, const struct m0_addb2_value *v1)
128 {
129  return v0->va_id == v1->va_id && v0->va_nr == v1->va_nr &&
130  memcmp(v0->va_data, v1->va_data,
131  v0->va_nr * sizeof v0->va_data[0]) == 0;
132 }
133 
134 bool receq(const struct m0_addb2_record *r0, const struct small_record *r1)
135 {
136  return valeq(&r0->ar_val, &r1->ar_val) &&
137  r0->ar_label_nr == r1->ar_label_nr &&
138  m0_forall(i, r0->ar_label_nr,
139  valeq(&r0->ar_label[i], &r1->ar_label[i]));
140 }
141 
142 #undef M0_TRACE_SUBSYSTEM
143 
144 /*
145  * Local variables:
146  * c-indentation-style: "K&R"
147  * c-basic-offset: 8
148  * tab-width: 8
149  * fill-column: 80
150  * scroll-step: 1
151  * End:
152  */
void mach_fini(struct m0_addb2_mach *m)
Definition: common.c:78
static void test_idle(struct m0_addb2_mach *mach)
Definition: common.c:42
#define NULL
Definition: misc.h:38
static struct m0_addb2_mach * m
Definition: consumer.c:38
struct m0_addb2_mach * tls_addb2_mach
Definition: thread.h:68
unsigned va_nr
Definition: consumer.h:105
struct m0_addb2_value ar_label[4]
Definition: common.h:59
void m0_addb2_mach_fini(struct m0_addb2_mach *mach)
Definition: addb2.c:560
bool sensor_finalised
Definition: common.c:98
uint64_t va_id
Definition: consumer.h:103
static struct m0_addb2_mach * mach
Definition: storage.c:42
unsigned ar_label_nr
Definition: common.h:58
unsigned ar_label_nr
Definition: consumer.h:116
static int test_submit(struct m0_addb2_mach *mach, struct m0_addb2_trace_obj *obj)
Definition: common.c:36
M0_ADDB2_ADD(M0_AVI_FS_CREATE, new_fid.f_container, new_fid.f_key, mode, rc)
static struct foo * obj
Definition: tlist.c:302
int submitted
Definition: common.c:30
int(* submit)(const struct m0_addb2_mach *mach, struct m0_addb2_trace *trace)
Definition: common.c:32
M0_INTERNAL struct m0_thread_tls * m0_thread_tls(void)
Definition: kthread.c:67
void mach_put(struct m0_addb2_mach *m)
Definition: common.c:89
int i
Definition: dir.c:1033
static struct m0_addb2_mach * __mach
Definition: common.c:55
static void snapshot(struct m0_addb2_sensor *s, uint64_t *area)
Definition: common.c:100
void(* idle)(const struct m0_addb2_mach *mach)
Definition: common.c:34
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
const uint64_t SENSOR_MARKER
Definition: common.c:96
static const struct m0_addb2_mach_ops test_mach_ops
Definition: common.c:48
void m0_addb2_mach_stop(struct m0_addb2_mach *mach)
Definition: addb2.c:630
struct m0_addb2_value ar_label[M0_ADDB2_LABEL_MAX]
Definition: consumer.h:118
struct m0_addb2_mach *(* m0_addb2__mach)(void)
Definition: addb2.c:873
bool receq(const struct m0_addb2_record *r0, const struct small_record *r1)
Definition: common.c:134
int(* apo_submit)(struct m0_addb2_mach *mach, struct m0_addb2_trace_obj *tobj)
Definition: addb2.h:368
struct m0_addb2_mach * mach_set(int(*s)(const struct m0_addb2_mach *, struct m0_addb2_trace *))
Definition: common.c:65
#define m0_forall(var, nr,...)
Definition: misc.h:112
struct m0_addb2_value ar_val
Definition: common.h:57
struct m0_addb2_value ar_val
Definition: consumer.h:114
const uint64_t * va_data
Definition: consumer.h:106
void(* so_snapshot)(struct m0_addb2_sensor *s, uint64_t *area)
Definition: addb2.h:298
M0_INTERNAL struct m0_thread * m0_thread_self(void)
Definition: thread.c:122
static struct m0_addb2_mach * getmach(void)
Definition: common.c:57
const struct m0_addb2_sensor_ops sensor_ops
Definition: common.c:112
bool valeq(const struct m0_addb2_value *v0, const struct m0_addb2_value *v1)
Definition: common.c:127
static void sensor_fini(struct m0_addb2_sensor *s)
Definition: common.c:106
int fill_one(struct m0_addb2_mach *m)
Definition: common.c:117
static unsigned issued
Definition: storage.c:262
static struct m0_addb2_source * s
Definition: consumer.c:39
#define M0_UT_ASSERT(a)
Definition: ut.h:46
uint64_t seq
Definition: common.c:97
static struct m0_thread * main_thread
Definition: common.c:54
struct m0_addb2_mach * m0_addb2_mach_init(const struct m0_addb2_mach_ops *ops, void *cookie)
Definition: addb2.c:521