CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... ·...

35
CS:4980 Foundations of Embedded Systems Copyright 20014-16, Rajeev Alurand Cesare Tinelli. Created by Cesare Tinelli at the University of Iowa from notes originally developed by Rajeev Alur at the University of Pennsylvania. These notes are copyrighted materials and may not be used in other course settings outside of the University of Iowa in their current form or modified form without the express written permission of one of the copyright holders. During this course, students are prohibited from selling notes to or being paid for taking notes by any person or commercial firm without the express written permission of one of the copyright holders. Timed Model

Transcript of CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... ·...

Page 1: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

CS:4980FoundationsofEmbeddedSystems

Copyright 20014-16, Rajeev Alur and Cesare Tinelli. Created by Cesare Tinelli at the University of Iowa from notes originally developed by Rajeev Alur at the University of Pennsylvania. These notes are copyrighted materials and may not be used in other course settings outside of the University of Iowa in their current form or modified form without the express written permission of one of the copyright holders. During this course, students are prohibited from selling notes to or being paid for taking notes by any person or commercial firm without the express written permission of one of the copyright holders.

TimedModel

Page 2: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ModelsofReactiveComputationq Synchronousmodel

§ Componentsexecuteinasequenceofdiscreteroundsinlock-step§ Computationwithinaround:Executealltasksinanorderconsistent

withprecedenceconstraintsq Asynchronousmodel

§ Speedsatwhichdifferentcomponentsexecuteareindependent§ Computationwithinastep:Executeasingletaskthatisenabled

q Continuous-timemodelfordynamicalsystem§ Synchronous,butnowtimeevolvescontinuously§ Executionofsystem:Solutiontodifferentialequations

q Timedmodel§ Likeasynchronousforcommunicationofinformation§ Canrelyonglobaltimeforcoordination

Page 3: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ExampleTimedModel

clockx:=0off

press?–>x:=0dim bright

press?

(press&x>=1)?(press&x<=1)?

Initialstate: (mode=off,x=0)Timedtransition:(off,0)–0.5–>(off,0.5)Inputtransition: (off,0.5)–press?–> (dim,0)Timedtransition:(dim,0)–0.8–> (dim,0.8)Inputtransition: (dim,0.8)–press?–> (bright,0.8)Timedtransition:(dim,0.8)–1–> (dim,1.8)Inputtransition: (dim,1.8)–press?–> (off,1.8)

Page 4: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ExampleTimedModel

q Clockvariables§ Testsandupdatesinmode-switcheslikeothervariables§ New: Duringatimedtransitionofdurationd,thevalueof

clockvariablesincreasesbyanamountequaltodq Timingconstraint:Settingx to0 foroff–>dimandguardx<=1

fordim–>bright specifiesthattimingofthesetwotransitionsis<=1 apart

clockx:=0off

press?–>x:=0dim bright

press?

(press&x>=1)?(press&x<=1)?

Page 5: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

Example:TimedBuffer

bool in bool out

q Bufferwithaboundeddelay

q Behavior:Inputreceivedonchannelinistransmittedonoutputchanneloutafteradelayofd,withLB<=d<=UB(i.e.weknowlowerandupperboundsonthisdelay)

Page 6: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ModelingTimedBuffer

clocky:=0

in?–>x:=in;y:=0

in?

q Modeindicateswhetherthebufferisfullornotq Statevariablex remembersthelastinputvaluewhenbufferisfullq Clockvariabley tracksthetimeelapsedsincebufferfilledupq Whenbufferisfull,inputeventsareignoredq Guardy>=1ensuresthatatleast1 timeunitelapsesinmodeFull

Howtoensurethatmode-switchfromFull toEmpty isexecutedbeforeclocky exceedstheupperbound1?

FullEmpty

y>=1–>out:=x

Page 7: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ClockInvariants

q Theconstrainty<=1associatedwithmodeFull isaclockinvariant

q Atimedtransitionofdurationd isallowedonlyiftheclockinvariantissatisfiedfortheentiredurationofthetransition§ (Full,x,0.8)–0.7–> (Full,x,1.5) allowed§ (Full,x,0.8)–1.4–> (Full,x,2.2) disallowed

q Clockinvariantstolimithowlongaprocessstaysinamode

clocky:=0

