Skip to content

Commit

Permalink
Add Linux LibC with sysinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Aug 15, 2018
1 parent 2e4395f commit 6c39b7d
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------
Expand Down
76 changes: 76 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/linux/LibC.java
Original file line number Diff line number Diff line change
@@ -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;

/**
* <I>libc</I> 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);
}
13 changes: 13 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/linux/package.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<html>
<head>
<!--
Copyright (c) 2017 Daniel Widdis
-->
</head>
<body bgcolor="white">

<!-- One sentence summary -->
Provides common library mappings for Linux.

</body>
</html>
56 changes: 56 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/linux/LibCTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 6c39b7d

Please sign in to comment.