Slide 1

Slide 1 text

JNI @jessewilson

Slide 2

Slide 2 text

Java • Productive • Lots of great libraries • Fast enough

Slide 3

Slide 3 text

JVM • Architecture neutral • Garbage collected • Fast Java Virtual Machine

Slide 4

Slide 4 text

JVM • Architecture neutral • Garbage collected • Fast Java Virtual Machine just-in-time compiled

Slide 5

Slide 5 text

JVM • Architecture neutral • Garbage collected • Fast Java Virtual Machine just-in-time compiled interpreted

Slide 6

Slide 6 text

ART • Architecture neutral • Garbage collected • Fast Android Runtime

Slide 7

Slide 7 text

ART • Architecture neutral • Garbage collected • Fast Android Runtime ahead-of- time compiled

Slide 8

Slide 8 text

ART • Architecture neutral • Garbage collected • Fast Android Runtime just-in-time compiled ahead-of- time compiled

Slide 9

Slide 9 text

ART • Architecture neutral • Garbage collected • Fast Android Runtime just-in-time compiled interpreted ahead-of- time compiled

Slide 10

Slide 10 text

Architecture Neutral

Slide 11

Slide 11 text

Popular Architectures • Desktops: x86, x64 • Sma rt phones: ARM, MIPS

Slide 12

Slide 12 text

Architecture Neutral • You ship one app • The runtime adapts it to the host computer’s OS and architecture

Slide 13

Slide 13 text

Operating System Computer Application

Slide 14

Slide 14 text

Operating System Computer Application architecture speci fi c

Slide 15

Slide 15 text

Operating System Computer JVM / ART Application architecture speci fi c

Slide 16

Slide 16 text

Operating System Computer JVM / ART Application architecture speci fi c architecture neutral

Slide 17

Slide 17 text

Operating System Computer JVM / ART Application

Slide 18

Slide 18 text

Operating System Computer JVM / ART Application

Slide 19

Slide 19 text

Operating System Computer JVM / ART Application

Slide 20

Slide 20 text

Operating System Computer JVM / ART Application

Slide 21

Slide 21 text

Operating System Computer JVM / ART Application

Slide 22

Slide 22 text

Operating System Computer JVM / ART Application

Slide 23

Slide 23 text

Operating System Computer JVM / ART Application

Slide 24

Slide 24 text

Operating System Computer JVM / ART Application

Slide 25

Slide 25 text

Operating System Computer JVM / ART Application

Slide 26

Slide 26 text

Operating System Computer JVM / ART Application

Slide 27

Slide 27 text

Operating System Computer JVM / ART Application

Slide 28

Slide 28 text

Operating System Computer JVM / ART Application

Slide 29

Slide 29 text

Operating System Computer JVM / ART Application

Slide 30

Slide 30 text

Operating System Computer JVM / ART Application

Slide 31

Slide 31 text

Operating System Computer JVM / ART Application

Slide 32

Slide 32 text

Operating System Computer JVM / ART App Java

Slide 33

Slide 33 text

Operating System Computer JVM / ART App Java App C/C++

Slide 34

Slide 34 text

Operating System Computer JVM / ART App Java App C/C++ architecture neutral

Slide 35

Slide 35 text

Operating System Computer JVM / ART App Java App C/C++ architecture neutral architecture speci fi c

Slide 36

Slide 36 text

Operating System Computer JVM / ART App Java App C/C++

Slide 37

Slide 37 text

Operating System Computer JVM / ART App Java App C/C++

Slide 38

Slide 38 text

Operating System Computer JVM / ART App Java App C/C++

Slide 39

Slide 39 text

Operating System Computer JVM / ART App Java App C/C++

Slide 40

Slide 40 text

Operating System Computer JVM / ART App Java App C/C++

Slide 41

Slide 41 text

Operating System Computer JVM / ART App Java App C/C++

Slide 42

Slide 42 text

Operating System Computer JVM / ART App Java App C/C++

Slide 43

Slide 43 text

Operating System Computer JVM / ART App Java App C/C++

Slide 44

Slide 44 text

Operating System Computer JVM / ART App Java App C/C++

Slide 45

Slide 45 text

Operating System Computer JVM / ART App Java App C/C++

Slide 46

Slide 46 text

Operating System Computer JVM / ART App Java App C/C++

Slide 47

Slide 47 text

Operating System Computer JVM / ART App Java App C/C++

Slide 48

Slide 48 text

Operating System Computer JVM / ART App Java App C/C++

Slide 49

Slide 49 text

Operating System Computer JVM / ART App Java App C/C++

Slide 50

Slide 50 text

App Java ARM C/C++

Slide 51

Slide 51 text

App Java ARM C/C++ foo.apk

Slide 52

Slide 52 text

App Java ARM C/C++ foo.apk Image Image Image Image Image Image Image Image Layout Layout Resource Resource Translation XML Translation XML

Slide 53

Slide 53 text

App Java ARM C/C++ x64 C/C++ x86 C/C++ MIPS C/C++ foo.apk Image Image Image Image Image Image Image Image Layout Layout Resource Resource Translation XML Translation XML

