SlideShare una empresa de Scribd logo
1 de 37
Trisha Gee & Michael Barker / LMAX


The Disruptor -
A Beginners Guide to
Hardcore Concurrency
Why is Concurrency So Difficult
              ?
Program Order:      Execution Order (maybe):

int   w   =   10;   int x = 20;
int   x   =   20;   int y = 30;
int   y   =   30;   int b = x * y;
int   z   =   40;
                    int w = 10;
int a = w + z;      int z = 40;
int b = x * y;      int a = w + z;
Why Should We Care About the
          Details
            ?
static long foo = 0;

private static void increment() {
    for (long l = 0; l < 500000000L; l++) {
        foo++;
    }
}
public static long foo = 0;
public static Lock lock = new Lock();

private static void increment() {
    for (long l = 0; l < 500000000L; l++){
        lock.lock();
        try {
            foo++;
        } finally {
            lock.unlock();
        }
    }
}
static AtomicLong foo = new AtomicLong(0);

private static void increment() {
    for (long l = 0; l < 500000000L; l++) {
        foo.getAndIncrement();
    }
}
Cost of Contention

     Increment a counter 500 000 000 times.

• One Thread              :      300 ms
Cost of Contention

     Increment a counter 500 000 000 times.

• One Thread             :      300 ms
• One Thread   (volatile):    4 700 ms    (15x)
Cost of Contention

     Increment a counter 500 000 000 times.

• One Thread             :      300 ms
• One Thread   (volatile):    4 700 ms    (15x)
• One Thread   (Atomic) :     5 700 ms    (19x)
Cost of Contention

          Increment a counter 500 000 000 times.

•   One   Thread             :       300   ms
•   One   Thread   (volatile):     4 700   ms   (15x)
•   One   Thread   (Atomic) :      5 700   ms   (19x)
•   One   Thread   (Lock)    :    10 000   ms   (33x)
Cost of Contention

          Increment a counter 500 000 000 times.

•   One   Thread              :        300   ms
•   One   Thread    (volatile):    4   700   ms (15x)
•   One   Thread    (Atomic) :     5   700   ms (19x)
•   One   Thread    (Lock)    :   10   000   ms (33x)
•   Two   Threads   (Atomic) :    30   000   ms (100x)
Cost of Contention

          Increment a counter 500 000 000 times.

•   One   Thread              :     300 ms
•   One   Thread    (volatile):   4 700 ms (15x)
•   One   Thread    (Atomic) :    5 700 ms (19x)
•   One   Thread    (Lock)    : 10 000 ms (33x)
•   Two   Threads   (Atomic) : 30 000 ms (100x)
•   Two   Threads   (Lock)    : 224 000 ms (746x)
                                ^^^^^^^^
                             ~4 minutes!!!
Parallel v. Serial - String Split

Guy Steele @ Strangle Loop:

http://www.infoq.com/presentations/Thinking-Parallel-
Programming

Scala Implementation and Brute Force version in Java:

https://github.com/mikeb01/folklore/




                                               15
Parallel (Scala)          Serial (Java)

2000.0

1500.0

1000.0

 500.0

    0
            String Split (ops/sec) higher is better
CPUs Are Getting Faster




                          17
Ya Rly!
          P8600 (Core 2 Duo)
          E5620 (Nehalem EP)
          i7 2667M (Sandy Bridge ULV)
          i7 2720QM (Sandy Bride)
3000.0

2250.0

1500.0

 750.0

    0
                String Split            18
What Problem Were Trying To Solve
               ?
20
21
Why Queues Suck - Array Backed




                            22
Why Queues Suck - Linked List




                                23
Why Queues Suck - Linked List




                                24
Contention Free Design




                         25
26
How Fast Is It - Throughput
             ABQ         Disruptor

30000000.0

22500000.0

15000000.0

 7500000.0

        0
               Unicast        Diamond
                                     27
How Fast Is It - Latency
                     ABQ               Disruptor

  Min                        145               29

  Mean                      32,757             52

  99 Percentile            2,097,152           128

  99.99 Percentile         4,194,304          8,192

  Max                      5,069,086         175,567



                                                       28
How Does It Work
      ?
