DEPENDEX 1Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05...

28
DEPENDEX 1 Synthesis of parametric specifications of dynamic memory utilizat ion. BGY. FTFJP'05 Synthesizing parametric specifications of dynamic memory utilization in object-oriented programs Víctor Braberman: DC, Víctor Braberman: DC, FCEN, UBA, Argentina Diego Garbervetsky : DC, FCEN, UBA, Argentina Sergio Yovine: Verimag. France Dependable Software Research Group DEPENDEX
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    224
  • download

    0

Transcript of DEPENDEX 1Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05...

DEPENDEX 1Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Synthesizing parametric specifications of dynamic

memory utilization in object-oriented programs

Víctor Braberman: DC, Víctor Braberman: DC, FCEN, UBA, Argentina Diego Garbervetsky: DC, FCEN, UBA, Argentina Sergio Yovine: Verimag. France

Dependable Software Research Group DEPENDEX

DEPENDEX 2Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

MotivationMotivationvoid m1(int k) { for(i=1;i<=k;i++)

{ a = new A(); m2(i); }}

void m2(int n) { for(j=1;j<=n;j++)

{ b = new B(); }}

How much dynamic How much dynamic memory is allocated memory is allocated when when method method m1m1 is invoked is invoked??

Not a trivial task!

k times new A() & (1+2+…+k) times new B() memAlloc(m1)=size(A) * k + size(B) * ( ½k2+½k)

DEPENDEX 3Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

ContextContext

Problem undecidable in generalProblem undecidable in general Impossible to find an exact expression Impossible to find an exact expression

of dynamic memory allocation even of dynamic memory allocation even knowing program inputs knowing program inputs

Several techniques for functional Several techniques for functional languageslanguages Usually linear upper boundsUsually linear upper bounds

Less explored for Object Oriented Less explored for Object Oriented programsprograms

DEPENDEX 4Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Our workOur work

Given a method m(pGiven a method m(p11,..,p,..,pnn)) memAlloc(m): symbolic expression (a memAlloc(m): symbolic expression (a

polynomial) in terms of ppolynomial) in terms of p11,…,p,…,pnn over- over-approximating the amount of dynamic approximating the amount of dynamic memory allocated by any run starting at mmemory allocated by any run starting at m

A general technique to find A general technique to find non-linear non-linear parametric upper- boundsparametric upper- bounds of dynamic memory of dynamic memory utilizationutilization

DEPENDEX 5Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Key idea: Counting visits to Key idea: Counting visits to statements that allocates statements that allocates

memorymemory

For linear invariants, # of integer solutions = For linear invariants, # of integer solutions = # of integer points = # of integer points = Ehrhart polynomial Ehrhart polynomial ((size(C) * ( ½k2+½k)))

for(i=0;i<n;i++) for(j=0;j<i;j++)

• new C() Dynamic Memory allocations Dynamic Memory allocations number of number of visitsvisits to to

newnew statements statements number of number of possible variable assignmentspossible variable assignments at at

statement’s control locationstatement’s control location number of integer solutionsnumber of integer solutions of a predicate of a predicate

constraining variable assignments at its control constraining variable assignments at its control location (i.e. an location (i.e. an invariantinvariant))

{0≤ i < n, 0≤j<i}: a set {0≤ i < n, 0≤j<i}: a set of constraints describing a of constraints describing a iteration spaceiteration space

i

DEPENDEX 6Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Our approachOur approach1.1. Identify every allocation site (new statement) Identify every allocation site (new statement)

reachable from the method under analysis reachable from the method under analysis (MUA)(MUA)

2.2. Generate invariants describing possible Generate invariants describing possible variables assignments at each allocation site variables assignments at each allocation site (the “iteration space”)(the “iteration space”)

