1/79

Developers

2/79

Portable Native Client (PNaCl)

How I Learned to Stop Compiling and Love the Translator

David Sehr

Tech lead, Native Client

Molly Mackinlay

Product Manager, Native Client

3/79

It’s a new web

We aren’t in the 90’s anymore...

4/79

The web in 1995

5/79

The web today

6/79

Google+Photos

7/79

We now expect native features in the web

Native

Web

8/79

Developers are expected to develop cross-platform

9/79

Native Code in the Web a History

1995

ActiveX

NPAPI

10/79

Native Code in the Web a History

Java

Flash

Silverlight

1995

11/79

Native Code in the Web a History

1995

2008

  • Security
  • Plugin
  • Integration

12/79

Native Code in the Web a History

Security issues by plugin type*

  • Security

*symantec

  • Plugin
  • Integration

13/79

Native Code in the Web a History

1995

ActiveX

Java

Flash

2008

Emscripten

2013

NPAPI

Silverlight

14/79

Native Code in the Web a History

  • Developer complexity
  • Not all native capabilities
  • Performance

1995

ActiveX

Java

Flash

2008

Emscripten

2013

NPAPI

Silverlight

15/79

Why Bring Native Code to the Web?

Better performance

Increased developer options

16/79

Wins of Native Code in the Web

  • Native capabilities
  • Performance
  • Familiar developer model

1995

ActiveX

Java

Flash

2008

Emscripten

2013

NPAPI

Silverlight

Native Client

Portable Native Client

gonacl.com

17/79

Developer Options

18/79

19/79

Our Solution: PNaCl

20/79

PNaCl is...

Native Code

Security

Performance

Platform

Independence

21/79

Access to Native: PPAPI

TCP/UDP

gonacl.com

22/79

Security

my_untrusted_code.nexe

Browser data

Browser APIs

OS APIs

X

gonacl.com

23/79

Platform Independence

ARM

x86

...

...

gonacl.com

24/79

Deploying to other browsers with pepper.js

app.cpp

Pepper API

Browser

25/79

Deploying to other browsers with pepper.js

app.cpp

Pepper API

Browser

PNaCl

app.pexe

Chrome

Pepper API

26/79

Deploying to other browsers with pepper.js

Emscripten

app.cpp

app.js

Pepper API

pepper.js

Browser

PNaCl

Other Browser

app.pexe

Chrome

Pepper API

27/79

28/79

Performance

% of native

ratio to native*

x86-32

90%

1.11

x86-64

81%

1.23

ARM (Chromebook)

93%+

1.07

*via SPEC2k

lower is better

gonacl.com

29/79

Under the Hood of a PNaCl App

30/79

Building a PNaCl Application

C++ Source Code

pnacl-clang

.pexe

.nmf file (manifest)

<embed name="nacl_module"

id="airmech"

src="airmech.nmf"

type="application/x-pnacl" />

HTML

gonacl.com

31/79

Running a PNaCl Application

<embed name="nacl_module"

id="airmech"

src="airmech.nmf"

type="application/x-pnacl" />

HTML

embed

starts

translation

PNaCl Translator

airmech.pexe

airmech.com

air_mech_x86_64.nexe

32/79

Running a PNaCl Application

Native Client Sandbox

validator

air_mech_x86_64.nexe

?

Browser data

Browser APIs

OS APIs

gonacl.com

33/79

Running a PNaCl Application

Native Client Sandbox

validator

air_mech_x86_64.nexe

Browser data

Browser APIs

OS APIs

gonacl.com

34/79

Running a PNaCl Application

Chrome Sandbox

Chrome Sandbox

Native Client Sandbox

NaCl Runtime

air_mech_x86_64.nexe

gonacl.com

35/79

Running a PNaCl Application

Chrome Sandbox

Chrome Sandbox

Native Client Sandbox

air_mech_x86_64.nexe

NaCl Runtime

PPAPI

Browser data

Browser APIs

gonacl.com

36/79

Where are we and what’s next?

37/79

Google+Photos

“it’s virtually impossible to build great photo apps that can rival the likes of Photoshop in HTML5. That’s where Native Client comes it.”

-TechCrunch

38/79

Pexe Size

