-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
13
contrib/platform/src/com/sun/jna/platform/linux/package.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
57 changes: 57 additions & 0 deletions
57
contrib/platform/test/com/sun/jna/platform/linux/LibCTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* 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; | ||
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() / (float) (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()); | ||
} | ||
} |