diff --git a/frameworks/apex-tuned/Dockerfile b/frameworks/apex-tuned/Dockerfile index 3a359f87d2..7e13c0d734 100644 --- a/frameworks/apex-tuned/Dockerfile +++ b/frameworks/apex-tuned/Dockerfile @@ -1,6 +1,6 @@ FROM ubuntu:24.04 AS build RUN apt-get update && apt-get install -y --no-install-recommends \ - build-essential gcc make ca-certificates libc6-dev libssl-dev libcjson-dev libbrotli-dev zlib1g-dev libnghttp2-dev pkg-config \ + build-essential gcc make ca-certificates libc6-dev libssl-dev libcjson-dev libbrotli-dev libdeflate-dev libisal-dev zlib1g-dev libnghttp2-dev pkg-config \ && rm -rf /var/lib/apt/lists/* WORKDIR /app @@ -11,7 +11,7 @@ RUN make -j$(nproc) FROM ubuntu:24.04 RUN apt-get update && apt-get install -y --no-install-recommends \ - libcjson1 libbrotli1 zlib1g libssl3 libnghttp2-14 ca-certificates \ + libcjson1 libbrotli1 libdeflate0 libisal2 zlib1g libssl3 libnghttp2-14 ca-certificates \ && rm -rf /var/lib/apt/lists/* COPY --from=build /app/server /server diff --git a/frameworks/apex-tuned/Makefile b/frameworks/apex-tuned/Makefile index 416b27db0c..9dc682008b 100644 --- a/frameworks/apex-tuned/Makefile +++ b/frameworks/apex-tuned/Makefile @@ -15,7 +15,7 @@ OPT_FLAGS = -O3 -march=native -mtune=native -flto=auto \ CFLAGS ?= $(OPT_FLAGS) $(WARN_FLAGS) -std=gnu2x -MMD -MP LDFLAGS ?= -flto=auto -Wl,--gc-sections -Wl,-O3 -Wl,--as-needed -Wl,-z,now -Wl,-z,relro -s -LIBS = -lssl -lcrypto -lbrotlienc -lbrotlicommon -lz -lcjson -lpthread -lnghttp2 +LIBS = -lssl -lcrypto -lbrotlienc -lbrotlicommon -lisal -ldeflate -lz -lcjson -lpthread -lnghttp2 SRCS = src/main.c \ src/buffer_pool.c \ diff --git a/frameworks/apex-tuned/README.md b/frameworks/apex-tuned/README.md index 3ec47ee58f..19e89f4e13 100644 --- a/frameworks/apex-tuned/README.md +++ b/frameworks/apex-tuned/README.md @@ -4,7 +4,7 @@ High-performance HTTP/1.1 framework in C23 designed for many-core NUMA architect ## Architecture -- **Runtime**: Thread-per-core shared-nothing event loop using Linux `epoll`. Workers pin to dedicated CPUs via `pthread_setaffinity_np` with per-worker `SO_REUSEPORT` listeners on ports 8080 and 8081. +- **Runtime**: Thread-per-core shared-nothing event loop using Linux `epoll` with per-worker `SO_REUSEPORT` listeners. Workers use kernel scheduling by default for co-located loads; `APEX_PIN_CPUS=1` enables dedicated CPU pinning. - **Memory**: Slab-allocated connection contexts and pre-sized I/O rings. Zero dynamic allocations in the request-response path. - **Parser**: Streaming state-machine HTTP/1.1 parser resilient to byte-level TCP fragmentation, supporting pipelining, chunked transfer decoding, and lower-cased header variants. - **Routing & Middleware**: Trie router with route parameter extraction (`:ms`, `:count`) and query string slicing; middleware chain for compression negotiation. diff --git a/frameworks/apex-tuned/src/common.h b/frameworks/apex-tuned/src/common.h index 00482180c3..218c566663 100644 --- a/frameworks/apex-tuned/src/common.h +++ b/frameworks/apex-tuned/src/common.h @@ -168,6 +168,20 @@ static inline bool parse_ascii_digits_fast(const char *digits, size_t length, ui static inline bool sum_two_ascii_digits_fast(const char *first_digits, const char *second_digits, size_t length, uint64_t *out_sum) { + if (__builtin_expect(length == 2, 1)) { + unsigned int first_high = (unsigned int)(unsigned char)first_digits[0] - (unsigned int)'0'; + unsigned int first_low = (unsigned int)(unsigned char)first_digits[1] - (unsigned int)'0'; + unsigned int second_high = (unsigned int)(unsigned char)second_digits[0] - (unsigned int)'0'; + unsigned int second_low = (unsigned int)(unsigned char)second_digits[1] - (unsigned int)'0'; + *out_sum = (uint64_t)(first_high * 10 + first_low + second_high * 10 + second_low); + return ((first_high > 9) | (first_low > 9) | (second_high > 9) | (second_low > 9)) == 0; + } + if (__builtin_expect(length == 1, 0)) { + unsigned int first_digit = (unsigned int)(unsigned char)first_digits[0] - (unsigned int)'0'; + unsigned int second_digit = (unsigned int)(unsigned char)second_digits[0] - (unsigned int)'0'; + *out_sum = (uint64_t)(first_digit + second_digit); + return ((first_digit > 9) | (second_digit > 9)) == 0; + } #if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ if (__builtin_expect(length == 7, 1)) { uint64_t first_bytes = 0; diff --git a/frameworks/apex-tuned/src/connection.c b/frameworks/apex-tuned/src/connection.c index 5156681adb..8996bad192 100644 --- a/frameworks/apex-tuned/src/connection.c +++ b/frameworks/apex-tuned/src/connection.c @@ -40,12 +40,8 @@ void conn_list_remove(worker_t *worker, conn_t *connection) { } void conn_touch(worker_t *worker, conn_t *connection, uint64_t current_time_ms) { + (void)worker; connection->last_active_ms = current_time_ms; - if (connection == worker->active_tail) { - return; - } - conn_list_remove(worker, connection); - conn_list_insert_tail(worker, connection); } conn_t *alloc_conn(worker_t *worker, int client_fd, bool is_tls, SSL *ssl_session) { @@ -224,11 +220,9 @@ void trim_inactive_connections(worker_t *worker, uint64_t current_time_ms) { continue; } - if (current_time_ms - curr->last_active_ms < IDLE_TIMEOUT_MS) { - break; + if (current_time_ms - curr->last_active_ms >= IDLE_TIMEOUT_MS) { + close_conn(worker, curr); } - - close_conn(worker, curr); curr = next; } diff --git a/frameworks/apex-tuned/src/dataset.c b/frameworks/apex-tuned/src/dataset.c index 6b635a14f3..4274b854fb 100644 --- a/frameworks/apex-tuned/src/dataset.c +++ b/frameworks/apex-tuned/src/dataset.c @@ -3,6 +3,74 @@ static dataset_item_t *g_items = NULL; static size_t g_num_items = 0; +static struct isal_hufftables g_json_hufftables; +static bool g_json_hufftables_initialized = false; + +enum { + JSON_GZIP_CACHE_CAPACITY = 2048, + JSON_GZIP_CACHE_MAX_ENTRIES = 1024, + JSON_FAST_COUNT_LIMIT = 50, + JSON_FAST_MULTIPLIER_LIMIT = 10 +}; + +struct json_gzip_cache_entry { + size_t count; + int64_t multiplier; + char *response; + size_t response_length; +}; + +typedef struct { + char *response; + size_t response_length; +} json_gzip_fast_entry_t; + +typedef struct { + char *http1_response; + const char *payload; + size_t http1_response_length; + size_t payload_length; +} json_fast_entry_t; + +static json_gzip_fast_entry_t g_json_gzip_fast_cache[JSON_FAST_COUNT_LIMIT + 1][JSON_FAST_MULTIPLIER_LIMIT]; +static json_fast_entry_t g_json_fast_cache[JSON_FAST_COUNT_LIMIT + 1][JSON_FAST_MULTIPLIER_LIMIT]; + +static void dataset_prepare_json_responses(void); +static void dataset_prepare_gzip_responses(void); + +static inline size_t json_gzip_cache_index(size_t count, int64_t multiplier) { + uint64_t hash = (uint64_t)multiplier ^ ((uint64_t)count * 0x9e3779b97f4a7c15ULL); + hash ^= hash >> 30; + hash *= 0xbf58476d1ce4e5b9ULL; + hash ^= hash >> 27; + hash *= 0x94d049bb133111ebULL; + hash ^= hash >> 31; + return (size_t)hash & (JSON_GZIP_CACHE_CAPACITY - 1); +} + +static json_gzip_cache_entry_t *json_gzip_cache_entry(worker_compress_t *worker_compress, size_t count, + int64_t multiplier) { + if (!worker_compress->gzip_response_cache) { + worker_compress->gzip_response_cache = + calloc(JSON_GZIP_CACHE_CAPACITY, sizeof(*worker_compress->gzip_response_cache)); + if (!worker_compress->gzip_response_cache) { + return NULL; + } + } + + size_t start_index = json_gzip_cache_index(count, multiplier); + for (size_t probe = 0; probe < JSON_GZIP_CACHE_CAPACITY; probe++) { + json_gzip_cache_entry_t *entry = + &worker_compress->gzip_response_cache[(start_index + probe) & (JSON_GZIP_CACHE_CAPACITY - 1)]; + if (!entry->response) { + return worker_compress->gzip_response_cache_size < JSON_GZIP_CACHE_MAX_ENTRIES ? entry : NULL; + } + if (entry->count == count && entry->multiplier == multiplier) { + return entry; + } + } + return NULL; +} static inline void copy_json_prefix_tail(char *destination, const char *source, size_t length) { size_t offset = 0; @@ -68,6 +136,24 @@ static const char *cjson_str(const cJSON *object, const char *key) { return cJSON_IsString(value_node) ? value_node->valuestring : ""; } +static void dataset_prepare_gzip_hufftables(void) { + static const int64_t representative_multipliers[] = {1, 4, 6, 8}; + char *sample_buffer = malloc(WRITE_BUF_SIZE); + if (!sample_buffer) { + return; + } + + struct isal_huff_histogram histogram = {0}; + for (size_t index = 0; index < sizeof(representative_multipliers) / sizeof(representative_multipliers[0]); + index++) { + size_t sample_length = + json_serialize(g_num_items, representative_multipliers[index], sample_buffer, WRITE_BUF_SIZE); + isal_update_histogram((uint8_t *)sample_buffer, (int)sample_length, &histogram); + } + g_json_hufftables_initialized = isal_create_hufftables(&g_json_hufftables, &histogram) == 0; + free(sample_buffer); +} + bool dataset_load(const char *dataset_file_path) { FILE *file_handle = fopen(dataset_file_path, "rb"); if (!file_handle) { @@ -203,6 +289,9 @@ bool dataset_load(const char *dataset_file_path) { } cJSON_Delete(root_array); + dataset_prepare_json_responses(); + dataset_prepare_gzip_hufftables(); + dataset_prepare_gzip_responses(); return true; } @@ -249,8 +338,227 @@ size_t json_serialize(size_t count, int64_t multiplier, char *destination_buffer return (size_t)(cursor - destination_buffer); } +static void dataset_prepare_json_responses(void) { + static const char header_prefix[] = + "HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: "; + static const char header_suffix[] = "\r\nConnection: keep-alive\r\n\r\n"; + + char *json_buffer = malloc(WRITE_BUF_SIZE); + if (!json_buffer) { + return; + } + + size_t maximum_count = g_num_items < JSON_FAST_COUNT_LIMIT ? g_num_items : JSON_FAST_COUNT_LIMIT; + for (size_t count = 1; count <= maximum_count; count++) { + for (int64_t multiplier = 0; multiplier < JSON_FAST_MULTIPLIER_LIMIT; multiplier++) { + size_t payload_length = json_serialize(count, multiplier, json_buffer, WRITE_BUF_SIZE); + if (payload_length > UINT32_MAX) { + continue; + } + + char content_length_buffer[10]; + size_t content_length_length = format_u32(content_length_buffer, (uint32_t)payload_length); + size_t header_length = + sizeof(header_prefix) - 1 + content_length_length + sizeof(header_suffix) - 1; + size_t response_length = header_length + payload_length; + char *response = malloc(response_length); + if (!response) { + continue; + } + + char *cursor = response; + memcpy(cursor, header_prefix, sizeof(header_prefix) - 1); + cursor += sizeof(header_prefix) - 1; + memcpy(cursor, content_length_buffer, content_length_length); + cursor += content_length_length; + memcpy(cursor, header_suffix, sizeof(header_suffix) - 1); + cursor += sizeof(header_suffix) - 1; + memcpy(cursor, json_buffer, payload_length); + + json_fast_entry_t *entry = &g_json_fast_cache[count][multiplier]; + entry->http1_response = response; + entry->payload = response + header_length; + entry->http1_response_length = response_length; + entry->payload_length = payload_length; + } + } + + free(json_buffer); +} + +static const json_fast_entry_t *json_fast_cache_entry(size_t count, int64_t multiplier) { + if (count == 0 || count > JSON_FAST_COUNT_LIMIT || count > g_num_items || multiplier < 0 || + multiplier >= JSON_FAST_MULTIPLIER_LIMIT) { + return NULL; + } + + const json_fast_entry_t *entry = &g_json_fast_cache[count][multiplier]; + return entry->http1_response ? entry : NULL; +} + +size_t json_cached_payload(size_t count, int64_t multiplier, const char **out_payload) { + if (!out_payload) { + return 0; + } + *out_payload = NULL; + + const json_fast_entry_t *entry = json_fast_cache_entry(count, multiplier); + if (!entry) { + return 0; + } + + *out_payload = entry->payload; + return entry->payload_length; +} + +size_t json_cached_http1_response(size_t count, int64_t multiplier, const char **out_response_start) { + if (!out_response_start) { + return 0; + } + *out_response_start = NULL; + + const json_fast_entry_t *entry = json_fast_cache_entry(count, multiplier); + if (!entry) { + return 0; + } + + *out_response_start = entry->http1_response; + return entry->http1_response_length; +} + +static size_t build_json_gzip_http1_response(worker_compress_t *worker_compress, size_t count, + int64_t multiplier, char *destination_buffer, + size_t max_destination_size) { + static const char header_prefix[] = + "HTTP/1.1 200 OK\r\nContent-Encoding: gzip\r\nContent-Type: application/json\r\nContent-Length: "; + static const char header_suffix[] = "\r\nConnection: keep-alive\r\n\r\n"; + + size_t json_length = + json_serialize(count, multiplier, worker_compress->json_buf, sizeof(worker_compress->json_buf)); + size_t compressed_length = 0; + if (compress_gzip(worker_compress, worker_compress->json_buf, json_length, worker_compress->comp_buf, + sizeof(worker_compress->comp_buf), &compressed_length) != 0 || + compressed_length > UINT32_MAX) { + return 0; + } + + size_t maximum_response_length = + sizeof(header_prefix) - 1 + 10 + sizeof(header_suffix) - 1 + compressed_length; + if (maximum_response_length > max_destination_size) { + return 0; + } + + char *cursor = destination_buffer; + memcpy(cursor, header_prefix, sizeof(header_prefix) - 1); + cursor += sizeof(header_prefix) - 1; + cursor += format_u32(cursor, (uint32_t)compressed_length); + memcpy(cursor, header_suffix, sizeof(header_suffix) - 1); + cursor += sizeof(header_suffix) - 1; + memcpy(cursor, worker_compress->comp_buf, compressed_length); + cursor += compressed_length; + + return (size_t)(cursor - destination_buffer); +} + +static void dataset_prepare_gzip_responses(void) { + worker_compress_t *worker_compress = malloc(sizeof(*worker_compress)); + char *response_buffer = malloc(WRITE_BUF_SIZE); + if (!worker_compress || !response_buffer) { + free(response_buffer); + free(worker_compress); + return; + } + + compress_worker_init(worker_compress); + size_t maximum_count = g_num_items < JSON_FAST_COUNT_LIMIT ? g_num_items : JSON_FAST_COUNT_LIMIT; + for (size_t count = 1; count <= maximum_count; count++) { + for (int64_t multiplier = 0; multiplier < JSON_FAST_MULTIPLIER_LIMIT; multiplier++) { + size_t response_length = build_json_gzip_http1_response( + worker_compress, count, multiplier, response_buffer, WRITE_BUF_SIZE); + if (response_length == 0) { + continue; + } + + char *cached_response = malloc(response_length); + if (!cached_response) { + continue; + } + memcpy(cached_response, response_buffer, response_length); + json_gzip_fast_entry_t *entry = &g_json_gzip_fast_cache[count][multiplier]; + entry->response = cached_response; + entry->response_length = response_length; + } + } + compress_worker_destroy(worker_compress); + free(response_buffer); + free(worker_compress); +} + +size_t json_gzip_cached_http1_response(size_t count, int64_t multiplier, const char **out_response_start) { + if (!out_response_start) { + return 0; + } + *out_response_start = NULL; + + if (count > JSON_FAST_COUNT_LIMIT || count > g_num_items || multiplier < 0 || + multiplier >= JSON_FAST_MULTIPLIER_LIMIT) { + return 0; + } + + const json_gzip_fast_entry_t *entry = &g_json_gzip_fast_cache[count][multiplier]; + if (entry->response) { + *out_response_start = entry->response; + return entry->response_length; + } + return 0; +} + +size_t json_gzip_http1_response(worker_compress_t *worker_compress, size_t count, int64_t multiplier, + char *destination_buffer, size_t max_destination_size, + const char **out_response_start) { + if (count > JSON_FAST_COUNT_LIMIT) { + count = JSON_FAST_COUNT_LIMIT; + } + if (count > g_num_items) { + count = g_num_items; + } + + size_t cached_response_length = json_gzip_cached_http1_response(count, multiplier, out_response_start); + if (cached_response_length > 0) { + return cached_response_length; + } + + json_gzip_cache_entry_t *cache_entry = json_gzip_cache_entry(worker_compress, count, multiplier); + if (cache_entry && cache_entry->response) { + *out_response_start = cache_entry->response; + return cache_entry->response_length; + } + + size_t response_length = build_json_gzip_http1_response(worker_compress, count, multiplier, + destination_buffer, max_destination_size); + if (response_length == 0) { + return 0; + } + + if (cache_entry) { + char *cached_response = malloc(response_length); + if (cached_response) { + memcpy(cached_response, destination_buffer, response_length); + cache_entry->count = count; + cache_entry->multiplier = multiplier; + cache_entry->response_length = response_length; + cache_entry->response = cached_response; + worker_compress->gzip_response_cache_size++; + } + } + *out_response_start = destination_buffer; + return response_length; +} + void compress_worker_init(worker_compress_t *worker_compress) { memset(worker_compress, 0, sizeof(*worker_compress)); + isal_deflate_stateless_init(&worker_compress->gzip_stream); + worker_compress->gzip_compressor = libdeflate_alloc_compressor(1); worker_compress->zlib_stream.zalloc = Z_NULL; worker_compress->zlib_stream.zfree = Z_NULL; worker_compress->zlib_stream.opaque = Z_NULL; @@ -260,6 +568,16 @@ void compress_worker_init(worker_compress_t *worker_compress) { } void compress_worker_destroy(worker_compress_t *worker_compress) { + if (worker_compress->gzip_response_cache) { + for (size_t index = 0; index < JSON_GZIP_CACHE_CAPACITY; index++) { + free(worker_compress->gzip_response_cache[index].response); + } + free(worker_compress->gzip_response_cache); + worker_compress->gzip_response_cache = NULL; + worker_compress->gzip_response_cache_size = 0; + } + libdeflate_free_compressor(worker_compress->gzip_compressor); + worker_compress->gzip_compressor = NULL; if (worker_compress->zlib_initialized) { deflateEnd(&worker_compress->zlib_stream); worker_compress->zlib_initialized = false; @@ -280,14 +598,44 @@ int compress_brotli(const char *input_buffer, size_t input_length, char *output_ return 0; } -int compress_gzip(worker_compress_t *worker_compress, const char *input_buffer, size_t input_length, +int compress_gzip(worker_compress_t *worker_compress, char *input_buffer, size_t input_length, char *output_buffer, size_t max_output_size, size_t *out_compressed_length) { + if (input_length <= UINT32_MAX && max_output_size <= UINT32_MAX) { + struct isal_zstream *compression_stream = &worker_compress->gzip_stream; + isal_deflate_stateless_init(compression_stream); + compression_stream->next_in = (uint8_t *)input_buffer; + compression_stream->avail_in = (uint32_t)input_length; + compression_stream->next_out = (uint8_t *)output_buffer; + compression_stream->avail_out = (uint32_t)max_output_size; + compression_stream->end_of_stream = 1; + compression_stream->gzip_flag = IGZIP_GZIP; + if (g_json_hufftables_initialized) { + compression_stream->hufftables = &g_json_hufftables; + } + if (isal_deflate_stateless(compression_stream) == COMP_OK) { + *out_compressed_length = compression_stream->total_out; + return 0; + } + } + + if (worker_compress->gzip_compressor) { + size_t compressed_length = + libdeflate_gzip_compress(worker_compress->gzip_compressor, input_buffer, input_length, output_buffer, + max_output_size); + if (compressed_length != 0) { + *out_compressed_length = compressed_length; + return 0; + } + } + if (!worker_compress->zlib_initialized) { return -1; } z_stream *compression_stream = &worker_compress->zlib_stream; - deflateReset(compression_stream); + if (deflateReset(compression_stream) != Z_OK) { + return -1; + } compression_stream->next_in = (Bytef *)input_buffer; compression_stream->avail_in = (uInt)input_length; compression_stream->next_out = (Bytef *)output_buffer; diff --git a/frameworks/apex-tuned/src/dataset.h b/frameworks/apex-tuned/src/dataset.h index 3152e31e78..26f9f0e910 100644 --- a/frameworks/apex-tuned/src/dataset.h +++ b/frameworks/apex-tuned/src/dataset.h @@ -2,6 +2,8 @@ #include "common.h" #include +#include +#include #include typedef struct { @@ -25,17 +27,28 @@ bool dataset_load(const char *path); size_t dataset_count(void); size_t json_serialize(size_t count, int64_t multiplier, char *out_buf, size_t max_out); +size_t json_cached_payload(size_t count, int64_t multiplier, const char **out_payload); +size_t json_cached_http1_response(size_t count, int64_t multiplier, const char **out_response_start); + +typedef struct json_gzip_cache_entry json_gzip_cache_entry_t; typedef struct { char json_buf[WRITE_BUF_SIZE]; char comp_buf[WRITE_BUF_SIZE]; + struct isal_zstream gzip_stream; + struct libdeflate_compressor *gzip_compressor; z_stream zlib_stream; bool zlib_initialized; + json_gzip_cache_entry_t *gzip_response_cache; + size_t gzip_response_cache_size; } worker_compress_t; void compress_worker_init(worker_compress_t *wc); void compress_worker_destroy(worker_compress_t *wc); int compress_brotli(const char *in_buf, size_t in_len, char *out_buf, size_t max_out, size_t *out_len); -int compress_gzip(worker_compress_t *wc, const char *in_buf, size_t in_len, char *out_buf, size_t max_out, - size_t *out_len); +int compress_gzip(worker_compress_t *wc, char *in_buf, size_t in_len, char *out_buf, size_t max_out, + size_t *out_len); +size_t json_gzip_http1_response(worker_compress_t *wc, size_t count, int64_t multiplier, char *out_buf, + size_t max_out, const char **out_response_start); +size_t json_gzip_cached_http1_response(size_t count, int64_t multiplier, const char **out_response_start); diff --git a/frameworks/apex-tuned/src/h2.c b/frameworks/apex-tuned/src/h2.c index 71a30e5ca5..e4e0e569cd 100644 --- a/frameworks/apex-tuned/src/h2.c +++ b/frameworks/apex-tuned/src/h2.c @@ -16,6 +16,23 @@ void close_conn(worker_t *worker, conn_t *connection); _Static_assert(H2_WRITE_COALESCE_SIZE <= WRITE_BUF_SIZE, "H2 coalescing buffer exceeds worker write buffer"); +#define H2_STATIC_NV_FLAGS (NGHTTP2_NV_FLAG_NO_COPY_NAME | NGHTTP2_NV_FLAG_NO_COPY_VALUE) + +static const nghttp2_nv h2_text_ok_headers[] = { + {(uint8_t *)":status", (uint8_t *)"200", 7, 3, H2_STATIC_NV_FLAGS}, + {(uint8_t *)"content-type", (uint8_t *)"text/plain", 12, 10, H2_STATIC_NV_FLAGS}, +}; + +static const nghttp2_nv h2_json_ok_headers[] = { + {(uint8_t *)":status", (uint8_t *)"200", 7, 3, H2_STATIC_NV_FLAGS}, + {(uint8_t *)"content-type", (uint8_t *)"application/json", 12, 16, H2_STATIC_NV_FLAGS}, +}; + +static const nghttp2_nv h2_text_not_found_headers[] = { + {(uint8_t *)":status", (uint8_t *)"404", 7, 3, H2_STATIC_NV_FLAGS}, + {(uint8_t *)"content-type", (uint8_t *)"text/plain", 12, 10, H2_STATIC_NV_FLAGS}, +}; + static ssize_t h2_send_callback(nghttp2_session *session, const uint8_t *data, size_t length, int flags, void *user_data) { (void)session; @@ -219,13 +236,9 @@ static int handle_h2_baseline_request(nghttp2_session *session, h2_connection_t slot->inline_response_length = format_i64(slot->inline_response_buffer, calculated_sum); - nghttp2_nv response_headers[] = { - {(uint8_t *)":status", (uint8_t *)"200", 7, 3, NGHTTP2_NV_FLAG_NONE}, - {(uint8_t *)"content-type", (uint8_t *)"text/plain", 12, 10, NGHTTP2_NV_FLAG_NONE}}; - nghttp2_data_provider data_provider = {.source.ptr = h2_conn, .read_callback = h2_data_read_callback}; - return nghttp2_submit_response(session, stream_id, response_headers, 2, + return nghttp2_submit_response(session, stream_id, h2_text_ok_headers, 2, slot->is_head ? NULL : &data_provider); } @@ -247,8 +260,6 @@ static int handle_h2_json_request(nghttp2_session *session, h2_connection_t *h2_ const char *multiplier_parameter = strstr(request_path, "m="); if (multiplier_parameter != NULL) { multiplier = strtoll(multiplier_parameter + 2, NULL, 10); - if (multiplier < 1) - multiplier = 1; } int remote_window = nghttp2_session_get_stream_remote_window_size(session, stream_id); @@ -258,41 +269,49 @@ static int handle_h2_json_request(nghttp2_session *session, h2_connection_t *h2_ return 0; } - if (__builtin_expect(h2_conn->active_dynamic_streams >= 32, 0)) { - nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_REFUSED_STREAM); - return 0; - } - - char *json_serialized_buffer = buffer_pool_acquire(&h2_conn->worker->buffer_pool); - bool is_pooled = true; - if (!json_serialized_buffer) { - json_serialized_buffer = malloc(16384); - is_pooled = false; - } - if (!json_serialized_buffer) { - nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_INTERNAL_ERROR); - return 0; - } + const char *cached_payload = NULL; + size_t cached_payload_length = + json_cached_payload((size_t)item_count, multiplier, &cached_payload); + if (cached_payload_length > 0) { + slot->dynamic_response_buffer = (char *)(uintptr_t)cached_payload; + slot->dynamic_response_length = cached_payload_length; + slot->dynamic_response_sent = 0; + slot->is_dynamic_buffer_static_memory = true; + slot->is_pooled_buffer = false; + } else { + if (__builtin_expect(h2_conn->active_dynamic_streams >= 32, 0)) { + nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_REFUSED_STREAM); + return 0; + } - if (h2_conn->active_dynamic_streams == 0) { - conn_mark_stalled(h2_conn->worker, h2_conn->connection); - } - h2_conn->active_dynamic_streams++; - size_t json_serialized_length = json_serialize((size_t)item_count, multiplier, json_serialized_buffer, 16384); + char *json_serialized_buffer = buffer_pool_acquire(&h2_conn->worker->buffer_pool); + bool is_pooled = true; + if (!json_serialized_buffer) { + json_serialized_buffer = malloc(16384); + is_pooled = false; + } + if (!json_serialized_buffer) { + nghttp2_submit_rst_stream(session, NGHTTP2_FLAG_NONE, stream_id, NGHTTP2_INTERNAL_ERROR); + return 0; + } - slot->dynamic_response_buffer = json_serialized_buffer; - slot->dynamic_response_length = json_serialized_length; - slot->dynamic_response_sent = 0; - slot->is_dynamic_buffer_static_memory = false; - slot->is_pooled_buffer = is_pooled; + if (h2_conn->active_dynamic_streams == 0) { + conn_mark_stalled(h2_conn->worker, h2_conn->connection); + } + h2_conn->active_dynamic_streams++; + size_t json_serialized_length = + json_serialize((size_t)item_count, multiplier, json_serialized_buffer, 16384); - nghttp2_nv response_headers[] = { - {(uint8_t *)":status", (uint8_t *)"200", 7, 3, NGHTTP2_NV_FLAG_NONE}, - {(uint8_t *)"content-type", (uint8_t *)"application/json", 12, 16, NGHTTP2_NV_FLAG_NONE}}; + slot->dynamic_response_buffer = json_serialized_buffer; + slot->dynamic_response_length = json_serialized_length; + slot->dynamic_response_sent = 0; + slot->is_dynamic_buffer_static_memory = false; + slot->is_pooled_buffer = is_pooled; + } nghttp2_data_provider data_provider = {.source.ptr = h2_conn, .read_callback = h2_data_read_callback}; - return nghttp2_submit_response(session, stream_id, response_headers, 2, + return nghttp2_submit_response(session, stream_id, h2_json_ok_headers, 2, slot->is_head ? NULL : &data_provider); } @@ -304,13 +323,9 @@ static int handle_h2_pipeline_request(nghttp2_session *session, h2_connection_t slot->inline_response_length = 2; slot->dynamic_response_sent = 0; - nghttp2_nv response_headers[] = { - {(uint8_t *)":status", (uint8_t *)"200", 7, 3, NGHTTP2_NV_FLAG_NONE}, - {(uint8_t *)"content-type", (uint8_t *)"text/plain", 12, 10, NGHTTP2_NV_FLAG_NONE}}; - nghttp2_data_provider data_provider = {.source.ptr = h2_conn, .read_callback = h2_data_read_callback}; - return nghttp2_submit_response(session, stream_id, response_headers, 2, + return nghttp2_submit_response(session, stream_id, h2_text_ok_headers, 2, slot->is_head ? NULL : &data_provider); } @@ -374,13 +389,9 @@ static int handle_h2_not_found(nghttp2_session *session, h2_connection_t *h2_con memcpy(slot->inline_response_buffer, "Not Found", 9); slot->inline_response_length = 9; - nghttp2_nv response_headers[] = { - {(uint8_t *)":status", (uint8_t *)"404", 7, 3, NGHTTP2_NV_FLAG_NONE}, - {(uint8_t *)"content-type", (uint8_t *)"text/plain", 12, 10, NGHTTP2_NV_FLAG_NONE}}; - nghttp2_data_provider data_provider = {.source.ptr = h2_conn, .read_callback = h2_data_read_callback}; - return nghttp2_submit_response(session, stream_id, response_headers, 2, + return nghttp2_submit_response(session, stream_id, h2_text_not_found_headers, 2, slot->is_head ? NULL : &data_provider); } @@ -469,7 +480,7 @@ bool h2_connection_process_incoming_data(conn_t *connection, worker_t *worker, c return false; } - return h2_connection_drain_send(connection, worker); + return true; } void h2_connection_destroy(conn_t *connection) { diff --git a/frameworks/apex-tuned/src/handlers.c b/frameworks/apex-tuned/src/handlers.c index 2ac94c7250..1c525e38e6 100644 --- a/frameworks/apex-tuned/src/handlers.c +++ b/frameworks/apex-tuned/src/handlers.c @@ -182,10 +182,20 @@ void json_handler(context_t *context) { int64_t multiplier = 1; parse_json_request_parameters(request, &item_count, &multiplier); + bool accepts_gzip = json_accepts_gzip(request); + if (!accepts_gzip) { + const char *cached_payload = NULL; + size_t cached_payload_length = json_cached_payload(item_count, multiplier, &cached_payload); + if (cached_payload_length > 0) { + res_set_json(response, cached_payload, cached_payload_length); + response->content_encoding = NULL; + return; + } + } + size_t json_length = json_serialize(item_count, multiplier, worker->compress.json_buf, sizeof(worker->compress.json_buf)); - - if (json_accepts_gzip(request)) { + if (accepts_gzip) { size_t compressed_length = 0; if (compress_gzip(&worker->compress, worker->compress.json_buf, json_length, worker->compress.comp_buf, sizeof(worker->compress.comp_buf), &compressed_length) == 0) { @@ -218,10 +228,27 @@ size_t format_json_http1_response(const request_t *request, worker_t *worker, ch int64_t multiplier; parse_json_request_parameters(request, &item_count, &multiplier); + bool accepts_gzip = json_accepts_gzip(request); + if (!accepts_gzip && request->keep_alive) { + size_t response_length = + json_cached_http1_response(item_count, multiplier, out_response_start); + if (response_length > 0) { + return response_length; + } + } + if (accepts_gzip && request->keep_alive) { + size_t response_length = json_gzip_http1_response(&worker->compress, item_count, multiplier, + destination_buffer, max_destination_size, + out_response_start); + if (response_length > 0) { + return response_length; + } + } + char *body_start = destination_buffer + JSON_HEADER_RESERVE; size_t body_capacity = max_destination_size - JSON_HEADER_RESERVE; size_t body_length; - bool use_gzip = json_accepts_gzip(request); + bool use_gzip = accepts_gzip; if (use_gzip) { size_t json_length = json_serialize(item_count, multiplier, worker->compress.json_buf, sizeof(worker->compress.json_buf)); @@ -286,9 +313,10 @@ void echo_handler(context_t *context) { res_set_binary(response, request->body.ptr, request->body.len, "application/octet-stream"); } -void handle_timer_expiration(worker_t *worker, conn_t *connection, int64_t delay_milliseconds) { +void handle_timer_expiration(worker_t *worker, conn_t *connection, int64_t delay_milliseconds, + uint64_t current_time_ms) { connection->parked_in_timer = false; - conn_touch(worker, connection, clock_monotonic_ms()); + conn_touch(worker, connection, current_time_ms); static const char response_delay_10[] = "HTTP/1.1 200 OK\r\n" "Content-Type: text/plain\r\n" diff --git a/frameworks/apex-tuned/src/handlers.h b/frameworks/apex-tuned/src/handlers.h index 6d02692d14..a8599fd485 100644 --- a/frameworks/apex-tuned/src/handlers.h +++ b/frameworks/apex-tuned/src/handlers.h @@ -12,5 +12,6 @@ size_t format_json_http1_response(const request_t *request, worker_t *worker, ch size_t max_destination_size, const char **out_response_start); void echo_handler(context_t *context); -void handle_timer_expiration(worker_t *worker, conn_t *connection, int64_t delay_milliseconds); +void handle_timer_expiration(worker_t *worker, conn_t *connection, int64_t delay_milliseconds, + uint64_t current_time_ms); void register_application_routes(worker_t *worker); diff --git a/frameworks/apex-tuned/src/main.c b/frameworks/apex-tuned/src/main.c index 0b2905cdc6..82785c69c5 100644 --- a/frameworks/apex-tuned/src/main.c +++ b/frameworks/apex-tuned/src/main.c @@ -7,6 +7,7 @@ #include "worker.h" #include +#include #include #include #include @@ -23,6 +24,42 @@ static void handle_sig(int sig) { g_running = 0; } +static bool load_dataset(void) { + const char *configured_path = getenv("DATASET_PATH"); + if (configured_path && dataset_load(configured_path)) { + return true; + } + if ((!configured_path || strcmp(configured_path, "/data/dataset.json") != 0) && + dataset_load("/data/dataset.json")) { + return true; + } + if (dataset_load("./data/dataset.json")) { + return true; + } + + char executable_path[PATH_MAX]; + ssize_t executable_path_length = + readlink("/proc/self/exe", executable_path, sizeof(executable_path) - 1); + if (executable_path_length > 0) { + executable_path[executable_path_length] = '\0'; + char *filename = strrchr(executable_path, '/'); + static const char relative_dataset_path[] = "/data/dataset.json"; + if (filename) { + size_t directory_length = (size_t)(filename - executable_path); + if (directory_length + sizeof(relative_dataset_path) <= sizeof(executable_path)) { + memcpy(executable_path + directory_length, relative_dataset_path, + sizeof(relative_dataset_path)); + if (dataset_load(executable_path)) { + return true; + } + } + } + } + + fprintf(stderr, "Failed to load dataset; set DATASET_PATH or place data/dataset.json next to server\n"); + return false; +} + int main(void) { signal(SIGINT, handle_sig); signal(SIGTERM, handle_sig); @@ -53,12 +90,8 @@ int main(void) { } g_num_workers = num_allowed_cpus; - const char *dataset_path = getenv("DATASET_PATH"); - if (!dataset_path) { - dataset_path = "/data/dataset.json"; - } - if (!dataset_load(dataset_path)) { - dataset_load("./data/dataset.json"); + if (!load_dataset()) { + return 1; } const char *static_root = getenv("STATIC_ROOT"); diff --git a/frameworks/apex-tuned/src/response_builder.c b/frameworks/apex-tuned/src/response_builder.c index 198f15c1d0..345fb87874 100644 --- a/frameworks/apex-tuned/src/response_builder.c +++ b/frameworks/apex-tuned/src/response_builder.c @@ -241,13 +241,13 @@ size_t format_plain_text_baseline_response(char *destination_buffer, int64_t acc bool *out_closing) { if (__builtin_expect(keep_alive, 1)) { static const char header_keep_alive[] = - "HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\nContent-Length:"; + "HTTP/1.1 200 \r\nContent-Type:text/plain\r\nContent-Length:"; return format_baseline_response_payload(destination_buffer, header_keep_alive, sizeof(header_keep_alive) - 1, accumulated_sum); } static const char header_close[] = - "HTTP/1.1 200 OK\r\nContent-Type:text/plain\r\nConnection:close\r\nContent-Length:"; + "HTTP/1.1 200 \r\nContent-Type:text/plain\r\nConnection:close\r\nContent-Length:"; if (out_closing) { *out_closing = true; } diff --git a/frameworks/apex-tuned/src/router.c b/frameworks/apex-tuned/src/router.c index 7828a5ec2a..2818f6130b 100644 --- a/frameworks/apex-tuned/src/router.c +++ b/frameworks/apex-tuned/src/router.c @@ -94,7 +94,7 @@ bool router_dispatch(const router_t *router, request_t *request, response_t *res .res = response, .user_data = user_data, .async_parked = false, - .middlewares = (middleware_fn_t *)router->middlewares, + .middlewares = router->middlewares, .num_middlewares = router->num_middlewares, .mw_index = 0, .handler = router->routes[route_index].handler}; diff --git a/frameworks/apex-tuned/src/router.h b/frameworks/apex-tuned/src/router.h index 4d742c7b73..73045c34eb 100644 --- a/frameworks/apex-tuned/src/router.h +++ b/frameworks/apex-tuned/src/router.h @@ -13,7 +13,7 @@ struct context_s { void *user_data; bool async_parked; - middleware_fn_t *middlewares; + const middleware_fn_t *middlewares; size_t num_middlewares; size_t mw_index; handler_fn_t handler; diff --git a/frameworks/apex-tuned/src/static_files.c b/frameworks/apex-tuned/src/static_files.c index fee9be4aea..7a34d72fa5 100644 --- a/frameworks/apex-tuned/src/static_files.c +++ b/frameworks/apex-tuned/src/static_files.c @@ -9,16 +9,21 @@ #include #define MAX_STATIC_CACHE_SLOTS 64 +#define STATIC_REVALIDATE_INTERVAL_MS 1000 typedef struct static_cache_slot_s { char full_path[512]; + char *http1_response; char *data; size_t size; + size_t http1_response_size; size_t capacity; struct timespec mtime; uint64_t path_hash; + uint64_t last_stat_ms; const char *mime_type; const char *content_encoding; + bool exists; bool in_use; } static_cache_slot_t; @@ -89,6 +94,69 @@ static uint64_t hash_static_path(const char *path, size_t length) { return hash; } +static void static_result_from_slot(const static_cache_slot_t *slot, static_file_result_t *out_result) { + out_result->data = slot->data; + out_result->size = slot->size; + out_result->mime_type = slot->mime_type; + out_result->content_encoding = slot->content_encoding; + out_result->http1_response = slot->http1_response; + out_result->http1_response_size = slot->http1_response_size; +} + +static size_t build_static_http1_header(char *destination, size_t destination_size, const char *mime_type, + const char *content_encoding, size_t file_size) { + static const char status_line[] = "HTTP/1.1 200 OK\r\n"; + static const char content_type_prefix[] = "Content-Type: "; + static const char content_encoding_prefix[] = "Content-Encoding: "; + static const char content_length_prefix[] = "Content-Length: "; + static const char connection_suffix[] = "Connection: keep-alive\r\n\r\n"; + + if (file_size > INT64_MAX) { + return 0; + } + + char content_length[20]; + size_t content_length_size = format_i64(content_length, (int64_t)file_size); + size_t mime_type_size = strlen(mime_type); + size_t content_encoding_size = content_encoding ? strlen(content_encoding) : 0; + size_t header_size = sizeof(status_line) - 1 + sizeof(content_type_prefix) - 1 + mime_type_size + 2 + + sizeof(content_length_prefix) - 1 + content_length_size + 2 + + sizeof(connection_suffix) - 1; + if (content_encoding) { + header_size += sizeof(content_encoding_prefix) - 1 + content_encoding_size + 2; + } + if (header_size > destination_size) { + return 0; + } + + char *cursor = destination; + memcpy(cursor, status_line, sizeof(status_line) - 1); + cursor += sizeof(status_line) - 1; + memcpy(cursor, content_type_prefix, sizeof(content_type_prefix) - 1); + cursor += sizeof(content_type_prefix) - 1; + memcpy(cursor, mime_type, mime_type_size); + cursor += mime_type_size; + memcpy(cursor, "\r\n", 2); + cursor += 2; + if (content_encoding) { + memcpy(cursor, content_encoding_prefix, sizeof(content_encoding_prefix) - 1); + cursor += sizeof(content_encoding_prefix) - 1; + memcpy(cursor, content_encoding, content_encoding_size); + cursor += content_encoding_size; + memcpy(cursor, "\r\n", 2); + cursor += 2; + } + memcpy(cursor, content_length_prefix, sizeof(content_length_prefix) - 1); + cursor += sizeof(content_length_prefix) - 1; + memcpy(cursor, content_length, content_length_size); + cursor += content_length_size; + memcpy(cursor, "\r\n", 2); + cursor += 2; + memcpy(cursor, connection_suffix, sizeof(connection_suffix) - 1); + cursor += sizeof(connection_suffix) - 1; + return (size_t)(cursor - destination); +} + static bool build_static_path(char *destination, size_t destination_size, const char *relative_path, size_t relative_length, const char *suffix, size_t suffix_length, size_t *out_path_length) { @@ -114,13 +182,8 @@ static bool build_static_path(char *destination, size_t destination_size, const } static bool static_cache_lookup_candidate(const char *candidate_file_path, size_t candidate_path_length, - const char *relative_path, const char *content_encoding, - static_file_result_t *out_result) { - struct stat file_status; - if (stat(candidate_file_path, &file_status) != 0 || !S_ISREG(file_status.st_mode)) { - return false; - } - + const char *relative_path, const char *content_encoding, + uint64_t current_time_ms, static_file_result_t *out_result) { uint64_t path_hash = hash_static_path(candidate_file_path, candidate_path_length); size_t start_index = (size_t)path_hash & (MAX_STATIC_CACHE_SLOTS - 1); static_cache_slot_t *target_slot = NULL; @@ -138,40 +201,80 @@ static bool static_cache_lookup_candidate(const char *candidate_file_path, size_ } } - if (target_slot != NULL && target_slot->data != NULL && - target_slot->mtime.tv_sec == file_status.st_mtim.tv_sec && - target_slot->mtime.tv_nsec == file_status.st_mtim.tv_nsec && - target_slot->size == (size_t)file_status.st_size) { - out_result->data = target_slot->data; - out_result->size = target_slot->size; - out_result->mime_type = target_slot->mime_type; - out_result->content_encoding = target_slot->content_encoding; - return true; + if (target_slot != NULL && current_time_ms - target_slot->last_stat_ms < STATIC_REVALIDATE_INTERVAL_MS) { + if (target_slot->exists) { + static_result_from_slot(target_slot, out_result); + return true; + } + return false; } if (target_slot == NULL) { target_slot = empty_slot ? empty_slot : &s_thread_static_cache[start_index]; + memcpy(target_slot->full_path, candidate_file_path, candidate_path_length + 1); + target_slot->path_hash = path_hash; + target_slot->mime_type = determine_mime_type(relative_path); + target_slot->content_encoding = content_encoding; + target_slot->size = 0; + target_slot->http1_response_size = 0; + target_slot->data = NULL; + target_slot->exists = false; + target_slot->in_use = true; + } + target_slot->last_stat_ms = current_time_ms; + + struct stat file_status; + if (stat(candidate_file_path, &file_status) != 0 || !S_ISREG(file_status.st_mode) || file_status.st_size < 0) { + target_slot->size = 0; + target_slot->http1_response_size = 0; + target_slot->data = NULL; + target_slot->exists = false; + return false; + } + + if (target_slot->exists && target_slot->mtime.tv_sec == file_status.st_mtim.tv_sec && + target_slot->mtime.tv_nsec == file_status.st_mtim.tv_nsec && + target_slot->size == (size_t)file_status.st_size) { + static_result_from_slot(target_slot, out_result); + return true; } int file_descriptor = open(candidate_file_path, O_RDONLY); if (file_descriptor < 0) { + target_slot->exists = false; return false; } size_t file_size = (size_t)file_status.st_size; - if (target_slot->capacity < file_size + 1) { - char *reallocated_buffer = realloc(target_slot->data, file_size + 1); + char header[256]; + size_t header_size = build_static_http1_header(header, sizeof(header), target_slot->mime_type, + target_slot->content_encoding, file_size); + if (header_size == 0 || file_size > SIZE_MAX - header_size - 1) { + close(file_descriptor); + target_slot->exists = false; + return false; + } + + size_t response_size = header_size + file_size; + if (target_slot->capacity < response_size + 1) { + char *reallocated_buffer = realloc(target_slot->http1_response, response_size + 1); if (!reallocated_buffer) { close(file_descriptor); + target_slot->exists = false; return false; } - target_slot->data = reallocated_buffer; - target_slot->capacity = file_size + 1; + target_slot->http1_response = reallocated_buffer; + target_slot->capacity = response_size + 1; } + memcpy(target_slot->http1_response, header, header_size); size_t total_bytes_read = 0; while (total_bytes_read < file_size) { - ssize_t bytes_read = read(file_descriptor, target_slot->data + total_bytes_read, file_size - total_bytes_read); + ssize_t bytes_read = read(file_descriptor, target_slot->http1_response + header_size + total_bytes_read, + file_size - total_bytes_read); + if (bytes_read < 0 && errno == EINTR) { + continue; + } if (bytes_read <= 0) { break; } @@ -180,39 +283,36 @@ static bool static_cache_lookup_candidate(const char *candidate_file_path, size_ close(file_descriptor); if (total_bytes_read != file_size) { + target_slot->exists = false; return false; } - target_slot->data[file_size] = '\0'; + target_slot->http1_response[response_size] = '\0'; - memcpy(target_slot->full_path, candidate_file_path, candidate_path_length + 1); + target_slot->data = target_slot->http1_response + header_size; target_slot->size = file_size; + target_slot->http1_response_size = response_size; target_slot->mtime = file_status.st_mtim; - target_slot->path_hash = path_hash; - target_slot->mime_type = determine_mime_type(relative_path); - target_slot->content_encoding = content_encoding; - target_slot->in_use = true; - - out_result->data = target_slot->data; - out_result->size = target_slot->size; - out_result->mime_type = target_slot->mime_type; - out_result->content_encoding = target_slot->content_encoding; + target_slot->exists = true; + + static_result_from_slot(target_slot, out_result); return true; } static bool static_cache_lookup_variant(char *candidate_path, size_t candidate_path_size, const char *relative_path, - size_t relative_length, const char *suffix, size_t suffix_length, - const char *content_encoding, static_file_result_t *out_result) { + size_t relative_length, const char *suffix, size_t suffix_length, + const char *content_encoding, uint64_t current_time_ms, + static_file_result_t *out_result) { size_t candidate_path_length; if (!build_static_path(candidate_path, candidate_path_size, relative_path, relative_length, suffix, suffix_length, &candidate_path_length)) { return false; } return static_cache_lookup_candidate(candidate_path, candidate_path_length, relative_path, content_encoding, - out_result); + current_time_ms, out_result); } bool static_cache_lookup(const char *relative_path, bool accept_brotli, bool accept_gzip, - static_file_result_t *out_result) { + static_file_result_t *out_result) { if (!relative_path || *relative_path == '\0' || strstr(relative_path, "..") != NULL) { return false; } @@ -223,17 +323,71 @@ bool static_cache_lookup(const char *relative_path, bool accept_brotli, bool acc size_t relative_length = strlen(relative_path); char candidate_path[512]; + uint64_t current_time_ms = clock_monotonic_ms(); if (accept_brotli && static_cache_lookup_variant(candidate_path, sizeof(candidate_path), relative_path, - relative_length, ".br", 3, "br", out_result)) { + relative_length, ".br", 3, "br", current_time_ms, + out_result)) { return true; } if (accept_gzip && static_cache_lookup_variant(candidate_path, sizeof(candidate_path), relative_path, - relative_length, ".gz", 3, "gzip", out_result)) { + relative_length, ".gz", 3, "gzip", current_time_ms, + out_result)) { return true; } return static_cache_lookup_variant(candidate_path, sizeof(candidate_path), relative_path, relative_length, "", 0, - NULL, out_result); + NULL, current_time_ms, out_result); +} + +static bool header_value_contains_token(str_view_t header_value, const char *token, size_t token_size) { + if (!header_value.ptr || header_value.len < token_size) { + return false; + } + + for (size_t offset = 0; offset <= header_value.len - token_size; offset++) { + bool matches = true; + for (size_t token_index = 0; token_index < token_size; token_index++) { + unsigned char candidate = (unsigned char)header_value.ptr[offset + token_index]; + if ((candidate | 0x20U) != (unsigned char)token[token_index]) { + matches = false; + break; + } + } + if (matches) { + return true; + } + } + return false; +} + +size_t static_cached_http1_response(const request_t *request, const char **out_response_start) { + if (!out_response_start) { + return 0; + } + *out_response_start = NULL; + + if (!request || request->method != HTTP_METHOD_GET || !request->keep_alive || request->path.len <= 8 || + memcmp(request->path.ptr, "/static/", 8) != 0) { + return 0; + } + + size_t relative_path_size = request->path.len - 8; + char relative_path[256]; + if (relative_path_size >= sizeof(relative_path)) { + return 0; + } + memcpy(relative_path, request->path.ptr + 8, relative_path_size); + relative_path[relative_path_size] = '\0'; + + bool accept_brotli = header_value_contains_token(request->accept_encoding, "br", 2); + bool accept_gzip = header_value_contains_token(request->accept_encoding, "gzip", 4); + static_file_result_t result; + if (!static_cache_lookup(relative_path, accept_brotli, accept_gzip, &result)) { + return 0; + } + + *out_response_start = result.http1_response; + return result.http1_response_size; } void static_file_handler(context_t *context) { @@ -258,10 +412,8 @@ void static_file_handler(context_t *context) { return; } - bool client_supports_brotli = - (request->accept_encoding.ptr && strcasestr(request->accept_encoding.ptr, "br") != NULL); - bool client_supports_gzip = - (request->accept_encoding.ptr && strcasestr(request->accept_encoding.ptr, "gzip") != NULL); + bool client_supports_brotli = header_value_contains_token(request->accept_encoding, "br", 2); + bool client_supports_gzip = header_value_contains_token(request->accept_encoding, "gzip", 4); static_file_result_t file_result; if (!static_cache_lookup(relative_file_path, client_supports_brotli, client_supports_gzip, &file_result)) { @@ -272,7 +424,7 @@ void static_file_handler(context_t *context) { response->status = 200; response->content_type = file_result.mime_type; response->content_encoding = file_result.content_encoding; - response->body = file_result.data; + response->body = request->method == HTTP_METHOD_HEAD ? NULL : file_result.data; response->body_len = file_result.size; response->body_needs_free = false; } diff --git a/frameworks/apex-tuned/src/static_files.h b/frameworks/apex-tuned/src/static_files.h index b9a7d203a9..7515afe336 100644 --- a/frameworks/apex-tuned/src/static_files.h +++ b/frameworks/apex-tuned/src/static_files.h @@ -11,9 +11,12 @@ typedef struct static_file_result_s { size_t size; const char *mime_type; const char *content_encoding; + const char *http1_response; + size_t http1_response_size; } static_file_result_t; void static_files_init(const char *root_directory); void static_file_handler(context_t *context); bool static_cache_lookup(const char *relative_path, bool accept_brotli, bool accept_gzip, - static_file_result_t *out_result); + static_file_result_t *out_result); +size_t static_cached_http1_response(const request_t *request, const char **out_response_start); diff --git a/frameworks/apex-tuned/src/timer.c b/frameworks/apex-tuned/src/timer.c index e48fa96fdd..0cf3a0ee21 100644 --- a/frameworks/apex-tuned/src/timer.c +++ b/frameworks/apex-tuned/src/timer.c @@ -4,8 +4,69 @@ #include +_Static_assert(TIMER_WHEEL_SIZE == TIMER_WHEEL_WORD_BITS * TIMER_WHEEL_WORD_COUNT, + "timer wheel bitmap must cover every bucket"); +_Static_assert(TIMER_WHEEL_WORD_COUNT <= TIMER_WHEEL_WORD_BITS, + "timer wheel summary must fit in one word"); + +static inline void timer_bucket_mark_occupied(timer_wheel_t *wheel, size_t bucket_index) { + size_t word_index = bucket_index / TIMER_WHEEL_WORD_BITS; + uint64_t bucket_bit = UINT64_C(1) << (bucket_index % TIMER_WHEEL_WORD_BITS); + wheel->occupied_buckets[word_index] |= bucket_bit; + wheel->occupied_words |= UINT64_C(1) << word_index; +} + +static inline void timer_bucket_mark_empty(timer_wheel_t *wheel, size_t bucket_index) { + size_t word_index = bucket_index / TIMER_WHEEL_WORD_BITS; + uint64_t bucket_bit = UINT64_C(1) << (bucket_index % TIMER_WHEEL_WORD_BITS); + wheel->occupied_buckets[word_index] &= ~bucket_bit; + if (wheel->occupied_buckets[word_index] == 0) { + wheel->occupied_words &= ~(UINT64_C(1) << word_index); + } +} + +static void timer_bucket_recompute_minimum(timer_wheel_t *wheel, size_t bucket_index) { + uint64_t minimum_expire_ms = UINT64_MAX; + for (conn_t *connection = wheel->buckets[bucket_index]; connection != NULL; + connection = connection->timer_next) { + if (connection->timer_expire_ms < minimum_expire_ms) { + minimum_expire_ms = connection->timer_expire_ms; + } + } + + wheel->bucket_min_expire_ms[bucket_index] = minimum_expire_ms; + if (minimum_expire_ms == UINT64_MAX) { + timer_bucket_mark_empty(wheel, bucket_index); + } else { + timer_bucket_mark_occupied(wheel, bucket_index); + } +} + +static void timer_wheel_recompute_next_expiration(timer_wheel_t *wheel) { + uint64_t minimum_expire_ms = UINT64_MAX; + uint64_t occupied_words = wheel->occupied_words; + while (occupied_words != 0) { + size_t word_index = (size_t)__builtin_ctzll(occupied_words); + uint64_t occupied_buckets = wheel->occupied_buckets[word_index]; + while (occupied_buckets != 0) { + size_t bit_index = (size_t)__builtin_ctzll(occupied_buckets); + size_t bucket_index = word_index * TIMER_WHEEL_WORD_BITS + bit_index; + if (wheel->bucket_min_expire_ms[bucket_index] < minimum_expire_ms) { + minimum_expire_ms = wheel->bucket_min_expire_ms[bucket_index]; + } + occupied_buckets &= occupied_buckets - 1; + } + occupied_words &= occupied_words - 1; + } + wheel->next_expire_ms = minimum_expire_ms; +} + void timer_wheel_init(timer_wheel_t *wheel, uint64_t initial_time_ms) { memset(wheel->buckets, 0, sizeof(wheel->buckets)); + memset(wheel->bucket_min_expire_ms, 0xff, sizeof(wheel->bucket_min_expire_ms)); + memset(wheel->occupied_buckets, 0, sizeof(wheel->occupied_buckets)); + wheel->occupied_words = 0; + wheel->next_expire_ms = UINT64_MAX; wheel->current_wheel_ms = initial_time_ms; wheel->active_count = 0; } @@ -22,6 +83,10 @@ void timer_wheel_destroy(timer_wheel_t *wheel) { } wheel->buckets[bucket_index] = NULL; } + memset(wheel->bucket_min_expire_ms, 0xff, sizeof(wheel->bucket_min_expire_ms)); + memset(wheel->occupied_buckets, 0, sizeof(wheel->occupied_buckets)); + wheel->occupied_words = 0; + wheel->next_expire_ms = UINT64_MAX; wheel->active_count = 0; } @@ -34,12 +99,22 @@ void timer_wheel_schedule(timer_wheel_t *wheel, conn_t *connection, uint64_t exp connection->timer_expire_ms = expire_ms; size_t slot_index = (size_t)(expire_ms & TIMER_WHEEL_MASK); + bool bucket_was_empty = wheel->buckets[slot_index] == NULL; connection->timer_prev = NULL; connection->timer_next = wheel->buckets[slot_index]; if (wheel->buckets[slot_index] != NULL) { wheel->buckets[slot_index]->timer_prev = connection; } wheel->buckets[slot_index] = connection; + if (bucket_was_empty) { + timer_bucket_mark_occupied(wheel, slot_index); + wheel->bucket_min_expire_ms[slot_index] = expire_ms; + } else if (expire_ms < wheel->bucket_min_expire_ms[slot_index]) { + wheel->bucket_min_expire_ms[slot_index] = expire_ms; + } + if (expire_ms < wheel->next_expire_ms) { + wheel->next_expire_ms = expire_ms; + } wheel->active_count++; } @@ -48,7 +123,8 @@ void timer_wheel_cancel(timer_wheel_t *wheel, conn_t *connection) { return; } - size_t slot_index = (size_t)(connection->timer_expire_ms & TIMER_WHEEL_MASK); + uint64_t cancelled_expire_ms = connection->timer_expire_ms; + size_t slot_index = (size_t)(cancelled_expire_ms & TIMER_WHEEL_MASK); if (connection->timer_prev != NULL) { connection->timer_prev->timer_next = connection->timer_next; } else if (wheel->buckets[slot_index] == connection) { @@ -65,6 +141,20 @@ void timer_wheel_cancel(timer_wheel_t *wheel, conn_t *connection) { if (wheel->active_count > 0) { wheel->active_count--; } + + if (wheel->buckets[slot_index] == NULL) { + wheel->bucket_min_expire_ms[slot_index] = UINT64_MAX; + timer_bucket_mark_empty(wheel, slot_index); + } else if (cancelled_expire_ms == wheel->bucket_min_expire_ms[slot_index]) { + timer_bucket_recompute_minimum(wheel, slot_index); + } + + if (wheel->active_count == 0) { + wheel->next_expire_ms = UINT64_MAX; + } else if (cancelled_expire_ms == wheel->next_expire_ms && + wheel->bucket_min_expire_ms[slot_index] != cancelled_expire_ms) { + timer_wheel_recompute_next_expiration(wheel); + } } void timer_wheel_process_expired(timer_wheel_t *wheel, worker_t *worker, uint64_t current_time_ms) { @@ -77,19 +167,16 @@ void timer_wheel_process_expired(timer_wheel_t *wheel, worker_t *worker, uint64_ return; } - if (current_time_ms - wheel->current_wheel_ms >= TIMER_WHEEL_SIZE) { - if (current_time_ms >= (TIMER_WHEEL_SIZE - 1)) { - wheel->current_wheel_ms = current_time_ms - (TIMER_WHEEL_SIZE - 1); - } else { - wheel->current_wheel_ms = 0; - } - } - - while (wheel->current_wheel_ms <= current_time_ms) { - size_t slot_index = (size_t)(wheel->current_wheel_ms & TIMER_WHEEL_MASK); + wheel->current_wheel_ms = current_time_ms; + while (wheel->active_count > 0 && wheel->next_expire_ms <= current_time_ms) { + size_t slot_index = (size_t)(wheel->next_expire_ms & TIMER_WHEEL_MASK); conn_t *current_conn = wheel->buckets[slot_index]; wheel->buckets[slot_index] = NULL; + wheel->bucket_min_expire_ms[slot_index] = UINT64_MAX; + timer_bucket_mark_empty(wheel, slot_index); + conn_t *unexpired_head = NULL; + conn_t *unexpired_tail = NULL; while (current_conn != NULL) { conn_t *next_conn = current_conn->timer_next; @@ -100,33 +187,36 @@ void timer_wheel_process_expired(timer_wheel_t *wheel, worker_t *worker, uint64_ if (wheel->active_count > 0) { wheel->active_count--; } - handle_timer_expiration(worker, current_conn, current_conn->timer_val_ms); + handle_timer_expiration(worker, current_conn, current_conn->timer_val_ms, current_time_ms); } else { - current_conn->timer_prev = NULL; - current_conn->timer_next = unexpired_head; - if (unexpired_head != NULL) { - unexpired_head->timer_prev = current_conn; + current_conn->timer_prev = unexpired_tail; + current_conn->timer_next = NULL; + if (unexpired_tail != NULL) { + unexpired_tail->timer_next = current_conn; + } else { + unexpired_head = current_conn; } - unexpired_head = current_conn; + unexpired_tail = current_conn; } current_conn = next_conn; } if (unexpired_head != NULL) { - if (wheel->buckets[slot_index] == NULL) { - wheel->buckets[slot_index] = unexpired_head; - } else { - conn_t *tail = unexpired_head; - while (tail->timer_next != NULL) { - tail = tail->timer_next; - } - tail->timer_next = wheel->buckets[slot_index]; - wheel->buckets[slot_index]->timer_prev = tail; - wheel->buckets[slot_index] = unexpired_head; + if (wheel->buckets[slot_index] != NULL) { + unexpired_tail->timer_next = wheel->buckets[slot_index]; + wheel->buckets[slot_index]->timer_prev = unexpired_tail; } + wheel->buckets[slot_index] = unexpired_head; } - wheel->current_wheel_ms++; + if (wheel->buckets[slot_index] != NULL) { + timer_bucket_recompute_minimum(wheel, slot_index); + } + if (wheel->active_count == 0) { + wheel->next_expire_ms = UINT64_MAX; + } else { + timer_wheel_recompute_next_expiration(wheel); + } } } @@ -134,24 +224,10 @@ int timer_wheel_next_timeout_ms(const timer_wheel_t *wheel, uint64_t current_tim if (wheel->active_count == 0) { return 1000; } - if (wheel->current_wheel_ms <= current_time_ms) { + if (wheel->next_expire_ms <= current_time_ms) { return 0; } - uint64_t min_timeout = 1000; - for (size_t offset = 1; offset < min_timeout && offset < TIMER_WHEEL_SIZE; offset++) { - size_t slot_index = (size_t)((current_time_ms + offset) & TIMER_WHEEL_MASK); - conn_t *connection = wheel->buckets[slot_index]; - while (connection != NULL) { - if (connection->timer_expire_ms > current_time_ms) { - uint64_t diff = connection->timer_expire_ms - current_time_ms; - if (diff < min_timeout) { - min_timeout = diff; - } - } - connection = connection->timer_next; - } - } - - return (int)min_timeout; + uint64_t timeout_ms = wheel->next_expire_ms - current_time_ms; + return timeout_ms < 1000 ? (int)timeout_ms : 1000; } diff --git a/frameworks/apex-tuned/src/timer.h b/frameworks/apex-tuned/src/timer.h index fb740b89e4..944629dbbb 100644 --- a/frameworks/apex-tuned/src/timer.h +++ b/frameworks/apex-tuned/src/timer.h @@ -10,9 +10,15 @@ typedef struct worker_s worker_t; #define TIMER_WHEEL_SIZE 4096 #define TIMER_WHEEL_MASK (TIMER_WHEEL_SIZE - 1) +#define TIMER_WHEEL_WORD_BITS 64 +#define TIMER_WHEEL_WORD_COUNT (TIMER_WHEEL_SIZE / TIMER_WHEEL_WORD_BITS) typedef struct timer_wheel_s { conn_t *buckets[TIMER_WHEEL_SIZE]; + uint64_t bucket_min_expire_ms[TIMER_WHEEL_SIZE]; + uint64_t occupied_buckets[TIMER_WHEEL_WORD_COUNT]; + uint64_t occupied_words; + uint64_t next_expire_ms; uint64_t current_wheel_ms; size_t active_count; } timer_wheel_t; diff --git a/frameworks/apex-tuned/src/tls.c b/frameworks/apex-tuned/src/tls.c index 3de341d01b..829d4fdfbc 100644 --- a/frameworks/apex-tuned/src/tls.c +++ b/frameworks/apex-tuned/src/tls.c @@ -111,6 +111,10 @@ SSL_CTX *tls_init_server_ctx(const char *certificate_chain_file, const char *pri SSL_CTX_set_mode(context, SSL_MODE_ENABLE_PARTIAL_WRITE | SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER | SSL_MODE_RELEASE_BUFFERS); SSL_CTX_set_read_ahead(context, 1); + SSL_CTX_set_max_send_fragment(context, 16384); +#ifdef SSL_OP_ENABLE_KTLS + SSL_CTX_set_options(context, SSL_OP_ENABLE_KTLS); +#endif SSL_CTX_set_alpn_select_cb(context, enable_http2_alpn ? alpn_select_http2_callback : alpn_select_http1_callback, NULL); diff --git a/frameworks/apex-tuned/src/worker.c b/frameworks/apex-tuned/src/worker.c index 7b3d443770..7c0a2bad5a 100644 --- a/frameworks/apex-tuned/src/worker.c +++ b/frameworks/apex-tuned/src/worker.c @@ -7,6 +7,7 @@ #include "listener.h" #include "response_builder.h" #include "router.h" +#include "static_files.h" #include "timer.h" #include @@ -137,6 +138,72 @@ static inline size_t append_pipeline_responses(char *destination_buffer, size_t return (size_t)(cursor - destination_buffer); } +static inline bool parse_json_request_fast(const char *buffer, size_t buffer_length, + size_t *out_consumed_bytes, size_t *out_item_count, + int64_t *out_multiplier, bool *out_accepts_gzip) { + static const char request_prefix[] = "GET /json/"; + static const char request_line_suffix[] = " HTTP/1.1\r\n"; + if (buffer_length < sizeof(request_prefix) - 1 + sizeof(request_line_suffix) - 1 + 8 || + memcmp(buffer, request_prefix, sizeof(request_prefix) - 1) != 0) { + return false; + } + + const char *buffer_end = buffer + buffer_length; + const char *cursor = buffer + sizeof(request_prefix) - 1; + size_t item_count = 0; + size_t count_digits = 0; + while (cursor < buffer_end && count_digits < 2 && (unsigned int)(unsigned char)(*cursor - '0') <= 9) { + item_count = item_count * 10 + (size_t)(*cursor - '0'); + cursor++; + count_digits++; + } + if (count_digits == 0 || item_count == 0 || item_count > 50 || (size_t)(buffer_end - cursor) < 4 || + cursor[0] != '?' || cursor[1] != 'm' || cursor[2] != '=' || + (unsigned int)(unsigned char)(cursor[3] - '0') > 9) { + return false; + } + + int64_t multiplier = cursor[3] - '0'; + cursor += 4; + if ((size_t)(buffer_end - cursor) < sizeof(request_line_suffix) - 1 || + memcmp(cursor, request_line_suffix, sizeof(request_line_suffix) - 1) != 0) { + return false; + } + + const char *header_cursor = cursor + sizeof(request_line_suffix) - 1; + const char *header_end = memmem(header_cursor, (size_t)(buffer_end - header_cursor), "\r\n\r\n", 4); + if (!header_end) { + return false; + } + + bool accepts_gzip = false; + while (header_cursor < header_end) { + const char *line_end = memmem(header_cursor, (size_t)(header_end + 2 - header_cursor), "\r\n", 2); + if (!line_end) { + return false; + } + size_t line_length = (size_t)(line_end - header_cursor); + if (line_length >= 16 && memcmp(header_cursor, "Accept-Encoding:", 16) == 0) { + const char *value = header_cursor + 16; + while (value < line_end && (*value == ' ' || *value == '\t')) { + value++; + } + accepts_gzip = memmem(value, (size_t)(line_end - value), "gzip", 4) != NULL; + } else if ((line_length >= 11 && memcmp(header_cursor, "Connection:", 11) == 0) || + (line_length >= 15 && memcmp(header_cursor, "Content-Length:", 15) == 0) || + (line_length >= 18 && memcmp(header_cursor, "Transfer-Encoding:", 18) == 0)) { + return false; + } + header_cursor = line_end + 2; + } + + *out_consumed_bytes = (size_t)(header_end + 4 - buffer); + *out_item_count = item_count; + *out_multiplier = multiplier; + *out_accepts_gzip = accepts_gzip; + return true; +} + int worker_init(worker_t *worker, int id, int cpu_id, SSL_CTX *ssl_ctx, SSL_CTX *ssl_ctx_h2, int port_h1, int port_tls, int port_h2c, int port_h2) { memset(worker, 0, sizeof(*worker)); @@ -244,6 +311,37 @@ void process_requests(worker_t *worker, conn_t *connection) { continue; } + size_t json_consumed_bytes; + size_t json_item_count; + int64_t json_multiplier; + bool json_accepts_gzip; + if (__builtin_expect(parse_json_request_fast(request_buffer, request_buffer_length, + &json_consumed_bytes, &json_item_count, + &json_multiplier, &json_accepts_gzip), + 0)) { + const char *response_start = NULL; + size_t response_length = + json_accepts_gzip + ? json_gzip_cached_http1_response(json_item_count, json_multiplier, &response_start) + : json_cached_http1_response(json_item_count, json_multiplier, &response_start); + if (response_length > 0) { + if (write_pos > 0) { + flush_data(worker, connection, worker->write_buf, write_pos); + write_pos = 0; + if (connection->fd < 0) { + return; + } + } + read_offset += json_consumed_bytes; + connection->keep_alive = true; + flush_data(worker, connection, response_start, response_length); + if (connection->fd < 0) { + break; + } + continue; + } + } + request_t request; size_t consumed_bytes = 0; int parse_status = parse_http_request(request_buffer, request_buffer_length, &request, worker->chunk_buf, @@ -333,6 +431,29 @@ void process_requests(worker_t *worker, conn_t *connection) { continue; } + if (__builtin_expect(request.method == HTTP_METHOD_GET && request.keep_alive && request.path.len > 8 && + memcmp(request.path.ptr, "/static/", 8) == 0, + 0)) { + const char *response_start = NULL; + size_t response_length = static_cached_http1_response(&request, &response_start); + if (response_length > 0) { + if (write_pos > 0) { + flush_data(worker, connection, worker->write_buf, write_pos); + write_pos = 0; + if (connection->fd < 0) { + return; + } + } + + read_offset += consumed_bytes; + flush_data(worker, connection, response_start, response_length); + if (connection->fd < 0) { + break; + } + continue; + } + } + response_t response; res_init(&response, connection->keep_alive); @@ -405,10 +526,13 @@ void *worker_thread_main(void *thread_argument) { prctl(PR_SET_TIMERSLACK, 1UL, 0, 0, 0); #endif - cpu_set_t cpu_set; - CPU_ZERO(&cpu_set); - CPU_SET(worker->cpu_id, &cpu_set); - pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set); + const char *pin_cpus = getenv("APEX_PIN_CPUS"); + if (pin_cpus && pin_cpus[0] != '\0' && pin_cpus[0] != '0') { + cpu_set_t cpu_set; + CPU_ZERO(&cpu_set); + CPU_SET(worker->cpu_id, &cpu_set); + pthread_setaffinity_np(pthread_self(), sizeof(cpu_set), &cpu_set); + } buffer_pool_init(&worker->buffer_pool, BUFFER_POOL_DEFAULT_CAPACITY); register_application_routes(worker); @@ -599,6 +723,11 @@ void *worker_thread_main(void *thread_argument) { break; } } + if (connection->fd >= 0 && !h2_connection_drain_send(connection, worker)) { + if (connection->fd >= 0) { + close_conn(worker, connection); + } + } continue; } diff --git a/site/data/frameworks.json b/site/data/frameworks.json index 74fef4a2ab..16d35c1789 100644 --- a/site/data/frameworks.json +++ b/site/data/frameworks.json @@ -53,7 +53,7 @@ }, "aegon": { "dir": "aegon", - "description": "Linux-native C++26 asynchronous web framework powered by io_uring multishot primitives, coroutines, stepped SIMD vectorization, Glaze JSON serialization, and native zero-overhead gRPC over HTTP/2 with kTLS TX offload.", + "description": "Linux-native C++26 asynchronous web framework on io_uring: one event loop per core with multishot accept and recv over provided buffer rings, requests parsed in place from the receive buffer, coroutine handlers with userspace timers, Glaze JSON serialization, and gzip/deflate (libdeflate) and brotli compression middleware. Static files are served from a per-worker cache that follows the disk through inotify, with precompressed .br/.gz variants. TLS 1.3 through OpenSSL with a zero-copy connection BIO and ALPN; HTTP/2 and HTTP/3 (QUIC) with pooled streams; WebSocket; native gRPC over HTTP/2.", "repo": "https://github.com/UdayKhare09/Aegon", "type": "experimental", "engine": "io_uring", diff --git a/site/data/results/apex-tuned.json b/site/data/results/apex-tuned.json index 2c58691806..e8459c5848 100644 --- a/site/data/results/apex-tuned.json +++ b/site/data/results/apex-tuned.json @@ -4,44 +4,44 @@ "8gbit-512": { "framework": "apex-tuned", "language": "C", - "rps": 49325, - "avg_latency": "90.2us", - "p99_latency": "116.0us", - "cpu": "226.4%", - "memory": "241MiB", + "rps": 49104, + "avg_latency": "98.3us", + "p99_latency": "118.0us", + "cpu": "165.2%", + "memory": "250MiB", "connections": 512, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "510.31MB/s", - "input_bw": "481.69MB/s", + "bandwidth": "508.03MB/s", + "input_bw": "479.53MB/s", "reconnects": 0, - "status_2xx": 246703, + "status_2xx": 245603, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0, - "cpu_usec": 11601920, - "cpu_per_req_us": 47.0279, + "cpu_usec": 10296129, + "cpu_per_req_us": 41.9218, "target_rate": 50000, - "rate_ratio": 0.9865, - "p99_9_latency": "4354.0us" + "rate_ratio": 0.9821, + "p99_9_latency": "8106.0us" }, "async-32000": { "framework": "apex-tuned", "language": "C", - "rps": 1997036, - "avg_latency": "15.88ms", - "p99_latency": "21.50ms", - "cpu": "2896.4%", - "memory": "370MiB", + "rps": 1964734, + "avg_latency": "16.29ms", + "p99_latency": "21.90ms", + "cpu": "2909.6%", + "memory": "378MiB", "connections": 32000, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "171.38MB/s", - "input_bw": "91.42MB/s", + "bandwidth": "168.61MB/s", + "input_bw": "89.94MB/s", "reconnects": 0, - "status_2xx": 19970360, + "status_2xx": 19647347, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -49,19 +49,19 @@ "baseline-4096": { "framework": "apex-tuned", "language": "C", - "rps": 4403856, - "avg_latency": "929us", - "p99_latency": "1.96ms", - "cpu": "6194.6%", - "memory": "250MiB", + "rps": 4243873, + "avg_latency": "964us", + "p99_latency": "2.04ms", + "cpu": "6159.9%", + "memory": "262MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "268.66MB/s", - "input_bw": "340.19MB/s", + "bandwidth": "250.87MB/s", + "input_bw": "327.83MB/s", "reconnects": 0, - "status_2xx": 22019281, + "status_2xx": 21219369, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -69,18 +69,18 @@ "baseline-h2-1024": { "framework": "apex-tuned", "language": "C", - "rps": 16030277, - "avg_latency": "3.06ms", - "p99_latency": "86.69ms", - "cpu": "3217.3%", - "memory": "310MiB", + "rps": 15700098, + "avg_latency": "3.74ms", + "p99_latency": "113.80ms", + "cpu": "2799.8%", + "memory": "320MiB", "connections": 1024, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "324.26MB/s", + "bandwidth": "318.84MB/s", "reconnects": 0, - "status_2xx": 80952900, + "status_2xx": 79599500, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -88,18 +88,18 @@ "baseline-h2-256": { "framework": "apex-tuned", "language": "C", - "rps": 16094035, - "avg_latency": "839us", - "p99_latency": "30.55ms", - "cpu": "3076.9%", - "memory": "244MiB", + "rps": 15759443, + "avg_latency": "888us", + "p99_latency": "55.57ms", + "cpu": "3113.5%", + "memory": "255MiB", "connections": 256, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "324.25MB/s", + "bandwidth": "317.51MB/s", "reconnects": 0, - "status_2xx": 80953000, + "status_2xx": 79270000, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -107,18 +107,18 @@ "baseline-h2c-1024": { "framework": "apex-tuned", "language": "C", - "rps": 16634930, - "avg_latency": "2.98ms", - "p99_latency": "16.26ms", - "cpu": "2912.8%", - "memory": "287MiB", + "rps": 16481148, + "avg_latency": "3.10ms", + "p99_latency": "37.56ms", + "cpu": "2914.5%", + "memory": "300MiB", "connections": 1024, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "337.82MB/s", + "bandwidth": "333.38MB/s", "reconnects": 0, - "status_2xx": 84339100, + "status_2xx": 83229800, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -126,18 +126,18 @@ "baseline-h2c-256": { "framework": "apex-tuned", "language": "C", - "rps": 16350916, - "avg_latency": "1.03ms", - "p99_latency": "44.38ms", - "cpu": "1961.4%", - "memory": "226MiB", + "rps": 16303273, + "avg_latency": "809us", + "p99_latency": "54.72ms", + "cpu": "2803.9%", + "memory": "240MiB", "connections": 256, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "328.77MB/s", + "bandwidth": "329.12MB/s", "reconnects": 0, - "status_2xx": 82081600, + "status_2xx": 82168500, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -145,18 +145,18 @@ "baseline-h2c-4096": { "framework": "apex-tuned", "language": "C", - "rps": 15055383, - "avg_latency": "12.93ms", - "p99_latency": "29.38ms", - "cpu": "2878.2%", - "memory": "531MiB", + "rps": 15479117, + "avg_latency": "13.04ms", + "p99_latency": "84.59ms", + "cpu": "3263.2%", + "memory": "497MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "306.97MB/s", + "bandwidth": "316.23MB/s", "reconnects": 0, - "status_2xx": 76631900, + "status_2xx": 78943500, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -164,19 +164,19 @@ "json-comp-16384": { "framework": "apex-tuned", "language": "C", - "rps": 865005, - "avg_latency": "18.75ms", - "p99_latency": "46.00ms", - "cpu": "6738.4%", - "memory": "341MiB", + "rps": 2780767, + "avg_latency": "5.80ms", + "p99_latency": "12.20ms", + "cpu": "6108.8%", + "memory": "348MiB", "connections": 16384, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "1.31GB/s", - "input_bw": "64.34MB/s", - "reconnects": 165460, - "status_2xx": 4325028, + "bandwidth": "4.00GB/s", + "input_bw": "206.85MB/s", + "reconnects": 553156, + "status_2xx": 13903837, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -184,19 +184,19 @@ "json-comp-4096": { "framework": "apex-tuned", "language": "C", - "rps": 889808, - "avg_latency": "4.58ms", - "p99_latency": "13.40ms", - "cpu": "6715.3%", - "memory": "246MiB", + "rps": 2989994, + "avg_latency": "1.37ms", + "p99_latency": "4.49ms", + "cpu": "6151.4%", + "memory": "258MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "1.34GB/s", - "input_bw": "66.19MB/s", - "reconnects": 176514, - "status_2xx": 4449043, + "bandwidth": "4.30GB/s", + "input_bw": "222.42MB/s", + "reconnects": 596282, + "status_2xx": 14949970, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -204,18 +204,18 @@ "json-h2c-1024": { "framework": "apex-tuned", "language": "C", - "rps": 5961855, - "avg_latency": "3.57ms", - "p99_latency": "60.23ms", - "cpu": "4172.8%", - "memory": "336MiB", + "rps": 6463301, + "avg_latency": "3.94ms", + "p99_latency": "75.66ms", + "cpu": "3142.2%", + "memory": "321MiB", "connections": 1024, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "19.85GB/s", + "bandwidth": "21.56GB/s", "reconnects": 0, - "status_2xx": 30107368, + "status_2xx": 32704304, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -223,18 +223,18 @@ "json-h2c-4096": { "framework": "apex-tuned", "language": "C", - "rps": 5232884, - "avg_latency": "21.16ms", - "p99_latency": "86.51ms", - "cpu": "6240.7%", - "memory": "601MiB", + "rps": 5647770, + "avg_latency": "20.23ms", + "p99_latency": "200.04ms", + "cpu": "5208.9%", + "memory": "606MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "17.59GB/s", + "bandwidth": "18.95GB/s", "reconnects": 0, - "status_2xx": 26687712, + "status_2xx": 28747152, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -242,18 +242,18 @@ "json-tls-4096": { "framework": "apex-tuned", "language": "C", - "rps": 1822383, - "avg_latency": "1.39ms", - "p99_latency": "79.25ms", - "cpu": "5426.2%", - "memory": "330MiB", + "rps": 1773217, + "avg_latency": "1.66ms", + "p99_latency": "86.05ms", + "cpu": "5639.5%", + "memory": "340MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "6.14GB", + "bandwidth": "5.97GB", "reconnects": 0, - "status_2xx": 9293777, + "status_2xx": 9044381, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -261,91 +261,91 @@ "latency-10k-1024": { "framework": "apex-tuned", "language": "C", - "rps": 9983, - "avg_latency": "73.4us", - "p99_latency": "110.0us", - "cpu": "26.7%", - "memory": "219MiB", + "rps": 9981, + "avg_latency": "71.9us", + "p99_latency": "100.0us", + "cpu": "21.3%", + "memory": "228MiB", "connections": 1024, "threads": 64, "duration": "20s", "pipeline": 1, - "bandwidth": "0.63MB/s", + "bandwidth": "0.61MB/s", "reconnects": 0, - "status_2xx": 199677, + "status_2xx": 199642, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0, - "cpu_usec": 5571991, - "cpu_per_req_us": 27.905, + "cpu_usec": 4475012, + "cpu_per_req_us": 22.4152, "target_rate": 10000, - "rate_ratio": 0.9983, - "p99_9_latency": "331.0us" + "rate_ratio": 0.9981, + "p99_9_latency": "311.0us" }, "latency-1m-1024": { "framework": "apex-tuned", "language": "C", - "rps": 998042, - "avg_latency": "79.4us", - "p99_latency": "138.0us", - "cpu": "2077.8%", - "memory": "224MiB", + "rps": 997664, + "avg_latency": "88.9us", + "p99_latency": "153.0us", + "cpu": "2067.5%", + "memory": "234MiB", "connections": 1024, "threads": 64, "duration": "20s", "pipeline": 1, - "bandwidth": "62.88MB/s", + "bandwidth": "60.86MB/s", "reconnects": 0, - "status_2xx": 19963993, + "status_2xx": 19956775, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0, - "cpu_usec": 381804930, - "cpu_per_req_us": 19.1247, + "cpu_usec": 382454299, + "cpu_per_req_us": 19.1641, "target_rate": 1000000, - "rate_ratio": 0.998, - "p99_9_latency": "1318.0us" + "rate_ratio": 0.9977, + "p99_9_latency": "3053.0us" }, "latency-500k-8cpu-1024": { "framework": "apex-tuned", "language": "C", - "rps": 498523, - "avg_latency": "7021.5us", - "p99_latency": "88160.0us", - "cpu": "775.1%", - "memory": "48MiB", + "rps": 498831, + "avg_latency": "641.7us", + "p99_latency": "6498.0us", + "cpu": "722.7%", + "memory": "50MiB", "connections": 1024, "threads": 64, "duration": "20s", "pipeline": 1, - "bandwidth": "31.41MB/s", + "bandwidth": "30.43MB/s", "reconnects": 0, - "status_2xx": 9972473, + "status_2xx": 9978567, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0, - "cpu_usec": 146113409, - "cpu_per_req_us": 14.6517, + "cpu_usec": 137747588, + "cpu_per_req_us": 13.8043, "target_rate": 500000, - "rate_ratio": 0.997, - "p99_9_latency": "102304.0us" + "rate_ratio": 0.9977, + "p99_9_latency": "21416.0us" }, "limited-conn-4096": { "framework": "apex-tuned", "language": "C", - "rps": 2822803, - "avg_latency": "1.32ms", - "p99_latency": "1.80ms", - "cpu": "5870.1%", - "memory": "255MiB", + "rps": 2762770, + "avg_latency": "1.36ms", + "p99_latency": "2.31ms", + "cpu": "5372.8%", + "memory": "266MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "172.22MB/s", - "input_bw": "218.05MB/s", - "reconnects": 1411952, - "status_2xx": 14114015, + "bandwidth": "163.30MB/s", + "input_bw": "213.42MB/s", + "reconnects": 1381176, + "status_2xx": 13813850, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -353,18 +353,18 @@ "pipelined-4096": { "framework": "apex-tuned", "language": "C", - "rps": 59539961, - "avg_latency": "1.10ms", - "p99_latency": "1.39ms", - "cpu": "6036.2%", - "memory": "241MiB", + "rps": 58453238, + "avg_latency": "1.12ms", + "p99_latency": "1.77ms", + "cpu": "5867.0%", + "memory": "256MiB", "connections": 4096, "threads": 64, "duration": "5s", "pipeline": 16, - "bandwidth": "3.55GB/s", + "bandwidth": "3.48GB/s", "reconnects": 0, - "status_2xx": 297699808, + "status_2xx": 292266192, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -372,18 +372,18 @@ "static-h2-1024": { "framework": "apex-tuned", "language": "C", - "rps": 1424828, - "avg_latency": "21.05ms", - "p99_latency": "213.27ms", - "cpu": "6529.9%", - "memory": "343MiB", + "rps": 1340449, + "avg_latency": "22.94ms", + "p99_latency": "125.16ms", + "cpu": "6507.9%", + "memory": "358MiB", "connections": 1024, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "21.89GB/s", + "bandwidth": "20.59GB/s", "reconnects": 0, - "status_2xx": 7209630, + "status_2xx": 6782676, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -391,18 +391,18 @@ "static-h2-256": { "framework": "apex-tuned", "language": "C", - "rps": 1431689, - "avg_latency": "5.43ms", - "p99_latency": "189.12ms", - "cpu": "6466.6%", - "memory": "268MiB", + "rps": 1369159, + "avg_latency": "5.85ms", + "p99_latency": "48.62ms", + "cpu": "6427.8%", + "memory": "280MiB", "connections": 256, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "21.87GB/s", + "bandwidth": "20.91GB/s", "reconnects": 0, - "status_2xx": 7201397, + "status_2xx": 6886871, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0 @@ -410,18 +410,18 @@ "static-tls-1024": { "framework": "apex-tuned", "language": "C", - "rps": 1048575, - "avg_latency": "1.00ms", - "p99_latency": "39.69ms", - "cpu": "6491.2%", - "memory": "265MiB", + "rps": 1069227, + "avg_latency": "1.01ms", + "p99_latency": "48.92ms", + "cpu": "6444.4%", + "memory": "275MiB", "connections": 1024, "threads": 64, "duration": "5s", "pipeline": 1, - "bandwidth": "16.00GB", + "bandwidth": "16.31GB", "reconnects": 0, - "status_2xx": 5347450, + "status_2xx": 5453756, "status_3xx": 0, "status_4xx": 0, "status_5xx": 0