Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation...

27
Component adaptation and assembly using interface relations Or: a tool called Cake . Stephen Kell [email protected] Computer Laboratory University of Cambridge Component adaptation. . . Cake – p.1/18

Transcript of Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation...

Page 1: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Component adaptation and assemblyusing interface relations

Or: a tool called Cake

.

Stephen Kell

[email protected]

Computer Laboratory

University of Cambridge

Component adaptation. . . Cake – p.1/18

Page 2: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

There’s something about software. . .

Software is expensive and inflexible.

Tools assume:

� ground-up

� perfect fit

� don’t change

� never replaced

Reality: none of the above!

Component adaptation. . . Cake – p.2/18

Page 3: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

A problem

A B

These programming tasks arise:

� as software evolves

� as new user requirements emerge

� as software ecosystem evolves

� e.g. alternative components become available

Component adaptation. . . Cake – p.3/18

Page 4: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

This talk in one slide

Cake is a tool for tasks like these. It is:

� a rule-based language

� . . . for describing adapters

� declarative

� black-box

� convenient

Key contributions:

� expressive enough for realistic tasks

� . . . context-sensitive, many-to-many relations

� . . . evaluated on real tasks

Component adaptation. . . Cake – p.4/18

Page 5: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Common approaches

A BA′

B

A′′

A Bglue code

edit or patch

glue coding

abstraction layer

Component adaptation. . . Cake – p.5/18

Page 6: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Overview

A.o B.o

generated

adapter

Cake

rules

compile

... ...

... ...

exists elf reloc ( ”A.o”) A; // assume that object files...

exists elf reloc ( ”B.o”) B; // ... contain debug info

derive elf reloc ( ”whole.o”) = link [A, B]

{

/∗ your rules here! ∗/

};

Component adaptation. . . Cake – p.6/18

Page 7: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Simple adaptations

A B

foo() bar()

foo (...) ←→ bar (...);

Component adaptation. . . Cake – p.7/18

Page 8: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Simple adaptations

A B

foo() bar()

baz(a,b) baz(b,a)

foo (...) ←→ bar (...);

baz(a, b) ←→ baz(b, a);

Component adaptation. . . Cake – p.7/18

Page 9: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Simple adaptations

A B

foo() bar()

baz(a,b) baz(b,a)

xyz(a) xyz(a,42)

foo (...) ←→ bar (...);

baz(a, b) ←→ baz(b, a);

xyz(a) ←→ xyz(a, 42);

Component adaptation. . . Cake – p.7/18

Page 10: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Simple adaptations

A B

foo() bar()

baz(a,b) baz(b,a)

xyz(a) xyz(a,42)

struct Br struct Tr

foo (...) ←→ bar (...);

baz(a, b) ←→ baz(b, a);

xyz(a) ←→ xyz(a, 42);

values Br←→ Tr

Component adaptation. . . Cake – p.7/18

Page 11: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Simple adaptations

A B

foo() bar()

baz(a,b) baz(b,a)

xyz(a) xyz(a,42)

struct Br

int sz; ... int len; ...

struct Tr

foo (...) ←→ bar (...);

baz(a, b) ←→ baz(b, a);

xyz(a) ←→ xyz(a, 42);

values Br←→ Tr

{ sz ←→ len; };

The Cake compiler generates wrapper functions from rules.

Component adaptation. . . Cake – p.7/18

Page 12: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

More complex adaptations

mpeg2_init();

fopen(fname, “rb”);

mpeg2_info(dec);

mpeg2_parse(dec);

mpeg2_buffer(dec, bgn, end)

fread(buf, size, n, f);

fwrite(buf, size, n, f);

fwrite(buf, size, n, f);

fwrite(buf, size, n, f);

mpeg2_close();

avcodec_init();

av_register_all();

av_open_input_file(out ic, fname, fmt, buf_size, params);

av_find_stream_info(ic);

avcodec_open(c_ctx, c_obj);

avcodec_find_decoder(c_id);

fwrite(buf, size, n, f);

fwrite(buf, size, n, f);

fwrite(buf, size, n, f);

avcodec_close();

av_read_frame(pkt, ic);

avcodec_alloc_frame();

avcodec_decode_video(c_obj, frame, out got_pic, buf, size);

av_close_input_file();

av_free(frame);

av_free_packet(pkt);

Open file and

decoder

Discover

streams

Read and

decode next

frame

Write decoded

output

Release per-

frame resources

Release

resources

