Fibers – blocking is cheap in a Parallel Universe jPrime Stefan Minev

Slides:



Advertisements
Similar presentations
Processes and Threads Chapter 3 and 4 Operating Systems: Internals and Design Principles, 6/E William Stallings Patricia Roy Manatee Community College,
Advertisements

Go Language * Go - Routines * Channels. New Concepts Do not communicate by sharing memory; instead, share memory by communicating. creating shared memory.
1 Chapter 5 Threads 2 Contents  Overview  Benefits  User and Kernel Threads  Multithreading Models  Solaris 2 Threads  Java Threads.
Threads Irfan Khan Myo Thein What Are Threads ? a light, fine, string like length of material made up of two or more fibers or strands of spun cotton,
Chapter 5: Threads Overview. Multithreading Models. Threading Issues. Thread Pools. Java ThreadLocal. Pthreads. Solaris 2 Threads. Java Threads.
Threads 1 CS502 Spring 2006 Threads CS-502 Spring 2006.
1 School of Computing Science Simon Fraser University CMPT 300: Operating Systems I Ch 4: Threads Dr. Mohamed Hefeeda.
Threads CSCI 444/544 Operating Systems Fall 2008.
1/28/2004CSCI 315 Operating Systems Design1 Operating System Structures & Processes Notice: The slides for this lecture have been largely based on those.
Threads. Processes and Threads  Two characteristics of “processes” as considered so far: Unit of resource allocation Unit of dispatch  Characteristics.
1 Threads Chapter 4 Reading: 4.1,4.4, Process Characteristics l Unit of resource ownership - process is allocated: n a virtual address space to.
Apache Jakarta Tomcat Suh, Junho. Road Map Tomcat Overview Tomcat Overview History History What is Tomcat? What is Tomcat? Servlet Container.
Chapter 51 Threads Chapter 5. 2 Process Characteristics  Concept of Process has two facets.  A Process is: A Unit of resource ownership:  a virtual.
Processes Part I Processes & Threads* *Referred to slides by Dr. Sanjeev Setia at George Mason University Chapter 3.
1 Lecture 4: Threads Operating System Fall Contents Overview: Processes & Threads Benefits of Threads Thread State and Operations User Thread.
1 Module Objective & Outline Module Objective: After completing this Module, you will be able to, appreciate java as a programming language, write java.
10/16/ Realizing Concurrency using the thread model B. Ramamurthy.
Processes & Threads Bahareh Goodarzi. Single & Multiple Thread of control code files data code files data.
© 2004, D. J. Foreman 2-1 Concurrency, Processes and Threads.
Silberschatz, Galvin and Gagne  2002 Modified for CSCI 399, Royden, Operating System Concepts Operating Systems Lecture 7 OS System Structure.
Continuations And Java Regis -
Threads G.Anuradha (Reference : William Stallings)
CPS110: Implementing threads Landon Cox. Recap and looking ahead Hardware OS Applications Where we’ve been Where we’re going.
Chapter 4: Threads. 4.2 Silberschatz, Galvin and Gagne ©2005 Operating System Concepts – 7 th edition, Jan 23, 2005 Chapter 4: Threads Overview Multithreading.
Threads-Process Interaction. CONTENTS  Threads  Process interaction.
Operating Systems Unit 2: – Process Context switch Interrupt Interprocess communication – Thread Thread models Operating Systems.
Silberschatz, Galvin and Gagne ©2009Operating System Concepts – 8 th Edition Chapter 4: Threads.
7/9/ Realizing Concurrency using Posix Threads (pthreads) B. Ramamurthy.
Tutorial 2: Homework 1 and Project 1
Chapter 4 – Thread Concepts
Chapter 4: Threads Modified by Dr. Neerja Mhaskar for CS 3SH3.
Introduction to threads
Object Oriented Programming in
OPERATING SYSTEM CONCEPT AND PRACTISE
Chapter 4: Threads.
Processes and threads.
Before You Begin Nahla Abuel-ola /WIT.
Google Web Toolkit Tutorial
Realizing Concurrency using the thread model
Day 12 Threads.
Concurrency, Processes and Threads
Chapter 4 – Thread Concepts
CS399 New Beginnings Jonathan Walpole.
Chapter 2 Processes and Threads Today 2.1 Processes 2.2 Threads
Operating System (013022) Dr. H. Iwidat
Chapter 4: Multithreaded Programming
CS 3305 System Calls Lecture 7.
Lecture 7 Processes and Threads.
Introduction Enosis Learning.
Chapter 4: Threads.
Chapter 4: Threads.
Node.Js Server Side Javascript
Introduction Enosis Learning.
Realizing Concurrency using Posix Threads (pthreads)
Chapter 4: Threads.
Realizing Concurrency using the thread model
Realizing Concurrency using the thread model
Lecture Topics: 11/1 General Operating System Concepts Processes
Threads and Concurrency
Threads Chapter 4.
CHAPTER 4:THreads Bashair Al-harthi OPERATING SYSTEM
Multithreaded Programming
Realizing Concurrency using the thread model
Realizing Concurrency using Posix Threads (pthreads)
Realizing Concurrency using the thread model
Chapter 4: Threads & Concurrency
Chapter 4: Threads.
Realizing Concurrency using Posix Threads (pthreads)
Concurrency, Processes and Threads
Presentation transcript:

