Motr  M0
sim.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012-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 
90 #pragma once
91 
92 #ifndef __MOTR_DESIM_SIM_H__
93 #define __MOTR_DESIM_SIM_H__
94 
95 #include <stdarg.h>
96 
97 #if defined(__APPLE__)
98 /* for ucontext:
99  http://lists.apple.com/archives/darwin-dev/2008/Jan/msg00229.html */
100 #define _XOPEN_SOURCE
101 #endif
102 
103 #include <stdlib.h>
104 #include <ucontext.h>
105 
106 #include "lib/tlist.h"
107 #include "desim/cnt.h"
108 
109 struct sim;
110 struct sim_callout;
111 struct sim_thread;
112 
113 typedef unsigned long long sim_time_t;
114 typedef int sim_call_t(struct sim_callout *);
115 typedef void sim_func_t(struct sim *, struct sim_thread *, void *);
116 
126 struct sim_callout {
138  void *sc_datum;
142  struct sim *sc_sim;
143  uint64_t sc_magic;
144 };
145 
152 struct sim {
165  struct m0_tl ss_future;
166 };
167 
202 struct sim_thread {
204  struct sim *st_sim;
206  void *st_stack;
208  unsigned st_size;
212  ucontext_t st_ctx;
213  /* channel waiting */
224  uint64_t st_magic;
225 };
226 
237 struct sim_chan {
244 };
245 
246 M0_INTERNAL void sim_init(struct sim *state);
247 M0_INTERNAL void sim_fini(struct sim *state);
248 M0_INTERNAL void sim_run(struct sim *state);
249 
250 M0_INTERNAL void sim_timer_add(struct sim *state, sim_time_t delta,
251  sim_call_t * cfunc, void *datum);
252 M0_INTERNAL void sim_timer_rearm(struct sim_callout *call, sim_time_t delta,
253  sim_call_t * cfunc, void *datum);
254 M0_INTERNAL void *sim_alloc(size_t size);
255 M0_INTERNAL void sim_free(void *ptr);
256 
257 M0_INTERNAL void sim_chan_init(struct sim_chan *chan, char *format, ...)
258  __attribute__((format(printf, 2, 3)));
259 M0_INTERNAL void sim_chan_fini(struct sim_chan *chan);
260 M0_INTERNAL void sim_chan_wait(struct sim_chan *chan,
261  struct sim_thread *thread);
262 M0_INTERNAL void sim_chan_signal(struct sim_chan *chan);
263 M0_INTERNAL void sim_chan_broadcast(struct sim_chan *chan);
264 
265 M0_INTERNAL void sim_thread_init(struct sim *state, struct sim_thread *thread,
266  unsigned stacksize, sim_func_t func,
267  void *arg);
268 M0_INTERNAL void sim_thread_fini(struct sim_thread *thread);
269 M0_INTERNAL void sim_thread_exit(struct sim_thread *thread);
270 
271 M0_INTERNAL void sim_sleep(struct sim_thread *thread, sim_time_t nap);
272 M0_INTERNAL struct sim_thread *sim_thread_current(void);
273 
274 /* get a pseudo-random number in the interval [a, b] */
275 M0_INTERNAL unsigned long long sim_rnd(unsigned long long a,
276  unsigned long long b);
277 
278 M0_INTERNAL void sim_name_set(char **name, const char *format, ...)
279  __attribute__((format(printf, 2, 3)));
280 
281 M0_INTERNAL void sim_name_vaset(char **name, const char *format,
282  va_list valist);
283 
289 };
290 
291 extern enum sim_log_level sim_log_level;
292 
293 M0_INTERNAL void
294 sim_log(struct sim *s, enum sim_log_level level, const char *format, ...)
295  __attribute__((format(printf, 3, 4)));
296 
297 M0_INTERNAL int sim_global_init(void);
298 M0_INTERNAL void sim_global_fini(void);
299 
300 #endif /* __MOTR_DESIM_SIM_H__ */
301 
304 /*
305  * Local variables:
306  * c-indentation-style: "K&R"
307  * c-basic-offset: 8
308  * tab-width: 8
309  * fill-column: 80
310  * scroll-step: 1
311  * End:
312  */
int sim_call_t(struct sim_callout *)
Definition: sim.h:114
static void ptr(struct m0_addb2__context *ctx, const uint64_t *v, char *buf)
Definition: dump.c:440
M0_INTERNAL void sim_thread_init(struct sim *state, struct sim_thread *thread, unsigned stacksize, sim_func_t func, void *arg)
Definition: sim.c:293
Definition: sim.h:152
M0_INTERNAL void sim_init(struct sim *state)
Definition: sim.c:106
M0_INTERNAL void sim_chan_fini(struct sim_chan *chan)
Definition: sim.c:400
M0_INTERNAL void sim_free(void *ptr)
Definition: sim.c:98
M0_INTERNAL struct sim_thread * sim_thread_current(void)
Definition: sim.c:465
enum m0_trace_level level
Definition: trace.c:111
void sim_func_t(struct sim *, struct sim_thread *, void *)
Definition: sim.h:115
Definition: sim.h:285
ucontext_t st_ctx
Definition: sim.h:212
uint64_t sc_magic
Definition: sim.h:143
struct m0_tlink st_block
Definition: sim.h:215
struct sim * st_sim
Definition: sim.h:204
M0_INTERNAL void sim_log(struct sim *s, enum sim_log_level level, const char *format,...)
Definition: sim.c:527
enum m0_md_lustre_logrec_type __attribute__
Definition: balloc.c:2745
M0_INTERNAL void sim_chan_broadcast(struct sim_chan *chan)
Definition: sim.c:456
unsigned st_size
Definition: sim.h:208
M0_INTERNAL void sim_chan_init(struct sim_chan *chan, char *format,...)
Definition: sim.c:385
M0_INTERNAL void sim_thread_exit(struct sim_thread *thread)
Definition: sim.c:346
void * st_stack
Definition: sim.h:206
M0_INTERNAL void sim_run(struct sim *state)
Definition: sim.c:129
M0_INTERNAL void sim_global_fini(void)
Definition: sim.c:549
M0_INTERNAL void sim_thread_fini(struct sim_thread *thread)
Definition: sim.c:333
Definition: cnt.h:36
const char * name
Definition: trace.c:110
struct sim_callout st_wake
Definition: sim.h:223
struct m0_thread thread
Definition: note.c:104
Definition: tlist.h:251
M0_INTERNAL void sim_timer_add(struct sim *state, sim_time_t delta, sim_call_t *cfunc, void *datum)
Definition: sim.c:189
struct cnt ch_cnt_sleep
Definition: sim.h:243
M0_INTERNAL void sim_sleep(struct sim_thread *thread, sim_time_t nap)
Definition: sim.c:372
sim_time_t ss_bolt
Definition: sim.h:158
M0_INTERNAL void sim_chan_wait(struct sim_chan *chan, struct sim_thread *thread)
Definition: sim.c:411
unsigned long long sim_time_t
Definition: sim.h:111
M0_INTERNAL void sim_chan_signal(struct sim_chan *chan)
Definition: sim.c:447
M0_INTERNAL unsigned long long sim_rnd(unsigned long long a, unsigned long long b)
Definition: sim.c:471
Definition: sim.h:237
M0_INTERNAL void * sim_alloc(size_t size)
Definition: sim.c:85
sim_call_t * sc_call
Definition: sim.h:134
format
Definition: hist.py:128
M0_INTERNAL void sim_name_vaset(char **name, const char *format, va_list valist)
Definition: sim.c:513
void * sc_datum
Definition: sim.h:138
struct sim * sc_sim
Definition: sim.h:142
sim_time_t sc_time
Definition: sim.h:128
Definition: sim.h:288
static struct m0_chan chan[RDWR_REQUEST_MAX]
struct m0_tl ch_threads
Definition: sim.h:239
m0_bcount_t size
Definition: di.c:39
M0_INTERNAL void sim_timer_rearm(struct sim_callout *call, sim_time_t delta, sim_call_t *cfunc, void *datum)
Definition: sim.c:202
sim_log_level
Definition: sim.h:284
uint64_t st_magic
Definition: sim.h:224
struct m0_tlink sc_linkage
Definition: sim.h:140
Definition: sim.h:286
M0_INTERNAL void sim_name_set(char **name, const char *format,...)
Definition: sim.c:500
M0_INTERNAL void sim_fini(struct sim *state)
Definition: sim.c:116
sim_time_t st_blocked
Definition: sim.h:219
static struct m0_addb2_source * s
Definition: consumer.c:39
struct m0_tl ss_future
Definition: sim.h:165
Definition: sim.h:287
M0_INTERNAL int sim_global_init(void)
Definition: sim.c:543