checkpoint on symbol server

This commit is contained in:
Ryan Fleury committed 2026-06-08 17:32:07 -07:00
1 parent 536ca53d18
commit b22f9f2d8b
21 files changed
+736 -99

No files matched your search

+95 -57
View File
@@ -293,6 +293,7 @@ http_async_tick(void)
}
req->hConnect = hConnect;
req->hRequest = hRequest;
req->arena_start_body_read_pos = arena_pos(req->arena);
//- rjf: send request
if(good)
@@ -324,6 +325,95 @@ http_async_tick(void)
}
}
//- rjf: push response output
{
Rng1U64 range = lane_range(ArrayCount(w32_http_state->active_requests));
for EachInRange(idx, range)
{
W32_HTTP_Request *req = &w32_http_state->active_requests[idx];
W32_HTTP_RequestStatus status = ins_atomic_u32_eval(&req->status);
if(req->active && (status == W32_HTTP_RequestStatus_Done ||
status == W32_HTTP_RequestStatus_Failed ||
req->finished_body_pieces.total_size >= req->out_ring->ring->size/2))
{
Temp scratch = scratch_begin(0, 0);
// rjf: figure out status code
if(req->status_code == 0)
{
DWORD status_code = 0;
DWORD status_code_size = sizeof(status_code);
WinHttpQueryHeaders(req->hRequest,
WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX,
&status_code, &status_code_size, WINHTTP_NO_HEADER_INDEX);
req->status_code = status_code;
}
// rjf: join all currently read body contents
String8 body = str8_list_join(scratch.arena, &req->finished_body_pieces, 0);
// rjf: clear current body output
arena_pop_to(req->arena, req->arena_start_body_read_pos);
MemoryZeroStruct(&req->finished_body_pieces);
// rjf: compute size per record header
U64 max_record_size = req->out_ring->ring->size;
U64 bytes_per_record_header = sizeof(U64) + sizeof(U64) + sizeof(B32) + sizeof(HTTP_StatusCode);
// rjf: push response records
for(U64 off = 0, next_off = 0; off <= body.size; off = next_off)
{
// rjf: determine how much of the body we can fit into this record
U64 body_data_size = (body.size - off);
if(bytes_per_record_header + body_data_size > max_record_size)
{
body_data_size = max_record_size - bytes_per_record_header;
}
// rjf: advance by that amount of the body
next_off = off + body_data_size;
// rjf: serialize this record
String8 record_data = {0};
{
String8List pieces = {0};
U64 record_size = bytes_per_record_header + (next_off - off);
B32 is_last = (next_off == body.size && (status == W32_HTTP_RequestStatus_Done || status == W32_HTTP_RequestStatus_Failed));
B32 has_more = !is_last;
str8_list_push(scratch.arena, &pieces, str8_struct(&record_size));
str8_list_push(scratch.arena, &pieces, str8_struct(&req->id));
str8_list_push(scratch.arena, &pieces, str8_struct(&has_more));
str8_list_push(scratch.arena, &pieces, str8_struct(&req->status_code));
str8_list_push(scratch.arena, &pieces, str8_substr(body, r1u64(off, next_off)));
record_data = str8_list_join(scratch.arena, &pieces, 0);
}
// rjf: push this record
{
// TODO(rjf): can't wait here technically, because we might be waiting for other
// work in the async threads to pop! so we'll just deadlock right here. two options:
//
// (a) http requests need to be on another timeline
// (b) we need to prepare for this to fail & batch it up or something.
//
RingGuard g = guarded_ring_open(req->out_ring);
guarded_ring_write_string_or_wait(&g, record_data, max_U64);
guarded_ring_close(&g);
}
// rjf: cancel on no movement
if(next_off == off)
{
break;
}
}
scratch_end(scratch);
}
}
}
//- rjf: kick off body size queries
{
Rng1U64 range = lane_range(ArrayCount(w32_http_state->active_requests));
@@ -364,62 +454,10 @@ http_async_tick(void)
{
W32_HTTP_Request *req = &w32_http_state->active_requests[idx];
W32_HTTP_RequestStatus status = ins_atomic_u32_eval(&req->status);
if(req->active &&
if(req->active && req->finished_body_pieces.total_size == 0 && req->next_body_piece_size == 0 &&
(status == W32_HTTP_RequestStatus_Done ||
status == W32_HTTP_RequestStatus_Failed))
{
Temp scratch = scratch_begin(0, 0);
B32 good = (status == W32_HTTP_RequestStatus_Done);
//- rjf: unpack
HINTERNET hRequest = req->hRequest;
//- rjf: read response code
HTTP_StatusCode code = 0;
if(good)
{
DWORD status_code = 0;
DWORD status_code_size = sizeof(status_code);
WinHttpQueryHeaders(hRequest,
WINHTTP_QUERY_STATUS_CODE|WINHTTP_QUERY_FLAG_NUMBER,
WINHTTP_HEADER_NAME_BY_INDEX,
&status_code, &status_code_size, WINHTTP_NO_HEADER_INDEX);
code = status_code;
}
//- rjf: serialize response
String8 response_data = {0};
U64 response_data_min_size = sizeof(U64) + sizeof(U64) + sizeof(HTTP_StatusCode) + sizeof(U64);
U64 response_data_max_size = req->out_ring->ring->size;
B32 response_is_possible = (response_data_min_size < response_data_max_size);
if(response_is_possible)
{
U64 body_size = req->finished_body_pieces.total_size;
String8List pieces = {0};
str8_list_push(scratch.arena, &pieces, str8_struct(&req->id));
str8_list_push(scratch.arena, &pieces, str8_struct(&code));
str8_list_push(scratch.arena, &pieces, str8_struct(&body_size));
str8_list_concat_in_place(&pieces, &req->finished_body_pieces);
U64 data_size = pieces.total_size;
str8_list_push_front(scratch.arena, &pieces, str8_struct(&data_size));
if(data_size + sizeof(U64) > response_data_max_size)
{
U64 overkill = data_size + sizeof(U64) - response_data_max_size;
data_size -= overkill;
}
response_data = str8_list_join(scratch.arena, &pieces, 0);
response_data.size = data_size + sizeof(U64);
}
//- rjf: write response
if(response_is_possible)
{
RingGuard g = guarded_ring_open(req->out_ring);
guarded_ring_write_string_or_wait(&g, response_data, max_U64);
guarded_ring_close(&g);
}
//- rjf: free this request slot
req->active = 0;
arena_clear(req->arena);
MemoryZeroStruct(&req->finished_body_pieces);
@@ -427,8 +465,6 @@ http_async_tick(void)
req->next_body_piece_size = 0;
WinHttpCloseHandle(req->hConnect);
WinHttpCloseHandle(req->hRequest);
scratch_end(scratch);
}
}
}
@@ -485,14 +521,16 @@ http_pop_response(Arena *arena, GuardedRing *out_ring, HTTP_Response *response_o
B32 result = guarded_ring_read_struct_or_wait(&g, &data_size, endt_us);
if(result)
{
U64 data_size_minus_size_prefix = data_size - sizeof(data_size);
String8 data = {0};
data.size = data_size;
data.size = data_size_minus_size_prefix;
data.str = push_array(arena, U8, data.size);
guarded_ring_read_or_wait(&g, data.size, data.str, max_U64);
U64 off = 0;
off += str8_deserial_read_struct(data, off, &response_out->id);
off += str8_deserial_read_struct(data, off, &response_out->has_more);
off += str8_deserial_read_struct(data, off, &response_out->code);
off += str8_deserial_read_struct(data, off, &response_out->body.size);
response_out->body.size = (data.size - off);
response_out->body.str = push_array(arena, U8, response_out->body.size);
off += str8_deserial_read(data, off, response_out->body.str, response_out->body.size, 1);
}
+2
View File
@@ -34,6 +34,8 @@ struct W32_HTTP_Request
HINTERNET hRequest;
void *optional; // NOTE(rjf): must persist with in-flight requests
U64 optional_size;
HTTP_StatusCode status_code;
U64 arena_start_body_read_pos;
String8List finished_body_pieces;
void *next_body_piece;
U64 next_body_piece_size;
+327 -8
View File
@@ -7,6 +7,8 @@ smsv_init(void)
Arena *arena = arena_alloc();
w32_smsv_state = push_array(arena, W32_SMSV_State, 1);
w32_smsv_state->arena = arena;
// rjf: get symbol server configuration from environment
{
// rjf: extract parameterizations from _NT_SYMBOL_PATH
{
@@ -71,24 +73,257 @@ smsv_init(void)
}
}
// rjf fall back to using local appdata temp symbol cache folder
// rjf fall back to using C:/SymbolCache (or wherever windows is located)
if(w32_smsv_state->symbol_cache_path.size == 0)
{
Temp scratch = scratch_begin(0, 0);
U64 size = KB(32);
U16 *buffer = push_array_no_zero(scratch.arena, U16, size);
if(SUCCEEDED(SHGetFolderPathW(0, CSIDL_LOCAL_APPDATA, 0, 0, (WCHAR*)buffer)))
if(SUCCEEDED(SHGetFolderPathW(0, CSIDL_SYSTEM, 0, 0, (WCHAR*)buffer)))
{
String8 local_appdata = str8_from_16(scratch.arena, str16_cstring(buffer));
w32_smsv_state->symbol_cache_path = str8f(arena, "%S\\Temp\\SymbolCache", local_appdata);
String8 system32 = str8_from_16(scratch.arena, str16_cstring(buffer));
String8 containing_drive = str8_prefix(system32, str8_find_needle(system32, 0, s("/"), StringMatchFlag_SlashInsensitive));
w32_smsv_state->symbol_cache_path = str8f(arena, "%S\\SymbolCache", containing_drive);
}
scratch_end(scratch);
}
}
// rjf: set up task cache
{
w32_smsv_state->task_slots_count = 256;
w32_smsv_state->task_slots = push_array(arena, W32_SMSV_TaskSlot, w32_smsv_state->task_slots_count);
w32_smsv_state->task_stripes = stripe_array_alloc(arena);
}
// rjf: set up rings
w32_smsv_state->http_response_ring = guarded_ring_alloc(arena, KB(256));
w32_smsv_state->new_task_ring = guarded_ring_alloc(arena, KB(8));
w32_smsv_state->retry_arena = arena_alloc();
}
internal void
smsv_async_tick(void)
{
Temp scratch = scratch_begin(0, 0);
//- rjf: pop new tasks from new task ring, gather them
W32_SMSV_TaskNode *first_request_task = 0;
W32_SMSV_TaskNode *last_request_task = 0;
U64 request_task_count = 0;
if(lane_idx() == 0)
{
for(B32 done = 0;!done;)
{
// rjf: get the next task ID to start
U64 task_id = 0;
{
RingGuard g = guarded_ring_open(w32_smsv_state->new_task_ring);
if(!guarded_ring_try_read_struct(&g, &task_id))
{
done = 1;
}
guarded_ring_close(&g);
}
// rjf: gather
if(!done)
{
W32_SMSV_Task *task = (W32_SMSV_Task *)task_id;
W32_SMSV_TaskNode *n = push_array(scratch.arena, W32_SMSV_TaskNode, 1);
n->task = task;
SLLQueuePush(first_request_task, last_request_task, n);
request_task_count += 1;
}
}
}
lane_sync();
//- rjf: gather tasks from the retry batch
if(lane_idx() == 0)
{
for(W32_SMSV_TaskNode *t = w32_smsv_state->first_retry_request; t != 0; t = t->next)
{
W32_SMSV_TaskNode *n = push_array(scratch.arena, W32_SMSV_TaskNode, 1);
n->task = t->task;
SLLQueuePush(first_request_task, last_request_task, n);
request_task_count += 1;
}
arena_clear(w32_smsv_state->retry_arena);
w32_smsv_state->first_retry_request = w32_smsv_state->last_retry_request = 0;
}
lane_sync();
//- rjf: pop responses -> for any responses that suggest that a particular debug info
// is not present on the server, we need to generate new request nodes for those tasks,
// to fall back to subsequent server URLS.
W32_SMSV_TaskNode *first_finish_task = 0;
W32_SMSV_TaskNode *last_finish_task = 0;
U64 finish_task_count = 0;
if(lane_idx() == 0)
{
for(HTTP_Response r = {0}; http_pop_response(scratch.arena, w32_smsv_state->http_response_ring, &r, 0);)
{
W32_SMSV_Task *task = (W32_SMSV_Task *)r.id;
if(task != 0)
{
if(r.body.size != 0)
{
str8_list_push(task->arena, &task->file_pieces, str8_copy(task->arena, r.body));
}
if(r.has_more == 0 && task->status != W32_SMSV_TaskStatus_DoneDownloading)
{
task->status = W32_SMSV_TaskStatus_DoneDownloading;
W32_SMSV_TaskNode *n = push_array(scratch.arena, W32_SMSV_TaskNode, 1);
n->task = task;
SLLQueuePush(first_finish_task, last_finish_task, n);
finish_task_count += 1;
}
}
}
}
lane_sync();
//- rjf: flatten all request tasks
W32_SMSV_Task **request_tasks = 0;
if(lane_idx() == 0)
{
request_tasks = push_array(scratch.arena, W32_SMSV_Task *, request_task_count);
{
U64 idx = 0;
for EachNode(n, W32_SMSV_TaskNode, first_request_task)
{
request_tasks[idx] = n->task;
idx += 1;
}
}
}
lane_sync_u64(&request_tasks, 0);
//- rjf: do all request tasks
{
Rng1U64 range = lane_range(request_task_count);
for EachInRange(idx, range)
{
W32_SMSV_Task *task = request_tasks[idx];
// rjf: adjust the server URL node that we're using for this task
if(task->status == W32_SMSV_TaskStatus_Failed || task->status == W32_SMSV_TaskStatus_Null)
{
if(task->current_server_url_node == 0)
{
task->current_server_url_node = w32_smsv_state->symbol_server_urls.first;
}
else
{
task->current_server_url_node = task->current_server_url_node->next;
}
}
// rjf: get server URL
String8 server_url = {0};
if(task->current_server_url_node != 0)
{
server_url = task->current_server_url_node->string;
}
// rjf: kick off request if we have a server URL
if(server_url.size != 0)
{
task->status = W32_SMSV_TaskStatus_Requested;
Guid guid = task->guid;
U64 age = task->age;
String8 debug_info_unique_identifier = {0};
{
debug_info_unique_identifier = str8f(scratch.arena, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%I64x",
guid.data1, guid.data2, guid.data3, guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3], guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7], age);
}
HTTP_RequestParams params =
{
.id = (U64)task,
.method = HTTP_Method_Get,
.url = str8f(scratch.arena, "%S/%S/%S/%S", server_url, task->dbg_name, debug_info_unique_identifier, task->dbg_name),
};
if(!http_push_request(w32_smsv_state->http_response_ring, &params, 0))
{
W32_SMSV_TaskNode *n = push_array(w32_smsv_state->retry_arena, W32_SMSV_TaskNode, 1);
SLLQueuePush(w32_smsv_state->first_retry_request, w32_smsv_state->last_retry_request, n);
n->task = task;
ins_atomic_u32_eval_assign(&async_loop_again, 1);
cond_var_broadcast(async_tick_start_cond_var);
}
}
// rjf: if we don't have a server URL, mark this task as failed
else
{
task->status = W32_SMSV_TaskStatus_Failed;
W32_SMSV_TaskNode *n = push_array(scratch.arena, W32_SMSV_TaskNode, 1);
n->task = task;
SLLQueuePush(first_finish_task, last_finish_task, n);
finish_task_count += 1;
}
}
}
//- rjf: flatten all finish tasks
W32_SMSV_Task **finish_tasks = 0;
if(lane_idx() == 0)
{
finish_tasks = push_array(scratch.arena, W32_SMSV_Task *, finish_task_count);
{
U64 idx = 0;
for EachNode(n, W32_SMSV_TaskNode, first_finish_task)
{
finish_tasks[idx] = n->task;
idx += 1;
}
}
}
lane_sync_u64(&finish_tasks, 0);
//- rjf: do all finish tasks
{
Rng1U64 range = lane_range(finish_task_count);
for EachInRange(idx, range)
{
W32_SMSV_Task *task = finish_tasks[idx];
// rjf: successful task -> write to local path
if(task->status != W32_SMSV_TaskStatus_Failed)
{
String8 directory = str8_chop_last_slash(task->local_path);
for(U64 slash_pos = 0; slash_pos <= directory.size;)
{
slash_pos = str8_find_needle(directory, slash_pos+1, s("/"), StringMatchFlag_SlashInsensitive);
String8 root_dir = str8_substr(directory, r1u64(0, slash_pos));
make_directory(root_dir);
if(slash_pos == directory.size)
{
break;
}
}
write_data_list_to_file_path(task->local_path, task->file_pieces);
}
// rjf: evict task from cache
U64 hash = u64_hash_from_str8(task->local_path);
U64 slot_idx = hash%w32_smsv_state->task_slots_count;
W32_SMSV_TaskSlot *slot = &w32_smsv_state->task_slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&w32_smsv_state->task_stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 1)
{
DLLRemove(slot->first, slot->last, task);
arena_release(task->arena);
}
}
}
scratch_end(scratch);
}
internal String8
smsv_local_path_from_key(Arena *arena, String8 module_name, String8 dbg_name, Guid guid, U64 age)
smsv_local_path_from_key(Arena *arena, String8 dbg_name, Guid guid, U64 age)
{
String8 result = {0};
Temp scratch = scratch_begin(&arena, 1);
@@ -96,19 +331,103 @@ smsv_local_path_from_key(Arena *arena, String8 module_name, String8 dbg_name, Gu
// rjf: form unique identifier based on guid
String8 debug_info_unique_identifier = {0};
{
debug_info_unique_identifier = str8f(scratch.arena, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%x",
debug_info_unique_identifier = str8f(scratch.arena, "%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X%I64x",
guid.data1, guid.data2, guid.data3, guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3], guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7], age);
}
// rjf: form full path
if(debug_info_unique_identifier.size != 0)
{
// TODO(rjf): assuming 'stripped' for now
String8 symbol_cache_path = w32_smsv_state->symbol_cache_path;
String8 path = str8f(scratch.arena, "%S/%S/%S/stripped/%S", symbol_cache_path, module_name, debug_info_unique_identifier, dbg_name);
String8 path = str8f(scratch.arena, "%S/%S/%S/%S", symbol_cache_path, dbg_name, debug_info_unique_identifier, dbg_name);
result = path_normalized_from_string(arena, path);
}
// rjf: determine if we already have this debug info stored locally
B32 already_cached_locally = (properties_from_file_path(result).modified != 0);
// rjf: if not cached: record (local path -> download task) mapping
B32 task_is_new = 0;
U64 task_id = 0;
if(!already_cached_locally)
{
U64 hash = u64_hash_from_str8(result);
U64 slot_idx = hash%w32_smsv_state->task_slots_count;
W32_SMSV_TaskSlot *slot = &w32_smsv_state->task_slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&w32_smsv_state->task_stripes, slot_idx);
for(B32 write_mode = 0; write_mode <= 1; write_mode += 1)
{
B32 already_exists = 0;
RWMutexScope(stripe->rw_mutex, write_mode)
{
W32_SMSV_Task *node = 0;
for(W32_SMSV_Task *n = slot->first; n != 0; n = n->next)
{
if(MemoryMatchStruct(&n->guid, &guid))
{
already_exists = 1;
node = n;
break;
}
}
if(node == 0 && write_mode)
{
Arena *arena = arena_alloc();
node = push_array(arena, W32_SMSV_Task, 1);
node->arena = arena;
node->local_path = str8_copy(arena, result);
node->dbg_name = str8_copy(arena, dbg_name);
node->guid = guid;
node->age = age;
DLLPushBack(slot->first, slot->last, node);
task_is_new = 1;
task_id = (U64)node;
}
}
if(already_exists)
{
break;
}
}
}
// rjf: if the task is new -> push request to start task
if(task_is_new)
{
RingGuard g = guarded_ring_open(w32_smsv_state->new_task_ring);
guarded_ring_write_struct_or_wait(&g, &task_id, max_U64);
guarded_ring_close(&g);
ins_atomic_u32_eval_assign(&async_loop_again, 1);
cond_var_broadcast(async_tick_start_cond_var);
}
}
scratch_end(scratch);
return result;
}
internal SMSV_Status
smsv_status_from_local_path(String8 path)
{
SMSV_Status status = SMSV_Status_Null;
{
Temp scratch = scratch_begin(0, 0);
String8 path_normalized = path_normalized_from_string(scratch.arena, path);
U64 hash = u64_hash_from_str8(path_normalized);
U64 slot_idx = hash%w32_smsv_state->task_slots_count;
W32_SMSV_TaskSlot *slot = &w32_smsv_state->task_slots[slot_idx];
Stripe *stripe = stripe_from_slot_idx(&w32_smsv_state->task_stripes, slot_idx);
RWMutexScope(stripe->rw_mutex, 0)
{
for(W32_SMSV_Task *n = slot->first; n != 0; n = n->next)
{
if(str8_match(n->local_path, path, 0))
{
status = SMSV_Status_Pending;
break;
}
}
}
scratch_end(scratch);
}
return status;
}
@@ -4,12 +4,62 @@
#ifndef WIN32_SYMBOL_SERVER_H
#define WIN32_SYMBOL_SERVER_H
typedef enum W32_SMSV_TaskStatus
{
W32_SMSV_TaskStatus_Null,
W32_SMSV_TaskStatus_Requested,
W32_SMSV_TaskStatus_DoneDownloading,
W32_SMSV_TaskStatus_Failed,
}
W32_SMSV_TaskStatus;
typedef struct W32_SMSV_Task W32_SMSV_Task;
struct W32_SMSV_Task
{
W32_SMSV_Task *next;
W32_SMSV_Task *prev;
// rjf: key / parameters
Arena *arena;
String8 local_path;
String8 dbg_name;
Guid guid;
U64 age;
// rjf: working state
W32_SMSV_TaskStatus status;
String8Node *current_server_url_node;
String8List file_pieces;
};
typedef struct W32_SMSV_TaskNode W32_SMSV_TaskNode;
struct W32_SMSV_TaskNode
{
W32_SMSV_TaskNode *next;
W32_SMSV_Task *task;
};
typedef struct W32_SMSV_TaskSlot W32_SMSV_TaskSlot;
struct W32_SMSV_TaskSlot
{
W32_SMSV_Task *first;
W32_SMSV_Task *last;
};
typedef struct W32_SMSV_State W32_SMSV_State;
struct W32_SMSV_State
{
Arena *arena;
String8 symbol_cache_path;
String8List symbol_server_urls;
U64 task_slots_count;
W32_SMSV_TaskSlot *task_slots;
StripeArray task_stripes;
GuardedRing *http_response_ring;
GuardedRing *new_task_ring;
Arena *retry_arena;
W32_SMSV_TaskNode *first_retry_request;
W32_SMSV_TaskNode *last_retry_request;
};
global W32_SMSV_State *w32_smsv_state = 0;