Using Modern C++ In Anger Todd L. Montgomery @toddlmontgomery Aeron - - PowerPoint PPT Presentation

using modern c in anger
SMART_READER_LITE
LIVE PREVIEW

Using Modern C++ In Anger Todd L. Montgomery @toddlmontgomery Aeron - - PowerPoint PPT Presentation

StoneTor Using Modern C++ In Anger Todd L. Montgomery @toddlmontgomery Aeron and C++? What constitutes Modern C++ ? How Aeron Adopts Modern C++? What Lessons were learned? Whats Next ? Aeron and C++? Truly modern


slide-1
SLIDE 1

Using Modern C++ In Anger

Todd L. Montgomery @toddlmontgomery

StoneTor

slide-2
SLIDE 2
  • Aeron and C++?
  • What constitutes Modern C++?
  • How Aeron Adopts Modern C++?
  • What Lessons were learned?
  • What’s Next?
slide-3
SLIDE 3

Aeron and C++?

slide-4
SLIDE 4

Truly modern messaging transport

slide-5
SLIDE 5

Feature Bloat & Complexity

slide-6
SLIDE 6

Not Fast Enough

slide-7
SLIDE 7

Low & Predictable Latency is key

slide-8
SLIDE 8

We are in a new world

Multi-core, Multi-socket, Cloud...

slide-9
SLIDE 9

We are in a new world

UDP, IPC, InfiniBand,
 RDMA, PCI-e Multi-core, Multi-socket, Cloud...

slide-10
SLIDE 10

Aeron is trying a new approach

slide-11
SLIDE 11

The Team

Todd Montgomery Richard Warburton Martin Thompson

slide-12
SLIDE 12

Publishers Subscribers Channel Stream

Messaging

Channel

slide-13
SLIDE 13

A library, not a framework, on which other abstractions and applications can be built

slide-14
SLIDE 14

Composable Design

slide-15
SLIDE 15

OSI layer 4 Transport for message oriented streams

slide-16
SLIDE 16

OSI Layer 4 (Transport) Services

  • 1. Connection Oriented Communication
  • 2. Reliability
  • 3. Flow Control
  • 4. Congestion Avoidance/Control
  • 5. Multiplexing
slide-17
SLIDE 17

Multi-Everything World!

slide-18
SLIDE 18

Publishers Subscribers Channel Stream

Multi-Everything World

slide-19
SLIDE 19

Design Principles

  • 1. Garbage free in steady state running
  • 2. Smart Batching in the message path
  • 3. Wait-free algos in the message path
  • 4. Non-blocking IO in the message path
  • 5. No exceptional cases in message path
  • 6. Apply the Single Writer Principle
  • 7. Prefer unshared state
  • 8. Avoid unnecessary data copies
slide-20
SLIDE 20

Publisher Subscriber Subscriber Publisher

Architecture

IPC Log Buffer

slide-21
SLIDE 21

Sender Receiver Receiver Sender Publisher Subscriber Subscriber Publisher

Architecture

Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0)

slide-22
SLIDE 22

Conductor Sender Receiver Conductor Receiver Sender Publisher Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-23
SLIDE 23

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-24
SLIDE 24

C++?

slide-25
SLIDE 25

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-26
SLIDE 26

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues Client

  • Application API
  • Java, C, C++, C#, etc.
slide-27
SLIDE 27

The Media Driver is (currently) Java…

slide-28
SLIDE 28
slide-29
SLIDE 29

What constitutes Modern C++?

slide-30
SLIDE 30
slide-31
SLIDE 31
slide-32
SLIDE 32

Modern C++

slide-33
SLIDE 33

Resource Ownership & Lifetime

slide-34
SLIDE 34

Modern C++

  • Idioms (RAII, etc.)
slide-35
SLIDE 35

std::lock_guard std::unique_ptr

Resource Acquisition is Initialization (RAII)

http://en.cppreference.com/w/cpp/language/raii

slide-36
SLIDE 36

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
slide-37
SLIDE 37

std::shared_ptr std::unique_ptr std::weak_ptr

Smart Pointers

http://en.cppreference.com/w/cpp/memory

slide-38
SLIDE 38

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
slide-39
SLIDE 39

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
slide-40
SLIDE 40

std::atomic<bool> std::atomic_flag

Atomic Operations

http://en.cppreference.com/w/cpp/atomic

slide-41
SLIDE 41

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
slide-42
SLIDE 42

std::thread std::mutex std::promise std::future

Thread Support

http://en.cppreference.com/w/cpp/thread

slide-43
SLIDE 43

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
slide-44
SLIDE 44

Modern C++

  • Idioms (RAII, etc.)
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-45
SLIDE 45

CMake Google Test Google Mock etc.

Modern Toolchain

https://cmake.org/ https://github.com/google/googletest

slide-46
SLIDE 46

How Aeron Adopts Modern C++?

slide-47
SLIDE 47

Smart Pointers

slide-48
SLIDE 48

Lambda’s & Function Objects

slide-49
SLIDE 49

Thread Support

