94
The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT CSAIL 1 / 64

The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

The benefits and costs of writing a UNIX kernel in ahigh-level language

Cody Cutler, M. Frans Kaashoek, Robert T. Morris

MIT CSAIL

1 / 64

Page 2: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

What language to use for developing a kernel?

A hotly-debated question but often with few facts

6.828 students: why are we using C? why not a type-safe language?

To shed some light, we focus on:

• A new kernel or monitor

• A language with automatic memory management (i.e., with a garbage collector)

• A traditional, monolithic UNIX kernel

2 / 64

Page 3: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

C is popular for kernels

Windows

Linux

*BSD

3 / 64

Page 4: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Why C is good: complete control

Control of memory allocation and freeing

Almost no implicit, hidden code

Direct access to memory

Few dependencies

4 / 64

Page 5: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Why C is bad

Writing secure C code is difficult

40 Linux kernel execute-code CVEs in 2017 due to memory-safety errors

(execute-code CVE is a bug that enables attacker to run malicious code in kernel)

5 / 64

Page 6: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

High-level languages (HLLs) provide memory-safety

All 40 CVEs would not execute malicious code in an HLL

6 / 64

Page 7: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

HLL benefits

Type safety

Automatic memory management with garbage collector

Concurrency

Abstraction

7 / 64

Page 8: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

HLL potential downsides

Poor performance:

• Bounds, cast, nil-pointer checks

• Garbage collection

Incompatibility with kernel programming:

• No direct memory access

• No hand-written assembly

• Limited concurrency or parallelism

8 / 64

Page 9: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Goal: measure HLL trade-offs

Explore total effect of using HLL instead of C:

• Impact on safety

• Impact on programmability

• Performance cost

...for production-grade kernel

9 / 64

Page 10: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Prior work: HLL trade-offs

Many studies of HLL trade-offs for user programs (Hertz’05, Yang’04)

But kernels different from user programs

(ex: more careful memory management)

Need to measure HLL trade-offs in kernel

10 / 64

Page 11: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Prior work: HLL kernels

Singularity(SOSP’07), J-kernel(ATC’98), Taos(ASPLOS’87), Spin(SOSP’95),Tock(SOSP’17), KaffeOS(ATC’00), House(ICFP’05),...

Explore new ideas and architectures

None measure HLL trade-offs vs C kernel

11 / 64

Page 12: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Measuring trade-offs is tricky

Must compare with production-grade C kernel (e.g., Linux)

Problem: can’t build production-grade HLL kernel

12 / 64

Page 13: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

The most we can do

Build HLL kernel

Keep important parts the same as Linux

Optimize until performance is roughly similar to Linux

Measure HLL trade-offs

Risk: measurements of production-grade kernels differ

13 / 64

Page 14: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Methodology

Built HLL kernel

Same apps, POSIX interface, and monolithic organization

Optimized, measured HLL trade-offs14 / 64

Page 15: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Contributions

BISCUIT, new x86-64 Go kernel

• source compatibility for Linux applications

New scheme to deal with heap exhaustion

Evaluation

• Measurements of HLL costs for two popular, kernel-intensive apps

• Description of qualitative ways HLL helped

15 / 64

Page 16: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Which HLL?

Go is a good choice:

• Easy to call assembly

• Compiled to machine code w/good compiler

• Easy concurrency

• Easy static analysis

• GC (Concurrent mark and sweep)

Rust might be a fine choice too

16 / 64

Page 17: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT overview

58 system calls, LOC: 28k Go,

1.5k assembly (boot, entry/exit)

17 / 64

Page 18: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT Features

• Multicore

• Threads

• Journaled FS (7k LOC)

• Virtual memory (2k LOC)

• TCP/IP stack (5k LOC)

• Drivers: AHCI and Intel 10Gb NIC (3k LOC)

18 / 64

Page 19: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

User programs

Process has own address space

User/kernel memory isolated by hardware

Each user thread has companion kernel thread

Kernel threads are “goroutines”

19 / 64

Page 20: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

System calls

User thread put args in registers

User thread executes SYSENTER

Control passes to kernel thread

Kernel thread executes system call, returns via SYSEXIT

20 / 64

Page 21: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT design puzzles

Runtime on bare-metal

Goroutines run different applications

Device interrupts in runtime critical sections

Hardest puzzle: heap exhaustion

21 / 64

Page 22: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Puzzle: Heap exhaustion

22 / 64

Page 23: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Puzzle: Heap exhaustion

22 / 64

Page 24: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Puzzle: Heap exhaustion

22 / 64