Ordering and Visibility
  private   static final int SIZE = 32;
  private   final Object[] data = new Object[SIZE];
  private   volatile long sequence = -1;
  private   long nextValue = -1;

  public void publish(Object value) {
      long index = ++nextValue;
      data[(int)(index % SIZE)] = value;
      sequence = index;
  }

  public Object get(long index) {
      if (index <= sequence) {
          return data[(int)(index % SIZE)];
      }
      return null;
  }

                                                30
Ordering and Visibility - Store
mov    $0x1,%ecx
add    0x18(%rsi),%rcx     ;*ladd
;...
lea    (%r12,%r8,8),%r11 ;*getfield data
;...
mov    %r12b,(%r11,%r10,1)
mov    %rcx,0x10(%rsi)
lock addl $0x0,(%rsp)      ;*ladd




                                  31
Ordering and Visibility - Load

 mov    %eax,-0x6000(%rsp)
 push   %rbp
 sub    $0x20,%rsp      ;*synchronization entry
                        ; - RingBuffer::get@-1
 mov    0x10(%rsi),%r10    ;*getfield sequence
                           ; - RingBuffer::get@2
 cmp    %r10,%rdx
 jl     0x00007ff92505f22d ;*iflt
                           ; - RingBuffer::get@6
 mov    %edx,%r11d ;*l2i ; - RingBuffer::get@14




                                           32
Look Ma’ No Memory Barrier



 AtomicLong sequence = new AtomicLong(-1);

 public void publish(Object value) {
     long index = ++nextValue;
     data[(int)(index % SIZE)] = value;
     sequence.lazySet(index);
 }




                                      33
False Sharing - Hidden Contention




                               34
Cache Line Padding

public class PaddedAtomicLong extends AtomicLong {

    public volatile long p1, p2, p3, p4, p5, p6 = 7L;

    //... lines omitted

    public long sumPaddingToPreventOptimisation() {
        return p1 + p2 + p3 + p4 + p5 + p6;
    }
}




                                             35
Summary


•   Concurrency is a tool
•   Ordering and visibility are the key challenges
•   For performance the details matter
•   Don't believe everything you read
     o Come up with your own theories and test them!




                                              36
Q&A

recruitment@lmax.com

Más contenido relacionado

La actualidad más candente

MongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDBMongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
Boxed Ice
 

La actualidad más candente (20)

Concurrency: Rubies, Plural
Concurrency: Rubies, PluralConcurrency: Rubies, Plural
Concurrency: Rubies, Plural
 
Java and the machine - Martijn Verburg and Kirk Pepperdine
Java and the machine - Martijn Verburg and Kirk PepperdineJava and the machine - Martijn Verburg and Kirk Pepperdine
Java and the machine - Martijn Verburg and Kirk Pepperdine
 
Actor Concurrency
Actor ConcurrencyActor Concurrency
Actor Concurrency
 
Rust: Reach Further
Rust: Reach FurtherRust: Reach Further
Rust: Reach Further
 
Java Concurrency Idioms
Java Concurrency IdiomsJava Concurrency Idioms
Java Concurrency Idioms
 
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDBMongoUK 2011 - Rplacing RabbitMQ with MongoDB
MongoUK 2011 - Rplacing RabbitMQ with MongoDB
 
About memcached
About memcachedAbout memcached
About memcached
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)Next Generation Indexes For Big Data Engineering (ODSC East 2018)
Next Generation Indexes For Big Data Engineering (ODSC East 2018)
 
GPU-Accelerated Parallel Computing
GPU-Accelerated Parallel ComputingGPU-Accelerated Parallel Computing
GPU-Accelerated Parallel Computing
 
MessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization formatMessagePack - An efficient binary serialization format
MessagePack - An efficient binary serialization format
 
Racing To Win: Using Race Conditions to Build Correct and Concurrent Software
Racing To Win: Using Race Conditions to Build Correct and Concurrent SoftwareRacing To Win: Using Race Conditions to Build Correct and Concurrent Software
Racing To Win: Using Race Conditions to Build Correct and Concurrent Software
 
Java Concurrency Gotchas
Java Concurrency GotchasJava Concurrency Gotchas
Java Concurrency Gotchas
 
Rust: Unlocking Systems Programming
Rust: Unlocking Systems ProgrammingRust: Unlocking Systems Programming
Rust: Unlocking Systems Programming
 
Engineering fast indexes
Engineering fast indexesEngineering fast indexes
Engineering fast indexes
 
Kotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutinesKotlin from-scratch 3 - coroutines
Kotlin from-scratch 3 - coroutines
 
Advanced locking
Advanced lockingAdvanced locking
Advanced locking
 
Fast indexes with roaring #gomtl-10
Fast indexes with roaring #gomtl-10 Fast indexes with roaring #gomtl-10
Fast indexes with roaring #gomtl-10
 
Rubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me LatelyRubinius - What Have You Done For Me Lately
Rubinius - What Have You Done For Me Lately
 
Alternative cryptocurrencies
Alternative cryptocurrencies Alternative cryptocurrencies
Alternative cryptocurrencies
 

Destacado

Apache DeltaSpike
Apache DeltaSpikeApache DeltaSpike
Apache DeltaSpike
os890
 
MongoDB Memory Management Demystified
MongoDB Memory Management DemystifiedMongoDB Memory Management Demystified
MongoDB Memory Management Demystified
MongoDB
 

Destacado (20)

Introduction to the Disruptor
Introduction to the DisruptorIntroduction to the Disruptor
Introduction to the Disruptor
 
LMAX Architecture
LMAX ArchitectureLMAX Architecture
LMAX Architecture
 
Concurrent Programming Using the Disruptor
Concurrent Programming Using the DisruptorConcurrent Programming Using the Disruptor
Concurrent Programming Using the Disruptor
 
Workshop: Introduction to the Disruptor
Workshop: Introduction to the DisruptorWorkshop: Introduction to the Disruptor
Workshop: Introduction to the Disruptor
 
LMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging LibraryLMAX Disruptor - High Performance Inter-Thread Messaging Library
LMAX Disruptor - High Performance Inter-Thread Messaging Library
 
Eurosega equipo de profesionales altamente cualificados
Eurosega equipo de profesionales altamente cualificadosEurosega equipo de profesionales altamente cualificados
Eurosega equipo de profesionales altamente cualificados
 
We do it Roca style
We do it Roca styleWe do it Roca style
We do it Roca style
 
Today's Trending Technologies 2014
Today's Trending Technologies 2014Today's Trending Technologies 2014
Today's Trending Technologies 2014
 
Akka - Developing SEDA Based Applications
Akka - Developing SEDA Based ApplicationsAkka - Developing SEDA Based Applications
Akka - Developing SEDA Based Applications
 
Value Types
Value TypesValue Types
Value Types
 
MyFaces CODI Conversations
MyFaces CODI ConversationsMyFaces CODI Conversations
MyFaces CODI Conversations
 
Make JSF more type-safe with CDI and MyFaces CODI
Make JSF more type-safe with CDI and MyFaces CODIMake JSF more type-safe with CDI and MyFaces CODI
Make JSF more type-safe with CDI and MyFaces CODI
 
OpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheConOpenWebBeans and DeltaSpike at ApacheCon
OpenWebBeans and DeltaSpike at ApacheCon
 
MyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpikeMyFaces CODI and JBoss Seam3 become Apache DeltaSpike
MyFaces CODI and JBoss Seam3 become Apache DeltaSpike
 
Apache DeltaSpike
Apache DeltaSpikeApache DeltaSpike
Apache DeltaSpike
 
Combining Front-End and Backend Testing with Sauce Labs & BlazeMeter
Combining Front-End and Backend Testing with Sauce Labs & BlazeMeterCombining Front-End and Backend Testing with Sauce Labs & BlazeMeter
Combining Front-End and Backend Testing with Sauce Labs & BlazeMeter
 
How To Combine Back-End 
 & Front-End Testing with BlazeMeter & Sauce Labs
How To Combine Back-End 
 & Front-End Testing with BlazeMeter & Sauce LabsHow To Combine Back-End 
 & Front-End Testing with BlazeMeter & Sauce Labs
How To Combine Back-End 
 & Front-End Testing with BlazeMeter & Sauce Labs
 
Single Page Applications on JavaScript and ASP.NET MVC4
Single Page Applications on JavaScript and ASP.NET MVC4Single Page Applications on JavaScript and ASP.NET MVC4
Single Page Applications on JavaScript and ASP.NET MVC4
 
MongoDB Memory Management Demystified
MongoDB Memory Management DemystifiedMongoDB Memory Management Demystified
MongoDB Memory Management Demystified
 
Introduction to the Actor Model
Introduction to the Actor ModelIntroduction to the Actor Model
Introduction to the Actor Model
 

Similar a Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concurrency | Trisha Gee & Mike Barker

Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performance
Duoyi Wu
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
Charles Nutter
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!
Michael Barker
 
Cracking Pseudorandom Sequences Generators in Java Applications
Cracking Pseudorandom Sequences Generators in Java ApplicationsCracking Pseudorandom Sequences Generators in Java Applications
Cracking Pseudorandom Sequences Generators in Java Applications
Positive Hack Days
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)
Ontico
 
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
Naoki (Neo) SATO
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
Positive Hack Days
 

Similar a Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concurrency | Trisha Gee & Mike Barker (20)

Javascript engine performance
Javascript engine performanceJavascript engine performance
Javascript engine performance
 
JavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for DummiesJavaOne 2012 - JVM JIT for Dummies
JavaOne 2012 - JVM JIT for Dummies
 
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
Øredev 2011 - JVM JIT for Dummies (What the JVM Does With Your Bytecode When ...
 
Low latency in managed code
Low latency in managed codeLow latency in managed code
Low latency in managed code
 
Vectorization in ATLAS
Vectorization in ATLASVectorization in ATLAS
Vectorization in ATLAS
 
Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++Options and trade offs for parallelism and concurrency in Modern C++
Options and trade offs for parallelism and concurrency in Modern C++
 
Hadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm ArchitectureHadoop Summit Europe 2014: Apache Storm Architecture
Hadoop Summit Europe 2014: Apache Storm Architecture
 
Locks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael BarkerLocks? We Don't Need No Stinkin' Locks - Michael Barker
Locks? We Don't Need No Stinkin' Locks - Michael Barker
 
Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!Lock? We don't need no stinkin' locks!
Lock? We don't need no stinkin' locks!
 
Cracking Pseudorandom Sequences Generators in Java Applications
Cracking Pseudorandom Sequences Generators in Java ApplicationsCracking Pseudorandom Sequences Generators in Java Applications
Cracking Pseudorandom Sequences Generators in Java Applications
 
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
.NET Fest 2019. Łukasz Pyrzyk. Daily Performance Fuckups
 
Mysql handle socket
Mysql handle socketMysql handle socket
Mysql handle socket
 
Grow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM StackGrow and Shrink - Dynamically Extending the Ruby VM Stack
Grow and Shrink - Dynamically Extending the Ruby VM Stack
 
Cryptography and secure systems
Cryptography and secure systemsCryptography and secure systems
Cryptography and secure systems
 
Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)Performance tweaks and tools for Linux (Joe Damato)
Performance tweaks and tools for Linux (Joe Damato)
 
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
Java/Scala Lab: Анатолий Кметюк - Scala SubScript: Алгебра для реактивного пр...
 
How Triton can help to reverse virtual machine based software protections
How Triton can help to reverse virtual machine based software protectionsHow Triton can help to reverse virtual machine based software protections
How Triton can help to reverse virtual machine based software protections
 
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
Deep Learning, Microsoft Cognitive Toolkit (CNTK) and Azure Machine Learning ...
 
New SPL Features in PHP 5.3 (TEK-X)
New SPL Features in PHP 5.3 (TEK-X)New SPL Features in PHP 5.3 (TEK-X)
New SPL Features in PHP 5.3 (TEK-X)
 
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
The System of Automatic Searching for Vulnerabilities or how to use Taint Ana...
 

Más de JAX London

Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver GierkeSpring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
JAX London
 
Keynote | The Rise and Fall and Rise of Java | James Governor
Keynote | The Rise and Fall and Rise of Java | James GovernorKeynote | The Rise and Fall and Rise of Java | James Governor
Keynote | The Rise and Fall and Rise of Java | James Governor
JAX London
 
