<<

Automated Distributed Implementation of Component-Based Models with Priorities Borzoo Bonakdarpour, Marius Bozga, Jean Quilbeuf

To cite this version:

Borzoo Bonakdarpour, Marius Bozga, Jean Quilbeuf. Automated Distributed Implementation of Component-Based Models with Priorities. 1th International Conference on Embedded , EM- SOFT 2011, Oct 2011, Taipei, Taiwan. pp.59-68, ￿10.1145/2038642.2038654￿. ￿hal-00722405￿

HAL Id: hal-00722405 https://hal.archives-ouvertes.fr/hal-00722405 Submitted on 1 Aug 2012

HAL is a multi-disciplinary open access L’archive ouverte pluridisciplinaire HAL, est archive for the deposit and dissemination of sci- destinée au dépôt et à la diffusion de documents entific research documents, whether they are pub- scientifiques de niveau recherche, publiés ou non, lished or not. The documents may come from émanant des établissements ’enseignement et de teaching and research institutions in France or recherche français ou étrangers, des laboratoires abroad, or from public or private research centers. publics ou privés. Automated Distributed Implementation of Component-based Models with Priorities∗

Borzoo Bonakdarpour Marius Bozga Jean Quilbeuf School of Science VERIMAG, Centre Équation VERIMAG, Centre Équation University of Waterloo 2 avenue de Vignate 2 avenue de Vignate 200 University Avenue West 38610, GIÈRES, France 38610, GIÈRES, France Waterloo, Canada, N2L3G1 [email protected] [email protected] [email protected]

