Reduce busy-waiting CPU load in waitEvents()#201
Conversation
781cc8f to
116784b
Compare
The
We can fix the build by either using older macos runner ( |
@pietrygamat thanks for digging into this... I have to admit each time I revisit this my memory is more and more foggy what we can and can't do with the older SDKs. I bump into this a lot with another project and this is what we do over there... macos:
strategy:
fail-fast: false
matrix:
arch: [ x86_64, arm64 ]
include:
- arch: x86_64
os: macos-15-intel
xcode: "16.4"
- arch: arm64
os: macos-15
xcode: "16.4"... specifically though, we leverage an environment variable
That's a lot to say that Copying the other project's strategy it does still use a hard-coded runner version Edit: Whoops, sorry for the copy/paste tags above. |
I did not yet understand the comments about MacOS. So what should jssc do? I'll give one of the suggestions a try 🙂 Edit 1: I could not find anything that would look like |
Your PR can just use an old runner, even if it's EOL in a month. @pietrygamat and I will have to look for a more permanent solution, but it doesn't necessarily need to be fixed here. |
The {@link #waitEvents()} implementation on non-windows systems
usually returns immediately. This is unfortunate for the callers
which want to await events in an infinite-loop. As doing so would
burn lot of CPU time.
For example: Code in `LinuxEventThread.run()` (in `SerialPort.java`)
calls us in an infinite-loop. As a work-around, it uses a (very) small
sleep, to not utilize a full CPU core all the time. But still, this
permanently wastes a lot of CPU cycles (that many, that it is a problem
in our production use-case). The win32 code uses `OVERLAPPED` structs
and `WaitSingleObject()` which already provide that kind of "wait"
mechanism.
Not perfect, but this patch at least provides a way to wait if nothing
is ready. That "feature" is off by default and can be enabled
individually.
Edit: TryFix CICD build
> - try fix error:
> "Compatibility with CMake < 3.5 has been removed from CMake."
> - Use an older ubuntu to broaden runtime compatiblity
> - try fix error:
> "Failed to locate 'make', requesting installation of command line developer tools"
> - Revert some non-working MacOS fixes
> - Explain why to use older ubuntu version
> - Update checked-in JNI header file as generated by JDK
> - GuessFix MacOS CICD
> java-native#201 (comment)
> - Drop unneccessary compatibility symbols. as discussed in:
> java-native#201 (comment)
> - Cleanup JavaDoc comment
Squashed-From: 8279b5d
116784b to
a63a20d
Compare
The {@link #waitEvents()} implementation on non-windows systems
usually returns immediately. This is unfortunate for the callers
which want to await events in an infinite-loop. As doing so would
burn lot of CPU time.
For example: Code in `LinuxEventThread.run()` (in `SerialPort.java`)
calls us in an infinite-loop. As a work-around, it uses a (very) small
sleep, to not utilize a full CPU core all the time. But still, this
permanently wastes a lot of CPU cycles (that many, that it is a problem
in our production use-case). The win32 code uses `OVERLAPPED` structs
and `WaitSingleObject()` which already provide that kind of "wait"
mechanism.
Not perfect, but this patch at least provides a way to wait if nothing
is ready. That "feature" is off by default and can be enabled
individually.
Edit: TryFix CICD build
> - try fix error:
> "Compatibility with CMake < 3.5 has been removed from CMake."
> - Use an older ubuntu to broaden runtime compatiblity
> - try fix error:
> "Failed to locate 'make', requesting installation of command line developer tools"
> - Revert some non-working MacOS fixes
> - Explain why to use older ubuntu version
> - Update checked-in JNI header file as generated by JDK
> - GuessFix MacOS CICD
> java-native#201 (comment)
> - Drop unneccessary compatibility symbols. as discussed in:
> java-native#201 (comment)
> - Cleanup JavaDoc comment
> - Whops. Fix the log-damp counter.
Squashed-From: eeab9b4
a63a20d to
cbb0cd8
Compare
|
I tested the baseline functionality for regressions, none were found. I did not test the new timeout feature, just regression tested the old feature. Since Windows already behaved this way, there's a part of me that wants this to just be the new default with a sane value to bring better parity however the code comments suggest that there are conditions where this is undesired, so I'm fine with it as-is. I do like the current implementation of setting this globally so all connections behave the same way, so no objections here. Perhaps the last enhancement I would request is to provide a recommended value for others experiencing this issue (e.g. 2ms?), since the implementation may be otherwise lost upon the casual onlooker. |
as discussed in java-native#201 (comment)
The {@link #waitEvents()} implementation on non-windows systems
usually returns immediately. This is unfortunate for the callers
which want to await events in an infinite-loop. As doing so would
burn lot of CPU time.
For example: Code in `LinuxEventThread.run()` (in `SerialPort.java`)
calls us in an infinite-loop. As a work-around, it uses a (very) small
sleep, to not utilize a full CPU core all the time. But still, this
permanently wastes a lot of CPU cycles (that many, that it is a problem
in our production use-case). The win32 code uses `OVERLAPPED` structs
and `WaitSingleObject()` which already provide that kind of "wait"
mechanism.
Not perfect, but this patch at least provides a way to wait if nothing
is ready. That "feature" is off by default and can be enabled
individually.
Edit:
> - try fix error:
> "Compatibility with CMake < 3.5 has been removed from CMake."
> - Use an older ubuntu to broaden runtime compatiblity
> - try fix error:
> "Failed to locate 'make', requesting installation of command line developer tools"
> - Revert some non-working MacOS fixes
> - Explain why to use older ubuntu version
> - Update checked-in JNI header file as generated by JDK
> - GuessFix MacOS CICD
> java-native#201 (comment)
> - Drop unneccessary compatibility symbols. as discussed in:
> java-native#201 (comment)
> - Cleanup JavaDoc comment
> - Whops. Fix the log-damp counter.
> - Drop convenience overload
> See java-native#201 (comment)
> - Allow `-1` in setter to disable again.
> See java-native#201 (comment)
> - Give hint about using 100ms
> java-native#201 (comment)
Squashed-From: e86ce17
cbb0cd8 to
b32964b
Compare
The {@link #waitEvents()} implementation on non-windows systems
usually returns immediately. This is unfortunate for the callers
which want to await events in an infinite-loop. As doing so would
burn lot of CPU time.
For example: Code in `LinuxEventThread.run()` (in `SerialPort.java`)
calls us in an infinite-loop. As a work-around, it uses a (very) small
sleep, to not utilize a full CPU core all the time. But still, this
permanently wastes a lot of CPU cycles (that many, that it is a problem
in our production use-case). The win32 code uses `OVERLAPPED` structs
and `WaitSingleObject()` which already provide that kind of "wait"
mechanism.
Not perfect, but this patch at least provides a way to wait if nothing
is ready. That "feature" is off by default and can be enabled
individually.
Edit:
> - try fix error:
> "Compatibility with CMake < 3.5 has been removed from CMake."
> - Use an older ubuntu to broaden runtime compatiblity
> - try fix error:
> "Failed to locate 'make', requesting installation of command line developer tools"
> - Revert some non-working MacOS fixes
> - Explain why to use older ubuntu version
> - Update checked-in JNI header file as generated by JDK
> - GuessFix MacOS CICD
> java-native#201 (comment)
> - Drop unneccessary compatibility symbols. as discussed in:
> java-native#201 (comment)
> - Cleanup JavaDoc comment
> - Whops. Fix the log-damp counter.
> - Drop convenience overload
> See java-native#201 (comment)
> - Allow `-1` in setter to disable again.
> See java-native#201 (comment)
> - Give hint about using 100ms
> java-native#201 (comment)
> - Skip timeout in EventThread ctor while state init
> java-native#201 (comment)
Squashed-From: 03704d8
b32964b to
476e6f3
Compare
The
waitEvents()implementation on non-windows systems usually returns immediately. This is unfortunate for the callers which want to await events in an infinite-loop. As doing so would burn lot of CPU time.For example: Code in
LinuxEventThread.run()(inSerialPort.java) calls us in an infinite-loop. As a work-around, it uses a (very) small sleep, to not utilize a full CPU core all the time. But still, this permanently wastes a lot of CPU cycles (that many, that it is a problem in our production use-case). The win32 code usesOVERLAPPEDstructs andWaitSingleObject()which already provide that kind of "wait" mechanism.Edit: Unfortunately I've no idea how to fix macOS CICD build :(