Browse Source

Cache the socket for KIST rather than obtaining a lock

Steven Engler 4 years ago
parent
commit
8f74c59b95
3 changed files with 13 additions and 5 deletions
  1. 9 0
      src/core/mainloop/connection.c
  2. 2 0
      src/core/or/connection_st.h
  3. 2 5
      src/core/or/scheduler_kist.c

+ 9 - 0
src/core/mainloop/connection.c

@@ -563,6 +563,7 @@ connection_init(time_t now, connection_t *conn, int type, int socket_family)
   }
 
   conn->s = TOR_INVALID_SOCKET; /* give it a default of 'not used' */
+  conn->scheduler_socket_cache = conn->s;
   conn->conn_array_index = -1; /* also default to 'not used' */
   conn->global_identifier = n_connections_allocated++;
 
@@ -804,6 +805,8 @@ connection_free_minimal(connection_t *conn)
     conn->s = TOR_INVALID_SOCKET;
   }
 
+  conn->scheduler_socket_cache = TOR_INVALID_SOCKET;
+
   if (conn->type == CONN_TYPE_OR &&
       !tor_digest_is_zero(TO_OR_CONN(conn)->identity_digest)) {
     log_warn(LD_BUG, "called on OR conn with non-zeroed identity_digest");
@@ -930,6 +933,7 @@ connection_close_immediate(connection_t *conn)
   if (SOCKET_OK(conn->s))
     tor_close_socket(conn->s);
   conn->s = TOR_INVALID_SOCKET;
+  conn->scheduler_socket_cache = TOR_INVALID_SOCKET;
   if (conn->linked)
     conn->linked_conn_is_closed = 1;
   if (conn->outbuf)
@@ -1885,6 +1889,8 @@ connection_handle_listener_read(connection_t *conn, int new_type)
       newconn->address = tor_addr_to_str_dup(&addr);
     }
 
+    newconn->scheduler_socket_cache = news;
+
     if (!connection_uses_safe_conn(newconn->type)) {
       newconn->s = news;
     } else {
@@ -1938,6 +1944,7 @@ connection_handle_listener_read(connection_t *conn, int new_type)
 
     newconn = connection_new(new_type, conn->socket_family);
     newconn->s = news;
+    newconn->scheduler_socket_cache = news;
 
     /* remember the remote address -- do we have anything sane to put here? */
     tor_addr_make_unspec(&newconn->addr);
@@ -2147,6 +2154,8 @@ connection_connect_sockaddr,(connection_t *conn,
          "Connection to socket %s (sock "TOR_SOCKET_T_FORMAT").",
          inprogress ? "in progress" : "established", s);
 
+  conn->scheduler_socket_cache = s;
+
   if (!connection_uses_safe_conn(conn->type)) {
     conn->s = s;
   } else {

+ 2 - 0
src/core/or/connection_st.h

@@ -92,6 +92,8 @@ struct connection_t {
   /** Our socket; set to TOR_INVALID_SOCKET if this connection is closed,
    * or has no socket. */
   tor_socket_t s;
+  tor_socket_t scheduler_socket_cache;
+
   int conn_array_index; /**< Index into the global connection array. */
 
   struct event *read_event; /**< Libevent event structure. */

+ 2 - 5
src/core/or/scheduler_kist.c

@@ -227,12 +227,8 @@ update_socket_info_impl, (socket_table_ent_t *ent))
   //const tor_socket_t sock =
   //  TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn)->s;
 
-  // TODO: fix this ugly locking
   connection_t *conn = TO_CONN(BASE_CHAN_TO_TLS((channel_t *) ent->chan)->conn);
-  tor_assert(conn->safe_conn != NULL);
-  tor_mutex_acquire(&(conn->safe_conn->lock));
-  const tor_socket_t sock = conn->safe_conn->socket;
-  tor_mutex_release(&(conn->safe_conn->lock));
+  const tor_socket_t sock = conn->scheduler_socket_cache;
 
   if (!SOCKET_OK(sock)) {
     // the socket is closed, so we should not write anything
@@ -348,6 +344,7 @@ update_socket_info_impl, (socket_table_ent_t *ent))
     clamp_double_to_int64(
                  (ent->cwnd * (int64_t)ent->mss) * sock_buf_size_factor) -
     ent->notsent - (int64_t)channel_outbuf_length((channel_t *) ent->chan);
+  // TODO: this call to channel_outbuf_length() requires a lock, which is bad
   if ((tcp_space + extra_space) < 0) {
     /* This means that the "notsent" queue is just too big so we shouldn't put
      * more in the kernel for now. */