C++ exceptions and SIMD

Translation

Time

Developer Responsivity

39/79

Thank You!

http://gonacl.com

sehr@google.com, mackinlay@google.com

native-client-discuss@googlegroups.com

40/79

Thank You!

http://gonacl.com

sehr@google.com, mackinlay@google.com

native-client-discuss@googlegroups.com

41/79

Thank You!

http://gonacl.com

sehr@google.com, mackinlay@google.com

native-client-discuss@googlegroups.com

42/79

How Do I Use It?

43/79

Developers

44/79

Backup

Subtitle Placeholder

45/79

Running a PNaCl Application

Native Client Sandbox

NaCl Runtime

air_mech_x86_64.nexe

gonacl.com

46/79

Running a PNaCl Application

Native Client Sandbox

air_mech_x86_64.nexe

Browser data

Browser APIs

OS APIs

47/79

Try PNaCl in Chrome 31

Try the PNaCl Examples extension

We want your feedback at http://gonacl.com

Try our demos

48/79

PNaCl SDK

  • Compiler, debugger, libraries
  • NaClPorts
  • Examples, demos

49/79

PPAPI

Writing a Secure Plugin

50/79

Porting an Application to NaCl

  • Build as an out-of-process plugin
  • Convert browser interaction to PPAPI
    • 2D, audio, mouse/keyboard, files
    • Convert 3D graphics to OpenGL ES2.0
    • Convert Windows threads to pthreads
  • Convert to newlib and static linking

51/79

Porting an Application to PNaCl

  • Build as an out-of-process plugin
  • Convert browser interaction to PPAPI
    • 2D, audio, mouse/keyboard, files
    • Convert 3D graphics to OpenGL ES2.0
    • Convert Windows threads to pthreads
  • Convert to newlib and static linking
  • Remove assembly code

52/79

Native Client and the Chrome Web Store

53/79

Native Client Limitations

"No ISAs in the web platform"

54/79

Portable Native Client

Developer

User

Distribution

55/79

Portable Native Client Distribution

Developer

User

Distribution

56/79

The Compatibility Story with Pepper.js

Pepper.js is a JavaScript library that enables the compilation of native Pepper applications into JavaScript using Emscripten.

57/79

The Compatibility Story with Pepper.js

  • Take C code using the Pepper interface (what you would normally compile into you PNaCl executable)
  • Make sure no threading, non-blocking
  • Run through Emscripten to output JavaScript
  • Bundle with Pepper.js
  • Deploy as cross-browser solution

58/79

Our Failures, and How We Address Them

59/79

“40% of people abandon a website that takes more than 3 seconds to load”

Sean Work

KISSmetrics

60/79

PNaCl Progress Events

Native Client Process

llc.nexe

Stream .pexe

from URL

ssh.nexe

Write .nexe

"progress" events

function moduleLoadProgress(event) {

var loadPercent = 0.0;

var loadPercentString;

if (event.lengthComputable && event.total > 0) {

loadPercent = event.loaded / event.total * 100.0;

loadPercentString = loadPercent + '%';

} else {

// The total length is not yet known.

loadPercent = -1.0;

loadPercentString = 'Computing...';

}

appendToEventLog('progress: ' + loadPercentString +

' (' + event.loaded + ' of ' + event.total + ' bytes)');

}

JavaScript

61/79

Translation Time and Application Architecture

Download .pexe

Download assets

Translate .pexe

Time

.pexe requests assets

Application runs

Download assets

Application runs

JavaScript requests assets

Download .pexe

Translate .pexe

pig.jpg

slingshot.jpg

redbird.jpg

bluebird.jpg

yellowbird.jpg

...

filelist.txt

62/79

Translation Options for PNaCl Applications

{� "files": {},

"program": { "portable": {� "pnacl-translate": {� "url": "ssh.pexe",

"-O": level� }� }� }�}

Translate Speed and Execution Speed

.nmf

level

translation time

perf vs. native

0

1x

~50%

2

~2-3x

~80+%

HTML

63/79

Translation Rate

O0

x86 (high-end)

~750 KB/s

ARM (Chromebook)

~130 KB/s

64/79

Translation Rate

O0

O2

x86 (high-end)

