14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.foreign.Libraries; 25 import java.foreign.Library; 26 import java.foreign.NativeTypes; 27 import java.foreign.Scope; 28 import java.foreign.memory.Array; 29 import java.foreign.memory.Pointer; 30 import java.foreign.memory.Struct; 31 import java.lang.invoke.MethodHandles; 32 import org.testng.annotations.BeforeTest; 33 import org.testng.annotations.Test; 34 import test.jextract.lp.*; 35 36 import static org.testng.Assert.assertEquals; 37 import static org.testng.Assert.assertTrue; 38 39 /* 40 * @test 41 * @library .. 42 * @requires (os.family == "mac") 43 * @run driver JtregJextract -t test.jextract.lp -lproc -rpath /usr/lib -- /usr/include/libproc.h /usr/include/sys/proc_info.h 44 * @run testng LibprocTest 45 */ 46 public class LibprocTest { 47 private static final int NAME_BUF_MAX = 256; 48 49 private libproc lp; 50 51 @BeforeTest 52 public void init() { 53 lp = Libraries.bind(MethodHandles.lookup(), libproc.class); 54 } 55 56 @Test 57 public void processListTest() { 58 long curProcPid = ProcessHandle.current().pid(); 59 boolean foundCurProc = false; 60 61 // Scope for native allocations 62 try (Scope s = Scope.newNativeScope()) { 63 // get the number of processes 64 int numPids = lp.proc_listallpids(Pointer.nullPointer(), 0); 65 // allocate an array 66 Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids); 67 // list all the pids into the native array 68 lp.proc_listallpids(pids.elementPointer(), numPids); 69 // convert native array to java array 70 int[] jpids = pids.toArray(num -> new int[num]); 71 // buffer for process name 72 Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX); 73 for (int i = 0; i < jpids.length; i++) { 74 int pid = jpids[i]; 75 // get the process name 76 lp.proc_name(pid, nameBuf, NAME_BUF_MAX); 77 String procName = Pointer.toString(nameBuf); 78 // print pid and process name 79 System.out.printf("%d %s\n", pid, procName); 80 if (curProcPid == pid) { 81 foundCurProc = true; 82 System.out.println("Found the current process!"); 83 } 84 } 85 } 86 assertTrue(foundCurProc); 87 } 88 89 @Test 90 public void processInfoTest() { 91 long curProcPid = ProcessHandle.current().pid(); 92 proc_info pi = Libraries.bind(MethodHandles.lookup(), proc_info.class); 93 try (Scope s = Scope.newNativeScope()) { 94 proc_info.proc_taskinfo ti = s.allocateStruct(proc_info.proc_taskinfo.class); 95 int taskInfoSize = (int)Struct.sizeof(proc_info.proc_taskinfo.class); 96 int resultSize = lp.proc_pidinfo((int)curProcPid, pi.PROC_PIDTASKINFO(), 0, ti.ptr(), taskInfoSize); 97 assertEquals(resultSize, taskInfoSize); 98 System.out.println("total virtual memory size = " + ti.pti_virtual_size$get()); 99 System.out.println("resident memory size = " + ti.pti_resident_size$get()); 100 System.out.println("total time = " + ti.pti_total_user$get()); 101 } 102 } 103 } | 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 import java.foreign.Libraries; 25 import java.foreign.Library; 26 import java.foreign.NativeTypes; 27 import java.foreign.Scope; 28 import java.foreign.memory.Array; 29 import java.foreign.memory.Pointer; 30 import java.foreign.memory.Struct; 31 import java.lang.invoke.MethodHandles; 32 import org.testng.annotations.BeforeTest; 33 import org.testng.annotations.Test; 34 import static test.jextract.lp.libproc_h.*; 35 import static test.jextract.lp.proc_info_h.*; 36 import test.jextract.lp.proc_info; 37 38 import static org.testng.Assert.assertEquals; 39 import static org.testng.Assert.assertTrue; 40 41 /* 42 * @test 43 * @library .. 44 * @requires (os.family == "mac") 45 * @run driver JtregJextract -t test.jextract.lp -lproc -rpath /usr/lib -- /usr/include/libproc.h /usr/include/sys/proc_info.h 46 * @run testng LibprocTest 47 */ 48 public class LibprocTest { 49 private static final int NAME_BUF_MAX = 256; 50 @Test 51 public void processListTest() { 52 long curProcPid = ProcessHandle.current().pid(); 53 boolean foundCurProc = false; 54 55 // Scope for native allocations 56 try (Scope s = Scope.newNativeScope()) { 57 // get the number of processes 58 int numPids = proc_listallpids(Pointer.nullPointer(), 0); 59 // allocate an array 60 Array<Integer> pids = s.allocateArray(NativeTypes.INT32, numPids); 61 // list all the pids into the native array 62 proc_listallpids(pids.elementPointer(), numPids); 63 // convert native array to java array 64 int[] jpids = pids.toArray(num -> new int[num]); 65 // buffer for process name 66 Pointer<Byte> nameBuf = s.allocate(NativeTypes.INT8, NAME_BUF_MAX); 67 for (int i = 0; i < jpids.length; i++) { 68 int pid = jpids[i]; 69 // get the process name 70 proc_name(pid, nameBuf, NAME_BUF_MAX); 71 String procName = Pointer.toString(nameBuf); 72 // print pid and process name 73 System.out.printf("%d %s\n", pid, procName); 74 if (curProcPid == pid) { 75 foundCurProc = true; 76 System.out.println("Found the current process!"); 77 } 78 } 79 } 80 assertTrue(foundCurProc); 81 } 82 83 @Test 84 public void processInfoTest() { 85 long curProcPid = ProcessHandle.current().pid(); 86 try (Scope s = Scope.newNativeScope()) { 87 proc_info.proc_taskinfo ti = s.allocateStruct(proc_info.proc_taskinfo.class); 88 int taskInfoSize = (int)Struct.sizeof(proc_info.proc_taskinfo.class); 89 int resultSize = proc_pidinfo((int)curProcPid, PROC_PIDTASKINFO, 0, ti.ptr(), taskInfoSize); 90 assertEquals(resultSize, taskInfoSize); 91 System.out.println("total virtual memory size = " + ti.pti_virtual_size$get()); 92 System.out.println("resident memory size = " + ti.pti_resident_size$get()); 93 System.out.println("total time = " + ti.pti_total_user$get()); 94 } 95 } 96 } |