feat: Improved sandbox timekeeping

master
Sean McBride 3 years ago
parent 72e6c3e043
commit 06910736d1

@ -100,7 +100,8 @@
"sandbox_set_as_running_user.h": "c",
"scheduler.h": "c",
"sandbox_set_as_returned.h": "c",
"software_interrupt_counts.h": "c"
"software_interrupt_counts.h": "c",
"sandbox_set_as_running_sys.h": "c"
},
"files.exclude": {
"**/.git": true,

@ -1 +1,3 @@
This is a state transition diagram of a sandbox. This maps to the state transition functions defined in `runtime/include/sandbox_set_as_*.h`
Technically, this does not capture all state transitions to or from SANDBOX_INTERRUPTED, as any state can be interrupted by a SIGALRM and this would clutter the diagram. The only transitions shown to or from SANDBOX_INTERRUPTED are those leading to SANDBOX_PREEMPTED, as this reflects actual changes to a sandbox within the scheduler. All other transitions to/from SANDBOX_INTERRUPTED are mostly concerned with preventing scheduler execution time from being counted against sandbox execution times.

@ -2,11 +2,19 @@ digraph {
Uninitialized -> Initialized
Initialized -> {Runnable Error}
Runnable -> Running_Sys
Running_User -> Running_Sys [label="interrupt"]
Running_User -> Interrupted [label="interrupt"]
Running_User -> Running_Sys [label="syscall"]
Interrupted -> Preempted [label="preempt"]
Interrupted -> Running_User
Running_Sys -> Asleep [label="sleep"]
Running_Sys -> {Error Returned}
Running_Sys -> Running_User [label="return"]
Running_Sys -> Preempted [label="preempt"]
Preempted -> Running_User
Returned -> Complete [label="exit_success"]
Asleep -> Runnable [label="wakeup"]

@ -4,154 +4,173 @@
<!-- Generated by graphviz version 2.43.0 (0)
-->
<!-- Title: %3 Pages: 1 -->
<svg width="650pt" height="437pt"
viewBox="0.00 0.00 649.79 437.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 433)">
<svg width="480pt" height="524pt"
viewBox="0.00 0.00 479.99 524.00" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
<g id="graph0" class="graph" transform="scale(1 1) rotate(0) translate(4 520)">
<title>%3</title>
<polygon fill="white" stroke="transparent" points="-4,4 -4,-433 645.79,-433 645.79,4 -4,4"/>
<polygon fill="white" stroke="transparent" points="-4,4 -4,-520 475.99,-520 475.99,4 -4,4"/>
<!-- Uninitialized -->
<g id="node1" class="node">
<title>Uninitialized</title>
<ellipse fill="none" stroke="black" cx="70.19" cy="-411" rx="70.39" ry="18"/>
<text text-anchor="middle" x="70.19" y="-407.3" font-family="Times,serif" font-size="14.00">Uninitialized</text>
<ellipse fill="none" stroke="black" cx="344.6" cy="-498" rx="70.39" ry="18"/>
<text text-anchor="middle" x="344.6" y="-494.3" font-family="Times,serif" font-size="14.00">Uninitialized</text>
</g>
<!-- Initialized -->
<g id="node2" class="node">
<title>Initialized</title>
<ellipse fill="none" stroke="black" cx="70.19" cy="-338" rx="57.39" ry="18"/>
<text text-anchor="middle" x="70.19" y="-334.3" font-family="Times,serif" font-size="14.00">Initialized</text>
<ellipse fill="none" stroke="black" cx="344.6" cy="-425" rx="57.39" ry="18"/>
<text text-anchor="middle" x="344.6" y="-421.3" font-family="Times,serif" font-size="14.00">Initialized</text>
</g>
<!-- Uninitialized&#45;&gt;Initialized -->
<g id="edge1" class="edge">
<title>Uninitialized&#45;&gt;Initialized</title>
<path fill="none" stroke="black" d="M70.19,-392.81C70.19,-384.79 70.19,-375.05 70.19,-366.07"/>
<polygon fill="black" stroke="black" points="73.69,-366.03 70.19,-356.03 66.69,-366.03 73.69,-366.03"/>
<path fill="none" stroke="black" d="M344.6,-479.81C344.6,-471.79 344.6,-462.05 344.6,-453.07"/>
<polygon fill="black" stroke="black" points="348.1,-453.03 344.6,-443.03 341.1,-453.03 348.1,-453.03"/>
</g>
<!-- Runnable -->
<g id="node3" class="node">
<title>Runnable</title>
<ellipse fill="none" stroke="black" cx="266.19" cy="-265" rx="54.69" ry="18"/>
<text text-anchor="middle" x="266.19" y="-261.3" font-family="Times,serif" font-size="14.00">Runnable</text>
<ellipse fill="none" stroke="black" cx="232.6" cy="-352" rx="54.69" ry="18"/>
<text text-anchor="middle" x="232.6" y="-348.3" font-family="Times,serif" font-size="14.00">Runnable</text>
</g>
<!-- Initialized&#45;&gt;Runnable -->
<g id="edge2" class="edge">
<title>Initialized&#45;&gt;Runnable</title>
<path fill="none" stroke="black" d="M106.14,-323.98C138.57,-312.23 186.39,-294.91 221.32,-282.26"/>
<polygon fill="black" stroke="black" points="222.59,-285.52 230.8,-278.82 220.21,-278.94 222.59,-285.52"/>
<path fill="none" stroke="black" d="M320.29,-408.59C304.3,-398.45 283.15,-385.05 265.63,-373.94"/>
<polygon fill="black" stroke="black" points="267.17,-370.77 256.85,-368.38 263.42,-376.69 267.17,-370.77"/>
</g>
<!-- Error -->
<g id="node4" class="node">
<title>Error</title>
<ellipse fill="none" stroke="black" cx="52.19" cy="-105" rx="36.29" ry="18"/>
<text text-anchor="middle" x="52.19" y="-101.3" font-family="Times,serif" font-size="14.00">Error</text>
<ellipse fill="none" stroke="black" cx="435.6" cy="-192" rx="36.29" ry="18"/>
<text text-anchor="middle" x="435.6" y="-188.3" font-family="Times,serif" font-size="14.00">Error</text>
</g>
<!-- Initialized&#45;&gt;Error -->
<g id="edge3" class="edge">
<title>Initialized&#45;&gt;Error</title>
<path fill="none" stroke="black" d="M68.84,-319.64C65.73,-279.74 58.06,-181.27 54.32,-133.24"/>
<polygon fill="black" stroke="black" points="57.8,-132.95 53.54,-123.25 50.83,-133.49 57.8,-132.95"/>
<path fill="none" stroke="black" d="M351.29,-407.01C367,-367.13 406.34,-267.27 425.2,-219.4"/>
<polygon fill="black" stroke="black" points="428.49,-220.6 428.9,-210.01 421.97,-218.03 428.49,-220.6"/>
</g>
<!-- Running_Sys -->
<g id="node5" class="node">
<title>Running_Sys</title>
<ellipse fill="none" stroke="black" cx="266.19" cy="-192" rx="70.69" ry="18"/>
<text text-anchor="middle" x="266.19" y="-188.3" font-family="Times,serif" font-size="14.00">Running_Sys</text>
<ellipse fill="none" stroke="black" cx="232.6" cy="-279" rx="70.69" ry="18"/>
<text text-anchor="middle" x="232.6" y="-275.3" font-family="Times,serif" font-size="14.00">Running_Sys</text>
</g>
<!-- Runnable&#45;&gt;Running_Sys -->
<g id="edge4" class="edge">
<title>Runnable&#45;&gt;Running_Sys</title>
<path fill="none" stroke="black" d="M266.19,-246.81C266.19,-238.79 266.19,-229.05 266.19,-220.07"/>
<polygon fill="black" stroke="black" points="269.69,-220.03 266.19,-210.03 262.69,-220.03 269.69,-220.03"/>
<path fill="none" stroke="black" d="M232.6,-333.81C232.6,-325.79 232.6,-316.05 232.6,-307.07"/>
<polygon fill="black" stroke="black" points="236.1,-307.03 232.6,-297.03 229.1,-307.03 236.1,-307.03"/>
</g>
<!-- Running_Sys&#45;&gt;Error -->
<g id="edge7" class="edge">
<g id="edge10" class="edge">
<title>Running_Sys&#45;&gt;Error</title>
<path fill="none" stroke="black" d="M202.68,-183.96C173.38,-178.94 138.83,-170.41 110.19,-156 96.75,-149.24 83.86,-138.69 73.66,-129.01"/>
<polygon fill="black" stroke="black" points="76,-126.4 66.42,-121.87 71.08,-131.38 76,-126.4"/>
<path fill="none" stroke="black" d="M278.15,-265.13C297.66,-259.14 320.53,-251.46 340.6,-243 362.39,-233.81 385.92,-221.45 404.1,-211.32"/>
<polygon fill="black" stroke="black" points="406.1,-214.21 413.09,-206.25 402.66,-208.11 406.1,-214.21"/>
</g>
<!-- Running_User -->
<g id="node6" class="node">
<title>Running_User</title>
<ellipse fill="none" stroke="black" cx="226.19" cy="-18" rx="76.89" ry="18"/>
<text text-anchor="middle" x="226.19" y="-14.3" font-family="Times,serif" font-size="14.00">Running_User</text>
<ellipse fill="none" stroke="black" cx="304.6" cy="-192" rx="76.89" ry="18"/>
<text text-anchor="middle" x="304.6" y="-188.3" font-family="Times,serif" font-size="14.00">Running_User</text>
</g>
<!-- Running_Sys&#45;&gt;Running_User -->
<g id="edge9" class="edge">
<g id="edge12" class="edge">
<title>Running_Sys&#45;&gt;Running_User</title>
<path fill="none" stroke="black" d="M256.46,-174C249.29,-160.59 239.98,-141.19 235.19,-123 228.52,-97.63 226.52,-67.69 226.04,-46.47"/>
<polygon fill="black" stroke="black" points="229.53,-46.19 225.91,-36.24 222.53,-46.28 229.53,-46.19"/>
<text text-anchor="middle" x="258.19" y="-101.3" font-family="Times,serif" font-size="14.00">return</text>
<path fill="none" stroke="black" d="M260.51,-262.41C268.39,-257.03 276.48,-250.45 282.6,-243 288.24,-236.12 292.71,-227.57 296.09,-219.49"/>
<polygon fill="black" stroke="black" points="299.41,-220.61 299.7,-210.02 292.87,-218.12 299.41,-220.61"/>
<text text-anchor="middle" x="313.6" y="-231.8" font-family="Times,serif" font-size="14.00">return</text>
</g>
<!-- Asleep -->
<g id="node7" class="node">
<g id="node9" class="node">
<title>Asleep</title>
<ellipse fill="none" stroke="black" cx="600.19" cy="-105" rx="41.69" ry="18"/>
<text text-anchor="middle" x="600.19" y="-101.3" font-family="Times,serif" font-size="14.00">Asleep</text>
<ellipse fill="none" stroke="black" cx="41.6" cy="-192" rx="41.69" ry="18"/>
<text text-anchor="middle" x="41.6" y="-188.3" font-family="Times,serif" font-size="14.00">Asleep</text>
</g>
<!-- Running_Sys&#45;&gt;Asleep -->
<g id="edge6" class="edge">
<g id="edge9" class="edge">
<title>Running_Sys&#45;&gt;Asleep</title>
<path fill="none" stroke="black" d="M318.7,-179.8C375.79,-167.3 469.52,-145.8 549.19,-123 552.38,-122.09 555.66,-121.1 558.95,-120.08"/>
<polygon fill="black" stroke="black" points="560.31,-123.32 568.77,-116.95 558.18,-116.65 560.31,-123.32"/>
<text text-anchor="middle" x="499.19" y="-144.8" font-family="Times,serif" font-size="14.00">sleep</text>
<path fill="none" stroke="black" d="M195.57,-263.48C180.32,-257.35 162.52,-250.03 146.6,-243 123.31,-232.73 97.5,-220.45 77.3,-210.63"/>
<polygon fill="black" stroke="black" points="78.68,-207.41 68.16,-206.17 75.61,-213.7 78.68,-207.41"/>
<text text-anchor="middle" x="165.6" y="-231.8" font-family="Times,serif" font-size="14.00">sleep</text>
</g>
<!-- Returned -->
<g id="node8" class="node">
<g id="node10" class="node">
<title>Returned</title>
<ellipse fill="none" stroke="black" cx="486.19" cy="-105" rx="53.89" ry="18"/>
<text text-anchor="middle" x="486.19" y="-101.3" font-family="Times,serif" font-size="14.00">Returned</text>
<ellipse fill="none" stroke="black" cx="155.6" cy="-192" rx="53.89" ry="18"/>
<text text-anchor="middle" x="155.6" y="-188.3" font-family="Times,serif" font-size="14.00">Returned</text>
</g>
<!-- Running_Sys&#45;&gt;Returned -->
<g id="edge8" class="edge">
<g id="edge11" class="edge">
<title>Running_Sys&#45;&gt;Returned</title>
<path fill="none" stroke="black" d="M312.74,-178.34C333.61,-172.25 358.38,-164.45 380.19,-156 403.62,-146.93 429.1,-134.98 449.21,-125.04"/>
<polygon fill="black" stroke="black" points="450.95,-128.09 458.33,-120.49 447.82,-121.82 450.95,-128.09"/>
<path fill="none" stroke="black" d="M217.38,-261.21C205.92,-248.55 190.04,-231.03 177.25,-216.91"/>
<polygon fill="black" stroke="black" points="179.82,-214.53 170.52,-209.47 174.64,-219.23 179.82,-214.53"/>
</g>
<!-- Running_User&#45;&gt;Running_Sys -->
<g id="edge6" class="edge">
<title>Running_User&#45;&gt;Running_Sys</title>
<path fill="none" stroke="black" d="M258.8,-206.54C248.87,-211.65 239.57,-218.61 233.6,-228 229.32,-234.72 227.98,-243.05 228.02,-250.97"/>
<polygon fill="black" stroke="black" points="224.54,-251.26 228.74,-260.98 231.52,-250.75 224.54,-251.26"/>
<text text-anchor="middle" x="258.1" y="-231.8" font-family="Times,serif" font-size="14.00">syscall</text>
</g>
<!-- Interrupted -->
<g id="node7" class="node">
<title>Interrupted</title>
<ellipse fill="none" stroke="black" cx="348.6" cy="-105" rx="64.99" ry="18"/>
<text text-anchor="middle" x="348.6" y="-101.3" font-family="Times,serif" font-size="14.00">Interrupted</text>
</g>
<!-- Running_User&#45;&gt;Interrupted -->
<g id="edge5" class="edge">
<title>Running_User&#45;&gt;Interrupted</title>
<path fill="none" stroke="black" d="M313.5,-173.8C319.74,-161.74 328.2,-145.4 335.25,-131.79"/>
<polygon fill="black" stroke="black" points="338.37,-133.38 339.86,-122.89 332.15,-130.16 338.37,-133.38"/>
<text text-anchor="middle" x="361.6" y="-144.8" font-family="Times,serif" font-size="14.00">interrupt</text>
</g>
<!-- Interrupted&#45;&gt;Running_User -->
<g id="edge8" class="edge">
<title>Interrupted&#45;&gt;Running_User</title>
<path fill="none" stroke="black" d="M333.3,-122.56C328.75,-128.12 324.05,-134.55 320.6,-141 316.76,-148.15 313.66,-156.37 311.25,-164.05"/>
<polygon fill="black" stroke="black" points="307.87,-163.13 308.46,-173.71 314.59,-165.07 307.87,-163.13"/>
</g>
<!-- Preempted -->
<g id="node9" class="node">
<g id="node8" class="node">
<title>Preempted</title>
<ellipse fill="none" stroke="black" cx="352.19" cy="-105" rx="61.99" ry="18"/>
<text text-anchor="middle" x="352.19" y="-101.3" font-family="Times,serif" font-size="14.00">Preempted</text>
<ellipse fill="none" stroke="black" cx="271.6" cy="-18" rx="61.99" ry="18"/>
<text text-anchor="middle" x="271.6" y="-14.3" font-family="Times,serif" font-size="14.00">Preempted</text>
</g>
<!-- Running_Sys&#45;&gt;Preempted -->
<g id="edge10" class="edge">
<title>Running_Sys&#45;&gt;Preempted</title>
<path fill="none" stroke="black" d="M283.18,-174.21C296.11,-161.43 314.05,-143.7 328.4,-129.52"/>
<polygon fill="black" stroke="black" points="330.88,-131.99 335.53,-122.47 325.96,-127.01 330.88,-131.99"/>
<text text-anchor="middle" x="345.69" y="-144.8" font-family="Times,serif" font-size="14.00">preempt</text>
<!-- Interrupted&#45;&gt;Preempted -->
<g id="edge7" class="edge">
<title>Interrupted&#45;&gt;Preempted</title>
<path fill="none" stroke="black" d="M333.38,-87.21C321.92,-74.55 306.04,-57.03 293.25,-42.91"/>
<polygon fill="black" stroke="black" points="295.82,-40.53 286.52,-35.47 290.64,-45.23 295.82,-40.53"/>
<text text-anchor="middle" x="345.1" y="-57.8" font-family="Times,serif" font-size="14.00">preempt</text>
</g>
<!-- Running_User&#45;&gt;Running_Sys -->
<g id="edge5" class="edge">
<title>Running_User&#45;&gt;Running_Sys</title>
<path fill="none" stroke="black" d="M196.06,-34.66C163.08,-53.98 117.48,-88.54 138.19,-123 153.49,-148.45 181.89,-164.89 208.05,-175.21"/>
<polygon fill="black" stroke="black" points="206.97,-178.54 217.56,-178.74 209.4,-171.98 206.97,-178.54"/>
<text text-anchor="middle" x="171.19" y="-101.3" font-family="Times,serif" font-size="14.00">interrupt</text>
<!-- Preempted&#45;&gt;Running_User -->
<g id="edge13" class="edge">
<title>Preempted&#45;&gt;Running_User</title>
<path fill="none" stroke="black" d="M270.09,-36.18C268.73,-57.01 267.83,-93.05 274.6,-123 277.85,-137.4 284.19,-152.51 290.2,-164.82"/>
<polygon fill="black" stroke="black" points="287.23,-166.71 294.89,-174.03 293.47,-163.53 287.23,-166.71"/>
</g>
<!-- Asleep&#45;&gt;Runnable -->
<g id="edge13" class="edge">
<g id="edge15" class="edge">
<title>Asleep&#45;&gt;Runnable</title>
<path fill="none" stroke="black" d="M579.26,-120.81C563.89,-131.34 542.26,-145.43 522.19,-156 450.82,-193.58 363.46,-228.15 311.45,-247.6"/>
<polygon fill="black" stroke="black" points="310,-244.41 301.85,-251.17 312.44,-250.97 310,-244.41"/>
<text text-anchor="middle" x="512.69" y="-188.3" font-family="Times,serif" font-size="14.00">wakeup</text>
<path fill="none" stroke="black" d="M46.7,-210.08C54.3,-232.82 70.67,-272.97 97.6,-297 120.32,-317.28 151.62,-330.45 178.36,-338.69"/>
<polygon fill="black" stroke="black" points="177.4,-342.06 187.98,-341.51 179.37,-335.34 177.4,-342.06"/>
<text text-anchor="middle" x="125.1" y="-275.3" font-family="Times,serif" font-size="14.00">wakeup</text>
</g>
<!-- Complete -->
<g id="node10" class="node">
<g id="node11" class="node">
<title>Complete</title>
<ellipse fill="none" stroke="black" cx="486.19" cy="-18" rx="55.49" ry="18"/>
<text text-anchor="middle" x="486.19" y="-14.3" font-family="Times,serif" font-size="14.00">Complete</text>
<ellipse fill="none" stroke="black" cx="151.6" cy="-105" rx="55.49" ry="18"/>
<text text-anchor="middle" x="151.6" y="-101.3" font-family="Times,serif" font-size="14.00">Complete</text>
</g>
<!-- Returned&#45;&gt;Complete -->
<g id="edge12" class="edge">
<g id="edge14" class="edge">
<title>Returned&#45;&gt;Complete</title>
<path fill="none" stroke="black" d="M486.19,-86.8C486.19,-75.16 486.19,-59.55 486.19,-46.24"/>
<polygon fill="black" stroke="black" points="489.69,-46.18 486.19,-36.18 482.69,-46.18 489.69,-46.18"/>
<text text-anchor="middle" x="530.69" y="-57.8" font-family="Times,serif" font-size="14.00">exit_success</text>
</g>
<!-- Preempted&#45;&gt;Running_User -->
<g id="edge11" class="edge">
<title>Preempted&#45;&gt;Running_User</title>
<path fill="none" stroke="black" d="M328.79,-88.21C309.06,-74.9 280.62,-55.72 258.69,-40.92"/>
<polygon fill="black" stroke="black" points="260.58,-37.97 250.33,-35.28 256.66,-43.78 260.58,-37.97"/>
<path fill="none" stroke="black" d="M153.91,-173.93C153.4,-168.23 152.91,-161.85 152.6,-156 152.21,-148.66 151.97,-140.71 151.82,-133.35"/>
<polygon fill="black" stroke="black" points="155.32,-133.08 151.66,-123.13 148.32,-133.19 155.32,-133.08"/>
<text text-anchor="middle" x="197.1" y="-144.8" font-family="Times,serif" font-size="14.00">exit_success</text>
</g>
</g>
</svg>

Before

Width:  |  Height:  |  Size: 8.1 KiB

After

Width:  |  Height:  |  Size: 9.2 KiB

@ -1,3 +1,4 @@
SLEDGE_SCHEDULER=EDF
SLEDGE_DISABLE_PREEMPTION=false
SLEDGE_SIGALRM_HANDLER=TRIAGED
SLEDGE_NWORKERS=4

@ -59,12 +59,6 @@ sandbox_get_priority(void *element)
return sandbox->absolute_deadline;
};
static inline bool
sandbox_is_preemptable(struct sandbox *sandbox)
{
return sandbox && sandbox->state == SANDBOX_RUNNING_USER;
};
static inline void
sandbox_open_http(struct sandbox *sandbox)
{

@ -36,15 +36,16 @@ sandbox_perf_log_print_entry(struct sandbox *sandbox)
* becomes more intelligent, then peak linear memory size needs to be tracked
* seperately from current linear memory size.
*/
fprintf(sandbox_perf_log, "%lu,%s,%d,%s,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%u,%u\n",
fprintf(sandbox_perf_log, "%lu,%s,%d,%s,%lu,%lu,%lu,,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%u,%u\n",
sandbox->id, sandbox->module->name, sandbox->module->port, sandbox_state_stringify(sandbox->state),
sandbox->module->relative_deadline, sandbox->total_time, queued_duration,
sandbox->duration_of_state[SANDBOX_UNINITIALIZED], sandbox->duration_of_state[SANDBOX_ALLOCATED],
sandbox->duration_of_state[SANDBOX_INITIALIZED], sandbox->duration_of_state[SANDBOX_RUNNABLE],
sandbox->duration_of_state[SANDBOX_PREEMPTED], sandbox->duration_of_state[SANDBOX_RUNNING_SYS],
sandbox->duration_of_state[SANDBOX_RUNNING_USER], sandbox->duration_of_state[SANDBOX_ASLEEP],
sandbox->duration_of_state[SANDBOX_RETURNED], sandbox->duration_of_state[SANDBOX_COMPLETE],
sandbox->duration_of_state[SANDBOX_ERROR], runtime_processor_speed_MHz, sandbox->memory.size);
sandbox->duration_of_state[SANDBOX_INTERRUPTED], sandbox->duration_of_state[SANDBOX_PREEMPTED],
sandbox->duration_of_state[SANDBOX_RUNNING_SYS], sandbox->duration_of_state[SANDBOX_RUNNING_USER],
sandbox->duration_of_state[SANDBOX_ASLEEP], sandbox->duration_of_state[SANDBOX_RETURNED],
sandbox->duration_of_state[SANDBOX_COMPLETE], sandbox->duration_of_state[SANDBOX_ERROR],
runtime_processor_speed_MHz, sandbox->memory.size);
}
static inline void

@ -0,0 +1,62 @@
#pragma once
#include <assert.h>
#include <stdint.h>
#include "arch/getcycles.h"
#include "current_sandbox.h"
#include "panic.h"
#include "sandbox_functions.h"
#include "sandbox_state_history.h"
#include "sandbox_types.h"
static inline void
sandbox_set_as_interrupted(struct sandbox *sandbox, sandbox_state_t last_state)
{
assert(sandbox);
/* WARNING: All code before this assignment is preemptable */
sandbox->state = SANDBOX_INTERRUPTED;
barrier();
uint64_t now = __getcycles();
/* State Change Bookkeeping */
sandbox->duration_of_state[last_state] += (now - sandbox->timestamp_of.last_state_change);
sandbox->timestamp_of.last_state_change = now;
/* We do not append SANDBOX_INTERRUPTED to the sandbox_state_history because it would quickly fill the buffer */
runtime_sandbox_total_increment(SANDBOX_INTERRUPTED);
runtime_sandbox_total_decrement(last_state);
}
static inline void
sandbox_interrupt(struct sandbox *sandbox)
{
sandbox_set_as_interrupted(sandbox, sandbox->state);
}
/**
* @brief Transition sandbox back to interrupted state
* @param sandbox
* @param interrupted_state - state to return to
*/
static inline void
sandbox_interrupt_return(struct sandbox *sandbox, sandbox_state_t interrupted_state)
{
assert(sandbox);
assert(interrupted_state != SANDBOX_INTERRUPTED);
uint64_t now = __getcycles();
/* State Change Bookkeeping */
sandbox->duration_of_state[SANDBOX_INTERRUPTED] += (now - sandbox->timestamp_of.last_state_change);
sandbox->timestamp_of.last_state_change = now;
/* We do not append SANDBOX_INTERRUPTED to the sandbox_state_history because it would quickly fill the buffer */
runtime_sandbox_total_increment(interrupted_state);
runtime_sandbox_total_decrement(SANDBOX_INTERRUPTED);
barrier();
/* WARNING: Code after this assignment may be preemptable */
sandbox->state = interrupted_state;
}

@ -26,7 +26,7 @@ sandbox_set_as_preempted(struct sandbox *sandbox, sandbox_state_t last_state)
uint64_t now = __getcycles();
switch (last_state) {
case SANDBOX_RUNNING_SYS: {
case SANDBOX_INTERRUPTED: {
break;
}
default: {
@ -46,6 +46,6 @@ sandbox_set_as_preempted(struct sandbox *sandbox, sandbox_state_t last_state)
static inline void
sandbox_preempt(struct sandbox *sandbox)
{
assert(sandbox->state == SANDBOX_RUNNING_SYS);
sandbox_set_as_preempted(sandbox, SANDBOX_RUNNING_SYS);
assert(sandbox->state == SANDBOX_INTERRUPTED);
sandbox_set_as_preempted(sandbox, SANDBOX_INTERRUPTED);
}

@ -29,12 +29,10 @@ sandbox_set_as_running_sys(struct sandbox *sandbox, sandbox_state_t last_state)
}
case SANDBOX_RUNNABLE: {
assert(sandbox);
/* 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,
panic("Sandbox %lu | Illegal transition from %s to Running Sys\n", sandbox->id,
sandbox_state_stringify(last_state));
}
}
@ -48,7 +46,7 @@ sandbox_set_as_running_sys(struct sandbox *sandbox, sandbox_state_t last_state)
}
static inline void
sandbox_interrupt(struct sandbox *sandbox)
sandbox_syscall(struct sandbox *sandbox)
{
assert(sandbox->state == SANDBOX_RUNNING_USER);
sandbox_set_as_running_sys(sandbox, SANDBOX_RUNNING_USER);

@ -24,7 +24,6 @@ sandbox_set_as_running_user(struct sandbox *sandbox, sandbox_state_t last_state)
break;
}
case SANDBOX_PREEMPTED: {
assert(sandbox);
break;
}
default: {

@ -15,6 +15,7 @@ typedef enum
SANDBOX_PREEMPTED,
SANDBOX_RUNNING_SYS,
SANDBOX_RUNNING_USER,
SANDBOX_INTERRUPTED,
SANDBOX_ASLEEP,
SANDBOX_RETURNED,
SANDBOX_COMPLETE,

@ -19,6 +19,7 @@
#include "sandbox_set_as_preempted.h"
#include "sandbox_set_as_runnable.h"
#include "sandbox_set_as_running_sys.h"
#include "sandbox_set_as_interrupted.h"
#include "sandbox_set_as_running_user.h"
#include "scheduler_execute_epoll_loop.h"
@ -214,9 +215,7 @@ scheduler_preemptive_sched(ucontext_t *interrupted_context)
struct sandbox *current = current_sandbox_get();
assert(current != NULL);
assert(current->state == SANDBOX_RUNNING_USER);
sandbox_interrupt(current);
assert(current->state == SANDBOX_INTERRUPTED);
struct sandbox *next = scheduler_get_next();
/* Assumption: the current sandbox is on the runqueue, so the scheduler should always return something */
@ -224,7 +223,7 @@ scheduler_preemptive_sched(ucontext_t *interrupted_context)
/* If current equals next, no switch is necessary, so resume execution */
if (current == next) {
sandbox_return(current);
sandbox_interrupt_return(current, SANDBOX_RUNNING_USER);
return;
}

@ -35,7 +35,7 @@ current_sandbox_sleep()
{
struct sandbox *sandbox = current_sandbox_get();
current_sandbox_set(NULL);
assert(sandbox != NULL);
struct arch_context *current_context = &sandbox->ctxt;
@ -139,7 +139,7 @@ current_sandbox_fini()
assert(sandbox != NULL);
char *error_message = "";
sandbox_interrupt(sandbox);
sandbox_syscall(sandbox);
sandbox->timestamp_of.completion = __getcycles();

@ -13,6 +13,7 @@ const char *sandbox_state_labels[SANDBOX_STATE_COUNT] = {
[SANDBOX_ALLOCATED] = "Allocated",
[SANDBOX_INITIALIZED] = "Initialized",
[SANDBOX_RUNNABLE] = "Runnable",
[SANDBOX_INTERRUPTED] = "Interrupted",
[SANDBOX_PREEMPTED] = "Preempted",
[SANDBOX_RUNNING_SYS] = "Running Sys",
[SANDBOX_RUNNING_USER] = "Running User",

@ -19,6 +19,7 @@
#include "panic.h"
#include "runtime.h"
#include "sandbox_set_as_running_user.h"
#include "sandbox_set_as_interrupted.h"
#include "sandbox_types.h"
#include "scheduler.h"
#include "software_interrupt.h"
@ -43,12 +44,6 @@ worker_thread_is_running_cooperative_scheduler(void)
return current_sandbox_get() == NULL;
}
static inline void
defer_sigalrm()
{
atomic_fetch_add(&deferred_sigalrm, 1);
}
/**
* A POSIX signal is delivered to only one thread.
* This function broadcasts the sigalarm signal to all other worker threads
@ -112,14 +107,21 @@ software_interrupt_handle_signals(int signal_type, siginfo_t *signal_info, void
switch (signal_type) {
case SIGALRM: {
propagate_sigalrm(signal_info);
if (worker_thread_is_running_cooperative_scheduler()) {
propagate_sigalrm(signal_info);
break;
}
if (worker_thread_is_running_cooperative_scheduler()) break;
sandbox_state_t interrupted_state = current_sandbox->state;
sandbox_interrupt(current_sandbox);
propagate_sigalrm(signal_info);
if (sandbox_is_preemptable(current_sandbox)) {
if (interrupted_state == SANDBOX_RUNNING_USER) {
/* Preemptable, so run scheduler. The scheduler handles outgoing state changes */
scheduler_preemptive_sched(interrupted_context);
} else {
defer_sigalrm();
atomic_fetch_add(&deferred_sigalrm, 1);
sandbox_interrupt_return(current_sandbox, interrupted_state);
}
break;

Loading…
Cancel
Save