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

Post on 14-Jun-2015

679 Views

Category:

Technology

1 Downloads

Preview:

Click to see full reader

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

© 2013 IBM Corporation

Ryan  A.  Sciampacone  –  Managed  Run4me  Architect  25  September  2013  

Effec4ve  Foreign  Func4on  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

© 2013 IBM Corporation

Your  Speaker:  Ryan  A.  Sciampacone  

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

(E)  ryan_sciampacone@ca.ibm.com  (T)  @rsciampacone  

3

© 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

© 2013 IBM Corporation

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

5

© 2013 IBM Corporation

Why  have  a  FFI?  

• Plaborm  interfacing  

• Legacy  System  Support  

• Cross  Language  Interop  • Speed  

6

© 2013 IBM Corporation

Mo4va4ng  Example:  Missed  some  API  

• New  things  come  up  /  Some  things  missed  

7

© 2013 IBM Corporation

JNI  Call  Example  

Java:  

8

C:  

© 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

© 2013 IBM Corporation

JNI  API  Building  Blocks  

10

© 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

© 2013 IBM Corporation

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

12

© 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

© 2013 IBM Corporation

Basic  ID  use  

14

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

15

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

16

© 2013 IBM Corporation

Caching  Classes,  Method  IDs  and  Field  IDs  

24x!    

17

NoCache Cache

Field ID Caching

© 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

© 2013 IBM Corporation

Triggering  Array  Copies  

19

© 2013 IBM Corporation

Triggering  Array  Copies  

20

© 2013 IBM Corporation

Triggering  Array  Copies  

21

Elements Region

GetElements vs. GetRegion

8.5x!    

© 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

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

23

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

24

© 2013 IBM Corporation

Reaching  Back  for  Parameters  

25

Fetch Parameter

Fetching vs. Parameters

6x!    

© 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

© 2013 IBM Corporation

Local  Reference  Abuse  

27

© 2013 IBM Corporation

Local  Reference  Abuse  

28

© 2013 IBM Corporation

CORRECTNESS  PITFALLS  Hey  it  ran  didn’t  it?  

29

© 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

© 2013 IBM Corporation

Using  Array  API  Correctly  

31

© 2013 IBM Corporation

Using  Array  API  Correctly  

32

© 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

© 2013 IBM Corporation

Respec4ng  “Cri4cal”  Sec4ons  

34

© 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

© 2013 IBM Corporation

Check  your  excep4ons!  

36

© 2013 IBM Corporation

Check  your  excep4ons!  

37

© 2013 IBM Corporation

Check  your  return  values!  

38

© 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

© 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

© 2013 IBM Corporation

EVOLUTION  If  it  is  broken  you  should  probably  fix  it  

41

© 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

© 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  

© 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

© 2013 IBM Corporation

?  45

© 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

top related