SlideShare uma empresa Scribd logo
1 de 21
Baixar para ler offline
Kilim-Clojure integration




              Message Passing Concurrency in Clojure using
                                Kilim

                              Antonio Garrote

                                  Forward


                             November 7, 2011
Kilim-Clojure integration




Clojure’s Concurrency Model




               JVM thread basic autonomous work unit
               Java shared memory explicit communication primitives
               Immutable data structures + implicit coordination
               mechanisms (STM, Refs)
Kilim-Clojure integration




Message Passing Communication

  1    % T e r m i t e Scheme b e n c h m a r k s r i n g . e r l
  2    m a k e r e l a y ( Next ) − >
  3           receive
  4                  K when K > 0 −          >
  5                         Next ! K − 1 ,
  6                         m a k e r e l a y ( Next ) ;
  7                  K−   >
  8                         Next ! K
  9          end .
 10
 11    l o o p (K , C u r r e n t , N) when N > 1 −          >
 12            l o o p (K , spawn ( r i n g , m a k e r e l a y , [ C u r r e n t ] ) ,N − 1 ) ;
 13
 14    l o o p (K , C u r r e n t , ) −>
 15            s e l f ( ) ! K,
 16            make relay ( Current ) .
 17
 18    % N = number p r o c e s s e s
 19    % K = number o f m e s s a g e e x c h a n g e s
 20    r i n g (N, K) −     >
 21            l o o p (K , s e l f ( ) , N ) .
Kilim-Clojure integration




Message Passing Communication


                            1
         2                      N
                                        N = 100K , K = 0
                                        518ms, 5µs process creation
   3                                K
                                        N = 100K , K = 1M
                                        759ms, 0.76µs local msg . send.
         4                      6
                            5
Kilim-Clojure integration




Message Passing Communication


       How does Erlang do it?


               User level processes executing recursive functions
               VM preemptive scheduling
               One heap/stack per process, no global heap
               Tail call optimisation
               More predictable GC behaviour
Kilim-Clojure integration




       Efficient implementation of message passing concurrency in the
       Java Virtual Machine?
Kilim-Clojure integration




Kilim


       “Kilim is a message-passing framework for Java that provides
       ultra-lightweight threads and facilities for fast, safe, zero-copy
       messaging between these threads”


               https://github.com/kilim/kilim
               2006 Sriram Srinivasan, Opera Group Cambridge University
               Current version 0.72
Kilim-Clojure integration




Kilim Tasks


                                                                                     Task

 1     p u b l i c c l a s s ExampleTask e x t e n d s Task {
 2                                                                                       execute
 3          p u b l i c void execute () throws Pausable {
 4                  doStuff ( ) ;
 5                  p u s a b l e S t u f f ( ) ; // t h r o w s P a u s a b l e    Worker
 6                  moreStuff ( ) ;                                                 Thread
 7          }
 8
 9    }                                                                                  notify

                                                                                   Scheduler
Kilim-Clojure integration




Kilim Weaving: Bytecode Transformation



               Additional step after compilation
               Transforms pausable methods
               Creates a custom class to store the task state
               Associates a Fiber object, tracks task’s stack
               Transformed code returns immediately if the task is paused
               Worker threads can resume the execution of paused stacks
               unwinding the associated fiber stack
Kilim-Clojure integration




Kilim Weaving: Bytecode Transformation
                 1    p u b l i c void execute ( Fiber f i b e r ) throws Pausable {
                 2            s w i t c h ( f i b e r . pc ) {
                 3               c a s e 0 : g o t o START ;
                 4               c a s e 1 : g o t o PAUSABLE CALL ;
                 5           }
                 6           START :
                 7               doStuff ( ) ;
                 8           PAUSABLE CALL :
                 9               f i b e r . down ( ) ;
                10               p u s a b l e S t u f f ( f i b e r ) ; // t h r o w s P a u s a b l e
                11               f i b e r . up ( ) ;
                12               switch ( f i b e r . status ) {
                13                        c a s e NOT PAUSING NO STATE :
                14                                g o t o RESUME ;
                15                        c a s e NOT PAUSING HAS STATE :
                16                                restoresState ( state );
                17                                g o t o RESUME ;
                18                        c a s e PAUSING NO STATE :
                19                                captureState ( state );
                20                                return ;
                21                        c a s e PAUSING HAS STATE :
                22                                return ;
                23               }
                24           RESUME :
                25               moreStuff ( ) ;
                26    }
