Commit 166c2f4d authored by Karsten Loesing's avatar Karsten Loesing
Browse files

Allow enabling or disabling *Statistics while Tor is running.

With this patch we stop scheduling when we should write statistics using a
single timestamp in run_scheduled_events(). Instead, we remember when a
statistics interval starts separately for each statistic type in geoip.c
and rephist.c. Every time run_scheduled_events() tries to write stats to
disk, it learns when it should schedule the next such attempt.

This patch also enables all statistics to be stopped and restarted at a
later time.

This patch comes with a few refactorings, some of which were not easily
doable without the patch.
parent cafd868a
Loading
Loading
Loading
Loading

changes/statsswitch

0 → 100644
+3 −0
Original line number Diff line number Diff line
  o Major features:
    - Allow enabling or disabling *Statistics while Tor is running.
+43 −12
Original line number Diff line number Diff line
@@ -1246,8 +1246,15 @@ options_act(or_options_t *old_options)
    }

    if (! bool_eq(options->BridgeRelay, old_options->BridgeRelay)) {
      log_info(LD_GENERAL, "Bridge status changed.  Forgetting GeoIP stats.");
      geoip_remove_old_clients(time(NULL)+(2*60*60));
      if (options->BridgeRelay) {
        geoip_bridge_stats_init(time(NULL) + (2 * 60 * 60));
        log_info(LD_CONFIG, "We are acting as a bridge now.  Starting new "
                 "GeoIP stats interval in 2 hours from now.");
      } else {
        geoip_bridge_stats_term();
        log_info(LD_GENERAL, "We are no longer acting as a bridge.  "
                 "Forgetting GeoIP stats.");
      }
    }

    if (options_transition_affects_workers(old_options, options)) {
@@ -1317,6 +1324,40 @@ options_act(or_options_t *old_options)
    }
  }

  if (options->CellStatistics || options->DirReqStatistics ||
      options->EntryStatistics || options->ExitPortStatistics) {
    time_t now = time(NULL);
    if ((!old_options || !old_options->CellStatistics) &&
        options->CellStatistics)
      rep_hist_buffer_stats_init(now);
    if ((!old_options || !old_options->DirReqStatistics) &&
        options->DirReqStatistics)
      geoip_dirreq_stats_init(now);
    if ((!old_options || !old_options->EntryStatistics) &&
        options->EntryStatistics)
      geoip_entry_stats_init(now);
    if ((!old_options || !old_options->ExitPortStatistics) &&
        options->ExitPortStatistics)
      rep_hist_exit_stats_init(now);
    if (!old_options)
      log_notice(LD_CONFIG, "Configured to measure statistics. Look for "
                 "the *-stats files that will first be written to the "
                 "data directory in 24 hours from now.");
  }

  if (old_options && old_options->CellStatistics &&
      !options->CellStatistics)
    rep_hist_buffer_stats_term();
  if (old_options && old_options->DirReqStatistics &&
      !options->DirReqStatistics)
    geoip_dirreq_stats_term();
  if (old_options && old_options->EntryStatistics &&
      !options->EntryStatistics)
    geoip_entry_stats_term();
  if (old_options && old_options->ExitPortStatistics &&
      !options->ExitPortStatistics)
    rep_hist_exit_stats_term();

  /* Check if we need to parse and add the EntryNodes config option. */
  if (options->EntryNodes &&
      (!old_options ||
@@ -3688,16 +3729,6 @@ options_transition_allowed(or_options_t *old, or_options_t *new_val,
    return -1;
  }

  if (old->CellStatistics != new_val->CellStatistics ||
      old->DirReqStatistics != new_val->DirReqStatistics ||
      old->EntryStatistics != new_val->EntryStatistics ||
      old->ExitPortStatistics != new_val->ExitPortStatistics) {
    *msg = tor_strdup("While Tor is running, changing either "
                      "CellStatistics, DirReqStatistics, EntryStatistics, "
                      "or ExitPortStatistics is not allowed.");
    return -1;
  }

  if (old->DisableAllSwap != new_val->DisableAllSwap) {
    *msg = tor_strdup("While Tor is running, changing DisableAllSwap "
                      "is not allowed.");
+186 −215

File changed.

Preview size limit exceeded, changes collapsed.

+7 −8
Original line number Diff line number Diff line
@@ -29,12 +29,8 @@ void geoip_remove_old_clients(time_t cutoff);

void geoip_note_ns_response(geoip_client_action_t action,
                            geoip_ns_response_t response);
time_t geoip_get_history_start(void);
char *geoip_get_client_history_dirreq(time_t now,
                                      geoip_client_action_t action);
char *geoip_get_client_history_bridge(time_t now,
                                      geoip_client_action_t action);
char *geoip_get_request_history(time_t now, geoip_client_action_t action);
char *geoip_get_client_history(geoip_client_action_t action);
char *geoip_get_request_history(geoip_client_action_t action);
int getinfo_helper_geoip(control_connection_t *control_conn,
                         const char *question, char **answer,
                         const char **errmsg);
@@ -46,11 +42,14 @@ void geoip_change_dirreq_state(uint64_t dirreq_id, dirreq_type_t type,
                               dirreq_state_t new_state);

void geoip_dirreq_stats_init(time_t now);
void geoip_dirreq_stats_write(time_t now);
time_t geoip_dirreq_stats_write(time_t now);
void geoip_dirreq_stats_term(void);
void geoip_entry_stats_init(time_t now);
void geoip_entry_stats_write(time_t now);
time_t geoip_entry_stats_write(time_t now);
void geoip_entry_stats_term(void);
void geoip_bridge_stats_init(time_t now);
time_t geoip_bridge_stats_write(time_t now);
void geoip_bridge_stats_term(void);
const char *geoip_get_bridge_stats_extrainfo(time_t);
const char *geoip_get_bridge_stats_controller(time_t);

+24 −34

File changed.

Preview size limit exceeded, changes collapsed.

Loading