libmpeg2 client control flow ffmpeg client control flow

Real interfaces

correspond less simply:

� non-1-to-1 mappings

� context-sensitive

� data, not just code

Component adaptation. . . Cake – p.8/18

Page 13: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Many-to-many mappings

mpeg2_dec_s mpeg2_info_t

sequence

display_fbuf

mpeg2_sequence_t

width

height

display_width

display_height

chroma_width

chroma_height

id

buf[3]

codec_id

codec_type

AVStream

codec

AVCodecContext

width

height

AVCodec

size

AVPacket

mpeg2_fbuf_t

linesize[4]

data[4]

AVFrame

Component adaptation. . . Cake – p.9/18

Page 14: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Many-to-many mappings

mpeg2_dec_s mpeg2_info_t

sequence

display_fbuf

mpeg2_sequence_t

width

height

display_width

display_height

chroma_width

chroma_height

id

buf[3]

codec_id

codec_type

AVStream

codec

AVCodecContext

width

height

AVCodec

size

AVPacket

mpeg2_fbuf_t

linesize[4]

data[4]

AVFrame

values (dec: mpeg2 dec s, info: mpeg2 info s,

seq: mpeg2 sequence s, fbuf: mpeg2 fbuf s)

←→( ctxt : AVCodecContext, vid idx: int, frame: AVFrame,

p: AVPacket, s: AVStream, codec: AVCodec)

{ fbuf ←→ frame{ buf [0] as line [seq.height] ptr ←→ data[0] as line [ ctxt.height ] ptr ;

} } /∗ ←−- more rules go here... ∗/

This creates an association at run time (like a join table).Component adaptation. . . Cake – p.9/18

Page 15: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Context-sensitive mappings

Trace of start-up calls appropriate for the two libraries:

mpeg2_init() 7→ dec

fopen("...", "rb") 7→f

mpeg2_info(dec) 7→info

mpeg2_parse(dec) 7→STATE_BUFFER

avcodec_init() 7→()

av_register_all() 7→()

av_open_file(...) 7→avf

av_find_stream_info(avf) 7→()

avcodec_find_decoder(...) 7→

codec

av_codec_open(...) 7→()

Problem!

� one info call wants decoder object; other wants file.

Solution: context predication with name binding.

let dec = mpeg2 init(), ...,

let f = fopen(fname, ”rb” ), ...,

mpeg2 info(dec) −→{ /∗ now both f and dec are available ∗/ };

Component adaptation. . . Cake – p.10/18

Page 16: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Complex object structures

“Passing objects” often means passing object graphs.list_node_t

next

data

list_node_t

next

data

list_node_t

next

data

...

x

point_t

y

x

point_t

y

x

point_t

y

ListNode

next

item

ListNode

next

item

next

item

...

x

XYPoint

y

x

XYPoint

y

x

XYPoint

y

ListNode

......

The Cake runtime traverses object structures automatically.

values list node t ←→ ListNode

{ data ←→ item; };

values point t ←→ XYPoint;

Incidentally, note the following:

� name-matching is Cake’s default policy

� can insert stub code for value transformation (not

shown) Component adaptation. . . Cake – p.11/18

Page 17: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Implementation

Compiler:

� accepts Cake source file

� emits wrapper functions as C++ code

� consumes DWARF debugging information

Runtime:

� allocator instrumentation

� dynamic points-to analysis

� “split heap” management, association tracking, . . .

Status: compiler still lagging language design, but WIP. . .

Component adaptation. . . Cake – p.12/18

Page 18: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Evaluation

Compare Cake implementations with pre-existing adapters:

1. p2k: a filesystem adapter from the NetBSD OS

2. ephy-webkit: abstraction layer from Epiphany browser

3. XCL (subset of): compatibility layer for XCB X11 library

Summary outcome:

� less code (code written; various syntactic measures)

� p2k: approx 70% reduction

� ephy-webkit: approx 65% reduction

� XCL: approx 30% reduction

� less scattering of concerns

Component adaptation. . . Cake – p.13/18

Page 19: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Comparison (1)

int seek( struct puffs usermount ∗pu,

puffs cookie t opc, off t oldoff ,

off t newoff, struct puffs cred ∗pcr)

{

kauth cred t cred ; int rv ;

cred = cred create (pcr );

VLE(opc);

rv = RUMP VOP SEEK(

opc, oldoff , newoff, cred );

VUL(opc);

cred destroy (cred );

return rv ;

}

