mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-10 21:23:58 +01:00
r18221@catbus: nickm | 2008-02-19 17:46:16 -0500
New debugging code to figure out what is happending with socket counts. svn:r13593
This commit is contained in:
parent
c126b79f07
commit
632c035ad9
@ -487,6 +487,12 @@ touch_file(const char *fname)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#undef DEBUG_SOCKET_COUNTING
|
||||||
|
#ifdef DEBUG_SOCKET_COUNTING
|
||||||
|
static bitarray_t *open_sockets = NULL;
|
||||||
|
static int max_socket = -1;
|
||||||
|
#endif
|
||||||
|
|
||||||
/** Count of number of sockets currently open. (Undercounts sockets opened by
|
/** Count of number of sockets currently open. (Undercounts sockets opened by
|
||||||
* eventdns and libevent.) */
|
* eventdns and libevent.) */
|
||||||
static int n_sockets_open = 0;
|
static int n_sockets_open = 0;
|
||||||
@ -498,6 +504,15 @@ int
|
|||||||
tor_close_socket(int s)
|
tor_close_socket(int s)
|
||||||
{
|
{
|
||||||
int r = 0;
|
int r = 0;
|
||||||
|
#ifdef DEBUG_SOCKET_COUNTING
|
||||||
|
if (s > max_socket || ! bitarray_is_set(open_sockets, s)) {
|
||||||
|
log_warn(LD_BUG, "Closing a socket (%d) that wasn't returned by tor_open_"
|
||||||
|
"socket(), or that was already closed or something.", s);
|
||||||
|
} else {
|
||||||
|
tor_assert(open_sockets && s <= max_socket);
|
||||||
|
bitarray_clear(open_sockets, s);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
/* On Windows, you have to call close() on fds returned by open(),
|
/* On Windows, you have to call close() on fds returned by open(),
|
||||||
* and closesocket() on fds returned by socket(). On Unix, everything
|
* and closesocket() on fds returned by socket(). On Unix, everything
|
||||||
* gets close()'d. We abstract this difference by always using
|
* gets close()'d. We abstract this difference by always using
|
||||||
@ -536,8 +551,25 @@ int
|
|||||||
tor_open_socket(int domain, int type, int protocol)
|
tor_open_socket(int domain, int type, int protocol)
|
||||||
{
|
{
|
||||||
int s = socket(domain, type, protocol);
|
int s = socket(domain, type, protocol);
|
||||||
if (s >= 0)
|
if (s >= 0) {
|
||||||
++n_sockets_open;
|
++n_sockets_open;
|
||||||
|
#ifdef DEBUG_SOCKET_COUNTING
|
||||||
|
if (s > max_socket) {
|
||||||
|
if (max_socket == -1) {
|
||||||
|
open_sockets = bitarray_init_zero(s+128);
|
||||||
|
max_socket = s+128;
|
||||||
|
} else {
|
||||||
|
open_sockets = bitarray_expand(open_sockets, max_socket, s+128);
|
||||||
|
max_socket = s+128;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (bitarray_is_set(open_sockets, s)) {
|
||||||
|
log_warn(LD_BUG, "I thought that %d was already open, but socket() just "
|
||||||
|
"gave it to me!", s);
|
||||||
|
}
|
||||||
|
bitarray_set(open_sockets, s);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -336,6 +336,21 @@ bitarray_init_zero(int n_bits)
|
|||||||
size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
|
size_t sz = (n_bits+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
|
||||||
return tor_malloc_zero(sz*sizeof(unsigned int));
|
return tor_malloc_zero(sz*sizeof(unsigned int));
|
||||||
}
|
}
|
||||||
|
static INLINE bitarray_t *
|
||||||
|
bitarray_expand(bitarray_t *ba, int n_bits_old, int n_bits_new)
|
||||||
|
{
|
||||||
|
size_t sz_old = (n_bits_old+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
|
||||||
|
size_t sz_new = (n_bits_new+BITARRAY_MASK) / (1u << BITARRAY_SHIFT);
|
||||||
|
char *ptr;
|
||||||
|
if (sz_new <= sz_old)
|
||||||
|
return ba;
|
||||||
|
ptr = tor_realloc(ba, sz_new);
|
||||||
|
memset(ptr+sz_old, 0, sz_new-sz_old); /* This does nothing to the older
|
||||||
|
* excess bytes. But they were
|
||||||
|
* already set to 0 by
|
||||||
|
* bitarry_init_zero. */
|
||||||
|
return (bitarray_t*) ptr;
|
||||||
|
}
|
||||||
/** Free the bit array <b>ba</b>. */
|
/** Free the bit array <b>ba</b>. */
|
||||||
static INLINE void
|
static INLINE void
|
||||||
bitarray_free(bitarray_t *ba)
|
bitarray_free(bitarray_t *ba)
|
||||||
|
Loading…
Reference in New Issue
Block a user