Project Loom Ron Pressler, Alan Bateman June 2018

Similar documents
Alan Bateman Java Platform Group, Oracle November Copyright 2018, Oracle and/or its affiliates. All rights reserved.!1

Chapter 4: Threads. Overview Multithreading Models Thread Libraries Threading Issues Operating System Examples Windows XP Threads Linux Threads

Welcome to the session...

Threads Questions Important Questions

Highlights from Java 10, 11 and 12 and Future of Java Javaland by Vadym Kazulkin, ip.labs GmbH

Overview of Java Threads (Part 2)

Ahead of Time (AOT) Compilation

Chapter 4: Threads. Operating System Concepts. Silberschatz, Galvin and Gagne

MultiThreading. Object Orientated Programming in Java. Benjamin Kenwright

Multitasking. Multitasking allows several activities to occur concurrently on the computer Levels of multitasking: Process based multitasking

Java ME Directions. JCP F2F - Austin. Florian Tournier - Oracle May 9, Copyright 2017, Oracle and/or its affiliates. All rights reserved.

OPERATING SYSTEM. Chapter 4: Threads

Capriccio : Scalable Threads for Internet Services

Chair of Software Engineering. Java and C# in depth. Carlo A. Furia, Marco Piccioni, Bertrand Meyer. Java: concurrency

Mark Falco Oracle Coherence Development

CS 351 Design of Large Programs Threads and Concurrency

Operating Systems 2 nd semester 2016/2017. Chapter 4: Threads

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

CS 450 Operating System Week 4 Lecture Notes

The New Java Technology Memory Model

Programmazione Avanzata e Paradigmi Ingegneria e Scienze Informatiche - UNIBO a.a 2013/2014 Lecturer: Alessandro Ricci

CS455: Introduction to Distributed Systems [Spring 2019] Dept. Of Computer Science, Colorado State University

Chapter 4: Threads. Chapter 4: Threads

Safe Harbor Statement

What is a thread anyway?

Chapter 4: Threads. Operating System Concepts 9 th Edit9on

JDK 9, 变化与未来. Xuelei Fan

CS420: Operating Systems

What every DBA needs to know about JDBC connection pools Bridging the language barrier between DBA and Middleware Administrators

INF 212 ANALYSIS OF PROG. LANGS CONCURRENCY. Instructors: Crista Lopes Copyright Instructors.

Copyright 2012, Oracle and/or its affiliates. All rights reserved.

<Insert Picture Here> Future<JavaEE>

InnoDB: What s new in 8.0

Using Java CompletionStage in Asynchronous Programming

Native POSIX Thread Library (NPTL) CSE 506 Don Porter

End-to-End Java Security Performance Enhancements for Oracle SPARC Servers Performance engineering for a revenue product

CS 455: INTRODUCTION TO DISTRIBUTED SYSTEMS [THREADS] Frequently asked questions from the previous class survey

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Transformation-free Data Pipelines by combining the Power of Apache Kafka and the Flexibility of the ESB's

NOSQL DATABASE CLOUD SERVICE. Flexible Data Models. Zero Administration. Automatic Scaling.

Lecture 03: Thread API (continue)

CMSC 433 Programming Language Technologies and Paradigms. Concurrency

Truffle A language implementation framework

Threads and Java Memory Model

Copyright 2014 Oracle and/or its affiliates. All rights reserved.

Motivation. Threads. Multithreaded Server Architecture. Thread of execution. Chapter 4

THREADS AND CONCURRENCY

Contents. G53SRP: Java Threads. Definition. Why we need it. A Simple Embedded System. Why we need it. Java Threads 24/09/2009 G53SRP 1 ADC

CMSC 330: Organization of Programming Languages

Chapter 4: Multithreaded

Chapter 4: Threads. Operating System Concepts 9 th Edition

Chapter 4: Threads. Chapter 4: Threads

Oracle Solaris Virtualization: From DevOps to Enterprise

Today s Topics. u Thread implementation. l Non-preemptive versus preemptive threads. l Kernel vs. user threads

Processes and Threads

Asynchronous API with CompletableFuture

Chapter 4: Multithreaded Programming

Introduction to Asynchronous Programming Fall 2014

Computation Abstractions. Processes vs. Threads. So, What Is a Thread? CMSC 433 Programming Language Technologies and Paradigms Spring 2007

