0

I need to send/fan-out the same message to a list of hosts through tcpip sockets efficiently. The logic would require looping through each socket to write the message out. If this logic runs in the linux user mode, the underlying OS will call the system call as many times as the number of hosts. This is expensive since each system call requires user-mode to kernel-mode context switch.

It would be more efficient if the loop can be moved in a system call that takes a list of hosts (file descriptors). Does such system call exist in linux?

If such system call exists, do we have an equivalent API in Java (similar to transferTo() in Java corresponds to sendfile() in linux)?

Thang
  • 31
  • 5
  • Can you tell us more about your use case? How many hosts? How large are the messages? How frequent are the messages? – David Schwartz Aug 28 '15 at 18:19
  • Any use cases can be applied with the number of hosts from 2 to 100. The size message is relatively small less than 1KB. – Thang Aug 30 '15 at 02:34
  • Repeated question, look at http://stackoverflow.com/questions/32176028/single-buffer-multiple-sockets-single-syscall-under-linux/32199626#32199626 – Luis Colorado Aug 31 '15 at 08:23
  • @LuisColorado: Thanks! Since there is no direct syscall from linux kernel, I can think of two alternative approaches to mitigate the bottleneck: tcp kernel bypass (unlikely feasible in my case), interrupt coalescing (http://stackoverflow.com/questions/28090086/what-are-the-advantages-napi-before-the-irq-coalesce). Not sure if Java has explored the latter. – Thang Aug 31 '15 at 14:54
  • @Thang, perhaps if you have a bottleneck, you had better to redesign your protocol with something broadcast (or multicast) based... This way you only send once, and the network does the work of distributing data. – Luis Colorado Aug 31 '15 at 20:50

1 Answers1

1

As I remember, Java NIO is supporting zero-copy. Take a look at SocketChannel and FileChannel classes.

Oleksandr Loushkin
  • 1,479
  • 12
  • 21