SlideShare a Scribd company logo
1 of 20
Download to read offline
Challenges of Resource Management
                 in an OSGi Environment
                 Chris Gray, /K/ Embedded Java Solutions
                 Christer Larsson, Makewave

                 20 September 2011




            /K/ Embedded Java Solutions
                                                                            OSGi Alliance Marketing © 2008-2010 . All1
                                                                                                             Page
                 COPYRIGHT © 2008-2011 OSGi Alliance. All Rights Reserved
                                                                            Rights Reserved
onsdag den 21 september 2011
The Problem
                      •        OSGi is a fine dynamic modular system which
                               allows many independent modules to co-exist
                               within a single JVM
                      •         Supports the business objective of offering
                               3rd-party services on top of supplier-
                               provided services
                      •         BUT: there is a need to protect the platform
                               against badly-written / malicious bundles

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Types of Misbehavior



                                                                                       Other OS Process
                               Bundle                                        Bundle
                                                   Bundle

                                                                                       Other OS Process
                                  Bundle             Bundle                  Bundle



                                                OSGi Framework
                                                                                       Other OS Process
                                                    JVM Process

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Types of Misbehavior
                                                                                Excessive Memory
                                                                                    Allocation




                                                                                                   Other OS Process
                               Bundle                                    Bundle
                                                   Bundle

                                                                                                   Other OS Process
                                  Bundle             Bundle                  Bundle



                                                OSGi Framework
                                                                                                   Other OS Process
                                                    JVM Process

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Types of Misbehavior
         Excessive CPU usage, or                                                Excessive Memory
         using too many Threads                                                     Allocation




                                                                                                   Other OS Process
                                                                         Bundle
                               Bundle

                                                   Bundle

                                                                                                   Other OS Process
                                  Bundle             Bundle                  Bundle



                                                OSGi Framework
                                                                                                   Other OS Process
                                                    JVM Process

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Types of Misbehavior



                                                                                       Other OS Process




                                                                                       Other OS Process




                                                                                       Other OS Process
                                      JVM / OSGi Rebooting. Please Wait.....

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Potential solutions to the
                                 problem
                      • Use Resource Management inside the VM
                      • Run multiple VM instances to isolate critical
                               OSGi applications from user application
                      • Careful and rigorous testing of all bundles
                               before the are installed
                      • These three items can of course be
                               combined

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
A Mika / Knopflerfish solution - resource
                                  constrain the bundles
                  Add a Resource Manager that can account for Bundle Resources using built-in VM features




                                                                                           Other OS Process
                               Bundle                                        Bundle
                                                   Bundle

                                                                                           Other OS Process
                                  Bundle             Bundle                  Bundle


                                                                                Resource
                                           Knopflerfish OSGi Framework            Manager    Other OS Process
                                                    Mika Max

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Identifying the culprit (1)
                      •        The first step is to associate resource allocations with
                               modules.
                      •        Various systems have been devised for this, but generally
                               they are not transparent
                               •   Certain APIs must be called for its resource consumption to
                                   be monitored.

                               •   But there is no guarantee all code is written in this way

                               •   And legacy code, or 3rd party libs are certainly not!

                      •        Instead we propose to gather resource usage information
                               by instrumenting the Java runtime itself.

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Identifying the culprit - (2)
                      •        OSGi helps us by allocating a unique classloader to each
                               bundle. We define the “current bundle class loader” of a
                               thread T as follows:
                      •        If T is the system startup thread then the current bundle
                               class loader of T's base frame is the system class loader.
                      •        Otherwise the bundle class loader of T's base frame is
                               inherited from the thread frame which called T.start().
                      •        The bundle class loader of any other frame is the class
                               loader of the method it is executing, or if this is the system
                               class loader then it is inherited from the parent frame.

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Identifying the culprit - (3)
                      •        For the purpose of this paper the bootstrap class
                               loader, extension class loader, and system class loader
                               are all lumped together as “system”. Note that this
                               includes resources consumed by the framework itself.
                      •        The overhead is much less than for the “stack
                               inspection” required by the Java2 fine-grained security
                               model, because we only need to follow a few pointers
                               (frame→method→class→loader) and test for
                               equality. Could be further optimised using auxiliary
                               data structures.

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Resource Accounting
                      •        For each class loader we maintain a data structure with
                               counters for each monitored resource.
                               •   Count up when resource is allocated
                               •   Count down when released, BUT

                      •        Resource is often released by another thread, e.g. Garbage
                               collector or finalizer thread. Therefore we need to remember
                               which class loader allocated which resource! Furthermore the
                               resource may outlive the classloader which allocated it, so we
                               use a “handle” instead of a direct pointer to the class loader.
                      •        CPU usage is tricky, as depends on OS and on threading model.


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Bundle Resource Accounting
                                                            For each monitored bundle resources are accounted
                                                               and associated with the bundle’s Class Loader




                                                                         Class Loader
                                                                                              Other OS Process
                               Bundle                                        Bundle
                                                   Bundle
                                                                         Class Loader
                                                                                              Other OS Process
                                  Bundle             Bundle                  Bundle



                                           Knopflerfish OSGi Framework            Resource
                                                                                Manager       Other OS Process
                                                    Mika Max

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Bundle Resource Accounting
                                                            For each monitored bundle resources are accounted
                                                               and associated with the bundle’s Class Loader




                                                                         Class Loader
                                                                                              Other OS Process
                               Bundle                                        Bundle
                                                   Bundle
                                                                         Class Loader
                                                                                              Other OS Process
                                  Bundle             Bundle                  Bundle



                                           Knopflerfish OSGi Framework            Resource
                                                                                Manager       Other OS Process
                                                    Mika Max

                                                                  Operating System


                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Resource Accounting (2)
                      • Knopflerfish Pro has a Resource Manager
                      • For every bundle quota can be set on CPU,
                               memory and number of Threads used
                      • When a Bundle has exceeded his quota of
                               some resource the Resource Manager is
                               notified and can take action.
                      • But it is not always obvious what to do!
                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Dealing with Resource
                               Overconsumption (1)
                      •        The default action when the bundle over-
                               consumes is as follows
                               •   Overconsumption of Memory
                                   •   Throw an OutOfMemoryError

                               •   Overconsumption of Threads or CPU
                                   •   Throw an InternalError

                      •        Alternatively a user-defined callback can be
                               invoked at that point

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Dealing with Resource
                               Overconsumption (2)
                      •        The Resource Manager can supply information on over-
                               consuming bundles to a Management Agent.
                      •        The Management Agent can also query the Resource
                               Manager for current bundle resources use and take
                               action before quota is reached
                      •        The Management Agent can then decide to send an alarm
                               and / or stop the bundle.
                      •        For a well-behaving bundle this is no problem
                      •        But for a malicious, or badly written bundle that has gone
                               bananas there are new problems on the horizon...

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Difficulties with Java
                                     Threads
                      •        Java threads are not OS processes, i.e.
                               •   Java threads can not be destroyed, stopped or suspended.
                                   Attempting to do that is unsafe and may lead to unpredictable
                                   results. See javadoc for java.lang.Thread for more details.

                      •        This means Activator start() and stop() methods that do not
                               return can not be stopped or killed like an OS process.
                      •        If this happens in Knopflerfish we:
                               •   invalidate the Bundle after a timeout period so that bundle no
                                   longer is usable. All its services are unregistered and it can not
                                   get new services.

                               •   Set the threads prio to lowest possible.

                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Showtime!




                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011