Intel Threading Tools

Java Threads. COMP 585 Noteset #2 1

Making The Future Java

The Z Garbage Collector Scalable Low-Latency GC in JDK 11

Asynchronous API with CompletableFuture

ECE 550D Fundamentals of Computer Systems and Engineering. Fall 2017

CSE 4/521 Introduction to Operating Systems

THREADS & CONCURRENCY

Chapter 4: Threads. Operating System Concepts 9 th Edition

Interoperation of tasks

Keep Learning with Oracle University

Java in a World of Containers

What s New in MySQL 5.7 Geir Høydalsvik, Sr. Director, MySQL Engineering. Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Last class: Today: Thread Background. Thread Systems

Outline. Threads. Single and Multithreaded Processes. Benefits of Threads. Eike Ritter 1. Modified: October 16, 2012

Process Description and Control

SEDA: An Architecture for Well-Conditioned, Scalable Internet Services

MultiThreading 07/01/2013. Session objectives. Introduction. Introduction. Advanced Java Programming Course

The Z Garbage Collector Low Latency GC for OpenJDK

Advanced Java Programming Course. MultiThreading. By Võ Văn Hải Faculty of Information Technologies Industrial University of Ho Chi Minh City

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Modern and Fast: A New Wave of Database and Java in the Cloud. Joost Pronk Van Hoogeveen Lead Product Manager, Oracle

