< prev index next >
test/jdk/com/sun/tools/jextract/libproc/LibprocTest.java
Print this page
*** 29,39 ****
import java.foreign.memory.Pointer;
import java.foreign.memory.Struct;
import java.lang.invoke.MethodHandles;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
! import test.jextract.lp.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/*
--- 29,41 ----
import java.foreign.memory.Pointer;
import java.foreign.memory.Struct;
import java.lang.invoke.MethodHandles;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
! import static test.jextract.lp.libproc_h.*;
! import static test.jextract.lp.proc_info_h.*;
! import test.jextract.lp.proc_info;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertTrue;
/*
*** 43,81 ****
* @run driver JtregJextract -t test.jextract.lp -lproc -rpath /usr/lib -- /usr/include/libproc.h /usr/include/sys/proc_info.h
* @run testng LibprocTest
*/
public class LibprocTest {
private static final int NAME_BUF_MAX = 256;
-
- private libproc lp;
-
- @BeforeTest
- public void init() {
- lp = Libraries.bind(MethodHandles.lookup(), libproc.class);
- }
-
@Test
public void processListTest() {
long curProcPid = ProcessHandle.current().pid();
boolean foundCurProc = false;
// Scope for native allocations
try (Scope s = Scope.newNativeScope()) {
// get the number of processes
! int numPids = lp.proc_listallpids(Pointer.nullPointer(), 0);
// allocate an array
Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids);
// list all the pids into the native array
! lp.proc_listallpids(pids.elementPointer(), numPids);
// convert native array to java array
int[] jpids = pids.toArray(num -> new int[num]);
// buffer for process name
Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX);
for (int i = 0; i < jpids.length; i++) {
int pid = jpids[i];
// get the process name
! lp.proc_name(pid, nameBuf, NAME_BUF_MAX);
String procName = Pointer.toString(nameBuf);
// print pid and process name
System.out.printf("%d %s\n", pid, procName);
if (curProcPid == pid) {
foundCurProc = true;
--- 45,75 ----
* @run driver JtregJextract -t test.jextract.lp -lproc -rpath /usr/lib -- /usr/include/libproc.h /usr/include/sys/proc_info.h
* @run testng LibprocTest
*/
public class LibprocTest {
private static final int NAME_BUF_MAX = 256;
@Test
public void processListTest() {
long curProcPid = ProcessHandle.current().pid();
boolean foundCurProc = false;
// Scope for native allocations
try (Scope s = Scope.newNativeScope()) {
// get the number of processes
! int numPids = proc_listallpids(Pointer.nullPointer(), 0);
// allocate an array
Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids);
// list all the pids into the native array
! proc_listallpids(pids.elementPointer(), numPids);
// convert native array to java array
int[] jpids = pids.toArray(num -> new int[num]);
// buffer for process name
Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX);
for (int i = 0; i < jpids.length; i++) {
int pid = jpids[i];
// get the process name
! proc_name(pid, nameBuf, NAME_BUF_MAX);
String procName = Pointer.toString(nameBuf);
// print pid and process name
System.out.printf("%d %s\n", pid, procName);
if (curProcPid == pid) {
foundCurProc = true;
*** 87,101 ****
}
@Test
public void processInfoTest() {
long curProcPid = ProcessHandle.current().pid();
- proc_info pi = Libraries.bind(MethodHandles.lookup(), proc_info.class);
try (Scope s = Scope.newNativeScope()) {
proc_info.proc_taskinfo ti = s.allocateStruct(proc_info.proc_taskinfo.class);
int taskInfoSize = (int)Struct.sizeof(proc_info.proc_taskinfo.class);
! int resultSize = lp.proc_pidinfo((int)curProcPid, pi.PROC_PIDTASKINFO(), 0, ti.ptr(), taskInfoSize);
assertEquals(resultSize, taskInfoSize);
System.out.println("total virtual memory size = " + ti.pti_virtual_size$get());
System.out.println("resident memory size = " + ti.pti_resident_size$get());
System.out.println("total time = " + ti.pti_total_user$get());
}
--- 81,94 ----
}
@Test
public void processInfoTest() {
long curProcPid = ProcessHandle.current().pid();
try (Scope s = Scope.newNativeScope()) {
proc_info.proc_taskinfo ti = s.allocateStruct(proc_info.proc_taskinfo.class);
int taskInfoSize = (int)Struct.sizeof(proc_info.proc_taskinfo.class);
! int resultSize = proc_pidinfo((int)curProcPid, PROC_PIDTASKINFO, 0, ti.ptr(), taskInfoSize);
assertEquals(resultSize, taskInfoSize);
System.out.println("total virtual memory size = " + ti.pti_virtual_size$get());
System.out.println("resident memory size = " + ti.pti_resident_size$get());
System.out.println("total time = " + ti.pti_total_user$get());
}
< prev index next >