+ All Categories
Transcript
Page 1: Java IPC and the CLIP library

Java IPCAnd the CLIP Library

Clark N. HobbieLong Term Software, LLC

[email protected]

Page 2: Java IPC and the CLIP library

Who Gives a Damn?

Page 3: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 3

You Should Care Because…• If you are in a corner• Java sync is one VM only• Others require JNI• Platform differences

Page 4: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 4

What Are the Options?

• Sockets• Message Queues• Semaphores• Shared Memory

Page 5: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 5

Which Option Should I Use?

Shared Memory• JRE support• Highest bandwidth• Decent synchronization• Naming support

Page 6: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 6

Why Should I Care about CLIP?

• New primitives– Semaphores– Message Queues

• Simplifies existing primitives– Shared Memory

Page 7: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 7

Java

RandomAccessFile raf = new RandomAccessFile("/temp/smfile", "rw");

FileChannel chan = raf.getChannel();

MappedByteBuffer buf = chan.map(MapMode.READ_WRITE, 0, size);

byte[] other = new byte[1024];

buf.position(0);

buf.get(other, 0, 1024);

Page 8: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 8

CLIP

SharedMemory smem =

new SharedMemory("/temp/smfile");

Page 9: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 9

What is IPC?

Inter-Process Communication (IPC)• Multiple processes• Naming• Synchronization• Bandwidth

Page 10: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 10

Naming

Me Tarzan,who you?

Page 11: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 11

Naming

How do you…• Connect processes together?• Determine who is allowed to connect?• Examples

– TCP/IP?– Email?– Telephones?

Page 12: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 12

File Naming

Many IPC methods use the file systembecause• Visible to all processes• Has access control built in

Page 13: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 13

File Naming Examples

• Memory Mapped Files• Named Pipes/FIFOs

Page 14: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 14

Synchronization

• Event ordering• Mutual exclusion

Page 15: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 15

Example: Online Purchase

1. Get user credit information2. Decision purchase3. Update credit information

Page 16: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 16

Without Event Ordering

Client A1 Get credit info

2 Decision purchase3 Update info

Client B

2 Get credit info

4 Decision purchase5 Update info

Page 17: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 17

With Event Ordering

Client A1 Get credit info

2 Decision purchase3 Update info

Client B

2 Wait for A

4 Get credit info5 Decision purchase6 Update info

Page 18: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 18

Synchronization is Event Ordering

Instead of this order:• A gets credit info• B gets credit info• A decisions purchase• A updates info• B decisions purchase• B updates credit info

We want this order:• A gets credit info• A decisions purchase• A updates info• B gets credit info• B decisions purchase• B updates credit info

Page 19: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 19

Which Type of IPC is Appropriate?

Page 20: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 20

IPC Types

• Shared Memory • Semaphores• Sockets • Message Queues

Page 21: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 21

Shared Memory

Page 22: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 22

Shared Memory

• Preferred approach• Any to any• get/put semantics• Synchronization support• JRE support• Naming support

Page 23: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 23

Shared Memory Details

• Synchronization– File Locking– Lock/unlock ~ 25 usec

• Naming– File naming

• Bandwidth– Synchronized 250MB/sec– Unsynchronized 1000MB/sec

Page 24: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 24

Memory Mapped Files

• Start with a file– Appears the same to everyone– Reads/writes appear to everyone

• Now speed it up– Until its as fast as memory– Like having the OS buffer

Page 25: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 25

Why Dear God, Why?!!

• Originally Unix– Where everything is a file– mmap system call– CreateFileMapping system call

• Solves the naming problem• Solves the access problem

Page 26: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 26

Example: World of Warcraft!

Shared Memory!

Page 27: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 27

WoW: Requirements

• One client process per player• One server process for all• Server periodically reads all orders• Server issues results

Page 28: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 28

WoW: Design

Client(Martha)

Client(Lancelot)

Client(EvilOne)

Client(Conan)

Orders

Player Order

EvilOne killLancelot

ConanMartha Stewart

killkill

make cookies…

Page 29: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 29

WoW: Design

Results

Player Result

EvilOne deadLancelot

ConanMartha Stewart

deaddead

overweight…

Orders

Player Order

EvilOne killLancelot

ConanMartha Stewart

killkill

make cookies…

Server Process

Page 30: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 30

Shared Memory: Summary

