SlideShare a Scribd company logo
Non-Blocking Strategies for FFI
Don’t Block me Now!
Pablo Tesone
Pharo Consortium Engineer
1
Guille Polito
CNRS UMR9189

CRIStAL, Inria

RMoD
Pablo Tesone
Pharo Consortium
Engineer
Guille Polito
CNRS Engineer

RMod Team
• 10 years of experience in industrial
applications

• PhD in Dynamic Software Update

• Interested in improving
development tools and the daily
development process. 

• Enthusiast of the object oriented
programming and their tools.
• Experience industrial on service-
oriented and mobile applications.

• PhD in Computer Science

• Main research interests are
modularity and development tools.

• In the Pharo community since 2010

• More noticeable contributions:
Pharo Bootstrap process and
Iceberg.
FFI? Foreign Function Interface
Image
VM
External Libraries
FFI
We can communicate with anything that
has a C API
Operating System API
!3
Unified FFI in a nutshell
UFFI handles:
- Look-up of functions
- Marshalling of arguments
- Execution
- Marshalling of the return values
Concurrency in Pharo
!5
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
VM Thread
Concurrency in Pharo
!6
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
VM
handles
process
scheduling
VM Thread
Concurrency in Pharo
!7
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
p1
int function(char* foo, int bar)
VM Thread
Concurrency in Pharo
!8
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
p1
Out
of
VM
VM
loses
control
int function(char* foo, int bar)
VM Thread
Conceptual Non-Blocking FFI
!9
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
p1
p3
p2
?
int function(char* foo, int bar)
VM Thread
Strategy #1: Thread per Call-out
!10
Interpreter
Thread
p1
p2
p1
p2
Thread 1
<<Spawn>>
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2 VM Thread
Strategy #1: Thread per Call-out
!11
Interpreter
Thread
p1
p2
p1
p2
Thread 1
<<Spawn>>
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2
• Simple
• Expensive to spawn threads
• Calls are not in the same thread
• Cannot reuse existing threads (e.g., UI threads)
Not all libraries are
designed equally
• Different requirements

• Must run in the main thread (Cocoa)

• Must run in a single thread (Gtk+3)

• Runs on any thread but not concurrent (libgit, sqlite)

• Is a Thread-safe Library

