Skip to content

HardwareAbstractionLayer.getProcessor no longer usable on Linux without libudev #2032

Closed

Description

@chrisribble

#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 usage

However, 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

  1. added
    compatibility
    on May 7, 2022
  2. chrisribble commented on May 7, 2022

    @chrisribble
    ContributorAuthor

    @dbwiddis would make sense to have two different implementations of AbstractCentralProcessor for Linux: one for systems with udev and one for systems without udev?

    Perhaps LinuxHardwareAbstractionLayer could detect whether udev is available by attempting to load com.sun.jna.platform.linux.Udev and catching the java.lang.UnsatisfiedLinkError and 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?

    @Override
    public CentralProcessor createProcessor() {
        try {
            return new LinuxCentralProcessor();
        } catch (UnsatisfiedLinkError e) {
            // log something here?
            return new LinuxNoUdevCentralProcessor();
        }
    }
  3. added
    design discussion
    on May 7, 2022
  4. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    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.

    • Can you read the same paths from sysfs, e.g., /sys/devices/system/cpu/cpuX?
    • Are "udev" command lines available, e.g., udevadm info --query=property --name /dev/sdd ?
  5. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    @dbwiddis would make sense to have two different implementations of AbstractCentralProcessor for Linux: one for systems with udev and one for systems without udev?

    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.

    public static final boolean HAS_KSTAT2;
    static {
    Kstat2 lib = null;
    try {
    lib = Kstat2.INSTANCE;
    } catch (UnsatisfiedLinkError e) {
    // 11.3 or earlier, no kstat2
    }
    HAS_KSTAT2 = lib != null;
    }

    if (HAS_KSTAT2) {
    // Use Kstat2 implementation
    return queryProcessorId2(cpu64bit);
    }

  6. chrisribble commented on May 7, 2022

    @chrisribble
    ContributorAuthor

    I'm not familiar with how much of the host OS can be seen from a docker container.

    * Can you read the same paths from sysfs, e.g., `/sys/devices/system/cpu/cpuX`?
    
    * Are "udev" command lines available, e.g., `udevadm info --query=property --name /dev/sdd` ?
    

    The host /sys filesystem should be visible in most containers runtimes; on my system I can see all of my CPU cores with:

    $ docker run -it debian:bullseye-slim ls /sys/devices/system/cpu/
    cpu0   cpu13  cpu18  cpu5  cpufreq       kernel_max  possible  umwait_control
    cpu1   cpu14  cpu19  cpu6  cpuidle       microcode   power     vulnerabilities
    cpu10  cpu15  cpu2   cpu7  hotplug       modalias    present
    cpu11  cpu16  cpu3   cpu8  intel_pstate  offline     smt
    cpu12  cpu17  cpu4   cpu9  isolated      online      uevent
    

    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:

    $ docker run -it debian:bullseye-slim udevadm info --query=property --name /dev/sdd
    docker: Error response from daemon: failed to create shim: OCI runtime create failed: container_linux.go:380: starting container process caused: exec: "udevadm": executable file not found in $PATH: unknown.
    ERRO[0000] error waiting for container: context canceled 
    

    Of course, it's simple to install the udev on debian:bullseye-slim, but it adds another 10MB or so to the image (which isn't great). For images like distroless/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 the udev or libudev1 package in their images, which probably won't happen since it's not something people typically need).

  7. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    The host /sys filesystem should be visible in most containers runtimes

    Excellent, this should work fine.

    However, "udev" CLI binaries would only be available if the image included them

    That's fine, but can you check for the presence of uevent files that list essentially the same thing? cat /sys/devices/system/cpu/cpu0/uevent? That'll give the same info.

    Initial shell in #2033.

  8. chrisribble commented on May 7, 2022

    @chrisribble
    ContributorAuthor

    That's fine, but can you check for the presence of uevent files that list essentially the same thing? cat /sys/devices/system/cpu/cpu0/uevent? That'll give the same info.

    Sure, here's the output on my system (with a Core i7-12700K):

    $ docker run -it debian:bullseye-slim cat /sys/devices/system/cpu/cpu0/uevent
    DRIVER=processor
    MODALIAS=cpu:type:x86,ven0000fam0006mod0097:feature:,0000,0001,0002,0003,0004,0005,0006,0007,0008,0009,000B,000C,000D,000E,000F,0010,0011,0013,0015,0016,0017,0018,0019,001A,001B,001C,001D,001F,002B,0034,003A,003B,003D,0068,006A,006B,006C,006D,006F,0070,0072,0074,0075,0076,0078,0079,007C,007F,0080,0081,0082,0083,0084,0085,0086,0087,0088,0089,008B,008C,008D,008E,008F,0091,0093,0094,0095,0096,0097,0098,0099,009A,009B,009C,009D,009E,00C0,00C5,00C8,00E1,00E7,00F0,00F1,00F3,00F5,00F9,00FA,00FB,00FE,00FF,0100,0101,0102,0103,0104,0111,0120,0121,0123,0125,0126,0127,0128,0129,012A,012D,0132,0133,0134,0137,0138,0139,013D,0140,0141,0142,0143,0164,0165,0166,0184,018B,0196,01C0,01C1,01C2,01C4,01C5,01C6,01C7,01C8,01C9,01CA,01CB,01CE,01CF,01D0,01D1,01D2,01D3,01D4,01D6,01D7,0202,0203,0204,0205,0207,0208,0209,020A,020D,0216,0217,021B,021C,021F,0244,024A,024E,024F,0252,0253,0254,025A,025B,025C,025D,025E,025F
    
  9. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    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.

  10. chrisribble commented on May 7, 2022

    @chrisribble
    ContributorAuthor

    @dbwiddis I built the patch in #2033 locally and tried it with my application in Docker (using a distroless/java base image without libudev) and it seems to work.

    A simple test where I spawn yes 6 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.

  11. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    Yeah, but your per-processor CPU usage is broken as the TODO ends up creating a single processor. :)

  12. dbwiddis commented on May 7, 2022

    @dbwiddis
    Member

    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.

  13. added
    containers
    on Jul 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    compatibilitycontainersdesign discussion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions