green
[phd-thesis.git] / top / imp.tex
index d33198b..6dbd564 100644 (file)
 
 \input{subfilepreamble}
 
+\setcounter{chapter}{4}
+
 \begin{document}
 \input{subfileprefix}
-\chapter{Implementation}%
+\chapter{The implementation of \texorpdfstring{\gls{MTASK}}{mTask}}%
 \label{chp:implementation}
 \begin{chapterabstract}
-       This chapter shows the implementation of the \gls{MTASK} system.
-       It is threefold: first it shows the implementation of the byte code compiler for \gls{MTASK}'s \gls{TOP} language, then is details of the implementation of \gls{MTASK}'s \gls{TOP} engine that executes the \gls{MTASK} tasks on the microcontroller, and finally it shows how the integration of \gls{MTASK} tasks and \glspl{SDS} is implemented both on the server and on the device.
+       This chapter shows the implementation of the \gls{MTASK} system by:
+       \begin{itemize}
+               \item giving details of the implementation of \gls{MTASK}'s \gls{TOP} engine that executes the \gls{MTASK} tasks on the microcontroller;
+               \item showing the implementation of the byte code compiler for \gls{MTASK}'s \gls{TOP} language;
+               \item explaining the machinery used to automatically serialise and deserialise data to-and-fro the device.
+       \end{itemize}
 \end{chapterabstract}
 
