From 901e60d8270ff49d4cf6b4ad2212f894bf55e7c4 Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Wed, 30 Apr 2014 13:37:00 +0400 Subject: [PATCH] Cleanup (arrangement; aggressive logging) --- .../src/com/intellij/util/lang/ClassPath.java | 201 +++++++++--------- .../intellij/util/io/zip/ReorderJarsTest.java | 4 +- 2 files changed, 103 insertions(+), 102 deletions(-) diff --git a/platform/util/src/com/intellij/util/lang/ClassPath.java b/platform/util/src/com/intellij/util/lang/ClassPath.java index e15cf8df2150..2b1e99c7e792 100644 --- a/platform/util/src/com/intellij/util/lang/ClassPath.java +++ b/platform/util/src/com/intellij/util/lang/ClassPath.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2013 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,7 +22,7 @@ import com.intellij.openapi.util.text.StringUtil; import com.intellij.util.SmartList; import com.intellij.util.containers.HashMap; import com.intellij.util.containers.Stack; -import org.jetbrains.annotations.NonNls; +import com.intellij.util.io.URLUtil; import org.jetbrains.annotations.Nullable; import sun.misc.Resource; @@ -35,76 +35,20 @@ import java.net.URL; import java.util.*; public class ClassPath { - private final Stack myUrls = new Stack(); - private final ArrayList myLoaders = new ArrayList(); - private final HashMap myLoadersMap = new HashMap(); - private final ClasspathCache myCache = new ClasspathCache(); + private static final ResourceStringLoaderIterator ourCheckedIterator = new ResourceStringLoaderIterator(true); + private static final ResourceStringLoaderIterator ourUncheckedIterator = new ResourceStringLoaderIterator(false); + private static final LoaderCollector ourLoaderCollector = new LoaderCollector(); - @NonNls private static final String FILE_PROTOCOL = "file"; - private static final boolean myDebugTime = false; - private static final boolean ourDumpOrder = "true".equals(System.getProperty("idea.dump.order")); -// private static final boolean ourPreloadClasses = "true".equals(System.getProperty("idea.preload.classes")); + private final Stack myUrls = new Stack(); + private final List myLoaders = new ArrayList(); + private final Map myLoadersMap = new HashMap(); + private final ClasspathCache myCache = new ClasspathCache(); private final boolean myCanLockJars; private final boolean myCanUseCache; - private static final long NS_THRESHOLD = 10000000L; - private static long total; - private static int requests; - - private static PrintStream ourOrder; - private static long ourOrderSize; - private static final Set ourOrderedUrls = new HashSet(); - private final boolean myAcceptUnescapedUrls; private final boolean myPreloadJarContents; - private static synchronized void printOrder(Loader loader, String url, Resource resource) { - if (!ourOrderedUrls.add(url)) return; - String home = FileUtil.toSystemIndependentName(PathManager.getHomePath()); - try { - ourOrderSize += resource.getContentLength(); - } - catch (IOException e) { - System.out.println(e); - } - if (ourOrder == null) { - final File orderFile = new File(PathManager.getBinPath() + File.separator + "order.txt"); - try { - if (!FileUtil.ensureCanCreateFile(orderFile)) return; - ourOrder = new PrintStream(new FileOutputStream(orderFile, true)); - ShutDownTracker.getInstance().registerShutdownTask(new Runnable() { - public void run() { - ourOrder.close(); - System.out.println(ourOrderSize); - } - }); - } - catch (IOException e) { - return; - } - } - - if (ourOrder != null) { - String jarURL = FileUtil.toSystemIndependentName(loader.getBaseURL().getFile()); - jarURL = StringUtil.trimStart(jarURL, "file:/"); - if (jarURL.startsWith(home)) { - jarURL = jarURL.replaceFirst(home, ""); - jarURL = StringUtil.trimEnd(jarURL, "!/"); - ourOrder.println(url + ":" + jarURL); - } - } - } - - /** @deprecated use {@link #ClassPath(java.util.List, boolean, boolean, boolean, boolean)} (to remove in IDEA 14) */ - public ClassPath(URL[] urls, boolean canLockJars, boolean canUseCache) { - this(Arrays.asList(urls), canLockJars, canUseCache, false, true); - } - - /** @deprecated use {@link #ClassPath(java.util.List, boolean, boolean, boolean, boolean)} (to remove in IDEA 14) */ - public ClassPath(URL[] urls, boolean canLockJars, boolean canUseCache, boolean acceptUnescapedUrls, boolean preloadJarContents) { - this(Arrays.asList(urls), canLockJars, canUseCache, acceptUnescapedUrls, preloadJarContents); - } - public ClassPath(List urls, boolean canLockJars, boolean canUseCache, boolean acceptUnescapedUrls, boolean preloadJarContents) { myCanLockJars = canLockJars; myCanUseCache = canUseCache; @@ -130,12 +74,11 @@ public class ClassPath { @Nullable public Resource getResource(String s, boolean flag) { - final long started = myDebugTime ? System.nanoTime():0; - + final long started = startTiming(); try { int i; if (myCanUseCache) { - Resource prevResource = myCache.iterateLoaders(s, flag ? checkedIterator:uncheckedIterator, s, this); + Resource prevResource = myCache.iterateLoaders(s, flag ? ourCheckedIterator : ourUncheckedIterator, s, this); if (prevResource != null) return prevResource; synchronized (myUrls) { @@ -148,7 +91,8 @@ public class ClassPath { i = 0; } - for (Loader loader; (loader = getLoader(i)) != null; i++) { + Loader loader; + while ((loader = getLoader(i++)) != null) { if (myCanUseCache) { if (!myCache.loaderHasName(s, loader)) continue; } @@ -157,12 +101,12 @@ public class ClassPath { return resource; } } - - return null; } finally { - if (myDebugTime) reportTime(started, s); + logTiming(this, started, s); } + + return null; } public Enumeration getResources(final String name, final boolean check) { @@ -207,20 +151,24 @@ public class ClassPath { @Nullable private Loader getLoader(final URL url, int index) throws IOException { String s; + if (myAcceptUnescapedUrls) { s = url.getFile(); - } else { + } + else { try { s = url.toURI().getSchemeSpecificPart(); - } catch (URISyntaxException thisShouldNotHappen) { + } + catch (URISyntaxException thisShouldNotHappen) { + //noinspection CallToPrintStackTrace thisShouldNotHappen.printStackTrace(); s = url.getFile(); } } Loader loader = null; - if (s != null && new File(s).isDirectory()) { - if (FILE_PROTOCOL.equals(url.getProtocol())) { + if (s != null && new File(s).isDirectory()) { + if (URLUtil.FILE_PROTOCOL.equals(url.getProtocol())) { loader = new FileLoader(url, index); } } @@ -260,9 +208,9 @@ public class ClassPath { synchronized (myUrls) { if (myUrls.isEmpty()) { loaders = new SmartList(); - myCache.iterateLoaders(name, myLoaderCollector, loaders, this); + myCache.iterateLoaders(name, ourLoaderCollector, loaders, this); if (!name.endsWith("/")) { - myCache.iterateLoaders(name.concat("/"), myLoaderCollector, loaders, this); + myCache.iterateLoaders(name.concat("/"), ourLoaderCollector, loaders, this); } } } @@ -273,9 +221,10 @@ public class ClassPath { private boolean next() { if (myRes != null) return true; - long started = myDebugTime ? System.nanoTime() : 0; - Loader loader; + + long started = startTiming(); try { + Loader loader; if (myLoaders != null) { while (myIndex < myLoaders.size()) { loader = myLoaders.get(myIndex++); @@ -296,10 +245,9 @@ public class ClassPath { } } finally { - if (myDebugTime) reportTime(started, myName); + logTiming(ClassPath.this, started, myName); } - return false; } @@ -319,18 +267,6 @@ public class ClassPath { } } - private void reportTime(long started, String msg) { - long doneFor = System.nanoTime() - started; - total += doneFor; - ++requests; - if (doneFor > NS_THRESHOLD) { - System.out.println((doneFor/1000000) + " ms for " +msg); - } - if (requests % 1000 == 0) { - System.out.println(toString() + ", requests:" + requests + ", time:" + (total / 1000000) + "ms"); - } - } - private static class ResourceStringLoaderIterator extends ClasspathCache.LoaderIterator { private final boolean myFlag; @@ -343,17 +279,12 @@ public class ClassPath { if (!classPath.myCache.loaderHasName(s, loader)) return null; final Resource resource = loader.getResource(s, myFlag); if (resource != null) { - if (ourDumpOrder) { - printOrder(loader, s, resource); - } + printOrder(loader, s, resource); return resource; } return null; } } - private static final ResourceStringLoaderIterator checkedIterator = new ResourceStringLoaderIterator(true); - private static final ResourceStringLoaderIterator uncheckedIterator = new ResourceStringLoaderIterator(false); - private static final LoaderCollector myLoaderCollector = new LoaderCollector(); private static class LoaderCollector extends ClasspathCache.LoaderIterator, Object> { @Override @@ -362,4 +293,74 @@ public class ClassPath { return null; } } + + private static final boolean ourDumpOrder = "true".equals(System.getProperty("idea.dump.order")); + private static PrintStream ourOrder; + private static long ourOrderSize; + private static final Set ourOrderedUrls = new HashSet(); + + @SuppressWarnings("UseOfSystemOutOrSystemErr") + private static synchronized void printOrder(Loader loader, String url, Resource resource) { + if (!ourDumpOrder) return; + if (!ourOrderedUrls.add(url)) return; + + String home = FileUtil.toSystemIndependentName(PathManager.getHomePath()); + try { + ourOrderSize += resource.getContentLength(); + } + catch (IOException e) { + e.printStackTrace(System.out); + } + + if (ourOrder == null) { + final File orderFile = new File(PathManager.getBinPath() + File.separator + "order.txt"); + try { + if (!FileUtil.ensureCanCreateFile(orderFile)) return; + ourOrder = new PrintStream(new FileOutputStream(orderFile, true)); + ShutDownTracker.getInstance().registerShutdownTask(new Runnable() { + public void run() { + ourOrder.close(); + System.out.println(ourOrderSize); + } + }); + } + catch (IOException e) { + return; + } + } + + if (ourOrder != null) { + String jarURL = FileUtil.toSystemIndependentName(loader.getBaseURL().getFile()); + jarURL = StringUtil.trimStart(jarURL, "file:/"); + if (jarURL.startsWith(home)) { + jarURL = jarURL.replaceFirst(home, ""); + jarURL = StringUtil.trimEnd(jarURL, "!/"); + ourOrder.println(url + ":" + jarURL); + } + } + } + + + private static final boolean ourLogTiming = Boolean.getBoolean("idea.print.classpath.timing"); + private static long ourTotalTime = 0; + private static int ourTotalRequests = 0; + + private static long startTiming() { + return ourLogTiming ? System.nanoTime() : 0; + } + + @SuppressWarnings("UseOfSystemOutOrSystemErr") + private static void logTiming(ClassPath path, long started, String msg) { + if (!ourLogTiming) return; + + long time = System.nanoTime() - started; + ourTotalTime += time; + ++ourTotalRequests; + if (time > 10000000L) { + System.out.println((time / 1000000) + " ms for " + msg); + } + if (ourTotalRequests % 1000 == 0) { + System.out.println(path.toString() + ", requests:" + ourTotalRequests + ", time:" + (ourTotalTime / 1000000) + "ms"); + } + } } diff --git a/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java b/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java index d49667825bbb..08c31a2fa2e3 100644 --- a/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java +++ b/platform/util/testSrc/com/intellij/util/io/zip/ReorderJarsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2012 JetBrains s.r.o. + * Copyright 2000-2014 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,7 +32,7 @@ import static org.junit.Assert.*; /** * @author Dmitry Avdeev - * @since 7/12/11 + * @since 12/07/2011 */ public class ReorderJarsTest { private File myTempDirectory;