diff --git a/CHANGES.md b/CHANGES.md index dfedc760d2..d070c2bf1a 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -27,6 +27,7 @@ Features * [#984](https://github.com/java-native-access/jna/issues/984): Added `CM_Locate_DevNode`, `CM_Get_Parent`, `CM_Get_Child`, `CM_Get_Sibling`, `CM_Get_Device_ID`, and `CM_Get_Device_ID_Size` to `c.s.j.platform.win32.Cfgmgr32.java` and a `c.s.j.platform.win32.Cfgmgr32Util` class for `CM_Get_Device_ID` - [@dbwiddis](https://github.com/dbwiddis). * [#988](https://github.com/java-native-access/jna/issues/988): Added `PdhLookupPerfIndexByEnglishName` to `c.s.j.platform.win32.PdhUtil` - [@dbwiddis](https://github.com/dbwiddis). * [#992](https://github.com/java-native-access/jna/pull/992): Improve stability of windows tests and add appveyor configuration for windows CI builds - [@matthiasblaesing](https://github.com/matthiasblaesing). +* [#997](https://github.com/java-native-access/jna/issues/997): Added `Sysinfo` structure and function to `c.s.j.platform.linux.LibC` - [@dbwiddis](https://github.com/dbwiddis). Bug Fixes --------- diff --git a/contrib/platform/src/com/sun/jna/platform/linux/LibC.java b/contrib/platform/src/com/sun/jna/platform/linux/LibC.java new file mode 100644 index 0000000000..28d5fdea41 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/linux/LibC.java @@ -0,0 +1,76 @@ +/* Copyright (c) 2017 Daniel Widdis, All Rights Reserved + * + * The contents of this file is dual-licensed under 2 + * alternative Open Source/Free licenses: LGPL 2.1 or later and + * Apache License 2.0. (starting with JNA version 4.0.0). + * + * You can freely decide which license you want to apply to + * the project. + * + * You may obtain a copy of the LGPL License at: + * + * http://www.gnu.org/licenses/licenses.html + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "LGPL2.1". + * + * You may obtain a copy of the Apache License at: + * + * http://www.apache.org/licenses/ + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "AL2.0". + */ +package com.sun.jna.platform.linux; + +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.NativeLong; +import com.sun.jna.Structure; +import com.sun.jna.Structure.FieldOrder; +import com.sun.jna.platform.unix.LibCAPI; + +/** + * libc API + * + * @author Daniel Widdis + */ +public interface LibC extends LibCAPI, Library { + String NAME = "c"; + LibC INSTANCE = Native.load(NAME, LibC.class); + + @FieldOrder({ "uptime", "loads", "totalram", "freeram", "sharedram", "bufferram", "totalswap", "freeswap", "procs", + "totalhigh", "freehigh", "mem_unit", "_f" }) + class Sysinfo extends Structure { + public NativeLong uptime; // Seconds since boot + // 1, 5, and 15 minute load averages + public NativeLong[] loads = new NativeLong[3]; + public NativeLong totalram; // Total usable main memory size + public NativeLong freeram; // Available memory size + public NativeLong sharedram; // Amount of shared memory + public NativeLong bufferram; // Memory used by buffers + public NativeLong totalswap; // Total swap space size + public NativeLong freeswap; // swap space still available + public short procs; // Number of current processes + public NativeLong totalhigh; // Total high memory size + public NativeLong freehigh; // Available high memory size + public int mem_unit; // Memory unit size in bytes + // Padding is based on the size of NativeLong. When the size is 4 we + // need 8 bytes of padding. When this size is 8, zero padding is needed + // but we are not allowed to declare an array of size zero. It's safe to + // declare extra padding in the structure; these bytes simply will not + // be written on 64-bit systems. + public byte[] _f = new byte[8]; // padding to 64 bytes + } + + /** + * sysinfo() provides a simple way of getting overall system statistics. + * This is more portable than reading /dev/kmem. + * + * @param info + * A Sysinfo structure which will be populated + * @return On success, zero is returned. On error, -1 is returned, and errno + * is set appropriately. + */ + int sysinfo(Sysinfo info); +} diff --git a/contrib/platform/src/com/sun/jna/platform/linux/package.html b/contrib/platform/src/com/sun/jna/platform/linux/package.html new file mode 100644 index 0000000000..a6be89f366 --- /dev/null +++ b/contrib/platform/src/com/sun/jna/platform/linux/package.html @@ -0,0 +1,13 @@ + + + + + + + +Provides common library mappings for Linux. + + + \ No newline at end of file diff --git a/contrib/platform/test/com/sun/jna/platform/linux/LibCTest.java b/contrib/platform/test/com/sun/jna/platform/linux/LibCTest.java new file mode 100644 index 0000000000..b7ff42ee4e --- /dev/null +++ b/contrib/platform/test/com/sun/jna/platform/linux/LibCTest.java @@ -0,0 +1,56 @@ +/* Copyright (c) 2017 Daniel Widdis, All Rights Reserved + * + * The contents of this file is dual-licensed under 2 + * alternative Open Source/Free licenses: LGPL 2.1 or later and + * Apache License 2.0. (starting with JNA version 4.0.0). + * + * You can freely decide which license you want to apply to + * the project. + * + * You may obtain a copy of the LGPL License at: + * + * http://www.gnu.org/licenses/licenses.html + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "LGPL2.1". + * + * You may obtain a copy of the Apache License at: + * + * http://www.apache.org/licenses/ + * + * A copy is also included in the downloadable source code package + * containing JNA, in file "AL2.0". + */ +package com.sun.jna.platform.linux; + +import org.junit.Test; + +import com.sun.jna.platform.linux.LibC.Sysinfo; + +import junit.framework.TestCase; + +/** + * Exercise the {@link LibC} class. + * + * @author widdis@gmail.com + */ +public class LibCTest extends TestCase { + + @Test + public void testSysinfo() { + Sysinfo info = new Sysinfo(); + assertEquals(0, LibC.INSTANCE.sysinfo(info)); + + // Get loadavg for comparison (rounds to nearest hundredth) + double[] loadavg = new double[3]; + LibC.INSTANCE.getloadavg(loadavg, 3); + // Sysinfo loadavg longs must be divided by 2^16 + for (int i = 0; i < 3; i++) { + assertEquals(loadavg[i], info.loads[i].longValue() / (double) (1 << 16), 0.02); + } + assertTrue(info.uptime.longValue() > 0); + assertTrue(info.totalram.longValue() > 0); + assertTrue(info.freeram.longValue() <= info.totalram.longValue()); + assertTrue(info.freeswap.longValue() <= info.totalswap.longValue()); + } +}