javaone 2013: effective foreign function interfaces: from jni to jnr

46
© 2013 IBM Corporation Ryan A. Sciampacone – Managed Run4me Architect 25 September 2013 Effec4ve Foreign Func4on Interfaces: From JNI to JNR

Upload: rsciampacone

Post on 14-Jun-2015

678 views

Category:

Technology


1 download

DESCRIPTION

Effective Foreign Function Interfaces: From JNI to JNR JavaOne 2013 CON4767 Ryan A. Sciampacone, Senior Software Developer, IBM JTC What do you do when your application needs access to platform features that aren’t available in the Java platform? You need a foreign function interface (FFI). The Java Native Interface (JNI) is the classic power tool for calling native code from your Java program. Using JNI means stepping out of the managed safety of the JVM into the wilds of native code. This session explains the most common JNI performance and correctness pitfalls and explains how to find and avoid them. As the JVM becomes the runtime of choice for more languages, the FFI landscape is also evolving. This session introduces alternative FFI approaches that minimizes effort (SWIG) and native code. It examines JNR in detail and shows how alternatives perform relative to handwritten JNI.

TRANSCRIPT

Page 1: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Ryan  A.  Sciampacone  –  Managed  Run4me  Architect  25  September  2013  

Effec4ve  Foreign  Func4on  Interfaces:  From  JNI  to  JNR  

Page 2: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Important  Disclaimers  THE  INFORMATION  CONTAINED  IN  THIS  PRESENTATION  IS  PROVIDED  FOR  INFORMATIONAL  PURPOSES  ONLY.    

WHILST  EFFORTS  WERE  MADE  TO  VERIFY  THE  COMPLETENESS  AND  ACCURACY  OF  THE  INFORMATION  CONTAINED  IN  THIS  PRESENTATION,  IT  IS  PROVIDED  “AS  IS”,  WITHOUT  WARRANTY  OF  ANY  KIND,  EXPRESS  OR  IMPLIED.    

ALL  PERFORMANCE  DATA  INCLUDED  IN  THIS  PRESENTATION  HAVE  BEEN  GATHERED  IN  A  CONTROLLED  ENVIRONMENT.    YOUR  OWN  TEST  RESULTS  MAY  VARY  BASED  ON  HARDWARE,  SOFTWARE  OR  INFRASTRUCTURE  DIFFERENCES.  

ALL  DATA  INCLUDED  IN  THIS  PRESENTATION  ARE  MEANT  TO  BE  USED  ONLY  AS  A  GUIDE.  

IN  ADDITION,  THE  INFORMATION  CONTAINED  IN  THIS  PRESENTATION  IS  BASED  ON  IBM’S  CURRENT  PRODUCT  PLANS  AND  STRATEGY,  WHICH  ARE  SUBJECT  TO  CHANGE  BY  IBM,  WITHOUT  NOTICE.    

IBM  AND  ITS  AFFILIATED  COMPANIES  SHALL  NOT  BE  RESPONSIBLE  FOR  ANY  DAMAGES  ARISING  OUT  OF  THE  USE  OF,  OR  OTHERWISE  RELATED  TO,  THIS  PRESENTATION  OR  ANY  OTHER  DOCUMENTATION.    

NOTHING  CONTAINED  IN  THIS  PRESENTATION  IS  INTENDED  TO,  OR  SHALL  HAVE  THE  EFFECT  OF:    

-­‐  CREATING  ANY  WARRANT  OR  REPRESENTATION  FROM  IBM,  ITS  AFFILIATED  COMPANIES  OR  ITS  OR  THEIR  SUPPLIERS  AND/OR  LICENSORS  

2

Page 3: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Your  Speaker:  Ryan  A.  Sciampacone  

• Run4me  Architect  @  IBM  (JTC)  •  Interpreters  • Garbage  Collec4on  • Plaborm  interfacing  /  op4miza4on  • Hardware  exploita4on  

(E)  [email protected]  (T)  @rsciampacone  

3

Page 4: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

What  should  you  get  from  this?  

• Why  a  foreign  func4on  interface  (FFI)  is  important  

• Performance  4ps  /  common  piballs  when  using  JNI  

• Next  steps  in  the  evolu4on  of  the  FFI  for  Java  

• At  the  very  least,  45  minutes  or  so  of  entertainment  

4

Page 5: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

WHY  IS  AN  FFI  IMPORTANT?  What  the  Java  Na4ve  Interface  (JNI)  does  for  you…  

5

Page 6: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Why  have  a  FFI?  

• Plaborm  interfacing  

• Legacy  System  Support  

• Cross  Language  Interop  • Speed  

6

Page 7: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Mo4va4ng  Example:  Missed  some  API  

• New  things  come  up  /  Some  things  missed  

7

Page 8: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

JNI  Call  Example  

Java:  

8

C:  

Page 9: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Benefits  of  the  JNI  implementa4on  

• API  based  • GC  Safety  

• Java  /  JVM  based  ac4vity  not  blocked  

• Plaborm  agnos4c  

• Clear  lines  between  Java  /  plaborm  

• Founda4on  for  JVM  Tooling  (JVMTI)  

9

Page 10: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

JNI  API  Building  Blocks  

10

Page 11: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

JNIEnv  is  your  lifeline  

• JNIEnv  is  the  connec4on  back  to  the  “Java  world”  • Calls  through  the  JNIEnv  are  effec4vely  round  trip  

• Performance  is  dependent  on  keeping  round  trips  down  

11

Page 12: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

PERFORMANCE  PITFALLS  But  it’s  already  slow,  what’s  a  few  more  instruc4ons?  

12

Page 13: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

• Core  elements  when  interac4ng  with  objects  

