Repository navigation
Description
#1849 added a hard dependency on libudev.
We have some applications that are using Oshi running in a Docker container and it's non-trivial to install udev in some of the images (specifically for our images based on https://github.com/GoogleContainerTools/distroless/tree/main/java).
In releases previous to 6.1.0, it was possible to do something simple like this without libudev:
SystemInfo system = new SystemInfo();
HardwareAbstractionLayer hal = system.getHardware();
CentralProcessor cpu = hal.getProcessor();
// get load ticks periodically and compute CPU usageHowever, in version 6.1.0 and newer, this doesn't work because LinuxCentralProcessor.initProcessorCounts() attempts to use load libudev.so (via JNA's Udev abstraction) but doesn't have any sort of fallback to the old mechanism.
Example stack trace:
Suppressed: java.lang.UnsatisfiedLinkError: libudev.so: cannot open shared object file: No such file or directory
at com.sun.jna.Native.open(Native Method) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:204) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:461) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.Library$Handler.<init>(Library.java:192) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.Native.load(Native.java:622) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.Native.load(Native.java:596) ~[jna-5.10.0.jar!/:5.10.0 (b0)]
at com.sun.jna.platform.linux.Udev.<clinit>(Udev.java:37) ~[jna-platform-5.10.0.jar!/:5.10.0 (b0)]
at oshi.hardware.platform.linux.LinuxCentralProcessor.initProcessorCounts(LinuxCentralProcessor.java:166) ~[oshi-core-6.1.5.jar!/:6.1.5]
at oshi.hardware.common.AbstractCentralProcessor.<init>(AbstractCentralProcessor.java:83) ~[oshi-core-6.1.5.jar!/:6.1.5]
at oshi.hardware.platform.linux.LinuxCentralProcessor.<init>(LinuxCentralProcessor.java:64) ~[oshi-core-6.1.5.jar!/:6.1.5]
at oshi.hardware.platform.linux.LinuxHardwareAbstractionLayer.createProcessor(LinuxHardwareAbstractionLayer.java:62) ~[oshi-core-6.1.5.jar!/:6.1.5]
at oshi.util.Memoizer$1.get(Memoizer.java:87) ~[oshi-core-6.1.5.jar!/:6.1.5]
at oshi.hardware.common.AbstractHardwareAbstractionLayer.getProcessor(AbstractHardwareAbstractionLayer.java:68) ~[oshi-core-6.1.5.jar!/:6.1.5]
// snip
I see that there was a reason behind this change (heterogeneous CPU core improvements), but it would be really awesome if Oshi could fall back to the old mechanism when libudev isn't available, perhaps gracefully degrading WRT which capabilities it is able to report.
For now, I can just stick with an older release, which works fine, but I'd love to be able to use a newer release without needing to install libudev in my Docker images.
Activity
chrisribble commentedon May 7, 2022
@dbwiddis would make sense to have two different implementations of
AbstractCentralProcessorfor Linux: one for systems with udev and one for systems without udev?Perhaps
LinuxHardwareAbstractionLayercould detect whether udev is available by attempting to loadcom.sun.jna.platform.linux.Udevand catching thejava.lang.UnsatisfiedLinkErrorand then fall back to the old implementation?Netty does something like this for detecting whether native capabilities are available.
EDIT: Maybe something simple like this would be reasonable?
dbwiddis commentedon May 7, 2022
It's entirely reasonable to catch that particular UnsatisfiedLinkError.
At one point I started work on an object-oriented abstraction of JNA's udev class that internally did a fallback to reading via sysfs. That would work not only for this case but for other uses of Udev in OSHI's code (that are not invoked in the constructor).
I'm not familiar with how much of the host OS can be seen from a docker container.
/sys/devices/system/cpu/cpuX?udevadm info --query=property --name /dev/sdd?dbwiddis commentedon May 7, 2022
This is solving the problem with a sledgehammer. :)
A better pattern is how Solaris Kstat lookups are routed to a method using the older kstat if libkstat2 isn't available.
oshi/oshi-core/src/main/java/oshi/software/os/unix/solaris/SolarisOperatingSystem.java
Lines 80 to 89 in 14540ca
oshi/oshi-core/src/main/java/oshi/hardware/platform/unix/solaris/SolarisCentralProcessor.java
Lines 65 to 68 in 48043ae
chrisribble commentedon May 7, 2022
The host /sys filesystem should be visible in most containers runtimes; on my system I can see all of my CPU cores with:
However, "udev" CLI binaries would only be available if the image included them and it would be rare for the type of slim images that most microservices are built with to include it.
For example:
Of course, it's simple to install the
udevondebian:bullseye-slim, but it adds another 10MB or so to the image (which isn't great). For images likedistroless/java(which elide the package manager), it's a bit more of a pain to do that (since it involves using Bazel to build your own image or convincing upstream to include theudevorlibudev1package in their images, which probably won't happen since it's not something people typically need).dbwiddis commentedon May 7, 2022
Excellent, this should work fine.
That's fine, but can you check for the presence of
ueventfiles that list essentially the same thing?cat /sys/devices/system/cpu/cpu0/uevent? That'll give the same info.Initial shell in #2033.
chrisribble commentedon May 7, 2022
Sure, here's the output on my system (with a Core i7-12700K):
dbwiddis commentedon May 7, 2022
Great, this should be enough for a graceful fallback for the CPU info, and I suspect we can replicate this elsewhere where Udev is used.
chrisribble commentedon May 7, 2022
@dbwiddis I built the patch in #2033 locally and tried it with my application in Docker (using a
distroless/javabase image withoutlibudev) and it seems to work.A simple test where I spawn
yes6 times on the host (8 performance cores with hyper-threading and 4 efficiency cores) results in about 30% CPU usage reported by my code that is using Oshi, which seems about right.dbwiddis commentedon May 7, 2022
Yeah, but your per-processor CPU usage is broken as the TODO ends up creating a single processor. :)
dbwiddis commentedon May 7, 2022
OK, I think #2033 should be good to go.
Bigger question on whether this is a scalable replacement for all the other Udev uses... perhaps best to wait for people to specifically request that.