Page 25: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Puzzle: Heap exhaustion

22 / 64

Page 26: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Puzzle: Heap exhaustion

Can’t allocate heap memory =⇒ nothing worksAll kernels face this problem

22 / 64

Page 27: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule

23 / 64

Page 28: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule

23 / 64

Page 29: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule

23 / 64

Page 30: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule

23 / 64

Page 31: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right

• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule

23 / 64

Page 32: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

How to recover?

Strawman 0: panic (xv6)

Strawman 1: Wait for memory in allocator?

• May deadlock!

Strawman 2: Check/handle allocation failure, like C kernels?

• Difficult to get right• Can’t – Go implicitly allocates• Doesn’t expose failed allocations

Both cause problems for Linux; see “too small to fail” rule23 / 64

Page 33: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

24 / 64

Page 34: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

24 / 64

Page 35: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

24 / 64

Page 36: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

24 / 64

Page 37: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

24 / 64

Page 38: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT solution: reserve memory

To execute system call...

No checks, no error handling code, no deadlock

24 / 64

Page 39: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Heap reservation bounds

How to compute max memory for each system call?

Smaller heap bounds =⇒ more concurrent system calls

25 / 64

Page 40: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Heap bounds via static analysis

HLL easy to analyze

Tool computes reservation via escape analysis

Using Go’s static analysis packages

Annotations for difficult cases

≈ three days of expert effort to apply tool

26 / 64

Page 41: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT implementation

Building BISCUIT was similar to other kernels

BISCUIT adopted many Linux optimizations:

• large pages for kernel text

• per-CPU NIC transmit queues

• RCU-like directory cache

• execute FS ops concurrently with commit

• pad structs to remove false sharing

Good OS performance more about optimizations, less about HLL

27 / 64

Page 42: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

BISCUIT implementation

Building BISCUIT was similar to other kernels

BISCUIT adopted many Linux optimizations:

• large pages for kernel text

• per-CPU NIC transmit queues

• RCU-like directory cache

• execute FS ops concurrently with commit

• pad structs to remove false sharing

Good OS performance more about optimizations, less about HLL

27 / 64

Page 43: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Evaluation

Part 1: HLL benefits

Part 2: HLL performance costs

28 / 64

Page 44: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Evaluation: HLL benefits

Should we use high-level languages to build OS kernels?

1 Does BISCUIT use HLL features?

2 Does HLL simplify BISCUIT code?

3 Would HLL prevent kernel exploits?

29 / 64

Page 45: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Does BISCUIT use HLL features?

Counted HLL feature use in BISCUIT and two huge Go projects

(Moby and Golang, >1M LOC)

30 / 64

Page 46: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: BISCUIT uses HLL features

0 2 4 6 8

10 12 14 16 18

Allocations

Maps

Slices

Channel

String

Multi-return

Closure

Finalizer

Defer

Go stmt

Interface

Type asserts

Imports

Count/

1K

lin

es

BiscuitGolang

Moby

Biscuit uses most HLL features similarly

31 / 64

Page 47: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: BISCUIT uses HLL features

0 2 4 6 8

10 12 14 16 18

Allocations

Maps

Slices

Channel

String

Multi-return

Closure

Finalizer

Defer

Go stmt

Interface

Type asserts

Imports

Count/

1K

lin

es

BiscuitGolang

Moby

Biscuit uses most HLL features similarly 31 / 64

Page 48: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Does HLL simplify BISCUIT code?

Qualitatively, my favorite features:

• GC’ed allocation

• slices

• defer

• multi-valued return

• strings

• closures

• maps

Net effect: simpler code32 / 64

Page 49: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Simpler concurrency

Simpler data sharing between threads

In HLL, GC frees memory

In C, programmer must free memory

33 / 64

Page 50: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Simpler concurrency example

buf := new(object_t)

// Initialize buf...

go func() {

process1(buf)

}()

process2(buf)

// When should C code free(buf)?

34 / 64

Page 51: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Simpler read-lock-free concurrency

Locks and reference counts expensive in hot paths

Good for performance to avoid them

Challenge in C: when is object free?

35 / 64

Page 52: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Read-lock-free example

var Head *Node

func get() *Node {return atomic_load(&Head)

}

func pop() {Lock()

v := Head

if v != nil {atomic_store(&Head, v.next)

}

Unlock()

}

// When should C code free(v)?

36 / 64

Page 53: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Simpler read-lock-free concurrency

Linux safely frees via RCU (McKenney’98)

Defers free until all CPUs context switch

Programmer must follow RCU rules:

• Prologue and epilogue surrounding accesses

• No sleeping or scheduling

Error prone in more complex situations

GC makes these challenges disappear

HLL significantly simplifies read-lock-free code

37 / 64

Page 54: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Simpler read-lock-free concurrency

Linux safely frees via RCU (McKenney’98)

Defers free until all CPUs context switch

Programmer must follow RCU rules:

• Prologue and epilogue surrounding accesses

• No sleeping or scheduling

Error prone in more complex situations

GC makes these challenges disappear

HLL significantly simplifies read-lock-free code

37 / 64

Page 55: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: Would HLL prevent kernel exploits?

Inspected fixes for all publicly-available execute code CVEs in Linux kernel for 2017

Classify based on outcome of bug in BISCUIT

38 / 64

Page 56: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: HLL prevents kernel exploits

Category # Outcome in Go

— 11 unknownlogic 14 sameuse-after-free/double-free 8 disappear due to GCout-of-bounds 32 panic or disappear

panic likely better than malicious code execution

HLL would prevent kernel exploits

39 / 64

Page 57: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: HLL prevents kernel exploits

Category # Outcome in Go

— 11 unknownlogic 14 sameuse-after-free/double-free 8 disappear due to GCout-of-bounds 32 panic or disappear

panic likely better than malicious code execution

HLL would prevent kernel exploits39 / 64

Page 58: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Evaluation: HLL performance

Should we use high-level languages to build OS kernels?

1 Is BISCUIT’s performance roughly similar to Linux?

2 What is the breakdown of HLL tax?

3 How much might GC cost?

4 What are the GC pauses?

5 What is the performance cost of Go compared to C?

6 Does BISCUIT’s performance scale with cores?

40 / 64

Page 59: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Experimental setup

Hardware:

• 4 core 2.8Ghz Xeon-X3460

• 16 GB RAM

• Hyperthreads disabled

Eval applications:

• NGINX (1.11.5) – webserver

• Redis (3.0.5) – key/value store

• CMailbench – mail-server benchmark

41 / 64

Page 60: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Applications are kernel intensive

No idle time; 79%-92% kernel time

In-memory FS

Ran for a minute

512MB heap RAM for BISCUIT

42 / 64

Page 61: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Is BISCUIT’s perf roughly similar to Linux?

i.e. is BISCUIT’s performace similar to production-grade kernel?

Compare app throughput on BISCUIT and Linux

43 / 64

Page 62: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Linux setup

Debian 9.4, Linux 4.9.82

Disabled features that slowed Linux down on our apps:

• page-table isolation

• retpoline

• kernel address space layout randomization

• transparent huge-pages

• ...

44 / 64

Page 63: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Is BISCUIT’s perf roughly similar to Linux?

BISCUIT ops/s Linux ops/s Ratio

45 / 64

Page 64: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Is BISCUIT’s perf roughly similar to Linux?

BISCUIT ops/s Linux ops/s Ratio

CMailbench (mem) 15,862 17,034 1.07NGINX 88,592 94,492 1.07Redis 711,792 775,317 1.09

45 / 64

Page 65: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Is BISCUIT’s perf roughly similar to Linux?

BISCUIT ops/s Linux ops/s Ratio

CMailbench (mem) 15,862 17,034 1.??NGINX 88,592 94,492 1.??Redis 711,792 775,317 1.??

45 / 64

Page 66: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

1: Is BISCUIT’s perf roughly similar to Linux?

BISCUIT ops/s Linux ops/s Ratio

CMailbench (mem) 15,862 17,034 1.??NGINX 88,592 94,492 1.??Redis 711,792 775,317 1.??

Linux has more features: NUMA, scales to many cores, ...

Not apples-to-apples, but BISCUIT perf roughly similar

45 / 64

Page 67: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: What is the breakdown of HLL tax?

Record CPU time profile of our apps

Categorize samples into HLL cost buckets

46 / 64

Page 68: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

Benchmarks allocate kernel heap rapidly

but have few long-lived kernel heap objects

47 / 64

Page 69: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

48 / 64

Page 70: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

48 / 64

Page 71: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

48 / 64

Page 72: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

48 / 64

Page 73: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

2: Prologue cycles are most expensive

GC GCs Prologue Write barrier Safetycycles cycles cycles cycles

CMailbench 3% 42 6% < 1% 3%NGINX 2% 32 6% < 1% 2%Redis 1% 30 4% < 1% 2%

Benchmarks allocate kernel heap rapidlybut have few long-lived kernel heap objects

48 / 64

Page 74: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