Java Tech & Tools | OSGi Best Practices | Emily Jiang
Java Tech & Tools | OSGi Best Practices | Emily JiangJava Tech & Tools | OSGi Best Practices | Emily Jiang
Java Tech & Tools | OSGi Best Practices | Emily Jiang
JAX London
 
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
JAX London
 
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
JAX London
 

Más de JAX London (20)

Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
Java Tech & Tools | Continuous Delivery - the Writing is on the Wall | John S...
 
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
Java Tech & Tools | Mapping, GIS and Geolocating Data in Java | Joachim Van d...
 
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark LittleKeynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
Keynote | Middleware Everywhere - Ready for Mobile and Cloud | Dr. Mark Little
 
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
Spring Day | WaveMaker - Spring Roo - SpringSource Tool Suite: Choosing the R...
 
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave SyerSpring Day | Behind the Scenes at Spring Batch | Dave Syer
Spring Day | Behind the Scenes at Spring Batch | Dave Syer
 
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam BrannenSpring Day | Spring 3.1 in a Nutshell | Sam Brannen
Spring Day | Spring 3.1 in a Nutshell | Sam Brannen
 
Spring Day | Identity Management with Spring Security | Dave Syer
Spring Day | Identity Management with Spring Security | Dave SyerSpring Day | Identity Management with Spring Security | Dave Syer
Spring Day | Identity Management with Spring Security | Dave Syer
 
Spring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard WolffSpring Day | Spring and Scala | Eberhard Wolff
Spring Day | Spring and Scala | Eberhard Wolff
 
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver GierkeSpring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
Spring Day | Data Access 2.0? Please Welcome Spring Data! | Oliver Gierke
 
Keynote | The Rise and Fall and Rise of Java | James Governor
Keynote | The Rise and Fall and Rise of Java | James GovernorKeynote | The Rise and Fall and Rise of Java | James Governor
Keynote | The Rise and Fall and Rise of Java | James Governor
 
Java Tech & Tools | OSGi Best Practices | Emily Jiang
Java Tech & Tools | OSGi Best Practices | Emily JiangJava Tech & Tools | OSGi Best Practices | Emily Jiang
Java Tech & Tools | OSGi Best Practices | Emily Jiang
 
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
Java Tech & Tools | Beyond the Data Grid: Coherence, Normalisation, Joins and...
 
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
Java Tech & Tools | Big Blobs: Moving Big Data In and Out of the Cloud | Adri...
 
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
Java Tech & Tools | Social Media in Programming in Java | Khanderao KandJava Tech & Tools | Social Media in Programming in Java | Khanderao Kand
Java Tech & Tools | Social Media in Programming in Java | Khanderao Kand
 
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
Java Tech & Tools | Just Keep Passing the Message | Russel WinderJava Tech & Tools | Just Keep Passing the Message | Russel Winder
Java Tech & Tools | Just Keep Passing the Message | Russel Winder
 
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter LedbrookJava Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
Java Tech & Tools | Grails in the Java Enterprise | Peter Ledbrook
 
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
Java Tech & Tools | Deploying Java & Play Framework Apps to the Cloud | Sande...
 
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim WardJava EE | Modular EJBs for Enterprise OSGi | Tim Ward
Java EE | Modular EJBs for Enterprise OSGi | Tim Ward
 
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan GallimoreJava EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
Java EE | Apache TomEE - Java EE Web Profile on Tomcat | Jonathan Gallimore
 
Java Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil Bartlett
Java Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil BartlettJava Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil Bartlett
Java Core | Java 8 and OSGi Modularisation | Tim Ellison & Neil Bartlett
 

Último

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
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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...
 
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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
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...
 
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...
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
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...
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
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
 
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)
 
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
 