-Microcontrollers usually have flash-based program memory which wears out fairly quick.
-For example, the atmega328p in the \gls{ARDUINO} UNO is rated for 10000 write cycles.
-While this sounds like a lot, if new tasks are sent to the device every minute or so, a lifetime of not even seven days is guaranteed.
-Hence, for dynamic applications, generating code at run-time for interpretation on the device is necessary.
-This byte code is then interpreted on MCUs with very little memory and processing power and thus save precious write cycles of the program memory.
-precious write cycles of the program memory.
+The \gls{MTASK} language targets resource-constrained edge devices that have little memory, processor speed and communication.
+Furthermore, microcontrollers usually have flash-based program memory which wears out fairly quick.
+For example, the flash memory of the popular atmega328p powering the \gls{ARDUINO} UNO is just rated for 10000 write cycles.
+While this sounds like a lot, if new tasks are sent to the device every minute or so, a lifetime of only seven days is guaranteed.
+Hence, for dynamic applications, storing the program in the \gls{RAM} of the device and interpreting this code is necessary, saving precious write cycles of the program memory.
+
+In the \gls{MTASK} system, this is done by the \gls{MTASK} \gls{RTS}.
+The \gls{RTS} is a customisable domain-specific \gls{OS} that takes care of the execution of tasks, but also low-level mechanisms such as the communication, multitasking, and memory management.
+Once a device is programmed with the \gls{MTASK} \gls{RTS}, it can continuously receive new tasks without the need for reprogramming.
+The \gls{OS} is written in portable \ccpp{} and only contains a small device-specific portion.
+In order to keep the abstraction level high and the hardware requirements low, much of the high-level functionality of the \gls{MTASK} language is implemented not in terms of lower-level constructs from \gls{MTASK} language but in terms of \ccpp{} code.
+
+\section{\texorpdfstring{\Glsxtrlong{RTS}}{Run time system}}
+The event loop of the \gls{RTS} is executed repeatedly and consists of three distinct phases.
+After doing the three phases, the devices goes to sleep for as long as possible (see \cref{chp:green_computing_mtask}).
+
+\subsection{Communication}
+In the first phase, the communication channels are processed.
+The exact communication method is a customisable device-specific option baked into the \gls{RTS}.
+The interface is deliberately kept simple and consists of a two layer interface: a link interface and a communication interface.
+Besides opening, closing and cleaning up, the link interface has only three functions that are shown in \cref{lst:link_interface}.
+Consequently, implementing this link interface is very simple but allows for many more advanced link settings such as buffering.
+There are implementations for this interface for serial or \gls{WIFI} connections using \gls{ARDUINO} and \gls{TCP} connections for Linux.
+
+\begin{lstArduino}[caption={Link interface of the \gls{MTASK} \gls{RTS}.},label={lst:link_interface}]
+bool link_input_available(void);
+uint8_t link_read_byte(void);
+void link_write_byte(uint8_t b);
+\end{lstArduino}
+
+The communication interface abstracts away from this link interface and is typed instead.
+It contains only two functions as seen in \cref{lst:comm_interface}.
+There are implementations for direct communication, or communication using an \gls{MQTT} broker.
+Both use the automatic serialisation and deserialisation shown in \cref{sec:ccodegen}.
+
+\begin{lstArduino}[caption={Communication interface of the \gls{MTASK} \gls{RTS}.},label={lst:comm_interface}]
+struct MTMessageTo receive_message(void);
+void send_message(struct MTMessageFro msg);
+\end{lstArduino}
+
+Processing the received messages from the communication channels happens synchronously and the channels are exhausted completely before moving on to the next phase.
+There are several possible messages that can be received from the server:
+
+\begin{description}
+       \item[SpecRequest]
+               is a message instructing the device to send its specification and is sent usually immediately after connecting.
+               The \gls{RTS} responds with a \texttt{Spec} answer containing the specification.
+       \item[TaskPrep]
+               tells the device a (big) task is on its way.
+               Especially on faster connections, it may be the case that the communication buffers overflow because a big message is sent while the \gls{RTS} is busy executing tasks.
+               This allows the \gls{RTS} to postpone execution for a while, until the big task has been received.
+               The server sends the big task when the device acknowledges (by sending a \texttt{TaskPrepAck} message) the preparation.
+       \item[Task]
+               contains a new task, its peripheral configuration, the \glspl{SDS}, and the bytecode.
+               The new task is immediately copied to the task storage but is only initialised during the next phase after which a \texttt{TaskAck} is sent.
+               Tasks are stored below the stack, but since the stack is only used in the middle phase, execution, it is no problem that it moves.
+       \item[SdsUpdate]
+               notifies the device of the new value for a lowered \gls{SDS}.
+               The old value of the lowered \gls{SDS} is immediately replaced with the new one.
+               There is no acknowledgement required.
+       \item[TaskDel]
+               instructs the device to delete a running task.
+               Tasks are automatically deleted when they become stable.
+               However, a task may also be deleted when the surrounding task on the server is deleted, for example when the task is on the left-hand side of a step combinator and the condition to step holds.
+               The device acknowledges by sending a \texttt{TaskDelAck}.
+       \item[Shutdown]
+               tells the device to reset.
+\end{description}
+
+\subsection{Execution}
+The second phase consists of performing one execution step for all tasks that wish for it.
+Tasks are ordered in a priority queue ordered by the time a task needs to be executed, the \gls{RTS} selects all tasks that can be scheduled, see \cref{sec:scheduling} for more details.
+Execution of a task is always an interplay between the interpreter and the \emph{rewriter}.
+
+If a task is not initialized yet, i.e.\ the pointer to the current task tree is still null, the byte code of the main function is interpreted.
+The main expression always produces a task tree.
+Execution of a task consists of continuously rewriting the task until its value is stable.
+Rewriting is a destructive process, i.e.\ the rewriting is done in place.
+The rewriting engine uses the interpreter when needed, e.g.\ to calculate the step continuations.
+The rewriter and the interpreter use the same stack to store intermediate values.
+Rewriting steps are small so that interleaving results in seemingly parallel execution.
+In this phase new task tree nodes may be allocated.
+Both rewriting and initialization are atomic operations in the sense that no processing on SDSs is done other than SDS operations from the task itself.
+The host is notified if a task value is changed after a rewrite step.
+
+\subsection{Memory management}
+The third and final phase is memory management.
+The \gls{MTASK} \gls{RTS} is designed to run on systems with as little as \qty{2}{\kibi\byte} of \gls{RAM}.
+Aggressive memory management is therefore vital.
+Not all firmwares for microprocessors support heaps and---when they do---allocation often leaves holes when not used in a \emph{last in first out} strategy.
+The \gls{RTS} uses a chunk of memory in the global data segment with its own memory manager tailored to the needs of \gls{MTASK}.
+The size of this block can be changed in the configuration of the \gls{RTS} if necessary.
+On an \gls{ARDUINO} UNO---equipped with \qty{2}{\kibi\byte} of \gls{RAM}---the maximum viable size is about \qty{1500}{\byte}.
+The self-managed memory uses a similar layout as the memory layout for \gls{C} programs only the heap and the stack are switched (see \cref{fig:memory_layout}).
+
+\begin{figure}
+       \centering
+       \includestandalone{memorylayout}
+       \caption{Memory layout}\label{fig:memory_layout}
+\end{figure}
+
+A task is stored below the stack and its complete state is a \gls{CLEAN} record contain most importantly the task id, a pointer to the task tree in the heap (null if not initialised yet), the current task value, the configuration of \glspl{SDS}, the configuration of peripherals, the byte code and some scheduling information.
+
+In memory, task data grows from the bottom up and the interpreter stack is located directly on top of it growing in the same direction.
+As a consequence, the stack moves when a new task is received.
+This never happens within execution because communication is always processed before execution.
+Values in the interpreter are always stored on the stack.
+Compound data types are stored unboxed and flattened.
+Task trees grow from the top down as in a heap.
+This approach allows for flexible ratios, i.e.\ many tasks and small trees or few tasks and big trees.
+
+Stable tasks, and unreachable task tree nodes are removed.
+If a task is to be removed, tasks with higher memory addresses are moved down.
+For task trees---stored in the heap---the \gls{RTS} already marks tasks and task trees as trash during rewriting so the heap can be compacted in a single pass.
+This is possible because there is no sharing or cycles in task trees and nodes contain pointers pointers to their parent.
 
