简体   繁体   English

Java TCP / IP套接字写入性能优化

[英]Java TCP/IP Socket write performance optimization

Server Environment 服务器环境

Linux/RedHat
6 cores
Java 7/8

About application : 关于申请:

We are working on developing a low latency (7-8 ms) high speed trading platform using Java. 我们正在使用Java开发低延迟(7-8 ms)的高速交易平台。 Multi-leg orders are sent after algo conditions are met 满足算法条件后发送多腿订单

Problem 问题

The orders to the exchange using TCP/IP java.net.Socket APIs (using java.io.OutputStream.write(bytes[] arg0) ). 使用TCP / IP java.net.Socket API(使用java.io.OutputStream.write(bytes[] arg0) )进行交换的命令。 Profiler measurement is records as 5-7 microsec which is very high as per our low latency reqs. Profiler的测量记录为5-7微秒,根据我们的低延迟要求非常高。 We are not made use of setPerformancePreferences() api as suggested in one of the questions posted in stacktrace. 堆栈跟踪中发布的问题之一未建议我们使用setPerformancePreferences()API。

Question

  1. Any alternatives to java.net.Socket to reduce the socket transmission time? 是否可以使用java.net.Socket的替代方法来减少套接字传输时间?
  2. Any optimization techniques to improve performance 任何提高性能的优化技术
  3. Is setPerformancePreferences() is of any use? setPerformancePreferences()有什么用吗?

We are not made use of setPerformancePreferences() api 我们没有使用setPerformancePreferences()API

It doesn't do anything and never has. 它什么也不做,也永远做不到。 I wouldn't worry about it. 我不会担心。

Any alternatives to java.net.Socket to reduce the socket transmission time? 是否可以使用java.net.Socket的替代方法来减少套接字传输时间?

The problem is most certainly not a software one. 问题肯定不是软件问题。 You can get <8 micro-seconds from Java to Java on different machines, but you need low latency network cards like Solarflare or Mellanox. 在不同的机器上,从Java到Java的时间可能少于8微秒,但是您需要低延迟的网卡,例如Solarflare或Mellanox。

If you want fast processing you should consider either a high GHz haswell processor, possibly over clocked to 4.2 or 4.5 GHz or a dual socket Haswell Xeon. 如果要进行快速处理,则应考虑使用GHz高的haswell处理器(可能时钟频率超过4.2或4.5 GHz)或双插槽Haswell Xeon。 The cost of these compared to the cost of trading is not high. 这些成本相比交易成本并不高。

Any optimization techniques to improve performance 任何提高性能的优化技术

Using non-blocking NIO ie ByteBuffers and busy waiting on the socket connections. 使用非阻塞NIO(即ByteBuffers)并忙于套接字连接。 (I wouldn't use Selectors as they add quite a bit of overhead) I would turn off nagle. (我不会使用选择器,因为它们会增加很多开销)我会关闭nagle。

For some micro-tuning, use an affinity bound thread on an isolated cpu. 对于某些微调,请在隔离的cpu上使用关联绑定线程。

Is setPerformancePreferences() is of any use? setPerformancePreferences()有什么用吗?

Looking at the source .. I will let you be the judge. 从源头上看..我会让你当法官。

public void setPerformancePreferences(int connectionTime,
                                      int latency,
                                      int bandwidth)
{
    /* Not implemented yet */
}

Java 7/8 Java 7/8

In term of which version to use, I would start with Java 8 as it has much improved escape analysis which can reduce garbage of short lived objects and thus help reduce latency between GCs and jitter from GCs. 在使用哪个版本方面,我将从Java 8开始,因为Java 8改进了转义分析,可以减少短期对象的垃圾,从而帮助减少GC之间的等待时间和GC的抖动。

A couple of things come to mind: 我想到了几件事:

JNI : JNI lets you write C code that is ran from your Java code. JNI :JNI允许您编写从Java代码运行的C代码。 Critical parts of your Java code that are running to slow can be migrated to C/C++ for improved performance. 可以将运行缓慢的Java代码的关键部分迁移到C / C ++,以提高性能。 Work would be needed to first identify what those critical points are and if its worth the effort to move it to C/C++. 首先需要确定这些关键点是什么,以及是否有必要努力将其转移到C / C ++。

Java Unsafe : Wanna get dangerous? Java不安全 :想变得危险吗? Use Java Unsafe to bypass that pesky GC. 使用Java Unsafe绕过该讨厌的GC。 Here is more info on it. 是更多信息。 On Github you may find some cool wrapper code to more-safely use Java Unsafe. 在Github上,您可能会发现一些很酷的包装器代码,可以更安全地使用Java Unsafe。 Here is one. 这是一个。 More info. 更多信息。

LMAX Disruptor : Read more about it here . LMAX Disruptor在此处了解更多信息。 This company is also building a fast trading system in Java. 该公司还在用Java构建快速交易系统。 Disruptor allows for faster inter-thread communication. Disruptor允许更快的线程间通信。

Bytecode scrutinization: Review your code by looking at the byte code. 字节码检查:通过查看字节码来检查代码。 I have done this for a video game I made and was able to streamline the code. 我已经为制作的视频游戏完成了此任务,并且能够简化代码。 You'll need a good tool for turning your class files into readable bytecode. 您需要一个很好的工具将您的类文件转换为可读的字节码。 THis might be the tool i used. 这可能是我使用的工具。

Improved garbage collection: Have you tried using the G1 garbage collector ? 改进的垃圾收集:您是否尝试过使用G1垃圾收集器 Or messing around with the older GC's ? 还是搞混了旧版GC

Highscalability : This site is full of good info on making code fast. 高度可扩展性 :该站点上有很多有关使代码快速运行的良好信息。 Here is an example that might help. 这是一个可能有帮助的示例。

New API I dont know exactly how to use New API, but it has come up in articles I have read. 新API我不确切知道如何使用新API,但是我已阅读的文章中已经提到了它。 Here is another article on it. 这是另一篇文章。 You might need to use it via JNI. 您可能需要通过JNI使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM