Skip to content

Commit

Permalink
Merge pull request #639 from dbwiddis/master
Browse files Browse the repository at this point in the history
Add getloadavg() to OS X and Unix
  • Loading branch information
dblock committed Apr 16, 2016
2 parents dda206e + e8edcc1 commit 4ba3c1d
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ Features
* [#616](https://github.com/java-native-access/jna/pull/616): Allow access to base interfaces (most important IDispatch) via ProxyObject and improve binding by allowing to use dispId for the call - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#621](https://github.com/java-native-access/jna/pull/621): Added TYPEFLAGS-constants for `wTypeFlags` in `com.sun.jna.platform.win32.OaIdl.TYPEATTR` - [@SevenOf9Sleeper](https://github.com/SevenOf9Sleeper).
* [#625](https://github.com/java-native-access/jna/pull/625): Make conversion to/from java to/from VARIANT in `com.sun.jna.platform.win32.COM.util.Convert` more flexible and dependable - [@matthiasblaesing](https://github.com/matthiasblaesing).
* [#639](https://github.com/java-native-access/jna/pull/639): Add getloadavg() to OS X and Unix - [@dbwiddis](https://github.com/dbwiddis).

Bug Fixes
---------
Expand Down
25 changes: 25 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/mac/SystemB.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,15 @@ protected List<String> getFieldOrder() {
* @return the host's name port
*/
int mach_host_self();

/**
* The mach_task_self system call returns the calling thread's task_self
* port. It has an effect equivalent to receiving a send right for the task's
* kernel port.
*
* @return the task's kernel port
*/
int mach_task_self();

/**
* The host_page_size function returns the page size for the given host.
Expand Down Expand Up @@ -333,4 +342,20 @@ int sysctlbyname(String name, Pointer oldp, IntByReference oldlenp,
*/
int host_processor_info(int machPort, int flavor, IntByReference procCount,
PointerByReference procInfo, IntByReference procInfoCount);

/**
* The getloadavg() function returns the number of processes in the system
* run queue averaged over various periods of time. Up to nelem samples are
* retrieved and assigned to successive elements of loadavg[]. The system
* imposes a maximum of 3 samples, representing averages over the last 1, 5,
* and 15 minutes, respectively.
* @param loadavg
* An array of doubles which will be filled with the results
* @param nelem
* Number of samples to return
* @return If the load average was unobtainable, -1 is returned; otherwise,
* the number of samples actually retrieved is returned.
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
*/
int getloadavg(double[] loadavg, int nelem);
}
14 changes: 14 additions & 0 deletions contrib/platform/src/com/sun/jna/platform/unix/LibCAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,18 @@ public interface LibCAPI extends Reboot, Resource {
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=setenv&sektion=3">getenv(3)</A>
*/
int unsetenv(String name);

/**
* The getloadavg() function returns the number of processes in the system
* run queue averaged over various periods of time. Up to nelem samples are
* retrieved and assigned to successive elements of loadavg[]. The system
* imposes a maximum of 3 samples, representing averages over the last 1, 5,
* and 15 minutes, respectively.
* @param loadavg An array of doubles which will be filled with the results
* @param nelem Number of samples to return
* @return If the load average was unobtainable, -1 is returned; otherwise,
* the number of samples actually retrieved is returned.
* @see <A HREF="https://www.freebsd.org/cgi/man.cgi?query=getloadavg&sektion=3">getloadavg(3)</A>
*/
int getloadavg(double[] loadavg, int nelem);
}
18 changes: 17 additions & 1 deletion contrib/platform/test/com/sun/jna/platform/mac/SystemBTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,23 @@ public void testHostProcessorInfo() {
assertEquals(procCpuLoadInfo.getValue().getIntArray(0,
procInfoCount.getValue()).length, procInfoCount.getValue());
}


public void testMachPorts() {
int machPort = SystemB.INSTANCE.mach_host_self();
assertTrue(machPort > 0);
machPort = SystemB.INSTANCE.mach_task_self();
assertTrue(machPort > 0);
}

public void testGetLoadAvg() {
double[] loadavg = new double[3];
int retval = SystemB.INSTANCE.getloadavg(loadavg, 3);
assertEquals(retval, 3);
assertTrue(loadavg[0] >= 0);
assertTrue(loadavg[1] >= 0);
assertTrue(loadavg[2] >= 0);
}

public static void main(java.lang.String[] argList) {
junit.textui.TestRunner.run(SystemBTest.class);
}
Expand Down
10 changes: 10 additions & 0 deletions contrib/platform/test/com/sun/jna/platform/unix/LibCTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,14 @@ public void testSetenv() {
LibC.INSTANCE.unsetenv(name);
}
}

@Test
public void testGetLoadAvg() {
double[] loadavg = new double[3];
int retval = LibC.INSTANCE.getloadavg(loadavg, 3);
assertEquals(retval, 3);
assertTrue(loadavg[0] >= 0);
assertTrue(loadavg[1] >= 0);
assertTrue(loadavg[2] >= 0);
}
}

0 comments on commit 4ba3c1d

Please sign in to comment.