in?–>x:=in;y:=0

in?

Fully<=2

Empty

y>=1–>out:=x

Page 8: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ExamplewithTwoClocks

clockx,y:=0

in?–> x:=0

q Inputevent:inq Outputevents:out1,out2q Twoclockvariables:x,yq Astimepasses,bothclocksincrease(andatthesamerate)q Sampletimedtransitionsfromstate(mode,x,y)=(Wait2,0.8,0):

(Wait2,0.8,0)–0.3–>(Wait2,1.1,0.3)–0.72–>(Wait2,1.82,1.02)

Wait1x<=1

Idle

y>=1–> out2!

out1!; y:=0Wait2x<=2

Page 9: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

TwoClockExample

q Clockx trackstimeelapsedsincethelastinputeventq Clocky trackstimeelapsedsincetheoutputeventq Whatisthebehaviorofthismodel?q Ifinputeventoccursattimet,theprocessissuesanoutputeventon

channelout1 attimet’ withintheinterval[t,t+1],andthenonchannelout2 attimet’’withintheinterval[t’+1,t+2]

clockx,y:=0

in?–> x:=0Wait1x<=1

Idle

y>=1–> out2!

out1!; y:=0Wait2x<=2

Page 10: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ExampleSpecification

q ConsideratimedprocesswithInput:eventx Output:eventy, eventz

q Desiredbehavior§ Foreachinput,producebothoutputevents§ Timedelaybetweenx? andy! isintheinterval[2,4]§ Timedelaybetweenx? andz! isintheinterval[3,5]§ Ignorelaterinputsreceivedintheseintervals

Page 11: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

DefinitionofTimedProcessq AtimedprocessTP consistsof

1. AnasynchronousprocessP,wheresomeofthestatevariablescanbeoftypeclock (rangingnon-negativereals)

2. AclockinvariantCI,aBooleanexpressionoverP’sstatevariables

q Inputs,outputs,states,initialstates,internalactions,inputactions,andoutputactionsexactlyasintheasynchronousmodel

q Notation:Forastates andtimet,lets+t denotethestatesuchthat§ (s+t)(x)=s(x)+t foreveryclockvariablex,and§ (s+t)(y)=s(y)foreverynon-clockvariabley

q Timedactions:Givenastates andatimed>0,s–d–>s+d isatransitionofdurationd aslongasthestates+t satisfiesinvariantCIforallt in[0,d]Note: Ifaclock-invariantisaconvexconstraintthenitissufficienttocheckthattheend-statess ands+d satisfyCI

Page 12: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

CompositionofProcesses

bool in boolout1

q Howtoconstructtimedprocesscorrespondingtothecompositionofthetwoprocesses?

q Whatarethepossiblebehaviorsofthecompositeprocess?

boolout2

TimedBuf1

TimedBuf2

Page 13: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

CompositionofTimedProcesses

clocky1:=0

in?–> x1:=in;y1:=0

in?

Fully1<=UB1Empty

y1>=LB1–> out1:=x1TimedBuf1

clocky2:=0

in?–> x2:=in;y2:=0

in?

Fully2<=UB2

Empty

y2>=LB2–> out2:=x2TimedBuf2

Thecompositeprocesshasfourmodes:(Empty,Empty),(Empty,Full),(Full,Empty),(Full,Full),

Page 14: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

CompositionofTimedProcesses

(mode=EF=>y2<=UB2)&(mode=FF=>y1<=UB1&y2<=UB2)&(mode=FE=>y1<=UB1)

Page 15: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

CompositionofProcesses

q IfUB1<LB2thenout1 guaranteedtooccurbeforeout2§ Implicitcoordinationbasedonboundsondelays

q Isitpossibletoobservetwoout1eventswithoutaninterveningout2event?§ Dependsonrelativemagnitudesofbounds(needtiminganalysis!)

bool in boolout1

boolout2

TimedBuf1

TimedBuf2

Page 16: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

DefinitionofParallelCompositionq ConsidertimedprocessesTP1 =(P1,CI1)andTP2 =(P2,CI2)

q WhenistheparallelcompositionTP1 |TP2 defined?§ ExactlywhentheasynchronousparallelcompositionP1 |P2

isdefined(thatis,whentheoutputsofthetwoaredisjoint)