Java Core | Understanding the Disruptor: a Beginner's Guide to Hardcore Concurrency | Trisha Gee & Mike Barker

  • 1. Trisha Gee & Michael Barker / LMAX The Disruptor - A Beginners Guide to Hardcore Concurrency
  • 2. Why is Concurrency So Difficult ?
  • 3. Program Order: Execution Order (maybe): int w = 10; int x = 20; int x = 20; int y = 30; int y = 30; int b = x * y; int z = 40; int w = 10; int a = w + z; int z = 40; int b = x * y; int a = w + z;
  • 4.
  • 5. Why Should We Care About the Details ?
  • 6. static long foo = 0; private static void increment() { for (long l = 0; l < 500000000L; l++) { foo++; } }
  • 7. public static long foo = 0; public static Lock lock = new Lock(); private static void increment() { for (long l = 0; l < 500000000L; l++){ lock.lock(); try { foo++; } finally { lock.unlock(); } } }
  • 8. static AtomicLong foo = new AtomicLong(0); private static void increment() { for (long l = 0; l < 500000000L; l++) { foo.getAndIncrement(); } }
  • 9. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms
  • 10. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms • One Thread (volatile): 4 700 ms (15x)
  • 11. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms • One Thread (volatile): 4 700 ms (15x) • One Thread (Atomic) : 5 700 ms (19x)
  • 12. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms • One Thread (volatile): 4 700 ms (15x) • One Thread (Atomic) : 5 700 ms (19x) • One Thread (Lock) : 10 000 ms (33x)
  • 13. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms • One Thread (volatile): 4 700 ms (15x) • One Thread (Atomic) : 5 700 ms (19x) • One Thread (Lock) : 10 000 ms (33x) • Two Threads (Atomic) : 30 000 ms (100x)
  • 14. Cost of Contention Increment a counter 500 000 000 times. • One Thread : 300 ms • One Thread (volatile): 4 700 ms (15x) • One Thread (Atomic) : 5 700 ms (19x) • One Thread (Lock) : 10 000 ms (33x) • Two Threads (Atomic) : 30 000 ms (100x) • Two Threads (Lock) : 224 000 ms (746x) ^^^^^^^^ ~4 minutes!!!
  • 15. Parallel v. Serial - String Split Guy Steele @ Strangle Loop: http://www.infoq.com/presentations/Thinking-Parallel- Programming Scala Implementation and Brute Force version in Java: https://github.com/mikeb01/folklore/ 15
  • 16. Parallel (Scala) Serial (Java) 2000.0 1500.0 1000.0 500.0 0 String Split (ops/sec) higher is better
  • 17. CPUs Are Getting Faster 17
  • 18. Ya Rly! P8600 (Core 2 Duo) E5620 (Nehalem EP) i7 2667M (Sandy Bridge ULV) i7 2720QM (Sandy Bride) 3000.0 2250.0 1500.0 750.0 0 String Split 18
  • 19. What Problem Were Trying To Solve ?
  • 20. 20
  • 21. 21
  • 22. Why Queues Suck - Array Backed 22
  • 23. Why Queues Suck - Linked List 23
  • 24. Why Queues Suck - Linked List 24
  • 26. 26
  • 27. How Fast Is It - Throughput ABQ Disruptor 30000000.0 22500000.0 15000000.0 7500000.0 0 Unicast Diamond 27
  • 28. How Fast Is It - Latency ABQ Disruptor Min 145 29 Mean 32,757 52 99 Percentile 2,097,152 128 99.99 Percentile 4,194,304 8,192 Max 5,069,086 175,567 28
  • 29. How Does It Work ?
  • 30. Ordering and Visibility private static final int SIZE = 32; private final Object[] data = new Object[SIZE]; private volatile long sequence = -1; private long nextValue = -1; public void publish(Object value) { long index = ++nextValue; data[(int)(index % SIZE)] = value; sequence = index; } public Object get(long index) { if (index <= sequence) { return data[(int)(index % SIZE)]; } return null; } 30
  • 31. Ordering and Visibility - Store mov $0x1,%ecx add 0x18(%rsi),%rcx ;*ladd ;... lea (%r12,%r8,8),%r11 ;*getfield data ;... mov %r12b,(%r11,%r10,1) mov %rcx,0x10(%rsi) lock addl $0x0,(%rsp) ;*ladd 31
  • 32. Ordering and Visibility - Load mov %eax,-0x6000(%rsp) push %rbp sub $0x20,%rsp ;*synchronization entry ; - RingBuffer::get@-1 mov 0x10(%rsi),%r10 ;*getfield sequence ; - RingBuffer::get@2 cmp %r10,%rdx jl 0x00007ff92505f22d ;*iflt ; - RingBuffer::get@6 mov %edx,%r11d ;*l2i ; - RingBuffer::get@14 32
  • 33. Look Ma’ No Memory Barrier AtomicLong sequence = new AtomicLong(-1); public void publish(Object value) { long index = ++nextValue; data[(int)(index % SIZE)] = value; sequence.lazySet(index); } 33
  • 34. False Sharing - Hidden Contention 34
  • 35. Cache Line Padding public class PaddedAtomicLong extends AtomicLong { public volatile long p1, p2, p3, p4, p5, p6 = 7L; //... lines omitted public long sumPaddingToPreventOptimisation() { return p1 + p2 + p3 + p4 + p5 + p6; } } 35
  • 36. Summary • Concurrency is a tool • Ordering and visibility are the key challenges • For performance the details matter • Don't believe everything you read o Come up with your own theories and test them! 36

Notas del editor

  1. (Trish)\n\nIntroduce ourselves, mention the award :)\n\n&amp;#x201C;Duke&apos;s Choice Award for&amp;#xA0;Innovative Programming Framework&amp;#x201D;\n\nIntroduce what we&amp;#x2019;re going to cover\n - concurrency/performance\n - deep &amp; narrow\n - contradictory - going to argue against abstractions\n
  2. (Trish)\n\nAll of the ask the audience questions:\n\n- Who works with concurrent code daily?\n- Who finds concurrency difficult?\n- Who cares about performance?\n\n
  3. (Trish)\n\nCompilers and CPUs are allowed to reorder instructions as long as program semantics are maintained. &amp;#xA0;Without any explicit requests those correctness semantics are limited to observers in the same thread.\n\nDifferent CPUs reorder&amp;#xA0;instructions&amp;#xA0;to varying degrees. &amp;#xA0;E.g. Intel x86 not much, DEC Alpha lots, Intel Atom not at all.\n\nUnless explicit instructions are used to ensure ordering observers in another thread will see different results. &amp;#xA0;I.e. a separate thread can&apos;t assume that because z = 40 is true, x = 20 is also true. &amp;#xA0;It may not have happened yet.\n\nThat&apos;s if the other thread can even see the data, throw to next slide on visibility.\n\n1) Compilers and CPUs are free to re-order instructions\n2) Different CPUs reorder different amounts - intel x86 not much\n3) unless otherwise specified, you can only guarantee ordering within same thread\n4) other thread can&apos;t guarantee order. x is not necessarily 20. if it&apos;s even visible\n\n
  4. (Trish)\n\nMemory on modern CPUs consists of multiple layers of buffering and caching. &amp;#xA0;Storing a value does not mean that it is immediately visible to all threads running on all cores.\n\nExplicit instructions are needed to make data&amp;#xA0;visible&amp;#xA0;to other threads.\n\nThis is the crux of why concurrency is hard. &amp;#xA0;The logical order of your program is not maintained when&amp;#xA0;observing&amp;#xA0;it from another thread (but sometimes it is).\n\nThere are tools to help reason about concurrent programs, the main one is the Memory Model. Memory Models exist at multiple levels langauge, VM and CPUs all can have memory models. &amp;#xA0;Java fortunately has a good one which is portable. &amp;#xA0;C++ only introduced one in the most recent spec. &amp;#xA0;C++ programmers often had to think about the CPUs memory model more often (though there are helpful libraries and compiler&amp;#xA0;intrinsics&amp;#xA0;too).\n\nReordering and smart use of caching is the result of many years of hardware engineering applied to deal with the signficant performance mismatch between the CPU and accessing Memory.\n\nHowever correctness is not the only concern, a lack of understanding of the detail can lead to other problems...(thow to Mike).\n\n1) different layers of storage on modern cpu (explain diag)\n2) the different levels exist because main memory is slow\n3) data for your instruction could be at any of these levels. &amp;#xA0;threads on a different CPU might not see it\n4) Java Memory Model is a good tool to reason about concurrent programming, and it&apos;s cross platform\n
  5. (Mike)\n\nAre we taking a step back (Martijn)? &amp;#xA0;Yes, necessarily so. Parallelism and concurrency are means to an end, not an end in their own right. &amp;#xA0;The goal being performance. &amp;#xA0;Performance generally covers throughput, latency, scalability. &amp;#xA0;I&apos;d through in 4th, energy efficiency. Concurrent code can bring with it a number of performance surprises, let&apos;s look an example.\n\n
  6. \n
  7. \n
  8. \n
  9. \n
  10. \n
  11. \n
  12. \n
  13. \n
  14. (Mike)\n\nUp to 3 orders of magnitude difference in performance between best and worst case. Important to understand what&apos;s happening at the lower layers to &amp;#xA0;understand why code can perform poorly. Understanding the details of how concurrency works at the machine level makes understanding higher level concurrency models (e.g. Clojure STM, Queues, Actors) much easier. Locks require kernal arbitration. The Disruptor looks to remove as much contention as possible.\n
  15. (Mike)\n\nDon&apos;t be scared of putting code into a single thread. Ported Guy Steele&apos;s algorithm written in Fortress to Scala and compared it to a brute single threaded implementation. Use Scala&apos;s new parallel collections library. Not too much detail on the algorithm, based on divide/conqueor model to fit easily with fork/join.\n\n
  16. (Mike)\n\nTested with a copy of Alice in Wonderland. Guess how many cores used to get 440 ops/sec? &amp;#xA0;8 cores with hyper-threading - 16 concurrent threads. While eventually the Scala version would be faster, given enough cores it is horribly inefficient with its use of energy. This is likely to be more of an issue as we move into the future. Don&apos;t take this as a negative regarding Scala&apos;s parallel collections.\n\n
  17. (Mike)\n\nCPU performance is more&amp;#xA0;complicated&amp;#xA0;than a simple measurement of GHz or number of cores. &amp;#xA0;Many other factors come into place. &amp;#xA0;Cache size, cache speed, bus architecture, data path sizes, numbers of caches.....\n
  18. \n
  19. (Trish)\n\nIntro to lmax\nReal world problems\nDR / replication, high availability\n\n
  20. (Trish)\n\nSEDA Architecture. A real enterprise solution needs more than just business logic. DR, journalling - gives you reliability\n\nThis is a single service. Business logic is the interesting thing.\n\n
  21. (Trish)\n\n- Testing showed each queue had its own latency overhead. \n- When you add it all up, even including your IO for replication and journalling, it&apos;s a big chunk of overall latency.\n- Business logic is such a tiny amount of time\n\n
  22. \n
  23. \n
  24. \n
  25. (Trish)\n\nIntroduce the Ring buffer, talk about how this is the basis of the Disruptor\n\nAll event processors own their own sequence numbers. Producer writes to ringbuffer, which updates its sequence number. Consumer reads from ring buffer, and writes to its own sequence number\n\n1) RingBuffer/Disruptor intro\n2) Producer writes, consumer reads\n3) Individual sequence numbers\n\n
  26. (Trish)\n\nNote that things can now be parallelised\n\n
  27. \n
  28. \n
  29. Diving in deep! If assembly code scares you, go get beer now\n\n
  30. \n
  31. \n
  32. (Mike)\n\nIntel doesn&apos;t need a&amp;#xA0;special&amp;#xA0;instruction for volatile reads. &amp;#xA0;Just ensures that it is not stored in a register and that the write takes care of the cache invalidation. &amp;#xA0;Doesn&apos;t reorder reads with respect to each other. &amp;#xA0;Intel has a strong memory model. &amp;#xA0;Other CPU would require fence instructions on the read too.\n\n*notes from Brown bag*\nFriendlier comment/java code\ntalk about where cache lines are flushed\ntalk about number of cycles\n\n
  33. \n
  34. (Trish)\n\nexplain cache lines\n64 bytes\n\nthread 2 reads tail\nthread 1 writes head\nthread 2 needs to reload tail\n
  35. (Trish)\n(Still not sure about this, doesn&apos;t make it clear where the sequence number is)\n\ncreate a bunch of empty longs to pad out the cache line\nadd them to a public method so they don&apos;t get optimised away\n\nsee our shiny method names\n\n
  36. Concurrency is a good way to make your code slower and more complex.\n
  37. \n