Packed Objects: Fast Talking Java Meets Native Code - Steve Poole (IBM)

1,045 views

Published on

Presented at JAX London 2013

Worried about the future of Java? Want to see it keep moving forward? Don't be concerned. The transformation of Java is already underway. Driven by new technologies and new opportunities Java and the JVM are entering uncharted worlds and challenging old approaches. In this session learn about one such expedition in the form of an introductory talk to technology being developed by IBM. This experimental technology is exploring a new way to share data between the JVM and other runtimes.

Published in: Technology, Education
  • Be the first to comment

Packed Objects: Fast Talking Java Meets Native Code - Steve Poole (IBM)

  1. 1. Steve Poole IBM Fast Talking Java Meets Native Code
  2. 2. 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
  3. 3. Steve Poole ! Works at IBM’s Hursley Laboratory in the UK Involved in IBM Java VM development since before Java was 1 Currently leading IBM’s OpenJDK technical engagement
  4. 4. What this talk is about “Packed Objects” aka “Compact Off-Heap Structures in the Java Language” ! This technology is being developed to help address important pressures on Java ! This talk focuses on how to improve Java interop to Non Java applications
  5. 5. You’ll learn “Compact Off-Heap Structures in the Java Language” 1. An overview of this technology 2. Why we need your input and support 3. How to get started with using this technology yourself
  6. 6. Part 1 Native interoperability Why it’s so important C
  7. 7. Interoperability - why is it important? Java is built on talking native ! Operating Systems Processor Architectures File Systems User interfaces Network Interfaces ! Java keeps the WORA monsters away ! !
  8. 8. WORA - the components JDK Java code JVM JIT native Class Library Native code Your JNI code Your Java application
  9. 9. 2013 - Not so Java-centric JVM JIT Class Library native Your Native code JDK Java code Your JNI code Your Java application
  10. 10. 2013 - clusters and new processors non Java Runtime High speed memory channels (RDMA etc) Specialised Processors (GPUs etc)
  11. 11. 2013 - Can we survive with JNI 1.1? JIT ? Other runtime JVM Class Library Your Native code JDK Java code Your JNI code Your Java application native High speed memory channels (RDMA etc) Specialised Processors (GPUs etc) ? ?
  12. 12. 2013 - we can’t survive with JNI 1.1 • 1997 - JNI 1.1 • Designed for independence • Version independence • Platform Independence • VM Independence • Java at the centre of the world • Not performance focused
  13. 13. Packed Objects to the rescue • What we need is • To be able to share data between Java and other runtimes with minimal overhead • To not require a Java centric data view • and share data off-heap too! • Can we do that? • Time for a demo.
  14. 14. A simple demo • • Screensaver style Rectangles moved around the screen, bounce off the sides…
  15. 15. A simple demo Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! RGB colour elements ! typedef struct { int red,green,blue; float vx,vy; SDL_Rect rec; float x,y; ! } RECT; !15 position adjustment vectors SDL structure contains location , height and width float version of location - makes it easier to handle different h/w speeds
  16. 16. A simple demo Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !16
  17. 17. More than one demo Demo Standard - all C code calling the SDL graphics routines Mixed C and JNI. Java updates the location of each element Mixed C with PackedObjects Java updates the location of each element !17 Frames per second number of JNI calls per frame
  18. 18. Demo 1 - all C Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !18
  19. 19. Results Demo Standard - all C code calling the SDL graphics routines Mixed C and JNI. Java updates the location of each element Mixed C with PackedObjects Java updates the location of each element !19 Frames per second 20 number of JNI calls per frame 0
  20. 20. Demo 2- Mixed JNI and C Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !20
  21. 21. Results Demo Standard - all C code calling the SDL graphics routines Mixed C and JNI. Java updates the location of each element Mixed C with PackedObjects Java updates the location of each element !21 Frames per second 20 number of JNI calls per frame 0
  22. 22. er frame) Results Demo Frames per second Standard - all C code calling the SDL graphics routines 20 Mixed C and JNI. Java updates the location of each element 11 number of JNI calls per frame 0 200000 (~4MB per frame) Mixed C with PackedObjects Java updates the location of each element 200 000 JNI calls per frame. Copying data, validating data everytime. AND ensuring there is no chance of optimization by the JIT. !22
  23. 23. Demo 2- Details Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !23
  24. 24. Demo 2- Details Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! for every rectangle… C function calls Java method via JNI passes in x,y, vx and vy (by copy) Java method updates the rectangles new position Java native calls native method passing in new x,y, vx and vy values (by copy) C native method returns Java method returns C function returns !24
  25. 25. Demo 3 - Packed Objects Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !25
  26. 26. er frame) Results Demo Frames per second Standard - all C code calling the SDL graphics routines 20 Mixed C and JNI. Java updates the location of each element 11 Mixed C with PackedObjects Java updates the location of each element !26 number of JNI calls per frame 0 200000 (~4MB per frame)
  27. 27. er frame) Results Demo Frames per second number of JNI calls per frame Standard - all C code calling the SDL graphics routines Mixed C and JNI. Java updates the location of each element 11 200000 (~4MB per frame) Mixed C with PackedObjects Java updates the location of each element !27 20 18 1 (4 bytes per frame) 0
  28. 28. Demo 3 - Packed Objects Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! Initialise Per frame Update rectangle locations Draw rectangles with SDL terminate !28
  29. 29. Demo 3 - Packed Objects Draw 100 000 random sized rectangles using SDL ! Move them around the screen and get them to change direction if they hit the sides ! once per frame.. Java method updates each rectangle’s position directly ! } !29 for(Rectangle r: rectangles) { r.x=r.x+r.vx; r.y=r.y+r.vy; …
  30. 30. Today Java only speaks Java • Getting data into and out of Java has always required some form of marshaling or serialization process ! • Interaction with native data structures in memory is particularly problematic • JNI is the slowest but safest – But you need good C / C++ knowledge • Unsafe and NIO are faster but more challenging to use – They both have their own programming ‘model’ • If the Java side is in control of storage layouts it’s easier • When mapping existing native structures it’s much, much, much more difficult ! • Ironically the JVM is completely aware of native data formats
  31. 31. Tomorrow, with Packed Objects.. Java will let you reference data stored like this ! typedef struct { int red,green,blue; float vx,vy; SDL_Rect rec; float x,y; ! } RECT; C structure
  32. 32. Tomorrow, with Packed Objects.. as if it looked like this ! ! typedef struct { public class Rectangle { ! int red,green,blue; float vx,vy; SDL_Rect rec; float x,y; ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; ! } RECT; C structure } Java structure
  33. 33. Tomorrow, with Packed Objects.. ! ! typedef struct { public class Rectangle { ! int red,green,blue; float vx,vy; SDL_Rect rec; float x,y; ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; without copying ! } RECT; C structure } Java structure
  34. 34. Tomorrow, with Packed Objects.. ! !! ! typedef struct { ! int red,green,blue; ! float vx,vy; ! SDL_Rect rec; ! float x,y; !! ! } RECT; ! ! ‘C’ runtime And off ! ! ! public class Rectangle { ! ! ! int red,green,blue; ! float vx,xy; ! heap SDLRect rec; ! float x,y; ! ! ! } ! ! Java heap
  35. 35. Part 2 Under the covers http://www.flickr.com/photos/mwichary/
  36. 36. Under the covers ! public class Rectangle { Let’s look at these classes from a JVM point of view.. ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; ! } public class SDLRect { short x,y int width,height }
  37. 37. Under the covers ! ! ! ! ! ! ! ! ! ! ! int red int green int blue float vx float vy SLDRect ptr float x float y Java heap ! public class Rectangle { ! short x short y int width int height int red,green,blue; float vx,xy; SDLRect rec; float x,y; ! } public class SDLRect { short x,y int width,height } Java structure !37 J
  38. 38. ! ! ! ! ! ! ! ! ! ! ! Questions ! int red int green int blue float vx float vy SLDRect ptr float x float y Java heap !38 1 - How does a JVM ‘know’ the class of these objects? ! ! short x short y int width int height 2 - What are the data format rules for these types? ! ! 3 - When synchonizing on a object - where is the monitor stored?
  39. 39. ! ! ! ! ! ! ! ! ! ! ! int red int green int blue float vx float vy SLDRect ptr float x float y Java heap !39 Answers class ptr monitor ! class ptr monitor short x short y int width int height 1 - How does a JVM ‘know’ the class of these objects? Additional internal ptr ! 2 - What are the data format rules for these types? JVM impl specific (including ordering) 3 - When synchonizing on a object - where is the monitor stored? Additional internal field
  40. 40. How to add the metadata ! ! ! ! ! ! ! ! ! ! ! class ptr monitor int red int green int blue float vx float vy SLDRect ptr float x float y Java heap !40 class ptr monitor short x short y int width int height ! ! ! ! ! ! ! ! ! ! ! [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime No space No space for the extra for the extra Java fields -Java fields what do we what do we do? do?
  41. 41. Under the covers ! ! ! ! ! ! ! ! ! ! ! Java heap !41 ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  42. 42. Under the covers ! ! ! ! ! ! ! ! ! ! ! Java heap ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height } Rectangle r=PackedObject.newNativePackedObject(Rectangle.class,0x******); !42
  43. 43. Under the covers ! ! ! ! ! ! ! ! ! ! ! class ptr monitor data ptr “r” Java heap !43 ! ! ! ! ! ! Constructor ! was not ! called! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  44. 44. Rectangle r=PackedObject.newNativePackedObject(Rectangle.class,0x******); Under the covers ! ! ! ! ! ! ! ! ! ! ! class ptr monitor data ptr “r” Java heap SDLRect s=r.rect; ! !44 ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  45. 45. Rectangle r=PackedObject.newNativePackedObject(Rectangle.class,0x******); Under the covers ! ! ! ! ! ! ! ! ! ! ! class ptr monitor data ptr “r” “s” class ptr monitor data ptr Java heap SDLRect s=r.rect; ! !45 ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  46. 46. Rectangle r=PackedObject.newNativePackedObject(Rectangle.class,0x******); Under the covers ! ! ! ! ! ! ! ! ! ! ! class ptr monitor data ptr “r” “s” class ptr monitor data ptr Java heap SDLRect s=r.rect; SDLRect t=r.rect; !46 ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  47. 47. Rectangle r=PackedObject.newNativePackedObject(Rectangle.class,0x******); Under the covers ! ! ! ! ! ! ! ! ! ! ! class ptr monitor data ptr “r” “s” class ptr monitor data ptr “t” class ptr monitor data ptr Java heap SDLRect s=r.rect; SDLRect t=r.rect; !47 ! ! ! ! ! ! ! ! ! ! ! public class Rectangle { [4] red [4] green [4] blue [4] vx [4] vy [2] rect.x [2] rect.y [4] rect.width [4] rect.height [4] x [4] y ‘C’ runtime ! int red,green,blue; float vx,xy; SDLRect rec; float x,y; } public class SDLRect { short x,y int width,height }
  48. 48. Part 3 Truth and Consequences
  49. 49. Design considerations • Off heap Packed Objects are considered to already exist so constructors are not called. • Since there is limited opportunity to store the vital metadata it is constructed when needed. • The assumption is that creating new objects when accessing embedded data is confusing and dangerous. • How does ‘==‘ work? • What about synchronization? • The idea of ‘identityless’ objects is being discussed. • Something that PackedObjects has in common with ValueTypes • Could you handle Objects of this style?
  50. 50. The way forward? • This approach offers Java a way forward in the multi environment world • Almost zero cost data interop will benefit Java when exploiting new processors and memory systems. • There are additional use cases that ‘fall out’ from this design. • Low cost r/w of files • Instant application startup • Basic unmanaged memory capabilities • Under the covers heap usage benefits through better control for ‘locality of reference’
  51. 51. Is this the right or only way? • IBM and Oracle have been discussing these sorts of concepts for some time • We need your input. How important are these use cases to you? What’s missing, not needed? What do you hate? • We are trying to keep clear of syntax discussions at this point - it’s too early and just distracting.
  52. 52. You too can use Packed Objects • Google for “IBM Java 8 Beta”
  53. 53. Hard hats must be worn… The current prototype implementation surfaces a lot of the internals that will eventually just disappear This approach makes it easier for us to quickly revise the design but means users of the prototype have to work harder. ! AND ! it avoids syntax wars :-) picture courtesy http://www.flickr.com/people/karen_od/
  54. 54. Wrap up -hopefully you now: “Compact Off-Heap Structures in the Java Language” 1. Have a basic understanding of this technology 2. Know why we need your input and support 3. Know how to get started with using this technology Thank You

×