Motr  M0
common.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_UT
24 #include "lib/trace.h"
25 
26 #include "conf/ut/common.h"
27 #include "lib/fs.h" /* m0_file_read */
28 #include "rpc/rpclib.h" /* m0_rpc_server_ctx */
29 #include "ut/ut.h"
30 
33 
34 /* Filters out intermediate state transitions of m0_confc_ctx::fc_mach. */
35 static bool _filter(struct m0_clink *link)
36 {
37  struct m0_conf_ut_waiter *waiter = M0_AMB(waiter, link, w_clink);
38  return !m0_confc_ctx_is_completed(&waiter->w_ctx);
39 }
40 
41 M0_INTERNAL void m0_conf_ut_waiter_init(struct m0_conf_ut_waiter *w,
42  struct m0_confc *confc)
43 {
47 }
48 
49 M0_INTERNAL void m0_conf_ut_waiter_fini(struct m0_conf_ut_waiter *w)
50 {
54 }
55 
56 M0_INTERNAL int m0_conf_ut_waiter_wait(struct m0_conf_ut_waiter *w,
57  struct m0_conf_obj **result)
58 {
59  int rc;
60 
62  m0_chan_wait(&w->w_clink);
63 
65  if (rc == 0 && result != NULL)
66  *result = m0_confc_ctx_result(&w->w_ctx);
67 
68  return rc;
69 }
70 
71 static struct conf_ut_ast {
72  bool run;
73  struct m0_thread thread;
74 } g_ast;
75 
76 static void conf_ut_ast_thread(int _ M0_UNUSED)
77 {
78  while (g_ast.run) {
83  }
84 }
85 
86 M0_INTERNAL int m0_conf_ut_ast_thread_init(void)
87 {
89  M0_SET0(&g_ast);
91  g_ast.run = true;
93  "ast_thread");
94 }
95 
96 M0_INTERNAL int m0_conf_ut_ast_thread_fini(void)
97 {
98  g_ast.run = false;
102  return 0;
103 }
104 
105 static struct m0_mutex conf_ut_lock;
106 
107 M0_INTERNAL int m0_conf_ut_cache_init(void)
108 {
111  return 0;
112 }
113 
114 M0_INTERNAL int m0_conf_ut_cache_fini(void)
115 {
118  return 0;
119 }
120 
121 #ifndef __KERNEL__
122 M0_INTERNAL void
124 {
125  char *confstr = NULL;
126  int rc;
127 
128  M0_PRE(path != NULL && *path != '\0');
129 
130  rc = m0_file_read(path, &confstr);
131  M0_UT_ASSERT(rc == 0);
132 
134  m0_conf_cache_clean(cache, NULL); /* start from scratch */
135  rc = m0_conf_cache_from_string(cache, confstr);
136  M0_UT_ASSERT(rc == 0);
138 
139  m0_free(confstr);
140 }
141 #endif /* !__KERNEL__ */
142 
143 #undef M0_TRACE_SUBSYSTEM
Definition: beck.c:235
M0_INTERNAL void m0_chan_wait(struct m0_clink *link)
Definition: chan.c:336
M0_INTERNAL int m0_conf_ut_cache_init(void)
Definition: common.c:107
#define M0_PRE(cond)
static struct m0_mutex conf_ut_lock
Definition: common.c:105
#define NULL
Definition: misc.h:38
M0_INTERNAL void m0_clink_init(struct m0_clink *link, m0_chan_cb_t cb)
Definition: chan.c:201
M0_INTERNAL void m0_clink_del_lock(struct m0_clink *link)
Definition: chan.c:293
int m0_thread_join(struct m0_thread *q)
Definition: kthread.c:169
M0_INTERNAL struct m0_conf_obj * m0_confc_ctx_result(struct m0_confc_ctx *ctx)
Definition: confc.c:771
bool run
Definition: common.c:72
struct m0_sm fc_mach
Definition: confc.h:538
M0_INTERNAL int m0_file_read(const char *path, char **out)
Definition: fs.c:61
M0_INTERNAL void m0_conf_ut_waiter_fini(struct m0_conf_ut_waiter *w)
Definition: common.c:49
static struct conf_ut_ast g_ast
struct m0_clink s_clink
Definition: sm.h:516
M0_INTERNAL int m0_conf_ut_waiter_wait(struct m0_conf_ut_waiter *w, struct m0_conf_obj **result)
Definition: common.c:56
M0_INTERNAL int32_t m0_confc_ctx_error_lock(const struct m0_confc_ctx *ctx)
Definition: confc.c:765
#define M0_THREAD_INIT(thread, TYPE, init, func, arg, namefmt,...)
Definition: thread.h:139
#define M0_SET0(obj)
Definition: misc.h:64
M0_INTERNAL void m0_sm_group_fini(struct m0_sm_group *grp)
Definition: sm.c:65
static void conf_ut_ast_thread(int _ M0_UNUSED)
Definition: common.c:76
M0_INTERNAL void m0_sm_group_unlock(struct m0_sm_group *grp)
Definition: sm.c:96
struct m0_confc_ctx w_ctx
Definition: common.h:38
struct m0_conf_cache m0_conf_ut_cache
Definition: common.c:31
struct m0_clink w_clink
Definition: common.h:39
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
M0_INTERNAL void m0_confc_ctx_fini(struct m0_confc_ctx *ctx)
Definition: confc.c:716
M0_INTERNAL int m0_conf_ut_ast_thread_fini(void)
Definition: common.c:96
static struct m0_confc * confc
Definition: file.c:94
M0_INTERNAL void m0_sm_group_init(struct m0_sm_group *grp)
Definition: sm.c:53
M0_INTERNAL bool m0_confc_ctx_is_completed(const struct m0_confc_ctx *ctx)
Definition: confc.c:742
M0_INTERNAL void m0_conf_cache_init(struct m0_conf_cache *cache, struct m0_mutex *lock)
Definition: cache.c:66
M0_INTERNAL int m0_confc_ctx_init(struct m0_confc_ctx *ctx, struct m0_confc *confc)
Definition: confc.c:643
M0_INTERNAL int m0_conf_ut_cache_fini(void)
Definition: common.c:114
M0_INTERNAL bool m0_confc_ctx_is_completed_lock(const struct m0_confc_ctx *ctx)
Definition: confc.c:749
M0_INTERNAL void m0_clink_signal(struct m0_clink *clink)
Definition: chan.c:326
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
static bool _filter(struct m0_clink *link)
Definition: common.c:35
struct m0_sm_group m0_conf_ut_grp
Definition: common.c:32
M0_INTERNAL int m0_conf_ut_ast_thread_init(void)
Definition: common.c:86
void m0_clink_add_lock(struct m0_chan *chan, struct m0_clink *link)
Definition: chan.c:255
struct m0_thread thread
Definition: common.c:73
M0_INTERNAL void m0_conf_ut_cache_from_file(struct m0_conf_cache *cache, const char *path)
Definition: common.c:123
struct m0_chan sm_chan
Definition: sm.h:331
M0_INTERNAL void m0_conf_ut_waiter_init(struct m0_conf_ut_waiter *w, struct m0_confc *confc)
Definition: common.c:41
M0_INTERNAL int m0_conf_cache_from_string(struct m0_conf_cache *cache, const char *str)
Definition: cache.c:278
M0_INTERNAL void m0_mutex_fini(struct m0_mutex *mutex)
Definition: mutex.c:42
M0_INTERNAL void m0_clink_fini(struct m0_clink *link)
Definition: chan.c:208
M0_INTERNAL void m0_sm_group_lock(struct m0_sm_group *grp)
Definition: sm.c:83
M0_INTERNAL void m0_conf_cache_clean(struct m0_conf_cache *cache, const struct m0_conf_obj_type *type)
Definition: cache.c:168
M0_INTERNAL void m0_conf_cache_fini(struct m0_conf_cache *cache)
Definition: cache.c:183
M0_INTERNAL void m0_sm_asts_run(struct m0_sm_group *grp)
Definition: sm.c:150
M0_INTERNAL void m0_conf_cache_lock(struct m0_conf_cache *cache)
Definition: cache.c:50
void m0_free(void *data)
Definition: memory.c:146
Definition: mutex.h:47
int32_t rc
Definition: trigger_fop.h:47
#define M0_UT_ASSERT(a)
Definition: ut.h:46
M0_INTERNAL void m0_conf_cache_unlock(struct m0_conf_cache *cache)
Definition: cache.c:55
#define M0_UNUSED
Definition: misc.h:380