• Effec4vely  your  na4ve  “resolve”  point  

• Not  as  cheap  to  acquire  • So  cache  them!  

13

Page 14: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Basic  ID  use  

14

Page 15: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

15

Page 16: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

16

Page 17: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

24x!    

17

NoCache Cache

Field ID Caching

Page 18: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Triggering  Array  Copies  

• Remember:  No  direct  manipula4on  of  Java  Objects  

• Basically  3  categories  of  API:  •  Some4mes  copying  the  en4re  array  (Get*ArrayElements)  • Always  copy  a  por4on  of  the  array  (Get*ArrayRegion)  •  (Usually)  Direct  on-­‐heap  access  (GetPrimi4veArrayCri4cal)  

• Be  aware  of  the  cost  of  copying!  

18

Page 19: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Triggering  Array  Copies  

19

Page 20: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Triggering  Array  Copies  

20

Page 21: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Triggering  Array  Copies  

21

Elements Region

GetElements vs. GetRegion

8.5x!    

Page 22: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

• We  all  hate  huge  func4on  prototypes  

• Basic  teaching  is  pass  the  object  go  from  there  

• Think  –  what  is  the  cost  of  accessing  fields  in  JNI?  

• Pass  what  you  need  as  arguments  

22

Page 23: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

23

Page 24: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

24

Page 25: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

25

Fetch Parameter

Fetching vs. Parameters

6x!    

Page 26: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Local  Reference  Abuse  

• Local  references  are  beau4ful  for  hiding  JVM-­‐isms  

• And  they  get  blissfully  ignored  by  a  lot  of  na4ve  code  

26

Page 27: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Local  Reference  Abuse  

27

Page 28: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Local  Reference  Abuse  

28

Page 29: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

CORRECTNESS  PITFALLS  Hey  it  ran  didn’t  it?  

29

Page 30: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Using  Array  API  Correctly  

• Recap:  Basically  3  categories  of  API  •  Some4mes  copying  the  en4re  array  (Get*ArrayElements)  • Always  copy  a  por4on  of  the  array  (Get*ArrayRegion)  •  (Usually)  Direct  on-­‐heap  access  (GetPrimi4veArrayCri4cal)  

• You  need  to  be  certain  that  you  are  geqng  a  copy!  

• Affected  by  VM  vendor,  GC  technology,  and  version  of  JDK  

30

Page 31: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Using  Array  API  Correctly  

31

Page 32: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Using  Array  API  Correctly  

32

Page 33: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Respec4ng  “Cri4cal”  Sec4ons  

• Cri4cal  API:  Get/Release  for  (almost)  assured  direct  access  

•  Idea:  Direct  on-­‐heap  access  which  locks  out  certain  JVM  events  

• General  advice:  Do  not  use  JNI  API  between  Get  and  Release  

33

Page 34: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Respec4ng  “Cri4cal”  Sec4ons  

34

Page 35: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Check  your  excep4ons!  

• Java  code  and  IDEs  really  help  you  with  excep4ons  • Na4ve  code  doesn’t  give  you  this  feedback  

• JNI  API  can  raise  excep4ons  -­‐  Check  and  act  accordingly  • You  are  the  try/catch  block  now!  

35

Page 36: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Check  your  excep4ons!  

36

Page 37: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Check  your  excep4ons!  

37

Page 38: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Check  your  return  values!  

38

Page 39: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Maintain  your  Global  References  

• Global  references  aren’t  cleaned  up  like  Local  references  • Great  for  caching,  but  watch  out  for  leaks  

39

Page 40: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Use  the  correct  JNIEnv  

• JNIEnv  are  unique  to  your  thread  • Using  the  wrong  one  can  cause  subtle  problems  

• GetEnv  /  GetJavaVM  exist  from  the  invoca4on  API  

• For  callbacks,  make  sure  you  have  the  correct  one  available  

40

Page 41: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

EVOLUTION  If  it  is  broken  you  should  probably  fix  it  

41

Page 42: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

JNI  is  good,  but  some4mes  its  in  the  way  

• Direct  func4on  calls  • Speed  

• Marshalling  data  is  expensive  

• Unsigned  types?  • Op4mizing  the  JNI  interface  can  be  tricky  

42

Page 43: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Packed  Objects  

• A  solu4on  to  accessing  na4ve  data  •  Session:  An  Introduc4on  to  Packed  Objects  (CON5758)  

43

int  int  

Java   Na4ve  

int[]  d  

anObject  

int  int  

int  …

Object  header  

Object  field  /  data  

Page 44: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

References  

•  Introduc4on  to  Packed  Objects  (CON5758)  • JVMLS  2013:  Packed  Objects  (Sciampacone)  hup://goo.gl/jWv2Jx  

• John  Duïmovich  (IBM  Java  CTO)  hup://goo.gl/OGeDix  

• JNI  Best  Prac4ces  (Dawson,Johnson,Low)  hup://goo.gl/hvyGDY  

44

Page 45: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

?  45

Page 46: JavaOne 2013: Effective Foreign Function Interfaces: From JNI to JNR

© 2013 IBM Corporation

Copyright  and  Trademarks  

©  IBM  Corpora4on  2013.  All  Rights  Reserved.      IBM,  the  IBM  logo,  and  ibm.com  are  trademarks  or  registered  trademarks  of  Interna4onal  Business  Machines  Corp.,  and  registered  in  many  jurisdic4ons  worldwide.    

 Other  product  and  service  names  might  be  trademarks  of  IBM  or  other  companies.      A  current  list  of  IBM  trademarks  is  available  on  the  Web  –  see  the  IBM  “Copyright  and  trademark  informa4on”  page  at  URL:    www.ibm.com/legal/copytrade.shtml  

46