q TP1|TP2=(P1 |P2,CI1 &CI2)§ AsynchronouscompositionofP1andP2 definestheinternal,

inputandoutputactionsofthecomposite§ Conjunctionoftheclock-invariantsdefinestheclock-

invariantofthecomposite

q Consequence:Thecompositeprocesscanallowatimedactionofdurationd exactlywhenboth TP1 andTP2 canwaitfortimed

Page 17: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

BlockDiagrams

q Componentscanbetimedprocessesnow§ Operation:instantiation(input/outputvariablerenaming),parallel

composition,andvariablehiding

q Astepofthecompositesystemiseither1. Aninternalstepofoneofcomponents2. Acommunication(input/output)stepinvolvingrelevantsender

andreceivers3. Atimedstepinvolvingallthecomponents

Page 18: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

TimedModel

q Timedmodelissometimescalledthesemi-synchronousmodel(mixofasynchronousandsynchronous)

q Definitions/conceptsthatcarryovernaturallyfromthosemodels:§ Executionsofatimedprocess§ Transitionsystemassociatedwithatimedprocess§ Safety/liveness requirements

q Distributedcoordinationproblems:howcanweexploittheknowledgeoftimingdelaystodesignprotocols?

Page 19: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

Recall:SharedMemoryAsynchronousProcesses

q ProcessesP1andP2communicatebyreading/writingsharedvariables

q Eachsharedvariablecanbemodeledasanasynchronousprocess§ Stateofeachsuchprocessisthevalueofcorrespondingvariable§ Inimplementation,sharedmemorycanbeaseparatesubsystem

q Readandwritechannelbetweeneachprocessandeachsharedvariable§ Towritex,P1synchronizeswithxonx.write1channel§ Toreadx,P2synchronizeswithx onx.read2channel

x.write1

P1

x

y

P2y.write1

y.read2

y.write2

x.read2

x.write2

y.read1

x.read1

Page 20: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

SharedMemoryProgramswithAtomicRegisters

AtomicReg nat x:=0

ProcessP1

nat y1:=0

y1:=x

x:=y1+1

ProcessP2

nat y2:=0

y2:=x

x:=y2+1

Declarationofsharedvariables+codeforeachprocess

Keyrestriction:Eachstatementofaprocesseither

changeslocalvariables,readsasinglesharedvar,orwritesasinglesharedvar

Executionmodel:executeonestepofoneoftheprocesses

Whatifweknewlowerandupperboundsonhowlongareadorawritetakes?Couldwesolvecoordinationproblemsbetter?

Page 21: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

AsynchronousExecutionModel

nat x:=0;y:=0

Ax:x:=x+1

Ay:y:=y+1

q TasksAx andAy executeinanarbitraryorder

q Foreverypossiblechoiceofnumbersm andn,thestate(m,n)isreachable

q Recall:Fairnessassumptionscanbeusedtoruleoutexecutionswhereoneofthetasksisignoredforever(althoughthisdoesnotaffectthesetofreachablestates)

q Whatifweknowhowlongeachoftheseincrementstake?

(x,y)

Page 22: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

TimedIncrements

q TaskAx incrementsx,andthistakesbetween1 to2 timeunits

q TaskAy incrementsy,andthisalsotakesbetween1 to2 timeunitsq Twotasksexecuteinparallel,asynchronously,buttimingintroduces

loosecoordinationq Whichstatesarereachable?Whatistherelationshipbetweenmand

n sothatthestate(m,n)isreachable?

clocku:=0nat x:=0

u>=1–>x:=x+1;u:=0

u<=2

clockv:=0nat y:=0

v>=1–> y:=y+1;v:=0

v<=2

Page 23: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

q Safetyrequirement:processesshouldnotbothbeincriticalsectionsimultaneously(canbeformalizedusinginvariants)

q Absenceofdeadlocks:ifanyprocessistryingtoenter,thensomeprocessshouldbeabletoenter

ProcessP1

EntryCode

CriticalSection

Tobedesigned

ProcessP2

EntryCode

CriticalSection

MutualExclusionProblem

Page 24: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

MutualExclusion:IncorrectSolutionAtomicReg {0,1,2}Turn:=0

Whatistheproblem?

ProcessP1

Idle Try1Turn=0?

