chore: refactor our sandbox_request_t

sledge_graph
Sean McBride 5 years ago
parent 1487614863
commit 2f164c3430

@ -3,8 +3,8 @@
#include "sandbox_request.h" #include "sandbox_request.h"
/* Returns pointer back if successful, null otherwise */ /* Returns pointer back if successful, null otherwise */
typedef sandbox_request_t *(*global_request_scheduler_add_fn_t)(void *); typedef struct sandbox_request *(*global_request_scheduler_add_fn_t)(void *);
typedef int (*global_request_scheduler_remove_fn_t)(sandbox_request_t **); typedef int (*global_request_scheduler_remove_fn_t)(struct sandbox_request **);
typedef uint64_t (*global_request_scheduler_peek_fn_t)(void); typedef uint64_t (*global_request_scheduler_peek_fn_t)(void);
struct global_request_scheduler_config { struct global_request_scheduler_config {
@ -16,6 +16,6 @@ struct global_request_scheduler_config {
void global_request_scheduler_initialize(struct global_request_scheduler_config *config); void global_request_scheduler_initialize(struct global_request_scheduler_config *config);
sandbox_request_t *global_request_scheduler_add(sandbox_request_t *); struct sandbox_request *global_request_scheduler_add(struct sandbox_request *);
int global_request_scheduler_remove(sandbox_request_t **); int global_request_scheduler_remove(struct sandbox_request **);
uint64_t global_request_scheduler_peek(); uint64_t global_request_scheduler_peek();

@ -75,8 +75,6 @@ struct sandbox {
char request_response_data[1]; /* of request_response_data_length, following sandbox mem.. */ char request_response_data[1]; /* of request_response_data_length, following sandbox mem.. */
} PAGE_ALIGNED; } PAGE_ALIGNED;
typedef struct sandbox sandbox_t;
/*************************** /***************************
* Externs * * Externs *
**************************/ **************************/
@ -85,16 +83,16 @@ typedef struct sandbox sandbox_t;
extern __thread arch_context_t *worker_thread_next_context; extern __thread arch_context_t *worker_thread_next_context;
extern void worker_thread_block_current_sandbox(void); extern void worker_thread_block_current_sandbox(void);
extern void worker_thread_on_sandbox_exit(sandbox_t *sandbox); extern void worker_thread_on_sandbox_exit(struct sandbox *sandbox);
extern void worker_thread_process_io(void); extern void worker_thread_process_io(void);
extern void __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void); extern void __attribute__((noreturn)) worker_thread_sandbox_switch_preempt(void);
extern void worker_thread_wakeup_sandbox(sandbox_t *sandbox); extern void worker_thread_wakeup_sandbox(struct sandbox *sandbox);
/*************************** /***************************
* Public API * * Public API *
**************************/ **************************/
struct sandbox *sandbox_allocate(sandbox_request_t *sandbox_request); struct sandbox *sandbox_allocate(struct sandbox_request *sandbox_request);
void sandbox_free(struct sandbox *sandbox); void sandbox_free(struct sandbox *sandbox);
void sandbox_main(struct sandbox *sandbox); void sandbox_main(struct sandbox *sandbox);
int sandbox_parse_http_request(struct sandbox *sandbox, size_t length); int sandbox_parse_http_request(struct sandbox *sandbox, size_t length);

@ -18,9 +18,7 @@ struct sandbox_request {
uint64_t absolute_deadline; /* cycles */ uint64_t absolute_deadline; /* cycles */
}; };
typedef struct sandbox_request sandbox_request_t; DEQUE_PROTOTYPE(sandbox, struct sandbox_request *);
DEQUE_PROTOTYPE(sandbox, sandbox_request_t *);
/** /**
* Allocates a new Sandbox Request and places it on the Global Deque * Allocates a new Sandbox Request and places it on the Global Deque
@ -31,11 +29,11 @@ DEQUE_PROTOTYPE(sandbox, sandbox_request_t *);
* @param start_time the timestamp of when we receives the request from the network (in cycles) * @param start_time the timestamp of when we receives the request from the network (in cycles)
* @return the new sandbox request * @return the new sandbox request
*/ */
static inline sandbox_request_t * static inline struct sandbox_request *
sandbox_request_allocate(struct module *module, char *arguments, int socket_descriptor, sandbox_request_allocate(struct module *module, char *arguments, int socket_descriptor,
const struct sockaddr *socket_address, uint64_t start_time) const struct sockaddr *socket_address, uint64_t start_time)
{ {
sandbox_request_t *sandbox_request = (sandbox_request_t *)malloc(sizeof(sandbox_request_t)); struct sandbox_request *sandbox_request = (struct sandbox_request *)malloc(sizeof(struct sandbox_request));
assert(sandbox_request); assert(sandbox_request);
sandbox_request->module = module; sandbox_request->module = module;
sandbox_request->arguments = arguments; sandbox_request->arguments = arguments;

@ -2,7 +2,7 @@
#include "types.h" #include "types.h"
/* current sandbox that is active.. */ /* current sandbox that is active.. */
static __thread sandbox_t *worker_thread_current_sandbox = NULL; static __thread struct sandbox *worker_thread_current_sandbox = NULL;
__thread struct sandbox_context_cache local_sandbox_context_cache = { __thread struct sandbox_context_cache local_sandbox_context_cache = {
.linear_memory_start = NULL, .linear_memory_start = NULL,

@ -2,14 +2,14 @@
#include "panic.h" #include "panic.h"
/* Default uninitialized implementations of the polymorphic interface */ /* Default uninitialized implementations of the polymorphic interface */
__attribute__((noreturn)) static sandbox_request_t * __attribute__((noreturn)) static struct sandbox_request *
uninitialized_add(void *arg) uninitialized_add(void *arg)
{ {
panic("Global Request Scheduler Add was called before initialization\n"); panic("Global Request Scheduler Add was called before initialization\n");
}; };
__attribute__((noreturn)) static int __attribute__((noreturn)) static int
uninitialized_remove(sandbox_request_t **arg) uninitialized_remove(struct sandbox_request **arg)
{ {
panic("Global Request Scheduler Remove was called before initialization\n"); panic("Global Request Scheduler Remove was called before initialization\n");
}; };
@ -41,8 +41,8 @@ global_request_scheduler_initialize(struct global_request_scheduler_config *conf
* Adds a sandbox request to the request scheduler * Adds a sandbox request to the request scheduler
* @param sandbox_request * @param sandbox_request
*/ */
sandbox_request_t * struct sandbox_request *
global_request_scheduler_add(sandbox_request_t *sandbox_request) global_request_scheduler_add(struct sandbox_request *sandbox_request)
{ {
return global_request_scheduler.add_fn(sandbox_request); return global_request_scheduler.add_fn(sandbox_request);
} }
@ -53,7 +53,7 @@ global_request_scheduler_add(sandbox_request_t *sandbox_request)
* @returns 0 if successful, -1 if empty, -2 if unable to take lock or perform atomic operation * @returns 0 if successful, -1 if empty, -2 if unable to take lock or perform atomic operation
*/ */
int int
global_request_scheduler_remove(sandbox_request_t **removed_sandbox) global_request_scheduler_remove(struct sandbox_request **removed_sandbox)
{ {
return global_request_scheduler.remove_fn(removed_sandbox); return global_request_scheduler.remove_fn(removed_sandbox);
} }

@ -8,11 +8,11 @@ static pthread_mutex_t global_request_scheduler_deque_mutex = PTHREAD_MUTE
* @param sandbox_request * @param sandbox_request
* @returns pointer to request if added. NULL otherwise * @returns pointer to request if added. NULL otherwise
*/ */
static sandbox_request_t * static struct sandbox_request *
global_request_scheduler_deque_add(void *sandbox_request_raw) global_request_scheduler_deque_add(void *sandbox_request_raw)
{ {
sandbox_request_t *sandbox_request = (sandbox_request_t *)sandbox_request_raw; struct sandbox_request *sandbox_request = (struct sandbox_request *)sandbox_request_raw;
int return_code = 1; int return_code = 1;
/* TODO: Running the runtime and listener cores on a single shared core is untested /* TODO: Running the runtime and listener cores on a single shared core is untested
We are unsure if the locking behavior is correct, so there may be deadlocks */ We are unsure if the locking behavior is correct, so there may be deadlocks */
@ -42,7 +42,7 @@ We are unsure if the locking behavior is correct, so there may be deadlocks */
* @returns 0 if successfully returned a sandbox request, -1 if empty, -2 if atomic instruction unsuccessful * @returns 0 if successfully returned a sandbox request, -1 if empty, -2 if atomic instruction unsuccessful
*/ */
static int static int
global_request_scheduler_deque_remove(sandbox_request_t **removed_sandbox_request) global_request_scheduler_deque_remove(struct sandbox_request **removed_sandbox_request)
{ {
int return_code; int return_code;
#if NCORES == 1 #if NCORES == 1

@ -9,7 +9,7 @@ static struct priority_queue global_request_scheduler_minheap;
* @param sandbox_request * @param sandbox_request
* @returns pointer to request if added. NULL otherwise * @returns pointer to request if added. NULL otherwise
*/ */
static sandbox_request_t * static struct sandbox_request *
global_request_scheduler_minheap_add(void *sandbox_request) global_request_scheduler_minheap_add(void *sandbox_request)
{ {
int return_code = priority_queue_enqueue(&global_request_scheduler_minheap, sandbox_request); int return_code = priority_queue_enqueue(&global_request_scheduler_minheap, sandbox_request);
@ -23,7 +23,7 @@ global_request_scheduler_minheap_add(void *sandbox_request)
* @returns 0 if successful, -1 if empty, -2 if unable to take lock or perform atomic operation * @returns 0 if successful, -1 if empty, -2 if unable to take lock or perform atomic operation
*/ */
int int
global_request_scheduler_minheap_remove(sandbox_request_t **removed_sandbox_request) global_request_scheduler_minheap_remove(struct sandbox_request **removed_sandbox_request)
{ {
return priority_queue_dequeue(&global_request_scheduler_minheap, (void **)removed_sandbox_request); return priority_queue_dequeue(&global_request_scheduler_minheap, (void **)removed_sandbox_request);
} }
@ -43,7 +43,7 @@ global_request_scheduler_minheap_peek(void)
uint64_t uint64_t
sandbox_request_get_priority_fn(void *element) sandbox_request_get_priority_fn(void *element)
{ {
sandbox_request_t *sandbox_request = (sandbox_request_t *)element; struct sandbox_request *sandbox_request = (struct sandbox_request *)element;
return sandbox_request->absolute_deadline; return sandbox_request->absolute_deadline;
}; };

@ -100,7 +100,7 @@ static void
wasm_fs_callback(uv_fs_t *req) wasm_fs_callback(uv_fs_t *req)
{ {
debuglog("[%p]\n", req->data); debuglog("[%p]\n", req->data);
worker_thread_wakeup_sandbox((sandbox_t *)req->data); worker_thread_wakeup_sandbox((struct sandbox *)req->data);
} }
// We define our own syscall numbers, because WASM uses x86_64 values even on systems that are not x86_64 // We define our own syscall numbers, because WASM uses x86_64 values even on systems that are not x86_64
@ -759,7 +759,7 @@ wasm_fchown(int32_t file_descriptor, uint32_t owner, uint32_t group)
static void static void
wasm_connection_callback(uv_stream_t *srv, int status) wasm_connection_callback(uv_stream_t *srv, int status)
{ {
sandbox_t *s = srv->data; struct sandbox *s = srv->data;
debuglog(" [%p]\n", s); debuglog(" [%p]\n", s);
s->return_value = status; s->return_value = status;
worker_thread_wakeup_sandbox(s); worker_thread_wakeup_sandbox(s);
@ -769,7 +769,7 @@ static void
wasm_connect_callback(uv_connect_t *req, int status) wasm_connect_callback(uv_connect_t *req, int status)
{ {
// TODO: how do we use the handle in uv_connect_t ?? // TODO: how do we use the handle in uv_connect_t ??
sandbox_t *s = req->data; struct sandbox *s = req->data;
debuglog(" [%p]\n", s); debuglog(" [%p]\n", s);
s->return_value = status; s->return_value = status;
worker_thread_wakeup_sandbox(s); worker_thread_wakeup_sandbox(s);

@ -45,7 +45,7 @@ local_runqueue_list_get_next()
{ {
// If our local runqueue is empty, try to pull and allocate a sandbox request from the global request scheduler // If our local runqueue is empty, try to pull and allocate a sandbox request from the global request scheduler
if (local_runqueue_is_empty()) { if (local_runqueue_is_empty()) {
sandbox_request_t *sandbox_request; struct sandbox_request *sandbox_request;
int return_code = global_request_scheduler_remove(&sandbox_request); int return_code = global_request_scheduler_remove(&sandbox_request);
if (return_code != 0) return NULL; if (return_code != 0) return NULL;

@ -82,8 +82,8 @@ local_runqueue_minheap_get_next()
local_runqueue_minheap_add(sandbox); local_runqueue_minheap_add(sandbox);
} else if (sandbox_rc == -1) { } else if (sandbox_rc == -1) {
/* local runqueue was empty, try to pull a sandbox request and return NULL if we're unable to get one */ /* local runqueue was empty, try to pull a sandbox request and return NULL if we're unable to get one */
sandbox_request_t *sandbox_request; struct sandbox_request *sandbox_request;
int sandbox_request_rc = global_request_scheduler_remove(&sandbox_request); int sandbox_request_rc = global_request_scheduler_remove(&sandbox_request);
if (sandbox_request_rc != 0) return NULL; if (sandbox_request_rc != 0) return NULL;
sandbox = sandbox_allocate(sandbox_request); sandbox = sandbox_allocate(sandbox_request);
@ -129,10 +129,10 @@ local_runqueue_minheap_preempt(ucontext_t *user_context)
if (local_deadline == ULONG_MAX) { assert(local_runqueue_minheap.first_free == 1); }; if (local_deadline == ULONG_MAX) { assert(local_runqueue_minheap.first_free == 1); };
/* If we're able to get a sandbox request with a tighter deadline, preempt the current context and run it */ /* If we're able to get a sandbox request with a tighter deadline, preempt the current context and run it */
sandbox_request_t *sandbox_request; struct sandbox_request *sandbox_request;
if (global_deadline < local_deadline) { if (global_deadline < local_deadline) {
sandbox_request_t *sandbox_request; struct sandbox_request *sandbox_request;
int return_code = global_request_scheduler_remove(&sandbox_request); int return_code = global_request_scheduler_remove(&sandbox_request);
// If we were unable to get a sandbox_request, exit // If we were unable to get a sandbox_request, exit
if (return_code != 0) goto done; if (return_code != 0) goto done;

@ -95,7 +95,7 @@ listener_thread_main(void *dummy)
total_requests++; total_requests++;
/* Allocate a Sandbox Request */ /* Allocate a Sandbox Request */
sandbox_request_t *sandbox_request = struct sandbox_request *sandbox_request =
sandbox_request_allocate(module, module->name, socket_descriptor, sandbox_request_allocate(module, module->name, socket_descriptor,
(const struct sockaddr *)&client_address, start_time); (const struct sockaddr *)&client_address, start_time);
assert(sandbox_request); assert(sandbox_request);

@ -346,7 +346,7 @@ err:
} }
int int
sandbox_allocate_stack(sandbox_t *sandbox) sandbox_allocate_stack(struct sandbox *sandbox)
{ {
assert(sandbox); assert(sandbox);
assert(sandbox->module); assert(sandbox->module);
@ -365,7 +365,7 @@ err_stack_allocation_failed:
} }
struct sandbox * struct sandbox *
sandbox_allocate(sandbox_request_t *sandbox_request) sandbox_allocate(struct sandbox_request *sandbox_request)
{ {
assert(sandbox_request != NULL); assert(sandbox_request != NULL);
assert(sandbox_request->module != NULL); assert(sandbox_request->module != NULL);

@ -82,7 +82,7 @@ worker_thread_switch_to_sandbox(struct sandbox *next_sandbox)
* @param sandbox the sandbox to check and update if blocked * @param sandbox the sandbox to check and update if blocked
*/ */
void void
worker_thread_wakeup_sandbox(sandbox_t *sandbox) worker_thread_wakeup_sandbox(struct sandbox *sandbox)
{ {
software_interrupt_disable(); software_interrupt_disable();
if (sandbox->state != SANDBOX_BLOCKED) goto done; if (sandbox->state != SANDBOX_BLOCKED) goto done;
@ -219,7 +219,7 @@ worker_thread_main(void *return_code)
* TODO: Consider moving this to a future current_sandbox file. This has thus far proven difficult to move * TODO: Consider moving this to a future current_sandbox file. This has thus far proven difficult to move
*/ */
void void
worker_thread_on_sandbox_exit(sandbox_t *exiting_sandbox) worker_thread_on_sandbox_exit(struct sandbox *exiting_sandbox)
{ {
assert(exiting_sandbox); assert(exiting_sandbox);

Loading…
Cancel
Save