Motr  M0
sm.h
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2011-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 #pragma once
24 
25 #ifndef __MOTR_SM_SM_H__
26 #define __MOTR_SM_SM_H__
27 
28 #include "lib/types.h" /* int32_t, uint64_t */
29 #include "lib/atomic.h"
30 #include "lib/time.h" /* m0_time_t */
31 #include "lib/timer.h"
32 #include "lib/semaphore.h"
33 #include "lib/chan.h"
34 #include "lib/mutex.h"
35 #include "lib/tlist.h"
36 #include "addb2/histogram.h"
37 
269 /* export */
270 struct m0_sm;
271 struct m0_sm_state_descr;
272 struct m0_sm_state_stats;
273 struct m0_sm_conf;
274 struct m0_sm_group;
275 struct m0_sm_ast;
276 struct m0_sm_addb2_stats;
277 struct m0_sm_group_addb2;
278 struct m0_sm_ast_wait;
279 
280 /* import */
281 struct m0_timer;
282 struct m0_mutex;
283 
301 struct m0_sm {
307  uint32_t sm_state;
311  uint64_t sm_id;
320  const struct m0_sm_conf *sm_conf;
331  struct m0_chan sm_chan;
336  int32_t sm_rc;
341 };
342 
350 struct m0_sm_conf {
351  uint64_t scf_magic;
352  const char *scf_name;
354  uint32_t scf_nr_states;
358  uint32_t scf_trans_nr;
361  uint64_t scf_addb2_key;
362  uint64_t scf_addb2_id;
364 };
365 
366 enum {
368 };
369 
378  uint32_t sd_flags;
383  const char *sd_name;
397  int (*sd_in)(struct m0_sm *mach);
402  void (*sd_ex)(struct m0_sm *mach);
413  bool (*sd_invariant)(const struct m0_sm *mach);
422  uint64_t sd_allowed;
434 };
435 
450  M0_SDF_INITIAL = 1 << 0,
462  M0_SDF_FAILURE = 1 << 1,
471  M0_SDF_TERMINAL = 1 << 2,
479  M0_SDF_FINAL = 1 << 3
480 
481 };
482 
487  const char *td_cause;
488  uint32_t td_src;
489  uint32_t td_tgt;
490 };
491 
504 struct m0_sm_ast {
506  void (*sa_cb)(struct m0_sm_group *grp, struct m0_sm_ast *);
508  void *sa_datum;
510  struct m0_sm *sa_mach;
511 };
512 
513 struct m0_sm_group {
514  struct m0_mutex s_lock;
515  unsigned int s_nesting;
518  struct m0_chan s_chan;
520 };
521 
527 M0_INTERNAL void m0_sm_init(struct m0_sm *mach, const struct m0_sm_conf *conf,
528  uint32_t state, struct m0_sm_group *grp);
534 M0_INTERNAL void m0_sm_fini(struct m0_sm *mach);
535 
536 M0_INTERNAL void m0_sm_group_init(struct m0_sm_group *grp);
537 M0_INTERNAL void m0_sm_group_fini(struct m0_sm_group *grp);
538 
539 M0_INTERNAL void m0_sm_group_lock(struct m0_sm_group *grp);
540 M0_INTERNAL void m0_sm_group_unlock(struct m0_sm_group *grp);
541 M0_INTERNAL bool m0_sm_group_is_locked(const struct m0_sm_group *grp);
542 M0_INTERNAL void m0_sm_group_lock_rec(struct m0_sm_group *grp, bool runast);
543 M0_INTERNAL void m0_sm_group_unlock_rec(struct m0_sm_group *grp, bool runast);
561 M0_INTERNAL int m0_sm_timedwait(struct m0_sm *mach, uint64_t states,
562  m0_time_t deadline);
563 
575 M0_INTERNAL void m0_sm_fail(struct m0_sm *mach, int fail_state, int32_t rc);
576 
581 M0_INTERNAL void m0_sm_move(struct m0_sm *mach, int32_t rc, int state);
582 
598 void m0_sm_state_set(struct m0_sm *mach, int state);
599 
601 M0_INTERNAL const char *m0_sm_state_name(const struct m0_sm *mach, int state);
602 M0_INTERNAL const char *m0_sm_conf_state_name(const struct m0_sm_conf *conf,
603  int state);
604 
611 struct m0_sm_timer {
616  void (*tr_cb)(struct m0_sm_timer *);
620  int tr_state;
621 };
622 
623 M0_INTERNAL void m0_sm_timer_init(struct m0_sm_timer *timer);
624 M0_INTERNAL void m0_sm_timer_fini(struct m0_sm_timer *timer);
625 
636 M0_INTERNAL int m0_sm_timer_start(struct m0_sm_timer *timer,
637  struct m0_sm_group *group,
638  void (*cb)(struct m0_sm_timer *),
639  m0_time_t deadline);
640 M0_INTERNAL void m0_sm_timer_cancel(struct m0_sm_timer *timer);
641 M0_INTERNAL bool m0_sm_timer_is_armed(const struct m0_sm_timer *timer);
642 
643 
660  int st_state;
664  uint64_t st_bitmask;
665 };
666 
670 M0_INTERNAL void m0_sm_timeout_init(struct m0_sm_timeout *to);
671 
696 M0_INTERNAL int m0_sm_timeout_arm(struct m0_sm *mach, struct m0_sm_timeout *to,
697  m0_time_t timeout, int state,
698  uint64_t bitmask);
702 M0_INTERNAL void m0_sm_timeout_fini(struct m0_sm_timeout *to);
703 
707 M0_INTERNAL bool m0_sm_timeout_is_armed(const struct m0_sm_timeout *to);
708 
715 M0_INTERNAL void m0_sm_ast_post(struct m0_sm_group *grp, struct m0_sm_ast *ast);
716 
724 M0_INTERNAL void m0_sm_ast_cancel(struct m0_sm_group *grp,
725  struct m0_sm_ast *ast);
726 
733 M0_INTERNAL void m0_sm_asts_run(struct m0_sm_group *grp);
734 
741 };
742 
752 M0_INTERNAL void m0_sm_conf_trans_extend(const struct m0_sm_conf *base,
753  struct m0_sm_conf *sub);
760 M0_INTERNAL void m0_sm_conf_extend(const struct m0_sm_state_descr *base,
761  struct m0_sm_state_descr *sub, uint32_t nr);
762 
763 M0_INTERNAL bool m0_sm_invariant(const struct m0_sm *mach);
764 
777 M0_INTERNAL void m0_sm_conf_init(struct m0_sm_conf *conf);
778 
786 M0_INTERNAL void m0_sm_conf_fini(struct m0_sm_conf *conf);
787 
791 M0_INTERNAL bool m0_sm_conf_is_initialized(const struct m0_sm_conf *conf);
792 
794  int (*cb)(void *), void *data);
795 
797  uint64_t as_id;
798  int as_nr;
800 };
801 
803  uint64_t ga_forq;
805 };
806 
807 M0_INTERNAL int m0_sm_addb2_init(struct m0_sm_conf *conf,
808  uint64_t id, uint64_t counter);
809 
810 M0_INTERNAL void m0_sm_addb2_fini(struct m0_sm_conf *conf);
811 
812 M0_INTERNAL bool m0_sm_addb2_counter_init(struct m0_sm *sm);
813 
843  struct m0_chan aw_chan;
844 };
845 
846 M0_INTERNAL void m0_sm_ast_wait_init(struct m0_sm_ast_wait *wait,
847  struct m0_mutex *ch_guard);
848 M0_INTERNAL void m0_sm_ast_wait_fini(struct m0_sm_ast_wait *wait);
849 
855 M0_INTERNAL void m0_sm_ast_wait(struct m0_sm_ast_wait *wait);
856 
862 M0_INTERNAL void m0_sm_ast_wait_post(struct m0_sm_ast_wait *wait,
863  struct m0_sm_group *grp,
864  struct m0_sm_ast *ast);
865 
871 M0_INTERNAL void m0_sm_ast_wait_signal(struct m0_sm_ast_wait *wait);
872 
873 M0_INTERNAL void m0_sm_ast_wait_prepare(struct m0_sm_ast_wait *wait,
874  struct m0_clink *clink);
875 M0_INTERNAL void m0_sm_ast_wait_complete(struct m0_sm_ast_wait *wait,
876  struct m0_clink *clink);
877 M0_INTERNAL void m0_sm_ast_wait_loop(struct m0_sm_ast_wait *wait,
878  struct m0_clink *clink);
879 
883 M0_INTERNAL void m0_sm_conf_print(const struct m0_sm_conf *conf);
884 
888 M0_INTERNAL uint64_t m0_sm_id_get(const struct m0_sm *sm);
889 
890 
892 #endif /* __MOTR_SM_SM_H__ */
893 
894 /*
895  * Local variables:
896  * c-indentation-style: "K&R"
897  * c-basic-offset: 8
898  * tab-width: 8
899  * fill-column: 80
900  * scroll-step: 1
901  * End:
902  */
int tr_state
Definition: sm.h:620
int st_state
Definition: sm.h:660
uint32_t scf_trans_nr
Definition: sm.h:358
static size_t nr
Definition: dump.c:1505
uint64_t scf_addb2_key
Definition: sm.h:361
M0_INTERNAL void m0_sm_conf_init(struct m0_sm_conf *conf)
Definition: sm.c:340
static struct m0_semaphore wait
Definition: item.c:151
M0_INTERNAL void m0_sm_fail(struct m0_sm *mach, int fail_state, int32_t rc)
Definition: sm.c:468
void(* sd_ex)(struct m0_sm *mach)
Definition: sm.h:402
struct m0_chan aw_chan
Definition: sm.h:843
M0_INTERNAL int m0_sm_addb2_init(struct m0_sm_conf *conf, uint64_t id, uint64_t counter)
Definition: sm.c:846
int(* sd_in)(struct m0_sm *mach)
Definition: sm.h:397
m0_sm_return
Definition: sm.h:735
void(* sa_cb)(struct m0_sm_group *grp, struct m0_sm_ast *)
Definition: sm.h:506
Definition: sm.h:350
struct m0_sm_group_addb2 * s_addb2
Definition: sm.h:519
uint32_t td_tgt
Definition: sm.h:489
static struct m0_sm_group * grp
Definition: bytecount.c:38
M0_INTERNAL void m0_sm_timeout_init(struct m0_sm_timeout *to)
Definition: sm.c:667
uint64_t m0_time_t
Definition: time.h:37
M0_INTERNAL void m0_sm_ast_wait(struct m0_sm_ast_wait *wait)
Definition: sm.c:950
M0_INTERNAL void m0_sm_ast_post(struct m0_sm_group *grp, struct m0_sm_ast *ast)
Definition: sm.c:135
M0_INTERNAL void m0_sm_group_unlock_rec(struct m0_sm_group *grp, bool runast)
Definition: sm.c:123
struct m0_clink s_clink
Definition: sm.h:516
struct m0_sm_ast * s_forkq
Definition: sm.h:517
uint64_t sd_allowed
Definition: sm.h:422
struct m0_bufvec data
Definition: di.c:40
M0_INTERNAL void m0_sm_ast_wait_init(struct m0_sm_ast_wait *wait, struct m0_mutex *ch_guard)
Definition: sm.c:903
bool(* sd_invariant)(const struct m0_sm *mach)
Definition: sm.h:413
M0_INTERNAL const char * m0_sm_state_name(const struct m0_sm *mach, int state)
Definition: sm.c:781
struct m0_chan s_chan
Definition: sm.h:518
M0_INTERNAL void m0_sm_conf_trans_extend(const struct m0_sm_conf *base, struct m0_sm_conf *sub)
Definition: sm.c:726
Definition: conf.py:1
M0_INTERNAL void m0_sm_conf_extend(const struct m0_sm_state_descr *base, struct m0_sm_state_descr *sub, uint32_t nr)
Definition: sm.c:763
static struct m0_addb2_mach * mach
Definition: storage.c:42
uint64_t sm_id
Definition: sm.h:311
Definition: sm.h:504
struct m0_sm * sa_mach
Definition: sm.h:510
struct m0_addb2_hist as_hist[0]
Definition: sm.h:799
M0_INTERNAL bool m0_sm_addb2_counter_init(struct m0_sm *sm)
Definition: sm.c:891
Definition: timer.h:39
bool aw_allowed
Definition: sm.h:839
M0_INTERNAL void m0_sm_group_fini(struct m0_sm_group *grp)
Definition: sm.c:65
uint64_t scf_addb2_id
Definition: sm.h:362
static bool runast
Definition: file.c:96
M0_INTERNAL int m0_sm_timedwait(struct m0_sm *mach, uint64_t states, m0_time_t deadline)
Definition: sm.c:387
static struct m0_sm_ast ast[NR]
Definition: locality.c:44
M0_INTERNAL void m0_sm_group_unlock(struct m0_sm_group *grp)
Definition: sm.c:96
struct m0_sm_trans_descr * scf_trans
Definition: sm.h:360
uint64_t scf_addb2_counter
Definition: sm.h:363
M0_INTERNAL bool m0_sm_timer_is_armed(const struct m0_sm_timer *timer)
Definition: sm.c:628
struct m0_atomic64 aw_active
Definition: sm.h:841
void * sa_datum
Definition: sm.h:508
uint64_t ga_forq
Definition: sm.h:803
int m0_sm_group_call(struct m0_sm_group *group, int(*cb)(void *), void *data)
Definition: sm.c:801
M0_INTERNAL void m0_sm_ast_wait_loop(struct m0_sm_ast_wait *wait, struct m0_clink *clink)
Definition: sm.c:942
struct m0_clink st_clink
Definition: sm.h:658
const char * scf_name
Definition: sm.h:352
M0_INTERNAL void m0_sm_group_init(struct m0_sm_group *grp)
Definition: sm.c:53
struct m0_sm_group * tr_grp
Definition: sm.h:612
void m0_sm_state_set(struct m0_sm *mach, int state)
Definition: sm.c:478
unsigned int s_nesting
Definition: sm.h:515
static int counter
Definition: mutex.c:32
uint32_t scf_nr_states
Definition: sm.h:354
int8_t sd_trans[M0_SM_MAX_STATES]
Definition: sm.h:433
struct m0_sm_addb2_stats * sm_addb2_stats
Definition: sm.h:327
struct m0_sm_group * sm_grp
Definition: sm.h:321
M0_INTERNAL void m0_sm_addb2_fini(struct m0_sm_conf *conf)
Definition: sm.c:870
uint32_t td_src
Definition: sm.h:488
int32_t sm_rc
Definition: sm.h:336
Definition: chan.h:229
static void group(void)
Definition: sm.c:386
struct m0_sm_ast * sa_next
Definition: sm.h:509
static struct m0_clink clink[RDWR_REQUEST_MAX]
struct m0_mutex s_lock
Definition: sm.h:514
struct m0_addb2_hist ga_forq_hist
Definition: sm.h:804
uint64_t scf_magic
Definition: sm.h:351
static uint32_t timeout
Definition: console.c:52
uint32_t sd_flags
Definition: sm.h:378
M0_INTERNAL int m0_sm_timeout_arm(struct m0_sm *mach, struct m0_sm_timeout *to, m0_time_t timeout, int state, uint64_t bitmask)
Definition: sm.c:674
const char * sd_name
Definition: sm.h:383
M0_INTERNAL void m0_sm_ast_wait_post(struct m0_sm_ast_wait *wait, struct m0_sm_group *grp, struct m0_sm_ast *ast)
Definition: sm.c:961
M0_INTERNAL void m0_sm_timeout_fini(struct m0_sm_timeout *to)
Definition: sm.c:705
struct m0_sm_state_descr * scf_state
Definition: sm.h:356
M0_INTERNAL void m0_sm_timer_cancel(struct m0_sm_timer *timer)
Definition: sm.c:610
uint64_t st_bitmask
Definition: sm.h:664
M0_INTERNAL void m0_sm_init(struct m0_sm *mach, const struct m0_sm_conf *conf, uint32_t state, struct m0_sm_group *grp)
Definition: sm.c:313
struct m0_chan sm_chan
Definition: sm.h:331
struct m0_sm_timer st_timer
Definition: sm.h:655
Definition: sm.h:301
M0_INTERNAL void m0_sm_ast_wait_fini(struct m0_sm_ast_wait *wait)
Definition: sm.c:910
M0_INTERNAL void m0_sm_conf_print(const struct m0_sm_conf *conf)
Definition: sm.c:993
M0_INTERNAL void m0_sm_move(struct m0_sm *mach, int32_t rc, int state)
Definition: sm.c:485
bool sm_invariant_chk_off
Definition: sm.h:340
M0_INTERNAL void m0_sm_ast_wait_prepare(struct m0_sm_ast_wait *wait, struct m0_clink *clink)
Definition: sm.c:917
M0_INTERNAL void m0_sm_group_lock(struct m0_sm_group *grp)
Definition: sm.c:83
m0_sm_state_descr_flags
Definition: sm.h:439
static uint64_t base
Definition: dump.c:1504
M0_INTERNAL void m0_sm_ast_cancel(struct m0_sm_group *grp, struct m0_sm_ast *ast)
Definition: sm.c:183
M0_INTERNAL void m0_sm_conf_fini(struct m0_sm_conf *conf)
Definition: sm.c:376
M0_INTERNAL bool m0_sm_conf_is_initialized(const struct m0_sm_conf *conf)
Definition: sm.c:382
M0_INTERNAL void m0_sm_timer_fini(struct m0_sm_timer *timer)
Definition: sm.c:566
M0_INTERNAL void m0_sm_asts_run(struct m0_sm_group *grp)
Definition: sm.c:150
M0_INTERNAL bool m0_sm_timeout_is_armed(const struct m0_sm_timeout *to)
Definition: sm.c:713
M0_INTERNAL void m0_sm_timer_init(struct m0_sm_timer *timer)
Definition: sm.c:559
const char * td_cause
Definition: sm.h:487
M0_INTERNAL uint64_t m0_sm_id_get(const struct m0_sm *sm)
Definition: sm.c:1021
uint64_t as_id
Definition: sm.h:797
void(* tr_cb)(struct m0_sm_timer *)
Definition: sm.h:616
Definition: mutex.h:47
uint32_t sm_state
Definition: sm.h:307
m0_time_t sm_state_epoch
Definition: sm.h:326
M0_INTERNAL void m0_sm_ast_wait_signal(struct m0_sm_ast_wait *wait)
Definition: sm.c:985
int32_t rc
Definition: trigger_fop.h:47
M0_INTERNAL bool m0_sm_group_is_locked(const struct m0_sm_group *grp)
Definition: sm.c:107
M0_INTERNAL int m0_sm_timer_start(struct m0_sm_timer *timer, struct m0_sm_group *group, void(*cb)(struct m0_sm_timer *), m0_time_t deadline)
Definition: sm.c:577
static struct m0_sm_state_descr states[C_NR]
Definition: sm.c:512
const struct m0_sm_conf * sm_conf
Definition: sm.h:320
struct m0_mutex * ch_guard
Definition: chan.h:231
M0_INTERNAL void m0_sm_ast_wait_complete(struct m0_sm_ast_wait *wait, struct m0_clink *clink)
Definition: sm.c:935
struct m0_sm_ast tr_ast
Definition: sm.h:614
struct m0_timer tr_timer
Definition: sm.h:613
M0_INTERNAL const char * m0_sm_conf_state_name(const struct m0_sm_conf *conf, int state)
Definition: sm.c:774
M0_INTERNAL void m0_sm_fini(struct m0_sm *mach)
Definition: sm.c:331
M0_INTERNAL bool m0_sm_invariant(const struct m0_sm *mach)
Definition: sm.c:267
M0_INTERNAL void m0_sm_group_lock_rec(struct m0_sm_group *grp, bool runast)
Definition: sm.c:112