int remove(struct puffs usermount ∗pu,

puffs cookie t opc, puffs cookie t targ ,

struct puffs cn ∗pcn)

{

struct componentname ∗cn; int rv ;

cn = makecn(pcn);

VLE(opc);

rump vp incref (opc);

VLE(targ);

rump vp incref ( targ );

rv = RUMP VOP REMOVE(

opc, targ , cn );

AUL(opc);

AUL(targ);

freecn (cn, 0);

return rv ;

} Component adaptation. . . Cake – p.14/18

Page 20: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Comparison (2)

// rules concerning functions

p2k node seek( , vn, oldoff , newoff, cred)

−→RUMP VOP SEEK(vn, oldoff, newoff, cred);

p2k node remove( , vn as vnode bump, tgtvn as vnode bump,

cn) −→RUMP VOP REMOVE(vn, tgtvn, cn);

// rules concerning values

values puffs cookie t −→ ({VLE(that); that}) vnode;

values puffs cookie t ←−({VUL(that); that}) vnode;

values vnode bump −→({VLE(that); rump vp incref(that);

that}) vnode; // also bump refcount

values vnode bump←−vnode; // unlock not required

values puffs cred (cred create( this ))−→ kauth cred;

values puffs cred←−(cred destroy(this)) kauth cred;

values puffs cn (makecn(this))−→ component name;

values puffs cn←−(freecn(this, 0)) component name;

+ these rules contribute to other wrapper functions (28 total)

Component adaptation. . . Cake – p.15/18

Page 21: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Related work

Similar tools with narrow domains or less expressiveness:

� Nimble (Purtilo, 1990), BCA (Keller, 1998)

� Jigsaw (Bracha, 1993), Knit (Reid, 2000)

� Swig (Beazley, 1996)

� C++ concept maps (Jarvi, 2007)

� Twinning (Nita, 2010)

Work focused on formalisation rather than implementation:

� Yellin & Strom, 1994; Bracciali, 2003

� subject-oriented composition (Ossher, 1995)

Clean-slate approaches to similar problems:

� Flexible Packaging (Deline, 2001)

Component adaptation. . . Cake – p.16/18

Page 22: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Future work, conclusions, . . .

So far, Cake is

� a simpler way of writing short modular adapters

� a convenient tool for binary composition

� a step towards more compositional development

Cake is a platform for lots of potential future work.

� styles (for abstracting heterogeneous object code)

� white-box complement

� improved bidirectionality

� semi-automatic generation of Cake rules

Component adaptation. . . Cake – p.17/18

Page 23: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Things I didn’t have time to mention

More language features:

� input versus output parameters

� error discovery & handling

� design for heterogeneity

� stub language (algorithms, lambdas, . . . )

� annotations, memory management adaptations, . . .

Repositories:

� http://www.cl.cam.ac.uk/̃ srk31/cake/

Thanks for your attention. Questions?

Component adaptation. . . Cake – p.18/18

Page 24: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

How Cake wins

� separate treatment of values from treatment of functions

� separate general from special cases

� name-matching

� black-box, binary, language-independent

� designed to accommodate heterogeneity

� make previously edit-requiring tasks black-boxable

� generates sequence recognition code automatically

� maintains object mappings (co-object relation) at run

time

� transitive treatment of object structures

� potential for bidirectional rules

Component adaptation. . . Cake – p.19/18

Page 25: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Partially split heap

Component adaptation. . . Cake – p.20/18

Page 26: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Numbers

p2k

C adjusted Cake remaining C

LoC (nb nc) 605 523 133 54

tokens 3469 3137 1131 347

semicolons 358 277 69 33

� wins: rule localisation (hugely), allocation

ephy

C adjusted Cake remaining C

LoC (nb nc) 525 513 161 0

tokens 2529 2455 784 0

semicolons 175 163 70 0

� wins: rule localisation, many-to-many, graph

exploration, pattern-matching Component adaptation. . . Cake – p.21/18

Page 27: Component adaptation and assembly using interface relations · 2010-10-20 · Component adaptation and assembly using interface relations Or: a tool called Cake. Stephen Kell Stephen.Kell@cl.cam.ac.uk

Numbers (2)

XCLC adjusted Cake remaining C

LoC (nb nc) 380 315 189 42

tokens 2581 2328 1543 232

semicolons 187 148 107 19

� problems: abstraction gap, cross-rule commonality,

more data types, more special cases, smaller subset of

code (increasing returns?)

Component adaptation. . . Cake – p.22/18