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

1,238 views

Published on

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.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,238
On SlideShare
0
From Embeds
0
Number of Embeds
121
Actions
Shares
0
Downloads
16
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

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

  1. 1. © 2013 IBM Corporation Ryan  A.  Sciampacone  –  Managed  Run4me  Architect   25  September  2013   Effec4ve  Foreign  Func4on  Interfaces:   From  JNI  to  JNR  
  2. 2. © 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
  3. 3. © 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
  4. 4. © 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
  5. 5. © 2013 IBM Corporation WHY  IS  AN  FFI  IMPORTANT?   What  the  Java  Na4ve  Interface  (JNI)  does  for  you…   5
  6. 6. © 2013 IBM Corporation Why  have  a  FFI?   • Plaborm  interfacing   • Legacy  System  Support   • Cross  Language  Interop   • Speed   6
  7. 7. © 2013 IBM Corporation Mo4va4ng  Example:  Missed  some  API   • New  things  come  up  /  Some  things  missed   7
  8. 8. © 2013 IBM Corporation JNI  Call  Example   Java:   8 C:  
  9. 9. © 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
  10. 10. © 2013 IBM Corporation JNI  API  Building  Blocks   10
  11. 11. © 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
  12. 12. © 2013 IBM Corporation PERFORMANCE  PITFALLS   But  it’s  already  slow,  what’s  a  few  more  instruc4ons?   12
  13. 13. © 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
  14. 14. © 2013 IBM Corporation Basic  ID  use   14
  15. 15. © 2013 IBM Corporation Caching  Classes,  Method  IDs  and  Field  IDs   15
  16. 16. © 2013 IBM Corporation Caching  Classes,  Method  IDs  and  Field  IDs   16
  17. 17. © 2013 IBM Corporation Caching  Classes,  Method  IDs  and  Field  IDs   24x!     17 NoCache Cache Field ID Caching
  18. 18. © 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
  19. 19. © 2013 IBM Corporation Triggering  Array  Copies   19
  20. 20. © 2013 IBM Corporation Triggering  Array  Copies   20
  21. 21. © 2013 IBM Corporation Triggering  Array  Copies   21 Elements Region GetElements vs. GetRegion 8.5x!    
  22. 22. © 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
  23. 23. © 2013 IBM Corporation Reaching  Back  for  Parameters   23
  24. 24. © 2013 IBM Corporation Reaching  Back  for  Parameters   24
  25. 25. © 2013 IBM Corporation Reaching  Back  for  Parameters   25 Fetch Parameter Fetching vs. Parameters 6x!    
  26. 26. © 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
  27. 27. © 2013 IBM Corporation Local  Reference  Abuse   27
  28. 28. © 2013 IBM Corporation Local  Reference  Abuse   28
  29. 29. © 2013 IBM Corporation CORRECTNESS  PITFALLS   Hey  it  ran  didn’t  it?   29
  30. 30. © 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
  31. 31. © 2013 IBM Corporation Using  Array  API  Correctly   31
  32. 32. © 2013 IBM Corporation Using  Array  API  Correctly   32
  33. 33. © 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
  34. 34. © 2013 IBM Corporation Respec4ng  “Cri4cal”  Sec4ons   34
  35. 35. © 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
  36. 36. © 2013 IBM Corporation Check  your  excep4ons!   36
  37. 37. © 2013 IBM Corporation Check  your  excep4ons!   37
  38. 38. © 2013 IBM Corporation Check  your  return  values!   38
  39. 39. © 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
  40. 40. © 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
  41. 41. © 2013 IBM Corporation EVOLUTION   If  it  is  broken  you  should  probably  fix  it   41
  42. 42. © 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
  43. 43. © 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  
  44. 44. © 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
  45. 45. © 2013 IBM Corporation ?  45
  46. 46. © 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

×