# Multi-Agents simulation principles

Marzhin simulator is based on a “Multi-Agents” simulation kernel. 
The main advantage of this approach is that each AADL thread is implemented by an autonomous “Agent”, 
which behaviour directly follows the semantics specified by the standard. 
Threads interactions are only described architecturally and the complexity of the global scheduler is thus dramatically reduced. 

The simulator operates as follow: 

1. Each simulation cycle starts with an evaluation of the priority of each thread according with the specified scheduling and concurrency control protocols. 
2. Then all the threads are randomly awaken and react according to their current state and specified dispatch protocol. 
3. Finally, one instruction belonging to one of the eligible threads is executed, the new state of each thread is evaluated and a new cycle can begin.

The plugin uses AADL import feature that is implemented with the LMP technology developed by Ellidiss. This approach makes use of a generic AADL syntactic analyzer (aadlrev) that produces a list of Prolog predicates describing the AADL specification to be analyzed. An AADL to Marzhin model transformation expressed by a set of Prolog rules is then used to generate the corresponding appropriate XML input file for Marzhin. This model transformation is stored in the marzhin.sbp file within the config directory of the product installation.

The AADL instance hierarchy is dynamically deduced from the AADL declarative model defined by the current set of opened files. The root of the instance hierarchy is automatically set to the first found System component declaration that contains an Actual_Processor_Binding property association. In case of multiple root System implementation candidates, it is possible for the user to select the instance hierarchy to be analyzed thanks to the Select root wizard.

# Supported AADL Run-Time semantics

## Processor

Marzhin now supports multiple AADL Processors. However, the simulator currently displays only one Processor at a time. 
It is possible to select the displayed Processor with the Configure simulations wizard. 
For each Processor instance, Actual_Processor_Binding property values are searched within the encompassing AADL System implementation in order to identify the bound AADL Threads and their associated AADL Process. 
An AADL Scheduling_Protocol property value is also required to specify the behaviour of the scheduler. Finally, all the AADL Thread, Subprogram and Data subcomponents features and properties, as well as the corresponding AADL connections are analysed to initialize the simulation. 

The following Scheduling Protocols are currently implemented in Marzhin. 
Note that the first three that are listed in the table below can also be applied to AADL Virtual Processors for partitioned architectures.

| Supported Scheduling Protocols | Description |
|-|-|
| Rate_Monotonic_Protocol (or RMS, or RM) | each thread must have a period |
| Deadline_Monotonic_Protocol  (or DM) | each thread must have a deadline |
| POSIX_1003_Highest_Priority_First_Protocol (or HPF) | each thread must have a priority |
| Hierarchical_Offline_Protocol (or ARINC653) | Each processor must define the following properties: <br>ARINC653::module_major_frame<br>ARINC653::partition_slots<br>ARINC653::slots_allocation |

In case of a rate monotonic scheduling protocol, thread priorities are set according to the value specified in their standard AADL period property value. In that case, the highest priority is given to thread having the shortest period.

In case of a deadline monotonic scheduling protocol, thread priorities are set according to the value specified in their standard AADL deadline property value. In that case, the highest priority is given to thread having the shortest deadline.

In case of a highest priority first protocol, thread priorities are set according to the value specified in their standard AADL priority property value. Note that the internal representation for priorities ordering is reversed: for the simulator, the highest priority value is zero. When priority values are set in the AADL specification, there are known by the simulator as “user priority” values.

In case of a hierarchical scheduler (ARINC 653), the AADL model must comply with the ARINC 653 Annex to the standard. In particular, each partition must be defined by the binding between a Virtual Processor and a Process. In addition, the ARINC653::Module_Major_Frame, ARINC653::Partition_Slots and ARINC653::Slots_Allocation must be specified with appropriate values.

The simulator also supports the AADL property Scheduler_Quantum which specifies the maximum continuous amount of time that can be allocated to a given thread. In practice, this property is only used by Marzhin to schedule multiple Background threads with a round robin protocol. If no Scheduler_Quantum is specified, the highest priority background thread runs until completion.

## Thread

The AADL standard defines the possible states and state transitions of a thread during run-time. The subset of this state automaton that is currently supported by Marzhin is shown below:

[TODO link to the diagram]()

A thread is in state Suspended when it has finished all its computation and is waiting for another dispatch. When it is dispatched, it reaches state Ready where it stays until it is the highest priority thread, in which case it moves to state Running. In state Running, the thread executes its instructions until it completes and goes back to state Suspended. Meanwhile, its execution may be delayed by higher priority threads, in which case its state becomes Ready, by unavailable resources, in which case its state becomes Awaiting Resource, or by remote subprogram call execution, in which case its state becomes Awaiting Return.

