2. MeMe
Software Engineer at
AP / Switch / IoT / System
Management and Controller / SaaS
If you are .
Software Development / Performance
Engineering
Ruckus Wireless - Part of Arris Group
interested
Github
Dockerhub
Slideshare
1 . 2
3. DisclaimerDisclaimer
Introductory level presentation
A bit material about Linux operating system
Mostly based on OpenJdk 1.8u
NOT going to cover performance measurement tools
Benchmark for reference ONLY. Source code available
on .
Correlated to settings from OS / hardware / cloud
service provider.
Recommended to write your own benchmark on
your test bed.
NOT an expert on many of the issues / areas.
github
1 . 3
7. Decorator PatternDecorator Pattern
Combination of BehaviorsCombination of Behaviors
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream("example.txt")));
int data = -1;
while ((data = reader.read()) != -1) {
//swallow
}
[1] Decorator Pattern
2 . 3
8. Decorator PatternDecorator Pattern
Combination of BehaviorsCombination of Behaviors
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream("example.txt")));
int data = -1;
while ((data = reader.read()) != -1) {
//swallow
}
read()
FileInputStream
raw byte data
[1] Decorator Pattern
2 . 3
9. read()
InputStreamReader
Decorator PatternDecorator Pattern
Combination of BehaviorsCombination of Behaviors
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream("example.txt")));
int data = -1;
while ((data = reader.read()) != -1) {
//swallow
}
read()
FileInputStream
raw byte data
unicoded byte data
[1] Decorator Pattern
2 . 3
10. read()
BufferedReader
read()
InputStreamReader
Decorator PatternDecorator Pattern
Combination of BehaviorsCombination of Behaviors
Reader reader = new BufferedReader(new InputStreamReader(new FileInputStream("example.txt")));
int data = -1;
while ((data = reader.read()) != -1) {
//swallow
}
read()
FileInputStream
raw byte data
unicoded byte data
buffered byte data
[1] Decorator Pattern
2 . 3
12. Short History of NIOShort History of NIO
Introduced with JDK 1.4 to complement an existing
standard I/O.
Originally stands for Non-Blocking IO
Implementation
Support Channel / ByteBuffer
Support FileChannel / SocketChannel
Support Selector I/O Model
Continuous development was added to JDK 7 as
Including a series of new file system API as - aka
NIO.2
Implementation
Support Asynchronous I/O Model
2002
JSR-51
JSR-202
2 . 5
13. Java NIO In a Nut ShellJava NIO In a Nut Shell
Channel and Buffer
has more flexibility
Non Blocking Paradigm
*
Common in Network I/O
Selector Model (Network I/O)
ByteBuffer
AsynchrnousFileChannel
2 . 6
25. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss
Populate Page
26. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss Cache Hit
Populate Page
27. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss
Speed up READ performance
Cache Hit
Populate Page
28. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss
Speed up READ performance
Cache Hit
Populate Page
29. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss
Speed up READ performance
Cache Hit
Populate Page
30. I/O Buffering EffectI/O Buffering Effect
Main
Memory
Memory Buffer
Disk
3 . 3
Cache Miss
Speed up READ performance
Speed up WRITE performance
Cache Hit
Populate Page
31. I/O Data PathI/O Data Path
I/O Data path is consisted of layers of buffers for
read and write.
Systems performance is the study of the entire
system, including all physical com-
ponents and the full software stack. [1]
pages from disk to memory -
()
Not in Cache: ()
In Cache: and exit
pages to disk - ()
FileSystem specific implementation
Read
do_generic_file_read
page_cache_sync_readahead
Copy page
Write generic_perform_write
[1]Systems Performance - Enterprise and the Cloud ( 2014)
3 . 4
34. mmapmmap
Read: An occurred page fault
will bring in the file data from
disk.
The affected page is marked
as dirty and will be eventually
flushed to file on disk.
Pros:
Avoid extra data copy
Minimize kernel/user
context switch
Cons:
Page faults may bring
to
performance
negative impact
3 . 7
37. sendfile()sendfile()
For kernel v2.4+ supporting
Scatter/Gather operations
file descriptor and meta data are
passed to socket buffer.
The DMA engine passes data
directly from the kernel buffer to
the protocol engine,
First appeared in kernel v2.2
file data to be copied into kernel
buffer directly by the DMA engine.
3 . 8
38. sendfile()sendfile()
For kernel v2.4+ supporting
Scatter/Gather operations
file descriptor and meta data are
passed to socket buffer.
The DMA engine passes data
directly from the kernel buffer to
the protocol engine,
First appeared in kernel v2.2
file data to be copied into kernel
buffer directly by the DMA engine.
Zero Copy
3 . 8
44. Data PersistenceData Persistence
Page CachePage Cache
As of Linux kernel 2.4, there is only one Page Cache
Write Path
Data is firstly written in page cache, called Dirty Pages:
When free memory shrinks below threshold - Flusher Threads
When dirty data grows older than specific threshold - Flusher
Threads
When user invokes sync() / fsync() on demand
Read Path
a read() would check page cache in memory before accessing
the disk.
[1]
4 . 6
50. Java Microbenchmark HarnessJava Microbenchmark Harness
is a Java harness for building, running, and
analysing nano/micro/milli/macro benchmarks
written in Java and other languages targetting the
JVM.
Common Microbenchmark Pitfall
Manage JVM optimizations
Warmup Stage
Dead Code Elimination
supported
JMH
Jenkins style report
5 . 4
70. Apache KafkaApache Kafka
Consumer Producer
Topic
Each partition is a
Every message is appended at the end of each partition.
Thus, each consumer remembers an offset of which
partition it has recently consumed for each topic.
Each message is transmitted via to transmit the
partition file with offset + length.
memory mapped file
sendfile()
9 . 2
71. ApacheApache CassandraCassandra
C* is known to provide high write throughput not only because
of its overall architecture, but also it fundamentally utilize
mmap() to appends data to , or from the
immutable SSTable file on disk
Commit Log read data
9 . 3
74. ReferenceReference
Source Code Repository
Books
Advanced Programming in the UNIX Environment -
3rd Edition (2013)
Linux System Programming - 2nd Edition (2013)
Linux Kernel Development - 3rd Edition (2011)
Java Performance - The Definitive Guide (2014)
Systems Performance - Enterprise and the Cloud (
2014)
OpenJdk 8u
Linux Source v2.6.39
10 . 2
77. SwapSwap
The kswapd daemon (v2.x+) is also known as the pageout daemon. It
supports the virtual memory subsystem by writing dirty pages to disk
slowly over time, so the pages can be reclaimed.
Disabling Swap Space
Suggested for many server application, i.e. ,
does not prevent disk I/O from becoming a problem under memory
contention, it simply shifts the disk I/O thrashing from
(MAP_PRIVATE) to (MAP_SHARED).
does not prevent pathological behaviour at near-OOM, although
having swap space may prolong it. Whether the system OOM killer
is invoked with or without swap, the result is the same: you are left
with a system in an unpredictable state.
ElasticSearch Cassandra
anonymous
pages file pages
11 . 3
78. Direct IODirect IO
Currently NOT supported, but possibly
available in
JNA/JNI compliant
Pros
IO throughput is more predictable:
bypass the influence of page cache.
Cons
Slow down reads, since bypassing
page cache completely.
Check
A lot of custom solution
JDK 10
C* ticket
Ignite-direct-io
jaydio
Write Through
11 . 4
79. PageCachePageCache
Page Writeback SettingsPage Writeback Settings
dirty_background_ratio As a percentage of total memory,
the number of pages at which
the flusher threads begin
writeback of dirty data.
dirty_expire_interval In milliseconds, how old data must
be to be written out the next time a
flusher thread wakes to perform
periodic writeback.
dirty_ratio As a percentage of total memory,
the number of pages a process
generates before it begins
writeback of dirty data.
dirty_writeback_interval In milliseconds, how often a flusher
thread should wake up to write
data back out to disk. 11 . 5
80. Performance MeasurementPerformance Measurement
Latency vs Throughput vs BandwidthLatency vs Throughput vs Bandwidth
Time (sec)
Event
Throughput is how much event per unit time travelled through the channel
Latency is how much time for an event to
complete
Bandwidth is maximum of event
traveled through the channel
11 . 6
81. Application BufferApplication Buffer
BufferedOutputStreamBufferedOutputStream
public class BufferedOutputStream extends FilterOutputStream {
/** Flush the internal buffer */
private void flushBuffer() throws IOException {
if (count > 0) {
out.write(buf, 0, count);
count = 0;
}
}
public synchronized void flush() throws IOException {
flushBuffer();
out.flush();
}
}
11 . 7