< prev index next >

test/java/lang/ProcessHandle/InfoTest.java

Print this page
rev 12467 : 8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX


 119             try (BufferedReader lines = p1.outputReader()) {
 120                 Duration lastCpu = Duration.ofMillis(0L);
 121                 for (int j = 0; j < 10; j++) {
 122 
 123                     p1.sendAction("cpuloop", cpuLoopTime);
 124                     p1.sendAction("cputime", "");
 125 
 126                     // Read cputime from child
 127                     Duration childCpuTime = null;
 128                     // Read lines from the child until the result from cputime is returned
 129                     String s;
 130                     while ((s = lines.readLine()) != null) {
 131                         String[] split = s.trim().split(" ");
 132                         if (split.length == 3 && split[1].equals("cputime")) {
 133                             long nanos = Long.valueOf(split[2]);
 134                             childCpuTime = Duration.ofNanos(nanos);
 135                             break;      // found the result we're looking for
 136                         }
 137                     }
 138 
 139 










 140                     ProcessHandle.Info info = p1.info();
 141                     System.out.printf(" info: %s%n", info);
 142 
 143                     if (info.user().isPresent()) {
 144                         String user = info.user().get();
 145                         Assert.assertNotNull(user, "User name");
 146                         Assert.assertEquals(user, whoami, "User name");
 147                     }
 148 
 149                     Optional<String> command = info.command();
 150                     if (command.isPresent()) {
 151                         String javaExe = System.getProperty("test.jdk") +
 152                                 File.separator + "bin" + File.separator + "java";
 153                         String expected = Platform.isWindows() ? javaExe + ".exe" : javaExe;
 154                         Path expectedPath = Paths.get(expected);
 155                         Path actualPath = Paths.get(command.get());
 156                         Assert.assertTrue(Files.isSameFile(expectedPath, actualPath),
 157                                 "Command: expected: " + javaExe + ", actual: " + command.get());
 158                     }
 159 
 160                     if (info.arguments().isPresent()) {
 161                         String[] args = info.arguments().get();
 162 
 163                         if (Platform.isLinux() || Platform.isOSX()) {
 164                             int offset = args.length - extraArgs.length;
 165                             for (int i = 0; i < extraArgs.length; i++) {
 166                                 Assert.assertEquals(args[offset + i], extraArgs[i],
 167                                         "Actual argument mismatch, index: " + i);
 168                             }
 169                         } else if (Platform.isSolaris()) {
 170                             Assert.assertEquals(args.length, 1,
 171                                     "Expected argument list length: 1");
 172                             Assert.assertNotNull(args[0],
 173                                     "Expected an argument");
 174                         } else {



 175                             System.out.printf("No argument test for OS: %s%n", Platform.getOsName());
 176                         }
 177 
 178                         // Now check that the first argument is not the same as the executed command
 179                         if (args.length > 0) {
 180                             Assert.assertNotEquals(args[0], command,
 181                                     "First argument should not be the executable: args[0]: "
 182                                             + args[0] + ", command: " + command);
 183                         }
 184                     }
 185 
 186                     if (info.totalCpuDuration().isPresent()) {
 187                         Duration totalCPU = info.totalCpuDuration().get();
 188                         Duration epsilon = Duration.ofMillis(200L);
 189                         if (childCpuTime != null) {
 190                             System.out.printf(" info.totalCPU: %s, childCpuTime: %s, diff: %s%n",
 191                                     totalCPU.toNanos(), childCpuTime.toNanos(),
 192                                     childCpuTime.toNanos() - totalCPU.toNanos());
 193                             Assert.assertTrue(checkEpsilon(childCpuTime, totalCPU, epsilon),
 194                                     childCpuTime + " should be within " +


 225     @Test
 226     public static void test3() {
 227         try {
 228             for (int sleepTime : Arrays.asList(1, 2)) {
 229                 Process p = spawn("sleep", String.valueOf(sleepTime));
 230                 ProcessHandle.Info info = p.info();
 231                 System.out.printf(" info: %s%n", info);
 232 
 233                 if (info.user().isPresent()) {
 234                     String user = info.user().get();
 235                     Assert.assertNotNull(user);
 236                     Assert.assertEquals(user, whoami);
 237                 }
 238                 if (info.command().isPresent()) {
 239                     String command = info.command().get();
 240                     String expected = Platform.isWindows() ? "sleep.exe" : "sleep";
 241                     Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
 242                             expected + "\', actual: " + command);
 243 
 244                     // Verify the command exists and is executable



 245                     File exe = new File(command);
 246                     Assert.assertTrue(exe.exists(), "command must exist: " + exe);
 247                     Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
 248                 }

 249                 if (info.arguments().isPresent()) {
 250                     String[] args = info.arguments().get();
 251                     if (args.length > 0) {
 252                         Assert.assertEquals(args[0], String.valueOf(sleepTime));
 253                     }
 254                 }
 255                 Assert.assertTrue(p.waitFor(15, TimeUnit.SECONDS));
 256             }
 257         } catch (IOException | InterruptedException ex) {
 258             ex.printStackTrace(System.out);
 259         } finally {
 260             // Destroy any children that still exist
 261             ProcessUtil.destroyProcessTree(ProcessHandle.current());
 262         }
 263     }
 264 
 265     /**
 266      * Cross check the cputime reported from java.management with that for the current process.
 267      */
 268     @Test
 269     public static void test4() {
 270         Duration myCputime1 = ProcessUtil.MXBeanCpuTime();
 271 












 272         Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration();
 273 
 274         Duration myCputime2 = ProcessUtil.MXBeanCpuTime();
 275 





 276         Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration();
 277 
 278         if (dur1.isPresent() && dur2.isPresent()) {
 279             Duration total1 = dur1.get();
 280             Duration total2 = dur2.get();
 281             System.out.printf(" total1 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
 282                     Objects.toString(total1), myCputime1, myCputime1.minus(total1));
 283             System.out.printf(" total2 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
 284                     Objects.toString(total2), myCputime2, myCputime2.minus(total2));
 285 
 286             Duration epsilon = Duration.ofMillis(200L);      // Epsilon is 200ms.
 287             Assert.assertTrue(checkEpsilon(myCputime1, myCputime2, epsilon),
 288                     myCputime1.toNanos() + " should be within " + epsilon
 289                             + " of " + myCputime2.toNanos());
 290             Assert.assertTrue(checkEpsilon(total1, total2, epsilon),
 291                     total1.toNanos() + " should be within " + epsilon
 292                             + " of " + total2.toNanos());
 293             Assert.assertTrue(checkEpsilon(myCputime1, total1, epsilon),
 294                     myCputime1.toNanos() + " should be within " + epsilon
 295                             + " of " + total1.toNanos());




 119             try (BufferedReader lines = p1.outputReader()) {
 120                 Duration lastCpu = Duration.ofMillis(0L);
 121                 for (int j = 0; j < 10; j++) {
 122 
 123                     p1.sendAction("cpuloop", cpuLoopTime);
 124                     p1.sendAction("cputime", "");
 125 
 126                     // Read cputime from child
 127                     Duration childCpuTime = null;
 128                     // Read lines from the child until the result from cputime is returned
 129                     String s;
 130                     while ((s = lines.readLine()) != null) {
 131                         String[] split = s.trim().split(" ");
 132                         if (split.length == 3 && split[1].equals("cputime")) {
 133                             long nanos = Long.valueOf(split[2]);
 134                             childCpuTime = Duration.ofNanos(nanos);
 135                             break;      // found the result we're looking for
 136                         }
 137                     }
 138 
 139                     if (Platform.isAix()) {
 140                         // Unfortunately, on AIX the usr/sys times reported through
 141                         // /proc/<pid>/psinfo which are used by ProcessHandle.Info
 142                         // are running slow compared to the corresponding times reported
 143                         // by the times()/getrusage() system calls which are used by
 144                         // OperatingSystemMXBean.getProcessCpuTime() and returned by
 145                         // the JavaChild for the "cputime" command.
 146                         // This is because /proc/<pid>/status is only updated once a second.
 147                         // So we better wait a little bit to get plausible values here.
 148                         Thread.sleep(1000);
 149                     }
 150                     ProcessHandle.Info info = p1.info();
 151                     System.out.printf(" info: %s%n", info);
 152 
 153                     if (info.user().isPresent()) {
 154                         String user = info.user().get();
 155                         Assert.assertNotNull(user, "User name");
 156                         Assert.assertEquals(user, whoami, "User name");
 157                     }
 158 
 159                     Optional<String> command = info.command();
 160                     if (command.isPresent()) {
 161                         String javaExe = System.getProperty("test.jdk") +
 162                                 File.separator + "bin" + File.separator + "java";
 163                         String expected = Platform.isWindows() ? javaExe + ".exe" : javaExe;
 164                         Path expectedPath = Paths.get(expected);
 165                         Path actualPath = Paths.get(command.get());
 166                         Assert.assertTrue(Files.isSameFile(expectedPath, actualPath),
 167                                 "Command: expected: " + javaExe + ", actual: " + command.get());
 168                     }
 169 
 170                     if (info.arguments().isPresent()) {
 171                         String[] args = info.arguments().get();
 172 
 173                         if (Platform.isLinux() || Platform.isOSX()) {
 174                             int offset = args.length - extraArgs.length;
 175                             for (int i = 0; i < extraArgs.length; i++) {
 176                                 Assert.assertEquals(args[offset + i], extraArgs[i],
 177                                         "Actual argument mismatch, index: " + i);
 178                             }





 179                         } else {
 180                             // Arguments on Solaris and AIX come from /proc/pid/psinfo and
 181                             // are usually truncated to 80 characters so there's not much
 182                             // we can check here.
 183                             System.out.printf("No argument test for OS: %s%n", Platform.getOsName());
 184                         }
 185 
 186                         // Now check that the first argument is not the same as the executed command
 187                         if (args.length > 0) {
 188                             Assert.assertNotEquals(args[0], command,
 189                                     "First argument should not be the executable: args[0]: "
 190                                             + args[0] + ", command: " + command);
 191                         }
 192                     }
 193 
 194                     if (info.totalCpuDuration().isPresent()) {
 195                         Duration totalCPU = info.totalCpuDuration().get();
 196                         Duration epsilon = Duration.ofMillis(200L);
 197                         if (childCpuTime != null) {
 198                             System.out.printf(" info.totalCPU: %s, childCpuTime: %s, diff: %s%n",
 199                                     totalCPU.toNanos(), childCpuTime.toNanos(),
 200                                     childCpuTime.toNanos() - totalCPU.toNanos());
 201                             Assert.assertTrue(checkEpsilon(childCpuTime, totalCPU, epsilon),
 202                                     childCpuTime + " should be within " +


 233     @Test
 234     public static void test3() {
 235         try {
 236             for (int sleepTime : Arrays.asList(1, 2)) {
 237                 Process p = spawn("sleep", String.valueOf(sleepTime));
 238                 ProcessHandle.Info info = p.info();
 239                 System.out.printf(" info: %s%n", info);
 240 
 241                 if (info.user().isPresent()) {
 242                     String user = info.user().get();
 243                     Assert.assertNotNull(user);
 244                     Assert.assertEquals(user, whoami);
 245                 }
 246                 if (info.command().isPresent()) {
 247                     String command = info.command().get();
 248                     String expected = Platform.isWindows() ? "sleep.exe" : "sleep";
 249                     Assert.assertTrue(command.endsWith(expected), "Command: expected: \'" +
 250                             expected + "\', actual: " + command);
 251 
 252                     // Verify the command exists and is executable
 253                     if (!Platform.isAix()) {
 254                         // On Aix, Info.command() only returns arg[0] as command which
 255                         // doesn't necessarily contains the full path to the executable.
 256                         File exe = new File(command);
 257                         Assert.assertTrue(exe.exists(), "command must exist: " + exe);
 258                         Assert.assertTrue(exe.canExecute(), "command must be executable: " + exe);
 259                     }
 260                 }
 261                 if (info.arguments().isPresent()) {
 262                     String[] args = info.arguments().get();
 263                     if (args.length > 0) {
 264                         Assert.assertEquals(args[0], String.valueOf(sleepTime));
 265                     }
 266                 }
 267                 Assert.assertTrue(p.waitFor(15, TimeUnit.SECONDS));
 268             }
 269         } catch (IOException | InterruptedException ex) {
 270             ex.printStackTrace(System.out);
 271         } finally {
 272             // Destroy any children that still exist
 273             ProcessUtil.destroyProcessTree(ProcessHandle.current());
 274         }
 275     }
 276 
 277     /**
 278      * Cross check the cputime reported from java.management with that for the current process.
 279      */
 280     @Test
 281     public static void test4() {
 282         Duration myCputime1 = ProcessUtil.MXBeanCpuTime();
 283 
 284         if (Platform.isAix()) {
 285             // Unfortunately, on AIX the usr/sys times reported through
 286             // /proc/<pid>/psinfo which are used by ProcessHandle.Info
 287             // are running slow compared to the corresponding times reported
 288             // by the times()/getrusage() system calls which are used by
 289             // OperatingSystemMXBean.getProcessCpuTime() and returned by
 290             // the JavaChild for the "cputime" command.
 291             // So we better wait a little bit to get plausible values here.
 292             try {
 293                 Thread.sleep(1000);
 294             } catch (InterruptedException ex) {}
 295         }
 296         Optional<Duration> dur1 = ProcessHandle.current().info().totalCpuDuration();
 297 
 298         Duration myCputime2 = ProcessUtil.MXBeanCpuTime();
 299 
 300         if (Platform.isAix()) {
 301             try {
 302                 Thread.sleep(1000);
 303             } catch (InterruptedException ex) {}
 304         }
 305         Optional<Duration> dur2 = ProcessHandle.current().info().totalCpuDuration();
 306 
 307         if (dur1.isPresent() && dur2.isPresent()) {
 308             Duration total1 = dur1.get();
 309             Duration total2 = dur2.get();
 310             System.out.printf(" total1 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
 311                     Objects.toString(total1), myCputime1, myCputime1.minus(total1));
 312             System.out.printf(" total2 vs. mbean: %s, getProcessCpuTime: %s, diff: %s%n",
 313                     Objects.toString(total2), myCputime2, myCputime2.minus(total2));
 314 
 315             Duration epsilon = Duration.ofMillis(200L);      // Epsilon is 200ms.
 316             Assert.assertTrue(checkEpsilon(myCputime1, myCputime2, epsilon),
 317                     myCputime1.toNanos() + " should be within " + epsilon
 318                             + " of " + myCputime2.toNanos());
 319             Assert.assertTrue(checkEpsilon(total1, total2, epsilon),
 320                     total1.toNanos() + " should be within " + epsilon
 321                             + " of " + total2.toNanos());
 322             Assert.assertTrue(checkEpsilon(myCputime1, total1, epsilon),
 323                     myCputime1.toNanos() + " should be within " + epsilon
 324                             + " of " + total1.toNanos());


< prev index next >