3.3. Count the number solutions for the invariant in Count the number solutions for the invariant in terms of MUA parameters (# of visits to the terms of MUA parameters (# of visits to the allocation site)allocation site)

4.4. Adapt those expressions to take into account the Adapt those expressions to take into account the size of object allocated (their types)size of object allocated (their types)

5.5. Sum up the resulting expression for each Sum up the resulting expression for each allocation siteallocation site

DEPENDEX 7Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Running ExampleRunning Examplevoid m0(int mc) {void m0(int mc) {1:1: m1(mc);m1(mc);2:2: B[] m2Arr=m2(2 * mc);B[] m2Arr=m2(2 * mc);}}

void m1(int k) {void m1(int k) {3:3: for (int i = 1; i <= k; i++) {for (int i = 1; i <= k; i++) {4:4: A a = A a = new A()new A();;5:5: B[] dummyArr= m2(i);B[] dummyArr= m2(i);

}}}}

B[] m2(int n) {B[] m2(int n) {6:6: B[] arrB = B[] arrB = new B[n]new B[n];;7:7: for (int j = 1; j <= n; j++) {for (int j = 1; j <= n; j++) {8:8: B b = B b = new B()new B();;

}}9:9: return arrB;return arrB;}}

DEPENDEX 8Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Step 1:Identifying Step 1:Identifying allocation sitesallocation sites

1: m1(mc):

2: m2(2*mc) :

5: m2(i) (i times):

m0

m1

4: new A

m2

6: new B[]8:new B

m2 is called at least twice

2 static traces for 6:newB[] and 8:new B

Distinguish program locations Distinguish program locations not only by a “methodnot only by a “method-local” local” control location but also by a control location but also by a call chaincall chain

Creation SiteCreation Site (cs=π.l) = a path (cs=π.l) = a path π from the MUA to a π from the MUA to a newnew statement at l.statement at l.

Denotes a Denotes a statementstatement and a and a call call stackstack..

Example: m0.2Example: m0.2.m2.6.m2.6, cs for , cs for statement new B[] with statement new B[] with stack (m0.2).stack (m0.2).

Creation sites reachable from m0:CSm0 = {m0.1.m1.4, m0.1.m1.5.m2.6, m0.1.m1.5.m2.8, m0.2.m2.6, m0.2.m2.8}

DEPENDEX 9Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

void m0(int mc) {void m0(int mc) {1:1: m1(mc);m1(mc);2:2: B[] m2Arr=m2(2 * mc);B[] m2Arr=m2(2 * mc);}}

void m1(int k) {void m1(int k) {3:3: for(int i = 1; i <= k; i++){for(int i = 1; i <= k; i++){4:4: A a = A a = new A()new A();;5:5: B[] dummyArr= B[] dummyArr=

m2(i);m2(i);}}

}}

B[] m2(int n) {B[] m2(int n) {6:6: B[] arrB = B[] arrB = new B[n]new B[n];;7:7: for(int j = 1; j <= n; j++){for(int j = 1; j <= n; j++){8:8: B b = B b = new B();new B();

}}9:9: return arrB;return arrB;}}

Step 2:Finding invariants Step 2:Finding invariants for creation sitesfor creation sites

Im0 (m0.1.m1.5.m2.6){k=mc 1≤i≤k n=i}

Im0(m0.1.m1.5.m2.8){k=mc 1≤i≤k n=i 1≤j≤n}Im0(m0.2.m2.6){n=2*mc}

Im0(m0.2.m2.8){n=2*mc 1≤j≤n}

Im0(m0.1.m1.4){k=mc 1≤i≤k}

We need invariants involving variables We need invariants involving variables in a path through several methods in a path through several methods (appearing in the creation site)(appearing in the creation site)

Creation Site invariants can be generated using local invariants and binding the calls

DEPENDEX 10Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Step 3: Counting the Step 3: Counting the number of solutions number of solutions (in (in terms of MUA parameters)terms of MUA parameters)

Example: # of visits (in terms of m0 parameters) to

m2.8 for the stack configuration [m0.1.m1.5]? Recall: Im0(m0.1.m1.5.m2.8){k=mc 1≤i≤k

n=i 1≤j≤n} Then # of visits in terms of mc (method m0

parameter) = #{(k,i,j,n)| (k=mc 1≤i≤k n=i 1≤j≤n) }

= = ½ mc2 + ½ mc

DEPENDEX 11Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Step 4:Transforming Step 4:Transforming number of visits into number of visits into memory consumptionmemory consumption

We know how to approximate number of visits of a We know how to approximate number of visits of a creation site, but not dynamic memory allocations creation site, but not dynamic memory allocations

Example: How much memory (in terms of m0 parameters) is

allocalated by to m2.8 for the stack configuration [m0.1.m1.5]?

Recall: # of visits in terms of mc (method m0 parameter) = ½ mc2 + ½ mc

Then memory allocated is size(B)*½ mc2 + ½ mc S(m,cs): computes an upper bound of the amount S(m,cs): computes an upper bound of the amount