• ….
!12
We need different
Strategies to choose from
!13
We need to choose different
strategies for each library
Strategy #2: Worker Threads
!14
Interpreter
Thread
p1
p2
p1
p2
Worker #1
<<Enqueue>>
Call out Queue
of Worker #1
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2 VM Thread
Strategy #2: Worker Threads
!15
Interpreter
Thread
p1
p2
p1
p2
Worker #1
<<Enqueue>>
Call out Queue
of Worker #1
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2
• Simple
• Group related calls
• No thread spawn overhead
• Expensive Callouts (synchronising queue)
• Do not support main thread!
Strategy #3: VM Thread Runner
!16
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
int function(char* foo, int bar)
p1
VM Thread
Strategy #3: VM Thread Runner
!17
P1 P2 P3
Interpreter
Thread
p1
p3
p1
p2
p2
int function(char* foo, int bar)
p1
• Simpler
• Group related calls
• No thread spawn overhead
• Backward compatibility
• Blocking
Strategy #4: Main Thread Runner
Interpreter
Thread
p1
p2
p1
p2
Worker #1
<<Enqueue>>
Call out Queue
of Worker #1
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2 VM Thread
Strategy #4: Main Thread Runner
Interpreter
Thread
p1
p2
p1
p2
Worker #1
<<Enqueue>>
Call out Queue
of Worker #1
<<Signal Semaphore>>
int function(char* foo, int bar)
P1 P2
• Simple
• Group related calls
• No thread spawn overhead
• Supports main thread
• Expensive Callouts (synchronising queue)
• VM should be run in separate thread
Strategy #5: Global Interpreter Lock
int function(char* foo, int bar)
P1 P2
Interpreter
#1
Interpreter
#2
!20
VM Thread #1 VM Thread #2
Strategy #5: Global Interpreter Lock
int function(char* foo, int bar)
P1 P2
Interpreter
#1
Interpreter
#2
!21
• Group related calls
• No thread spawn overhead
• No Backward compatibility
• Application should be written with threading
in mind
• Requires VM modification
Strategy #6: Thread-safe interpreters
int function(char* foo, int bar)
!22
P1 P2
Interpreter
#1
Interpreter
#2
VM Thread #1 VM Thread #2
Strategy #6: Thread-safe interpreters
int function(char* foo, int bar)
!23
P1 P2
Interpreter
#1
Interpreter
#2
• Does not exist for Pharo
• Requires extensive modification of VM,
Plugins and Image core libraries
• Application should be written with threading
in mind
• Real multithreading not only for FFI
Implementations
!24
Queue Based FFI
(Pharo Threaded FFI Plugin)
GILda VM
(Global Interpreter Lock VM)
Strategy #1: Thread per Call-out
Strategy #2: Worker Threads
Strategy #3: VM Thread Runner
Strategy #5: Global Interpreter Lock
Strategy #4: Main Thread Runner
Future???
Strategy #6: Thread-safe
interpreters
Implementations
!25
Queue Based FFI
(Pharo Threaded FFI Plugin)
GILda VM
(Global Interpreter Lock VM)
Strategy #1: Thread per Call-out
Strategy #2: Worker Threads
Strategy #3: VM Thread Runner
Strategy #5: Global Interpreter Lock
Strategy #4: Main Thread Runner
Future???
Strategy #6: Thread-safe
interpreters
Transparency through
UFFI
!26
Your Program
Unified FFI
Pharo Threaded FFI
Pharo Threaded FFI Plugin
S#1 S#2 S#3 S#4
Squeak FFI
Squeak FFI
Plugin
Library
Decision Table
!27
Same Thread Worker Threads
Long Calls
Blocking Parallel
Short Calls
Little Overhead Lots of Overhead
OPTIMIZATIONS
Implementations
!28
GILda VM
(Global Interpreter Lock VM)
Strategy #5: Global Interpreter Lock
Queue Based FFI
(Pharo Threaded FFI Plugin)
Strategy #1: Thread per Call-out
Strategy #2: Worker Threads
Strategy #3: VM Thread Runner
Strategy #4: Main Thread Runner
Future???
Strategy #6: Thread-safe
interpreters
More Details
IWST - Virtual Machines Session
Thursday 11:00 am
Start Using It!
!29
Metacello new
baseline: ‘ThreadedFFI'
repository:
‘github://pharo-project/threadedFFI-Plugin/src';
load
pharo-project/threadedFFI-Plugin
Only in Pharo 8 + Headless VM
Conclusion
• Beta Version (In usage for Gtk+)

• Transparent for the user

• Strategies selected in the Image

• Uses LibFFI

• Re-entrant Callback support

• Tests!
!30
Preliminar results
• All marshalling image side + Lib FFI

• Short call

• Same thread 27 us

• Single worker thread 6791 us

• 2 Parallel long Calls (1 second per call)

• Same Thread 2001.9 ms

• Different working threads 1006.4ms

More Related Content

What's hot

Dataflow Management From Edge to Core with Apache NiFi
Dataflow Management From Edge to Core with Apache NiFiDataflow Management From Edge to Core with Apache NiFi
Dataflow Management From Edge to Core with Apache NiFi
DataWorks Summit
 

What's hot (20)

Linux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old SecretsLinux Performance Analysis: New Tools and Old Secrets
Linux Performance Analysis: New Tools and Old Secrets
 
Nginx
NginxNginx
Nginx
 
Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...
Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...
Best Practices for ETL with Apache NiFi on Kubernetes - Albert Lewandowski, G...
 
NGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA BroadcastNGINX: Basics & Best Practices - EMEA Broadcast
NGINX: Basics & Best Practices - EMEA Broadcast
 
Intro to the Alfresco Public API
Intro to the Alfresco Public APIIntro to the Alfresco Public API
Intro to the Alfresco Public API
 
Futex Scaling for Multi-core Systems
Futex Scaling for Multi-core SystemsFutex Scaling for Multi-core Systems
Futex Scaling for Multi-core Systems
 
Dataflow Management From Edge to Core with Apache NiFi
Dataflow Management From Edge to Core with Apache NiFiDataflow Management From Edge to Core with Apache NiFi
Dataflow Management From Edge to Core with Apache NiFi
 
Unix v6 Internals
Unix v6 InternalsUnix v6 Internals
Unix v6 Internals
 