Fibers – blocking is cheap in a Parallel Universe jPrime 2017 Stefan Minev

Stefan Minev Software Developer

Quasar -  library that provides high-performance lightweight threads and actors for the JVM Quick overview Demo QA are during the demo and after it Private and confidential

At the beginning the threads were “Green”

What are fibers? Thread is a continuation scheduled to run on a CPU core at the appropriate time by a scheduler. Continuation is a program counter, marking a given point in the sequence of instructions, and a stack, storing the values of the variables. Quasar fibers are threads implemented in JVM bytecode rather than in the OS kernel.

Why fibers instead of OS threads? Threads are expensive: Thread switching incurs significant performance penalty. The kernel schedules threads by using a general-purpose scheduling algorithm. We can have only few thousands of them. Quasar fibers are cheap Fibers put a far lesser burden on the CPU when task-switching. Fibers are scheduled at the application layer by a high-quality work-stealing scheduler that uses fork join pool. Ideal for transactions that block very often. We can have millions of them - an idle fiber occupies ~400 bytes of RAM.

How do fibers work? new Fiber<>((SuspendableRunnable) () -> { // Instrumentation here - set bytecode index on resume long mills = System.currentTimeMillis(); System.out.println( "before suspension: " + mills); // Instrumentation here - store variables Fiber.sleep(3000); // Instrumentation here - restore variables System.out.println("and now on resume: " + mills); }.start(); before suspension: 1494491529024 and now on resume: 1494491529024

Identify suspendable methods to be instrumented Throws SuspendExecution exception in a method signature Add @Suspendable annotation to a method List classes/methods in suspendables and suspendable-supers files Use the SuspendablesScanner Ant task to fill the suspendable-supers file Use the SuspendablesScanner Ant task in auto mode to fill suspendables and suspendable-supers files without manually marking suspendable methods at all. Could end up instrumenting more than necessary.

Ways to perform the bytecode instrumentation Running the Instrumentation Java Agent (-javaagent:path-to-quasar-jar.jar as a JVM argument). This works with standalone Java applications and embedded Servlet containers. Ahead-of-Time (AOT) Instrumentation by running InstrumentationTask via Ant after compilation. Using QuasarWebAppClassLoader for standalone Servlet containers (Tomcat and Jetty).

Official caveats Runaway fibers – stuck in a loop or blocking the thread Thread locals in Fibers – works as expected Synchronized and blocking “Thread” calls – not allowed by default but could be handled

“Do not communicate by sharing memory; instead, share memory by communicating.” Channels - queues used to pass messages between strands (fibers and threads). ch: = make(chan string) go func() { ch < -"ping" }() msg: = < -ch fmt.Println(msg) Channel<String> ch = Channels.newChannel(0); new Fiber( ()-> ch.send("ping") ).start(); String msg = ch.receive(); System.out.println(msg);

Quasar’s Actor System Quasar provides an implementation of the actor model practically identical to Erlang’s. Akka’s callback-based API vs Quasar’s fiber blocking API

Comsat – your universe implemented with fibers Servlets – extending FiberHttpServlet instead of HttpServlet JAX-RS API –using the co.paralleluniverse.fibers.jersey.ServletContainer with enabled async support instead of the one provided by Jersey. HTTP clients - Apache Http Client, JAX-RS client, Retrofit and OkHttp DB access – JDBC, MongoDB, JDBI, JOOQ Dropwizard Spring – Spring Web MVC, Spring Boot, Spring Security Web Actors – it is a new API

Demo

Awesome! What’s the gain? Go on with your easy to write synchronous code and benefit from the power and efficiency of the asynchronous programming. Just leave the complexity in a Parallel Universe! Private and confidential

So…is it production ready and should I use it?

Let’s make Java great again! “Note: work is ongoing with the OpenJDK team that will allow to remove this restriction completely starting with a JDK9 version of Quasar: efficient, automatic runtime instrumentation will be performed at the bytecode level, that is for all code written in any JVM language, without need anymore for annotations nor instrumentation plugins.”

Questions?