diff options
-rw-r--r-- | include/osmocom/core/fsm.h | 1 | ||||
-rw-r--r-- | src/fsm.c | 41 | ||||
-rw-r--r-- | tests/fsm/fsm_test.c | 3 | ||||
-rw-r--r-- | tests/fsm/fsm_test.err | 20 |
4 files changed, 53 insertions, 12 deletions
diff --git a/include/osmocom/core/fsm.h b/include/osmocom/core/fsm.h index df7e3486..ae1c8575 100644 --- a/include/osmocom/core/fsm.h +++ b/include/osmocom/core/fsm.h @@ -118,6 +118,7 @@ struct osmo_fsm_inst { }; void osmo_fsm_log_addr(bool log_addr); +void osmo_fsm_log_timeouts(bool log_timeouts); /*! Log using FSM instance's context, on explicit logging subsystem and level. * \param fi An osmo_fsm_inst. @@ -90,6 +90,7 @@ LLIST_HEAD(osmo_g_fsms); static bool fsm_log_addr = true; +static bool fsm_log_timeouts = false; /*! specify if FSM instance addresses should be logged or not * @@ -104,6 +105,26 @@ void osmo_fsm_log_addr(bool log_addr) fsm_log_addr = log_addr; } +/*! Enable or disable logging of timeout values for FSM instance state changes. + * + * By default, state changes are logged by state name only, omitting the timeout. When passing true, each state change + * will also log the T number and the chosen timeout in seconds. osmo_fsm_inst_state_chg_keep_timer() will log remaining + * timeout in millisecond precision. + * + * The default for this is false to reflect legacy behavior. Since various C tests that verify logging output already + * existed prior to this option, keeping timeout logging off makes sure that they continue to pass. Particularly, + * osmo_fsm_inst_state_chg_keep_timer() may cause non-deterministic logging of remaining timeout values. + * + * For any program that does not explicitly require deterministic logging output, i.e. anything besides regression tests + * involving FSM instances, it is recommended to call osmo_fsm_log_timeouts(true). + * + * \param[in] log_timeouts Pass true to log timeouts on state transitions, false to omit timeouts. + */ +void osmo_fsm_log_timeouts(bool log_timeouts) +{ + fsm_log_timeouts = log_timeouts; +} + struct osmo_fsm *osmo_fsm_find_by_name(const char *name) { struct osmo_fsm *fsm; @@ -436,6 +457,7 @@ static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, struct osmo_fsm *fsm = fi->fsm; uint32_t old_state = fi->state; const struct osmo_fsm_state *st = &fsm->states[fi->state]; + struct timeval remaining; /* Limit to 0x7fffffff seconds as explained by * _osmo_fsm_inst_state_chg()'s API doc. */ @@ -458,8 +480,23 @@ static int state_chg(struct osmo_fsm_inst *fi, uint32_t new_state, if (st->onleave) st->onleave(fi, new_state); - LOGPFSMSRC(fi, file, line, "state_chg to %s\n", - osmo_fsm_state_name(fsm, new_state)); + if (fsm_log_timeouts) { + if (keep_timer && fi->timer.active && (osmo_timer_remaining(&fi->timer, NULL, &remaining) == 0)) + LOGPFSMSRC(fi, file, line, "State change to %s (keeping T%d, %ld.%03lds remaining)\n", + osmo_fsm_state_name(fsm, new_state), + fi->T, remaining.tv_sec, remaining.tv_usec / 1000); + else if (timeout_secs && !keep_timer) + LOGPFSMSRC(fi, file, line, "State change to %s (T%d, %lus)\n", + osmo_fsm_state_name(fsm, new_state), + T, timeout_secs); + else + LOGPFSMSRC(fi, file, line, "State change to %s (no timeout)\n", + osmo_fsm_state_name(fsm, new_state)); + } else { + LOGPFSMSRC(fi, file, line, "state_chg to %s\n", + osmo_fsm_state_name(fsm, new_state)); + } + fi->state = new_state; st = &fsm->states[new_state]; diff --git a/tests/fsm/fsm_test.c b/tests/fsm/fsm_test.c index 7aac8d3e..f8ebdc7b 100644 --- a/tests/fsm/fsm_test.c +++ b/tests/fsm/fsm_test.c @@ -406,6 +406,9 @@ int main(int argc, char **argv) osmo_fsm_log_addr(false); + /* Using fake time to get deterministic timeout logging */ + osmo_fsm_log_timeouts(true); + log_init(&log_info, NULL); stderr_target = log_target_create_stderr(); log_add_target(stderr_target); diff --git a/tests/fsm/fsm_test.err b/tests/fsm/fsm_test.err index bf474aba..13cbacd9 100644 --- a/tests/fsm/fsm_test.err +++ b/tests/fsm/fsm_test.err @@ -3,9 +3,9 @@ Checking FSM allocation [0;mTest_FSM(my_id){NULL}: Received Event EV_B [0;mTest_FSM(my_id){NULL}: Event EV_B not permitted [0;mTest_FSM(my_id){NULL}: Received Event EV_A -[0;mTest_FSM(my_id){NULL}: state_chg to ONE +[0;mTest_FSM(my_id){NULL}: State change to ONE (no timeout) [0;mTest_FSM(my_id){ONE}: Received Event EV_B -[0;mTest_FSM(my_id){ONE}: state_chg to TWO +[0;mTest_FSM(my_id){ONE}: State change to TWO (T2342, 1s) [0;mTest_FSM(my_id){TWO}: Timeout of T2342 [0;mTimer [0;mTest_FSM(my_id){TWO}: Deallocated @@ -83,16 +83,16 @@ Test_FSM(arbitrary_id){NULL}: Terminating (cause = OSMO_FSM_TERM_REQUEST) [0;m --- test_state_chg_keep_timer() Test_FSM{NULL}: Allocated -[0;mTest_FSM{NULL}: state_chg to ONE -[0;mTest_FSM{ONE}: state_chg to TWO +[0;mTest_FSM{NULL}: State change to ONE (no timeout) +[0;mTest_FSM{ONE}: State change to TWO (no timeout) [0;mTest_FSM{TWO}: Terminating (cause = OSMO_FSM_TERM_REQUEST) [0;mTest_FSM{TWO}: Freeing instance [0;mTest_FSM{TWO}: Deallocated [0;mTotal time passed: 0.000000 s Test_FSM{NULL}: Allocated -[0;mTest_FSM{NULL}: state_chg to ONE +[0;mTest_FSM{NULL}: State change to ONE (T10, 10s) [0;mTotal time passed: 2.000342 s -Test_FSM{ONE}: state_chg to TWO +Test_FSM{ONE}: State change to TWO (keeping T10, 7.999s remaining) [0;mTotal time passed: 2.000342 s Total time passed: 9.999999 s Total time passed: 10.000000 s @@ -104,14 +104,14 @@ Test_FSM{TWO}: Timeout of T10 --- test_state_chg_T() Test_FSM{NULL}: Allocated -[0;mTest_FSM{NULL}: state_chg to ONE -[0;mTest_FSM{ONE}: state_chg to TWO +[0;mTest_FSM{NULL}: State change to ONE (T42, 23s) +[0;mTest_FSM{ONE}: State change to TWO (no timeout) [0;mTest_FSM{TWO}: Terminating (cause = OSMO_FSM_TERM_REQUEST) [0;mTest_FSM{TWO}: Freeing instance [0;mTest_FSM{TWO}: Deallocated [0;mTest_FSM{NULL}: Allocated -[0;mTest_FSM{NULL}: state_chg to ONE -[0;mTest_FSM{ONE}: state_chg to TWO +[0;mTest_FSM{NULL}: State change to ONE (T42, 23s) +[0;mTest_FSM{ONE}: State change to TWO (no timeout) [0;mTest_FSM{TWO}: Terminating (cause = OSMO_FSM_TERM_REQUEST) [0;mTest_FSM{TWO}: Freeing instance [0;mTest_FSM{TWO}: Deallocated |