Kilim-Clojure integration




Kilim Tasks: Execution

                                                                           Fiber.stack

                                           currentState                    StateObj.(1)
                            Pausable()
                                                                           StateObj.(2)
                                              currentState
                                  down()
                                                                           StateObj.(3)

                              Fiber         Pausable()

                                                                                  currentState
                                                       down()

                                               Fiber                 Pausable()



                                                                        up()


                                                             Pause             Normal Return
Kilim-Clojure integration




Kilim Messages




               Buffered mailboxes
               Allows asynchronous communication between tasks
               Blocking / not blocking interface
               Polling interface
Kilim-Clojure integration




Kilim-Clojure Integration (first attempt)




               Modified version of Clojure’s compiler
               Clojure’s functions executed as Kilim’s tasks
               Weaving performed at run-time


       https://github.com/antoniogarrote/clojure/tree/kilim
Kilim-Clojure integration




Kilim-Clojure Integration (second attempt)




               Clojure library
               [clj-kilim ”0.2.0-SNAPSHOT”]
               Doc: http://antoniogarrote.github.com/clojure kilim/
               Source: https://github.com/antoniogarrote/clojure kilim
Kilim-Clojure integration




Introduction

                Clojure
               Compiler


                      Compiles bytecode


              Java Class


                      Java instrumentation


                                               Kilim             Weaved Java
                clj-kilim
                                              Weaver                Class

                    Bytecode Transformation   Bytecode Transformation


                                                                     Class
                                                                    Loader
Kilim-Clojure integration




Kilim-Clojure Integration: API


  1    ( d e f − p a u s a b l e y i e l d i n g − f n [ mbox ]
  2          ( mbox−get−pausable mbox ) )
  3
  4
  5    ( def−pausable task−fn1 [ ]
  6       ...
  7       ( c a l l − p a u s a b l e y i e l d i n g − f n ambox )
  8       ...)
  9
 10    ( d e f task−fn2 ( p a u s a b l e        []    ...))
 11
 12
 13    ( t a s k − s t a r t task−fn1 )
 14    ( t a s k − s t a r t task−fn2 )
Kilim-Clojure integration




Kilim-Clojure Integration




       Demo
Kilim-Clojure integration




       Performance
Kilim-Clojure integration




Ring example again
  1    ( d e f n ˆ { : p a u s a b l e t r u e } make−relay [ ˆ k i l i m . M a i l b o x s e l f
  2                                                           ˆ k i l i m . Mailbox next
  3                                                           ˆ k i l i m . Mailbox f i n a l ]
  4        ( loop [ k ( r e c e i v e s e l f ) ]
  5            ( i f (> k 0 )
  6                ( do
  7                   ( ! next ( dec k ) )
  8                   ( recur ( . get s e l f )))
  9                ( do
 10                   ( ! next ( dec k ) )
 11                   ( when ( n o t ( n i l ? f i n a l ) )
 12                      (! f i n a l true ))))))
 13
 14
 15    ( d e f n make−loop [ n k ]
 16        ( l e t [ˆ k i l i m . Mailbox f i r s t − m a i l b o x ( k i l i m . Mailbox . )
 17                   ˆ k i l i m . Mailbox final−mailbox ( k i l i m . Mailbox . ) ]
 18            ( loop [ current           first−mailbox
 19                         n n]
 20               ( i f (> n 1 )
 21                   ( recur
 22                     ( spawn ( p a u s a b l e [ ˆ k i l i m . M a i l b o x s e l f ]
 23                                               ( make−relay s e l f c u r r e n t n i l ) ) )
 24                     ( dec n ) )
 25                   ( do ( spawn ( p a u s a b l e [ ˆ k i l i m . M a i l b o x      ]
 26                                                    ( make−relay f i r s t − m a i l b o x c u r r e n t f i n a l − m a i l b o x ) ) )
 27                             (! first−mailbox k)
 28                             ( . getb final−mailbox ) ) ) ) ) )