Attack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and KibanaAttack monitoring using ElasticSearch Logstash and Kibana
Attack monitoring using ElasticSearch Logstash and Kibana
 
Ch03 請求與回應
Ch03 請求與回應Ch03 請求與回應
Ch03 請求與回應
 
Free rtos seminar
Free rtos seminarFree rtos seminar
Free rtos seminar
 
New Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using TracingNew Ways to Find Latency in Linux Using Tracing
New Ways to Find Latency in Linux Using Tracing
 
Delivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWSDelivering High-Availability Web Services with NGINX Plus on AWS
Delivering High-Availability Web Services with NGINX Plus on AWS
 
Making Linux do Hard Real-time
Making Linux do Hard Real-timeMaking Linux do Hard Real-time
Making Linux do Hard Real-time
 
제프리 리처의 Windows via C/C++ : 8장 유저 모드에서의 스레드 동기화
제프리 리처의 Windows via C/C++ : 8장 유저 모드에서의 스레드 동기화제프리 리처의 Windows via C/C++ : 8장 유저 모드에서의 스레드 동기화
제프리 리처의 Windows via C/C++ : 8장 유저 모드에서의 스레드 동기화
 
How Functions Work
How Functions WorkHow Functions Work
How Functions Work
 
ZStack architecture overview
ZStack architecture overviewZStack architecture overview
ZStack architecture overview
 
Nginx
NginxNginx
Nginx
 
Simulcast/SVC @ IIT-RTC 2019
Simulcast/SVC @ IIT-RTC 2019Simulcast/SVC @ IIT-RTC 2019
Simulcast/SVC @ IIT-RTC 2019
 
Nifi
NifiNifi
Nifi
 

Similar to Non-Blocking Strategies for FFI

FFR GreenKiller - Automatic kernel-mode malware analysis system
FFR GreenKiller - Automatic kernel-mode malware analysis systemFFR GreenKiller - Automatic kernel-mode malware analysis system
FFR GreenKiller - Automatic kernel-mode malware analysis system
FFRI, Inc.
 
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
Maksim Shudrak
 

Similar to Non-Blocking Strategies for FFI (20)

Improving the Pharo VM
Improving the Pharo VMImproving the Pharo VM
Improving the Pharo VM
 
Present and Future of the Pharo VM: Headless and Beyond
 Present and Future of the Pharo VM: Headless and Beyond Present and Future of the Pharo VM: Headless and Beyond
Present and Future of the Pharo VM: Headless and Beyond
 
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream ProjectsITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
ITCamp 2017 - Raffaele Rialdi - Adopting .NET Core in Mainstream Projects
 
What is (not) Pharo 8?
What is (not) Pharo 8?What is (not) Pharo 8?
What is (not) Pharo 8?
 
[Rakuten TechConf2014] [E-4] Rakuten Front-end Framework Project
[Rakuten TechConf2014] [E-4] Rakuten Front-end Framework Project[Rakuten TechConf2014] [E-4] Rakuten Front-end Framework Project
[Rakuten TechConf2014] [E-4] Rakuten Front-end Framework Project
 
Presentation 3 software developer in rfid
Presentation 3 software developer in rfidPresentation 3 software developer in rfid
Presentation 3 software developer in rfid
 
Возможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OSВозможности интерпретатора Python в NX-OS
Возможности интерпретатора Python в NX-OS
 
GildaVM: a Non-Blocking I/O Architecture for the Cog VM
GildaVM: a Non-Blocking I/O Architecture for the Cog VMGildaVM: a Non-Blocking I/O Architecture for the Cog VM
GildaVM: a Non-Blocking I/O Architecture for the Cog VM
 
Using Aspects for Language Portability (SCAM 2010)
Using Aspects for Language Portability (SCAM 2010)Using Aspects for Language Portability (SCAM 2010)
Using Aspects for Language Portability (SCAM 2010)
 
Pharo Consortium: A roadmap to solid evolution
Pharo Consortium: A roadmap to solid evolutionPharo Consortium: A roadmap to solid evolution
Pharo Consortium: A roadmap to solid evolution
 
Pharo: A roadmap to solid evolution.
Pharo: A roadmap to solid evolution.Pharo: A roadmap to solid evolution.
Pharo: A roadmap to solid evolution.
 
