6

The goal of this application is to handle 800 simultaneous clients over TCP, each of them sends a 3.5kb xml every second. Each of these requests needs to be parsed (see code snipped). This happens on different threads.

The limitation of this project is that it has to run on a small Raspberry Pi3 (1.2 ghz quad core, 1gb ram). And i run into utilization issues when i increase the load above 150 simultaneous clients (80% cpu usage).

When i run this program my development machine it seems to run very well. (0-1% usage, under 150). I understand that my development machine is more powerful than the RPI, and therefore runs better. But the difference seems to be too big.

In my current setup i use Java nio to handle/read all the incoming connections. Then i use multiple threads to read the data.

Current Setup

This is the simple code that currently runs on the processing thread. Also tried reading a simple byte[] array 1 byte at a time. And even reading a StaX stream. Every variation of reading that i tried, the 'read type' of operation gives gives the worst performance.

BufferedInputStream input = new BufferedInputStream(new ByteArrayInputStream(buffer.array(), 0, bytecount));
int current;
/* In this snippet input.read() is the cause of performance issues 
Reading directly from byte[] gives similar poor performance.
*/
while ((current = input.read()) != -1) {
    continue;
}

According to my profiler the Input.read() call uses a huge amount of processing power on the Pi and accounts for 97% of the total cpu time. the other 3% is the Main thread that handles the connections.

On my development machine this is almost flipped and the Main thread accounts for most of the cpu usage, 93%. And 7% goes to the processing threads.

What could cause this big difference? why is this read() call on the pi so expensive compared to my other machine, could it have something to do with memory?

Notes:

  • Pi runs raspbian linux - openjdk 1.8.0_40-internal
  • Dev machine runs win 10 - Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
  • Tried running with -Xms -Xmx flag on both machines, same result.
14
  • 3
    The Pi has absolutely the worst IO. Your PC has a massive spinning disc, or possibly even an SSD - this is orders of magnitude faster than a crappy SD card. And that's not even starting on CPU architecture, number of cores, hyperthreading etc. Jan 31, 2017 at 11:31
  • 1
    The application doesnt do any (explicit) IO to disk, The pi can handle the sockets IO just fine (without processing), in the main thread the sockets get read and stored in bytebuffers. the processing thread should then read from RAM if i am correctly. Jan 31, 2017 at 11:53
  • 1
    Also, the RPI3's hardware is ARMv8 (with a Cortext-A53), but the raspian image is legacy ARM-32 armhf (its not even Aarch32). Raspian is not using the device to its potential. You might consider using an Aarch64 image provided by openSUSE, CentOS or one of the other distros.
    – jww
    Jan 31, 2017 at 11:56
  • 1
    It is a bit of an odd construction with lots of overhead, if I read this correctly. I wonder what the purpose is of a BufferedInputStream when you're already tanking everything into memory inside a ByteArrayInputStream... only to then take it out 1 byte at a time again.
    – Gimby
    Jan 31, 2017 at 13:29
  • 2
    @Georggroenendaal - I recently got bit by differences between the Corext-A53 and Cortex-A57. Both are ARMv8, and the A57 is the higher-end model. But my BLAKE2b implementation for Crypto++ ran slower on the A57. It was even slower than a C++ implementation. It turned out NEON/ASIMD shifts were more expensive: they had a 7 cycle latency and could only be run on one of the pipes. A non-NEON implementation using integer ops was faster. The Optimization Guide gave us the details and explained it.
    – jww
    Jan 31, 2017 at 16:47

1 Answer 1

0

Turns out that the problem was a combination of both JVM and 32bits OS on the Raspberry Pi 3. When running 32bit raspbian with OpenJDK my application had very poor performance (Especially on the 'read' calls). Switching to the Oracle JVM gave me the 'better' expected performance.

However when switching to a 64bit OS (OpensSuse in my case) the performance was good with both the OpenJDK and Oracle JVM.

(Credits to @jww in the comments, for suggesting to switch to a 64 bit OS)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.