Enable/disable per-second callback as needed.

There are three cases where this can happen: changes in our
controller events, changes in our DisableNetwork setting, and
changes in our hibernation state.

Closes ticket 26063.
This commit is contained in:
Nick Mathewson 2018-05-09 12:47:59 -04:00
parent e722bba263
commit 8b4cf7771e
4 changed files with 15 additions and 0 deletions

5
changes/ticket26063 Normal file
View File

@ -0,0 +1,5 @@
o Major features (CPU usage, mobile):
- When Tor is disabled (via DisableNetwork or via hibernation), it
no longer needs to run any per-second events. This change should
make it easier for mobile applications to disable Tor while the
device is sleeping, or Tor is not running. Closes ticket 26063.

View File

@ -2001,6 +2001,9 @@ options_act(const or_options_t *old_options)
finish_daemon(options->DataDirectory); finish_daemon(options->DataDirectory);
} }
/* See whether we need to enable/disable our once-a-second timer. */
reschedule_per_second_timer();
/* We want to reinit keys as needed before we do much of anything else: /* We want to reinit keys as needed before we do much of anything else:
keys are important, and other things can depend on them. */ keys are important, and other things can depend on them. */
if (transition_affects_workers || if (transition_affects_workers ||

View File

@ -277,6 +277,7 @@ control_update_global_event_mask(void)
smartlist_t *conns = get_connection_array(); smartlist_t *conns = get_connection_array();
event_mask_t old_mask, new_mask; event_mask_t old_mask, new_mask;
old_mask = global_event_mask; old_mask = global_event_mask;
int any_old_per_sec_events = control_any_per_second_event_enabled();
global_event_mask = 0; global_event_mask = 0;
SMARTLIST_FOREACH(conns, connection_t *, _conn, SMARTLIST_FOREACH(conns, connection_t *, _conn,
@ -316,6 +317,10 @@ control_update_global_event_mask(void)
uint64_t r, w; uint64_t r, w;
control_get_bytes_rw_last_sec(&r, &w); control_get_bytes_rw_last_sec(&r, &w);
} }
if (any_old_per_sec_events != control_any_per_second_event_enabled()) {
reschedule_per_second_timer();
}
#undef NEWLY_ENABLED #undef NEWLY_ENABLED
} }

View File

@ -1137,6 +1137,8 @@ on_hibernate_state_change(hibernate_state_t prev_state)
if (prev_state != HIBERNATE_STATE_INITIAL) { if (prev_state != HIBERNATE_STATE_INITIAL) {
rescan_periodic_events(get_options()); rescan_periodic_events(get_options());
} }
reschedule_per_second_timer();
} }
#ifdef TOR_UNIT_TESTS #ifdef TOR_UNIT_TESTS