~750 KB/s

~250 KB/s

ARM (Chromebook)

~130 KB/s

~70 KB/s

65/79

A Taste of Things to Come

PNaCl in Chrome

66/79

PNaCl Roadmap

Developer Features

Launch

x86-32, x86-64, ARM

yes

pthreads, atomics

yes

SIMD

no

C++ Exception Handling

no

Tools, Examples, Libraries, Debugger

yes

Dynamic Linking

no

JIT Support

no

67/79

PNaCl Roadmap

Developer Features

Launch

First update

x86-32, x86-64, ARM

yes

pthreads, atomics

yes

SIMD

no

no

C++ Exception Handling

no

yes

Tools, Examples, Libraries, Debugger

yes

Dynamic Linking

no

no

JIT Support

no

no

68/79

PNaCl Roadmap

Developer Features

Launch

First update

Second update

x86-32, x86-64, ARM

yes

pthreads, atomics

yes

SIMD

no

no

C++ Exception Handling

no

yes

yes

Tools, Examples, Libraries, Debugger

yes

Dynamic Linking

no

no

???

JIT Support

no

no

???

69/79

PNaCl Roadmap

User Features

Launch

Translation Cache

yes

Developer Choice of Optimization Level

yes

Streaming Translation

yes

Multicore Translation

no

Custom Fast Translator

no

70/79

PNaCl Roadmap

User Features

Launch

First update

Translation Cache

yes

Developer Choice of Optimization Level

yes

Streaming Translation

yes

Multicore Translation

no

???

Custom Fast Translator

no

no

71/79

PNaCl Roadmap

User Features

Launch

First update

Second update

Translation Cache

yes

Developer Choice of Optimization Level

yes

Streaming Translation

yes

Multicore Translation

no

???

yes

Custom Fast Translator

no

no

???

72/79

Portable Native Client

  • C++ code
  • Run securely in the open web
  • 80+% of native performance
  • Try it right now in Chrome 31

73/79

Building a Portable Native Client Application

  • Native executable
  • Portable across operating systems
  • Specific to x86, ARM, etc.

C++ Source Code

gcc/g++

.nexe

74/79

Building a Portable Native Client Application

C++ Source Code

gcc/g++

.nexe

.nexe

.nmf file (manifest)

<embed name="nacl_module"

id="air_mech"

src="air_mech.nmf"

type="application/x-nacl" />

HTML

.nexe

75/79

Instruction Set Architecture (ISA) Dependency

{

"files": {},

"program": {

"x86-64": { "url": "air_mech_x86_64.nexe" },

"x86-32": { "url": "air_mech_x86_32.nexe" },

"arm": { "url": "air_mech_arm.nexe" }

}

}

NaCl Manifest File

.nmf

Every supported ISA needs an entry here

Which ISA

Where to find its executable

76/79

Running a Native Client Application

<embed name="nacl_module"

id="air_mech"

src="air_mech.nmf"

type="application/x-nacl" />

Native Client Process

air_mech_x86_64.nexe

Loads correct

.nexe

HTML

{

"files": {},

"program": {

"x86-64": { "url": "air_mech_x86_64.nexe" },

"x86-32": { "url": "air_mech_x86_32.nexe" },

"arm": { "url": "air_mech_arm.nexe" }

}

}

air_mech.nmf

embed reads

manifest

.nmf

77/79

So We're Done, Right?

  • C++ executables
  • Run in the browser
  • Distributed in the web store
  • Native ISA code is not right for the open web
  • We need a way to upload device-independent code, then translate to the client's ISA...

78/79

Related Technology

  • Asm.js
    • Similarities
      • C/C++
      • LLVM
      • Performance, ~50% of native
    • Differences
      • Interchange format is JavaScript with type annotations
      • In-renderer, faster DOM accesses
      • No threading or SIMD without further JavaScript support
    • Significant effort required to attain 80+% of native

79/79

The PNaCl Translator

"ssh.pexe"

no translation exists

Native Client Process

llc.nexe

Send .nexe

from cache

Native Client Process

ssh_x86_64.nexe

Translation

Cache

Stream .pexe

from URL

ssh_x86_64.nexe

Write .nexe

Save ssh_x86_64.nexe