slide-50
SLIDE 50

What Lessons were learned?

slide-51
SLIDE 51

Lessons - What do you think?

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-52
SLIDE 52

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-53
SLIDE 53
slide-54
SLIDE 54

RAII & Smart Pointers

  • Give in to Smart Pointers
slide-55
SLIDE 55

RAII & Smart Pointers

  • Give in to Smart Pointers
  • Explicit Coupling
slide-56
SLIDE 56

RAII & Smart Pointers

  • Give in to Smart Pointers
  • Explicit Coupling
  • Explicit Scoping
slide-57
SLIDE 57

[insert excuse Y] … Escape Analysis Value Types …

Stack Allocation (Lack of)

slide-58
SLIDE 58
slide-59
SLIDE 59
slide-60
SLIDE 60

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-61
SLIDE 61
slide-62
SLIDE 62

Move Construction/Assignment

  • Much more than you think
slide-63
SLIDE 63

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
slide-64
SLIDE 64

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
  • Optimization Interactions
slide-65
SLIDE 65

Move Construction/Assignment

  • Much more than you think
  • Sometimes/Often better to copy
  • Optimization Interactions
  • When you have to do it… why?!?
slide-66
SLIDE 66

Lessons

  • Idioms (RAII, etc.)
  • Stack Allocation
  • Smart Pointers
  • Lambda's & Function Objects
  • Atomics (std::atomic)
  • Thread Support
  • Move Construction/Assignment
  • Toolchain (Build/Test)
slide-67
SLIDE 67
slide-68
SLIDE 68

Client Media Driver Media Driver Conductor Sender Receiver Conductor Receiver Sender Client Publisher Conductor Conductor Subscriber Subscriber Publisher Admin Events

Architecture

Admin Events Media IPC Log Buffer IPC Ring/Broadcast Buffer Media (UDP, InfiniBand, PCI-e 3.0) Function/Method Call Volatile Fields & Queues

slide-69
SLIDE 69

Data Structures (Shared Memory)

  • IPC Ring Buffers
  • IPC Broadcast Buffers
  • IPC Log Buffers
slide-70
SLIDE 70

Creates a


replicated persistent log


  • f messages

What Aeron does

slide-71
SLIDE 71

Tail File

slide-72
SLIDE 72

Tail File Message 1 Header

slide-73
SLIDE 73

Tail File Message 1 Header Message 2 Header

slide-74
SLIDE 74

Tail File Message 1 Header Message 2 Header

slide-75
SLIDE 75

Tail File Message 1 Header Message 2 Header Message 3

slide-76
SLIDE 76

Tail File Message 1 Header Message 2 Header Message 3 Header

slide-77
SLIDE 77

Log Buffer File

Term 0 Term 1 Term 2 Term Meta Data 0 Term Meta Data 2 Term Meta Data 1 Log Meta Data

slide-78
SLIDE 78

Log Buffer File

Term 0 Term 1 Term 2 Term Meta Data 0 Term Meta Data 2 Term Meta Data 1 Log Meta Data

Atomic & Ordered Operations

slide-79
SLIDE 79

Position

slide-80
SLIDE 80

Unique identification of a byte within each stream

slide-81
SLIDE 81

Publishers, Senders,
 Receivers, and Subscribers
 all keep position counters

slide-82
SLIDE 82

Position counters are the key to
 flow control and monitoring

slide-83
SLIDE 83

Statistics & Position Counters are accessible in shared memory

Atomic & Ordered Operations

slide-84
SLIDE 84

Multiple Challenges

  • Size (and Layout)
  • Memory Models (C++11 to Java)
slide-85
SLIDE 85

Size

Matters

slide-86
SLIDE 86
slide-87
SLIDE 87

Size Matters (Atomics)

  • Not designed for arbitrary memory
  • Size not the same as the types
  • Concerned only with operations
slide-88
SLIDE 88

Memory Models

  • Interoperability with JMM
  • std::memory_order fit for purpose
slide-89
SLIDE 89

So… Aeron uses its own atomic

  • perations C/C++ functions

(JMM compatible)

* gcc x86_64 initially contributed by phaynes (phaynes@threatmetrix.com)

slide-90
SLIDE 90

What’s Next?

slide-91
SLIDE 91

Finished a few passes of
 Profiling and Tuning

slide-92
SLIDE 92

C++ to C++ 32+ Million messages per second

IPC, 32-byte Messages

slide-93
SLIDE 93

C++: 32M msg/sec Java: 30M msg/sec .NET: 15M msg/sec

IPC, 32-byte Messages

slide-94
SLIDE 94

Replication Services Aeron Core Persistence Performance C/C++ Driver Multi Unicast Send Efficient FEC Encryption/Security

1.0!!

slide-95
SLIDE 95

In closing…

slide-96
SLIDE 96
slide-97
SLIDE 97

https://github.com/real-logic/Aeron

Where can I find it?

slide-98
SLIDE 98

Aeron: https://github.com/real-logic/Aeron Twitter: @toddlmontgomery

Thank You!

Questions?