-In order to provide the device with the tools to interpret the byte code, it is programmed with a \gls{RTS}, a customisable domain-specific \gls{OS} that takes care of the execution of tasks but also low-level mechanisms such as the communication, multi tasking, and memory management.
-Once the device is programmed with the \gls{MTASK} \gls{RTS}, it can continuously receive new tasks.
+\todo{plaa\-tje van me\-mo\-ry hier uitbreiden}
 
+\section{Compiler}
 \subsection{Instruction set}
-The instruction set is a fairly standard stack machine instruction set extended with special \gls{TOP} instructions.
-\Cref{lst:instruction_type} shows the \gls{CLEAN} type representing the instruction set of which \cref{tbl:instr_task} gives detailed semantics.
-Type synonyms are used to provide insight on the arguments of the instructions.
-One notable instruction is the \cleaninline{MkTask} instruction, it allocates and initialises a task tree node and pushes a pointer to it on the stack.
+The instruction set is a fairly standard stack machine instruction set extended with special \gls{TOP} instructions for creating task tree nodes.
+All instructions are housed in a \gls{CLEAN} \gls{ADT} and serialised to the byte representation using a generic function.
+Type synonyms (\cref{lst:type_synonyms}) are used to provide insight on the arguments of the instructions.
+Labels are always two bytes long, all other arguments are one byte long.
 
-\begin{lstClean}[caption={The type housing the instruction set.},label={lst:instruction_type}]
+\begin{lstClean}[caption={Type synonyms for instructions arguments.},label={lst:type_synonyms}]
 :: ArgWidth    :== UInt8         :: ReturnWidth :== UInt8
 :: Depth       :== UInt8         :: Num         :== UInt8
 :: SdsId       :== UInt8         :: JumpLabel   =: JL UInt16
+\end{lstClean}
+
+\Cref{lst:instruction_type} shows an excerpt of the \gls{CLEAN} type that represents the instruction set.
+For example, shorthand instructions are omitted for brevity.
+Detailed semantics for the instructions are given in \cref{chp:bytecode_instruction_set}.
+One notable instruction is the \cleaninline{MkTask} instruction, it allocates and initialises a task tree node and pushes a pointer to it on the stack.
 
-//** Datatype housing all instructions
+\begin{lstClean}[caption={The type housing the instruction set.},label={lst:instruction_type}]
 :: BCInstr
-       //Return instructions
        //Jumps
        = BCJumpF JumpLabel | BCJump JumpLabel | BCLabel JumpLabel | BCJumpSR ArgWidth JumpLabel
        | BCReturn ReturnWidth ArgWidth | BCTailcall ArgWidth ArgWidth JumpLabel
@@ -42,8 +162,6 @@ One notable instruction is the \cleaninline{MkTask} instruction, it allocates an
        | BCArgs ArgWidth ArgWidth
        //Task node creation and refinement
        | BCMkTask BCTaskType | BCTuneRateMs | BCTuneRateSec
-       //Task value ops
-       | BCIsStable | BCIsUnstable | BCIsNoValue | BCIsValue
        //Stack ops
        | BCPush String255 | BCPop Num | BCRot Depth Num | BCDup | BCPushPtrs
        //Casting
@@ -52,9 +170,8 @@ One notable instruction is the \cleaninline{MkTask} instruction, it allocates an
        | BCAddI | BCSubI | ...
        ...
 
-//** Datatype housing all task types
 :: BCTaskType
-       = BCStableNode ArgWidth | ArgWidth
+       = BCStableNode ArgWidth | BCUnstableNode ArgWidth
        // Pin io
        | BCReadD | BCWriteD | BCReadA | BCWriteA | BCPinMode
        // Interrupts
@@ -62,7 +179,7 @@ One notable instruction is the \cleaninline{MkTask} instruction, it allocates an
        // Repeat
        | BCRepeat
        // Delay
