SlideShare a Scribd company logo
Zero Copy: A Green Approach
to Data Transfer
Pramod B Nagaraja
IBM India Pvt LTD
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice

Performance Comparison

Application Scenario

Limitations

Conclusion
What is Zero Copy ?
•
Data transfer from disk to user buffers without
an intermediate copy into OS Kernel buffers
•
Computer operations in which CPU does not
perform the task of copying data from one
memory area to another
• DMA
• MMU
What is Zero Copy ?
•
Fewer intermediate Data Copies, fewer
context switches
•
Linux :: sendfile, sendfile64
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice

Performance Comparison

Application Scenario

Limitations

Conclusion
Application Scenario
Data Transfer : Age-Old Practice
File.read(fd, buf, len);
Socket.send(socket, buf, len);
inputStream = new FileInputStream(fname);
output = new
DataOutputStream(socket.getOutputStream());
byte[] b = new byte[4096];
long read = 0, total = 0;
while((read = inputStream.read(b))>=0) {
total = total + read;
output.write(b);
}
Data Transfer : Age-Old Practice

4 Data Copies
Data Transfer : Age-Old Practice

4 Context Switches
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice

Performance Comparison

Application Scenario

Limitations

Conclusion
Data Transfer : Best Practice

FileChannel Approach
java.nio.channels.FileChannel.transferTo(pos, cnt, writableChannel);
SocketChannel sc = SocketChannel.open();
sc.connect(sad);
FileChannel fc = new
FileInputStream(fname).getChannel();
long nsent = 0, curnset = 0;
curnset = fc.transferTo(0, fsize, sc);
Data Transfer : Best Practice
 3 Data Copies
Data Transfer : Best Practice
 2 Context Switches
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice
•
Performance Comparison

Application Scenario

Limitations

Conclusion
Performance Comparison
• ~ 65% Performance Boost up !
0
2000
4000
6000
8000
10000
12000
14000
16000
18000
63
M B
350
M B
1
GB
Traditio
nal
FileCha
nnel
Measurements take on Linux (xi32) with 2.6 kernel
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice

Performance Comparison
•
Application Scenario
•
Limitations
•
Conclusion
Application Scenario

FTP Servers

Web Servers

Mail Servers

Any Applications serving static content
Agenda

Zero Copy

Data Transfer : Age-Old Practice

Data Transfer : Best Practice

Performance Comparison

Application Scenario
•
Limitations
•
Conclusion
Theoretically Speaking there is no difference
b/w Theory and Practice .. but in Practice
there is !!
Limitations

Suitable only for static content

Not suitable if Data to be transferred needs to
be modified/appended with/appended to, etc

FileChannels can only be used to transfer
data from File to File, File to Socket objects.
References

More details can be found @
http://www.ibm.com/developerworks/library/j-
zerocopy/index.html

http://www.linuxjournal.com/article/6345
Q&A
Thank You !!!!

More Related Content

Optimize Performance of I/O-intensive Java applications Using Zero Copy

  • 1. Zero Copy: A Green Approach to Data Transfer Pramod B Nagaraja IBM India Pvt LTD
  • 2. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice  Performance Comparison  Application Scenario  Limitations  Conclusion
  • 3. What is Zero Copy ? • Data transfer from disk to user buffers without an intermediate copy into OS Kernel buffers • Computer operations in which CPU does not perform the task of copying data from one memory area to another • DMA • MMU
  • 4. What is Zero Copy ? • Fewer intermediate Data Copies, fewer context switches • Linux :: sendfile, sendfile64
  • 5. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice  Performance Comparison  Application Scenario  Limitations  Conclusion
  • 7. Data Transfer : Age-Old Practice File.read(fd, buf, len); Socket.send(socket, buf, len); inputStream = new FileInputStream(fname); output = new DataOutputStream(socket.getOutputStream()); byte[] b = new byte[4096]; long read = 0, total = 0; while((read = inputStream.read(b))>=0) { total = total + read; output.write(b); }
  • 8. Data Transfer : Age-Old Practice  4 Data Copies
  • 9. Data Transfer : Age-Old Practice  4 Context Switches
  • 10. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice  Performance Comparison  Application Scenario  Limitations  Conclusion
  • 11. Data Transfer : Best Practice  FileChannel Approach java.nio.channels.FileChannel.transferTo(pos, cnt, writableChannel); SocketChannel sc = SocketChannel.open(); sc.connect(sad); FileChannel fc = new FileInputStream(fname).getChannel(); long nsent = 0, curnset = 0; curnset = fc.transferTo(0, fsize, sc);
  • 12. Data Transfer : Best Practice  3 Data Copies
  • 13. Data Transfer : Best Practice  2 Context Switches
  • 14. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice • Performance Comparison  Application Scenario  Limitations  Conclusion
  • 15. Performance Comparison • ~ 65% Performance Boost up ! 0 2000 4000 6000 8000 10000 12000 14000 16000 18000 63 M B 350 M B 1 GB Traditio nal FileCha nnel Measurements take on Linux (xi32) with 2.6 kernel
  • 16. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice  Performance Comparison • Application Scenario • Limitations • Conclusion
  • 17. Application Scenario  FTP Servers  Web Servers  Mail Servers  Any Applications serving static content
  • 18. Agenda  Zero Copy  Data Transfer : Age-Old Practice  Data Transfer : Best Practice  Performance Comparison  Application Scenario • Limitations • Conclusion
  • 19. Theoretically Speaking there is no difference b/w Theory and Practice .. but in Practice there is !!
  • 20. Limitations  Suitable only for static content  Not suitable if Data to be transferred needs to be modified/appended with/appended to, etc  FileChannels can only be used to transfer data from File to File, File to Socket objects.
  • 21. References  More details can be found @ http://www.ibm.com/developerworks/library/j- zerocopy/index.html  http://www.linuxjournal.com/article/6345
  • 22. Q&A