You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Performance too slow for control feedback for a moving robot #748
Review alternatives to improve performance in Sysfs.java
Evaluate the performance with JMH
Consolidate changes with current architecture
Original Request:
The java code attached runs in about 100 to 140 milliseconds per iteration. Similar code using the ev3 classroom from LEGO runs each iteration in 2 to 4 milliseconds. 100 milliseconds is enough time for the robot to travel an inch or 3, which becomes significant quickly.
My question is - is the delay in the ev3 os streaming implementation, or in ev3dev-lang-java:2.6.2-SNAPSHOT code that reads and writes those streams? That'll tell me what to try first to speed it up my own code. If it's at the OS level then probably the best I can do is read and write the buffers in parallel. If it's in Java IO then maybe a JNI approach or an NIO loop could help.
I'm using ev3dev-lang-java:2.6.2-SNAPSHOT - on a stock EV3, not overclocked, with the stock ev3dev OS SDD card. It does touch the lejos SampleProvider to use the gyro sensor, but that seems to pass right through to Sysfs.readFloat(...).
Juan Antonio Breña Moral asked me to open an issue for the ev3dev-lang-java project here.
However, I've noticed that even though the benchmark sets the speed attribute, it doesn't order the motors to start moving. Is this intentional? I've followed this in the C++ program. I believe the situation with leJOS/ev3dev-lang-java is similar and you'd have to call forward() for the speed setting to take full effect.
It seems that the compiler in Ubuntu 18.04 is too new to compile for ev3dev-stretch, I'll provide a binary built with a "universal" compiler (download here) that I was using for different C development.
EDIT: I'm going to use a arm-linux-musleabi-cross compiler from http://musl.cc/, they're available for Windows as well, so then a comparable setup should be available.
I've noticed that even though the benchmark sets the speed attribute, it doesn't order the motors to start moving. Is this intentional?
That's correct. Starting the motors isn't in the control loop, so I left it out of the benchmark. That gives me a reason better than "I don't want to take the wheels off the robot, and don't want it crashing into stuff on my desk."
this is a kind a result from JMH and indeed, I didn´t add to reduce the time invested in the tests but they works.
It is a pity that you don´t believe in JMH but this is the official Java tool for MicroBenchmarking.
Review in Internet, you will find positive results with JMH.
this is a kind a result from JMH and indeed, I didn´t add to reduce the time invested in the tests but they works.
It is a pity that you don´t believe in JMH but this is the official Java tool for MicroBenchmarking.
Review in Internet, you will find positive results with JMH.
I do admit that my initial opinions about JMH based on the first measured data in this thread were wrong. My evaluation was coming from the error margins; I thought this was the best JMH was capable of on EV3.
However, after experiments, I found that JMH is capable of providing solid results even on the EV3 (this was done in parallel with the RtControlBench tests, see earlier comments). However, it seems that to get small error margins from JMH, I had to keep some things in mind (this is not specific to JMH though, any measurement of anything would be affected by this).
steady state need to be ensured (either the JIT has to be warmed up, or it has to be disabled altogether, or sufficiently small amount of measurements has to be done (but see below))
the more measurements (iteration count or measurementTime), the better
because JMH runs two JVMs in parallel (can be verified by running top when the benchmark is running), care needs to be taken to prevent a situation where one of the JVM's memory has to be pushed out to swap
presence of sensors in sensor ports can affect the resulting score due to the additional CPU load they create
I'm also not saying that the stack profiler and GC profiler are broken in general, I just want to point out that from the data that I can access, it seems it didn't produce interpretable results in this specific environment.
I'm pretty pragmatic about how performance is measured. Whatever you like is fine, so long as it matches with the reality of how fast my kid can close a control loop. I saw useful values via System.currentTimeMillis().
If the JMH profiler is available as a library you can probably run the profiler in the same JVM as the code you are observing to save the switching overhead. I'm not sure if that is of interest.
And the same for NIO vs JNA vs JNI. Whatever performs best is the right answer.
I like the advantage of an all-Java NIO fix because it takes me a lot less to explain what the code does to the kids I'm teaching. "This code reads the sensor the same way it would read a file. It's tweaked out a bit to read the file really fast," is pretty easy. In fact "It reads this many bytes or fewer," is helpful for inviting the kids a level deeper without a huge context switch.
The next big step forward in performance is keeping the file handle open, at least on the NIO front. It's a big boost for not a lot of new concepts or complexity. Closable is extremely common, inescapable, and other people do a great job of explaining it.
As for what API to supply - KISS is the best answer. Java's culture and community tends to bring in a lot of object-oriented hierarchy complexity that doesn't help a developer understand or use a library. Here on the EV3 it really matters. Restarts are frequent. Loading extra classes is really slow. Each extra layer of abstraction - especially classes - has a big cost, but not a lot of benefit. The kid plugged in the gyro sensor to read the robot's heading. The fact that it has the same API as the color sensor doesn't make up for the complexity of creating a float array to get one number - a number that is always an int. Discover the actual useful commonality and refactor to take advantage of it instead of forcing the internal programming model on people who use your library. Do a good job of exposing everything using a similar pattern, but do a great job with the simple common use cases.
I especially want to discourage anything that involves making decisions about concurrency. The big lesson we learned while writing and proofing Concurrency in Practice was that concurrency was a really hard systemic problem best solved in outermost layers of code. Making the choice over-constrains the system. No one can predict what the right answer will be.
The next big step forward in performance is keeping the file handle open, at least on the NIO front. It's a big boost for not a lot of new concepts or complexity.
Tomorrow, I will clean the branch and I will merge to master and release the library in order to close this issue too: #739
Tomorrow, I will open a new issue to begin with the next level of optimisation.
I especially want to discourage anything that involves making decisions about concurrency. The big lesson we learned while writing and proofing Concurrency in Practice was that concurrency was a really hard systemic problem best solved in outermost layers of code.
Agree with you.
In the next iteration, we will remove the following blocking delay in a more elegant way:
this.detect(LEGO_PORT, port);
if (log.isDebugEnabled()) {
log.debug("Setting port in mode: {}", TACHO_MOTOR);
}
this.setStringAttribute(MODE, TACHO_MOTOR);
Delay.msDelay(1000);
this.detect(TACHO_MOTOR, port);
Activity
dwalend commentedon Nov 30, 2020
Here's the java code:
dwalend commentedon Nov 30, 2020
Here's the ev3 classroom code that runs in 2-4 milliseconds per iteration.
jabrena commentedon Dec 1, 2020
Hi @dwalend,
many thanks for the issue.
I will review this week, using the example provided.
Juan Antonio
jabrena commentedon Dec 1, 2020
https://github.com/ev3dev-lang-java/ev3dev-lang-java/blob/master/src/main/java/ev3dev/utils/Sysfs.java
https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#readAllLines(java.nio.file.Path,%20java.nio.charset.Charset)
JakubVanek commentedon Dec 1, 2020
I will try to hack something together in C++ in the coming days to see what is the limit of the underlying OS.
JakubVanek commentedon Dec 1, 2020
I have an initial version of the C++ benchmark available.
"Rolling-release" links (pointing at master, not the commit https://github.com/JakubVanek/ev3dev-lang-cpp-bench/commit/b01a3b22d6d4bbc3cecd23c53c214706d4e23e76):
-O3and CPU-specific optimizations enabled)However, I've noticed that even though the benchmark sets the speed attribute, it doesn't order the motors to start moving. Is this intentional? I've followed this in the C++ program. I believe the situation with leJOS/ev3dev-lang-java is similar and you'd have to call
forward()for the speed setting to take full effect.JakubVanek commentedon Dec 1, 2020
@jabrena I'd check the ideas proposed in #652, while not everything is useful, the
writeStringmethod looks more optimized than the current one.JakubVanek commentedon Dec 1, 2020
It seems that the compiler in Ubuntu 18.04 is too new to compile for ev3dev-stretch, I'll provide a binary built with a "universal" compiler (download here) that I was using for different C development.
JakubVanek commentedon Dec 1, 2020
EDIT: I'm going to use a
arm-linux-musleabi-crosscompiler from http://musl.cc/, they're available for Windows as well, so then a comparable setup should be available.dwalend commentedon Dec 1, 2020
That's correct. Starting the motors isn't in the control loop, so I left it out of the benchmark. That gives me a reason better than "I don't want to take the wheels off the robot, and don't want it crashing into stuff on my desk."
267 remaining items
jabrena commentedon Dec 25, 2020
The profilers works. Any other idea?
JakubVanek commentedon Dec 25, 2020
The performance measurement itself does work, but the last time there were only 0.0 and NaN values in the profiler results.
ev3dev-lang-java/docs/performance/jmh-results_4.json
Lines 71 to 214 in d36cc72
jabrena commentedon Dec 25, 2020
Hi @JakubVanek,
this is a kind a result from JMH and indeed, I didn´t add to reduce the time invested in the tests but they works.
It is a pity that you don´t believe in
JMHbut this is the officialJavatool for MicroBenchmarking.Review in Internet, you will find positive results with JMH.
JakubVanek commentedon Dec 25, 2020
I do admit that my initial opinions about JMH based on the first measured data in this thread were wrong. My evaluation was coming from the error margins; I thought this was the best JMH was capable of on EV3.
However, after experiments, I found that JMH is capable of providing solid results even on the EV3 (this was done in parallel with the RtControlBench tests, see earlier comments). However, it seems that to get small error margins from JMH, I had to keep some things in mind (this is not specific to JMH though, any measurement of anything would be affected by this).
topwhen the benchmark is running), care needs to be taken to prevent a situation where one of the JVM's memory has to be pushed out to swapI'm also not saying that the stack profiler and GC profiler are broken in general, I just want to point out that from the data that I can access, it seems it didn't produce interpretable results in this specific environment.
jabrena commentedon Dec 25, 2020
Oki
jabrena commentedon Dec 27, 2020
@dwalend Any feedback?
dwalend commentedon Dec 27, 2020
I'm pretty pragmatic about how performance is measured. Whatever you like is fine, so long as it matches with the reality of how fast my kid can close a control loop. I saw useful values via System.currentTimeMillis().
If the JMH profiler is available as a library you can probably run the profiler in the same JVM as the code you are observing to save the switching overhead. I'm not sure if that is of interest.
And the same for NIO vs JNA vs JNI. Whatever performs best is the right answer.
I like the advantage of an all-Java NIO fix because it takes me a lot less to explain what the code does to the kids I'm teaching. "This code reads the sensor the same way it would read a file. It's tweaked out a bit to read the file really fast," is pretty easy. In fact "It reads this many bytes or fewer," is helpful for inviting the kids a level deeper without a huge context switch.
The next big step forward in performance is keeping the file handle open, at least on the NIO front. It's a big boost for not a lot of new concepts or complexity.
Closableis extremely common, inescapable, and other people do a great job of explaining it.As for what API to supply - KISS is the best answer. Java's culture and community tends to bring in a lot of object-oriented hierarchy complexity that doesn't help a developer understand or use a library. Here on the EV3 it really matters. Restarts are frequent. Loading extra classes is really slow. Each extra layer of abstraction - especially classes - has a big cost, but not a lot of benefit. The kid plugged in the gyro sensor to read the robot's heading. The fact that it has the same API as the color sensor doesn't make up for the complexity of creating a float array to get one number - a number that is always an int. Discover the actual useful commonality and refactor to take advantage of it instead of forcing the internal programming model on people who use your library. Do a good job of exposing everything using a similar pattern, but do a great job with the simple common use cases.
I especially want to discourage anything that involves making decisions about concurrency. The big lesson we learned while writing and proofing Concurrency in Practice was that concurrency was a really hard systemic problem best solved in outermost layers of code. Making the choice over-constrains the system. No one can predict what the right answer will be.
jabrena commentedon Dec 27, 2020
Thanks for your comments @dwalend,
Tomorrow, I will clean the branch and I will merge to master and release the library in order to close this issue too:
#739
Tomorrow, I will open a new issue to begin with the next level of optimisation.
Agree with you.
In the next iteration, we will remove the following blocking delay in a more elegant way:
Thanks @dwalend and @JakubVanek for the contributions.
Juan Antonio
jabrena commentedon Dec 27, 2020
In progress:
#761
jabrena commentedon Dec 27, 2020
Merged with success in Github Actions.
https://github.com/ev3dev-lang-java/ev3dev-lang-java/actions