Skip to content

Commit c0b3190

Browse files
YHNdnzjkeszybz
authored andcommitted
core/service: log the next restart usec only when we're arming timer
service_restart_usec_next() is also called when querying 'RestartUSecNext' dbus property. Let's avoid the redundant logging triggered every time user does a 'systemctl status'. (cherry picked from commit fe3d33c)
1 parent c58c69a commit c0b3190

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

src/core/service.c

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,6 @@ static void service_start_watchdog(Service *s) {
283283

284284
usec_t service_restart_usec_next(Service *s) {
285285
unsigned n_restarts_next;
286-
usec_t value;
287286

288287
assert(s);
289288

@@ -297,26 +296,23 @@ usec_t service_restart_usec_next(Service *s) {
297296
s->restart_usec == 0 ||
298297
s->restart_max_delay_usec == USEC_INFINITY ||
299298
s->restart_usec >= s->restart_max_delay_usec)
300-
value = s->restart_usec;
301-
else if (n_restarts_next > s->restart_steps)
302-
value = s->restart_max_delay_usec;
303-
else {
304-
/* Enforced in service_verify() and above */
305-
assert(s->restart_max_delay_usec > s->restart_usec);
306-
307-
/* r_i / r_0 = (r_n / r_0) ^ (i / n)
308-
* where,
309-
* r_0 : initial restart usec (s->restart_usec),
310-
* r_i : i-th restart usec (value),
311-
* r_n : maximum restart usec (s->restart_max_delay_usec),
312-
* i : index of the next step (n_restarts_next - 1)
313-
* n : num maximum steps (s->restart_steps) */
314-
value = (usec_t) (s->restart_usec * powl((long double) s->restart_max_delay_usec / s->restart_usec,
315-
(long double) (n_restarts_next - 1) / s->restart_steps));
316-
}
317-
318-
log_unit_debug(UNIT(s), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(value, 0));
319-
return value;
299+
return s->restart_usec;
300+
301+
if (n_restarts_next > s->restart_steps)
302+
return s->restart_max_delay_usec;
303+
304+
/* Enforced in service_verify() and above */
305+
assert(s->restart_max_delay_usec > s->restart_usec);
306+
307+
/* r_i / r_0 = (r_n / r_0) ^ (i / n)
308+
* where,
309+
* r_0 : initial restart usec (s->restart_usec),
310+
* r_i : i-th restart usec (value),
311+
* r_n : maximum restart usec (s->restart_max_delay_usec),
312+
* i : index of the next step (n_restarts_next - 1)
313+
* n : num maximum steps (s->restart_steps) */
314+
return (usec_t) (s->restart_usec * powl((long double) s->restart_max_delay_usec / s->restart_usec,
315+
(long double) (n_restarts_next - 1) / s->restart_steps));
320316
}
321317

322318
static void service_extend_event_source_timeout(Service *s, sd_event_source *source, usec_t extended) {
@@ -1999,6 +1995,8 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
19991995
}
20001996

20011997
if (allow_restart) {
1998+
usec_t restart_usec_next;
1999+
20022000
/* We make two state changes here: one that maps to the high-level UNIT_INACTIVE/UNIT_FAILED
20032001
* state (i.e. a state indicating deactivation), and then one that that maps to the
20042002
* high-level UNIT_STARTING state (i.e. a state indicating activation). We do this so that
@@ -2009,10 +2007,14 @@ static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart)
20092007
if (s->restart_mode != SERVICE_RESTART_MODE_DIRECT)
20102008
service_set_state(s, restart_state);
20112009

2012-
r = service_arm_timer(s, /* relative= */ true, service_restart_usec_next(s));
2010+
restart_usec_next = service_restart_usec_next(s);
2011+
2012+
r = service_arm_timer(s, /* relative= */ true, restart_usec_next);
20132013
if (r < 0)
20142014
goto fail;
20152015

2016+
log_unit_debug(UNIT(s), "Next restart interval calculated as: %s", FORMAT_TIMESPAN(restart_usec_next, 0));
2017+
20162018
service_set_state(s, SERVICE_AUTO_RESTART);
20172019
} else {
20182020
service_set_state(s, end_state);

0 commit comments

Comments
 (0)