Path: blob/master/test/jdk/java/lang/ProcessBuilder/checkHandles/CheckHandles.java
41152 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.BufferedReader;24import java.io.IOException;25import java.io.InputStream;26import java.io.InputStreamReader;27import java.lang.ProcessHandle;2829import jdk.test.lib.util.FileUtils;3031/*32* @test33* @bug 823989334* @summary Verify that handles for processes that terminate do not accumulate35* @requires ((os.family == "windows") & (vm.compMode != "Xcomp"))36* @library /test/lib37* @run main/othervm/native -Xint CheckHandles38*/39public class CheckHandles {4041public static void main(String[] args) throws Exception {42System.out.println("mypid: " + ProcessHandle.current().pid());4344// Warmup the process launch mechanism and vm to stabilize the number of handles in use45int MAX_WARMUP = 20;46long prevCount = FileUtils.getProcessHandleCount();47for (int i = 0; i < MAX_WARMUP; i++) {48oneProcess();49System.gc(); // an opportunity to close unreferenced handles50sleep(10);5152long count = FileUtils.getProcessHandleCount();53if (count < 0)54throw new AssertionError("getProcessHandleCount failed");55System.out.println("warmup handle delta: " + (count - prevCount));56prevCount = count;57}58System.out.println("Warmup done");59System.out.println();6061prevCount = FileUtils.getProcessHandleCount();62long startHandles = prevCount;63long maxHandles = startHandles;64int MAX_SPAWN = 50;65for (int i = 0; i < MAX_SPAWN; i++) {66oneProcess();67System.gc(); // an opportunity to close unreferenced handles68sleep(10);6970long count = FileUtils.getProcessHandleCount();71if (count < 0)72throw new AssertionError("getProcessHandleCount failed");73System.out.println("handle delta: " + (count - prevCount));74prevCount = count;75maxHandles = Math.max(maxHandles, count);76}7778System.out.println("Processes started: " + MAX_SPAWN);79System.out.println("startHandles: " + startHandles);80System.out.println("maxHandles: " + maxHandles);8182final float ERROR_PERCENT = 10.0f; // allowable extra handles83final long ERROR_THRESHOLD = startHandles + Math.round(startHandles * ERROR_PERCENT / 100.0f);84if (maxHandles >= ERROR_THRESHOLD) {85throw new AssertionError("Handle use increased by more than " + ERROR_PERCENT + " percent.");86}87}8889/**90* Start a single process and consume its output.91*/92private static void oneProcess() {93try {9495Process testProcess = new ProcessBuilder("cmd", "/c", "dir").start();9697Thread outputConsumer = new Thread(() -> consumeStream(testProcess.getInputStream()));98outputConsumer.setDaemon(true);99outputConsumer.start();100Thread errorConsumer = new Thread(() -> consumeStream(testProcess.getErrorStream()));101errorConsumer.setDaemon(true);102errorConsumer.start();103104testProcess.waitFor();105outputConsumer.join();106errorConsumer.join();107} catch (IOException | InterruptedException e) {108e.printStackTrace();109throw new RuntimeException("Exception", e);110}111}112113private static void consumeStream(InputStream inputStream) {114BufferedReader reader = null;115try {116int lines = 0;117reader = new BufferedReader(new InputStreamReader(inputStream));118while (reader.readLine() != null) {119lines++;120}121} catch (IOException e) {122e.printStackTrace();123} finally {124if (reader != null) {125try {126reader.close();127} catch (IOException e) {128e.printStackTrace();129}130}131}132}133134private static void sleep(long millis) {135try {136Thread.sleep(millis);137} catch (InterruptedException ie) {138// ignore139}140}141}142143144