litespeed-quic/test/unittests/test_conn_hash.c

83 lines
2 KiB
C
Raw Normal View History

/* Copyright (c) 2017 - 2018 LiteSpeed Technologies Inc. See LICENSE. */
2017-09-22 21:00:03 +00:00
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/queue.h>
#ifndef WIN32
2017-09-22 21:00:03 +00:00
#include <unistd.h>
#endif
2017-09-22 21:00:03 +00:00
#include "lsquic.h"
#include "lsquic_int_types.h"
#include "lsquic_types.h"
#include "lsquic_conn.h"
#include "lsquic_conn_hash.h"
#include "lsquic_mm.h"
#include "lsquic_malo.h"
#include "lsquic_logger.h"
#include "lsquic.h"
static struct lsquic_conn *
get_new_lsquic_conn (struct malo *malo)
{
struct lsquic_conn *lconn = lsquic_malo_get(malo);
memset(lconn, 0, sizeof(*lconn));
lconn->cn_cid = (uintptr_t) lconn;
return lconn;
}
int
main (int argc, char **argv)
{
struct malo *malo;
struct conn_hash conn_hash;
unsigned n, nelems;
2017-09-22 21:00:03 +00:00
struct lsquic_conn *lconn, *find_lsconn;
int s;
if (argc > 1)
nelems = atoi(argv[1]);
else
nelems = 1000000;
lsquic_log_to_fstream(stderr, LLTS_HHMMSSMS);
lsquic_set_log_level("info");
malo = lsquic_malo_create(sizeof(*lconn));
[API Change, OPTIMIZATION] Only process conns that need to be processed The API is simplified: do not expose the user code to several queues. A "connection queue" is now an internal concept. The user processes connections using the single function lsquic_engine_process_conns(). When this function is called, only those connections are processed that need to be processed. A connection needs to be processed when: 1. New incoming packets have been fed to the connection. 2. User wants to read from a stream that is readable. 3. User wants to write to a stream that is writeable. 4. There are buffered packets that can be sent out. (This means that the user wrote to a stream outside of the lsquic library callback.) 5. A control frame (such as BLOCKED) needs to be sent out. 6. A stream needs to be serviced or delayed stream needs to be created. 7. An alarm rings. 8. Pacer timer expires. To achieve this, the library places the connections into two priority queues (min heaps): 1. Tickable Queue; and 2. Advisory Tick Time queue (ATTQ). Each time lsquic_engine_process_conns() is called, the Tickable Queue is emptied. After the connections have been ticked, they are queried again: if a connection is not being closed, it is placed either in the Tickable Queue if it is ready to be ticked again or it is placed in the Advisory Tick Time Queue. It is assumed that a connection always has at least one timer set (the idle alarm). The connections in the Tickable Queue are arranged in the least recently ticked order. This lets connections that have been quiet longer to get their packets scheduled first. This change means that the library no longer needs to be ticked periodically. The user code can query the library when is the next tick event and schedule it exactly. When connections are processed, only the tickable connections are processed, not *all* the connections. When there are no tick events, it means that no timer event is necessary -- only the file descriptor READ event is active. The following are improvements and simplifications that have been triggered: - Queue of connections with incoming packets is gone. - "Pending Read/Write Events" Queue is gone (along with its history and progress checks). This queue has become the Tickable Queue. - The connection hash no longer needs to track the connection insertion order.
2018-04-09 13:39:38 +00:00
s = conn_hash_init(&conn_hash);
2017-09-22 21:00:03 +00:00
assert(0 == s);
for (n = 0; n < nelems; ++n)
{
lconn = get_new_lsquic_conn(malo);
lconn->cn_if = (void *) (uintptr_t) n; /* This will be used for verification later the test */
find_lsconn = conn_hash_find(&conn_hash, lconn->cn_cid);
2017-09-22 21:00:03 +00:00
assert(!find_lsconn);
s = conn_hash_add(&conn_hash, lconn);
2017-09-22 21:00:03 +00:00
assert(0 == s);
}
assert(nelems == conn_hash_count(&conn_hash));
for (lconn = lsquic_malo_first(malo); lconn;
lconn = lsquic_malo_next(malo))
{
find_lsconn = conn_hash_find(&conn_hash, lconn->cn_cid);
2017-09-22 21:00:03 +00:00
assert(find_lsconn == lconn);
conn_hash_remove(&conn_hash, lconn);
find_lsconn = conn_hash_find(&conn_hash, lconn->cn_cid);
2017-09-22 21:00:03 +00:00
assert(!find_lsconn);
}
assert(0 == conn_hash_count(&conn_hash));
conn_hash_cleanup(&conn_hash);
lsquic_malo_destroy(malo);
exit(0);
}