of memory allocated by of memory allocated by one creation siteone creation site, in , in terms of the parameters of mterms of the parameters of m Transforms #of visits into estimations of memory Transforms #of visits into estimations of memory

consumptionsconsumptions Special treatment for arrays allocations (Special treatment for arrays allocations (new T[e1]..[en]new T[e1]..[en]))

Treated as n nested loops: for(t1=0;t1<e1;t1++)…for(tn=0;tn<en;tn++) new

RefT

DEPENDEX 12Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Step 5: Summing up Step 5: Summing up expressionsexpressions

To predict the amount of memory allocated by a method m.To predict the amount of memory allocated by a method m. memAlloc(m)memAlloc(m) = computeAlloc(m,CSm) = computeAlloc(m,CSm)

For every creation site: Get an invariant, For every creation site: Get an invariant, compute the S function and sum them upcompute the S function and sum them up

memAlloc(m0)memAlloc(m0) = =S(m0,(m0,m0.1.m1.4)+S(m0,m0,m0.1.m1.5.m2.6) +S(m0,m0,m0.1.m1.5.m2.8)+S(m0,m0,m0.2.m2.6)+S(m0,m0,m0.2.m2.8 ) = = size(B) * (1/2 * mc2 + 5/2 * mc) + size(B[]) * (1/2 * mc2 + 5/2 * mc) + size(A) * mc

where

DEPENDEX 13Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

ExperimentsExperiments

Example:Class.Method memAlloc Param. #Objs Estimation Err% em3d:Bigraph.create(nN, nD) 32 (10, 5) 348 348 0

(20, 6) 808 808 0(100, 7) 4608 4608 0

(1000, 8) 52008 52008 0

(*)health: (rec): Village.createVillage(l, lab, b, s) 8 2 554 9358 240295

fft:FFT.test(n) 10 8 38 40 532 134 136 1,47

1024 4102 4104 0,05

We tested our prototype with some We tested our prototype with some JOlden and JavaGrande benchmarks.JOlden and JavaGrande benchmarks.

In general, when the amount of memory allocated is In general, when the amount of memory allocated is polynomialpolynomial , we obtained , we obtained accurate upper boundsaccurate upper boundsThe main issue is finding good invariants…The main issue is finding good invariants…

Obtained by hand

DEPENDEX 14Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Scoped-memory Scoped-memory ManagementManagement

Leveraging escape analysis, we can Leveraging escape analysis, we can compute upper bounds of memory escaping compute upper bounds of memory escaping and captured by a method (assuming a and captured by a method (assuming a region per method)region per method) memEscapes(m) = computeAlloc(m,escapes(m))memEscapes(m) = computeAlloc(m,escapes(m)) memCaptured(m)=computeAlloc(m,capture(m))memCaptured(m)=computeAlloc(m,capture(m))

Useful for RTSJUseful for RTSJ Predicting regions sizesPredicting regions sizes Predicting how much allocated memory by the Predicting how much allocated memory by the

MUA will remain uncollected after its executionMUA will remain uncollected after its execution

DEPENDEX 15Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Prototype ToolPrototype Tool

Local Invariant Generator

Java Application(bytecode or src)

Path Invariant Generator

Symbolic Pohyhedral Calculator

Local Invariants

Polynomials Evaluator

Creation Sites’ Polynomials

Path Invariants

Creation SitesFinder

Daikon (dymanic)

JInvariant (static)

Inductive Variables Analysis

Escape Analysis

DEPENDEX 16Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

ConclusionsConclusions

A technique that computes A technique that computes non-linearnon-linear parametric upper bounds of dynamic parametric upper bounds of dynamic memory allocationmemory allocation

An application to scoped memory An application to scoped memory managementmanagement Use for estimating region size in RTSJ Use for estimating region size in RTSJ

Useful for embedded systemsUseful for embedded systems Benchmarks results are promising…Benchmarks results are promising… But many challenges remain…But many challenges remain…

DEPENDEX 17Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Current and future WorkCurrent and future Work Find a symbolic upper-bound of memory Find a symbolic upper-bound of memory requiredrequired

to run a method (assuming scoped-memory to run a method (assuming scoped-memory management)management) We need to solve an optimization problem (symbolically)We need to solve an optimization problem (symbolically)

Improving precision of upper-bounds under Improving precision of upper-bounds under weaker invariantsweaker invariants [if ([if (cond)cond) then B1 else B2] statements, not capturing then B1 else B2] statements, not capturing

condcond The same for polymorphismThe same for polymorphism

Dealing with recursionDealing with recursion Automated code generation for RTSJ Automated code generation for RTSJ

Using Using memCapturedmemCaptured estimator to determine region’s estimator to determine region’s sizesize

DEPENDEX 18Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Extra MaterialExtra Material

How we compute the path invariantsHow we compute the path invariants Memory required to run a methodMemory required to run a method Improving method precisionImproving method precision Counting (more formally)Counting (more formally) Definition of function S()Definition of function S()

DEPENDEX 19Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

On computing InvariantsOn computing Invariants

We need linear invariants involving variables in a path We need linear invariants involving variables in a path through several methodsthrough several methods Strategy: we compute or annotate Strategy: we compute or annotate locallocal invariants and bind invariants and bind

themthem Our technique could deal with some patterns of Our technique could deal with some patterns of

iteration beyond integer-counter based ones.iteration beyond integer-counter based ones. for iterations over collections we introduce a virtual counter for iterations over collections we introduce a virtual counter

bounded by the collection size (i.e. bounded by the collection size (i.e. {0{0i i c.size()})c.size()}) We (try) to obtain invariants that only predicates We (try) to obtain invariants that only predicates

about inductive set of variables (roughly speaking, a about inductive set of variables (roughly speaking, a subset of variables which is enough to count the subset of variables which is enough to count the number of visits of a given statement)number of visits of a given statement) Currently we approximate inductive variables sets by Currently we approximate inductive variables sets by

combining a field sensitive live variables analysis and manual combining a field sensitive live variables analysis and manual adjustmentsadjustments

DEPENDEX 20Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Step 2:Finding invariants Step 2:Finding invariants for creation sitesfor creation sites

1: m1(mc):

2: m2(2*mc) :

5: m2(i) (i times):

m0

m1

4: new A

m2

6: new B[]8:new B

Example for: cs m0.1.m1.5.m2.8

I(m0.1){}

I(m1.5){1≤i≤k }

I(m2.8){1j≤n }

I(m0.1.m1) {k=mc } I(m1.5.m2){n=i }(bindings)

Im0(m0.1.m1.5.m2.8){k=mc 1≤i≤k n=i 1j≤n}

?

?

?

?

?

We need linear invariants involving We need linear invariants involving variables in a path through several methodsvariables in a path through several methods

We compute or annotate We compute or annotate locallocal invariants and bind theminvariants and bind them

DEPENDEX 21Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Computing invariants using Computing invariants using DaikonDaikon

DEPENDEX 22Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Memory required to run Memory required to run a methoda method

1: m1(mc):

2: m2(2*mc) :

5: m2(i) (i times):

m0

m1

4: new A

m2

6: new B[]8:new B

Knowing the amount memory Knowing the amount memory captured by a method is not enoughcaptured by a method is not enough

We must consider the We must consider the regions of the method it regions of the method it callscalls1.1. They are not in terms of They are not in terms of

MUA parametersMUA parameters

2.2. A method could be called A method could be called several times with several times with different argumentsdifferent arguments

DEPENDEX 23Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Two maximization Two maximization problemsproblems

In any run In any run only only one stack (path) one stack (path) configuration will be active (single-configuration will be active (single-threading)threading) required(m0)(mc) = max required(m0)(mc) = max

((rsize(rsize(m0.1.m1.5m0.1.m1.5,mc)+ ,mc)+ rsize(rsize(m0.1.m1.5.m2m0.1.m1.5.m2,mc), ,mc), rsize(rsize(m0.2.m2m0.2.m2,mc),mc)))

In one path a region can be created In one path a region can be created several times and have different sizesseveral times and have different sizes memCapture(m2) depends may vary memCapture(m2) depends may vary

depending on depending on i i in the path in the path m0.1.m1.5.m2m0.1.m1.5.m2 For every path, we need an expression in For every path, we need an expression in

terms of MUA parameters that maximizes terms of MUA parameters that maximizes the size of every region in the paththe size of every region in the path

1: m1(mc):

2: m2(2*mc) :

5: m2(i) (i times):

m0

m1

4: new A

m2

6: new B[]8:new B

DEPENDEX 24Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Maximizing a pathMaximizing a path

rsize(rsize(.m,p.m,pmrmr)=Maximize )=Maximize memCaptured(m)memCaptured(m) subject to I subject to Imrmr(()[P/p)[P/pmrmr]]

This is, find an expression in terms of This is, find an expression in terms of method method mrmr parameters that represents parameters that represents the maximum region for method the maximum region for method mm knowing that knowing that mm will be called with stack will be called with stack and the variables in call stack are and the variables in call stack are

constrained by the invariant constrained by the invariant IImrmr(())

DEPENDEX 25Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Improving technique Improving technique precisionprecision

computeAlloc relies on having good computeAlloc relies on having good invariants capturing “control-flow” invariants capturing “control-flow” decisions decisions

Consider this example:Consider this example:1: for(int i=1;i<=n;i++) 2: if(t(i))3: a[i] = new Integer[2*i]; {1≤i≤n t(i)}4: else5: a[i] = new Integer[10]; {1≤i≤n t(i)}

What happens if t(i) cannot be capture by the invariants?

The statements 3: and 4: will have the same invariant… And the technique will sum their upper-bounds ignoring the impossibility of visit both statements 3: and 4: in the same iteration!

DEPENDEX 26Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Improving precision Improving precision (cont…)(cont…)

How do we cope with this problem?How do we cope with this problem? Find a condition that maximizes the amount of Find a condition that maximizes the amount of

memory allocated by the statements knowing memory allocated by the statements knowing that they cannot by executed togetherthat they cannot by executed together

In the example we can In the example we can add a new restriction add a new restriction over over ii 3:{1≤i≤n i>5} 5:{1≤i≤n i≤5}

DEPENDEX 27Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Counting the number of Counting the number of solutions (more formally)solutions (more formally)

Given an invariant and a set of selected variables (parameters) Given an invariant and a set of selected variables (parameters) we can get an expression in terms of their parameterswe can get an expression in terms of their parameters It represents the number solutions to the invariant, fixing the values of It represents the number solutions to the invariant, fixing the values of

that parametersthat parameters Example: Im0(m0.1.m1.5.m2.8){k=mc 1≤i≤k n=i 1≤j≤n}

C(Im0(m0.1.m1.5.m2.8),{k,i,j,n},{mc})(mc) = = #{(k,i,j,n)| (k=mc 1≤i≤k n=i 1≤j≤n) } = = ½ mc2 + ½ mc

Theoretical Framework: Given a set of constraints such that var()=PW, the

number of solutions for fixing the values of P: C(,W, P)(p) = #{w| [W/w,P/p] }is a function in terms of P.

For polytypes, # of integer solutions = # of integer points = Ehrhart polynomial

Counting the number of solutions for an invariant for a creation site cs=π.l over approximates the number of visits of the new statement when program stack is π

i

j

DEPENDEX 28Synthesis of parametric specifications of dynamic memory utilization. BGY. FTFJP'05

Function S (more formally)Function S (more formally) C(IC(Ics,cs,W,P) approximates number of visits of a creation siteW,P) approximates number of visits of a creation site S(I,P,cs): computes an upper bound of the amount of memory S(I,P,cs): computes an upper bound of the amount of memory

allocated by a allocated by a creation sitecreation site, in terms of , in terms of P P using C(Iusing C(Ics,cs,W,P)W,P) Example for creation site Example for creation site m0.1.m1.5.m2.8(new B):

Im0(m0.1.m1.5.m2.8){k=mc 1≤i≤k n=i 1≤j≤n}, C(Im0(m0.1.m1.5..m2.8),{n,i,k,j},{mc})= ½ mc2 + ½ mc S(Im0(m0.1.m1.5.m2.8),{mc}, m0.2.m2.8) =

= size(B)*(C(Im0(m0.1.m1.5.m2.8),{n,i,k,j},{mc})=size(B)*½ mc2 + ½ mc

Adaptations performed by S(I,P,cs)Adaptations performed by S(I,P,cs)new T()new T(): Size(: Size(TT)*C(I,W,P))*C(I,W,P)new T[e1]..[en]new T[e1]..[en]: : Size(Size(T[]T[])) * C(IC(I {0≤t1<e1} … {0≤t1<e1} … {0≤tn<en}{0≤tn<en} ,W,P) ,W,P)

Simulating n nested loops: for(t1=0;t1<e1;t1++)…for(tn=0;tn<en;tn++) new T[]