GC cost varies by program

More live data =⇒ more cycles per GC

Less free heap RAM =⇒ GC more frequent

Total GC cost ∝ ratio of live data to free heap RAM

49 / 64

Page 75: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

GC cost varies by program

More live data =⇒ more cycles per GC

Less free heap RAM =⇒ GC more frequent

Total GC cost ∝ ratio of live data to free heap RAM

49 / 64

Page 76: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Created two million vnodes of live data

Varied free heap RAM

Ran CMailbench, measured GC cost

50 / 64

Page 77: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Live Free Ratio Tput GC%(MB) (MB)

51 / 64

Page 78: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Live Free Ratio Tput GC%(MB) (MB)

640 320 2 10,448 34%

51 / 64

Page 79: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Live Free Ratio Tput GC%(MB) (MB)

640 320 2 10,448 34%640 640 1 12,848 19%

51 / 64

Page 80: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Live Free Ratio Tput GC%(MB) (MB)

640 320 2 10,448 34%640 640 1 12,848 19%640 1280 0.5 14,430 9%

51 / 64

Page 81: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: How much might GC cost?

Live Free Ratio Tput GC%(MB) (MB)

640 320 2 10,448 34%640 640 1 12,848 19%640 1280 0.5 14,430 9%

⇒ Need 3× heap RAM to keep GC < 10%

51 / 64

Page 82: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

3: GC memory cost in practice?

Few programs allocate millions of resources

MIT’s big time-sharing machines:

80 users, 800 tasks, 9-16GB RSS, <2GB kernel heap

(Exception: cached files, maybe evictable)

Memory cost acceptable in common situations?

52 / 64

Page 83: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

GC pauses

GC must eventually execute

Could delay latency-sensitive work

Some GCs cause one large pause, but not Go’s

• Go’s GC is interleaved with execution (Baker’78, McCloskey’08)

• Causes many small delays

53 / 64

Page 84: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

4: What are the GC pauses?

Measured duration of each GC pause during NGINX

Multiple pauses occur during a single request

Sum pause durations over each request

54 / 64

Page 85: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

4: What are the GC pauses?

Max single pause: 115 µs

(marking large part of TCP connection table)

Max total pauses during request: 582 µs

Less than 0.3% of requests paused > 100µs

55 / 64

Page 86: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

4: GC pauses OK?

Some programs can’t tolerate rare 582 µs pauses

But many probably can

99%-ile latency in service of Google’s “Tail at Scale” was 10ms

56 / 64

Page 87: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

5: What is the cost of Go compared to C?

Compared OS code paths with identical functionality

Chose paths that are:

• core OS paths

• small enough to make them have same functionality

Two code paths in OSDI’18 paper

• pipe ping-pong (systems calls, context switching)

• page-fault handler (exceptions, VM)

57 / 64

Page 88: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

5: What is the cost of Go compared to C?

Pipe ping-pong code path:

• LOC: 1.2k Go, 1.8k C

• No allocation; no GC

• Top-10 most expensive instructions match

58 / 64

Page 89: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

5: C is 15% faster

Pipe ping-pong:

C Go(ops/s) (ops/s) Ratio

536,193 465,811 1.15

Prologue/safety-checks⇒ 16% more instructions

Go slower, but competitive

59 / 64

Page 90: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

6: Does BISCUIT scale?

Can BISCUIT efficiently use many cores?

Is Go scalability bottleneck?

60 / 64

Page 91: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

6: Does BISCUIT scale?

Ran CMailbench, varied cores from 1 to 20

Measured throughput

61 / 64

Page 92: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

6: BISCUIT scales well to 10 cores

0

10

20

30

40

50

60

70

80

0 5 10 15 20

Thro

ughput

(k/s

)

Cores

PerfectBiscuit

Lock contention in CMailbench at 20 cores, not NUMA-aware62 / 64

Page 93: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

Should one use HLL for a new kernel?

The HLL worked well for kernel development

Performance is paramount⇒ use C (up to 15%)

Minimize memory use⇒ use C (↓ mem. budget, ↑ GC cost)

Safety is paramount⇒ use HLL (40 CVEs stopped)

Performance merely important⇒ use HLL (pay 15%, memory)

63 / 64

Page 94: The benefits and costs of writing a UNIX kernel in a …The benefits and costs of writing a UNIX kernel in a high-level language Cody Cutler, M. Frans Kaashoek, Robert T. Morris MIT

6.S081/6.828 and HLL

Should we use HLL in 6.828?

git clone https://github.com/mit-pdos/biscuit.git

64 / 64