1 /*
   2  * Copyright (c) 2015, 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 
  25 /*
  26  * @test
  27  * @summary Run multiple processes with the same archive, ensure they share
  28  *
  29  * @requires vm.cds
  30  * @library /test/lib
  31  * @modules java.base/jdk.internal.misc
  32  *          java.management
  33  *          jdk.jartool/sun.tools.jar
  34  * @build sun.hotspot.WhiteBox
  35  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  36  * @compile test-classes/MultiProcClass.java
  37  * @run driver MultiProcessSharing
  38  */
  39 
  40 import java.io.File;
  41 import jdk.test.lib.Asserts;
  42 import jdk.test.lib.Platform;
  43 import jdk.test.lib.process.OutputAnalyzer;
  44 import sun.hotspot.WhiteBox;
  45 
  46 
  47 public class MultiProcessSharing {
  48     static String useWbJar;
  49     static String sharedClass1Jar;
  50     static boolean checkPmap = false;
  51 
  52     public static void main(String[] args) throws Exception {
  53         String wbJar = JarBuilder.build(true, "WhiteBox", "sun/hotspot/WhiteBox");
  54         useWbJar = "-Xbootclasspath/a:" + wbJar;
  55         sharedClass1Jar = JarBuilder.build("shared_class1", "MultiProcClass");
  56 
  57         // create an archive
  58         OutputAnalyzer out = TestCommon.dump(sharedClass1Jar,
  59             TestCommon.list("MultiProcClass"), useWbJar);
  60         TestCommon.checkDump(out);
  61 
  62         // determine whether OK to use pmap for extra test verification
  63         long myPid = ProcessHandle.current().pid();
  64         checkPmap = (Platform.isLinux() && (MultiProcClass.runPmap(myPid, false) == 0));
  65         System.out.println("MultiProcessSharing: checkPmap is " + checkPmap);
  66 
  67         // use an archive in several processes concurrently
  68         int numProcesses = 3;
  69         Thread[] threads = new Thread[numProcesses];
  70         ProcessHandler[] processHandlers = new ProcessHandler[numProcesses];
  71         for (int i = 0; i < numProcesses; i++) {
  72             processHandlers[i] = new ProcessHandler(i);
  73             threads[i] = new Thread(processHandlers[i]);
  74         }
  75 
  76         for (Thread t : threads) {
  77             t.start();
  78         }
  79 
  80         for (Thread t : threads) {
  81             try {
  82                 t.join();
  83             } catch (InterruptedException ie) {
  84                 throw ie;
  85             }
  86         }
  87 
  88         // check results
  89         for (ProcessHandler ph : processHandlers) {
  90             TestCommon.checkExec(ph.out);
  91             if (checkPmap && !TestCommon.isUnableToMap(ph.out)) {
  92                 checkPmapOutput(ph.out.getOutput());
  93             }
  94         }
  95     }
  96 
  97 
  98     static class ProcessHandler implements Runnable {
  99         int processNumber;
 100         OutputAnalyzer out;
 101 
 102         ProcessHandler(int processNumber) {
 103             this.processNumber = processNumber;
 104         }
 105 
 106         @Override
 107         public void run() {
 108             try {
 109                 out = TestCommon.exec(sharedClass1Jar,
 110                    "-XX:+UnlockDiagnosticVMOptions", "-XX:+WhiteBoxAPI", useWbJar,
 111                    "MultiProcClass", "" + processNumber, "" + checkPmap);
 112             } catch (Exception e) {
 113                 throw new RuntimeException("Error occurred when using archive, exec()" + e);
 114             }
 115         }
 116     }
 117 
 118 
 119     private static void checkPmapOutput(String stdio) {
 120         System.out.println("Checking pmap output ...");
 121         String[] lines = stdio.split("\n");
 122 
 123         boolean foundJsa = false;
 124         boolean foundReadOnlyJsaSection = false;
 125 
 126         for (String line : lines) {
 127             if (line.contains(TestCommon.getCurrentArchiveName()))
 128                 System.out.println(line);
 129                 foundJsa = true;
 130                 if (line.contains("r--")) {
 131                     foundReadOnlyJsaSection = true;
 132                 }
 133 
 134                 // On certain ARM platforms system maps r/o memory mapped files
 135                 // as r/x; see JDK-8145694 for details
 136                 if ( (Platform.isARM() || Platform.isAArch64()) && line.contains("r-x") ) {
 137                     foundReadOnlyJsaSection = true;
 138                 }
 139         }
 140 
 141         Asserts.assertTrue(foundJsa && foundReadOnlyJsaSection);
 142     }
 143 
 144 }