Kilim-Clojure integration




Performance




       N = 100K , K = 0
       1264ms, 12µs process creation

       N = 100K , K = 1M
       3276ms, 3µs local msg . send.
Kilim-Clojure integration




Conclusions


               Efficient message passing possible in the JVM thanks to Kilim
               Clojure can be a nice match for Kilim (immutable messages,
               run-time weaving)
               Benefits of asynchronous evented code with a nicer interface
               Limitations: non-preemptive
               Way to go?
                        Improve performance
                        Add distribution

Mais conteúdo relacionado

Semelhante a Message Passing Concurrency in Clojure using Kilim

Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Igalia
 
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada ProgramsAst2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada ProgramsGneuromante canalada.org
 
Range Extended Second Order Digital Phase Locked Loop
Range Extended Second Order Digital Phase Locked LoopRange Extended Second Order Digital Phase Locked Loop
Range Extended Second Order Digital Phase Locked LoopIDES Editor
 
The State of Lightweight Threads for the JVM
The State of Lightweight Threads for the JVMThe State of Lightweight Threads for the JVM
The State of Lightweight Threads for the JVMVolkan Yazıcı
 
Advanced Topics Sorting
Advanced Topics SortingAdvanced Topics Sorting
Advanced Topics SortingSri Prasanna
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil Witecki
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfSergioUlisesRojasAla
 
Introduction to Verilog & code coverage
Introduction to Verilog & code coverageIntroduction to Verilog & code coverage
Introduction to Verilog & code coverageJyun-Kai Hu
 
Errors errors, everywhere! - JSession
Errors errors, everywhere! - JSessionErrors errors, everywhere! - JSession
Errors errors, everywhere! - JSessionDaniel Pokusa
 
Nondeterministic testing of Sequential Quantum Logic Propositions on a Quant...
Nondeterministic testing of Sequential Quantum Logic  Propositions on a Quant...Nondeterministic testing of Sequential Quantum Logic  Propositions on a Quant...
Nondeterministic testing of Sequential Quantum Logic Propositions on a Quant...Matthew Leifer
 
Goblin Plonk.pdf
Goblin Plonk.pdfGoblin Plonk.pdf
Goblin Plonk.pdfAztecLabs
 
Learning LWF Chain Graphs: A Markov Blanket Discovery Approach
Learning LWF Chain Graphs: A Markov Blanket Discovery ApproachLearning LWF Chain Graphs: A Markov Blanket Discovery Approach
Learning LWF Chain Graphs: A Markov Blanket Discovery ApproachPooyan Jamshidi
 

Semelhante a Message Passing Concurrency in Clojure using Kilim (20)

Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
Channels, Concurrency, and Cores: A new Concurrent ML implementation (Curry O...
 
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada ProgramsAst2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs
 
Range Extended Second Order Digital Phase Locked Loop
Range Extended Second Order Digital Phase Locked LoopRange Extended Second Order Digital Phase Locked Loop
Range Extended Second Order Digital Phase Locked Loop
 
The State of Lightweight Threads for the JVM
The State of Lightweight Threads for the JVMThe State of Lightweight Threads for the JVM
The State of Lightweight Threads for the JVM
 
Advanced Topics Sorting
Advanced Topics SortingAdvanced Topics Sorting
Advanced Topics Sorting
 
Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)Fork Join (BeJUG 2012)
Fork Join (BeJUG 2012)
 
Kamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, codeKamil witecki asynchronous, yet readable, code
Kamil witecki asynchronous, yet readable, code
 
MSc_thesis
MSc_thesisMSc_thesis
MSc_thesis
 
Nondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdfNondeterministic Finite Automata AFN.pdf
Nondeterministic Finite Automata AFN.pdf
 
Introduction to Verilog & code coverage
Introduction to Verilog & code coverageIntroduction to Verilog & code coverage
Introduction to Verilog & code coverage
 
Lecture 23 loop transfer function
Lecture 23 loop transfer functionLecture 23 loop transfer function
Lecture 23 loop transfer function
 
Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!Fork/Join for Fun and Profit!
Fork/Join for Fun and Profit!
 
Errors errors, everywhere! - JSession
Errors errors, everywhere! - JSessionErrors errors, everywhere! - JSession
Errors errors, everywhere! - JSession
 
Mit cilk
Mit cilkMit cilk
Mit cilk
 
DHow2 - L6 VHDL
DHow2 - L6 VHDLDHow2 - L6 VHDL
DHow2 - L6 VHDL
 
2706264.ppt
2706264.ppt2706264.ppt
2706264.ppt
 
Nondeterministic testing of Sequential Quantum Logic Propositions on a Quant...
Nondeterministic testing of Sequential Quantum Logic  Propositions on a Quant...Nondeterministic testing of Sequential Quantum Logic  Propositions on a Quant...
Nondeterministic testing of Sequential Quantum Logic Propositions on a Quant...
 
NLP@ICLR2019
NLP@ICLR2019NLP@ICLR2019
NLP@ICLR2019
 
Goblin Plonk.pdf
Goblin Plonk.pdfGoblin Plonk.pdf
Goblin Plonk.pdf
 
Learning LWF Chain Graphs: A Markov Blanket Discovery Approach
Learning LWF Chain Graphs: A Markov Blanket Discovery ApproachLearning LWF Chain Graphs: A Markov Blanket Discovery Approach
Learning LWF Chain Graphs: A Markov Blanket Discovery Approach
 

Mais de Antonio Garrote Hernández

API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017Antonio Garrote Hernández
 
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...Antonio Garrote Hernández
 
RESTful writable APIs for the web of Linked Data using relational storage sol...
RESTful writable APIs for the web of Linked Data using relational storage sol...RESTful writable APIs for the web of Linked Data using relational storage sol...
RESTful writable APIs for the web of Linked Data using relational storage sol...Antonio Garrote Hernández
 

Mais de Antonio Garrote Hernández (7)

API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
API Modeling Framework: a toolbox ofr API specs. Gluecon 2017
 
Linked Data APIs (Funding Circle May 2015)
Linked Data APIs (Funding Circle May 2015)Linked Data APIs (Funding Circle May 2015)
Linked Data APIs (Funding Circle May 2015)
 
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
4th European Lisp Symposium: Jobim: an Actors Library for the Clojure Program...
 
RESTful writable APIs for the web of Linked Data using relational storage sol...
RESTful writable APIs for the web of Linked Data using relational storage sol...RESTful writable APIs for the web of Linked Data using relational storage sol...
RESTful writable APIs for the web of Linked Data using relational storage sol...
 
lisp (vs ruby) metaprogramming
lisp (vs ruby) metaprogramminglisp (vs ruby) metaprogramming
lisp (vs ruby) metaprogramming
 
Developing Distributed Semantic Systems
Developing Distributed Semantic SystemsDeveloping Distributed Semantic Systems
Developing Distributed Semantic Systems
 
Egearmand: an Erlang Gearman daemon
Egearmand: an Erlang Gearman daemonEgearmand: an Erlang Gearman daemon
Egearmand: an Erlang Gearman daemon
 

Último

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfOrbitshub
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 

Último (20)

Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 

