< prev index next >

src/jdk.jfr/share/classes/jdk/jfr/internal/Utils.java

Print this page




  48 import java.util.Arrays;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.List;
  52 import java.util.Map;
  53 import java.util.Objects;
  54 
  55 import jdk.internal.org.objectweb.asm.ClassReader;
  56 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
  57 import jdk.jfr.Event;
  58 import jdk.jfr.FlightRecorderPermission;
  59 import jdk.jfr.Recording;
  60 import jdk.jfr.RecordingState;
  61 import jdk.jfr.internal.handlers.EventHandler;
  62 import jdk.jfr.internal.settings.PeriodSetting;
  63 import jdk.jfr.internal.settings.StackTraceSetting;
  64 import jdk.jfr.internal.settings.ThresholdSetting;
  65 
  66 public final class Utils {
  67 


  68     private static Boolean SAVE_GENERATED;
  69 
  70     public static final String EVENTS_PACKAGE_NAME = "jdk.jfr.events";
  71     public static final String INSTRUMENT_PACKAGE_NAME = "jdk.jfr.internal.instrument";
  72     public static final String HANDLERS_PACKAGE_NAME = "jdk.jfr.internal.handlers";
  73     public static final String REGISTER_EVENT = "registerEvent";
  74     public static final String ACCESS_FLIGHT_RECORDER = "accessFlightRecorder";
  75 
  76     private final static String LEGACY_EVENT_NAME_PREFIX = "com.oracle.jdk.";
  77 
  78     public static void checkAccessFlightRecorder() throws SecurityException {
  79         SecurityManager sm = System.getSecurityManager();
  80         if (sm != null) {
  81             sm.checkPermission(new FlightRecorderPermission(ACCESS_FLIGHT_RECORDER));
  82         }
  83     }
  84 
  85     public static void checkRegisterPermission() throws SecurityException {
  86         SecurityManager sm = System.getSecurityManager();
  87         if (sm != null) {


  97 
  98         TimespanUnit(String unit, long amount) {
  99             this.text = unit;
 100             this.amount = amount;
 101         }
 102     }
 103 
 104     public static String formatBytes(long bytes, String separation) {
 105         if (bytes < 1024) {
 106             return bytes + " bytes";
 107         }
 108         int exp = (int) (Math.log(bytes) / Math.log(1024));
 109         char bytePrefix = "kMGTPE".charAt(exp - 1);
 110         return String.format("%.1f%s%cB", bytes / Math.pow(1024, exp), separation, bytePrefix);
 111     }
 112 
 113     public static String formatTimespan(Duration dValue, String separation) {
 114         if (dValue == null) {
 115             return "0";
 116         }
 117 
 118         long value = dValue.toNanos();
 119         TimespanUnit result = TimespanUnit.NANOSECONDS;
 120         for (TimespanUnit unit : TimespanUnit.values()) {
 121             result = unit;
 122             long amount = unit.amount;
 123             if (result == TimespanUnit.DAYS || value < amount || value % amount != 0) {
 124                 break;
 125             }
 126             value /= amount;
 127         }
 128         return String.format("%d%s%s", value, separation, result.text);







 129     }
 130 
 131     public static long parseTimespan(String s) {
 132         if (s.endsWith("ns")) {
 133             return Long.parseLong(s.substring(0, s.length() - 2).trim());
 134         }
 135         if (s.endsWith("us")) {
 136             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MICROSECONDS);
 137         }
 138         if (s.endsWith("ms")) {
 139             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MILLISECONDS);
 140         }
 141         if (s.endsWith("s")) {
 142             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
 143         }
 144         if (s.endsWith("m")) {
 145             return 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
 146         }
 147         if (s.endsWith("h")) {
 148             return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);




  48 import java.util.Arrays;
  49 import java.util.Collections;
  50 import java.util.HashMap;
  51 import java.util.List;
  52 import java.util.Map;
  53 import java.util.Objects;
  54 
  55 import jdk.internal.org.objectweb.asm.ClassReader;
  56 import jdk.internal.org.objectweb.asm.util.CheckClassAdapter;
  57 import jdk.jfr.Event;
  58 import jdk.jfr.FlightRecorderPermission;
  59 import jdk.jfr.Recording;
  60 import jdk.jfr.RecordingState;
  61 import jdk.jfr.internal.handlers.EventHandler;
  62 import jdk.jfr.internal.settings.PeriodSetting;
  63 import jdk.jfr.internal.settings.StackTraceSetting;
  64 import jdk.jfr.internal.settings.ThresholdSetting;
  65 
  66 public final class Utils {
  67 
  68     private static final String INFINITY = "infinity";
  69 
  70     private static Boolean SAVE_GENERATED;
  71 
  72     public static final String EVENTS_PACKAGE_NAME = "jdk.jfr.events";
  73     public static final String INSTRUMENT_PACKAGE_NAME = "jdk.jfr.internal.instrument";
  74     public static final String HANDLERS_PACKAGE_NAME = "jdk.jfr.internal.handlers";
  75     public static final String REGISTER_EVENT = "registerEvent";
  76     public static final String ACCESS_FLIGHT_RECORDER = "accessFlightRecorder";
  77 
  78     private final static String LEGACY_EVENT_NAME_PREFIX = "com.oracle.jdk.";
  79 
  80     public static void checkAccessFlightRecorder() throws SecurityException {
  81         SecurityManager sm = System.getSecurityManager();
  82         if (sm != null) {
  83             sm.checkPermission(new FlightRecorderPermission(ACCESS_FLIGHT_RECORDER));
  84         }
  85     }
  86 
  87     public static void checkRegisterPermission() throws SecurityException {
  88         SecurityManager sm = System.getSecurityManager();
  89         if (sm != null) {


  99 
 100         TimespanUnit(String unit, long amount) {
 101             this.text = unit;
 102             this.amount = amount;
 103         }
 104     }
 105 
 106     public static String formatBytes(long bytes, String separation) {
 107         if (bytes < 1024) {
 108             return bytes + " bytes";
 109         }
 110         int exp = (int) (Math.log(bytes) / Math.log(1024));
 111         char bytePrefix = "kMGTPE".charAt(exp - 1);
 112         return String.format("%.1f%s%cB", bytes / Math.pow(1024, exp), separation, bytePrefix);
 113     }
 114 
 115     public static String formatTimespan(Duration dValue, String separation) {
 116         if (dValue == null) {
 117             return "0";
 118         }

 119         long value = dValue.toNanos();
 120         TimespanUnit result = TimespanUnit.NANOSECONDS;
 121         for (TimespanUnit unit : TimespanUnit.values()) {
 122             result = unit;
 123             long amount = unit.amount;
 124             if (result == TimespanUnit.DAYS || value < amount || value % amount != 0) {
 125                 break;
 126             }
 127             value /= amount;
 128         }
 129         return String.format("%d%s%s", value, separation, result.text);
 130     }
 131 
 132     public static long parseTimespanWithInfinity(String s) {
 133         if (INFINITY.equals(s)) {
 134             return Long.MAX_VALUE;
 135         }
 136         return parseTimespan(s);
 137     }
 138 
 139     public static long parseTimespan(String s) {
 140         if (s.endsWith("ns")) {
 141             return Long.parseLong(s.substring(0, s.length() - 2).trim());
 142         }
 143         if (s.endsWith("us")) {
 144             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MICROSECONDS);
 145         }
 146         if (s.endsWith("ms")) {
 147             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 2).trim()), MILLISECONDS);
 148         }
 149         if (s.endsWith("s")) {
 150             return NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
 151         }
 152         if (s.endsWith("m")) {
 153             return 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);
 154         }
 155         if (s.endsWith("h")) {
 156             return 60 * 60 * NANOSECONDS.convert(Long.parseLong(s.substring(0, s.length() - 1).trim()), SECONDS);


< prev index next >