feat: add deadline logic

main
Sean McBride 5 years ago
parent 6478ff9b84
commit 6a7bbc2e97

@ -8,8 +8,9 @@
#include "sandbox.h" #include "sandbox.h"
#include "types.h" #include "types.h"
extern int runtime_epoll_file_descriptor; extern int runtime_epoll_file_descriptor;
extern __thread uv_loop_t worker_thread_uvio_handle; extern __thread uv_loop_t worker_thread_uvio_handle;
extern float runtime_processor_speed_MHz;
void alloc_linear_memory(void); void alloc_linear_memory(void);
void expand_memory(void); void expand_memory(void);

@ -46,6 +46,7 @@ struct sandbox {
u64 total_time; u64 total_time;
u64 start_time; u64 start_time;
u64 absolute_deadline;
struct module *module; // the module this is an instance of struct module *module; // the module this is an instance of
@ -97,7 +98,7 @@ extern void worker_thread_wakeup_sandbox(sandbox_t *sandbox);
**************************/ **************************/
struct sandbox *sandbox_allocate(struct module *module, char *arguments, int socket_descriptor, struct sandbox *sandbox_allocate(struct module *module, char *arguments, int socket_descriptor,
const struct sockaddr *socket_address, u64 start_time); const struct sockaddr *socket_address, u64 start_time, u64 absolute_deadline);
void sandbox_free(struct sandbox *sandbox); void sandbox_free(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);

@ -10,7 +10,8 @@ struct sandbox_request {
char * arguments; char * arguments;
int socket_descriptor; int socket_descriptor;
struct sockaddr *socket_address; struct sockaddr *socket_address;
u64 start_time; // cycles u64 start_time; // cycles
u64 absolute_deadline; // cycles
}; };
typedef struct sandbox_request sandbox_request_t; typedef struct sandbox_request sandbox_request_t;
@ -36,6 +37,10 @@ sandbox_request_allocate(struct module *module, char *arguments, int socket_desc
sandbox_request->socket_descriptor = socket_descriptor; sandbox_request->socket_descriptor = socket_descriptor;
sandbox_request->socket_address = (struct sockaddr *)socket_address; sandbox_request->socket_address = (struct sockaddr *)socket_address;
sandbox_request->start_time = start_time; sandbox_request->start_time = start_time;
sandbox_request->absolute_deadline = start_time + module->relative_deadline_us * runtime_processor_speed_MHz;
// TODO: Refactor to log file
printf("Set absolute deadline of %lu\n", sandbox_request->absolute_deadline);
debuglog("[%p: %s]\n", sandbox_request, sandbox_request->module->name); debuglog("[%p: %s]\n", sandbox_request, sandbox_request->module->name);
return sandbox_request; return sandbox_request;

@ -18,10 +18,11 @@
i32 runtime_log_file_descriptor = -1; i32 runtime_log_file_descriptor = -1;
#endif #endif
u32 runtime_total_online_processors = 0; float runtime_processor_speed_MHz = 0;
u32 runtime_total_worker_processors = 0; u32 runtime_total_online_processors = 0;
u32 runtime_first_worker_processor = 0; u32 runtime_total_worker_processors = 0;
int runtime_worker_threads_argument[WORKER_THREAD_CORE_COUNT] = { 0 }; // The worker sets its argument to -1 on error u32 runtime_first_worker_processor = 0;
int runtime_worker_threads_argument[WORKER_THREAD_CORE_COUNT] = { 0 }; // The worker sets its argument to -1 on error
pthread_t runtime_worker_threads[WORKER_THREAD_CORE_COUNT]; pthread_t runtime_worker_threads[WORKER_THREAD_CORE_COUNT];
@ -88,8 +89,35 @@ runtime_allocate_available_cores()
runtime_total_worker_processors = 1; runtime_total_worker_processors = 1;
} }
printf("Number of cores %u, sandboxing cores %u (start: %u) and module reqs %u\n", printf("Number of cores %u, sandboxing cores %u (start: %u) and module reqs %u\n",
runtime_total_online_processors, runtime_total_worker_processors, runtime_first_worker_processor, runtime_total_online_processors, runtime_total_worker_processors, runtime_first_worker_processor,
LISTENER_THREAD_CORE_ID); LISTENER_THREAD_CORE_ID);
}
/**
* Returns a float of the cpu MHz entry for CPU0 in /proc/cpuinfo
* We are assuming all cores are the same clock speed, which is not true of many systems
* We are also assuming this value is static
* @return proceccor speed in MHz
**/
static inline float
runtime_get_processor_speed_MHz(void)
{
FILE *cmd = popen("grep '^cpu MHz' /proc/cpuinfo | head -n 1 | awk '{print $4}'", "r");
if (cmd == NULL) return -1;
float processor_speed_MHz;
size_t n;
char buff[16];
if ((n = fread(buff, 1, sizeof(buff) - 1, cmd)) <= 0) return -1;
buff[n] = '\0';
if (sscanf(buff, "%f", &processor_speed_MHz) != 1) return -1;
pclose(cmd);
return processor_speed_MHz;
} }
#ifdef DEBUG #ifdef DEBUG
@ -168,6 +196,9 @@ main(int argc, char **argv)
memset(runtime_worker_threads, 0, sizeof(pthread_t) * WORKER_THREAD_CORE_COUNT); memset(runtime_worker_threads, 0, sizeof(pthread_t) * WORKER_THREAD_CORE_COUNT);
runtime_processor_speed_MHz = runtime_get_processor_speed_MHz();
printf("Detected processor speed of %f MHz\n", runtime_processor_speed_MHz);
runtime_set_resource_limits_to_max(); runtime_set_resource_limits_to_max();
runtime_allocate_available_cores(); runtime_allocate_available_cores();
runtime_initialize(); runtime_initialize();