ABSTRACT and Meanings of Programs]: Specifying and Verifying In this paper, we introduce a novel model-based approach for and Reasoning about Programs—Logic of programs; I.2.2 constructing correct distributed implementation of [Artificial Intelligence ]: —Pro- component-based models constrained by priorities. We ar- gram transformation gue that model-based methods are especially of interest in the context of distributed due to their General Terms inherent complexity. Our three-phase method’s input is a model specified in terms of a set of behavioural components Theory, Design, Languages, Reliability, Performance that interact through a set of high-level synchronization primitives (.g., rendezvous and broadcasts) and priority rules for purposes. Our technique, first, trans- Keywords forms the input model into a model that has no priorities. Component-based modeling, Automated transformation, Then, it transforms the deprioritized model into another Distributed systems, BIP, Correctness-by-construction, model that resolves distributed conflicts by incorporating a Committee coordination, Conflict resolution. solution to the committee coordination problem. Finally, it generates distributed code using asynchronous point-to- point send/receive primitives. All transformations preserve 1. INTRODUCTION the properties of their input model by ensuring observational equivalence. The transformations are implemented and our Correct design and implementation of systems experiments validate their effectiveness. has been an ongoing research topic in the past three decades. This problem is significantly more challenging in the context Categories and Subject Descriptors of distributed systems due to a number of factors such as non-determinism, non-atomic execution of processes, race C.2.4 [Computer-Communication Networks]: conditions, and occurrence of faults. Correctness of dis- Distributed Systems[Distributed applications] tributed implementations is of significant importance in the ; D.1.3 [Programming Techniques]: Concurrent Program- context of embedded applications, as such applications are ming—Distributed programming, Parallel programming often employed in safety-critical systems. Model-based de- ; D.2.13 []: Reusable Software— velopment of embedded distributed applications aims at in- Reuse models; D.4.7 [Operating Systems]: Organization creasing their integrity by using explicit models employed in and Design—Real-time and embedded systems; F.3.1 [Logics clearly defined transformation steps leading to correct-by- ∗The research leading to these results has received funding construction artifacts. This approach is beneficial, as one from the European Community’s Seventh Framework Pro- can ensure functional correctness of the system by dealing gramme [FP7/2007-2013] under grant agreement no 248776 with a high-level formally specified model that abstracts im- (PRO3D) and no 257414 (ASCENS), from ARTEMIS JU plementation details and then derives a correct implemen- grant agreement ARTEMIS-2009-1-100230 (SMECY) and tation through a series of transformations that terminates from Canada ORF RE03-045, NSERC DG 357121-2008, and when an actual executable code is obtained. IS09-06-037 grants. In this paper, we focus on the BIP framework [5] as our formal modelling language. BIP (Behaviour, Interaction, Priority) is based on a semantic model encompassing com- Permission to make digital or hard copies of all or part of this work for position of heterogeneous components. The behaviour of personal or classroom use is granted without fee provided that copies are components is described as an automaton or ex- not made or distributed for profit or commercial advantage and that copies tended by data and functions given in C++. BIP uses a bear this notice and the full citation on the first page. To copy otherwise, to diverse set of composition operators for obtaining compos- republish, to post on servers or to redistribute to lists, requires prior specific ite components from a set of components. The operators are permission and/or a fee. EMSOFT'11, October 9–14, 2011, Taipei, Taiwan. parametrized by a set of interactions between the composed Copyright 2011 ACM 978-1-4503-0714-7/11/10 ...$10.00. components. Finally, priorities are used to specify different scheduling mechanisms1. Transforming a BIP model into a BIP model 1 Deprioritized 2 Distributed distributed implementation involves addressing three funda- Multiparty BIP model BIP model mental issues: interactions Multiparty Send/Receive + Priorities interactions interactions 1. Concurrency: Components and interactions should be able to run concurrently while respecting the se- 3 quential semantics of the high-level model. Distributed code 2. Conflict resolution: Interactions that share a common component can potentially conflict with each other. Figure 1: Steps for generating a distributed imple- mentation from a high-level BIP model. 3. Enforcing priorities: When two interactions can execute simultaneously, the one with higher priority must be executed. are non-uniform. Hence, in order to improve the per- formance, it is reasonable to devise that These issues introduce challenging problems in a give priority to groups that require resources with higher distributed setting. The conflict resolution issue can be ad- demand. A concrete example of group mutual exclu- dressed by incorporating solutions to the committee coordi- sion is the well-known readers/writers problem. In nation problem [9] for implementing multiparty interactions. most cases, we give priority to readers to improve the For example, Bagrodia [2] proposes different solutions with performance. different degrees of parallelism. The most distributed solu- tion is based on the drinking philosophers problem [8], and • Reducing non-determinism. Non-determinism has inspired the approaches by P´erez et al. [14] and Parrow in distributed and is one of the et al. [13]. In the context of BIP, a transformation address- sources of obtaining a diverse set of behaviours. In ing all the three challenges through employing centralized many scenarios and in particular, in embedded appli- scheduler is proposed in [4]. Moreover, in [6,7], we pro- cations, it is desirable to guide the system to behave pose transformations that address the concurrency issue by in a certain predictable fashion. breaking the atomicity of interactions and conflict resolu- tion by embedding a solution to the committee coordination The main challenge in ensuring priorities in a distributed problem in a distributed fashion. On the contrary, designing setting is their correct implementation. This is due to the transformations that enforce priorities between interactions fact that components need to obtain a reliable knowledge in a distributed setting remains unaddressed in spite of the about enabledness of interactions, so that only the inter- vital role specifying priorities plays in designing systems. action with highest priority is executed. In [3], the authors propose a model checking approach that determines whether actions of a given Petri net can be executed without violat- 1.1 Motivation ing priority rules. However, the downside of this approach is (1) it has scaling issues, as it uses model checking, and Priorities are widely used in system design, as a way of (2) in most cases the local knowledge of processes is shown scheduling events. Below, we present examples of how ap- to be insufficient to decide whether or not an action can be plying priorities can guide a system to satisfy certain prop- executed. Other approaches include applying customized al- erties: gorithms to implement priority rules for specific problems in (e.g., [12]). • Ensuring safety. Safety properties are normally of These examples demonstrate the demand for developing the form “nothing bad happens during the system ex- methods that automatically construct a correct distributed ecution”. In the context of concurrent and distributed implementation by starting from a high-level model along computing, such bad things are often due to existence with a set of priority rules. This way, all implementation of a set of processes competing over a resource. Prior- issues are dealt with by transformation algorithms and de- ities can be used to resolve such race conditions. For signers only need to make minimal effort to develop models. instance, one way to prevent two processes to enter a simultaneously is to give explicit pri- ority to one . Dynamic priorities can then be 1.2 Contributions used to ensure non-starvation. Our contributions in this paper are as follows: • Improving performance. In distributed sys- tems, it is often the case that certain resources have • We propose a transformation that, given a high-level higher demands. For example, in group mutual exclu- BIP model with priorities, generates a BIP model with- sion [10], as Mittal and Mohan argue [12], in many out priorities, that behaves equivalently. This corre- commonly considered systems, group access requests sponds to the first step in Figure 1. 1Although our focus is on BIP, all results in this paper can be applied to any model that is specified in terms of a set • We show the correctness of this transformation by prov- of components synchronized by broadcast and rendezvous ing that the initial and transformed models are obser- interactions. vationally equivalent. ′′ p v • We apply the transformation introduced in [7] to derive ( p ) ′ ′ ′ Xp. We write (q, v) −→ (q , v ), iff τ = (q, p, g, f, q ) ∈ T , a distributed model, where multiparty interactions are ′ ′′ ′ g(v) is true, and v = f(v[Xp ← vp ]), (i.e., v is obtained by implemented in terms of asynchronous point-to-point applying f after updating variables Xp associated to p by send/receive primitives. This corresponds to the sec- ′′ the values vp ). When the communication port is irrelevant, ond step in Figure 1. From this distributed model, we p we simply write (q, v) → (q′, v′). Similarly, (q, v) → means generate distributed code, as explained in [6,7], which that there exists a transition τ = (q, p, g, f, q′) such that g(v) completes the design flow from the initial BIP model is true; i.e., p is enabled in state (q, v). with priorities to a correct distributed implementation. Figure 2(a) shows an atomic component B, where Q = • Finally, we validate the effectiveness of our approach {s}, X = {n}, P = {(p, {n})}, and T = {(s, p, g, f, s)}. by modelling a jukebox application in BIP and con- Here g is always true and f is the identity function. ducting experiments on the generated distributed code. The jukebox application incorporates priorities to man- Interactions. For a model built from a set of n atomic n age demands on reading discs and our experiments components {Bi = (Qi,Xi,Pi,Ti)}i=1, we assume that their show that the overhead of our transformations has respective sets of ports and variables are pairwise disjoint; minimal effect on the benefit of using priorities. i.e., for any two i 6= j in {1..n}, we require that Pi ∩ Pj = ∅ n and Xi ∩ Xj = ∅. Thus, we define the set P = Si=1 Pi of Organization. The rest of the paper is organized as n all ports in the model as well as the set X = Si=1 Xi of all follows. In Section 2, we present the basic semantics model variables. An interaction a is a triple (Pa,Ga,Fa), where of BIP. Then, in Section 3, we describe our transformation Pa ⊆ P is a set of ports, Ga is a guard, and Fa is an up- for deriving a model that has no priorities. Our approach for date function, both defined on the variables associated by deriving a distributed model and code is presented in Section the ports in P (i.e., X ). By P = {p } ∈ , we mean a Sp∈Pa p a i i I 4. We discuss our case study and experimental results in that for all i ∈ I, pi ∈ Pi, where I ⊆ {1..n}. We denote by i Section 5. Finally, we conclude in Section 6. Fa the projection of Fa on Xpi .

2. BASIC SEMANTIC MODELS OF BIP Priorities. Given a set γ of interactions, a priority be- tween two interactions specifies which one is preferred over the other. We define such priorities through a partial order In this section, we present operational global state seman- π ⊆ γ × γ. We write aπb if (a, b) ∈ π, which means that a tics of BIP [5]. BIP is a component framework for con- has less priority than b. structing systems by superposing three layers of modelling: Behaviour, Interaction, and Priority. Definition 2 (Composite Component). A compos- ite component (or simply component) is defined by a set of Atomic Components. We define atomic components as components, composed by a set of interactions γ and a prior- transition systems extended with a set of ports and a set of def variables. Each transition is guarded by a predicate on the ity partial order π ⊆ γ×γ. We denote B = πγ(B1,...,Bn) variables, triggers an update function, and is labelled by a the component obtained by composing components B1, ··· ,Bn port. The ports are used for communication among different using the interactions γ and the priorities π. components and each port is associated with a subset of variables of the component. Note that if the system does not contain any priority, we may omit π. Definition 1 (Atomic Component). An atomic com- ponent B is a labelled transition system represented by a Definition 3 (Composite Component Semantics). tuple (Q, X, P, T ) where: The behaviour of a composite component without priority γ(B1, ··· ,Bn), where Bi = (Qi,Xi,Pi,Ti) and →i is the • Q is a set of control states. transition relation between states of Bi, is a transition sys- tem (Q, γ, X, → ), where Q = Πn Q , X = n X and • X is a set of variables. γ i=1 i Si=1 i →γ is the least set of transitions satisfying the rule: • P is a set of communication ports. Each port is a pair a = ({pi}i∈I ,Ga,Fa) ∈ γ (p, Xp) where p is a label and Xp ⊆ X is the set of ′ ′ Ga(v1, . . . , vn) ∀i 6∈ I. (qi, vi) = (qi, vi) ′′ variables bound to p. By abuse of notation, we denote p (v ) i pi ′ ′ ′′ i a port (p, Xp) by p. ∀i ∈ I. (qi, vi) −→ i (qi, vi), vpi = Fa(v1, . . . , vn) a ′ ′ ′ ′ ′ ((q , v ),..., (q , v )) → ((q , v ),..., (q , v )) • T is a set of transitions of the form τ = (q, p, g, f, q ) 1 1 n n γ 1 1 n n q, q′ Q p P g where ∈ are control states, ∈ is a port, is We denote (q, v) the state of γ(B1, ··· ,Bn) that correspond τ f τ g the guard of and is the update function of . is to the states (q1, v1), ··· , (qn, vn) of the components B1, ··· ,Bn. a predicate defined over the variables in X and f is a We define the behaviour of the composite component B = X function that computes new values for according to πγ(B1,...,Bn) as the transition system (Q, γ, X, →π) where the previous ones. →π is the least set of transitions satisfying the rule:

′ We denote X the set of valuations on X, and Q × X the a ′ ′ ′ ′ a ′ ′ (q, v) →γ (q , v ) ∀a ∈ γ. aπa =⇒ (q, v) 6→γ set of local states. Let (q, v) and (q , v ) be two states in a ′ ′ ′′ (q, v) →π (q , v ) Q × X, p be a port in P , and vp be a valuation in Xp of p share a common port or there is a couple of ports with one n s member in each interaction such that these two ports label p two conflicting transitions of the same component. Clearly, (a) An atomic strong conflict implies weak conflict but the converse is not component true.

a = p1p2 b = p2p3 c = p3p4 3. DEPRIORITIZING A BIP MODEL ⌈n1 > n2⌋ ⌈n2 > n3⌋ ⌈n3 > n4⌋ sw(n , n ) sw(n , n ) sw(n , n ) 1 2 2 3 3 4 In this section, we describe our approach to transform a BIP model B into an equivalent model without priorities, ˜ p1 p2 p3 p4 denoted B. Intuitively, our transformation proceeds as fol- lows: n1 n2 n3 n4 s1 s2 s3 s4 p1 p2 p3 p4 1. First, it replaces atomic components in B by func- (b) A BIP composite component that sorts integers tionally equivalent send/receive atomic components, ni, obtained by gluing 4 atomic components using where atomicity of transitions and interactions is bro- 3 interactions. ken. This first transformation, already used in [4,6,7] separates the synchronization from on Figure 2: Atomic and composite components in BIP component transitions and enables the concurrent ex- ecution of atomic components.

Intuitively, the first inference rule specifies that a compos- 2. Secondly, it inserts manager components for handling ite component B = γ(B1,...,Bn) can execute an interac- interactions. These managers detect enabledness of tion a ∈ γ, iff (1) for each port pi ∈ Pa, the corresponding interactions and schedule them for execution according atomic component Bi can execute a transition labelled by to priority rules. Managers interact with each other pi, and (2) the guard Ga of the interaction evaluates to true through multi-party interactions in order to maintain in the current state. Execution of the interaction modifies a consistent view on the state of the system. components’ variables by first applying update function Fa to associated variables and then function fi inside each com- ponent. The states of components that do not participate in 3.1 Breaking Atomicity the interaction stay unchanged. The second inference rule simply filters out transitions which are not maximal with re- spect to priorities. A transition is executed only if no other The transformation of atomic components splits each tran- one with higher priority is enabled. sition into two consecutive steps: (i) an offer that publishes the current state of the component, and (ii) a notification Figure 2(b) illustrates a composite component γ(B1, ··· , that triggers the update function. The intuition behind this B4), where each Bi is identical to component B in Fig- ure 2(a). The set γ of interactions is {a, b, c}, where a = transformation is that the offer transition corresponds to sending information about component’s intention to inter- ({p1, p2}, n1 > n2, sw(n1, n2)) and function sw swaps the values of its arguments. Interactions b and c are defined in a act with the other components. The notification transition receives the response from the scheduler, once some interac- similar fashion. Interaction a is enabled when ports p1 and tion has been completed. Local update functions can then p2 are enabled and the value of n1 (in B1) is greater than be executed concurrently and independently by components the value of n2 (in B2). Thus, the composite component B upon notification reception. sorts variables n1 ··· n4, such that n1 contains the smallest The offer transition publishes its enabled ports through a and n4 contains largest values. It may be desirable to always execute interaction a when special port named o. Enabled ports are encoded through a possible. This can be done by adding the two priority rules list of Boolean variables. After the computation of the local function, this list is updated to the ports that are enabled at bπa and cπa. We denote the obtained component by πγ(B1, the next control state. Notification transitions are triggered ...,B4). We will use this example to illustrate the trans- formations presented in this paper. by corresponding ports from the original atomic component. We now introduce the notion of conflicting interactions. Definition 5 (Transformed atomic components). Intuitively, two interactions a and a are weakly conflicting 1 2 Let B = (Q, X, P, T ) be an atomic component. The corre- iff they share a common component. sponding transformed atomic component is B⊥ Q⊥,X⊥,P ⊥,T ⊥ Definition 4 (Weak Conflict). Two interactions a1 = ( ), such that: and a2 are weakly conflicting (denoted a1 ⊕a2) iff there exist ⊥ • Q = Q ∪ {⊥s |s ∈ Q}. two ports p and q in some component B such that p ∈ Pa1 ⊥ and q ∈ Pa2 . • X = X ∪ {xp}p∈P , where each xp is a Boolean vari- able indicating whether port p is enabled. This kind of conflict is called weak because it is weaker than the definition of conflict in [7], that we call here strong • P ⊥ = P ∪ {o}, where o is the offer port. All variables ⊥ ⊥ conflict. Two interactions are strongly conflicting iff they in X are associated to o (i.e., Xo = X ). o p Table 1: Ports of a manager component xp n port variables description p xp := true a a a receives offers from atomic compo- oi {xpi } ∪ Xpi ⊥s s nent Bi o change status to enabled or disabled ι ∅ (internal port) start ∅ triggers interaction execution Figure 3: Transformed version of one atomic com- a notifies atomic components upon ponent from Figure 2(b) n {Xa } a pi execution signals disabled status to other man- disa ∅ • For each transition τ = (q, p, g, f, q′) ∈ T , we include agers T ⊥ gets notified about execution of the following two transitions in : a ⊕a {bi } weakly conflicting interactions by q other managers 1. offer τo = (⊥q, o, go, fo, q) where go is true, fo is the identity function, and a similar to port ⊕a, but for interac- ⊕disa {bi } q tions with higher priority 2. notification τp = (q, p, gp, fp, ⊥q′ ) where gp is true and fp applies fτ on X and for each port r ∈ P , ′ ′ ′ ′ ′′ it sets xr to true if τ = (q , r, g , f , q ) ∈ T for ′′ ′ some q and g is true. Otherwise, xr is set to • The set of control states is Q = {undef , en, dis, exc}. false. Intuitively, in state undef (undefined), the manager does not have enough information to decide whether In Definition 5, states {⊥s |s ∈ Q} from where the compo- or not interaction a is enabled. This is normally be- nent sends offers, are called busy or unstable states. States cause some offers have not been received yet. In states Q, from where the component is waiting to receive a notifi- en (enabled) and dis (disabled), the manager knows cation, are called stable states. that a is enabled or disabled, respectively. In state exc Figure 3 shows the transformed version of the atomic com- (executing), the interaction a is being executed. ponent shown in Figure 2(a). Initially, the component is in a a a busy state ⊥s and the value of xp is true; i.e., the compo- • The set of variables is X = {bi }i∈I ∪{xp }∪Xp . i i pi∈a nent is willing to interact on port p. Then, it sends an offer For every component Bi, the manager holds a Boolean through port o containing the current values of xp and n a variable bi which is true iff component Bi is in a stable and reaches stable state s. The reception of a notification state, that is, waiting for a notification. For every port corresponds to the p-labelled transition that brings back the a pi ∈ a, the manager holds respectively, a Boolean xpi component to the initial busy state. which indicates the status of the port (i.e., enabled or a disabled) and variables Xpi that is, data associated to 3.2 Interaction Managers the port pi. • The set of ports P and their associated variables is The set of managers are introduced to execute interactions presented in Table 1. according to the global semantics of the original BIP model described in Section 2. To this end, a manager component • The set of transitions T and their intuitive meaning is for an interaction a has to (i) detect enabledness of a by presented in Table 2. listening to offers sent by atomic components, (ii) trigger the execution of a, (iii) notifies atomic components as well as the other conflicting managers, whenever the interaction ⊕a ⊕disa disa starta a is executed. b2 ⌈(ba ∧ xa )∧ Let us observe that if two interactions are weakly conflict- a a 1 p ⌈(b ∧ b )∧ a a 1 a 1 2 (b ∧ x )∧ ing, then executing one can change the status of the other. b (¬xa 2 p2 1 p1 a a a (n1 > n2 )⌋ For instance, let a and b be two interactions, such that a⊕b; ∨¬xp a 2 a ι en ∨n1 ≤ n2 )⌋ i.e., they share some component B. Obviously, executing a ι starta a triggers a transition in component B. This transition can disa dis undef ⊕a b1 := false ⊕a a result in changing the status of interaction b. That is, until b2 := false sw(na,na) ⊕disa n 1 2 ⊕ o a component B completes its local execution and sends a new a a a i exc xp xp a ba := true offer, the status of enabled ports and values of variables in 1 2 n1 i B can change. a a na o1 o2 2 na Definition 6 (Interaction Manager). Let a ∈ γ be an interaction, where Pa = {pi}i∈I . The interaction man- ager Ma is an atomic component Ma = (Q, X, P, T ) defined Figure 4: The manager component for interaction a as follows: between components B1 and B2 in Figure 2(b). Table 2: Transitions of a manager component Transition Guard / Function Description

a oi a undef −→ undef -/ bi := true receive offer from Bi

ι Ga ∧ undef −→ en a a change state to enabled ∀i ∈ I. (bi ∧ xpi )/- (∀i ∈ I.ba) ∧ (¬G ∨ ∃i ∈ I.¬xa ) ι i a pi change state to disabled undef −→ dis /- a starta -/ {bi } := false; en → exc a a execute interaction, apply update function. {Xpi }:=Fa({Xpi }) n exc →a undef -/- notifies atomic components on execution dis dis →a dis -/- signals disabled state ⊕ dis →a undef ⊕ gets notified about execution of a weakly conflicting undef →a undef -/- ⊕ interaction en →a undef gets notified about execution of a higher priority weakly ⊕disa -/- dis → undef conflicting interaction

Figure 4 represents the manager for interaction a in Figure B = πγ(B1, ··· ,Bn), with γ = {a1 ··· am}, we define its de- a a ˜ ⊥ ⊥ 2(b). It contains the variables b1 and b2 since interaction a prioritized version as B =γ ˜(B1 , ··· ,Bn ,Ma1 , ··· ,Mam ), ⊥ involves components B1 and B2. The manager contains two where Bi is obtained from Bi as explained in definition 5, a a a offer ports o1 and o2 . Port oi , i ∈ {1, 2}, is associated with Maj is obtained from aj as explained in definition 6, and γ˜ a variables (1) xpi , which indicates the status of port pi in Bi, contains the following interactions: and (2) na, that are local copies of variables n associated i i • Offer interactions. For each i ∈ {1 ··· n}, γ˜ con- to ports pi in Figure 2(b). All these variables are refreshed a tains the interaction offi, where upon receiving an offer through ports oi . The transition a P = {o } ∪ (i) {o }. For each interaction a ∈ ba xa ba xa na > offi i Sa∈γ i from undef to en guarded by ( 1 ∧ p1 ) ∧ ( 2 ∧ p2 ) ∧ ( 1 i a ( ) n γ , the update function Foffi sets the values of vari- 2 ) switches from undefined to enabled state. The two first a a ables {x } ∪ X to the values of {xp} ∪ Xp associated conjuncts ensures that (1) B1 and B2 are in stable state, and pi pi to oi, where p is the of port Bi involved in a. Offer in- (2) p1 and p2 are enabled. The latter conjunct corresponds to the guard of interaction a in Figure 2(b). Likewise, the teractions have no guard and they only copy data from transition from undef to dis allows reaching the state where the sender component to the manager. the interaction a is disabled. The update function associated • Notifications interactions. For each interaction a a start to τ sets b1 and b2 to false and then swaps the variables a ∈ γ, where a = {pi}i∈I , γ˜ contains the interaction na and na. Both na and na are associated to the notification 1 2 1 2 not a, such that Pnota = na ∪ {pi}i∈I . This interaction port na, so their new values are sent back to the component. notifies each component which port has been selected.

The update function Fnota copy back data to each com-

ponent Bi involved in a. That is, the values of Xpi (in a Bi) are set to the values of Xpi (from Ma). • Schedule interactions. For each interaction a ∈ γ, 3.3 Connecting Managers γ˜ contains the interaction a˜:

Pa˜ = {start a} The transformed atomic components and interaction man- ∪ {⊕ |c ⊕ a, c 6> a} agers are interconnected using three types of interactions: c (i) offer interactions where components send their enabled ∪ {disc|c ⊕ a, aπc} ports to corresponding managers, (ii) notification interac- ∪ {⊕disc|c ⊕ a, aπc} tions where managers notify components after execution of This interaction has no guard. For each interaction c an interaction, and (iii) schedule interactions where priority weakly conflicting with a, the update function F sets rules are handled. a˜ variable bc of manager M to false through the port We now formally define the deprioritized model, by spec- i c ⊕ if {a, c} ⊆ γ(i). In other terms, the start inter- ifying how we connect the components defined so far. Let c a action informs the manager M that the components γ(i) denote the set of all interactions in γ that involve the c causing the weak conflict with a have moved and are component B . i not in their stable state anymore. This information c maintains coherence between the bi variable in each Definition 7 (Deprioritized model). Given a model manager Mc and the actual state of component Bi. ˜ b c˜ Proof. In any reachable state, if a manager reaches the a˜ state exc then the corresponding notification is enabled, ⊕ d s ⊕ d s ⊕ d s a ⊕a da a b ⊕b db b c ⊕c dc c since schedule interactions and boolean variables bi ensure Ma Mb Mc that each component may receive only one notification after oa oa ob ob oc oc 1 2 na 2 3 nb 3 4 nc each offer. Similarly, if any component reaches an unstable state, then the corresponding offer is enabled. Offer interactions are independent since they do not share any port nor change a common variable. Thus, the order of o1 p1 o2 p2 o3 p3 o4 p4 ⊥ ⊥ ⊥ ⊥ their execution does not change the final state. B1 B2 B3 B4 Notification interactions (that correspond to interactions of the original model, augmented by a notification port) en- abled from a reachable state are not conflicting since sched- Figure 5: Deprioritized version of model from Fig- ule interactions handle weak conflicts. Thus, notification ure 2(b). interactions are independent and their order of execution β does not change the final state. We can conclude that → is Figure 5 presents the deprioritized model from Figure confluent. 2(b). Please note that the port names have been shortened From proposition 1 and 2, for each reachable stateq ˜ of for space reasons (e.g. sa and da stand for start a and disa ∗ β respectively. For offer and notification interactions, we in- B˜, there is a unique state denoted [˜q] such thatq ˜ → [˜q] and terpret a triangle port as a send port (i.e., for sending offers) β and bullet port as a receive port (i.e., for receiving offers). [˜q] 6→. Note that offers and notifications only copy variables be- We recall the definition of observational equivalence of tween components and managers. two transition systems A = (QA,P ∪ {β}, →A) and B = If we assume priorities bπa and cπa for the model in Figure (QB ,P ∪ {β}, →B ). It is based on the usual definition of 2(b), we obtain the following schedule interactions: a has no weak bisimilarity [11], where β-transitions are considered higher priority interaction and is weakly conflicting with b, unobservable. The same definition is trivially extended for b atomic and composite BIP components. thus Pa˜ = {start a, ⊕b}. Executinga ˜ will set the variable b2 to false in Mb, since B2 will become busy. b has less priority than a and is weakly conflicting with both a and c, thus Definition 8 (Weak Simulation). A weak simulation over A and B, denoted A ⊂ B, is a relation R ⊆ QA × QB , P˜b = {start b, ⊕disa, ⊕c}. c has less priority than a and is a ′ ′ such that we have ∀(q, r) ∈ R, a ∈ P : q →A q =⇒ ∃r : weakly conflicting with b, thus Pc˜ = {start c, disa, ⊕b}. ∗ ∗ ′ ′ β aβ ′ β ′ (q , r ) ∈ R ∧ r → B r and ∀(q, r) ∈ R : q →A q =⇒ ∗ ′ ′ ′ β ′ 3.4 Correctness ∃r :(q , r ) ∈ R ∧ r →B r A weak bisimulation over A and B is a relation R such We now show that the above transformation preserves the that R and R−1 are both weak simulations. We say that A semantics of the original BIP model. By preserving the orig- and B are observationally equivalent and we write A ∼ B inal semantics, we mean ensuring observational equivalence if for each state of A there is a weakly bisimilar state of between the original model and the transformed model. This B and conversely. We consider the correspondence between is proved in Theorem 1. observable actions of B and B˜ as follows. To each interaction ˜ ⊥ Let B = πγ(B1, ··· ,Bn) be a BIP model and B =γ ˜(B1 , a ∈ γ, where γ is the set of interactions of B, we associate ⊥ ··· ,Bn ,Ma1 , ··· ,Mam ) be its unprioritized version. We the schedule interactiona ˜ of B˜. denote q = (q1, ··· , qn) a state of B andq ˜ = (˜q1, ··· , q˜n, ˜ ˜ s1, ··· , sm) a state of B. We show that B is observationally Theorem 1. B ∼ B˜. equivalent to B. The observable actions of B are the interactions γ. The Proof. We define the relation R between the states of ˜ B and the states of B˜ as follows: the couple (˜q, q) is in the observable actions of B are only the schedule interactions, ⊥ ⊥ that is {a˜|a ∈ γ}. The remaining interactions in B˜, namely relation R if the states of atomic components B1 , ··· ,Bn in [˜q] are the same as in q. Formally, we have (˜q, q) ∈ R if offers offi and notifications nota, are unobservable and are β [˜q] = (q , ··· , qn, s , ··· , sm) and q = (q , ··· , qn). We show denoted β. We denoteq ˜ → q˜′ if a β action brings the system 1 1 1 that R is an observational equivalence by proving the next from stateq ˜ to state q˜′. three assertions:

β β Proposition 1. → is terminating. (i) If (˜q, q) ∈ R andq ˜ → r˜ then (˜r, q) ∈ R. Proof. Each β action involve at least a component. Each a˜ a component can take part in at most 2 β actions, 1 notifica- (ii) If (˜q, q) ∈ R andq ˜ → r˜ then ∃r : q → r and (˜r, r) ∈ R. tion and 1 offer, then no other β action is possible until an ∗ a β a˜ a˜ action is executed. Thus at most 2n consecutive β-steps (iii) If (˜q, q) ∈ R and q → r then ∃r˜ :q ˜ −→ r˜ and (˜r, r) ∈ R. are possible. The point (i) comes from the definition of R. β Proposition 2. From any reachable state q˜ of B˜, → is (ii) If the interactiona ˜ is enabled, then manager Ma is in confluent. state en, which implies that at equivalent state q: • All ports of a are enabled and the guard Ga is true, Conflict Resolution CRP en since the guard of the τ transition is true Protocol ok f ok f ok f • No higher priority interaction is enabled sincea ˜ is en- abled only when managers corresponding to such in- ra rb teractions are in state dis. Interaction IP IP Protocol 1 2 a n n˜ n Thus we have q → r, and the reader can easily check that a˜ b c˜ (˜r, r) ∈ R. (iii) Fromq ˜ we can reach [˜q] by using only β transitions. In state [˜q], since every atomic component has sent an offer, the state of each manager will be either en or dis, according o o o Components M ⊥ M ⊥ M ⊥ to the status of the corresponding interaction at state q in tier a b c B. Then since a is enabled at state q, Ma is in state en at state [˜q]. If there is any interaction b with higher priority than a, then it is disabled in state q, thus the manager Mb Part copied is in state dis at state [˜q]. Thusa ˜ is enabled at state [˜q] from deprior- ∗ itized model ⊥ ⊥ ⊥ ⊥ β a˜ B B B B and we haveq ˜ −→ r˜. Executing the notification interaction 1 2 3 4 na and the offer interactions from components involved in a lead B˜ in a state where atomic components have the same state as in r. Thus (˜r, r) ∈ R. Figure 6: Distributed version of the deprioritized model from Figure 5. 4. BUILDING A DISTRIBUTED MODEL: THE 3-TIER ARCHITECTURE the transformation explained in Subsection 3.1 to break atomicity), and Once we construct a model with no priorities as prescribed ⊥ ⊥ • B1 ··· Bn are copied from the deprioritized model, in Section 3, one can apply the technique presented in [6] to since they have already been transformed by the de- generate distributed code. We now briefly recap this tech- prioritization. nique. The code generation is accomplished in two steps. First, from a given BIP model, we generate another BIP Interaction Protocol. This tier consists of a set of com- model that only incorporates asynchronous ponents each hosting a set of interactions from the depriori- as interactions (denoted SR-BIP). Then, we transform the tized BIP model. Conflicts between interactions included in SR-BIP model into a set of executables – one per atomic the same component are resolved by that component locally. component – that communicate using asynchronous mes- For instance, interactionsa ˜ and ˜b in Figure 5 are grouped sage passing primitives such as MPI or TCP sockets. We into component IP1 in Figure 6. Thus, the conflict between only review the first step. a˜ and ˜b is handled locally in IP1. To the contrary, the con- Distributed execution of interactions may introduce con- flict between ˜b andc ˜ has to be resolved using the third tier flicts even if we do not consider priorities. Thus, our target of our model. The interaction protocol also evaluates the SR-BIP model in a transformation should have the follow- guard of each interaction and executes the code associated ing three properties: (1) preserving the behaviour of each with an interaction that is selected locally or by the upper atomic component, (2) preserving the behaviour of interac- tier. The interface between this tier and the component tier tions, and (3) resolving conflicts in a distributed manner. provides ports for receiving enabled ports from each com- Moreover, we require that interactions in the target model ponent and notifying the components on permitted port for are asynchronous message passing. execution (ports na˜, n˜b, nc˜). We design our target BIP model based on the three tasks identified above, where we incorporate one tier for each Conflict Resolution Protocol. This tier accommo- task. Since several distributed algorithms exist in the litera- dates an that solves the committee coordination ture for conflict resolution, we design the tier corresponding problem [9] to resolve conflicts between interactions hosted to conflict resolution so that it provides appropriate inter- in separate interaction protocol components. For instance, faces with minimal restrictions. As a running example, we the external conflict between interactions ˜b andc ˜ is resolved use the part of the model presented in Figure 5 formed by by the central component CRP in Figure 6. We emphasize γsched(Ma,Mb,Mc) where γsched = {a,˜ ˜b, c˜} to describe the that the structure of components in this tier solely depends concepts of our transformation. The distributed version of upon the augmented committee coordination algorithm. In- γsched(Ma,Mb,Mc) is presented in Figure 6. Our 3-tier ar- corporating a centralized algorithm results in one compo- chitecture consists of the following. nent CRP as illustrated in Figure 6. Other algorithms, such as ones that use a circulating token [2] or dining philoso- ˜ ⊥ ⊥ Components Tier. Let B =γ ˜(B1 ··· Bn ,Ma1 ··· Mam ) phers [1,9] result in different structures in this tier and are be a deprioritized BIP model. The component tier includes discussed in detail in [7]. The interface between this tier components: and the Interaction Protocol involves ports for receiving re- quests to reserve an interaction (labelled r) and responding ⊥ ⊥ • Ma1 ··· Mam (i.e., manager components obtained by by either success (labelled ok) or failure (labelled f). R1 R2 R3 R4 to read = 2 read read read read read ⌈current == to read⌋ to read R data data read J1 J2 load unload load unload data J D to load =to load - current load load data current unload unload load unload load unload load unload load unload if (to load== ∅) to load={1,2,3,4} to load D1 D2 D3 D4 id load unload unload load Figure 7: BIP Model for the jukebox example. ⌈id ∈ to load⌋ current=id 5. CASE STUDY Figure 8: Behaviour of jukebox components and in- teractions. In this section, we use a jukebox example to illustrate our deprioritization transformation and conduct experiments to study the effectiveness of our method (see the models in Since each disc is eventually loaded, each read inter- Figures 7 and 8). This model represents a system, where a action will take place and the execution terminates. set of readers (R1,...,R4) need to access the data located Otherwise, sequences of load/unload interaction could on discs (D1,...,D4). A reader may need any disc. Access occur forever. Note that here, assuming fairness en- to the disc is managed by jukebox components (J1,J2) that sures that the model eventually terminates. can load any disc to make it available for reading. Each pair (Di,Jk), i ∈ {1 ··· 4} and k ∈ {1, 2}, has two interactions: • Priorities to speed up execution. By inspect- (1) a loadi,k interaction for loading the disc in the jukebox ing the discs requested by the readers, we know that and an unloadi,k interaction for unloading it. Each reader some discs are more often needed than others. Thus, Rj is connected to a jukebox through a readj interaction. we give higher priority to the corresponding load in- During the test, we simulate execution of interactions by teractions. Here, we give higher priority to Disc 1 in waiting a given amount of time. Namely, we wait 100ms for Jukebox 1 by adding the following set of priorities: load/unload and 500ms for read. {loadi,1 π load1,1 | i ∈ {2, 3, 4}}. Figure 8 presents the behaviour of atomic components and the data transfer on interactions. To ensure that all discs are For both versions B∅ and Bπ, we generate the correspond- eventually loaded, each jukebox keeps a list of discs to load, ing deprioritized models B˜∅ and B˜π. In Table 3, we present namely to load. Each time a disc is loaded, it is removed the size – the number of atomic components and the number from the list by the load transition in the jukebox compo- of interactions – of these different models, in the columns la- nent. The guard of a load interaction prevents the disc to be belled “Orig.”. We then apply the transformation provided loaded if it is not on the list. When the to load list becomes in [7] to the models B∅, B˜∅, and B˜π to obtain a distributed empty, it is reinitialized to the set of all discs on the unload version of each model including a centralized scheduler2. interaction. The variable current contains the identity (i.e., The number of Send/Receive components and interactions 1 ... 4) of the disc currently loaded in the jukebox, and is contained in the distributed version of these models is given updated by the load interaction. In order to ensure that the in the columns labelled “S/R” in Table 3. We simulate the reader gets the correct data, a guard on the {read, data} execution of these models on two different platforms. The interaction holds, only if the disc in the jukebox (current) first one is centralized, where only one processor is available is the one to be read (to read). Each reader has a sequence to execute all components. The second one is fully decentral- of 2 discs to read. The variable to read contains the id of ized, where each atomic component has its own processor. the next disc to be read. It is initialized with the first value We assume that executing a load, unload or read interaction (not shown in the figure), and is updated after the first read. completely blocks the processor. For each couple (model, This model has finite runs: the execution terminates when execution platform), we measure the average time of termi- all readers have read the two discs they needed. nating executions. The results are presented in Table 3. We consider two versions of the model. The first model, As mentioned earlier, we applied our deprioritization trans- denoted B∅, does not contain priorities. The second model, formation to model B∅ although we can directly obtain a denoted Bπ, is the B∅ restricted by two types of priorities: distributed model. By comparing the execution times of B∅ and B˜∅ on the centralized platform, we observe that our • Priorities to enforce termination. We give pri- deprioritization transformation does not introduce a signifi- ority to the read interactions over the unload interac- cant overhead, even if it increases the number of components tions. Formally, it corresponds to the sets of priorities and interactions. {unloadi,1 π readj | i ∈ {1, ··· , 4}, j ∈ {1, 2}} and {unloadi, π readj | i ∈ {1, ··· , 4}, j ∈ {3, 4}}, for 2 2 We cannot transform directly Bπ into such a distributed each jukebox. This ensures that any enabled read in- model since the transformation presented in [7] does not teraction will be executed before the disc is unloaded. take priorities into account. 7. REFERENCES Table 3: Model size and execution time (s) for dif- ferent implementations of Figure 7. [1] R. Bagrodia. Process synchronization: Design and performance evaluation of distributed algorithms. Model Size IEEE Transactions on Software Engineering (TSE), Execution time # Atoms # Interactions 15(9):1053–1065, 1989. Orig. S/R Orig. S/R Cent. Decent. [2] Rajive Bagrodia. A distributed algorithm to B∅ 10 11 20 28 15.2 11.0 implement n-party rendevouz. In Foundations of B˜∅ 30 31 70 148 12.0 5.9 Software Technology and Theoretical , Seventh Conference (FSTTCS), pages B˜π 30 31 70 154 5.4 2.8 138–152, 1987. [3] A. Basu, S. Bensalem, D. Peled, and J. Sifakis. Priority scheduling of distributed systems based on More importantly, the distributed execution of B˜∅ is al- model checking. In Computer Aided Verification most twice faster than B∅. This is due to the fact all time (CAV), pages 79–93, 2009. consuming in B∅ are on interactions, which [4] A. Basu, P. Bidinger, M. Bozga, and J. Sifakis. are all executed on the same processor (the one hosting the Distributed semantics and implementation for systems scheduler). When switching to B˜∅, these interactions are with interaction and priority. In Formal Techniques executed by the manager components and, hence, run con- for Networked and Distributed Systems (FORTE), currently on different processors. pages 116–133, 2008. ˜ Furthermore, the model Bπ runs faster than B∅ on a cen- [5] A. Basu, M. Bozga, and J. Sifakis. Modeling tralized platform. In this scenario, priorities enforce a better heterogeneous real-time components in BIP. In scheduling – we first load the discs that are often used and Software Engineering and (SEFM), we do not perform an unload if a reader has something left pages 3–12, 2006. to read – and thus reduce the total execution time. Again, [6] B. Bonakdarpour, M. Bozga, M. Jaber, J. Quilbeuf, switching to decentralized execution gives almost twice bet- and J. Sifakis. Automated conflict-free distributed ter results, as (time consuming) interactions are now running implementation of component-based models. In IEEE concurrently. Symposium on Industrial Embedded Systems (SIES), pages 108 – 117, 2010. 6. CONCLUSION [7] B. Bonakdarpour, M. Bozga, M. Jaber, J. Quilbeuf, and J. Sifakis. From high-level component-based In this paper, we proposed an automated method to derive models to distributed implementations. In ACM correct distributed implementation from high-level International Conference on Embedded Software component-based models encompassing prioritized multiparty (EMSOFT), pages 209–218, 2010. interactions. Our method consists of three steps: (1) one [8] K. M. Chandy and J. Misra. The drinking transformation to deprioritize the initial model, (2) a trans- philosophers problem. ACM Transactions on formation from [6,7] that generates a distributed model from Programming Languages and Systems (TOPLAS), the deprioritized model by resolving interaction conflicts, 6(4):632–646, 1984. and (3) a final transformation from the distributed model [9] K. M. Chandy and J. Misra. Parallel program design: into C++ code. All steps preserve observational equiva- a foundation. Addison-Wesley Longman Publishing lence between the input and output models. We illustrated Co., Inc., Boston, MA, USA, 1988. our approach using a non-trivial example and presented en- [10] Marcin Jurdzinski. Small progress measures for solving couraging experimental results. parity games. In Symposium on Theoretical Aspects of There exist several research directions for future work. Computer Science (STACS), pages 290–301, 2000. First, more rigorous and deeper case studies and experi- [11] R. Milner. Communication and concurrency. Prentice ments are needed to completely understand the overheads Hall International (UK) Ltd., Hertfordshire, UK, 1995. introduced by our transformations. Since deprioritization [12] N. Mittal and P. K. Mohan. A priority-based is an independent step of our method and is isolated from distributed group algorithm when conflict resolution (i.e., step two), one can study the over- group access is non-uniform. Journal of Parallel head of each step separately. Another direction is to devise Distributed Computing, 67(7):797–815, 2007. a committee coordination algorithm for conflict resolution [13] J. Parrow and P. Sj¨odin. Multiway synchronizaton that takes priority issues into account. This allows us to in- verified with coupled simulation. In International corporate such an algorithm directly in our 3-tier model [7]. Conference on Concurrency Theory (CONCUR), This approach can potentially have less overhead than the pages 518–533, 1992. one presented in this paper. Finally, one can speed-up dis- [14] J. A. P´erez, R. Corchuelo, and M. Toro. An tributed execution of models with priorities by detecting dis- order-based algorithm for implementing multiparty abled interactions as early as possible. Such detection can synchronization. Concurrency and Computation: benefit from knowledge-based methods (e.g., [3]). Practice and Experience, 16(12):1173–1206, 2004.