< prev index next >

src/java.base/share/classes/sun/launcher/LauncherHelper.java

Print this page
@  rev 57734 : Review feedback
|
o  rev 57733 : 8231111: Cgroups v2: Rework Metrics in java.base so as to recognize unified hierarchy
|  Reviewed-by: bobv, mchung
~
   1 /*
   2  * Copyright (c) 2007, 2019, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  28 /*
  29  *
  30  *  <p><b>This is NOT part of any API supported by Sun Microsystems.
  31  *  If you write code that depends on this, you do so at your own
  32  *  risk.  This code and its internal interfaces are subject to change
  33  *  or deletion without notice.</b>
  34  *
  35  */
  36 
  37 /**
  38  * A utility package for the java(1), javaw(1) launchers.
  39  * The following are helper methods that the native launcher uses
  40  * to perform checks etc. using JNI, see src/share/bin/java.c
  41  */
  42 import java.io.File;
  43 import java.io.IOException;
  44 import java.io.PrintStream;
  45 import java.io.UnsupportedEncodingException;
  46 import java.lang.module.Configuration;
  47 import java.lang.module.ModuleDescriptor;
  48 import java.lang.module.ModuleDescriptor.Requires;
  49 import java.lang.module.ModuleDescriptor.Exports;
  50 import java.lang.module.ModuleDescriptor.Opens;
  51 import java.lang.module.ModuleDescriptor.Provides;

  52 import java.lang.module.ModuleFinder;
  53 import java.lang.module.ModuleReference;
  54 import java.lang.module.ResolvedModule;
  55 import java.lang.reflect.InvocationTargetException;
  56 import java.lang.reflect.Method;
  57 import java.lang.reflect.Modifier;
  58 import java.math.BigDecimal;
  59 import java.math.RoundingMode;
  60 import java.net.URI;
  61 import java.nio.charset.Charset;
  62 import java.nio.file.DirectoryStream;
  63 import java.nio.file.Files;
  64 import java.nio.file.Path;
  65 import java.text.Normalizer;
  66 import java.text.MessageFormat;

  67 import java.util.ArrayList;
  68 import java.util.Collections;
  69 import java.util.Comparator;
  70 import java.util.Iterator;
  71 import java.util.List;
  72 import java.util.Locale;
  73 import java.util.Locale.Category;
  74 import java.util.Optional;
  75 import java.util.Properties;
  76 import java.util.ResourceBundle;
  77 import java.util.Set;
  78 import java.util.TreeSet;
  79 import java.util.jar.Attributes;
  80 import java.util.jar.JarFile;
  81 import java.util.jar.Manifest;
  82 import java.util.stream.Collectors;
  83 import java.util.stream.Stream;
  84 
  85 import jdk.internal.misc.VM;
  86 import jdk.internal.module.ModuleBootstrap;


 102     private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX =
 103             "sun.launcher.LauncherHelper$FXHelper";
 104     private static final String LAUNCHER_AGENT_CLASS = "Launcher-Agent-Class";
 105     private static final String MAIN_CLASS = "Main-Class";
 106     private static final String ADD_EXPORTS = "Add-Exports";
 107     private static final String ADD_OPENS = "Add-Opens";
 108 
 109     private static StringBuilder outBuf = new StringBuilder();
 110 
 111     private static final String INDENT = "    ";
 112     private static final String VM_SETTINGS     = "VM settings:";
 113     private static final String PROP_SETTINGS   = "Property settings:";
 114     private static final String LOCALE_SETTINGS = "Locale settings:";
 115 
 116     // sync with java.c and jdk.internal.misc.VM
 117     private static final String diagprop = "sun.java.launcher.diag";
 118     static final boolean trace = VM.getSavedProperty(diagprop) != null;
 119 
 120     private static final String defaultBundleName =
 121             "sun.launcher.resources.launcher";

 122     private static class ResourceBundleHolder {
 123         private static final ResourceBundle RB =
 124                 ResourceBundle.getBundle(defaultBundleName);
 125     }
 126     private static PrintStream ostream;
 127     private static Class<?> appClass; // application class, for GUI/reporting purposes
 128 
 129     /*
 130      * A method called by the launcher to print out the standard settings,
 131      * by default -XshowSettings is equivalent to -XshowSettings:all,
 132      * Specific information may be gotten by using suboptions with possible
 133      * values vm, properties and locale.
 134      *
 135      * printToStderr: choose between stdout and stderr
 136      *
 137      * optionFlag: specifies which options to print default is all other
 138      *    possible values are vm, properties, locale.
 139      *
 140      * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates
 141      *    this code should determine this value, using a suitable method or


 308             // print columns of 8
 309             if ((i + 1) % 8 == 0) {
 310                 ostream.println();
 311                 ostream.print(INDENT + INDENT);
 312             }
 313         }
 314     }
 315 
 316     public static void printSystemMetrics() {
 317         Metrics c = Container.metrics();
 318 
 319         ostream.println("Operating System Metrics:");
 320 
 321         if (c == null) {
 322             ostream.println(INDENT + "No metrics available for this platform");
 323             return;
 324         }
 325 
 326         ostream.println(INDENT + "Provider: " + c.getProvider());
 327         ostream.println(INDENT + "Effective CPU Count: " + c.getEffectiveCpuCount());
 328         ostream.println(INDENT + "CPU Period: " + c.getCpuPeriod() +
 329                (c.getCpuPeriod() == -1 ? "" : "us"));
 330         ostream.println(INDENT + "CPU Quota: " + c.getCpuQuota() +
 331                (c.getCpuQuota() == -1 ? "" : "us"));
 332         ostream.println(INDENT + "CPU Shares: " + c.getCpuShares());
 333 
 334         int cpus[] = c.getCpuSetCpus();

 335         ostream.println(INDENT + "List of Processors, "
 336                 + cpus.length + " total: ");
 337 
 338         ostream.print(INDENT);
 339         for (int i = 0; i < cpus.length; i++) {
 340             ostream.print(cpus[i] + " ");
 341         }
 342         if (cpus.length > 0) {
 343             ostream.println("");
 344         }



 345 
 346         cpus = c.getEffectiveCpuSetCpus();

 347         ostream.println(INDENT + "List of Effective Processors, "
 348                 + cpus.length + " total: ");
 349 
 350         ostream.print(INDENT);
 351         for (int i = 0; i < cpus.length; i++) {
 352             ostream.print(cpus[i] + " ");
 353         }
 354         if (cpus.length > 0) {
 355             ostream.println("");
 356         }



 357 
 358         int mems[] = c.getCpuSetMems();

 359         ostream.println(INDENT + "List of Memory Nodes, "
 360                 + mems.length + " total: ");
 361 
 362         ostream.print(INDENT);
 363         for (int i = 0; i < mems.length; i++) {
 364             ostream.print(mems[i] + " ");
 365         }
 366         if (mems.length > 0) {
 367             ostream.println("");
 368         }



 369 
 370         mems = c.getEffectiveCpuSetMems();

 371         ostream.println(INDENT + "List of Available Memory Nodes, "
 372                 + mems.length + " total: ");
 373 
 374         ostream.print(INDENT);
 375         for (int i = 0; i < mems.length; i++) {
 376             ostream.print(mems[i] + " ");
 377         }
 378         if (mems.length > 0) {
 379             ostream.println("");
 380         }
 381 
 382         ostream.println(INDENT + "CPUSet Memory Pressure Enabled: "
 383                 + c.isCpuSetMemoryPressureEnabled());
 384 
 385         long limit = c.getMemoryLimit();
 386         ostream.println(INDENT + "Memory Limit: " +
 387                 ((limit >= 0) ? SizePrefix.scaleValue(limit) : "Unlimited"));
 388 
 389         limit = c.getMemorySoftLimit();
 390         ostream.println(INDENT + "Memory Soft Limit: " +
 391                 ((limit >= 0) ? SizePrefix.scaleValue(limit) : "Unlimited"));
 392 
 393         limit = c.getMemoryAndSwapLimit();
 394         ostream.println(INDENT + "Memory & Swap Limit: " +
 395                 ((limit >= 0) ? SizePrefix.scaleValue(limit) : "Unlimited"));
 396 
 397         limit = c.getKernelMemoryLimit();
 398         ostream.println(INDENT + "Kernel Memory Limit: " +
 399                 ((limit >= 0) ? SizePrefix.scaleValue(limit) : "Unlimited"));
 400 
 401         limit = c.getTcpMemoryLimit();
 402         ostream.println(INDENT + "TCP Memory Limit: " +
 403                 ((limit >= 0) ? SizePrefix.scaleValue(limit) : "Unlimited"));
 404 
 405         ostream.println(INDENT + "Out Of Memory Killer Enabled: "
 406                 + c.isMemoryOOMKillEnabled());







 407 
 408         ostream.println("");















 409     }
 410 
 411     private enum SizePrefix {
 412 
 413         KILO(1024, "K"),
 414         MEGA(1024 * 1024, "M"),
 415         GIGA(1024 * 1024 * 1024, "G"),
 416         TERA(1024L * 1024L * 1024L * 1024L, "T");
 417         long size;
 418         String abbrev;
 419 
 420         SizePrefix(long size, String abbrev) {
 421             this.size = size;
 422             this.abbrev = abbrev;
 423         }
 424 
 425         private static String scale(long v, SizePrefix prefix) {
 426             return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size),
 427                     2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev;
 428         }


   1 /*
   2  * Copyright (c) 2007, 2020, 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.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  28 /*
  29  *
  30  *  <p><b>This is NOT part of any API supported by Sun Microsystems.
  31  *  If you write code that depends on this, you do so at your own
  32  *  risk.  This code and its internal interfaces are subject to change
  33  *  or deletion without notice.</b>
  34  *
  35  */
  36 
  37 /**
  38  * A utility package for the java(1), javaw(1) launchers.
  39  * The following are helper methods that the native launcher uses
  40  * to perform checks etc. using JNI, see src/share/bin/java.c
  41  */
  42 import java.io.File;
  43 import java.io.IOException;
  44 import java.io.PrintStream;
  45 import java.io.UnsupportedEncodingException;
  46 import java.lang.module.Configuration;
  47 import java.lang.module.ModuleDescriptor;

  48 import java.lang.module.ModuleDescriptor.Exports;
  49 import java.lang.module.ModuleDescriptor.Opens;
  50 import java.lang.module.ModuleDescriptor.Provides;
  51 import java.lang.module.ModuleDescriptor.Requires;
  52 import java.lang.module.ModuleFinder;
  53 import java.lang.module.ModuleReference;
  54 import java.lang.module.ResolvedModule;
  55 import java.lang.reflect.InvocationTargetException;
  56 import java.lang.reflect.Method;
  57 import java.lang.reflect.Modifier;
  58 import java.math.BigDecimal;
  59 import java.math.RoundingMode;
  60 import java.net.URI;
  61 import java.nio.charset.Charset;
  62 import java.nio.file.DirectoryStream;
  63 import java.nio.file.Files;
  64 import java.nio.file.Path;

  65 import java.text.MessageFormat;
  66 import java.text.Normalizer;
  67 import java.util.ArrayList;
  68 import java.util.Collections;
  69 import java.util.Comparator;
  70 import java.util.Iterator;
  71 import java.util.List;
  72 import java.util.Locale;
  73 import java.util.Locale.Category;
  74 import java.util.Optional;
  75 import java.util.Properties;
  76 import java.util.ResourceBundle;
  77 import java.util.Set;
  78 import java.util.TreeSet;
  79 import java.util.jar.Attributes;
  80 import java.util.jar.JarFile;
  81 import java.util.jar.Manifest;
  82 import java.util.stream.Collectors;
  83 import java.util.stream.Stream;
  84 
  85 import jdk.internal.misc.VM;
  86 import jdk.internal.module.ModuleBootstrap;


 102     private static final String JAVAFX_FXHELPER_CLASS_NAME_SUFFIX =
 103             "sun.launcher.LauncherHelper$FXHelper";
 104     private static final String LAUNCHER_AGENT_CLASS = "Launcher-Agent-Class";
 105     private static final String MAIN_CLASS = "Main-Class";
 106     private static final String ADD_EXPORTS = "Add-Exports";
 107     private static final String ADD_OPENS = "Add-Opens";
 108 
 109     private static StringBuilder outBuf = new StringBuilder();
 110 
 111     private static final String INDENT = "    ";
 112     private static final String VM_SETTINGS     = "VM settings:";
 113     private static final String PROP_SETTINGS   = "Property settings:";
 114     private static final String LOCALE_SETTINGS = "Locale settings:";
 115 
 116     // sync with java.c and jdk.internal.misc.VM
 117     private static final String diagprop = "sun.java.launcher.diag";
 118     static final boolean trace = VM.getSavedProperty(diagprop) != null;
 119 
 120     private static final String defaultBundleName =
 121             "sun.launcher.resources.launcher";
 122     private static final long LONG_RETVAL_NOT_SUPPORTED = -2;
 123     private static class ResourceBundleHolder {
 124         private static final ResourceBundle RB =
 125                 ResourceBundle.getBundle(defaultBundleName);
 126     }
 127     private static PrintStream ostream;
 128     private static Class<?> appClass; // application class, for GUI/reporting purposes
 129 
 130     /*
 131      * A method called by the launcher to print out the standard settings,
 132      * by default -XshowSettings is equivalent to -XshowSettings:all,
 133      * Specific information may be gotten by using suboptions with possible
 134      * values vm, properties and locale.
 135      *
 136      * printToStderr: choose between stdout and stderr
 137      *
 138      * optionFlag: specifies which options to print default is all other
 139      *    possible values are vm, properties, locale.
 140      *
 141      * initialHeapSize: in bytes, as set by the launcher, a zero-value indicates
 142      *    this code should determine this value, using a suitable method or


 309             // print columns of 8
 310             if ((i + 1) % 8 == 0) {
 311                 ostream.println();
 312                 ostream.print(INDENT + INDENT);
 313             }
 314         }
 315     }
 316 
 317     public static void printSystemMetrics() {
 318         Metrics c = Container.metrics();
 319 
 320         ostream.println("Operating System Metrics:");
 321 
 322         if (c == null) {
 323             ostream.println(INDENT + "No metrics available for this platform");
 324             return;
 325         }
 326 
 327         ostream.println(INDENT + "Provider: " + c.getProvider());
 328         ostream.println(INDENT + "Effective CPU Count: " + c.getEffectiveCpuCount());
 329         ostream.println(formatCpuVal(c.getCpuPeriod(), INDENT + "CPU Period: "));
 330         ostream.println(formatCpuVal(c.getCpuQuota(), INDENT + "CPU Quota: "));
 331         ostream.println(formatCpuVal(c.getCpuShares(), INDENT + "CPU Shares: "));


 332 
 333         int cpus[] = c.getCpuSetCpus();
 334         if (cpus != null) {
 335             ostream.println(INDENT + "List of Processors, "
 336                     + cpus.length + " total: ");
 337 
 338             ostream.print(INDENT);
 339             for (int i = 0; i < cpus.length; i++) {
 340                 ostream.print(cpus[i] + " ");
 341             }
 342             if (cpus.length > 0) {
 343                 ostream.println("");
 344             }
 345         } else {
 346             ostream.println(INDENT + "List of Processors: N/A");
 347         }
 348 
 349         cpus = c.getEffectiveCpuSetCpus();
 350         if (cpus != null) {
 351             ostream.println(INDENT + "List of Effective Processors, "
 352                     + cpus.length + " total: ");
 353 
 354             ostream.print(INDENT);
 355             for (int i = 0; i < cpus.length; i++) {
 356                 ostream.print(cpus[i] + " ");
 357             }
 358             if (cpus.length > 0) {
 359                 ostream.println("");
 360             }
 361         } else {
 362             ostream.println(INDENT + "List of Effective Processors: N/A");
 363         }
 364 
 365         int mems[] = c.getCpuSetMems();
 366         if (mems != null) {
 367             ostream.println(INDENT + "List of Memory Nodes, "
 368                     + mems.length + " total: ");
 369 
 370             ostream.print(INDENT);
 371             for (int i = 0; i < mems.length; i++) {
 372                 ostream.print(mems[i] + " ");
 373             }
 374             if (mems.length > 0) {
 375                 ostream.println("");
 376             }
 377         } else {
 378             ostream.println(INDENT + "List of Memory Nodes: N/A");
 379         }
 380 
 381         mems = c.getEffectiveCpuSetMems();
 382         if (mems != null) {
 383             ostream.println(INDENT + "List of Available Memory Nodes, "
 384                     + mems.length + " total: ");
 385 
 386             ostream.print(INDENT);
 387             for (int i = 0; i < mems.length; i++) {
 388                 ostream.print(mems[i] + " ");
 389             }
 390             if (mems.length > 0) {
 391                 ostream.println("");
 392             }
 393         } else {
 394             ostream.println(INDENT + "List of Available Memory Nodes: N/A");
 395         }
 396 
 397         long limit = c.getMemoryLimit();
 398         ostream.println(formatLimitString(limit, INDENT + "Memory Limit: "));

 399 
 400         limit = c.getMemorySoftLimit();
 401         ostream.println(formatLimitString(limit, INDENT + "Memory Soft Limit: "));

 402 
 403         limit = c.getMemoryAndSwapLimit();
 404         ostream.println(formatLimitString(limit, INDENT + "Memory & Swap Limit: "));

 405 
 406         ostream.println("");
 407     }





 408 
 409     private static String formatLimitString(long limit, String prefix) {
 410         if (limit >= 0) {
 411             return prefix + SizePrefix.scaleValue(limit);
 412         } else if (limit == LONG_RETVAL_NOT_SUPPORTED) {
 413             return prefix + "N/A";
 414         } else {
 415             return prefix + "Unlimited";
 416         }
 417     }
 418 
 419     private static String formatCpuVal(long cpuVal, String prefix) {
 420         if (cpuVal >= 0) {
 421             return prefix + cpuVal + "us";
 422         } else if (cpuVal == LONG_RETVAL_NOT_SUPPORTED) {
 423             return prefix + "N/A";
 424         } else {
 425             return prefix + cpuVal;
 426         }
 427     }
 428 
 429     private static String formatBoolean(Boolean value, String prefix) {
 430         if (value != null) {
 431             return prefix + value;
 432         } else {
 433             return prefix + "N/A";
 434         }
 435     }
 436 
 437     private enum SizePrefix {
 438 
 439         KILO(1024, "K"),
 440         MEGA(1024 * 1024, "M"),
 441         GIGA(1024 * 1024 * 1024, "G"),
 442         TERA(1024L * 1024L * 1024L * 1024L, "T");
 443         long size;
 444         String abbrev;
 445 
 446         SizePrefix(long size, String abbrev) {
 447             this.size = size;
 448             this.abbrev = abbrev;
 449         }
 450 
 451         private static String scale(long v, SizePrefix prefix) {
 452             return BigDecimal.valueOf(v).divide(BigDecimal.valueOf(prefix.size),
 453                     2, RoundingMode.HALF_EVEN).toPlainString() + prefix.abbrev;
 454         }


< prev index next >