[tor-commits] [tor/master] Teach the OOM handler about the DNS cache

nickm at torproject.org nickm at torproject.org
Fri Aug 10 16:39:35 UTC 2018


commit 32db806dfa85b016a2d56d0d5248f17c34b690c4
Author: Neel Chauhan <neel at neelc.org>
Date:   Mon Jul 16 17:36:22 2018 -0400

    Teach the OOM handler about the DNS cache
---
 src/core/or/relay.c     |  7 +++++++
 src/feature/relay/dns.c | 30 ++++++++++++++++++++++++++++++
 src/feature/relay/dns.h |  1 +
 3 files changed, 38 insertions(+)

diff --git a/src/core/or/relay.c b/src/core/or/relay.c
index 32bb69d25..62a4bca84 100644
--- a/src/core/or/relay.c
+++ b/src/core/or/relay.c
@@ -64,6 +64,7 @@
 #include "lib/crypt_ops/crypto_rand.h"
 #include "lib/crypt_ops/crypto_util.h"
 #include "feature/dircache/directory.h"
+#include "feature/relay/dns.h"
 #include "feature/stats/geoip.h"
 #include "feature/hs/hs_cache.h"
 #include "core/mainloop/main.h"
@@ -2537,6 +2538,7 @@ cell_queues_check_size(void)
   const size_t geoip_client_cache_total =
     geoip_client_cache_total_allocation();
   alloc += geoip_client_cache_total;
+  const size_t dns_cache_total = dns_cache_total_allocation();
   if (alloc >= get_options()->MaxMemInQueues_low_threshold) {
     last_time_under_memory_pressure = approx_time();
     if (alloc >= get_options()->MaxMemInQueues) {
@@ -2554,6 +2556,11 @@ cell_queues_check_size(void)
           (size_t)(get_options()->MaxMemInQueues / 10);
         alloc -= geoip_client_cache_handle_oom(now, bytes_to_remove);
       }
+      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);
+      }
       circuits_handle_oom(alloc);
       return 1;
     }
diff --git a/src/feature/relay/dns.c b/src/feature/relay/dns.c
index 8a49829d5..48319b47c 100644
--- a/src/feature/relay/dns.c
+++ b/src/feature/relay/dns.c
@@ -2100,6 +2100,36 @@ dump_dns_mem_usage(int severity)
       (unsigned)hash_mem);
 }
 
+/* Do a round of OOM cleanup on all DNS entries. Return the amount of removed
+ * bytes. It is possible that the returned value is lower than min_remove_bytes
+ * if the caches get emptied out so the caller should be aware of this. */
+size_t
+dns_cache_handle_oom(time_t now, size_t min_remove_bytes)
+{
+  time_t time_inc = 0;
+  size_t total_bytes_removed = 0;
+  size_t current_size = dns_cache_total_allocation();
+
+  do {
+    /* If no DNS entries left, break loop. */
+    if (!dns_cache_entry_count())
+      break;
+
+    /* Get cutoff interval and remove entries. */
+    time_t cutoff = now + time_inc;
+    purge_expired_resolves(cutoff);
+
+    /* Update amount of bytes removed and array size. */
+    size_t bytes_removed = current_size - dns_cache_total_allocation();
+    current_size -= bytes_removed;
+    total_bytes_removed += bytes_removed;
+
+    time_inc += 3600; /* Increase time_inc by 1 hour. */
+  } while (total_bytes_removed < min_remove_bytes);
+
+  return total_bytes_removed;
+}
+
 #ifdef DEBUG_DNS_CACHE
 /** Exit with an assertion if the DNS cache is corrupt. */
 static void
diff --git a/src/feature/relay/dns.h b/src/feature/relay/dns.h
index 1d5266c73..1dd6f903d 100644
--- a/src/feature/relay/dns.h
+++ b/src/feature/relay/dns.h
@@ -40,6 +40,7 @@ int dns_seems_to_be_broken_for_ipv6(void);
 void dns_reset_correctness_checks(void);
 size_t dns_cache_total_allocation(void);
 void dump_dns_mem_usage(int severity);
+size_t dns_cache_handle_oom(time_t now, size_t min_remove_bytes);
 
 #ifdef DNS_PRIVATE
 #include "feature/relay/dns_structs.h"





More information about the tor-commits mailing list