Thank you




                               Chris Gray - /K/ Embedded Java Solutions - www.k-embedded-java.com
                                        Christer Larsson - Makewave - www.makewave.com



                 OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson


onsdag den 21 september 2011

More Related Content

More from mfrancis

Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
mfrancis
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
mfrancis
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
mfrancis
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
mfrancis
 
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
mfrancis
 
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
Journey from Monolith to a Modularized Application - Approach and Key Learnin...Journey from Monolith to a Modularized Application - Approach and Key Learnin...
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
mfrancis
 

More from mfrancis (20)

OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
OSGi with Docker - a powerful way to develop Java systems - Udo Hafermann (So...
 
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
A real world use case with OSGi R7 - Jurgen Albert (Data In Motion Consulting...
 
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
OSGi Feature Model - Where Art Thou - David Bosschaert (Adobe)
 
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
Migrating from PDE to Bndtools in Practice - Amit Kumar Mondal (Deutsche Tele...
 
OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)OSGi CDI Integration Specification - Ray Augé (Liferay)
OSGi CDI Integration Specification - Ray Augé (Liferay)
 
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
How OSGi drives cross-sector energy management - Jörn Tümmler (SMA Solar Tech...
 
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
Improved developer productivity thanks to Maven and OSGi - Lukasz Dywicki (Co...
 
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
It Was Twenty Years Ago Today - Building an OSGi based Smart Home System - Ch...
 
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)Popular patterns revisited on OSGi - Christian Schneider (Adobe)
Popular patterns revisited on OSGi - Christian Schneider (Adobe)
 
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
Integrating SLF4J and the new OSGi LogService 1.4 - BJ Hargrave (IBM)
 
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
OSG(a)i: because AI needs a runtime - Tim Verbelen (imec)
 
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
Flying to Jupiter with OSGi - Tony Walsh (ESA) & Hristo Indzhov (Telespazio V...
 
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
MicroProfile, OSGi was meant for this - Ray Auge (Liferay)
 
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
Prototyping IoT systems with a hybrid OSGi & Node-RED platform - Bruce Jackso...
 
How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)How to connect your OSGi application - Dirk Fauth (Bosch)
How to connect your OSGi application - Dirk Fauth (Bosch)
 
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
Visualization of OSGi based Software Architectures in Virtual Reality - Lisa ...
 
Turtles all the Way Up – From OSGi bundles to Fog Computing - Tim Ward (Paremus)
Turtles all the Way Up – From OSGi bundles to Fog Computing - Tim Ward (Paremus)Turtles all the Way Up – From OSGi bundles to Fog Computing - Tim Ward (Paremus)
Turtles all the Way Up – From OSGi bundles to Fog Computing - Tim Ward (Paremus)
 
OSGi in Action - How we use OSGi to build Open Liberty - Alasdair Nottingham ...
OSGi in Action - How we use OSGi to build Open Liberty - Alasdair Nottingham ...OSGi in Action - How we use OSGi to build Open Liberty - Alasdair Nottingham ...
OSGi in Action - How we use OSGi to build Open Liberty - Alasdair Nottingham ...
 
Software AG Application Modularity - OSGi and JPMS (Jigsaw)
Software AG Application Modularity - OSGi and JPMS (Jigsaw)Software AG Application Modularity - OSGi and JPMS (Jigsaw)
Software AG Application Modularity - OSGi and JPMS (Jigsaw)
 
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
Journey from Monolith to a Modularized Application - Approach and Key Learnin...Journey from Monolith to a Modularized Application - Approach and Key Learnin...
Journey from Monolith to a Modularized Application - Approach and Key Learnin...
 

Recently uploaded

Recently uploaded (20)

Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 

Challenges of resource management in an os gi environment chris gray + christer larsson

  • 1. Challenges of Resource Management in an OSGi Environment Chris Gray, /K/ Embedded Java Solutions Christer Larsson, Makewave 20 September 2011 /K/ Embedded Java Solutions OSGi Alliance Marketing © 2008-2010 . All1 Page COPYRIGHT © 2008-2011 OSGi Alliance. All Rights Reserved Rights Reserved onsdag den 21 september 2011
  • 2. The Problem • OSGi is a fine dynamic modular system which allows many independent modules to co-exist within a single JVM • Supports the business objective of offering 3rd-party services on top of supplier- provided services • BUT: there is a need to protect the platform against badly-written / malicious bundles OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 3. Types of Misbehavior Other OS Process Bundle Bundle Bundle Other OS Process Bundle Bundle Bundle OSGi Framework Other OS Process JVM Process Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 4. Types of Misbehavior Excessive Memory Allocation Other OS Process Bundle Bundle Bundle Other OS Process Bundle Bundle Bundle OSGi Framework Other OS Process JVM Process Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 5. Types of Misbehavior Excessive CPU usage, or Excessive Memory using too many Threads Allocation Other OS Process Bundle Bundle Bundle Other OS Process Bundle Bundle Bundle OSGi Framework Other OS Process JVM Process Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 6. Types of Misbehavior Other OS Process Other OS Process Other OS Process JVM / OSGi Rebooting. Please Wait..... Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 7. Potential solutions to the problem • Use Resource Management inside the VM • Run multiple VM instances to isolate critical OSGi applications from user application • Careful and rigorous testing of all bundles before the are installed • These three items can of course be combined OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 8. A Mika / Knopflerfish solution - resource constrain the bundles Add a Resource Manager that can account for Bundle Resources using built-in VM features Other OS Process Bundle Bundle Bundle Other OS Process Bundle Bundle Bundle Resource Knopflerfish OSGi Framework Manager Other OS Process Mika Max Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 9. Identifying the culprit (1) • The first step is to associate resource allocations with modules. • Various systems have been devised for this, but generally they are not transparent • Certain APIs must be called for its resource consumption to be monitored. • But there is no guarantee all code is written in this way • And legacy code, or 3rd party libs are certainly not! • Instead we propose to gather resource usage information by instrumenting the Java runtime itself. OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 10. Identifying the culprit - (2) • OSGi helps us by allocating a unique classloader to each bundle. We define the “current bundle class loader” of a thread T as follows: • If T is the system startup thread then the current bundle class loader of T's base frame is the system class loader. • Otherwise the bundle class loader of T's base frame is inherited from the thread frame which called T.start(). • The bundle class loader of any other frame is the class loader of the method it is executing, or if this is the system class loader then it is inherited from the parent frame. OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 11. Identifying the culprit - (3) • For the purpose of this paper the bootstrap class loader, extension class loader, and system class loader are all lumped together as “system”. Note that this includes resources consumed by the framework itself. • The overhead is much less than for the “stack inspection” required by the Java2 fine-grained security model, because we only need to follow a few pointers (frame→method→class→loader) and test for equality. Could be further optimised using auxiliary data structures. OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 12. Resource Accounting • For each class loader we maintain a data structure with counters for each monitored resource. • Count up when resource is allocated • Count down when released, BUT • Resource is often released by another thread, e.g. Garbage collector or finalizer thread. Therefore we need to remember which class loader allocated which resource! Furthermore the resource may outlive the classloader which allocated it, so we use a “handle” instead of a direct pointer to the class loader. • CPU usage is tricky, as depends on OS and on threading model. OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 13. Bundle Resource Accounting For each monitored bundle resources are accounted and associated with the bundle’s Class Loader Class Loader Other OS Process Bundle Bundle Bundle Class Loader Other OS Process Bundle Bundle Bundle Knopflerfish OSGi Framework Resource Manager Other OS Process Mika Max Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 14. Bundle Resource Accounting For each monitored bundle resources are accounted and associated with the bundle’s Class Loader Class Loader Other OS Process Bundle Bundle Bundle Class Loader Other OS Process Bundle Bundle Bundle Knopflerfish OSGi Framework Resource Manager Other OS Process Mika Max Operating System OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 15. Resource Accounting (2) • Knopflerfish Pro has a Resource Manager • For every bundle quota can be set on CPU, memory and number of Threads used • When a Bundle has exceeded his quota of some resource the Resource Manager is notified and can take action. • But it is not always obvious what to do! OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 16. Dealing with Resource Overconsumption (1) • The default action when the bundle over- consumes is as follows • Overconsumption of Memory • Throw an OutOfMemoryError • Overconsumption of Threads or CPU • Throw an InternalError • Alternatively a user-defined callback can be invoked at that point OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 17. Dealing with Resource Overconsumption (2) • The Resource Manager can supply information on over- consuming bundles to a Management Agent. • The Management Agent can also query the Resource Manager for current bundle resources use and take action before quota is reached • The Management Agent can then decide to send an alarm and / or stop the bundle. • For a well-behaving bundle this is no problem • But for a malicious, or badly written bundle that has gone bananas there are new problems on the horizon... OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 18. Difficulties with Java Threads • Java threads are not OS processes, i.e. • Java threads can not be destroyed, stopped or suspended. Attempting to do that is unsafe and may lead to unpredictable results. See javadoc for java.lang.Thread for more details. • This means Activator start() and stop() methods that do not return can not be stopped or killed like an OS process. • If this happens in Knopflerfish we: • invalidate the Bundle after a timeout period so that bundle no longer is usable. All its services are unregistered and it can not get new services. • Set the threads prio to lowest possible. OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 19. Showtime! OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011
  • 20. Thank you Chris Gray - /K/ Embedded Java Solutions - www.k-embedded-java.com Christer Larsson - Makewave - www.makewave.com OSGi Community Event 2011 Darmstadt - Chris Gray / Christer Larsson onsdag den 21 september 2011