Concurrent Programming Threads of execution: Thread objects running - - PowerPoint PPT Presentation

concurrent programming
SMART_READER_LITE
LIVE PREVIEW

Concurrent Programming Threads of execution: Thread objects running - - PowerPoint PPT Presentation

T RANSPARENT P ROXIES FOR J AVA F UTURES Polyvios Pratikakis Jaime Spacco Michael Hicks University of Maryland, College Park Transparent Proxies for Java Futures p. 1/51 Concurrent Programming Threads of execution: Thread objects


slide-1
SLIDE 1

TRANSPARENT PROXIES FOR JAVA FUTURES

Polyvios Pratikakis Jaime Spacco Michael Hicks University of Maryland, College Park

Transparent Proxies for Java Futures – p. 1/51

slide-2
SLIDE 2

Concurrent Programming

Threads of execution: Thread objects running

in parallel

Asynchronous method invocations: Methods

can be called asynchronously

e.g:

Main thread:

  • = new O();
  • .m(); //async

Transparent Proxies for Java Futures – p. 2/51

slide-3
SLIDE 3

Concurrent Programming

Threads of execution: Thread objects running

in parallel

Asynchronous method invocations: Methods

can be called asynchronously

e.g:

Main thread: Child thread:

  • = new O();
  • .m(); //async

  • .m();

Transparent Proxies for Java Futures – p. 2/51

slide-4
SLIDE 4

Concurrent Programming

Threads of execution: Thread objects running

in parallel

Asynchronous method invocations: Methods

can be called asynchronously

e.g:

Main thread: Child thread:

  • = new O();
  • .m(); //async

  • .m();

...

Transparent Proxies for Java Futures – p. 2/51

slide-5
SLIDE 5

Futures

What happens with returned value? “Future” or “promise”: a placeholder for the

result

“Claim” a future:

If the result is not available, wait for it

Futures are proxies. Other examples: Suspensions (lazy invocation) Remote objects Other wrappers

Transparent Proxies for Java Futures – p. 3/51

slide-6
SLIDE 6

Java Transparent Proxy Framework

Static analysis and program transformation Based on qualifier inference system Formalization and proof of soundness Implementation of Futures via async methods Also lazy invocations, other applications Benefits Simple programming model Can improve application performance

Transparent Proxies for Java Futures – p. 4/51

slide-7
SLIDE 7

MultiLISP futures

(future e) means e executes in parallel Lisp is functional and dynamically typed No need for the programmer to insert claims:

The runtime system checks every access. If it is a future, claim before access

Programmer only inserts future notations Futures are transparent

Transparent Proxies for Java Futures – p. 5/51

slide-8
SLIDE 8

Java Futures not Transparent

Java is statically typed Futures in JSR166 must be claimed explicitly:

public interface Future<V> { V get(); V get(long timeout, TimeUnit unit); ... }

Transparent Proxies for Java Futures – p. 6/51

slide-9
SLIDE 9

Programming Overhead

To convert a method invocation to asynchronous:

Change the call site to be asynchronous Change the type of the result to Future Change the type of variables to which the

result flows

Insert claims

It is usual to claim early to avoid rewriting a lot of code Question: can we do this automatically?

Transparent Proxies for Java Futures – p. 7/51

slide-10
SLIDE 10

Type Qualifiers

Qualifiers refine the meaning of types final Integer is to Integer like

Future<String> is to String

“Proxyness” is a type qualifier: proxy or

nonproxy.

If x has type

proxy String then x could be a proxy nonproxy String then x is not a proxy nonproxy ≤ proxy

Transparent Proxies for Java Futures – p. 8/51

slide-11
SLIDE 11

Automatic Transformation

Use qualifier inference to determine where

proxies flow

Transform program based on results Rewrite proxy types Insert claims whenever a proxy is used

concretely

Transparent Proxies for Java Futures – p. 9/51

slide-12
SLIDE 12

Example: Initial Program

