SlideShare ist ein Scribd-Unternehmen logo
1 von 22
Downloaden Sie, um offline zu lesen
A Scalable I/O Manager for GHC

             Johan Tibell
           29 January 2010
                    
    http://github.com/tibbe/event
Server applications

 Performance matters
    Servers cost (a lot of) money
    We want as high throughput as possible

 Scalability: Performance shouldn't degrade (too much)
 when the number of clients increase

 Observation: In most (HTTP) servers, the majority
 of  clients are idle at any given point in time
Why Haskell?

 Simple programming model:
    Light-weight threads (forkIO)
    Blocking system calls

    server = forever $ do
      sock <- accept serverSock
      forkIO $ talk sock >> sClose sock

    talk sock = do
      req <- recv sock
      send sock (f req)
Why Haskell?

 Performance:
    Lots of concurrency
    Statically compiled; should perform favorably in
    comparison with e.g. Python and Ruby
    Alternative to C++ or Java when performance matters

 Correctness:
    Pure functions
    Strong static typing
What we are missing

 Support for a large number of concurrent connections

 Support for a large number of active timeouts
    Typically one per connection
Implementing light-weight threads

 Schedule many light-weight threads across a set of OS
 threads.

 To avoid blocking the OS threads, use the select system
 call to monitor multiple file descriptors using a single OS
 thread.
Non-blocking I/O refresher

 select: a system call for polling the status of multiple file
 descriptors.
     A call to select returns when one or more file
     descriptors are ready for reading writing, or
     a timeout occurs.

 Only call a potentially blocking system call (e.g. recv)
 when we know it won't block!
Reading

 data IOReq = Read Fd (MVar ())
            | Write Fd (MVar ())

 read fd = do waitForReadEvent fd
              c_read fd

 waitForReadEvent fd = do
   m <- newEmptyMVar
   atomicModifyIORef watechedFds (xs ->
     (Read fd m : xs, ()))
   takeMVar m
Sleeping/timers

 data DelayReq = Delay USecs (MVar ())

 threadDelay time = waitForDelayEvent time

 waitForDelayEvent usecs = do
   m <- newEmptyMVar
   target <- calculateTarget usecs
   atomicModifyIORef delays (xs ->
     (Delay target m : xs, ()))
   takeMVar m
I/O manager event loop

