]> git.ipfire.org Git - thirdparty/tor.git/commitdiff
relay: Add the OOM invocation metrics
authorDavid Goulet <dgoulet@torproject.org>
Thu, 15 Apr 2021 18:23:47 +0000 (14:23 -0400)
committerDavid Goulet <dgoulet@torproject.org>
Wed, 12 May 2021 15:58:25 +0000 (11:58 -0400)
With this commit, a relay now emits metrics event on the MetricsPort
related to the OOM invocation for:

  - DNS cache
  - GeoIP database
  - Cell queues
  - HSDir caches

Everytime the OOM is invoked, the number of bytes is added to the
metrics counter for that specific type of invocation.

Related to #40367

Signed-off-by: David Goulet <dgoulet@torproject.org>
src/core/or/circuitlist.c
src/core/or/circuitlist.h
src/core/or/relay.c
src/core/or/relay.h
src/feature/relay/relay_metrics.c
src/feature/relay/relay_metrics.h

index 4f62284e299f44920d5bc472f97c6eb94269d458..46be358decc22752bfcfec757a46f79464d2db25 100644 (file)
@@ -2586,8 +2586,10 @@ conns_compare_by_buffer_age_(const void **a_, const void **b_)
 
 /** We're out of memory for cells, having allocated <b>current_allocation</b>
  * bytes' worth.  Kill the 'worst' circuits until we're under
- * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage. */
-void
+ * FRACTION_OF_DATA_TO_RETAIN_ON_OOM of our maximum usage.
+ *
+ * Return the number of bytes removed. */
+size_t
 circuits_handle_oom(size_t current_allocation)
 {
   smartlist_t *circlist;
@@ -2613,12 +2615,11 @@ circuits_handle_oom(size_t current_allocation)
              tor_zstd_get_total_allocation(),
              tor_lzma_get_total_allocation(),
              hs_cache_get_total_allocation());
-
   {
     size_t mem_target = (size_t)(get_options()->MaxMemInQueues *
                                  FRACTION_OF_DATA_TO_RETAIN_ON_OOM);
     if (current_allocation <= mem_target)
-      return;
+      return 0;
     mem_to_recover = current_allocation - mem_target;
   }
 
@@ -2697,7 +2698,6 @@ circuits_handle_oom(size_t current_allocation)
   } SMARTLIST_FOREACH_END(circ);
 
  done_recovering_mem:
-
   log_notice(LD_GENERAL, "Removed %"TOR_PRIuSZ" bytes by killing %d circuits; "
              "%d circuits remain alive. Also killed %d non-linked directory "
              "connections.",
@@ -2705,6 +2705,8 @@ circuits_handle_oom(size_t current_allocation)
              n_circuits_killed,
              smartlist_len(circlist) - n_circuits_killed,
              n_dirconns_killed);
+
+  return mem_recovered;
 }
 
 /** Verify that circuit <b>c</b> has all of its invariants
index f5791d7c12ba0fc3a777682c2b9f6741921769ae..147e2cb2f81b588b2170873d7e215171bbefbd57 100644 (file)
@@ -232,7 +232,7 @@ int circuit_count_pending_on_channel(channel_t *chan);
 
 MOCK_DECL(void, assert_circuit_ok,(const circuit_t *c));
 void circuit_free_all(void);
-void circuits_handle_oom(size_t current_allocation);
+size_t circuits_handle_oom(size_t current_allocation);
 
 void circuit_clear_testing_cell_stats(circuit_t *circ);
 
index 2248b2c180aac068ff8307afb84f9b68c11b06b6..7e1f1dc43d3c14b56e0e33ffde55d57f66701a7f 100644 (file)
@@ -2701,11 +2701,18 @@ cell_queues_get_total_allocation(void)
 /** The time at which we were last low on memory. */
 static time_t last_time_under_memory_pressure = 0;
 
+/** Statistics on how many bytes were removed by the OOM per type. */
+uint64_t oom_stats_n_bytes_removed_dns = 0;
+uint64_t oom_stats_n_bytes_removed_cell = 0;
+uint64_t oom_stats_n_bytes_removed_geoip = 0;
+uint64_t oom_stats_n_bytes_removed_hsdir = 0;
+
 /** Check whether we've got too much space used for cells.  If so,
  * call the OOM handler and return 1.  Otherwise, return 0. */
 STATIC int
 cell_queues_check_size(void)
 {
+  size_t removed = 0;
   time_t now = time(NULL);
   size_t alloc = cell_queues_get_total_allocation();
   alloc += half_streams_get_total_allocation();
@@ -2730,20 +2737,27 @@ cell_queues_check_size(void)
       if (hs_cache_total > get_options()->MaxMemInQueues / 5) {
         const size_t bytes_to_remove =
           hs_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
-        alloc -= hs_cache_handle_oom(now, bytes_to_remove);
+        removed = hs_cache_handle_oom(now, bytes_to_remove);
+        oom_stats_n_bytes_removed_hsdir += removed;
+        alloc -= removed;
       }
       if (geoip_client_cache_total > get_options()->MaxMemInQueues / 5) {
         const size_t bytes_to_remove =
           geoip_client_cache_total -
           (size_t)(get_options()->MaxMemInQueues / 10);
-        alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
+        removed = geoip_client_cache_handle_oom(now, bytes_to_remove);
+        oom_stats_n_bytes_removed_geoip += removed;
+        alloc -= removed;
       }
       if (dns_cache_total > get_options()->MaxMemInQueues / 5) {
         const size_t bytes_to_remove =
           dns_cache_total - (size_t)(get_options()->MaxMemInQueues / 10);
-        alloc -= dns_cache_handle_oom(now, bytes_to_remove);
+        removed = dns_cache_handle_oom(now, bytes_to_remove);
+        oom_stats_n_bytes_removed_dns += removed;
+        alloc -= removed;
       }
-      circuits_handle_oom(alloc);
+      removed = circuits_handle_oom(alloc);
+      oom_stats_n_bytes_removed_cell += removed;
       return 1;
     }
   }