procRequest(Socket sock) { Buffer in = readBuf(sock); Request req = translate(in); Buffer out = process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 10/51

slide-13
SLIDE 13

Example: Async Calls

procRequest(Socket sock) { Buffer in = @readBuf(sock); Request req = @translate(in); Buffer out = @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 11/51

slide-14
SLIDE 14

Example: Qualified Types

procRequest(Socket sock) { Buffer in = proxy @readBuf(sock); Request req = proxy @translate(in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 12/51

slide-15
SLIDE 15

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); Request req = proxy @translate(in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 13/51

slide-16
SLIDE 16

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); Request req = proxy @translate(in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 14/51

slide-17
SLIDE 17

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); Request req = proxy @translate(proxy in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 15/51

slide-18
SLIDE 18

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); Request req = proxy @translate(proxy in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 16/51

slide-19
SLIDE 19

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 17/51

slide-20
SLIDE 20

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 18/51

slide-21
SLIDE 21

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); Buffer out = proxy @process(proxy req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 19/51

slide-22
SLIDE 22

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); Buffer out = proxy @process(proxy req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 20/51

slide-23
SLIDE 23

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); proxy Buffer out = proxy @process(proxy req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 21/51

slide-24
SLIDE 24

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); proxy Buffer out = proxy @process(proxy req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 22/51

slide-25
SLIDE 25

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); proxy Buffer out = proxy @process(proxy req); writeBuf(sock,proxy out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 23/51

slide-26
SLIDE 26

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); proxy Buffer out = proxy @process(proxy req); writeBuf(sock,proxy out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 24/51

slide-27
SLIDE 27

Example: Qualified Types

procRequest(Socket sock) { proxy Buffer in = proxy @readBuf(sock); proxy Request req = proxy @translate(proxy in); proxy Buffer out = proxy @process(proxy req); writeBuf(sock,proxy out); } Request translate(proxy Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 25/51

slide-28
SLIDE 28

Example: Future Transformation

procRequest(Socket sock) { Buffer in = @readBuf(sock); Request req = proxy @translate(in); Buffer out = proxy @process(req); writeBuf(sock,out); } Request translate(Buffer in) { Request result; ... in.foo() ... return result; }

Transparent Proxies for Java Futures – p. 26/51

slide-29
SLIDE 29

Example: Future Transformation

procRequest(Socket sock) { Object in = new Proxy{ private Object result; public void run() { result = readBuf(sock); } public synchronized Object get(){ ... return result; } }(); Executor.run((Runnable)in); Request req = @translate(in); Buffer out = @process(req); writeBuf(sock,out); }

Transparent Proxies for Java Futures – p. 27/51

slide-30
SLIDE 30

Example: Future Transformation

procRequest(Socket sock) { Object in = new Proxy{ private Object result; public void run() { result = readBuf(sock); } public synchronized Object get(){ ... return result; } }(); Executor.run((Runnable)in); Object req = new Proxy{... translate(in) ... Object out = new Proxy{... process(req) ... writeBuf(sock,out); }

Transparent Proxies for Java Futures – p. 28/51

slide-31
SLIDE 31

Example: Qualified types

Request translate(Buffer in) { Request result; ... in.foo(); ... return result; }

Transparent Proxies for Java Futures – p. 29/51

slide-32
SLIDE 32

Example: Qualified types

Request translate(Buffer in) { Request result; ... in.foo(); ... return result; }

concrete use

Transparent Proxies for Java Futures – p. 30/51

slide-33
SLIDE 33

Example: Qualified types

Request translate(Buffer in) { Request result; ... (nonproxy in).foo(); ... return result; }

Transparent Proxies for Java Futures – p. 31/51

slide-34
SLIDE 34

Example: Qualified types

Request translate(Buffer in) { proxy Request result; ... (nonproxy in).foo(); ... return result; }

Transparent Proxies for Java Futures – p. 32/51

slide-35
SLIDE 35

Example: Qualified types

Request translate(Buffer in) { Request result; ... (nonproxy in).foo(); ... return result; }

Transparent Proxies for Java Futures – p. 33/51

slide-36
SLIDE 36

Example: Future Transformation

Request translate(Object inF) { Request result; ... Buffer in = (Buffer) (inF instanceof Proxy ? inF.get() : inF); in.foo(); ... return result; }

claim

Transparent Proxies for Java Futures – p. 34/51

slide-37
SLIDE 37

Other Uses of Proxies

User can define: What is a concrete usage (places where an

unwrapping would be necessary)

Where proxies are created What is a claim E.g. suspensions (lazy proxies): Concrete usage: same as futures Proxy creation: lazy invocations Claim: evaluate the invocation and return

its result

Transparent Proxies for Java Futures – p. 35/51

slide-38
SLIDE 38

Other Uses of Qualifier Inference

tainted / untainted qualifiers (for security) null / non-null qualifiers: can be used to

enforce stronger interfaces

stack / heap allocation for objects:

Figure out which objects can be stack-allocated, and if/when they need to be moved to the heap

Transparent Proxies for Java Futures – p. 36/51

slide-39
SLIDE 39

Formalism: Checking System

FJQ: Featherweight Java + Qualifiers: checking

system

If the programmer annotated every type

with a qualifier by hand, would it be correct?

Proof of soundness for FJQ: If the annotated program typechecks, then

everything is claimed before used

Transparent Proxies for Java Futures – p. 37/51

slide-40
SLIDE 40

Formalism: Inference System

FJi

Q: Inference system that produces FJQ

programs

Find the values of the proxy qualifier

automatically

Correctness of inference: If FJi

Q finds a solution, the resulting

annotated program typechecks in FJQ

Transparent Proxies for Java Futures – p. 38/51

slide-41
SLIDE 41

Formalism: Features

Use set types to improve precision:

class A { ... } class B extends A { ... } class C extends A { ... } ... A var = new B();

Variable var has type {B}A

Selective flow-sensitive coercions from proxy

to nonproxy

Transparent Proxies for Java Futures – p. 39/51

slide-42
SLIDE 42

Implementation

Uses Soot Jimple: Three-address code SSA-like

representation of bytecode

Spark: Points-to analysis engine Extends Spark’s Andersen-style points-to

analysis with selective flow-sensitivity

Bytecode to bytecode transformation Handles the full Java language (exceptions,

JNI, reflection, etc)

Transparent Proxies for Java Futures – p. 40/51

slide-43
SLIDE 43

Performance

RMI peer-to-peer application Each peer searches in the network to find

service providers

Transparent Proxies for Java Futures – p. 41/51

slide-44
SLIDE 44

Example

Service findService(LocalPeer self, String sName) { Service s = self.getService(sName); if (s != null) return s; else { self.forward( new FindServiceMessage(sName)); return getRemoteService(self, sName); } }

Transparent Proxies for Java Futures – p. 42/51

slide-45
SLIDE 45

Example

Service findService(LocalPeer self, String sName) { Service s = self.getService(sName); if (s != null) return s; else { self.forward( new FindServiceMessage(sName)); return getRemoteService(self, sName); } }

Transparent Proxies for Java Futures – p. 43/51

slide-46
SLIDE 46

Example

Service findService(LocalPeer self, String sName) { Service s = self.getService(sName); if (s != null) return s; else { self.forward( new FindServiceMessage(sName)); return getRemoteService(self, sName); } }

Transparent Proxies for Java Futures – p. 44/51

slide-47
SLIDE 47

Example

Service findService(LocalPeer self, String sName) { Service s = self.getService(sName); if (s != null) return s; else { self.forward( new FindServiceMessage(sName)); return getRemoteService(self, sName); } }

Transparent Proxies for Java Futures – p. 45/51

slide-48
SLIDE 48

Example

Service findService(LocalPeer self, String sName) { Service s = self.getService(sName); if (s != null) return s; else { @self.forward( new FindServiceMessage(sName)); return $getRemoteService(self, sName); } }

$ denotes a lazy invocation

Transparent Proxies for Java Futures – p. 46/51

slide-49
SLIDE 49

Analysis Performance

Analysis Time classes (s) analyzed w/ fut. changed claims FI 73 1324 27 2 7 FS 92 1324 9 2 2 SPARK 66 1320 n/a n/a n/a

Transparent Proxies for Java Futures – p. 47/51

slide-50
SLIDE 50

Related Work

Futures and async methods MultiLISP (Halstead et al.) Promises (Liskov and Shrira) Touch elimination (Flanagan and Felleisen) Polyphonic C# (Benton et al.) Lazy Task Creation (Halstead, Frigo et al.) SCOOP/Eiffel (Compton) Async RMI (Raje et al, Sysala et al.)

Transparent Proxies for Java Futures – p. 48/51

slide-51
SLIDE 51

Related Work

Static Analysis Points-to analysis (many) Qualifier inference (Foster et al.) Value flow analysis (Heintze and Tardieu)

Transparent Proxies for Java Futures – p. 49/51

slide-52
SLIDE 52

Future Work

Incorporate into a general framework for

qualifiers in Java

Arbitrary qualifier partial order Context-sensitive analysis Faster algorithm Incremental analysis Only re-run analysis for files affected by

local change

Don’t reanalyze the library every time

Transparent Proxies for Java Futures – p. 50/51

slide-53
SLIDE 53

Summary

Framework for programming with proxies Simplifies programming process Avoids violations of transparency Novel static analysis Extends qualifier inference with

flow-sensitive coercions

Has additional applications (Stack-allocated

  • bjects, not-null types, etc)

Formalization and proof of soundness Prototype implementation

Transparent Proxies for Java Futures – p. 51/51