September 15th, Finagle + Java. A love story (

<Insert Picture Here> Get on the Grid. JVM Language Summit Getting Started Guide. Cameron Purdy, VP of Development, Oracle

Elastic Data. Harvey Raja Principal Member Technical Staff Oracle Coherence

A Quick Tour p. 1 Getting Started p. 1 Variables p. 3 Comments in Code p. 6 Named Constants p. 6 Unicode Characters p. 8 Flow of Control p.

Chapter 4: Multi-Threaded Programming

Oracle Application Container Cloud

Application Container Cloud

Mission Possible - Near zero overhead profiling. Klara Ward Principal Software Developer Java Mission Control team, Oracle February 6, 2018

The Z Garbage Collector An Introduction

Consolidate and Prepare for Cloud Efficiencies Oracle Database 12c Oracle Multitenant Option

Operating System Performance and Large Servers 1

An Oracle Technical White Paper September Oracle VM Templates for PeopleSoft

<Insert Picture Here> Multi-language JDI? You're Joking, Right?

Threads. CS-3013 Operating Systems Hugh C. Lauer. CS-3013, C-Term 2012 Threads 1

Using the MySQL Document Store

OpenJDK Adoption Group

CMSC 132: Object-Oriented Programming II. Threads in Java

Chapter 4 Multithreaded Programming

Java Enterprise Edition

Transcription:

Project Loom Ron Pressler, Alan Bateman June 2018 Copyright 2018, Oracle and/or its affiliates. All rights reserved.!1

Safe Harbor Statement The following is intended to outline our general product direction. It is intended for information purposes only, and may not be incorporated into any contract. It is not a commitment to deliver any material, code, or functionality, and should not be relied upon in making purchasing decisions. The development, release, and timing of any features or functionality described for Oracle s products remains at the sole discretion of Oracle. Copyright 2018, Oracle and/or its affiliates. All rights reserved.!2

Project Loom Continuations Fibers Tail-calls Copyright 2018, Oracle and/or its affiliates. All rights reserved.!3

Why Fibers Today, developers are forced to choose between Connections App Connections App simple (blocking / synchronous), but less scalable code (with threads) and complex, non-legacy-interoperable, but scalable code (asynchronous) Copyright 2018, Oracle and/or its affiliates. All rights reserved.!4

Why Fibers With fibers, devs have both: simple, familiar, maintainable, interoperable code, that is also scalable Connections App Fibers make even existing server applications consume fewer machines (by increasing utilization), significantly reducing costs Copyright 2018, Oracle and/or its affiliates. All rights reserved.!5

Continuations: The User Perspective Copyright 2018, Oracle and/or its affiliates. All rights reserved.!6

What A continuation (precisely: delimited continuation) is a program object representing a computation that may be suspended and resumed (also, possibly, cloned or even serialized). Copyright 2018, Oracle and/or its affiliates. All rights reserved.!7

Continuations: User Perspective package java.lang; public class Continuation implements Runnable { public Continuation(ContinuationScope scope, Runnable body); public final void run(); public static void yield(continuationscope scope); public boolean isdone(); } protected void onpinned(reason reason) { throw new IllegalStateException("Pinned: " + reason); } Copyright 2018, Oracle and/or its affiliates. All rights reserved.!8

Continuations: User Perspective Continuation cont = new Continuation(SCOPE, () -> { while (true) { System.out.println("before"); Continuation.yield(SCOPE); System.out.println("after"); } }); while (!cont.isdone()) { cont.run(); } Copyright 2018, Oracle and/or its affiliates. All rights reserved.!9

Fibers Copyright 2018, Oracle and/or its affiliates. All rights reserved.!10

What is a fiber? A light weight or user mode thread, scheduled by the Java virtual machine, not the operating system Fibers are low footprint and have negilgible taskswitching overhead. You can have millions of them! Copyright 2018, Oracle and/or its affiliates. All rights reserved.!11

Why fibers? The runtime is well positioned to manage and schedule application threads, esp. if they interleave computation and I/O and interact very often (exactly how server threads behave) Make concurrency simple again Copyright 2018, Oracle and/or its affiliates. All rights reserved.!12

fiber = continuation + scheduler Copyright 2018, Oracle and/or its affiliates. All rights reserved.!13

fiber = continuation + scheduler A fiber wraps a task in a continuation The continuation yields when the task needs to block The continuation is continued when the task is ready to continue Scheduler executes tasks on a pool of carrier threads java.util.concurrent.executor in the current prototype Default/built-in scheduler is a ForkJoinPool Copyright 2018, Oracle and/or its affiliates. All rights reserved.!14

User facing API Current focus is on the control flow and concepts, not the API Minimal java.lang.fiber in current prototype that supports 1. Starting a fiber to execute a task 2. Parking/unparking 3. Waiting for a fiber to terminate Copyright 2018, Oracle and/or its affiliates. All rights reserved.!15

Implementing Fibers Copyright 2018, Oracle and/or its affiliates. All rights reserved.!16

A fiber wraps a user s task in a continuation The fiber task is submited to the scheduler to start or continue the continuation, essentially: mount(); try { cont.run(); } finally { unmount(); } Copyright 2018, Oracle and/or its affiliates. All rights reserved.!17

Copyright 2018, Oracle and/or its affiliates. All rights reserved.!18

ForkJoinWorkerThread.run Carrier thread waiting for work ForkJoinPool.runWorker LockSupport.park Unsafe.park Copyright 2017, Oracle and/or its affiliates. All rights reserved.!19

A fiber is scheduled on the carrier thread. The fiber task runs. ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Copyright 2017, Oracle and/or its affiliates. All rights reserved.!20

ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec The fiber runs the continuation to run the user s task. Fiber.runContinuation Continuation.run Continuation.enter Continuation.enter0 Main.lambda$main$0 Copyright 2017, Oracle and/or its affiliates. All rights reserved.!21

ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec The task attempts acquire a lock which leads to the continuation yielding Fiber.runContinuation Continuation.run Continuation.enter Continuation.enter0 Main.lambda$main$0 java.util.concurrent.reentrantlock.lock : java.util.concurrent.locksupport.park Fiber.park Contuation.yield Copyright 2017, Oracle and/or its affiliates. All rights reserved.!22

ForkJoinWorkerThread.run The continuation stack is saved and control returns to the fiber s task at the instruction following the call to Continuation.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Copyright 2017, Oracle and/or its affiliates. All rights reserved.!23

The fiber task terminates. The carrier thread goes back to waiting for work. ForkJoinWorkerThread.run ForkJoinPool.runWorker LockSupport.park Unsafe.park Copyright 2017, Oracle and/or its affiliates. All rights reserved.!24

The owner of the lock releases it. This unparks the Fiber waiting to acquire the lock by scheduling its task to run again. ReentrantLock.unlock LockSupport.unpark Fiber.unpark ForkJoinPool.execute Copyright 2017, Oracle and/or its affiliates. All rights reserved.!25

The fiber task runs again, maybe on a different carrier thread ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Copyright 2017, Oracle and/or its affiliates. All rights reserved.!26

ForkJoinWorkerThread.run The fiber task invokes Continuation run (again) to continue it : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Continuation.run Copyright 2017, Oracle and/or its affiliates. All rights reserved.!27

ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Continuation.run Continuation.enter Continuation.enter0 Main.lambda$main$0 java.util.concurrent.reentrantlock.lock The stack is restored and control continues at the instruction following the call to Continuation.yield : java.util.concurrent.locksupport.park Fiber.park Copyright 2017, Oracle and/or its affiliates. All rights reserved.!28

ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation Continuation.run Continuation.enter The user s task continues. Continuation.enter0 Main.lambda$main$0 Copyright 2017, Oracle and/or its affiliates. All rights reserved.!29

ForkJoinWorkerThread.run : ForkJoinTask$RunnableExecuteAction.exec Fiber.runContinuation The user s task completes and the continuation terminates. Control returns to the fiber s task at the instruction following the call to Continuation.run Copyright 2017, Oracle and/or its affiliates. All rights reserved.!30

How much existing code can fibers run? A big question, lots of trade-offs Do we completely re-imagine threads? Do we attempt to allow all existing code to run in the context of a fiber? Likely to wrestle with this topic for a long time Current prototype can run existing code but with some limitations, as we will see Copyright 2018, Oracle and/or its affiliates. All rights reserved.!31

Example using existing code/libraries Example uses Jetty and Jersey Copyright 2018, Oracle and/or its affiliates. All rights reserved.!32

Example with existing code/libraries Assume servlet or REST service that spends a long time waiting assume this takes 100ms Copyright 2018, Oracle and/or its affiliates. All rights reserved.!33

Default configuration (maxthreads = 200), load = 5000 HTTP request/s Copyright 2018, Oracle and/or its affiliates. All rights reserved.!34

maxthreads = 400, load = 5000 HTTP request/s Copyright 2018, Oracle and/or its affiliates. All rights reserved.!35

fiber per request, load = 5000 HTTP request/s Copyright 2018, Oracle and/or its affiliates. All rights reserved.!36

Limitations Can t yield with native frames on continuation stack may park/yield native method Copyright 2018, Oracle and/or its affiliates. All rights reserved.!37

Limitations Can t yield while holding or waiting for a monitor may park carrier thread may park carrier thread Copyright 2018, Oracle and/or its affiliates. All rights reserved.!38

Limitations Current limitations Can t yield with native frames on continuation stack Can t yield while holding or waiting for a monitor In both cases, parking may pin the carrier thread What about the existing Thread API and Thread.currentThread()? Copyright 2018, Oracle and/or its affiliates. All rights reserved.!39

Relationship between Fiber and Thread in current prototype Strand Thread Fiber Copyright 2018, Oracle and/or its affiliates. All rights reserved.!40

Thread.currentThread() and Thread API in current prototype Current prototype First use of Thread.currentThread() in a fiber creates a shadow Thread unstarted Thread from perspective of VM, no VM meta data Shadow Thread implements Thread API except for stop, suspend, resume, and uncaught exception handlers Thread locals become fiber local (for now) ThreadLocal and the baggage that is InheritableThreadLocal, context ClassLoader,.. Special case ThreadLocal for now to avoid needing Thread object Copyright 2018, Oracle and/or its affiliates. All rights reserved.!41

Thread Locals Spectrum of uses Container managed cache of connection or credentials context Approximating processor/core local in lower level libraries Significant topic for later Copyright 2018, Oracle and/or its affiliates. All rights reserved.!42

Footprint Thread Typically 1MB reserved for stack + 16KB of kernel data structures ~2300 bytes per started Thread, includes VM meta data Fiber Continuation stack: hundreds of bytes to KBs 200-240 bytes per fiber in current prototype Copyright 2018, Oracle and/or its affiliates. All rights reserved.!43

APIs that potentially park Thread sleep, join java.util.concurrent and LockSupport.park I/O Networking I/O: socket read/write/connect/accept File I/O Pipe I/O Copyright 2018, Oracle and/or its affiliates. All rights reserved.!44

Communication between fibers Current prototype executes tasks as Runnable. Easy to use CompletableFuture too. j.u.concurrent just works so can share objects or share by communicating Not an explicit goal at this time to introduce new concurrency APIs but new APIs may emerge Copyright 2018, Oracle and/or its affiliates. All rights reserved.!45

Implementing Continuations Copyright 2018, Oracle and/or its affiliates. All rights reserved.!46

We need: Millions of continuations (=> low RAM overhead) Fast task-switching (=> no stack copying) Copyright 2018, Oracle and/or its affiliates. All rights reserved.!47

Native Stack Continuation run stack refstack Copyright 2018, Oracle and/or its affiliates. All rights reserved.!48

Native Stack Continuation run stack refstack Entry enter Copyright 2018, Oracle and/or its affiliates. All rights reserved.!49

Native Stack Continuation run stack refstack Entry enter A B C Yield yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!50

Native Stack Continuation run stack refstack Entry enter A B C Yield yield freeze Copyright 2018, Oracle and/or its affiliates. All rights reserved.!51

Native Stack Continuation run stack refstack Entry enter A B C yield Yield yield freeze Copyright 2018, Oracle and/or its affiliates. All rights reserved.!52

Native Stack Continuation run stack refstack Entry enter A B C Examine the frame for pinning yield Yield yield freeze Copyright 2018, Oracle and/or its affiliates. All rights reserved.!53

Native Stack Continuation run stack refstack Entry enter A B C Raw copy C yield Yield yield freeze Copyright 2018, Oracle and/or its affiliates. All rights reserved.!54

Native Stack Continuation run stack refstack Entry enter A B C Extract oops C yield Yield yield freeze Copyright 2018, Oracle and/or its affiliates. All rights reserved.!55

Native Stack Continuation run stack refstack Entry Yield enter A B C yield freeze A B C yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!56

Native Stack Continuation run stack A B C yield refstack Copyright 2018, Oracle and/or its affiliates. All rights reserved.!57

Native Stack Continuation run stack refstack Entry enter docontinue A B C yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!58

Native Stack Continuation run stack refstack Entry enter A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!59

Native Stack Continuation run stack refstack Entry enter Raw copy A A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!60

Native Stack Continuation run stack refstack Entry enter Restore oops A A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!61

Native Stack Continuation run stack refstack Entry enter A Patch A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!62

Native Stack Continuation run stack refstack Entry Yield enter A B C yield thaw A B C yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!63

Native Stack Continuation run stack refstack Entry enter A B C Yield yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!64

Native Stack Continuation run stack refstack Entry enter A Lazy copy B C yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!65

Native Stack Continuation run stack refstack Entry enter docontinue A B C yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!66

Native Stack Continuation run stack refstack Entry enter A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!67

Native Stack Continuation run stack refstack Entry enter C yield A B C yield thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!68

Native Stack Continuation run stack refstack Entry enter C yield A B thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!69

Native Stack Continuation run stack refstack Entry enter C yield Install return barrier (if there are more frozen frames) A B thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!70

Native Stack Continuation run stack refstack Entry Yield enter C yield A B Copyright 2018, Oracle and/or its affiliates. All rights reserved.!71

Native Stack Continuation run stack refstack Entry enter C A B Copyright 2018, Oracle and/or its affiliates. All rights reserved.!72

Native Stack Continuation run stack refstack Entry enter A B thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!73

Native Stack Continuation run stack refstack Entry enter B A B thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!74

Native Stack Continuation run stack refstack Entry enter B A thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!75

Native Stack Continuation run stack refstack Entry enter B Install return barrier A thaw Copyright 2018, Oracle and/or its affiliates. All rights reserved.!76

Native Stack Continuation run stack refstack Entry Yield enter B D yield A Copyright 2018, Oracle and/or its affiliates. All rights reserved.!77

Native Stack Continuation run stack refstack Entry Yield enter B D yield freeze A B D yield Copyright 2018, Oracle and/or its affiliates. All rights reserved.!78

Native Stack Continuation run stack A B D yield refstack Copyright 2018, Oracle and/or its affiliates. All rights reserved.!79

Epilogue Copyright 2018, Oracle and/or its affiliates. All rights reserved.!80

Features not in current prototype Serialization and cloning JVM TI and debugging support for fibers Tail calls Copyright 2018, Oracle and/or its affiliates. All rights reserved.!81

Next Steps Design behavior and API Add missing features Improve performance Copyright 2018, Oracle and/or its affiliates. All rights reserved.!82

More information Project Loom page: http://openjdk.java.net/projects/loom/ Mailing list: loom-dev@openjdk.java.net Repo: http://hg.openjdk.java/net/loom/loom Copyright 2018, Oracle and/or its affiliates. All rights reserved.!83