1 /*
   2  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  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 }
--- EOF ---