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 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 }
--- EOF ---