• Preferred IPC• 250 to 1000MB/sec• File naming• Synchronization through file locking• JRE support

Page 31: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 31

Sockets

Page 32: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 32

Sockets

• TCP/IP• Point to point• Stream oriented• Client/Server• Synchronized• Java support• Naming support

Page 33: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 33

Sockets Details

• Naming– 127.0.0.1 port 7777

• Synchronization– Accept, read, write– 70 usec

• Bandwidth– 15 MB/sec

Page 34: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 34

Sockets: CLIP

• Some utility classes– ThreadedSocketServer

• JRE already has excellent support

Page 35: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 35

Example: Google Maps

Copyright © Google

Page 36: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 36

GMapsCombining Data

Image Streets

Overlay

Images are Copyright © www.Google.com

Page 37: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 37

GMaps: Requirements

• C legacy code• One instance/process• Receive file name• Process for 1 to 10 sec• Respond with new file name

Page 38: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 38

GMaps: Design

Server Process

Java VM

ClientClientClientClientClientWorkerProcess

ClientClientClientClientClientClientThread

TCP/IP

Worker Pool

Page 39: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 39

Sockets vs Shared Memory

• Faster synchronization– 25 usec vs. 70 usec

• More bandwidth– 250 MB/sec vs. 15 MB/sec

Page 40: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 40

Message Queues

Page 41: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 41

Message Queues

• Point to point• Message or stream• Client/Server• Synchronization support• No Java Support• No naming support

Page 42: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 42

Message Queue Details

• Synchronization– Accept, read, write– 25 usec

• Bandwidth– 167 MB/sec

Page 43: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 43

Platform Differences

• Direction– Linux is one-way– Windows is two-way

• Naming– Linux: any– Windows: must be \\.\pipe\name

• Misc– Windows pipes are networkable

Page 44: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 44

Message Queues: CLIP

• MessageQueue class• One direction• File naming• JNI under the hood

Page 45: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 45

Message Queues vs. Shared Memory

• Less bandwidth– Synchronized: 250MB vs. 167MB– Unsynchronized: 1000MB vs. 167MB

• No Java support• Platform differences

Page 46: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 46

Semaphores

Page 47: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 47

Semaphores

• Synchronization only• Any to any• Access via increment/decrement• No Java support• No naming support

Page 48: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 48

Semaphore Details

• Integer value• Decrement reserves

– Blocks if the value is 0 or less• Increment releases

– May wake a blocked process• N-ary semaphores

– Values other than 0 or 1

Page 49: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 49

Semaphore Details

• Synchronization– 25 usec

• Platform naming differences– Linux: /somename– Windows: somename– Ad hoc access control

Page 50: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 50

Semaphores CLIP

• Semaphore class• File system naming• JNI under the hood

Page 51: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 51

Example: The Liminator

Page 52: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 52

The Liminator: Requirements

• Start with Google Maps• Too many processes == poor performance• Limit processes with a semaphore

– Initial value = max number of processes– Reserve when trying to spawn– Release when complete

Page 53: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 53

Liminator: Design

Running

ClientClientClientClientClientClient

Waiting

ClientClientClientClientClientClient

Semaphore

Page 54: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 54

Semaphores vs Shared Memory

• About the same speed• No naming support• No JRE support

Page 55: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 55

Summary

Page 56: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 56

Summary

• IPC– Multiple processes– Naming– Bandwidth

• CLIP– Open source Linux & Windows– New primitives via JNI– Simplify others

Page 57: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 57

Summary of IPC Types

NoN/A25 usecSemaphores

Yes250MB25 usecShared Memory

No167MB25 usecMessage Queues

15MB

Band

70 usec

Sync Java Support?

IPC Type

YesSockets

Page 58: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 58

Resources

codeproject.comCode Projectjava.sun.comSun Forums

JTUXbasepath.com/aup/jtux

Java

Advanced UNIX Programmingbasepath.com/aup/index.htm

Linuxmsdn.microsoft.comWindowsltsllc.com/talks/ipcSlides & code

Page 59: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 59

The End

Page 60: Java IPC and the CLIP library

3/3/2009 http://ltsllc.com/slides/ipc.html 60

Which Option is Best?

Sync?

Data?

Semaphores

Yes

No

> 10MB/s?Yes

Sockets

No

SharedMemory

MessageQueues

Yes

No

1-to-1?

SharedMemory*

No

Yes


Top Related