Slide 54

Slide 54 text

JNI & Architecture • Build & ship a JNI library for every CPU+OS pair you suppo rt • That means you need to decide which CPU+OS pairs to suppo rt ! • Fo rt unately, Android is a single OS, but there are still several CPUs!

Slide 55

Slide 55 text

Garbage Collected

Slide 56

Slide 56 text

Garbage Collection • You solve application problems • The runtime worries about allocating and freeing memory • Why make a clumsy human do a computer’s job?

Slide 57

Slide 57 text

List characters = new ArrayList<>(); 
 characters.add("grant"); 
 characters.add("hammond"); 
 characters.add("malcolm");

Slide 58

Slide 58 text

String String String ArrayList Object[] char[] char[] char[]

Slide 59

Slide 59 text

String String String ArrayList Object[] char[] char[] char[] 0x00 0x10 0x20 0x30 0x40 0x50

Slide 60

Slide 60 text

String String String ArrayList Object[] char[] char[] char[] 0x00 0x10 0x20 0x30 0x40 0x50

Slide 61

Slide 61 text

String String String ArrayList Object[] char[] char[] char[] 0x00 0x10 0x20 0x30 0x40 0x50

Slide 62

Slide 62 text

String String String ArrayList Object[] char[] char[] char[] uint struct void* unsigned int* struct struct uint struct char* void* struct

Slide 63

Slide 63 text

String String String ArrayList Object[] char[] char[] char[] uint struct void* unsigned int* struct struct uint struct char* void* struct native heap

Slide 64

Slide 64 text

String String String ArrayList Object[] char[] char[] char[] uint struct void* unsigned int* struct struct uint struct char* void* struct native heap managed heap

Slide 65

Slide 65 text

Garbage Collector • Within a managed heap, an object’s identity and location in memory are decoupled • A compacting collector defragments the heap by moving objects around

Slide 66

Slide 66 text

Moving References • You need to be careful about pointers into the managed heap • If an object is moved, your pointers will break!

Slide 67

Slide 67 text

Tracking References • You need to keep the garbage collector informed about what objects you’re using • Otherwise they could be collected out from under you!

Slide 68

Slide 68 text

Fast Enough

Slide 69

Slide 69 text

A Langauge of Abstraction • List vs. array • Methods are vi rt ual by default • Strict encapsulation

Slide 70

Slide 70 text

A Dynamic Runtime • Re fl ection • Runtime code loading • Memory managed

Slide 71

Slide 71 text

Native Code

Slide 72

Slide 72 text

Typical Native Libraries • Cryptography • Databases • PDF viewers

Slide 73

Slide 73 text

Why Native? • Cross-platform • Historical accident • Inef fi cient on Java’s pe rf ormance model • Bene fi ts from access to fancy operating system features

Slide 74

Slide 74 text

Why Not Native? • “faster”

Slide 75

Slide 75 text

Operating System Computer JVM / ART App Java App C/C++

Slide 76

Slide 76 text

Operating System Computer JVM / ART App Java App C/C++

Slide 77

Slide 77 text

“We rewrote our program in and it was 5x faster” C

Slide 78

Slide 78 text

“We rewrote our program in and it was 5x faster” Java

Slide 79

Slide 79 text

Java, Fast & Slow • Most programs are slow because they use inappropriate datastructures, algorithms, and abstractions • Because Java has many rich APIs, Java programs are often faster

Slide 80

Slide 80 text

Lots of fast Java programs • javac vs. gcj • IntelliJ vs. Visual Studio

Slide 81

Slide 81 text

How to JNI

Slide 82

Slide 82 text

http://tinyurl.com/jni-example

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

public final class Hello { 
 static { 
 System.loadLibrary("jniexample"); 
 } 
 
 public static native String greet(String name); 
 }

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

No content

Slide 88

Slide 88 text

#include 
 #include 
 
 JNIEXPORT jstring JNICALL 
 Java_com_squareup_jniexample_Hello_greet(JNIEnv *env, jclass type, jstring name_) { 
 const char *name = (*env)->GetStringUTFChars(env, name_, 0); 
 
 char *greeting; 
 if (strcmp(name, "Jesse") == 0) { 
 greeting = "Yo dog"; 
 } else { 
 greeting = "Hello"; 
 } 
 
 (*env)->ReleaseStringUTFChars(env, name_, name); 
 
 return (*env)->NewStringUTF(env, greeting); 
 }

Slide 89

Slide 89 text

http://tinyurl.com/jnidocs

Slide 90

Slide 90 text

JNI Reference • Mapping Java types to C types • References • Calling into Java • Exceptions • Synchronization

Slide 91

Slide 91 text

Tips • Android Studio • ProGuard • CheckJNI

Slide 92

Slide 92 text

Building JNI • Much improved in 2015 • Gradle! • Testing on desktop JVM

Slide 93

Slide 93 text

Testing JNI • You’ll want a separate Gradle module

Slide 94

Slide 94 text

Questions?