FFR GreenKiller - Automatic kernel-mode malware analysis system
FFR GreenKiller - Automatic kernel-mode malware analysis systemFFR GreenKiller - Automatic kernel-mode malware analysis system
FFR GreenKiller - Automatic kernel-mode malware analysis system
 
Industrialise PHP ~ ZendCon Europe 2013
Industrialise PHP ~ ZendCon Europe 2013Industrialise PHP ~ ZendCon Europe 2013
Industrialise PHP ~ ZendCon Europe 2013
 
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
Fuzzing malware for fun & profit. Applying Coverage-Guided Fuzzing to Find Bu...
 
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
Zero bugs found? Hold my beer AFL! how to improve coverage-guided fuzzing and...
 
DEF CON 27 - MAKSIM SHUDRAK - zero bugs found hold my beer afl how to improve...
DEF CON 27 - MAKSIM SHUDRAK - zero bugs found hold my beer afl how to improve...DEF CON 27 - MAKSIM SHUDRAK - zero bugs found hold my beer afl how to improve...
DEF CON 27 - MAKSIM SHUDRAK - zero bugs found hold my beer afl how to improve...
 
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
44CON London 2015 - Reverse engineering and exploiting font rasterizers: the ...
 
Programming on Windows 8.1: The New Stream and Storage Paradigm (Raffaele Ria...
Programming on Windows 8.1: The New Stream and Storage Paradigm (Raffaele Ria...Programming on Windows 8.1: The New Stream and Storage Paradigm (Raffaele Ria...
Programming on Windows 8.1: The New Stream and Storage Paradigm (Raffaele Ria...
 
OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Symposium_Python Bindings_9.21.11OpenSAF Symposium_Python Bindings_9.21.11
OpenSAF Symposium_Python Bindings_9.21.11
 
Pharo IoT Installation Improvements and Continuous Integration
Pharo IoT Installation Improvements and Continuous IntegrationPharo IoT Installation Improvements and Continuous Integration
Pharo IoT Installation Improvements and Continuous Integration
 

More from ESUG

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
ESUG
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
ESUG
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
ESUG
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
ESUG
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
ESUG
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
ESUG
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
ESUG
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
ESUG
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
ESUG
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
ESUG
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
ESUG
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
ESUG
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
ESUG
 

More from ESUG (20)

Workshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programmingWorkshop: Identifying concept inventories in agile programming
Workshop: Identifying concept inventories in agile programming
 
Technical documentation support in Pharo
Technical documentation support in PharoTechnical documentation support in Pharo
Technical documentation support in Pharo
 
The Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and RoadmapThe Pharo Debugger and Debugging tools: Advances and Roadmap
The Pharo Debugger and Debugging tools: Advances and Roadmap
 
Sequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in PharoSequence: Pipeline modelling in Pharo
Sequence: Pipeline modelling in Pharo
 
Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...Migration process from monolithic to micro frontend architecture in mobile ap...
Migration process from monolithic to micro frontend architecture in mobile ap...
 
Analyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early resultsAnalyzing Dart Language with Pharo: Report and early results
Analyzing Dart Language with Pharo: Report and early results
 
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
Transpiling Pharo Classes to JS ECMAScript 5 versus ECMAScript 6
 
A Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test GenerationA Unit Test Metamodel for Test Generation
A Unit Test Metamodel for Test Generation
 
Creating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic ProgrammingCreating Unit Tests Using Genetic Programming
Creating Unit Tests Using Genetic Programming
 
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution ModesThreaded-Execution and CPS Provide Smooth Switching Between Execution Modes
Threaded-Execution and CPS Provide Smooth Switching Between Execution Modes
 
Exploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience ReportExploring GitHub Actions through EGAD: An Experience Report
Exploring GitHub Actions through EGAD: An Experience Report
 
Pharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIsPharo: a reflective language A first systematic analysis of reflective APIs
Pharo: a reflective language A first systematic analysis of reflective APIs
 
Garbage Collector Tuning
Garbage Collector TuningGarbage Collector Tuning
Garbage Collector Tuning
 
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame CaseImproving Performance Through Object Lifetime Profiling: the DataFrame Case
Improving Performance Through Object Lifetime Profiling: the DataFrame Case
 
Pharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and FuturePharo DataFrame: Past, Present, and Future
Pharo DataFrame: Past, Present, and Future
 
thisContext in the Debugger
thisContext in the DebuggerthisContext in the Debugger
thisContext in the Debugger
 
Websockets for Fencing Score
Websockets for Fencing ScoreWebsockets for Fencing Score
Websockets for Fencing Score
 
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScriptShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
ShowUs: PharoJS.org Develop in Pharo, Run on JavaScript
 
Advanced Object- Oriented Design Mooc
Advanced Object- Oriented Design MoocAdvanced Object- Oriented Design Mooc
Advanced Object- Oriented Design Mooc
 
A New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and TransformationsA New Architecture Reconciling Refactorings and Transformations
A New Architecture Reconciling Refactorings and Transformations
 

Recently uploaded

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
Alluxio, Inc.
 

Recently uploaded (20)

Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Breaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdfBreaking the Code : A Guide to WhatsApp Business API.pdf
Breaking the Code : A Guide to WhatsApp Business API.pdf
 
Agnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in KrakówAgnieszka Andrzejewska - BIM School Course in Kraków
Agnieszka Andrzejewska - BIM School Course in Kraków
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
AI/ML Infra Meetup | Improve Speed and GPU Utilization for Model Training & S...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024Globus Connect Server Deep Dive - GlobusWorld 2024
Globus Connect Server Deep Dive - GlobusWorld 2024
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Large Language Models and the End of Programming
Large Language Models and the End of ProgrammingLarge Language Models and the End of Programming
Large Language Models and the End of Programming
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 

Non-Blocking Strategies for FFI

  • 1. Non-Blocking Strategies for FFI Don’t Block me Now! Pablo Tesone Pharo Consortium Engineer 1 Guille Polito CNRS UMR9189 CRIStAL, Inria RMoD
  • 2. Pablo Tesone Pharo Consortium Engineer Guille Polito CNRS Engineer RMod Team • 10 years of experience in industrial applications • PhD in Dynamic Software Update • Interested in improving development tools and the daily development process. • Enthusiast of the object oriented programming and their tools. • Experience industrial on service- oriented and mobile applications. • PhD in Computer Science • Main research interests are modularity and development tools. • In the Pharo community since 2010 • More noticeable contributions: Pharo Bootstrap process and Iceberg.
  • 3. FFI? Foreign Function Interface Image VM External Libraries FFI We can communicate with anything that has a C API Operating System API !3
  • 4. Unified FFI in a nutshell UFFI handles: - Look-up of functions - Marshalling of arguments - Execution - Marshalling of the return values
  • 5. Concurrency in Pharo !5 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 VM Thread
  • 6. Concurrency in Pharo !6 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 VM handles process scheduling VM Thread
  • 7. Concurrency in Pharo !7 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 p1 int function(char* foo, int bar) VM Thread
  • 8. Concurrency in Pharo !8 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 p1 Out of VM VM loses control int function(char* foo, int bar) VM Thread
  • 9. Conceptual Non-Blocking FFI !9 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 p1 p3 p2 ? int function(char* foo, int bar) VM Thread
  • 10. Strategy #1: Thread per Call-out !10 Interpreter Thread p1 p2 p1 p2 Thread 1 <<Spawn>> <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 VM Thread
  • 11. Strategy #1: Thread per Call-out !11 Interpreter Thread p1 p2 p1 p2 Thread 1 <<Spawn>> <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 • Simple • Expensive to spawn threads • Calls are not in the same thread • Cannot reuse existing threads (e.g., UI threads)
  • 12. Not all libraries are designed equally • Different requirements • Must run in the main thread (Cocoa) • Must run in a single thread (Gtk+3) • Runs on any thread but not concurrent (libgit, sqlite) • Is a Thread-safe Library • …. !12
  • 13. We need different Strategies to choose from !13 We need to choose different strategies for each library
  • 14. Strategy #2: Worker Threads !14 Interpreter Thread p1 p2 p1 p2 Worker #1 <<Enqueue>> Call out Queue of Worker #1 <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 VM Thread
  • 15. Strategy #2: Worker Threads !15 Interpreter Thread p1 p2 p1 p2 Worker #1 <<Enqueue>> Call out Queue of Worker #1 <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 • Simple • Group related calls • No thread spawn overhead • Expensive Callouts (synchronising queue) • Do not support main thread!
  • 16. Strategy #3: VM Thread Runner !16 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 int function(char* foo, int bar) p1 VM Thread
  • 17. Strategy #3: VM Thread Runner !17 P1 P2 P3 Interpreter Thread p1 p3 p1 p2 p2 int function(char* foo, int bar) p1 • Simpler • Group related calls • No thread spawn overhead • Backward compatibility • Blocking
  • 18. Strategy #4: Main Thread Runner Interpreter Thread p1 p2 p1 p2 Worker #1 <<Enqueue>> Call out Queue of Worker #1 <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 VM Thread
  • 19. Strategy #4: Main Thread Runner Interpreter Thread p1 p2 p1 p2 Worker #1 <<Enqueue>> Call out Queue of Worker #1 <<Signal Semaphore>> int function(char* foo, int bar) P1 P2 • Simple • Group related calls • No thread spawn overhead • Supports main thread • Expensive Callouts (synchronising queue) • VM should be run in separate thread
  • 20. Strategy #5: Global Interpreter Lock int function(char* foo, int bar) P1 P2 Interpreter #1 Interpreter #2 !20 VM Thread #1 VM Thread #2
  • 21. Strategy #5: Global Interpreter Lock int function(char* foo, int bar) P1 P2 Interpreter #1 Interpreter #2 !21 • Group related calls • No thread spawn overhead • No Backward compatibility • Application should be written with threading in mind • Requires VM modification
  • 22. Strategy #6: Thread-safe interpreters int function(char* foo, int bar) !22 P1 P2 Interpreter #1 Interpreter #2 VM Thread #1 VM Thread #2
  • 23. Strategy #6: Thread-safe interpreters int function(char* foo, int bar) !23 P1 P2 Interpreter #1 Interpreter #2 • Does not exist for Pharo • Requires extensive modification of VM, Plugins and Image core libraries • Application should be written with threading in mind • Real multithreading not only for FFI
  • 24. Implementations !24 Queue Based FFI (Pharo Threaded FFI Plugin) GILda VM (Global Interpreter Lock VM) Strategy #1: Thread per Call-out Strategy #2: Worker Threads Strategy #3: VM Thread Runner Strategy #5: Global Interpreter Lock Strategy #4: Main Thread Runner Future??? Strategy #6: Thread-safe interpreters
  • 25. Implementations !25 Queue Based FFI (Pharo Threaded FFI Plugin) GILda VM (Global Interpreter Lock VM) Strategy #1: Thread per Call-out Strategy #2: Worker Threads Strategy #3: VM Thread Runner Strategy #5: Global Interpreter Lock Strategy #4: Main Thread Runner Future??? Strategy #6: Thread-safe interpreters
  • 26. Transparency through UFFI !26 Your Program Unified FFI Pharo Threaded FFI Pharo Threaded FFI Plugin S#1 S#2 S#3 S#4 Squeak FFI Squeak FFI Plugin Library
  • 27. Decision Table !27 Same Thread Worker Threads Long Calls Blocking Parallel Short Calls Little Overhead Lots of Overhead OPTIMIZATIONS
  • 28. Implementations !28 GILda VM (Global Interpreter Lock VM) Strategy #5: Global Interpreter Lock Queue Based FFI (Pharo Threaded FFI Plugin) Strategy #1: Thread per Call-out Strategy #2: Worker Threads Strategy #3: VM Thread Runner Strategy #4: Main Thread Runner Future??? Strategy #6: Thread-safe interpreters More Details IWST - Virtual Machines Session Thursday 11:00 am
  • 29. Start Using It! !29 Metacello new baseline: ‘ThreadedFFI' repository: ‘github://pharo-project/threadedFFI-Plugin/src'; load pharo-project/threadedFFI-Plugin Only in Pharo 8 + Headless VM
  • 30. Conclusion • Beta Version (In usage for Gtk+) • Transparent for the user • Strategies selected in the Image • Uses LibFFI • Re-entrant Callback support • Tests! !30
  • 31. Preliminar results • All marshalling image side + Lib FFI • Short call • Same thread 27 us • Single worker thread 6791 us • 2 Parallel long Calls (1 second per call) • Same Thread 2001.9 ms • Different working threads 1006.4ms