diff --git a/runtime/include/sandbox.h b/runtime/include/sandbox.h index 1a4698c..aa6e559 100644 --- a/runtime/include/sandbox.h +++ b/runtime/include/sandbox.h @@ -80,14 +80,14 @@ struct sandbox_request { char * args; int sock; struct sockaddr *addr; - unsigned long long int start_time_in_cycles; + u64 start_time; // cycles }; typedef struct sandbox_request sbox_request_t; DEQUE_PROTOTYPE(sandbox, sbox_request_t *); // a runtime resource, malloc on this! -struct sandbox *sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *addr); +struct sandbox *sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *addr, u64 start_time); // should free stack and heap resources.. also any I/O handles. void sandbox_free(struct sandbox *sbox); @@ -104,7 +104,7 @@ sbox_request_alloc( char *args, int sock, const struct sockaddr *addr, - unsigned long long int start_time_in_cycles) + u64 start_time) { // sandbox_alloc seems to be sbox_request_t *s = malloc(sizeof(sbox_request_t)); @@ -113,7 +113,7 @@ sbox_request_alloc( s->args = args; s->sock = sock; s->addr = (struct sockaddr *)addr; - s->start_time_in_cycles = start_time_in_cycles; + s->start_time = start_time; sandbox_run(s); return s; } @@ -239,6 +239,9 @@ sandbox_deque_pop(sbox_request_t **s) return ret; } +/** + * @returns A Sandbox Request or NULL + **/ static inline sbox_request_t * sandbox_deque_steal(void) { diff --git a/runtime/src/main.c b/runtime/src/main.c index 28634be..882087f 100644 --- a/runtime/src/main.c +++ b/runtime/src/main.c @@ -16,7 +16,7 @@ i32 log_file_descriptor = -1; u32 total_online_processors = 0; u32 total_worker_processors = 0; u32 first_worker_processor = 0; -int worker_threads_argument[SBOX_NCORES] = { 0 }; // This is always empty, as we don't pass an argument +int worker_threads_argument[SBOX_NCORES] = { 0 }; // The worker sets its argument to -1 on error pthread_t worker_threads[SBOX_NCORES]; static unsigned long long diff --git a/runtime/src/runtime.c b/runtime/src/runtime.c index 28ddc00..c71a6a6 100644 --- a/runtime/src/runtime.c +++ b/runtime/src/runtime.c @@ -31,7 +31,7 @@ __thread arch_context_t base_context; __thread uv_loop_t uvio; /** - * Append the sandbox to the local_run_queueueue + * Append the sandbox to the local_run_queue * @param s sandbox to add */ static inline void @@ -42,24 +42,28 @@ sandbox_local_run(struct sandbox *s) ps_list_head_append_d(&local_run_queue, s); } +/** + * Pulls up to 1..n sandbox requests, allocates them as sandboxes, sets them as runnable and places them on the local runqueue, and then frees the sandbox requests + * The batch size pulled at once is set by SBOX_PULL_MAX + * @return the number of sandbox requests pulled + */ static inline int sandbox_pull(void) { - int n = 0; - - while (n < SBOX_PULL_MAX) { - sbox_request_t *s = sandbox_deque_steal(); - - if (!s) break; - struct sandbox *sb = sandbox_alloc(s->mod, s->args, s->sock, s->addr); - assert(sb); - free(s); - sb->state = SANDBOX_RUNNABLE; - sandbox_local_run(sb); - n++; + int total_sandboxes_pulled = 0; + + while (total_sandboxes_pulled < SBOX_PULL_MAX) { + sbox_request_t *sandbox_request; + if ((sandbox_request = sandbox_deque_steal()) == NULL) break; + struct sandbox *sandbox = sandbox_alloc(sandbox_request->mod, sandbox_request->args, sandbox_request->sock, sandbox_request->addr, sandbox_request->start_time); + assert(sandbox); + free(sandbox_request); + sandbox->state = SANDBOX_RUNNABLE; + sandbox_local_run(sandbox); + total_sandboxes_pulled++; } - return n; + return total_sandboxes_pulled; } static __thread unsigned int in_callback; @@ -229,14 +233,15 @@ sandbox_run_func(void *data) pthread_exit(data); } +/** + * Push the Sandbox Request to the Global Dequeue + * @param s The sandbox request we're pushing + **/ void -sandbox_run(sbox_request_t *s) +sandbox_run(sbox_request_t *sandbox_request) { - // for now, a pull model... - // sandbox_run adds to the global ready queue.. - // each sandboxing thread pulls off of that global ready queue.. - debuglog("[%p: %s]\n", s, s->mod->name); - sandbox_deque_push(s); + debuglog("[%p: %s]\n", sandbox_request, sandbox_request->mod->name); + sandbox_deque_push(sandbox_request); } // perhaps respond to request @@ -274,7 +279,7 @@ runtime_accept_thdfn(void *d) int total_requests = 0; while (true) { int ready = epoll_wait(epoll_file_descriptor, epoll_events, EPOLL_MAX, -1); - unsigned long long int start_time_in_cycles = rdtsc(); + u64 start_time = rdtsc(); for (int i = 0; i < ready; i++) { if (epoll_events[i].events & EPOLLERR) { perror("epoll_wait"); @@ -292,10 +297,9 @@ runtime_accept_thdfn(void *d) assert(0); } total_requests++; - printf("Received Request %d at %lld\n", total_requests, start_time_in_cycles); + printf("Received Request %d at %lu\n", total_requests, start_time); - // struct sandbox *sb = sandbox_alloc(m, m->name, s, (const struct sockaddr *)&client); - sbox_request_t *sb = sbox_request_alloc(m, m->name, s, (const struct sockaddr *)&client, start_time_in_cycles); + sbox_request_t *sb = sbox_request_alloc(m, m->name, s, (const struct sockaddr *)&client, start_time); assert(sb); } } diff --git a/runtime/src/sandbox.c b/runtime/src/sandbox.c index 3dec631..32a0311 100644 --- a/runtime/src/sandbox.c +++ b/runtime/src/sandbox.c @@ -325,7 +325,7 @@ sandbox_entry(void) } struct sandbox * -sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *addr) +sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *addr, u64 start_time) { if (!module_is_valid(mod)) return NULL; @@ -334,6 +334,9 @@ sandbox_alloc(struct module *mod, char *args, int sock, const struct sockaddr *a struct sandbox *sb = (struct sandbox *)sandbox_memory_map(mod); if (!sb) return NULL; + // Assign the start time from the request + sb->start_time = start_time; + // actual module instantiation! sb->args = (void *)args; sb->stack_size = mod->stack_size; diff --git a/runtime/src/util.c b/runtime/src/util.c index 90f3f3f..db050d8 100644 --- a/runtime/src/util.c +++ b/runtime/src/util.c @@ -212,7 +212,8 @@ parse_sandbox_file_custom(char *filename) assert(0); } - sb = sandbox_alloc(mod, args, 0, NULL); + // TODO: Adding 0 as start time to match new signature. Unsure how this function is used... -SPM + sb = sandbox_alloc(mod, args, 0, NULL, 0); assert(sb); total_boxes++; @@ -262,7 +263,8 @@ util_parse_sandbox_string_json(struct module *mod, char *str, const struct socka *(args + ((k - 1) * MOD_ARG_MAX_SZ) + g->end - g->start) = '\0'; } - struct sandbox *sb = sandbox_alloc(mod, args, 0, addr); + // TODO: Adding 0 as start time to match new signature. Unsure how this function is used... -SPM + struct sandbox *sb = sandbox_alloc(mod, args, 0, addr, 0); assert(sb); return sb; @@ -296,7 +298,8 @@ util_parse_sandbox_string_custom(struct module *mod, char *str, const struct soc assert(ntoks < MOD_MAX_ARGS); } - struct sandbox *sb = sandbox_alloc(mod, args, 0, addr); + // TODO: Adding 0 as start time to match new signature. Unsure how this function is used... -SPM + struct sandbox *sb = sandbox_alloc(mod, args, 0, addr, 0); assert(sb); return sb;