Download presentation

Presentation is loading. Please wait.

Published byKylie King Modified over 2 years ago

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

2
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
3 Challenges Determine live heap usage over entire program execution, and over multiple execution paths. Unlike running times, live heap space increases and decreases during execution. Static determination of liveness of objects.

4
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
5 Outline Bound functions Recurrences containing the max operator Soundness checks for R functions Examples Related work Conclusion

6
6 Bound Functions Construct three bound functions for each function f in program P. Arguments of each bound function for f(v 1,…,v n ) are the sizes v 1 s,…,v n s, of v 1,…,v n. Size of a boolean or number: its value Size of a flat list: its length

7
7 Bound Functions For f(v 1,…,v n ) in the input program, construct Live heap space bound function, S f (v 1 s,…,v n s ) : upper bound on live heap use of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s. New result-space bound function, N f (v 1 s,…,v n s,v 1 w,…,v n w ) : upper bound on amount of newly allocated space in the result of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s. v i w is the amount of new space in v i. –New is w.r.t. start of evaluation of call to f. Size bound function, R f (v 1 s,…,v n s ) : upper bound on size of result of f, for all possible arguments v 1,…,v n, of sizes v 1 s,…,v n s.

8
8 Live Heap Space Bound Functions For f(v 1,…,v n ) = e, S f (v 1 s,…,v n s ) = S[e]. Example clauses in the definition of S[.]. S[v] = 0, S[cons(e 1, e 2 )] = 1 + max(S[e 1 ], S[e 2 ]) S[f(e 1, e 2 )] = max(S[e 1 ], N[e 1 ] + S[e 2 ], N[e 1 ] + N[e 2 ] + S f (R[e 1 ], R[e 2 ])) reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) S rev (ls s ) = if ls s =0 then 0 else max(S rev (ls s -1), N rev (ls s -1)+1, N rev (ls s -1)+1+S app (R rev (ls s -1)))

9
9 Size Bound Functions For f( v 1,…,v n ) = e, R f ( v 1 s,…,v n s ) = R[e]. Example clauses in the definition of R[.]. R[v] = v s, R[cons(e 1, e 2 )] = 1 + R[e 2 ] R[if p then e 1 else e 2 ] max(R[e 1 ], R[e 2 ]), if p contains car if R[p] then R[e 1 ] else R[e 2 ] R[f(e 1,…,e n )] = R f (R[e 1 ],…, R[e n ]) = { reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) R rev (ls s ) = if ls s =0 then 0 else R app (R rev (ls s -1), 1)

10
10 New Result-Space Bound Functions For f(v) in the input, the N function is N f (v s, v w ). f(e ) in input program yields N f (R[e ], N[e ]) in bound functions. – New is w.r.t. the start of evaluation of f(e ). – v w is the part of v created in e. – N f (…) is the part of the result created in e or the body of f. For f(v) = e, N f (v s, v w ) = N[e]. Example clauses in the definition of N[.]. N[v] = v w, N[cons(e 1, e 2 )] = 1 + N[e 2 ] N[f(e 1,…,e n )] = N f (R[e 1 ],…,R[e n ], N[e 1 ],…,N[e n ])

11
11 New Result-Space Bound Functions reverse(ls) = if null?(ls) then nil else append(reverse(cdr(ls)), cons(car(ls), nil)) N rev (ls s, ls w ) = if ls s =0 then 0 else N app (R rev (ls s -1), 1, N rev (ls s -1, 0), 1)

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

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

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

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

16
16 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, R f (n), of greaterlist in quicksort. R f (n) is the maximum of a set of values. R f is a complex R function. R f (n) = { 0, if n=0 max(1+R f (n-1), R f (n-1)), o.w. } = max(n, …, 0)

17
17 Soundness Checks on Uses of R functions Using the result of R f (n) as an argument to a bound function, say S g, yields a true upper bound down the line, only if the following hold: –Case 1: If S g (R f (n)) is a recursive call in the body of S g, then using the result of R f (n) in the definition of S g, 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 S g is monotonically increasing w.r.t. its single argument. T(n) = { c 1,…,c j+1, if n = i,…,(i+j), respectively e(n) + aT(R(n)), otherwise

18
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: S f (e 1,…,e n ) N f (e 1,…,e n,0,…,0). So, max(S f (e 1,…,e n ), N f (e 1,…,e n,0,…,0)) = S f (e 1,…,e n ). Use simpler, approximate definitions of N functions. precise: N f (v 1 s,…,v n s, v 1 w,…,v n w ) = N[e] approx: N f (v 1 s,…,v n s ) = R f (v 1 s,…,v n s ) In our examples, even with this simplification, we get tight bounds on worst-case space usage.

19
19 Examples ProgramS Function ( v s > c) Tight? reverse(ls)S rev (ls s ) = 2ls s – 1 Yes insertion-sort(ls)S is (ls s ) = 2ls s – 1 Yes selection-sort(ls)S ss (ls s ) = ls s (ls s +1)/2 Yes merge-sort(ls)S ms (ls s ) = 2ls s – 1 Yes quicksort(ls) S qs (ls s ) = 3·2 ls s -1 – 1 No longest-common-subseq(ls 1, ls 2 )S lcs (ls 1 s, ls 2 s ) = 2ls 2 s + 2 Yes string-edit-distance(ls 1, ls 2 )S se (ls 1 s, ls 2 s ) = 2ls 1 + 2ls Yes binomial-coefficient(n, m)S bc (n s,m s ) = 2m s +2, if m s

20
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
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
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
23 Thank You!

24
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: R height, R total Extend S, N, R transformations to new data type. R height [ tree(e, e l, e r ) ] = 1 + max(R height [ e l ], R height [ e r ]) R total [ tree(e, e l, e r ) ] = 1 + R total [ e l ] + R total [ e r ] Live heap usage of a given program may be better represented in terms of one specific size measure, than others.

25
25 Example 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))) Input Program S is (ls s ) = if ls s =0 then 0 else max(S is (ls s -1), N is (ls s -1) + S ins (R is (ls s -1))) N is (ls s ) = if ls s =0 then 0 else N ins (R is (ls s -1)) R is (ls s ) = if ls s =0 then 0 else R ins (R is (ls s -1)) S ins (ls s ) = if ls s =0 then 1 else max(1, 1+S ins (ls s -1)) N ins (ls s ) = R ins (ls s ) R ins (ls s ) = if ls s =0 then 1+0 else max(1+ls s, 1+R ins (ls s -1)) Bound Functions

26
26 Example 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))) Input Program N ins (ls s ) = 1+ls s S is (ls s ) = { 0, if ls s =0 max(S is (ls s -1), 2ls s -1), otherwise S ins (ls s ) = { 1, if ls s =0 max(1, 1+S ins (ls s -1)), otherwise R ins (ls s ) = { 1, if ls s =0 max(1+ls s, 1+R ins (ls s -1)), otherwise R is (ls s ) = 0, if ls s =0 1+ R is (ls s -1), otherwise { N is (ls s ) = ls s Recurrences/Equations

27
27 Example 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))) Input Program S is (ls s ) = { 0, if ls s =0 2ls s -1, otherwise N is (ls s ) = ls s N ins (ls s ) = 1+ls s R is (ls s ) = ls s S ins (ls s ) = 1+ls s R ins (ls s ) = 1+ls s Closed Forms

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google