Motr  M0
stats_fom.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013-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 #include "lib/memory.h"
23 #include "lib/misc.h"
24 
25 #include "fop/ut/stats/stats_fom.h"
26 
27 static struct m0_chan chan;
28 static struct m0_mutex mutex;
29 static struct m0_clink clink;
30 
31 static struct m0_sm_state_descr phases[] = {
32  [PH_INIT] = {
34  .sd_name = "Init",
35  .sd_allowed = M0_BITS(PH_RUN, PH_FINISH)
36  },
37  [PH_RUN] = {
38  .sd_name = "fom_run",
39  .sd_allowed = M0_BITS(PH_FINISH)
40  },
41  [PH_FINISH] = {
42  .sd_flags = M0_SDF_TERMINAL,
43  .sd_name = "SM finish",
44  }
45 };
46 
47 static struct m0_sm_trans_descr trans[] = {
48  { "Start", PH_INIT, PH_RUN },
49  { "Failed", PH_INIT, PH_FINISH },
50  { "Stop", PH_RUN, PH_FINISH }
51 };
52 
53 static struct m0_sm_conf fom_phases_conf = {
54  .scf_name = "FOM phases",
55  .scf_nr_states = ARRAY_SIZE(phases),
56  .scf_state = phases,
57  .scf_trans_nr = ARRAY_SIZE(trans),
58  .scf_trans = trans
59 };
60 
61 static size_t fom_stats_home_locality(const struct m0_fom *fom);
62 static void fop_stats_fom_fini(struct m0_fom *fom);
63 static int fom_stats_tick(struct m0_fom *fom);
64 
66 
68 static const struct m0_fom_ops fom_stats_ops = {
70  .fo_tick = fom_stats_tick,
71  .fo_home_locality = fom_stats_home_locality
72 };
73 
76  .fto_create = NULL
77 };
78 
81 
82 static size_t fom_stats_home_locality(const struct m0_fom *fom)
83 {
84  static size_t locality = 0;
85 
86  M0_PRE(fom != NULL);
87  return locality++;
88 }
89 
90 static int stats_fom_create(struct m0_fom **m, struct m0_reqh *reqh)
91 {
92  struct m0_fom *fom;
93  struct fom_stats *fom_obj;
94 
95  M0_PRE(m != NULL);
96 
97  M0_ALLOC_PTR(fom_obj);
98  M0_UT_ASSERT(fom_obj != NULL);
99 
100  fom = &fom_obj->fs_gen;
102 
103  *m = fom;
104  return 0;
105 }
106 
107 static void fop_stats_fom_fini(struct m0_fom *fom)
108 {
109  struct fom_stats *fom_obj = M0_AMB(fom_obj, fom, fs_gen);
110  m0_fom_fini(fom);
111  m0_free(fom_obj);
113 }
114 
115 static int fom_stats_tick(struct m0_fom *fom)
116 {
117  if (m0_fom_phase(fom) == PH_INIT) {
118  /* Must sleep to advance the clock */
119  m0_nanosleep(10000, NULL);
121  return M0_FSO_AGAIN;
122  } else if (m0_fom_phase(fom) == PH_RUN) {
123  m0_nanosleep(10000, NULL);
125  return M0_FSO_WAIT;
126  } else {
127  M0_UT_ASSERT(0); /* we should not get here */
128  return 0;
129  }
130 }
131 
132 static void test_stats_req_handle(struct m0_reqh *reqh)
133 {
134  struct m0_fom *fom;
135  int rc;
136 
138  m0_chan_init(&chan, &mutex);
141 
143  M0_UT_ASSERT(rc == 0);
144 
145  m0_fom_queue(fom);
146 
148 
153 }
154 
155 
156 /*
157  * Local variables:
158  * c-indentation-style: "K&R"
159  * c-basic-offset: 8
160  * tab-width: 8
161  * fill-column: 80
162  * scroll-step: 1
163  * End:
164  */
M0_INTERNAL void m0_chan_wait(struct m0_clink *link)
Definition: chan.c:336
#define M0_PRE(cond)
static struct m0_sm_state_descr phases[]
Definition: stats_fom.c:31
#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
static struct m0_addb2_mach * m
Definition: consumer.c:38
M0_INTERNAL void m0_clink_del_lock(struct m0_clink *link)
Definition: chan.c:293
static size_t locality(const struct m0_fom *fom)
Definition: rm_foms.c:269
Definition: sm.h:350
static void fop_stats_fom_fini(struct m0_fom *fom)
Definition: stats_fom.c:107
int(* fto_create)(struct m0_fop *fop, struct m0_fom **out, struct m0_reqh *reqh)
Definition: fom.h:650
static size_t fom_stats_home_locality(const struct m0_fom *fom)
Definition: stats_fom.c:82
#define M0_BITS(...)
Definition: misc.h:236
struct m0_fom_type stats_fom_type
Definition: stats_fom.c:80
m0_fom_phase
Definition: fom.h:372
const struct m0_fom_type_ops fom_stats_type_ops
Definition: stats_fom.c:75
struct m0_fom fs_gen
Definition: stats_fom.h:43
void m0_fom_init(struct m0_fom *fom, const struct m0_fom_type *fom_type, const struct m0_fom_ops *ops, struct m0_fop *fop, struct m0_fop *reply, struct m0_reqh *reqh)
Definition: fom.c:1372
static const struct m0_fom_ops fom_stats_ops
Definition: stats_fom.c:68
#define M0_AMB(obj, ptr, field)
Definition: misc.h:320
void m0_fom_fini(struct m0_fom *fom)
Definition: fom.c:1324
M0_INTERNAL void m0_chan_init(struct m0_chan *chan, struct m0_mutex *ch_guard)
Definition: chan.c:96
const char * scf_name
Definition: sm.h:352
static struct m0_chan chan
Definition: stats_fom.c:27
M0_INTERNAL void m0_mutex_init(struct m0_mutex *mutex)
Definition: mutex.c:35
Definition: reqh.h:94
struct m0_reqh_service_type ut_stats_service_type
Definition: stats_ut.c:88
Definition: dump.c:103
Definition: chan.h:229
M0_INTERNAL void m0_chan_signal_lock(struct m0_chan *chan)
Definition: chan.c:165
void m0_clink_add_lock(struct m0_chan *chan, struct m0_clink *link)
Definition: chan.c:255
uint32_t sd_flags
Definition: sm.h:378
Definition: fom.h:481
static struct m0_mutex mutex
Definition: stats_fom.c:28
struct m0_reqh reqh
Definition: rm_foms.c:48
static struct m0_sm_trans_descr trans[]
Definition: stats_fom.c:47
#define M0_ALLOC_PTR(ptr)
Definition: memory.h:86
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_fom_queue(struct m0_fom *fom)
Definition: fom.c:624
void(* fo_fini)(struct m0_fom *fom)
Definition: fom.h:657
static struct m0_clink clink
Definition: stats_fom.c:29
static void test_stats_req_handle(struct m0_reqh *reqh)
Definition: stats_fom.c:132
void m0_fom_phase_set(struct m0_fom *fom, int phase)
Definition: fom.c:1688
M0_INTERNAL void m0_chan_fini_lock(struct m0_chan *chan)
Definition: chan.c:112
void m0_free(void *data)
Definition: memory.c:146
Definition: mutex.h:47
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
#define M0_UT_ASSERT(a)
Definition: ut.h:46
static int stats_fom_create(struct m0_fom **m, struct m0_reqh *reqh)
Definition: stats_fom.c:90
static struct m0_sm_conf fom_phases_conf
Definition: stats_fom.c:53
static int fom_stats_tick(struct m0_fom *fom)
Definition: stats_fom.c:115
int m0_nanosleep(const m0_time_t req, m0_time_t *rem)
Definition: ktime.c:73