eventLoop delays watchedFds = do
  now <- getCurrentTime
  (delays', timeout) <- expire now delays
  readyFds <- select watchedFds timeout
  watchedFds' <- wakeupFds readyFds watchedFds
  eventLoop delays' watchedFds'

expire _   [] = return ([], Never)
expire now ds@(Delay d m : ds')
  | d <= now = putMVar m () >> expire now ds'
  | otherwise = return (ds, Timeout (d - now))
I/O manager event loop cont.

wakeupFds readyFds fds = go fds []
  where
    go []                fds' = return fds'
    go (Read fd m : fds) fds'
      | fd `member` readyFds =
          putMVar m () >> go fds fds'
      | otherwise = go fds (Read fd m : fds')
    go (Write fd m : fds) fds'
      | fd `member` readyFds =
          putMVar m () >> go fds fds'
      | otherwise = go fds (Read fd m : fds')
The problem

 select:
     ~O(watched file descriptors)
         Most file descriptors are idle!
     Limited number of file descriptors (FD_SETSIZE)

 Iterating through all watched file descriptors every
 time around the event loop.

 Timeouts are kept in a list, sorted by time
    Insertion: O(n) as we need to keep the list sorted
A scalable I/O manager

 Scalable system calls
    epoll, kqueue, and some Windows thing...

 Better data structures
    Trees and heaps instead of lists
Timeouts

    New I/O manager uses a priority search queue
      Insertion: O(log n)
      Getting all expired timeouts: O(k*(log n - log
      k)), where k is the number of expired timeouts

    The API for timeouts is quite limited (to say the least!)
      One function: threadDelay
 
    Priority search queues allows us to
       adjust/cancel pending timeouts
Priority search queue performance

 Used Criterion extensively to benchmark and verify
 micro optimizations.

 Biggest performance gains:
    Specialized to a single type of key/priority
    Strict sub-trees
    Unpacked data types

 Used QuickCheck to make sure that the optimizations
 didn't break anything.
threadDelay 1ms
Light-weight threads vs event loop
Reading/writing

 Scalable system calls
    epoll/kqueue: O(active file descriptors)

 Unblocking threads that are ready to perform I/O
   O(log n) per thread, using an IntMap from file
   descriptor to MVar
   Total running time for k active file descriptors is O(k
   * log n) instead of O(n).
Send 1M 1-byte messages through pipes
Aside: Good tools are important

 ThreadScope
    Helped us find a pathological case in an interaction
    between atomicModifyIORef and GHC's scheduler
Current status

 Close to feature complete

 Needs more
   testing
   benchmarking

 Final step remaining: Integrate into GHC
Conclusions

 Haskell is (soon) ready for the server!

 We still need:
   High performance HTTP server
   High performance HTML combinator library
   Composable and secure HTML form generation
       Formlets + cross-site scripting protection
   Scalable and distributed data store
       We could just write binding to an existing one (but
       where's the fun in that!)

Weitere ähnliche Inhalte

Was ist angesagt?

Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedAnne Nicolas
 
How Netflix Tunes EC2 Instances for Performance
How Netflix Tunes EC2 Instances for PerformanceHow Netflix Tunes EC2 Instances for Performance
How Netflix Tunes EC2 Instances for PerformanceBrendan Gregg
 
[若渴]Study on Side Channel Attacks and Countermeasures
[若渴]Study on Side Channel Attacks and Countermeasures [若渴]Study on Side Channel Attacks and Countermeasures
[若渴]Study on Side Channel Attacks and Countermeasures Aj MaChInE
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Baruch Sadogursky
 
Linux Performance Tunning introduction
Linux Performance Tunning introductionLinux Performance Tunning introduction
Linux Performance Tunning introductionShay Cohen
 
JavaOne 2015 Java Mixed-Mode Flame Graphs
JavaOne 2015 Java Mixed-Mode Flame GraphsJavaOne 2015 Java Mixed-Mode Flame Graphs
JavaOne 2015 Java Mixed-Mode Flame GraphsBrendan Gregg
 
Shaping Clouds with Terraform
Shaping Clouds with TerraformShaping Clouds with Terraform
Shaping Clouds with TerraformMike Fowler
 
Performance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudPerformance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudBrendan Gregg
 
Stateless Hypervisors at Scale
Stateless Hypervisors at ScaleStateless Hypervisors at Scale
Stateless Hypervisors at ScaleAntony Messerl
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!Zubair Nabi
 
Varnish Configuration Step by Step
Varnish Configuration Step by StepVarnish Configuration Step by Step
Varnish Configuration Step by StepKim Stefan Lindholm
 
Live migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchasLive migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchasDocker, Inc.
 
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFUSENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFBrendan Gregg
 
Mark Farnam : Minimizing the Concurrency Footprint of Transactions
Mark Farnam  : Minimizing the Concurrency Footprint of TransactionsMark Farnam  : Minimizing the Concurrency Footprint of Transactions
Mark Farnam : Minimizing the Concurrency Footprint of TransactionsKyle Hailey
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSESUSE Labs Taipei
 
[若渴計畫] Studying ASLR^cache
[若渴計畫] Studying ASLR^cache[若渴計畫] Studying ASLR^cache
[若渴計畫] Studying ASLR^cacheAj MaChInE
 
An Updated Performance Comparison of Virtual Machines and Linux Containers
An Updated Performance Comparison of Virtual Machines and Linux ContainersAn Updated Performance Comparison of Virtual Machines and Linux Containers
An Updated Performance Comparison of Virtual Machines and Linux ContainersKento Aoyama
 
Debugging Tools & Techniques for Persistent Memory Programming
Debugging Tools & Techniques for Persistent Memory ProgrammingDebugging Tools & Techniques for Persistent Memory Programming
Debugging Tools & Techniques for Persistent Memory ProgrammingIntel® Software
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web ServersDavid Evans
 

Was ist angesagt? (20)

Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all startedKernel Recipes 2019 - ftrace: Where modifying a running kernel all started
Kernel Recipes 2019 - ftrace: Where modifying a running kernel all started
 
How Netflix Tunes EC2 Instances for Performance
How Netflix Tunes EC2 Instances for PerformanceHow Netflix Tunes EC2 Instances for Performance
How Netflix Tunes EC2 Instances for Performance
 
[若渴]Study on Side Channel Attacks and Countermeasures
[若渴]Study on Side Channel Attacks and Countermeasures [若渴]Study on Side Channel Attacks and Countermeasures
[若渴]Study on Side Channel Attacks and Countermeasures
 
Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java Everything you wanted to know about writing async, concurrent http apps in java
Everything you wanted to know about writing async, concurrent http apps in java
 
Linux Performance Tunning introduction
Linux Performance Tunning introductionLinux Performance Tunning introduction
Linux Performance Tunning introduction
 
JavaOne 2015 Java Mixed-Mode Flame Graphs
JavaOne 2015 Java Mixed-Mode Flame GraphsJavaOne 2015 Java Mixed-Mode Flame Graphs
JavaOne 2015 Java Mixed-Mode Flame Graphs
 
Shaping Clouds with Terraform
Shaping Clouds with TerraformShaping Clouds with Terraform
Shaping Clouds with Terraform
 
Performance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloudPerformance Analysis: new tools and concepts from the cloud
Performance Analysis: new tools and concepts from the cloud
 
Stateless Hypervisors at Scale
Stateless Hypervisors at ScaleStateless Hypervisors at Scale
Stateless Hypervisors at Scale
 
AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!AOS Lab 1: Hello, Linux!
AOS Lab 1: Hello, Linux!
 
Speeding up ps and top
Speeding up ps and topSpeeding up ps and top
Speeding up ps and top
 
Varnish Configuration Step by Step
Varnish Configuration Step by StepVarnish Configuration Step by Step
Varnish Configuration Step by Step
 
Live migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchasLive migrating a container: pros, cons and gotchas
Live migrating a container: pros, cons and gotchas
 
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPFUSENIX ATC 2017 Performance Superpowers with Enhanced BPF
USENIX ATC 2017 Performance Superpowers with Enhanced BPF
 
Mark Farnam : Minimizing the Concurrency Footprint of Transactions
Mark Farnam  : Minimizing the Concurrency Footprint of TransactionsMark Farnam  : Minimizing the Concurrency Footprint of Transactions
Mark Farnam : Minimizing the Concurrency Footprint of Transactions
 
Kernel debug log and console on openSUSE
Kernel debug log and console on openSUSEKernel debug log and console on openSUSE
Kernel debug log and console on openSUSE
 
[若渴計畫] Studying ASLR^cache
[若渴計畫] Studying ASLR^cache[若渴計畫] Studying ASLR^cache
[若渴計畫] Studying ASLR^cache
 
An Updated Performance Comparison of Virtual Machines and Linux Containers
An Updated Performance Comparison of Virtual Machines and Linux ContainersAn Updated Performance Comparison of Virtual Machines and Linux Containers
An Updated Performance Comparison of Virtual Machines and Linux Containers
 
Debugging Tools & Techniques for Persistent Memory Programming
Debugging Tools & Techniques for Persistent Memory ProgrammingDebugging Tools & Techniques for Persistent Memory Programming
Debugging Tools & Techniques for Persistent Memory Programming
 
Scheduling in Linux and Web Servers
Scheduling in Linux and Web ServersScheduling in Linux and Web Servers
Scheduling in Linux and Web Servers
 

Andere mochten auch

Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageMaurice Naftalin
 
Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Bartek Zdanowski
 
Managing gang of chaotic developers is complex at Agile Tour Riga 2012
Managing gang of chaotic developers is complex at Agile Tour Riga 2012Managing gang of chaotic developers is complex at Agile Tour Riga 2012
Managing gang of chaotic developers is complex at Agile Tour Riga 2012Piotr Burdylo
 
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Alexandre Morgaut
 
Agile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itAgile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itPiotr Burdylo
 
Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Kiwamu Okabe
 
Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013Gabriel Pettier
 
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...thegdb
 
erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09Bhasker Kode
 
O'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAO'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAOliver Steele
 
Monadologie
MonadologieMonadologie
Monadologieleague
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Maurice Naftalin
 
Jensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJen Simmons
 
Mendeley presentation
Mendeley presentationMendeley presentation
Mendeley presentationDiogo Provete
 
Federated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowFederated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowPatrick Hurley
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing UpDavid Padbury
 

Andere mochten auch (20)

Shooting the Rapids
Shooting the RapidsShooting the Rapids
Shooting the Rapids
 
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative LanguageParallel-Ready Java Code: Managing Mutation in an Imperative Language
Parallel-Ready Java Code: Managing Mutation in an Imperative Language
 
Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)Vert.x - JDD 2013 (English)
Vert.x - JDD 2013 (English)
 
Let's Get to the Rapids
Let's Get to the RapidsLet's Get to the Rapids
Let's Get to the Rapids
 
Managing gang of chaotic developers is complex at Agile Tour Riga 2012
Managing gang of chaotic developers is complex at Agile Tour Riga 2012Managing gang of chaotic developers is complex at Agile Tour Riga 2012
Managing gang of chaotic developers is complex at Agile Tour Riga 2012
 
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
Wakanda: a new end-to-end JavaScript platform - JSConf Berlin 2009
 
Agile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko itAgile Management 2013 - Nie tylko it
Agile Management 2013 - Nie tylko it
 
Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"Metasepi team meeting #6: "Snatch-driven development"
Metasepi team meeting #6: "Snatch-driven development"
 
Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013Présentation Kivy (et projets associés) à Pycon-fr 2013
Présentation Kivy (et projets associés) à Pycon-fr 2013
 
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...
There's a Monster in My Closet: Architecture of a MongoDB-powered Event Proce...
 
erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09erlang at hover.in , Devcamp Blr 09
erlang at hover.in , Devcamp Blr 09
 
O'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIAO'Reilly ETech Conference: Laszlo RIA
O'Reilly ETech Conference: Laszlo RIA
 
Laszlo PyCon 2005
Laszlo PyCon 2005Laszlo PyCon 2005
Laszlo PyCon 2005
 
Monadologie
MonadologieMonadologie
Monadologie
 
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
Good and Wicked Fairies, and the Tragedy of the Commons: Understanding the Pe...
 
Jensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesignJensimmons html5live-responsivedesign
Jensimmons html5live-responsivedesign
 
Mendeley presentation
Mendeley presentationMendeley presentation
Mendeley presentation
 
Federated CDNs: What every service provider should know
Federated CDNs: What every service provider should knowFederated CDNs: What every service provider should know
Federated CDNs: What every service provider should know
 
Masters Defense 2013
Masters Defense 2013Masters Defense 2013
Masters Defense 2013
 
JavaScript Growing Up
JavaScript Growing UpJavaScript Growing Up
JavaScript Growing Up
 

Ähnlich wie A Scalable I/O Manager for GHC

GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconPeter Lawrey
 
Oracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.pptOracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.pptTricantinoLopezPerez
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow enginedmoebius
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisationgrooverdan
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notificationMahendra M
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsMarcus Frödin
 
Python twisted
Python twistedPython twisted
Python twistedMahendra M
 
Data Grids with Oracle Coherence
Data Grids with Oracle CoherenceData Grids with Oracle Coherence
Data Grids with Oracle CoherenceBen Stopford
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Emery Berger
 
Privilege Escalation with Metasploit
Privilege Escalation with MetasploitPrivilege Escalation with Metasploit
Privilege Escalation with Metasploitegypt
 
Logging for Production Systems in The Container Era
Logging for Production Systems in The Container EraLogging for Production Systems in The Container Era
Logging for Production Systems in The Container EraSadayuki Furuhashi
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfKrystian Zybała
 
Ajuste (tuning) del rendimiento de SQL Server 2008
Ajuste (tuning) del rendimiento de SQL Server 2008Ajuste (tuning) del rendimiento de SQL Server 2008
Ajuste (tuning) del rendimiento de SQL Server 2008Eduardo Castro
 
SQL Server Performance Analysis
SQL Server Performance AnalysisSQL Server Performance Analysis
SQL Server Performance AnalysisEduardo Castro
 
Sedna XML Database: Executor Internals
Sedna XML Database: Executor InternalsSedna XML Database: Executor Internals
Sedna XML Database: Executor InternalsIvan Shcheklein
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Schedulermatthewrdale
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimizationManish Rawat
 

Ähnlich wie A Scalable I/O Manager for GHC (20)

GC free coding in @Java presented @Geecon
GC free coding in @Java presented @GeeconGC free coding in @Java presented @Geecon
GC free coding in @Java presented @Geecon
 
Oracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.pptOracle Wait Events That Everyone Should Know.ppt
Oracle Wait Events That Everyone Should Know.ppt
 
Copper: A high performance workflow engine
Copper: A high performance workflow engineCopper: A high performance workflow engine
Copper: A high performance workflow engine
 
1230 Rtf Final
1230 Rtf Final1230 Rtf Final
1230 Rtf Final
 
Clug 2011 March web server optimisation
Clug 2011 March  web server optimisationClug 2011 March  web server optimisation
Clug 2011 March web server optimisation
 
Kqueue : Generic Event notification
Kqueue : Generic Event notificationKqueue : Generic Event notification
Kqueue : Generic Event notification
 
Non-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.jsNon-blocking I/O, Event loops and node.js
Non-blocking I/O, Event loops and node.js
 
Python twisted
Python twistedPython twisted
Python twisted
 
Data Grids with Oracle Coherence
Data Grids with Oracle CoherenceData Grids with Oracle Coherence
Data Grids with Oracle Coherence
 
Data race
Data raceData race
Data race
 
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
Exploiting Multicore CPUs Now: Scalability and Reliability for Off-the-shelf ...
 
Privilege Escalation with Metasploit
Privilege Escalation with MetasploitPrivilege Escalation with Metasploit
Privilege Escalation with Metasploit
 
Logging for Production Systems in The Container Era
Logging for Production Systems in The Container EraLogging for Production Systems in The Container Era
Logging for Production Systems in The Container Era
 
Here comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdfHere comes the Loom - Ya!vaConf.pdf
Here comes the Loom - Ya!vaConf.pdf
 
Tuning Java Servers
Tuning Java Servers Tuning Java Servers
Tuning Java Servers
 
Ajuste (tuning) del rendimiento de SQL Server 2008
Ajuste (tuning) del rendimiento de SQL Server 2008Ajuste (tuning) del rendimiento de SQL Server 2008
Ajuste (tuning) del rendimiento de SQL Server 2008
 
SQL Server Performance Analysis
SQL Server Performance AnalysisSQL Server Performance Analysis
SQL Server Performance Analysis
 
Sedna XML Database: Executor Internals
Sedna XML Database: Executor InternalsSedna XML Database: Executor Internals
Sedna XML Database: Executor Internals
 
Demystifying the Go Scheduler
Demystifying the Go SchedulerDemystifying the Go Scheduler
Demystifying the Go Scheduler
 
Sql server performance tuning and optimization
Sql server performance tuning and optimizationSql server performance tuning and optimization
Sql server performance tuning and optimization
 

Kürzlich hochgeladen

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native ApplicationsWSO2
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
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
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024The Digital Insurer
 
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
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...apidays
 
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
 
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
 

Kürzlich hochgeladen (20)

ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
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
 
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
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
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
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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...
 
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, ...
 

A Scalable I/O Manager for GHC

  • 1. A Scalable I/O Manager for GHC Johan Tibell 29 January 2010   http://github.com/tibbe/event
  • 2. Server applications Performance matters Servers cost (a lot of) money We want as high throughput as possible Scalability: Performance shouldn't degrade (too much) when the number of clients increase Observation: In most (HTTP) servers, the majority of  clients are idle at any given point in time
  • 3. Why Haskell? Simple programming model: Light-weight threads (forkIO) Blocking system calls server = forever $ do sock <- accept serverSock forkIO $ talk sock >> sClose sock talk sock = do req <- recv sock send sock (f req)
  • 4. Why Haskell? Performance: Lots of concurrency Statically compiled; should perform favorably in comparison with e.g. Python and Ruby Alternative to C++ or Java when performance matters Correctness: Pure functions Strong static typing
  • 5. What we are missing Support for a large number of concurrent connections Support for a large number of active timeouts Typically one per connection
  • 6. Implementing light-weight threads Schedule many light-weight threads across a set of OS threads. To avoid blocking the OS threads, use the select system call to monitor multiple file descriptors using a single OS thread.
  • 7. Non-blocking I/O refresher select: a system call for polling the status of multiple file descriptors. A call to select returns when one or more file descriptors are ready for reading writing, or a timeout occurs. Only call a potentially blocking system call (e.g. recv) when we know it won't block!
  • 8. Reading data IOReq = Read Fd (MVar ()) | Write Fd (MVar ()) read fd = do waitForReadEvent fd c_read fd waitForReadEvent fd = do m <- newEmptyMVar atomicModifyIORef watechedFds (xs -> (Read fd m : xs, ())) takeMVar m
  • 9. Sleeping/timers data DelayReq = Delay USecs (MVar ()) threadDelay time = waitForDelayEvent time waitForDelayEvent usecs = do m <- newEmptyMVar target <- calculateTarget usecs atomicModifyIORef delays (xs -> (Delay target m : xs, ())) takeMVar m
  • 10. I/O manager event loop eventLoop delays watchedFds = do now <- getCurrentTime (delays', timeout) <- expire now delays readyFds <- select watchedFds timeout watchedFds' <- wakeupFds readyFds watchedFds eventLoop delays' watchedFds' expire _ [] = return ([], Never) expire now ds@(Delay d m : ds') | d <= now = putMVar m () >> expire now ds' | otherwise = return (ds, Timeout (d - now))
  • 11. I/O manager event loop cont. wakeupFds readyFds fds = go fds [] where go [] fds' = return fds' go (Read fd m : fds) fds' | fd `member` readyFds = putMVar m () >> go fds fds' | otherwise = go fds (Read fd m : fds') go (Write fd m : fds) fds' | fd `member` readyFds = putMVar m () >> go fds fds' | otherwise = go fds (Read fd m : fds')
  • 12. The problem select: ~O(watched file descriptors) Most file descriptors are idle! Limited number of file descriptors (FD_SETSIZE) Iterating through all watched file descriptors every time around the event loop. Timeouts are kept in a list, sorted by time Insertion: O(n) as we need to keep the list sorted
  • 13. A scalable I/O manager Scalable system calls epoll, kqueue, and some Windows thing... Better data structures Trees and heaps instead of lists
  • 14. Timeouts New I/O manager uses a priority search queue Insertion: O(log n) Getting all expired timeouts: O(k*(log n - log k)), where k is the number of expired timeouts The API for timeouts is quite limited (to say the least!) One function: threadDelay   Priority search queues allows us to adjust/cancel pending timeouts
  • 15. Priority search queue performance Used Criterion extensively to benchmark and verify micro optimizations. Biggest performance gains: Specialized to a single type of key/priority Strict sub-trees Unpacked data types Used QuickCheck to make sure that the optimizations didn't break anything.
  • 18. Reading/writing Scalable system calls epoll/kqueue: O(active file descriptors) Unblocking threads that are ready to perform I/O O(log n) per thread, using an IntMap from file descriptor to MVar Total running time for k active file descriptors is O(k * log n) instead of O(n).
  • 19. Send 1M 1-byte messages through pipes
  • 20. Aside: Good tools are important ThreadScope Helped us find a pathological case in an interaction between atomicModifyIORef and GHC's scheduler
  • 21. Current status Close to feature complete Needs more testing benchmarking Final step remaining: Integrate into GHC
  • 22. Conclusions Haskell is (soon) ready for the server! We still need: High performance HTTP server High performance HTML combinator library Composable and secure HTML form generation Formlets + cross-site scripting protection Scalable and distributed data store We could just write binding to an existing one (but where's the fun in that!)