Thread dispatch is controlled by the AADL Dispatch_Protocol property value. Marzhin supports all the dispatch protocols that are specified by the AADL standard.

| Supported Dispatch Protocols | Description |
|-|-|
| Periodic (default) | dispatched periodically with specified period |
| Aperiodic | dispatched by received events |
| Sporadic | same as Aperiodic, with a minimum inter-arrival time |
| Timed | same as Aperiodic, with a timeout |
| Hybrid | disjunction of Periodic and Aperiodic dispatch conditions |
| Background | dispatched when the processor is free |
	
Threads are described by their standard AADL features, property values and annexes. Marzhin only considers the following the ones specified below:

| Supported Thread Features | Description |
|-|-|
| Event Port | used to dispatch Aperiodic, Sporadic, Timed and Hybrid threads |
| Data Port | local variable whose value can be accessed within a Behavior Annex |
| Event Data Port | combination of a Data Port and a  Event Port |
| Provides Subprogram Access | same as Event Port |
| Requires Subprogram Access | used to express a remote subprogram call |
| Requires Data Access | used to express a remote data access |

| Supported Thread Properties | Description |
|-|-|
| Period | required for Periodic, Sporadic, Timed and Hybrid threads |
| Deadline | required when the scheduling protocol is Deadline_Monotonic_Protocol | 
| Priority | required when the scheduling protocol is POSIX_1003_Highest_Priority_First_Protocol | 
| Compute_Execution_Time | required when no behaviour annex is specified |

In addition, a few additional standard AADL properties can be used to refine the behaviour of dispatching events:

| Supported Feature Properties | Description |
|-|-|
| Queue_Size | maximum number of stored events or calls (default is 1) |
| Dequeue_Protocol | may be OneItem or AllItems |

When a thread is in state Running, it executes a sequence of instructions that can be specified by a Behavior_Specification annex, otherwise default instructions are deduced from existing features and properties. Supported instructions are described in a further section.

## Shared Data

AADL data subcomponents can be shared between thread subcomponents thanks to data access features and connections. If no Behavior_Specification annex subclause is defined for the thread accessing a shared data subcomponent, the corresponding critical section will be considered starting at Dispatch time and ending at Complete time. However, it is possible to define a more precise critical section using appropriate instructions in a Behavior_Specification subclause.

By default, no specific protocol is supported by the simulator for concurrent access to shared data subcomponents. The only one that is supported at that time is Priority_Ceiling_Protocol which requires to be specified in a proper Concurrency_Control_Protocol property.

| Supported Data Properties | Description |
|-|-|
| Concurrency_Control_Protocol | can be used to ensure mutually exclusive access with: Priority_Ceiling_Protocol |

## Subprograms

A client-server communication can be expressed in AADL thanks to subprogram access features. Client thread must have a requires subprogram access feature whereas the server thread must have a provides subprogram access feature. Both features must then be linked by a proper access connection. Note that local calls to subprogram subcomponents within the same thread are not supported yet.

The behaviour of the called subprogram must be expressed within the corresponding subprogram component classifier, under the form of a Behavior_Specification annex subclause or a Compute_Execution_Time property value.

| Supported Subprogram Properties | Description |
|-|-|
| Compute_Execution_Time | specify use of the processor for a given duration. Required when no behaviour annex is specified |

## Instructions

Marzhin can process a limited set of execute instructions that may be explicitly specified with standard AADL Behavior_Specification annex subclauses attached to threads or subprogram component classifiers, or implicitly from existing features and properties.

| Supported Behavior Annex Actions | Description |
|-|-|
| Computation | specify use of the processor for a given duration |
| !< | specify lock of a shared data (GetResource) |
| >! | specify unlock of a shared data (ReleaseResource) |
| ! | specify send of an out event or call of a subprogram |
| := | assigns a value to an out data port |
| If | specify a conditional action block |
| For | specify a loop action block |

Note that only integer values are currently supported for Data Ports.

## Main shortcomings

In addition to the fact that the simulation can only deal with a single processor, this first version of Marzhin suffers from three main restrictions. 

The first limitation resides in that each instruction defines in a Behavior Annex subclause has a fixed duration of one execution tick. If both a compute_execution_time property and a Behavior Annex are provided, the latter will be taken into accounts.

The second restriction is related to the time scale. As opposed to normal AADL specifications, it is not possible to use different time units within the same AADL specification to simulate. Time values are interpreted by their integer value, and a value of one represents a single tick in the simulation trace.

Finally, as described above in this section, Marzhin does not support the entire set of AADL constructs yet. In particular, AADL modes and mode transitions are currently not implemented.