host function profiling

This commit is contained in:
jacob 2025-02-07 15:52:25 -06:00
parent 72728e6a98
commit 6f7c19e053
2 changed files with 302 additions and 295 deletions

View File

@ -11,14 +11,12 @@
//#define HOST_NETWORK_ADDRESS_NONE //#define HOST_NETWORK_ADDRESS_NONE
#define PACKET_MAGIC 0xd9e3b8b6 #define PACKET_MAGIC 0xd9e3b8b6
#define PACKET_CHUNK_MAX_LEN 256 #define PACKET_MSG_CHUNK_MAX_LEN 1024
#define PACKET_DATA_MAX_LEN (1280 * 2) /* Give enough space for msg chunk + header */
#define NUM_CHANNEL_LOOKUP_BUCKETS 512 #define NUM_CHANNEL_LOOKUP_BUCKETS 512
#define NUM_MSG_ASSEMBLER_LOOKUP_BUCKETS 16384 #define NUM_MSG_ASSEMBLER_LOOKUP_BUCKETS 16384
/* Give enough space for msg chunk + header */
#define PACKET_DATA_MAX_LEN (PACKET_CHUNK_MAX_LEN * 2)
enum host_packet_kind { enum host_packet_kind {
HOST_PACKET_KIND_NONE, HOST_PACKET_KIND_NONE,
HOST_PACKET_KIND_TRY_CONNECT, HOST_PACKET_KIND_TRY_CONNECT,
@ -399,7 +397,7 @@ INTERNAL struct host_msg_assembler *host_msg_assembler_alloc(struct host_channel
ma->num_chunks_total = chunk_count; ma->num_chunks_total = chunk_count;
/* FIXME: Use buddy allocator or something */ /* FIXME: Use buddy allocator or something */
u64 data_size = chunk_count * PACKET_CHUNK_MAX_LEN; u64 data_size = chunk_count * PACKET_MSG_CHUNK_MAX_LEN;
u64 bitmap_size = ((chunk_count - 1) / 8) + 1; u64 bitmap_size = ((chunk_count - 1) / 8) + 1;
ma->testarena = arena_alloc(data_size + bitmap_size); ma->testarena = arena_alloc(data_size + bitmap_size);
/* FIXME: Ensure chunk_count > 0 */ /* FIXME: Ensure chunk_count > 0 */
@ -627,6 +625,8 @@ void host_update(struct host *host)
i64 now_ns = sys_time_ns(); i64 now_ns = sys_time_ns();
{
__profscope(host_update_read_packets);
struct string read_buff = ZI; struct string read_buff = ZI;
read_buff.len = PACKET_DATA_MAX_LEN; read_buff.len = PACKET_DATA_MAX_LEN;
read_buff.text = arena_push_array(scratch.arena, u8, read_buff.len); read_buff.text = arena_push_array(scratch.arena, u8, read_buff.len);
@ -717,7 +717,7 @@ void host_update(struct host *host)
u64 chunk_count = br_read_var_uint(&br); u64 chunk_count = br_read_var_uint(&br);
b32 is_last_chunk = (chunk_id + 1) == chunk_count; b32 is_last_chunk = (chunk_id + 1) == chunk_count;
u64 data_len = is_last_chunk ? (br_read_u8(&br) + 1) : PACKET_CHUNK_MAX_LEN; u64 data_len = is_last_chunk ? (br_read_var_uint(&br) + 1) : PACKET_MSG_CHUNK_MAX_LEN;
struct host_msg_assembler *ma = host_get_msg_assembler(host, channel->id, msg_id); struct host_msg_assembler *ma = host_get_msg_assembler(host, channel->id, msg_id);
if (!ma) { if (!ma) {
@ -728,7 +728,7 @@ void host_update(struct host *host)
if (!host_msg_assembler_is_chunk_filled(ma, chunk_id)) { if (!host_msg_assembler_is_chunk_filled(ma, chunk_id)) {
u8 *src = br_seek(&br, data_len); u8 *src = br_seek(&br, data_len);
if (src) { if (src) {
u8 *dst = &ma->data[chunk_id * PACKET_CHUNK_MAX_LEN]; u8 *dst = &ma->data[chunk_id * PACKET_MSG_CHUNK_MAX_LEN];
MEMCPY(dst, src, data_len); MEMCPY(dst, src, data_len);
if (is_last_chunk) { if (is_last_chunk) {
ma->last_chunk_len = data_len; ma->last_chunk_len = data_len;
@ -741,7 +741,7 @@ void host_update(struct host *host)
/* TODO: Message ordering */ /* TODO: Message ordering */
struct host_queued_event *queued_event = host_queued_event_alloc_and_append(host); struct host_queued_event *queued_event = host_queued_event_alloc_and_append(host);
struct string data = ZI; struct string data = ZI;
data.len = ((chunk_count - 1) * PACKET_CHUNK_MAX_LEN) + ma->last_chunk_len; data.len = ((chunk_count - 1) * PACKET_MSG_CHUNK_MAX_LEN) + ma->last_chunk_len;
data.text = arena_push_array(&host->queued_event_arena, u8, data.len); data.text = arena_push_array(&host->queued_event_arena, u8, data.len);
MEMCPY(data.text, ma->data, data.len); MEMCPY(data.text, ma->data, data.len);
queued_event->event.kind = HOST_EVENT_KIND_MSG; queued_event->event.kind = HOST_EVENT_KIND_MSG;
@ -771,8 +771,11 @@ void host_update(struct host *host)
recv_buffer->first_packet = NULL; recv_buffer->first_packet = NULL;
recv_buffer->last_packet = NULL; recv_buffer->last_packet = NULL;
arena_reset(&recv_buffer->arena); arena_reset(&recv_buffer->arena);
}
/* Update channels */ /* Update channels */
{
__profscope(host_update_channels);
for (u64 i = 0; i < host->num_channels_reserved; ++i) { for (u64 i = 0; i < host->num_channels_reserved; ++i) {
struct host_channel *channel = &host->channels[i]; struct host_channel *channel = &host->channels[i];
if (channel->valid) { if (channel->valid) {
@ -822,9 +825,12 @@ void host_update(struct host *host)
} }
} }
} }
}
/* Process cmds */ /* Process cmds */
/* TODO: Unreliable packets don't need to be allocated into unreliable packet queue, should just send them and forget */ /* TODO: Unreliable packets don't need to be allocated into unreliable packet queue, should just send them and forget */
{
__profscope(host_update_process_cmds);
for (struct host_cmd *cmd = host->first_cmd; cmd; cmd = cmd->next) { for (struct host_cmd *cmd = host->first_cmd; cmd; cmd = cmd->next) {
enum host_cmd_kind kind = cmd->kind; enum host_cmd_kind kind = cmd->kind;
struct host_channel_id channel_id = cmd->channel_id; struct host_channel_id channel_id = cmd->channel_id;
@ -834,8 +840,6 @@ void host_update(struct host *host)
switch (kind) { switch (kind) {
case HOST_CMD_KIND_TRY_CONNECT: case HOST_CMD_KIND_TRY_CONNECT:
{ {
logf_info("TRY CONNECT TO %F family: %F, valid: %F", FMT_STR(sock_string_from_address(scratch.arena, channel->address)), FMT_SINT(channel->address.family), FMT_SINT(channel->address.valid));
u8 packet_flags = 0; u8 packet_flags = 0;
struct host_packet *host_packet = host_channel_packet_alloc(channel, false); struct host_packet *host_packet = host_channel_packet_alloc(channel, false);
struct byte_writer bw = bw_from_buffer(STRING_FROM_ARRAY(host_packet->data)); struct byte_writer bw = bw_from_buffer(STRING_FROM_ARRAY(host_packet->data));
@ -878,18 +882,18 @@ void host_update(struct host *host)
u64 chunk_count = 0; u64 chunk_count = 0;
if (msg.len > 0) { if (msg.len > 0) {
chunk_count = (msg.len - 1) / PACKET_CHUNK_MAX_LEN; chunk_count = (msg.len - 1) / PACKET_MSG_CHUNK_MAX_LEN;
} }
chunk_count += 1; chunk_count += 1;
u64 msg_id = ++channel->last_sent_msg_id; u64 msg_id = ++channel->last_sent_msg_id;
for (u64 i = 0; i < chunk_count; ++i) { for (u64 i = 0; i < chunk_count; ++i) {
u64 data_len = PACKET_CHUNK_MAX_LEN; u64 data_len = PACKET_MSG_CHUNK_MAX_LEN;
b32 is_last_chunk = i + 1 == chunk_count; b32 is_last_chunk = i + 1 == chunk_count;
if (is_last_chunk) { if (is_last_chunk) {
data_len = msg.len % PACKET_CHUNK_MAX_LEN; data_len = msg.len % PACKET_MSG_CHUNK_MAX_LEN;
} }
u8 *data = msg.text + (i * PACKET_CHUNK_MAX_LEN); u8 *data = msg.text + (i * PACKET_MSG_CHUNK_MAX_LEN);
struct host_packet *host_packet = host_channel_packet_alloc(channel, is_reliable); struct host_packet *host_packet = host_channel_packet_alloc(channel, is_reliable);
struct byte_writer bw = bw_from_buffer(STRING_FROM_ARRAY(host_packet->data)); struct byte_writer bw = bw_from_buffer(STRING_FROM_ARRAY(host_packet->data));
bw_write_u32(&bw, PACKET_MAGIC); bw_write_u32(&bw, PACKET_MAGIC);
@ -904,7 +908,7 @@ void host_update(struct host *host)
bw_write_var_uint(&bw, chunk_count); bw_write_var_uint(&bw, chunk_count);
if (is_last_chunk) { if (is_last_chunk) {
/* FIXME: Ensure data_len can never be 0 */ /* FIXME: Ensure data_len can never be 0 */
bw_write_u8(&bw, data_len - 1); bw_write_var_uint(&bw, data_len - 1);
} }
bw_write_buffer(&bw, STRING(data_len, data)); bw_write_buffer(&bw, STRING(data_len, data));
host_packet->data_len = bw_pos(&bw); host_packet->data_len = bw_pos(&bw);
@ -915,9 +919,12 @@ void host_update(struct host *host)
} }
} }
} }
}
/* Process packets */ /* Process packets */
/* TODO: Aggregate small packets */ /* TODO: Aggregate small packets */
{
__profscope(host_update_send_packets);
for (u64 i = 0; i < host->num_channels_reserved; ++i) { for (u64 i = 0; i < host->num_channels_reserved; ++i) {
struct sock *sock = host->sock; struct sock *sock = host->sock;
struct host_channel *channel = &host->channels[i]; struct host_channel *channel = &host->channels[i];
@ -941,6 +948,7 @@ void host_update(struct host *host)
} }
} }
} }
}
/* Reset cmds */ /* Reset cmds */

View File

@ -65,7 +65,6 @@ INTERNAL struct sock_address sock_address_from_ip_port_cstr(char *ip_cstr, char
MEMCPY(res.ipnb, (void *)&sockaddr->sin_addr, 4); MEMCPY(res.ipnb, (void *)&sockaddr->sin_addr, 4);
break; break;
} else if (ai_res->ai_family == AF_INET6) { } else if (ai_res->ai_family == AF_INET6) {
/* FIXME: Why must this be disabled to work? */
#if 0 #if 0
struct sockaddr_in6 *sockaddr = (struct sockaddr_in6 *)ai_res->ai_addr; struct sockaddr_in6 *sockaddr = (struct sockaddr_in6 *)ai_res->ai_addr;
res.valid = true; res.valid = true;