-       | BCDelay | BCDelayUntil //* Only for internal use
+       | BCDelay | BCDelayUntil
        // Parallel
        | BCTAnd | BCTOr
        //Step
@@ -77,20 +194,20 @@ One notable instruction is the \cleaninline{MkTask} instruction, it allocates an
        ...
 \end{lstClean}
 
-\subsection{Compiler}
+\subsection{Compiler infrastructure}
 The bytecode compiler interpretation for the \gls{MTASK} language is implemented as a monad stack containing a writer monad and a state monad.
 The writer monad is used to generate code snippets locally without having to store them in the monadic values.
 The state monad accumulates the code, and stores the stateful data the compiler requires.
 \Cref{lst:compiler_state} shows the data type for the state, storing:
 function the compiler currently is in;
 code of the main expression;
-context (see \todo{insert ref to compilation rules step here});
+context (see \cref{ssec:step});
 code for the functions;
 next fresh label;
 a list of all the used \glspl{SDS}, either local \glspl{SDS} containing the initial value (\cleaninline{Left}) or lifted \glspl{SDS} (see \cref{sec:liftsds}) containing a reference to the associated \gls{ITASK} \gls{SDS};
 and finally there is a list of peripherals used.
 
-\begin{lstClean}[label={lst:compiler_state},caption={\Gls{MTASK}'s byte code compiler type}]
+\begin{lstClean}[label={lst:compiler_state},caption={The type for the \gls{MTASK} byte code compiler}]
 :: BCInterpret a :== StateT BCState (WriterT [BCInstr] Identity) a
 :: BCState =
        { bcs_infun        :: JumpLabel
@@ -117,7 +234,7 @@ For example, the \cleaninline{BCArg} instruction is often called with argument \
 Furthermore, redundant instructions (e.g.\ pop directly after push) are removed as well in order not to burden the code generation with these intricacies.
 Finally the labels are resolved to represent actual program addresses instead of freshly generated identifiers.
 After the byte code is ready, the lifted \glspl{SDS} are resolved to provide an initial value for them.
-The result---byte code, \gls{SDS} specification and perpipheral specifications---are the result of the process, ready to be sent to the device. 
+The result---byte code, \gls{SDS} specification and perpipheral specifications---are the result of the process, ready to be sent to the device.
 
 \section{Compilation rules}
 This section describes the compilation rules, the translation from abstract syntax to byte code.
@@ -200,7 +317,7 @@ instance expr BCInterpret where
                e >>| tell` [BCLabel endiflabel]
 \end{lstClean}
 
-\subsection{Functions}
+\subsection{Functions}\label{ssec:functions}
 Compiling functions occurs in $\mathcal{F}$, which generates bytecode for the complete program by iterating over the functions and ending with the main expression.
 When compiling the body of the function, the arguments of the function are added to the context so that the addresses can be determined when referencing arguments.
 The main expression is a special case of $\mathcal{F}$ since it neither has arguments nor something to continue.
@@ -289,7 +406,7 @@ callFunction :: JumpLabel UInt8 [BCInterpret b] -> BCInterpret c | ...
 liftFunction :: JumpLabel UInt8 (BCInterpret a) (?UInt8) -> BCInterpret ()
 \end{lstClean}
 
-\subsection{Tasks}
+\subsection{Tasks}\label{ssec:scheme_tasks}
 Task trees are created with the \cleaninline{BCMkTask} instruction that allocates a node and pushes it to the stack.
 It pops arguments from the stack according to the given task type.
 The following extension of $\mathcal{E}$ shows this compilation scheme (except for the step combinator, explained in \cref{ssec:step}).
@@ -339,7 +456,9 @@ where
        rtrn m = m >>| tell` [BCMkTask (bcstable m)]
 \end{lstClean}
 
-\subsection{Step combinator}\label{ssec:step}
+\todo[inline]{uitleg delay}
+
+\subsection{Sequential combinator}\label{ssec:step}
 The \cleaninline{step} construct is a special type of task because the task value of the left-hand side may change over time.
 Therefore, the continuation tasks on the right-hand side are \emph{observing} this task value and acting upon it.
 In the compilation scheme, all continuations are first converted to a single function that has two arguments: the stability of the task and its value.
@@ -457,11 +576,7 @@ This initial value is stored as a byte code encoded value in the state and the c
 
 Compiling \cleaninline{getSds} is a matter of executing the \cleaninline{BCInterpret} representing the \gls{SDS}, which yields the identifier that can be embedded in the instruction.
 Setting the \gls{SDS} is similar: the identifier is retrieved and the value is written to put in a task tree so that the resulting task can remember the value it has written.
-Lifted SDSs are compiled in a very similar way.\todo{deze \P{} moet naar integration?}
-The only difference is that there is no initial value but an iTasks SDS when executing the Clean function.
-A lens on this SDS converting \cleaninline{a} from the \cleaninline{Shared a} to a \cleaninline{String255}---a bytecode encoded version---is stored in the state.
-The encoding and decoding itself is unsafe when used directly but the type system of the language and the abstractions make it safe.
-Upon sending the mTask task to the device, the initial values of the lifted SDSs are fetched to complete the SDS specification.
+Lifted SDSs are compiled in a very similar way \cref{sec:liftsds}.
 
 % VimTeX: SynIgnore on
 \begin{lstClean}[caption={Backend implementation for the SDS classes.},label={lst:comp_sds}]
@@ -480,49 +595,16 @@ instance sds BCInterpret where
 \end{lstClean}
 % VimTeX: SynIgnore off
 
+\section{\texorpdfstring{\Gls{C}}{C} code generation}\label{sec:ccodegen}
+All communication between the \gls{ITASK} server and the \gls{MTASK} server is type-parametrised.
+From the structural representation of the type, a \gls{CLEAN} parser and printer is constructed using generic programming.
+Furthermore, a \ccpp{} parser and printer is generated for use on the \gls{MTASK} device.
+The technique for generating the \ccpp{} parser and printer is very similar to template metaprogramming and requires a generic programming library or compiler support that includes a lot of metadata in the record and constructor nodes.
 
-\section{\texorpdfstring{\Gls{RTS}}{Run time system}}
-
-The \gls{RTS} is designed to run on systems with as little as \qty{2}{\kibi\byte} of \gls{RAM}.
-Aggressive memory management is therefore vital.
-Not all firmwares for microprocessors support heaps and---when they do---allocation often leaves holes when not used in a \emph{last in first} out strategy.
-Therefore the \gls{RTS} uses a chunk of memory in the global data segment with its own memory manager tailored to the needs of \gls{MTASK}.
-The size of this block can be changed in the configuration of the \gls{RTS} if necessary.
-On an \gls{ARDUINO} UNO ---equipped with \qty{2}{\kibi\byte} of \gls{RAM}--- this size is about \qty{1500}{\byte}.
-
-In memory, task data grows from the bottom up and an interpreter stack is located directly on top of it growing in the same direction.
-As a consequence, the stack moves when a new task is received.
-This never happens within execution because communication is always processed before execution.
-Values in the interpreter are always stored on the stack, even tuples.
-Task trees grow from the top down as in a heap.
-This approach allows for flexible ratios, i.e.\ many tasks and small trees or few tasks and big trees.
-
-The event loop of the \gls{RTS} is executed repeatedly and consists of three distinct phases.
-\todo{plaa\-tje van me\-mo\-ry hier}
-\todo{pseu\-do\-code hier van de ex\-e\-cu\-tie}
-
-%TODO evt subsubsections verwijderen
-\subsection{Communication}
-In the first phase, the communication channels are processed.
-The messages announcing \gls{SDS} updates are applied immediately, the initialization of new tasks is delayed until the next phase.
+\section{Conclusion}
+Tasks in the \gls{MTASK} system are executed on resource-constrained \gls{IOT} edge devices.
 
-\subsection{Execution}
-The second phase consists of executing tasks.
-The \gls{RTS} executes all tasks in a round robin fashion.
-If a task is not initialized yet, the bytecode of the main function is interpreted to produce the initial task tree.
-The rewriting engine uses the interpreter when needed, e.g.\ to calculate the step continuations.
-The rewriter and the interpreter use the same stack to store intermediate values.
-Rewriting steps are small so that interleaving results in seemingly parallel execution.
-In this phase new task tree nodes may be allocated.
-Both rewriting and initialization are atomic operations in the sense that no processing on SDSs is done other than SDS operations from the task itself.
-The host is notified if a task value is changed after a rewrite step.
-
-\subsection{Memory management}
-The third and final phase is memory management.
-Stable tasks, and unreachable task tree nodes are removed.
-If a task is to be removed, tasks with higher memory addresses are moved down.
-For task trees---stored in the heap---the \gls{RTS} already marks tasks and task trees as trash during rewriting so the heap can be compacted in a single pass.
-This is possible because there is no sharing or cycles in task trees and nodes contain pointers pointers to their parent.
+\todo[inline]{conclusion}
 
 \input{subfilepostamble}
 \end{document}