1 / 27

Parametric Heap Usage Analysis for Functional Programs

Parametric Heap Usage Analysis for Functional Programs. Leena Unnikrishnan Scott D. Stoller. Motivation. Goal: upper bound on live heap usage. Understanding memory usage of programs. Guiding automatic methods, e.g., help program transformations select space-efficient programs.

kapila
Télécharger la présentation

Parametric Heap Usage Analysis for Functional Programs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Parametric Heap Usage Analysis for Functional Programs Leena Unnikrishnan Scott D. Stoller

  2. Motivation • Goal: upper bound on live heap usage. • Understanding memory usage of programs. • Guiding automatic methods, e.g., help program transformations select space-efficient programs. • Required for time analysis: running times depend on memory allocation, garbage collection, etc. • Determining/verifying space usage of embedded applications.

  3. Challenges • Determine live heap usage over entire program execution, and over multiple execution paths. • Unlike running times, live heap space increases and decreasesduring execution. • Static determination of liveness of objects.

  4. Overview • Input: Program P in a functional language with lists. • Output: worst-case live heap usage of P expressed in terms of sizes of P’s inputs. • Example Input: program reverse(ls)to reverse a list Output: Max live heap usage =2|ls| - 1, if |ls| > 0 • Method • Transform functions in P into bound functions that describe their heap space usage and related metrics. • Simplify and rewrite bound functions into recurrences. • Solve recurrences into closed forms.

  5. Outline • Bound functions • Recurrences containing the max operator • Soundness checks for R functions • Examples • Related work • Conclusion

  6. Bound Functions • Construct three bound functions for each function f in program P. • Arguments of each bound function for f(v1,…,vn) are the sizes v1s,…,vns, of v1,…,vn. • Size of a boolean or number: its value • Size of a flat list: its length

  7. Bound Functions For f(v1,…,vn) in the input program, construct • Live heap space bound function,Sf(v1s,…,vns): upper bound on live heap use of f, for all possible arguments v1,…,vn, of sizes v1s,…,vns. • New result-space bound function,Nf(v1s,…,vns,v1w,…,vnw): upper bound on amount of newly allocated space in the result of f, for all possible arguments v1,…,vn, of sizes v1s,…,vns. viw is the amount of new space in vi. • “New” is w.r.t. start of evaluation of call to f. • Size bound function,Rf(v1s,…,vns): upper bound on size of result of f, for all possible arguments v1,…,vn, of sizes v1s,…,vns.

  8. reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) Srev(lss) = if lss=0 then 0 else max(Srev(lss-1), Nrev(lss-1)+1, Nrev(lss-1)+1+Sapp(Rrev(lss-1))) Live Heap Space Bound Functions • For f(v1,…,vn)= e, Sf(v1s,…,vns) = S[e]. • Example clauses in the definition of S[.]. S[v]= 0, S[cons(e1, e2)]= 1 + max(S[e1], S[e2]) S[f(e1, e2)]= max(S[e1], N[e1] + S[e2], N[e1] + N[e2] + Sf(R[e1], R[e2]))

  9. reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) Rrev(lss) = if lss=0 then 0 else Rapp(Rrev(lss-1), 1) Size Bound Functions • For f(v1,…,vn) = e, Rf(v1s,…,vns) = R[e]. • Example clauses in the definition of R[.]. R[v]= vs , R[cons(e1, e2)]= 1 + R[e2] R[if p then e1 else e2] max(R[e1], R[e2]), if pcontains “car” if R[p] then R[e1] else R[e2] R[f(e1,…,en)]= Rf(R[e1],…, R[en]) = {

  10. New Result-Space Bound Functions • For f(v)in the input, the N function is Nf(vs, vw). • f(e’)in input program yields Nf(R[e’], N[e’]) in bound functions. • “New” is w.r.t. the start of evaluation of f(e’). • vw is the part of v created in e’. • Nf(…)is the part of the result created in e’or the body of f. • For f(v)= e, Nf(vs, vw) = N[e]. • Example clauses in the definition of N[.]. N[v]= vw , N[cons(e1, e2)]= 1 + N[e2] N[f(e1,…,en)]= Nf(R[e1],…,R[en],N[e1],…,N[en])

  11. reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) Nrev(lss , lsw) = if lss=0 then 0 else Napp(Rrev(lss-1), 1, Nrev(lss-1, 0), 1) New Result-Space Bound Functions

  12. Outline • Bound functions • Recurrences containing the max operator • Soundness checks for R functions • Examples • Related work • Conclusion

  13. Composite Recurrences • Bound functions are simplified into recurrences that may contain max. We call these composite recurrences. • Example: For insert in insertion sort, Rins(n) = max(1+n, 1+Rins(n-1)), if n>0 • Solve composite recurrences using a library of solution templates.

  14. { • c1,…,cj+1, if n = i,…,(i+j), respectively • max(e1(n), e2(n) + aT(n-b)), otherwise T(n) = Solving Composite Recurrences: Example For a recurrence of the form where, the arguments of max are equal for (a) “base cases”, i.e., e1(n) = e2(n) + a·c(n-b-i+1), for n in[i+j+1, i+j+b] (b) all other values of n, i.e. e1(n) = e2(n) + a·e1(n-b), for n>(i+j+b) the solution is T(n) = e1(n), for n>(i+j).

  15. Outline • Bound functions • Recurrences containing the max operator • Soundness checks for R functions • Examples • Related work • Conclusion

  16. { } 0, if n=0 max(1+Rf(n-1), Rf(n-1)), o.w. = max(n, …, 0) Rf(n) = Soundness Checks on Uses of R Functions • Intuition: It is not necessarily the object with the largest size that leads to worst-case live heap usage. • R function results are arguments to S and N functions. • Consider the R function, Rf(n), of greaterlistin quicksort. Rf(n)is the maximum of a set of values. Rf is a complex R function.

  17. { • c1,…,cj+1, if n = i,…,(i+j), respectively • e(n) + aT(R(n)), otherwise T(n) = Soundness Checks on Uses of R functions Using the result of Rf(n) as an argument to a bound function, say Sg, yields a true upper bound down the line, only if the following hold: • Case 1: If Sg(Rf(n))is a recursive call in the body of Sg, then using the result of Rf(n) in the definition of Sg, yields the maximum result. E.g., this holds for recurrences of the form where • base values of T are monotonically increasing • largest base value < value at smallest non-base input • e(n) is monotonically increasing w.r.t. n • Case 2: check if the solution of Sgis monotonically increasing w.r.t. its single argument.

  18. Improving Solvability of Recurrences • Eliminate unused arguments from bound functions. • Simplifies recurrences. May eliminate dependence on other recurrences. • Use invariants to simplify max expressions. • May simplify composite recurrences or make them regular. • Example: Sf(e1,…,en)  Nf(e1,…,en,0,…,0). So, max(Sf(e1,…,en), Nf(e1,…,en,0,…,0)) = Sf(e1,…,en). • Use simpler, approximate definitions of N functions. precise:Nf(v1s,…,vns, v1w,…,vnw) = N[e] approx:Nf(v1s,…,vns) = Rf(v1s,…,vns) In our examples, even with this simplification, we get tight bounds on worst-case space usage.

  19. Examples • Derives linear, quadratic, and exponential bounds. • Bounds are tight (exact) for all examples except quicksort.

  20. Related Work • Timing analysis using recurrence relations [Wegbreit ‘75, LeMetayer ‘88]. • Static prediction of heap space [Hofmann & Jost ‘03] • Applies to a linearly-typed functional language. • Derives linear heap bounds. • Parametric prediction of heap memory requirements [Braberman et al ’08] • Java-like language. • Region-based memory management models heap recovery. • Derives polynomial bounds.

  21. Related Work • Memory resource bounds for low-level programs [Chin et al ‘08]: • Assembly-like language. • Explicit deallocation to model heap recovery. • Derives linear bounds. • Cost analysis [Albert et al ‘07]: • Java bytecode. • Escape analysis to model heap recovery. • Analysis for live heap at ISMM ‘09!

  22. Conclusion • Automatic, accurate analysis of live heap usage of functional programs. • Framework of bound functions that describe live heap usage of input programs. • Methods to simplify and solve composite recurrences. • Soundness checks involving monotonicity and monotonicity–like properties of recurrences. • Results are at source-code level and are easy to understand. • Results are not restricted to any complexity class.

  23. Thank You!

  24. Extensions to Handle Other Data Types • Specialize R functions and size arguments to each data type. • Binary Trees • Possible size measures: height, total. • R functions: Rheight, Rtotal • Extend S, N, R transformations to new data type. Rheight[tree(e, el, er)] = 1 + max(Rheight[el], Rheight[er]) Rtotal [tree(e, el, er)] = 1 + Rtotal[el]+ Rtotal[er] • Live heap usage of a given program may be better represented in terms of one specific size measure, than others.

  25. Bound Functions Sis(lss) = if lss=0 then 0 else max(Sis(lss-1), Nis(lss-1) + Sins(Ris(lss-1))) Nis(lss) = if lss=0 then 0 else Nins(Ris(lss-1)) Ris(lss) = if lss=0 then 0 else Rins(Ris(lss-1)) Sins(lss) = if lss=0 then 1 else max(1, 1+Sins(lss-1)) Nins(lss) = Rins(lss) Rins(lss) = if lss=0 then 1+0 else max(1+lss, 1+Rins(lss-1)) insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Example Input Program

  26. { Recurrences/Equations 0, if lss=0 max(Sis(lss-1), 2lss-1), otherwise insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Sis(lss) = { Nis(lss) = lss 0, if lss=0 1+ Ris(lss-1), otherwise Ris(lss) = { 1, if lss=0 max(1, 1+Sins(lss-1)), otherwise Sins(lss) = Nins(lss) =1+lss { Rins(lss) = 1, if lss=0 max(1+lss, 1+Rins(lss-1)), otherwise Example Input Program

  27. { Input Program 0, if lss=0 2lss-1, otherwise Sis(lss) = insertion-sort(ls) = if null?(ls) then nil else insert(car(ls), insertion-sort(cdr(ls))) insert(x, ls) = if null?(ls) then cons(x, nil) else if lesseroreq?(x, car(ls)) then cons(x, ls) else cons(car(ls), insert(x,cdr(ls))) Nis(lss) = lss Ris(lss) = lss Sins(lss) = 1+lss Nins(lss) = 1+lss Rins(lss) = 1+lss Example Closed Forms

More Related