parent
2c1a33970e
commit
815546852c
@ -0,0 +1,58 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "arch/getcycles.h"
|
||||
#include "local_runqueue.h"
|
||||
#include "panic.h"
|
||||
#include "sandbox_types.h"
|
||||
|
||||
/**
|
||||
* Transitions a sandbox to the SANDBOX_PREEMPTED state.
|
||||
*
|
||||
* This occurs in the following scenarios:
|
||||
* - A sandbox in the SANDBOX_INITIALIZED state completes initialization and is ready to be run
|
||||
* - A sandbox in the SANDBOX_BLOCKED state completes what was blocking it and is ready to be run
|
||||
*
|
||||
* @param sandbox
|
||||
* @param last_state the state the sandbox is transitioning from. This is expressed as a constant to
|
||||
* enable the compiler to perform constant propagation optimizations.
|
||||
*/
|
||||
static inline void
|
||||
sandbox_set_as_preempted(struct sandbox *sandbox, sandbox_state_t last_state)
|
||||
{
|
||||
assert(sandbox);
|
||||
|
||||
/* Preemption occurs indirectly via the SANDBOX_RUNNING_KERNEL state, so preemptable is set
|
||||
* to false during the process of preemption.
|
||||
*/
|
||||
assert(sandbox->ctxt.preemptable == false);
|
||||
|
||||
uint64_t now = __getcycles();
|
||||
uint64_t duration_of_last_state = now - sandbox->timestamp_of.last_state_change;
|
||||
|
||||
sandbox->state = SANDBOX_SET_AS_PREEMPTED;
|
||||
sandbox_state_history_append(sandbox, SANDBOX_SET_AS_PREEMPTED);
|
||||
|
||||
switch (last_state) {
|
||||
case SANDBOX_RUNNING_KERNEL: {
|
||||
sandbox->duration_of_state.preempted += duration_of_last_state;
|
||||
current_sandbox_set(NULL);
|
||||
runtime_worker_threads_deadline[worker_thread_idx] = UINT64_MAX;
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
panic("Sandbox %lu | Illegal transition from %s to Preempted\n", sandbox->id,
|
||||
sandbox_state_stringify(last_state));
|
||||
}
|
||||
}
|
||||
|
||||
sandbox->timestamp_of.last_state_change = now;
|
||||
sandbox->state = SANDBOX_PREEMPTED;
|
||||
|
||||
/* State Change Bookkeeping */
|
||||
sandbox_state_history_append(sandbox, SANDBOX_PREEMPTED);
|
||||
runtime_sandbox_total_increment(SANDBOX_PREEMPTED);
|
||||
runtime_sandbox_total_decrement(last_state);
|
||||
}
|
@ -1,41 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "arch/getcycles.h"
|
||||
#include "panic.h"
|
||||
#include "sandbox_types.h"
|
||||
|
||||
static inline void
|
||||
sandbox_set_as_running(struct sandbox *sandbox, sandbox_state_t last_state)
|
||||
{
|
||||
assert(sandbox);
|
||||
|
||||
uint64_t now = __getcycles();
|
||||
uint64_t duration_of_last_state = now - sandbox->timestamp_of.last_state_change;
|
||||
|
||||
sandbox->state = SANDBOX_SET_AS_RUNNING;
|
||||
|
||||
switch (last_state) {
|
||||
case SANDBOX_RUNNABLE: {
|
||||
sandbox->duration_of_state.runnable += duration_of_last_state;
|
||||
current_sandbox_set(sandbox);
|
||||
runtime_worker_threads_deadline[worker_thread_idx] = sandbox->absolute_deadline;
|
||||
/* Does not handle context switch because the caller knows if we need to use fast or slow switched */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
panic("Sandbox %lu | Illegal transition from %s to Running\n", sandbox->id,
|
||||
sandbox_state_stringify(last_state));
|
||||
}
|
||||
}
|
||||
|
||||
sandbox->timestamp_of.last_state_change = now;
|
||||
sandbox->state = SANDBOX_RUNNING;
|
||||
|
||||
/* State Change Bookkeeping */
|
||||
sandbox_state_log_transition(sandbox->id, last_state, SANDBOX_RUNNING);
|
||||
runtime_sandbox_total_increment(SANDBOX_RUNNING);
|
||||
runtime_sandbox_total_decrement(last_state);
|
||||
}
|
@ -0,0 +1,71 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "arch/getcycles.h"
|
||||
#include "current_sandbox.h"
|
||||
#include "panic.h"
|
||||
#include "sandbox_types.h"
|
||||
#include "sandbox_functions.h"
|
||||
|
||||
static inline void
|
||||
sandbox_set_as_running_kernel(struct sandbox *sandbox, sandbox_state_t last_state)
|
||||
{
|
||||
assert(sandbox);
|
||||
|
||||
/* Disable preemption at start of state transition */
|
||||
if (last_state == SANDBOX_RUNNING_USER) {
|
||||
assert(sandbox->ctxt.preemptable == true);
|
||||
sandbox_disable_preemption(sandbox);
|
||||
} else {
|
||||
assert(sandbox->ctxt.preemptable == false);
|
||||
}
|
||||
|
||||
uint64_t now = __getcycles();
|
||||
uint64_t duration_of_last_state = now - sandbox->timestamp_of.last_state_change;
|
||||
|
||||
sandbox->state = SANDBOX_SET_AS_RUNNING_KERNEL;
|
||||
sandbox_state_history_append(sandbox, SANDBOX_SET_AS_RUNNING_KERNEL);
|
||||
|
||||
switch (last_state) {
|
||||
case SANDBOX_RUNNING_USER: {
|
||||
assert(sandbox == current_sandbox_get());
|
||||
sandbox->duration_of_state.running_user += duration_of_last_state;
|
||||
assert(worker_thread_current_sandbox == sandbox);
|
||||
assert(runtime_worker_threads_deadline[worker_thread_idx] == sandbox->absolute_deadline);
|
||||
break;
|
||||
}
|
||||
case SANDBOX_RUNNABLE: {
|
||||
assert(sandbox);
|
||||
sandbox->duration_of_state.runnable += duration_of_last_state;
|
||||
current_sandbox_set(sandbox);
|
||||
runtime_worker_threads_deadline[worker_thread_idx] = sandbox->absolute_deadline;
|
||||
/* Does not handle context switch because the caller knows if we need to use fast or slow switched. We
|
||||
* can fix this by breakout out SANDBOX_RUNNABLE and SANDBOX_PREEMPTED */
|
||||
break;
|
||||
}
|
||||
case SANDBOX_PREEMPTED: {
|
||||
assert(sandbox);
|
||||
assert(sandbox->interrupted_state == SANDBOX_RUNNING_USER);
|
||||
sandbox->duration_of_state.preempted += duration_of_last_state;
|
||||
current_sandbox_set(sandbox);
|
||||
runtime_worker_threads_deadline[worker_thread_idx] = sandbox->absolute_deadline;
|
||||
/* Does not handle context switch because the caller knows if we need to use fast or slow switched. We
|
||||
* can fix this by breakout out SANDBOX_RUNNABLE and SANDBOX_PREEMPTED */
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
panic("Sandbox %lu | Illegal transition from %s to Running Kernel\n", sandbox->id,
|
||||
sandbox_state_stringify(last_state));
|
||||
}
|
||||
}
|
||||
|
||||
sandbox->timestamp_of.last_state_change = now;
|
||||
sandbox->state = SANDBOX_RUNNING_KERNEL;
|
||||
|
||||
/* State Change Bookkeeping */
|
||||
sandbox_state_history_append(sandbox, SANDBOX_RUNNING_KERNEL);
|
||||
runtime_sandbox_total_increment(SANDBOX_RUNNING_KERNEL);
|
||||
runtime_sandbox_total_decrement(last_state);
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
#pragma once
|
||||
|
||||
#include <assert.h>
|
||||
#include <stdint.h>
|
||||
|
||||
#include "arch/getcycles.h"
|
||||
#include "current_sandbox.h"
|
||||
#include "panic.h"
|
||||
#include "sandbox_types.h"
|
||||
#include "sandbox_functions.h"
|
||||
|
||||
static inline void
|
||||
sandbox_set_as_running_user(struct sandbox *sandbox, sandbox_state_t last_state)
|
||||
{
|
||||
assert(sandbox);
|
||||
|
||||
uint64_t now = __getcycles();
|
||||
uint64_t duration_of_last_state = now - sandbox->timestamp_of.last_state_change;
|
||||
|
||||
sandbox->state = SANDBOX_SET_AS_RUNNING_USER;
|
||||
|
||||
switch (last_state) {
|
||||
case SANDBOX_RUNNING_KERNEL: {
|
||||
sandbox->duration_of_state.running_user += duration_of_last_state;
|
||||
assert(sandbox == current_sandbox_get());
|
||||
assert(runtime_worker_threads_deadline[worker_thread_idx] == sandbox->absolute_deadline);
|
||||
|
||||
break;
|
||||
}
|
||||
default: {
|
||||
panic("Sandbox %lu | Illegal transition from %s to Running\n", sandbox->id,
|
||||
sandbox_state_stringify(last_state));
|
||||
}
|
||||
}
|
||||
|
||||
sandbox->timestamp_of.last_state_change = now;
|
||||
sandbox->state = SANDBOX_RUNNING_USER;
|
||||
|
||||
/* State Change Bookkeeping */
|
||||
sandbox_state_history_append(sandbox, SANDBOX_RUNNING_USER);
|
||||
runtime_sandbox_total_increment(SANDBOX_RUNNING_USER);
|
||||
runtime_sandbox_total_decrement(last_state);
|
||||
|
||||
/* Enable preemption at the end of state transition*/
|
||||
assert(last_state == SANDBOX_RUNNING_KERNEL);
|
||||
|
||||
sandbox_enable_preemption(sandbox);
|
||||
}
|
Loading…
Reference in new issue