Crit

else

Turn:=0

Try2Turn:=1

ProcessP2

Idle Try1Turn=0?

Crit

else

Turn:=0

Try2Turn:=2

Page 25: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

Timing-basedMutualExclusion

1. Beforeenteringcriticalsection,readthesharedvariableTurn2. IfTurn!=0thengotostep1andtryagain3. IfTurn=0thensetTurn toyourID

Proceedingdirectlytocriticalsectionisaproblem(sincetheotherprocessmayalsohaveconcurrentlyreadTurn tobe0,andupdatingTurn toitsownID).Solution:

4. Delayandwaittillyouaresurethatconcurrentwritesarefinished

5. ReadTurn again:ifTurn equalsyourownIDthenproceedtocriticalsection;otherwise,gotoStep1andtryagain

6. Whendonewithcriticalsection,setTurn backto0

Page 26: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

Fisher’sMutualExclusionProtocol

AtomicReg Turn:=0

Idlenat y,clockx

Testy:=Turn Set

Delay

y=0–> Turn:=myID

y!=0?

;x:=0x<=Δ1

Timingassumption:writingTurn takesatmostΔ1

Checkx>=Δ2 –> y:=Turn x:=0

Crit

y=myID ?

y!=myID ?Turn:=0

Whydoesthiswork?

WaitforatleastΔ2 timeunits,andreadTurn again

Page 27: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

PropertiesofTimedFisher’sProtocolq AssumingΔ2 >Δ1,thealgorithmsatisfies:

§ Mutualexclusion:Twoprocessescannotbeincriticalsectionsimultaneously

§ Deadlockfreedom:Ifaprocesswantstoentercriticalsectionthensomeprocesswillentercriticalsection

q Protocolworksforarbitrarilymanyprocesses(notjust2)§ Incontrast,intheasynchronousmodel,mutualexclusion

protocolforN processesislotmorecomplexthanPeterson’salgorithm

q Exercise:Doestheprotocolsatisfythestrongerpropertyofstarvationfreedom(ifaprocesswantstoentercriticalsectiontheniteventuallywill)?

q Exercise:IfΔ2 <=Δ1 doesmutualexclusionhold?Deadlockfreedom?

Page 28: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

TimedCommunication

q Supposeasenderwantstotransmitasequenceofbitstoareceiverconnectedbyacommunicationbus

q Naturalstrategy:Dividetimeintoslots,andineachslottransmitabitusinghigh/lowvoltagevaluestoencode0/1

q Manchesterencoding:0 encodedasafallingedge,and1encodedasarisingedge

Page 29: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

TimedCommunicationChallenges

q Senderandreceiverknowthedurationofeachtimeslot,but…q Receiverdoesnotknowwhenthecommunicationbegins

§ Whenidle,thevoltageissettolowq Receivercannot reliablydetectfallingedgesq Senderandreceiverclocksaresynchronizedimperfectlyduetodrift

§ Whenaclockx is1,actualelapsedtimeisininterval[1-ε,1+ε]§ Sinceinthetimedmodelclocksareconsideredtobeperfect,

wecancapturethiserrorbyusingx<=1+ε insteadofx<=1,and1-ε <=xinsteadof1<=x

q Addressingthechallenges:§ Allmessagesstartwith1 andendwith00§ Processesusetiminginformationtotransmit0s

Page 30: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

AudioControlProtocol

q ProtocoldevelopedbyPhilipstoreliablytransmitmessagesinpresenceofimperfectclocks

q Designlogicforreceivertomapmeasureddelaysbetweensuccessiveraisingedgestosequenceofbits

q Verification:Provethatmessagetransmissionisreliableforagivendriftrateε

q Optimization:Findthelargestskewvaluethattheprotocoltolerates

Page 31: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

AudioControlSystem

Page 32: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

SenderProcess

Page 33: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ReceiverProcess

Page 34: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

ExecutionExample

Page 35: CS:4980 Foundations of Embedded Systemshomepage.cs.uiowa.edu/~tinelli/classes/4980/Spring... · Definition of Timed Process q A timed process TPconsists of 1. An asynchronous process

Credits

NotesbasedonChapter7of

PrinciplesofCyber-PhysicalSystemsbyRajeevAlurMITPress,2015