1 /*
   2  * Copyright (c) 2014, 2017, 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 
  25 import java.io.IOException;
  26 import java.time.Duration;
  27 import java.time.Instant;
  28 import java.util.ArrayList;
  29 import java.util.Arrays;
  30 import java.util.List;
  31 import java.util.Optional;
  32 import java.util.Set;
  33 import java.util.concurrent.ConcurrentHashMap;
  34 import java.util.concurrent.CountDownLatch;
  35 import java.util.concurrent.ExecutionException;
  36 import java.util.concurrent.TimeUnit;
  37 import java.util.stream.Collectors;
  38 import java.util.stream.Stream;
  39 
  40 import jdk.test.lib.Utils;
  41 import org.testng.Assert;
  42 import org.testng.TestNG;
  43 import org.testng.annotations.Test;
  44 
  45 /*
  46  * @test
  47  * @library /test/lib
  48  * @modules java.base/jdk.internal.misc
  49  *          jdk.management
  50  * @run testng/othervm TreeTest
  51  * @summary Test counting and JavaChild.spawning and counting of Processes.
  52  * @author Roger Riggs
  53  */
  54 public class TreeTest extends ProcessUtil {
  55     // Main can be used to run the tests from the command line with only testng.jar.
  56     @SuppressWarnings("raw_types")
  57     public static void main(String[] args) {
  58         Class<?>[] testclass = {TreeTest.class};
  59         TestNG testng = new TestNG();
  60         testng.setTestClasses(testclass);
  61         testng.run();
  62     }
  63 
  64     /**
  65      * Test counting and spawning and counting of Processes.
  66      */
  67     @Test
  68     public static void test1() {
  69         final int MAXCHILDREN = 2;
  70         List<JavaChild> spawned = new ArrayList<>();
  71 
  72         try {
  73             ProcessHandle self = ProcessHandle.current();
  74 
  75             printf("self pid: %d%n", self.pid());
  76             printDeep(self, "");
  77 
  78             for (int i = 0; i < MAXCHILDREN; i++) {
  79                 // spawn and wait for instructions
  80                 spawned.add(JavaChild.spawnJavaChild("pid", "stdin"));
  81             }
  82 
  83             // Verify spawned Process is in list of children
  84             final List<ProcessHandle> initialChildren = getChildren(self);
  85             spawned.stream()
  86                     .map(Process::toHandle)
  87                     .filter(p -> !initialChildren.contains(p))
  88                     .forEach(p -> Assert.fail("Spawned process missing from children: " + p));
  89 
  90             // Send exit command to each spawned Process
  91             spawned.forEach(p -> {
  92                     try {
  93                         p.sendAction("exit", "");
  94                     } catch (IOException ex) {
  95                         Assert.fail("IOException in sendAction", ex);
  96                     }
  97                 });
  98 
  99             // Wait for each Process to exit
 100             spawned.forEach(p -> {
 101                     do {
 102                         try {
 103                             Assert.assertEquals(p.waitFor(), 0, "exit status incorrect");
 104                             break;
 105                         } catch (InterruptedException  ex) {
 106                             continue; // Retry
 107                         }
 108                     } while (true);
 109                 });
 110 
 111             // Verify that ProcessHandle.isAlive sees each of them as not alive
 112             for (Process p : spawned) {
 113                 ProcessHandle ph = p.toHandle();
 114                 Assert.assertFalse(ph.isAlive(),
 115                         "ProcessHandle.isAlive for exited process: " + ph);
 116             }
 117 
 118             // Verify spawned processes are not visible as children
 119             final List<ProcessHandle> afterChildren = getChildren(self);
 120             spawned.stream()
 121                     .map(Process::toHandle)
 122                     .filter(p -> afterChildren.contains(p))
 123                     .forEach(p -> Assert.fail("Spawned process missing from children: " + p));
 124 
 125         } catch (IOException ioe) {
 126             Assert.fail("unable to spawn process", ioe);
 127         } finally {
 128             // Cleanup any left over processes
 129             spawned.stream()
 130                     .map(Process::toHandle)
 131                     .filter(ProcessHandle::isAlive)
 132                     .forEach(ph -> {
 133                         printDeep(ph, "test1 cleanup: ");
 134                         ph.destroyForcibly();
 135                     });
 136         }
 137     }
 138 
 139     /**
 140      * Test counting and spawning and counting of Processes.
 141      */
 142     @Test
 143     public static void test2() {
 144         try {
 145             ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>();
 146 
 147             ProcessHandle self = ProcessHandle.current();
 148             List<ProcessHandle> initialChildren = getChildren(self);
 149             long count = initialChildren.size();
 150             if (count > 0) {
 151                 initialChildren.forEach(p -> printDeep(p, "test2 initial unexpected: "));
 152             }
 153 
 154             JavaChild p1 = JavaChild.spawnJavaChild("stdin");
 155             ProcessHandle p1Handle = p1.toHandle();
 156             printf("  p1 pid: %d%n", p1.pid());
 157 
 158             // Gather the PIDs from the output of the spawing process
 159             p1.forEachOutputLine((s) -> {
 160                 String[] split = s.trim().split(" ");
 161                 if (split.length == 3 && split[1].equals("spawn")) {
 162                     Long child = Long.valueOf(split[2]);
 163                     Long parent = Long.valueOf(split[0].split(":")[0]);
 164                     processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get());
 165                 }
 166             });
 167 
 168             int spawnNew = 3;
 169             p1.sendAction("spawn", spawnNew, "stdin");
 170 
 171             // Wait for direct children to be created and save the list
 172             List<ProcessHandle> subprocesses = waitForAllChildren(p1Handle, spawnNew);
 173             Optional<Instant> p1Start = p1Handle.info().startInstant();
 174             for (ProcessHandle ph : subprocesses) {
 175                 Assert.assertTrue(ph.isAlive(), "Child should be alive: " + ph);
 176                 // Verify each child was started after the parent
 177                 ph.info().startInstant()
 178                         .ifPresent(childStart -> p1Start.ifPresent(parentStart -> {
 179                             Assert.assertFalse(childStart.isBefore(parentStart),
 180                                     String.format("Child process started before parent: child: %s, parent: %s",
 181                                             childStart, parentStart));
 182                         }));
 183             }
 184 
 185             // Each child spawns two processes and waits for commands
 186             int spawnNewSub = 2;
 187             p1.sendAction("child", "spawn", spawnNewSub, "stdin");
 188 
 189             // Poll until all 9 child processes exist or the timeout is reached
 190             int expected = 9;
 191             long timeout = Utils.adjustTimeout(60L);
 192             Instant endTimeout = Instant.now().plusSeconds(timeout);
 193             do {
 194                 Thread.sleep(200L);
 195                 printf(" subprocess count: %d, waiting for %d%n", processes.size(), expected);
 196             } while (processes.size() < expected &&
 197                     Instant.now().isBefore(endTimeout));
 198 
 199             if (processes.size() < expected) {
 200                 printf("WARNING: not all children have been started. Can't complete test.%n");
 201                 printf("         You can try to increase the timeout or%n");
 202                 printf("         you can try to use a faster VM (i.e. not a debug version).%n");
 203             }
 204 
 205             // show the complete list of children (for debug)
 206             List<ProcessHandle> descendants = getDescendants(p1Handle);
 207             printf(" descendants:  %s%n",
 208                     descendants.stream().map(p -> p.pid())
 209                            .collect(Collectors.toList()));
 210 
 211             // Verify that all spawned children show up in the descendants  List
 212             processes.forEach((p, parent) -> {
 213                 Assert.assertEquals(p.isAlive(), true, "Child should be alive: " + p);
 214                 Assert.assertTrue(descendants.contains(p), "Spawned child should be listed in descendants: " + p);
 215             });
 216 
 217             // Closing JavaChild's InputStream will cause all children to exit
 218             p1.getOutputStream().close();
 219 
 220             for (ProcessHandle p : descendants) {
 221                 try {
 222                     p.onExit().get();       // wait for the child to exit
 223                 } catch (ExecutionException e) {
 224                     Assert.fail("waiting for process to exit", e);
 225                 }
 226             }
 227             p1.waitFor();           // wait for spawned process to exit
 228 
 229             // Verify spawned processes are no longer alive
 230             processes.forEach((ph, parent) -> Assert.assertFalse(ph.isAlive(),
 231                             "process should not be alive: " + ph));
 232         } catch (IOException | InterruptedException t) {
 233             t.printStackTrace();
 234             throw new RuntimeException(t);
 235         }
 236     }
 237 
 238     /**
 239      * Test destroy of processes.
 240      * A JavaChild is started and it starts three children.
 241      * Each one is then checked to be alive and listed by descendants
 242      * and forcibly destroyed.
 243      * After they exit they should no longer be listed by descendants.
 244      */
 245     @Test
 246     public static void test3() {
 247         ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>();
 248 
 249         try {
 250             ProcessHandle self = ProcessHandle.current();
 251 
 252             JavaChild p1 = JavaChild.spawnJavaChild("stdin");
 253             ProcessHandle p1Handle = p1.toHandle();
 254             printf(" p1: %s%n", p1.pid());
 255 
 256             int newChildren = 3;
 257             CountDownLatch spawnCount = new CountDownLatch(newChildren);
 258             // Spawn children and have them wait
 259             p1.sendAction("spawn", newChildren, "stdin");
 260 
 261             // Gather the PIDs from the output of the spawning process
 262             p1.forEachOutputLine((s) -> {
 263                 String[] split = s.trim().split(" ");
 264                 if (split.length == 3 && split[1].equals("spawn")) {
 265                     Long child = Long.valueOf(split[2]);
 266                     Long parent = Long.valueOf(split[0].split(":")[0]);
 267                     processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get());
 268                     spawnCount.countDown();
 269                 }
 270             });
 271 
 272             // Wait for all the subprocesses to be listed as started
 273             Assert.assertTrue(spawnCount.await(Utils.adjustTimeout(30L), TimeUnit.SECONDS),
 274                     "Timeout waiting for processes to start");
 275 
 276             // Debugging; list descendants that are not expected in processes
 277             List<ProcessHandle> descendants = ProcessUtil.getDescendants(p1Handle);
 278             long count = descendants.stream()
 279                     .filter(ph -> !processes.containsKey(ph))
 280                     .count();
 281             if (count > 0) {
 282                 descendants.stream()
 283                     .filter(ph -> !processes.containsKey(ph))
 284                     .forEach(ph1 -> ProcessUtil.printProcess(ph1, "Extra process: "));
 285                 ProcessUtil.logTaskList();
 286                 Assert.assertEquals(0, count, "Extra processes in descendants");
 287             }
 288 
 289             // Verify that all spawned children are alive, show up in the descendants list
 290             // then destroy them
 291             processes.forEach((p, parent) -> {
 292                 Assert.assertEquals(p.isAlive(), true, "Child should be alive: " + p);
 293                 Assert.assertTrue(descendants.contains(p), "Spawned child should be listed in descendants: " + p);
 294                 p.destroyForcibly();
 295             });
 296             Assert.assertEquals(processes.size(), newChildren, "Wrong number of children");
 297 
 298             // Wait for each of the processes to exit
 299             processes.forEach((p, parent) ->  {
 300                 for (long retries = Utils.adjustTimeout(100L); retries > 0 ; retries--) {
 301                     if (!p.isAlive()) {
 302                         return;                 // not alive, go on to the next
 303                     }
 304                     // Wait a bit and retry
 305                     try {
 306                         Thread.sleep(100L);
 307                     } catch (InterruptedException ie) {
 308                         // try again
 309                     }
 310                 }
 311                 printf("Timeout waiting for exit of pid %s, parent: %s, info: %s%n",
 312                         p, parent, p.info());
 313                 Assert.fail("Process still alive: " + p);
 314             });
 315             p1.destroyForcibly();
 316             p1.waitFor();
 317 
 318             // Verify that none of the spawned children are still listed by descendants
 319             List<ProcessHandle> remaining = getDescendants(self);
 320             Assert.assertFalse(remaining.remove(p1Handle), "Child p1 should have exited");
 321             remaining = remaining.stream().filter(processes::containsKey).collect(Collectors.toList());
 322             Assert.assertEquals(remaining.size(), 0, "Subprocess(es) should have exited: " + remaining);
 323 
 324         } catch (IOException ioe) {
 325             Assert.fail("Spawn of subprocess failed", ioe);
 326         } catch (InterruptedException inte) {
 327             Assert.fail("InterruptedException", inte);
 328         } finally {
 329             processes.forEach((p, parent) -> {
 330                 if (p.isAlive()) {
 331                     ProcessUtil.printProcess(p);
 332                     p.destroyForcibly();
 333                 }
 334             });
 335         }
 336     }
 337 
 338     /**
 339      * Test (Not really a test) that dumps the list of all Processes.
 340      */
 341     @Test
 342     public static void test4() {
 343         printf("    Parent     Child  Info%n");
 344         Stream<ProcessHandle> s = ProcessHandle.allProcesses();
 345         ProcessHandle[] processes = s.toArray(ProcessHandle[]::new);
 346         int len = processes.length;
 347         ProcessHandle[] parent = new ProcessHandle[len];
 348         Set<ProcessHandle> processesSet =
 349                 Arrays.stream(processes).collect(Collectors.toSet());
 350         Integer[] sortindex = new Integer[len];
 351         for (int i = 0; i < len; i++) {
 352             sortindex[i] = i;
 353          }
 354         for (int i = 0; i < len; i++) {
 355             parent[sortindex[i]] = processes[sortindex[i]].parent().orElse(null);
 356         }
 357         Arrays.sort(sortindex, (i1, i2) -> {
 358             int cmp = Long.compare((parent[i1] == null ? 0L : parent[i1].pid()),
 359                     (parent[i2] == null ? 0L : parent[i2].pid()));
 360             if (cmp == 0) {
 361                 cmp = Long.compare((processes[i1] == null ? 0L : processes[i1].pid()),
 362                         (processes[i2] == null ? 0L : processes[i2].pid()));
 363             }
 364             return cmp;
 365         });
 366         boolean fail = false;
 367         for (int i = 0; i < len; i++) {
 368             ProcessHandle p = processes[sortindex[i]];
 369             ProcessHandle p_parent = parent[sortindex[i]];
 370             ProcessHandle.Info info = p.info();
 371             String indent = "    ";
 372             if (p_parent != null) {
 373                 if (!processesSet.contains(p_parent)) {
 374                     fail = true;
 375                     indent = "*** ";
 376                 }
 377             }
 378             printf("%s %7s, %7s, %s%n", indent, p_parent, p, info);
 379         }
 380         Assert.assertFalse(fail, "Parents missing from all Processes");
 381 
 382     }
 383 
 384     /**
 385      * A test for scale; launch a large number (14) of subprocesses.
 386      */
 387     @Test
 388     public static void test5() {
 389         ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>();
 390 
 391         int factor = 2;
 392         JavaChild p1 = null;
 393         Instant start = Instant.now();
 394         try {
 395             p1 = JavaChild.spawnJavaChild("stdin");
 396             ProcessHandle p1Handle = p1.toHandle();
 397 
 398             printf("Spawning %d x %d x %d processes, pid: %d%n",
 399                     factor, factor, factor, p1.pid());
 400 
 401             // Start the first tier of subprocesses
 402             p1.sendAction("spawn", factor, "stdin");
 403 
 404             // Start the second tier of subprocesses
 405             p1.sendAction("child", "spawn", factor, "stdin");
 406 
 407             // Start the third tier of subprocesses
 408             p1.sendAction("child", "child", "spawn", factor, "stdin");
 409 
 410             int newChildren = factor * (1 + factor * (1 + factor));
 411             CountDownLatch spawnCount = new CountDownLatch(newChildren);
 412 
 413             // Gather the PIDs from the output of the spawning process
 414             p1.forEachOutputLine((s) -> {
 415                 String[] split = s.trim().split(" ");
 416                 if (split.length == 3 && split[1].equals("spawn")) {
 417                     Long child = Long.valueOf(split[2]);
 418                     Long parent = Long.valueOf(split[0].split(":")[0]);
 419                     processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get());
 420                     spawnCount.countDown();
 421                 }
 422             });
 423 
 424             // Wait for all the subprocesses to be listed as started
 425             Assert.assertTrue(spawnCount.await(Utils.adjustTimeout(30L), TimeUnit.SECONDS),
 426                     "Timeout waiting for processes to start");
 427 
 428             // Debugging; list descendants that are not expected in processes
 429             List<ProcessHandle> descendants = ProcessUtil.getDescendants(p1Handle);
 430             long count = descendants.stream()
 431                     .filter(ph -> !processes.containsKey(ph))
 432                     .count();
 433             if (count > 0) {
 434                 descendants.stream()
 435                     .filter(ph -> !processes.containsKey(ph))
 436                     .forEach(ph1 -> ProcessUtil.printProcess(ph1, "Extra process: "));
 437                 ProcessUtil.logTaskList();
 438                 Assert.assertEquals(0, count, "Extra processes in descendants");
 439             }
 440 
 441             Assert.assertEquals(getChildren(p1Handle).size(),
 442                     factor, "expected direct children");
 443             count = getDescendants(p1Handle).size();
 444             long totalChildren = factor * factor * factor + factor * factor + factor;
 445             Assert.assertTrue(count >= totalChildren,
 446                     "expected at least " + totalChildren + ", actual: " + count);
 447 
 448             List<ProcessHandle> subprocesses = getDescendants(p1Handle);
 449             printf(" descendants:  %s%n",
 450                     subprocesses.stream().map(p -> p.pid())
 451                     .collect(Collectors.toList()));
 452 
 453             p1.getOutputStream().close();  // Close stdin for the controlling p1
 454             p1.waitFor();
 455         } catch (InterruptedException | IOException ex) {
 456             Assert.fail("Unexpected Exception", ex);
 457         } finally {
 458             printf("Duration: %s%n", Duration.between(start, Instant.now()));
 459             if (p1 != null) {
 460                 p1.destroyForcibly();
 461             }
 462             processes.forEach((p, parent) -> {
 463                 if (p.isAlive()) {
 464                     ProcessUtil.printProcess(p, "Process Cleanup: ");
 465                     p.destroyForcibly();
 466                 }
 467             });
 468         }
 469     }
 470 
 471 }