Message Passing Concurrency in Clojure using Kilim

  • 1. Kilim-Clojure integration Message Passing Concurrency in Clojure using Kilim Antonio Garrote Forward November 7, 2011
  • 2. Kilim-Clojure integration Clojure’s Concurrency Model JVM thread basic autonomous work unit Java shared memory explicit communication primitives Immutable data structures + implicit coordination mechanisms (STM, Refs)
  • 3. Kilim-Clojure integration Message Passing Communication 1 % T e r m i t e Scheme b e n c h m a r k s r i n g . e r l 2 m a k e r e l a y ( Next ) − > 3 receive 4 K when K > 0 − > 5 Next ! K − 1 , 6 m a k e r e l a y ( Next ) ; 7 K− > 8 Next ! K 9 end . 10 11 l o o p (K , C u r r e n t , N) when N > 1 − > 12 l o o p (K , spawn ( r i n g , m a k e r e l a y , [ C u r r e n t ] ) ,N − 1 ) ; 13 14 l o o p (K , C u r r e n t , ) −> 15 s e l f ( ) ! K, 16 make relay ( Current ) . 17 18 % N = number p r o c e s s e s 19 % K = number o f m e s s a g e e x c h a n g e s 20 r i n g (N, K) − > 21 l o o p (K , s e l f ( ) , N ) .
  • 4. Kilim-Clojure integration Message Passing Communication 1 2 N N = 100K , K = 0 518ms, 5µs process creation 3 K N = 100K , K = 1M 759ms, 0.76µs local msg . send. 4 6 5
  • 5. Kilim-Clojure integration Message Passing Communication How does Erlang do it? User level processes executing recursive functions VM preemptive scheduling One heap/stack per process, no global heap Tail call optimisation More predictable GC behaviour
  • 6. Kilim-Clojure integration Efficient implementation of message passing concurrency in the Java Virtual Machine?
  • 7. Kilim-Clojure integration Kilim “Kilim is a message-passing framework for Java that provides ultra-lightweight threads and facilities for fast, safe, zero-copy messaging between these threads” https://github.com/kilim/kilim 2006 Sriram Srinivasan, Opera Group Cambridge University Current version 0.72
  • 8. Kilim-Clojure integration Kilim Tasks Task 1 p u b l i c c l a s s ExampleTask e x t e n d s Task { 2 execute 3 p u b l i c void execute () throws Pausable { 4 doStuff ( ) ; 5 p u s a b l e S t u f f ( ) ; // t h r o w s P a u s a b l e Worker 6 moreStuff ( ) ; Thread 7 } 8 9 } notify Scheduler
  • 9. Kilim-Clojure integration Kilim Weaving: Bytecode Transformation Additional step after compilation Transforms pausable methods Creates a custom class to store the task state Associates a Fiber object, tracks task’s stack Transformed code returns immediately if the task is paused Worker threads can resume the execution of paused stacks unwinding the associated fiber stack
  • 10. Kilim-Clojure integration Kilim Weaving: Bytecode Transformation 1 p u b l i c void execute ( Fiber f i b e r ) throws Pausable { 2 s w i t c h ( f i b e r . pc ) { 3 c a s e 0 : g o t o START ; 4 c a s e 1 : g o t o PAUSABLE CALL ; 5 } 6 START : 7 doStuff ( ) ; 8 PAUSABLE CALL : 9 f i b e r . down ( ) ; 10 p u s a b l e S t u f f ( f i b e r ) ; // t h r o w s P a u s a b l e 11 f i b e r . up ( ) ; 12 switch ( f i b e r . status ) { 13 c a s e NOT PAUSING NO STATE : 14 g o t o RESUME ; 15 c a s e NOT PAUSING HAS STATE : 16 restoresState ( state ); 17 g o t o RESUME ; 18 c a s e PAUSING NO STATE : 19 captureState ( state ); 20 return ; 21 c a s e PAUSING HAS STATE : 22 return ; 23 } 24 RESUME : 25 moreStuff ( ) ; 26 }
  • 11. Kilim-Clojure integration Kilim Tasks: Execution Fiber.stack currentState StateObj.(1) Pausable() StateObj.(2) currentState down() StateObj.(3) Fiber Pausable() currentState down() Fiber Pausable() up() Pause Normal Return
  • 12. Kilim-Clojure integration Kilim Messages Buffered mailboxes Allows asynchronous communication between tasks Blocking / not blocking interface Polling interface
  • 13. Kilim-Clojure integration Kilim-Clojure Integration (first attempt) Modified version of Clojure’s compiler Clojure’s functions executed as Kilim’s tasks Weaving performed at run-time https://github.com/antoniogarrote/clojure/tree/kilim
  • 14. Kilim-Clojure integration Kilim-Clojure Integration (second attempt) Clojure library [clj-kilim ”0.2.0-SNAPSHOT”] Doc: http://antoniogarrote.github.com/clojure kilim/ Source: https://github.com/antoniogarrote/clojure kilim
  • 15. Kilim-Clojure integration Introduction Clojure Compiler Compiles bytecode Java Class Java instrumentation Kilim Weaved Java clj-kilim Weaver Class Bytecode Transformation Bytecode Transformation Class Loader
  • 16. Kilim-Clojure integration Kilim-Clojure Integration: API 1 ( d e f − p a u s a b l e y i e l d i n g − f n [ mbox ] 2 ( mbox−get−pausable mbox ) ) 3 4 5 ( def−pausable task−fn1 [ ] 6 ... 7 ( c a l l − p a u s a b l e y i e l d i n g − f n ambox ) 8 ...) 9 10 ( d e f task−fn2 ( p a u s a b l e [] ...)) 11 12 13 ( t a s k − s t a r t task−fn1 ) 14 ( t a s k − s t a r t task−fn2 )
  • 19. Kilim-Clojure integration Ring example again 1 ( d e f n ˆ { : p a u s a b l e t r u e } make−relay [ ˆ k i l i m . M a i l b o x s e l f 2 ˆ k i l i m . Mailbox next 3 ˆ k i l i m . Mailbox f i n a l ] 4 ( loop [ k ( r e c e i v e s e l f ) ] 5 ( i f (> k 0 ) 6 ( do 7 ( ! next ( dec k ) ) 8 ( recur ( . get s e l f ))) 9 ( do 10 ( ! next ( dec k ) ) 11 ( when ( n o t ( n i l ? f i n a l ) ) 12 (! f i n a l true )))))) 13 14 15 ( d e f n make−loop [ n k ] 16 ( l e t [ˆ k i l i m . Mailbox f i r s t − m a i l b o x ( k i l i m . Mailbox . ) 17 ˆ k i l i m . Mailbox final−mailbox ( k i l i m . Mailbox . ) ] 18 ( loop [ current first−mailbox 19 n n] 20 ( i f (> n 1 ) 21 ( recur 22 ( spawn ( p a u s a b l e [ ˆ k i l i m . M a i l b o x s e l f ] 23 ( make−relay s e l f c u r r e n t n i l ) ) ) 24 ( dec n ) ) 25 ( do ( spawn ( p a u s a b l e [ ˆ k i l i m . M a i l b o x ] 26 ( make−relay f i r s t − m a i l b o x c u r r e n t f i n a l − m a i l b o x ) ) ) 27 (! first−mailbox k) 28 ( . getb final−mailbox ) ) ) ) ) )
  • 20. Kilim-Clojure integration Performance N = 100K , K = 0 1264ms, 12µs process creation N = 100K , K = 1M 3276ms, 3µs local msg . send.
  • 21. Kilim-Clojure integration Conclusions Efficient message passing possible in the JVM thanks to Kilim Clojure can be a nice match for Kilim (immutable messages, run-time weaving) Benefits of asynchronous evented code with a nicer interface Limitations: non-preemptive Way to go? Improve performance Add distribution