@ -272,8 +272,8 @@ worker_thread_pull_and_process_sandbox_requests(void)
// Actually allocate the sandbox for the requests that we've pulled // Actually allocate the sandbox for the requests that we've pulled
struct sandbox *sandbox = sandbox_allocate(sandbox_request->module, sandbox_request->arguments, struct sandbox *sandbox = sandbox_allocate(sandbox_request->module, sandbox_request->arguments,
sandbox_request->socket_descriptor, sandbox_request->socket_descriptor,
sandbox_request->socket_address, sandbox_request->socket_address, sandbox_request->start_time,
sandbox_request->start_time); sandbox_request->absolute_deadline);
assert(sandbox); assert(sandbox);
free(sandbox_request); free(sandbox_request);
// Set the sandbox as runnable and place on the local runqueue // Set the sandbox as runnable and place on the local runqueue

@ -139,7 +139,17 @@ current_sandbox_build_and_send_client_response(void)
done: done:
assert(sndsz == curr->request_response_data_length); assert(sndsz == curr->request_response_data_length);
// Get End Timestamp // Get End Timestamp
curr->total_time = __getcycles() - curr->start_time; u64 end_time = __getcycles();
curr->total_time = end_time - curr->start_time;
// TODO: Refactor to log file
printf("Function returned in %lu cycles\n", curr->total_time);
if (end_time < curr->absolute_deadline) {
printf("Deadline Met with %f us to spare\n",
(curr->absolute_deadline - end_time) / runtime_processor_speed_MHz);
} else {
printf("Deadline NOT MET! Overran by %f us\n",
(end_time - curr->absolute_deadline) / runtime_processor_speed_MHz);
}
#ifndef USE_HTTP_UVIO #ifndef USE_HTTP_UVIO
int r = send(curr->client_socket_descriptor, curr->request_response_data, sndsz, 0); int r = send(curr->client_socket_descriptor, curr->request_response_data, sndsz, 0);
@ -296,7 +306,7 @@ sandbox_allocate_memory(struct module *module)
struct sandbox * struct sandbox *
sandbox_allocate(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address, sandbox_allocate(struct module *module, char *arguments, int socket_descriptor, const struct sockaddr *socket_address,
u64 start_time) u64 start_time, u64 absolute_deadline)
{ {
if (!module_is_valid(module)) return NULL; if (!module_is_valid(module)) return NULL;
@ -306,7 +316,8 @@ sandbox_allocate(struct module *module, char *arguments, int socket_descriptor,
if (!sandbox) return NULL; if (!sandbox) return NULL;
// Assign the start time from the request // Assign the start time from the request
sandbox->start_time = start_time; sandbox->start_time = start_time;
sandbox->absolute_deadline = absolute_deadline;
// actual module instantiation! // actual module instantiation!
sandbox->arguments = (void *)arguments; sandbox->arguments = (void *)arguments;

@ -13,6 +13,7 @@ static sandbox_request_t *
sandbox_request_scheduler_ps_add(void *sandbox_request_raw) sandbox_request_scheduler_ps_add(void *sandbox_request_raw)
{ {
// TODO // TODO
return NULL;
} }
/** /**
@ -23,6 +24,7 @@ static sandbox_request_t *
sandbox_request_scheduler_ps_remove(void) sandbox_request_scheduler_ps_remove(void)
{ {
// TODO // TODO
return NULL;
} }
/** /**

Loading…
Cancel
Save