index 2f337d5d1633d8caf4c4c74a0cee1ee0efd0c160..eac920f4912db4fa312c122227f070db947d1128 100644 (file)
@@ -49,6 +49,11 @@ extern uint64_t stats_n_data_bytes_packaged;
 extern uint64_t stats_n_data_cells_received;
 extern uint64_t stats_n_data_bytes_received;
 
+extern uint64_t oom_stats_n_bytes_removed_dns;
+extern uint64_t oom_stats_n_bytes_removed_cell;
+extern uint64_t oom_stats_n_bytes_removed_geoip;
+extern uint64_t oom_stats_n_bytes_removed_hsdir;
+
 void dump_cell_pool_usage(int severity);
 size_t packed_cell_mem_cost(void);
 
index 3d392847e1403613a1bd1ce5f4354456ca9db683..2686249548e91ad6c1e7ab0fdc4332352366db35 100644 (file)
@@ -10,6 +10,9 @@
 
 #include "orconfig.h"
 
+#include "core/or/or.h"
+#include "core/or/relay.h"
+
 #include "lib/malloc/malloc.h"
 #include "lib/container/smartlist.h"
 #include "lib/metrics/metrics_store.h"
 
 #include "feature/relay/relay_metrics.h"
 
+/** Declarations of each fill function for metrics defined in base_metrics. */
+static void fill_oom_values(void);
+
 /** The base metrics that is a static array of metrics added to the metrics
  * store.
  *
  * The key member MUST be also the index of the entry in the array. */
-static const relay_metrics_entry_t base_metrics[] = {};
+static const relay_metrics_entry_t base_metrics[] =
+{
+  {
+    .key = RELAY_METRICS_NUM_OOM_BYTES,
+    .type = METRICS_TYPE_COUNTER,
+    .name = METRICS_NAME(relay_load_oom_bytes_total),
+    .help = "Total number of bytes the OOM has freed by subsystem",
+    .fill_fn = fill_oom_values,
+  },
+};
 static const size_t num_base_metrics = ARRAY_LENGTH(base_metrics);
 
 /** The only and single store of all the relay metrics. */
 static metrics_store_t *the_store;
 
+/** Fill function for the RELAY_METRICS_NUM_OOM_BYTES metrics. */
+static void
+fill_oom_values(void)
+{
+  metrics_store_entry_t *sentry;
+  const relay_metrics_entry_t *rentry =
+    &base_metrics[RELAY_METRICS_NUM_OOM_BYTES];
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry, "subsys=cell");
+  metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_cell);
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry, "subsys=dns");
+  metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_dns);
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry, "subsys=geoip");
+  metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_geoip);
+
+  sentry = metrics_store_add(the_store, rentry->type, rentry->name,
+                             rentry->help);
+  metrics_store_entry_add_label(sentry, "subsys=hsdir");
+  metrics_store_entry_update(sentry, oom_stats_n_bytes_removed_hsdir);
+}
+
+/** Reset the global store and fill it with all the metrics from base_metrics
+ * and their associated values.
+ *
+ * To pull this off, every metrics has a "fill" function that is called and in
+ * charge of adding the metrics to the store, appropriate labels and finally
+ * updating the value to report. */
+static void
+fill_store(void)
+{
+  /* Reset the current store, we are about to fill it with all the things. */
+  metrics_store_reset(the_store);
+
+  /* Call the fill function for each metrics. */
+  for (size_t i = 0; i < num_base_metrics; i++) {
+    if (BUG(!base_metrics[i].fill_fn)) {
+      continue;
+    }
+    base_metrics[i].fill_fn();
+  }
+}
+
 /** Return a list of all the relay metrics stores. This is the
  * function attached to the .get_metrics() member of the subsys_t. */
 const smartlist_t *
@@ -36,6 +101,13 @@ relay_metrics_get_stores(void)
    * simply update it. */
   static smartlist_t *stores_list = NULL;
 
+  /* We dynamically fill the store with all the metrics upon a request. The
+   * reason for this is because the exposed metrics of a relay are often
+   * internal counters in the fast path and thus we fetch the value when a
+   * metrics port request arrives instead of keeping a local metrics store of
+   * those values. */
+  fill_store();
+
   if (!stores_list) {
     stores_list = smartlist_new();
     smartlist_add(stores_list, the_store);
index 7bc4760916704ce270421570d0442a39d22cf2ac..7d644badd3ae669ea9ae91ecef394c57efb87c9c 100644 (file)
 #include "lib/container/smartlist.h"
 #include "lib/metrics/metrics_common.h"
 
-#ifdef RELAY_METRICS_ENTRY_PRIVATE
-
 /** Metrics key for each reported metrics. This key is also used as an index in
  * the base_metrics array. */
 typedef enum {
-  /* XXX So code compiles. */
-  PLACEHOLDER = 0,
+  /** Number of OOM invocation. */
+  RELAY_METRICS_NUM_OOM_BYTES = 0,
 } relay_metrics_key_t;
 
 /** The metadata of a relay metric. */
@@ -31,10 +29,10 @@ typedef struct relay_metrics_entry_t {
   const char *name;
   /* Metrics output help comment. */
   const char *help;
+  /* Update value function. */
+  void (*fill_fn)(void);
 } relay_metrics_entry_t;
 
-#endif /* RELAY_METRICS_ENTRY_PRIVATE */
-
 /* Init. */
 void relay_metrics_init(void);
 void relay_metrics_free(void);