chore: de-dunder runtime

master
Sean McBride 5 years ago
parent 4d660e666b
commit ba032a5905

@ -4,7 +4,7 @@
#include "sandbox.h"
#include "types.h"
extern http_parser_settings runtime__http_parser_settings;
extern http_parser_settings runtime_http_parser_settings;
/**
* Getter for the current sandbox executing on this thread

@ -8,9 +8,9 @@
#include "sandbox.h"
#include "types.h"
extern int runtime__epoll_file_descriptor;
extern struct deque_sandbox *runtime__global_deque;
extern pthread_mutex_t runtime__global_deque_mutex;
extern int runtime_epoll_file_descriptor;
extern struct deque_sandbox *runtime_global_deque;
extern pthread_mutex_t runtime_global_deque_mutex;
extern __thread uv_loop_t worker_thread__uvio_handle;
void alloc_linear_memory(void);
@ -18,7 +18,7 @@ void expand_memory(void);
void free_linear_memory(void *base, u32 bound, u32 max);
INLINE char *get_function_from_table(u32 idx, u32 type_id);
INLINE char *get_memory_ptr_for_runtime(u32 offset, u32 bounds_check);
void runtime__initialize(void);
void runtime_initialize(void);
void listener_thread_initialize(void);
void stub_init(i32 offset);
void * worker_thread__main(void *return_code);

@ -28,11 +28,11 @@ sandbox_request__push_to_dequeue(sandbox_request_t *sandbox_request)
// 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
#if NCORES == 1
pthread_mutex_lock(&runtime__global_deque_mutex);
pthread_mutex_lock(&runtime_global_deque_mutex);
#endif
return_code = deque_push_sandbox(runtime__global_deque, &sandbox_request);
return_code = deque_push_sandbox(runtime_global_deque, &sandbox_request);
#if NCORES == 1
pthread_mutex_unlock(&runtime__global_deque_mutex);
pthread_mutex_unlock(&runtime_global_deque_mutex);
#endif
return return_code;
@ -76,11 +76,11 @@ sandbox_request__pop_from_dequeue(sandbox_request_t **sandbox_request)
// 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
#if NCORES == 1
pthread_mutex_lock(&runtime__global_deque_mutex);
pthread_mutex_lock(&runtime_global_deque_mutex);
#endif
return_code = deque_pop_sandbox(runtime__global_deque, sandbox_request);
return_code = deque_pop_sandbox(runtime_global_deque, sandbox_request);
#if NCORES == 1
pthread_mutex_unlock(&runtime__global_deque_mutex);
pthread_mutex_unlock(&runtime_global_deque_mutex);
#endif
return return_code;
}
@ -107,7 +107,7 @@ sandbox_request__steal_from_dequeue(void)
#if NCORES == 1
sandbox_request__pop_from_dequeue(&sandbox_request);
#else
int r = deque_steal_sandbox(runtime__global_deque, &sandbox_request);
int r = deque_steal_sandbox(runtime_global_deque, &sandbox_request);
if (r) sandbox_request = NULL;
#endif

@ -81,7 +81,7 @@ extern __thread struct indirect_table_entry *module_indirect_table;
// for sandbox linear memory isolation
extern __thread void *sandbox_lmbase;
extern __thread u32 sandbox_lmbound;
extern i32 runtime__log_file_descriptor; // TODO: LOG_TO_FILE logic is untested
extern i32 runtime_log_file_descriptor; // TODO: LOG_TO_FILE logic is untested
// functions in the module to lookup and call per sandbox.
typedef i32 (*mod_main_fn_t)(i32 a, i32 b);
@ -102,13 +102,13 @@ typedef enum
/**
* debuglog is a macro that behaves based on the macros DEBUG and LOG_TO_FILE
* If DEBUG is not set, debuglog does nothing
* If DEBUG is set and LOG_TO_FILE is set, debuglog prints to the logfile defined in runtime__log_file_descriptor
* If DEBUG is set and LOG_TO_FILE is set, debuglog prints to the logfile defined in runtime_log_file_descriptor
* If DEBUG is set adn LOG_TO_FILE is not set, debuglog prints to STDOUT
**/
#ifdef DEBUG
#ifdef LOG_TO_FILE
#define debuglog(fmt, ...) \
dprintf(runtime__log_file_descriptor, "(%d,%lu) %s: " fmt, sched_getcpu(), pthread_self(), __func__, \
dprintf(runtime_log_file_descriptor, "(%d,%lu) %s: " fmt, sched_getcpu(), pthread_self(), __func__, \
##__VA_ARGS__)
#else // !LOG_TO_FILE
#define debuglog(fmt, ...) printf("(%d,%lu) %s: " fmt, sched_getcpu(), pthread_self(), __func__, ##__VA_ARGS__)

@ -15,14 +15,14 @@
// Conditionally used by debuglog when DEBUG is set
#ifdef DEBUG
i32 runtime__log_file_descriptor = -1;
i32 runtime_log_file_descriptor = -1;
#endif
u32 runtime__total_online_processors = 0;
u32 runtime__total_worker_processors = 0;
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];
u32 runtime_total_online_processors = 0;
u32 runtime_total_worker_processors = 0;
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];
/**
@ -30,7 +30,7 @@ pthread_t runtime__worker_threads[WORKER_THREAD__CORE_COUNT];
* @param cmd - The command the user entered
**/
static void
runtime__usage(char *cmd)
runtime_usage(char *cmd)
{
printf("%s <modules_file>\n", cmd);
debuglog("%s <modules_file>\n", cmd);
@ -41,7 +41,7 @@ runtime__usage(char *cmd)
* (RLIMIT_NOFILE) soft limit to its hard limit (see man getrlimit)
**/
void
runtime__set_resource_limits_to_max()
runtime_set_resource_limits_to_max()
{
struct rlimit resource_limit;
if (getrlimit(RLIMIT_DATA, &resource_limit) < 0) {
@ -68,27 +68,27 @@ runtime__set_resource_limits_to_max()
* Check the number of cores and the compiler flags and allocate available cores
**/
void
runtime__allocate_available_cores()
runtime_allocate_available_cores()
{
// Find the number of processors currently online
runtime__total_online_processors = sysconf(_SC_NPROCESSORS_ONLN);
runtime_total_online_processors = sysconf(_SC_NPROCESSORS_ONLN);
// If multicore, we'll pin one core as a listener and run sandbox threads on all others
if (runtime__total_online_processors > 1) {
runtime__first_worker_processor = 1;
if (runtime_total_online_processors > 1) {
runtime_first_worker_processor = 1;
// WORKER_THREAD__CORE_COUNT can be used as a cap on the number of cores to use
// But if there are few cores that WORKER_THREAD__CORE_COUNT, just use what is available
u32 max_possible_workers = runtime__total_online_processors - 1;
runtime__total_worker_processors = (max_possible_workers >= WORKER_THREAD__CORE_COUNT)
u32 max_possible_workers = runtime_total_online_processors - 1;
runtime_total_worker_processors = (max_possible_workers >= WORKER_THREAD__CORE_COUNT)
? WORKER_THREAD__CORE_COUNT
: max_possible_workers;
} else {
// If single core, we'll do everything on CPUID 0
runtime__first_worker_processor = 0;
runtime__total_worker_processors = 1;
runtime_first_worker_processor = 0;
runtime_total_worker_processors = 1;
}
debuglog("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);
}
@ -99,19 +99,19 @@ runtime__allocate_available_cores()
* Otherwise, it writes to STDOUT
**/
void
runtime__process_debug_log_behavior()
runtime_process_debug_log_behavior()
{
#ifdef LOG_TO_FILE
fclose(stdout);
fclose(stderr);
fclose(stdin);
runtime__log_file_descriptor = open(RUNTIME__LOG_FILE, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU | S_IRWXG);
if (runtime__log_file_descriptor < 0) {
runtime_log_file_descriptor = open(RUNTIME__LOG_FILE, O_CREAT | O_TRUNC | O_WRONLY, S_IRWXU | S_IRWXG);
if (runtime_log_file_descriptor < 0) {
perror("open");
exit(-1);
}
#else
runtime__log_file_descriptor = 1;
runtime_log_file_descriptor = 1;
#endif // LOG_TO_FILE
}
#endif // DEBUG
@ -120,11 +120,11 @@ runtime__process_debug_log_behavior()
* Starts all worker threads and sleeps forever on pthread_join, which should never return
**/
void
runtime__start_runtime__worker_threads()
runtime_start_runtime_worker_threads()
{
for (int i = 0; i < runtime__total_worker_processors; i++) {
int ret = pthread_create(&runtime__worker_threads[i], NULL, worker_thread__main,
(void *)&runtime__worker_threads_argument[i]);
for (int i = 0; i < runtime_total_worker_processors; i++) {
int ret = pthread_create(&runtime_worker_threads[i], NULL, worker_thread__main,
(void *)&runtime_worker_threads_argument[i]);
if (ret) {
errno = ret;
perror("pthread_create");
@ -133,14 +133,14 @@ runtime__start_runtime__worker_threads()
cpu_set_t cs;
CPU_ZERO(&cs);
CPU_SET(runtime__first_worker_processor + i, &cs);
ret = pthread_setaffinity_np(runtime__worker_threads[i], sizeof(cs), &cs);
CPU_SET(runtime_first_worker_processor + i, &cs);
ret = pthread_setaffinity_np(runtime_worker_threads[i], sizeof(cs), &cs);
assert(ret == 0);
}
debuglog("Sandboxing environment ready!\n");
for (int i = 0; i < runtime__total_worker_processors; i++) {
int ret = pthread_join(runtime__worker_threads[i], NULL);
for (int i = 0; i < runtime_total_worker_processors; i++) {
int ret = pthread_join(runtime_worker_threads[i], NULL);
if (ret) {
errno = ret;
perror("pthread_join");
@ -156,20 +156,20 @@ int
main(int argc, char **argv)
{
#ifdef DEBUG
runtime__process_debug_log_behavior();
runtime_process_debug_log_behavior();
#endif
printf("Starting Awsm\n");
if (argc != 2) {
runtime__usage(argv[0]);
runtime_usage(argv[0]);
exit(-1);
}
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__set_resource_limits_to_max();
runtime__allocate_available_cores();
runtime__initialize();
runtime_set_resource_limits_to_max();
runtime_allocate_available_cores();
runtime_initialize();
debuglog("Parsing modules file [%s]\n", argv[1]);
if (module_new_from_json(argv[1])) {
@ -178,5 +178,5 @@ main(int argc, char **argv)
}
listener_thread_initialize();
runtime__start_runtime__worker_threads();
runtime_start_runtime_worker_threads();
}

@ -50,7 +50,7 @@ module_initialize_as_server(struct module *module)
struct epoll_event accept_evt;
accept_evt.data.ptr = (void *)module;
accept_evt.events = EPOLLIN;
if (epoll_ctl(runtime__epoll_file_descriptor, EPOLL_CTL_ADD, module->socket_descriptor, &accept_evt) < 0)
if (epoll_ctl(runtime_epoll_file_descriptor, EPOLL_CTL_ADD, module->socket_descriptor, &accept_evt) < 0)
assert(0);
}

@ -28,10 +28,10 @@
* Shared Process State *
**************************/
struct deque_sandbox *runtime__global_deque;
pthread_mutex_t runtime__global_deque_mutex = PTHREAD_MUTEX_INITIALIZER;
int runtime__epoll_file_descriptor;
http_parser_settings runtime__http_parser_settings;
struct deque_sandbox *runtime_global_deque;
pthread_mutex_t runtime_global_deque_mutex = PTHREAD_MUTEX_INITIALIZER;
int runtime_epoll_file_descriptor;
http_parser_settings runtime_http_parser_settings;
/******************************************
* Shared Process / Listener Thread Logic *
@ -41,23 +41,23 @@ http_parser_settings runtime__http_parser_settings;
* Initialize runtime global state, mask signals, and init http parser
*/
void
runtime__initialize(void)
runtime_initialize(void)
{
runtime__epoll_file_descriptor = epoll_create1(0);
assert(runtime__epoll_file_descriptor >= 0);
runtime_epoll_file_descriptor = epoll_create1(0);
assert(runtime_epoll_file_descriptor >= 0);
// Allocate and Initialize the global deque
runtime__global_deque = (struct deque_sandbox *)malloc(sizeof(struct deque_sandbox));
assert(runtime__global_deque);
runtime_global_deque = (struct deque_sandbox *)malloc(sizeof(struct deque_sandbox));
assert(runtime_global_deque);
// Note: Below is a Macro
deque_init_sandbox(runtime__global_deque, RUNTIME__MAX_SANDBOX_REQUEST_COUNT);
deque_init_sandbox(runtime_global_deque, RUNTIME__MAX_SANDBOX_REQUEST_COUNT);
// Mask Signals
software_interrupt__mask_signal(SIGUSR1);
software_interrupt__mask_signal(SIGALRM);
// Initialize http_parser_settings global
http_parser_settings_initialize(&runtime__http_parser_settings);
http_parser_settings_initialize(&runtime_http_parser_settings);
}
/********************************
@ -71,7 +71,7 @@ runtime__initialize(void)
* @return NULL
*
* Used Globals:
* runtime__epoll_file_descriptor - the epoll file descriptor
* runtime_epoll_file_descriptor - the epoll file descriptor
*
*/
void *
@ -82,7 +82,7 @@ listener_thread_main(void *dummy)
int total_requests = 0;
while (true) {
int request_count = epoll_wait(runtime__epoll_file_descriptor, epoll_events,
int request_count = epoll_wait(runtime_epoll_file_descriptor, epoll_events,
LISTENER_THREAD__MAX_EPOLL_EVENTS, -1);
u64 start_time = util__rdtsc();
for (int i = 0; i < request_count; i++) {

@ -47,14 +47,14 @@ current_sandbox_setup_arguments(i32 argument_count)
* @param length The size of the request_response_data that we want to parse
* @returns 0
*
* Globals: runtime__http_parser_settings
* Globals: runtime_http_parser_settings
**/
int
sandbox__parse_http_request(struct sandbox *sandbox, size_t length)
{
// Why is our start address sandbox->request_response_data + sandbox->request_response_data_length?
// it's like a cursor to keep track of what we've read so far
http_parser_execute(&sandbox->http_parser, &runtime__http_parser_settings,
http_parser_execute(&sandbox->http_parser, &runtime_http_parser_settings,
sandbox->request_response_data + sandbox->request_response_data_length, length);
return 0;
}

@ -32,7 +32,7 @@ __thread volatile sig_atomic_t software_interrupt__is_disabled = 0;
* Externs
***************************************/
extern pthread_t runtime__worker_threads[];
extern pthread_t runtime_worker_threads[];
/***************************************
* Private Static Inlines
@ -65,11 +65,11 @@ software_interrupt__handle_signals(int signal_type, siginfo_t *signal_info, void
int rt = 0;
// deliver signal to all other runtime threads..
for (int i = 0; i < WORKER_THREAD__CORE_COUNT; i++) {
if (pthread_self() == runtime__worker_threads[i]) {
if (pthread_self() == runtime_worker_threads[i]) {
rt = 1;
continue;
}
pthread_kill(runtime__worker_threads[i], SIGALRM);
pthread_kill(runtime_worker_threads[i], SIGALRM);
}
assert(rt == 1);
} else {

Loading…
Cancel
Save