Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/Pack200Test.java
38833 views
/*1* Copyright (c) 2007, 2013, 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*/222324import java.util.*;25import java.io.*;26import java.lang.management.ManagementFactory;27import java.lang.management.MemoryMXBean;28import java.util.jar.*;2930/*31* @test32* @bug 6521334 6712743 800790233* @requires (sun.arch.data.model == "64" & os.maxMemory >= 4g)34* @summary check for memory leaks, test general packer/unpacker functionality\35* using native and java unpackers36* @compile -XDignore.symbol.file Utils.java Pack200Test.java37* @run main/othervm/timeout=1200 -Xmx512m Pack200Test38* @author ksrini39*/4041/**42* Tests the packing/unpacking via the APIs.43*/44public class Pack200Test {4546private static ArrayList <File> jarList = new ArrayList<File>();47static final MemoryMXBean mmxbean = ManagementFactory.getMemoryMXBean();48static final long m0 = getUsedMemory();49static final int LEAK_TOLERANCE = 21000; // OS and GC related variations.5051/** Creates a new instance of Pack200Test */52private Pack200Test() {}5354static long getUsedMemory() {55mmxbean.gc();56mmxbean.gc();57mmxbean.gc();58return mmxbean.getHeapMemoryUsage().getUsed()/1024;59}6061private static void leakCheck() throws Exception {62long diff = getUsedMemory() - m0;63System.out.println(" Info: memory diff = " + diff + "K");64if ( diff > LEAK_TOLERANCE) {65throw new Exception("memory leak detected " + diff);66}67}6869private static void doPackUnpack() throws IOException {70for (File in : jarList) {71JarOutputStream javaUnpackerStream = null;72JarOutputStream nativeUnpackerStream = null;73JarFile jarFile = null;74try {75jarFile = new JarFile(in);7677// Write out to a jtreg scratch area78File packFile = new File(in.getName() + Utils.PACK_FILE_EXT);7980System.out.println("Packing [" + in.toString() + "]");81// Call the packer82Utils.pack(jarFile, packFile);83jarFile.close();84leakCheck();8586System.out.println(" Unpacking using java unpacker");87File javaUnpackedJar = new File("java-" + in.getName());88// Write out to current directory, jtreg will setup a scratch area89javaUnpackerStream = new JarOutputStream(90new FileOutputStream(javaUnpackedJar));91Utils.unpackj(packFile, javaUnpackerStream);92javaUnpackerStream.close();93System.out.println(" Testing...java unpacker");94leakCheck();95// Ok we have unpacked the file, lets test it.96Utils.doCompareVerify(in.getAbsoluteFile(), javaUnpackedJar);9798System.out.println(" Unpacking using native unpacker");99// Write out to current directory100File nativeUnpackedJar = new File("native-" + in.getName());101nativeUnpackerStream = new JarOutputStream(102new FileOutputStream(nativeUnpackedJar));103Utils.unpackn(packFile, nativeUnpackerStream);104nativeUnpackerStream.close();105System.out.println(" Testing...native unpacker");106leakCheck();107// the unpackers (native and java) should produce identical bits108// so we use use bit wise compare, the verification compare is109// very expensive wrt. time.110Utils.doCompareBitWise(javaUnpackedJar, nativeUnpackedJar);111System.out.println("Done.");112} catch (Exception e) {113throw new RuntimeException(e);114} finally {115Utils.close(nativeUnpackerStream);116Utils.close(javaUnpackerStream);117Utils.close((Closeable) jarFile);118}119}120Utils.cleanup(); // cleanup artifacts, if successful run121}122123/**124* @param args the command line arguments125*/126public static void main(String[] args) throws IOException {127// select the jars carefully, adding more jars will increase the128// testing time, especially for jprt.129jarList.add(Utils.locateJar("tools.jar"));130jarList.add(Utils.locateJar("rt.jar"));131jarList.add(Utils.locateJar("golden.jar"));132System.out.println(jarList);133doPackUnpack();134}135}136137138