8337501: JFR: Use TimespanUnit

Reviewed-by: mgronlun
This commit is contained in:
Erik Gahlin 2024-07-31 17:58:52 +00:00
parent 8f039b5629
commit a45bb55ddb
4 changed files with 35 additions and 47 deletions
src/jdk.jfr/share/classes/jdk/jfr/internal

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -32,6 +32,7 @@ import java.util.Map;
import java.util.Set;
import java.util.StringJoiner;
import jdk.jfr.internal.util.SpellChecker;
import jdk.jfr.internal.util.TimespanUnit;
final class ArgumentParser {
private final Map<String, Object> options = new HashMap<>();
@ -302,16 +303,11 @@ final class ArgumentParser {
}
throw new IllegalArgumentException("Integer parsing error nanotime value: unit required");
}
return switch(unit) {
case "ns" -> time;
case "us" -> time * 1000;
case "ms" -> time * 1000 * 1000;
case "s" -> time * 1000 * 1000 * 1000;
case "m" -> time * 60 * 1000 * 1000 * 1000;
case "h" -> time * 60 * 60* 1000 * 1000 * 1000;
case "d" -> time * 24 * 60 * 60 * 1000 * 1000 * 1000;
default -> throw new IllegalArgumentException("Integer parsing error nanotime value: illegal unit");
};
TimespanUnit tu = TimespanUnit.fromText(unit);
if (tu == null) {
throw new IllegalArgumentException("Integer parsing error nanotime value: illegal unit");
}
return tu.toNanos(time);
}
int indexOfUnit(String text) {

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -25,11 +25,9 @@
package jdk.jfr.internal.jfc.model;
import java.util.StringJoiner;
import jdk.jfr.internal.util.TimespanUnit;
public final class Utilities {
private static final String[] UNITS = new String[] {
"ns", "us", "ns", "ms", "s", "m", "h", "d" // order matters
};
static XmlElement instantiate(Class<? extends XmlElement> type) {
try {
@ -104,9 +102,9 @@ public final class Utilities {
static String parseTimespan(String s) {
StringBuilder sb = new StringBuilder();
try {
for (String unit : UNITS) {
if (s.endsWith(unit)) {
return parseForUnit(s, unit);
for (TimespanUnit timespan : TimespanUnit.values()) {
if (s.endsWith(timespan.text)) {
return parseForUnit(s, timespan.text);
}
}
Long.parseLong(s);

@ -24,21 +24,29 @@
*/
package jdk.jfr.internal.util;
import java.util.concurrent.TimeUnit;
public enum TimespanUnit {
NANOSECONDS ("ns", 1L, 1000),
MICROSECONDS("us", 1000L, 1000),
MILLISECONDS("ms", 1_000_000L, 1000),
SECONDS ("s", 1_000_000_000L, 60),
MINUTES ("m", 60 * 1_000_000_000L, 60),
HOURS ("h", 60 * 60 * 1_000_000_000L, 24),
DAYS ("d", 24 * 60 * 60 * 1_000_000_000L, 7);
NANOSECONDS ("ns", TimeUnit.NANOSECONDS, 1000),
MICROSECONDS("us", TimeUnit.MICROSECONDS, 1000),
MILLISECONDS("ms", TimeUnit.MILLISECONDS, 1000),
SECONDS ("s", TimeUnit.SECONDS, 60),
MINUTES ("m", TimeUnit.MINUTES, 60),
HOURS ("h", TimeUnit.HOURS, 24),
DAYS ("d", TimeUnit.DAYS, 7);
public final String text;
public final long nanos;
public final int size;
TimespanUnit(String text, long nanos, int size) {
private final TimeUnit timeUnit;
TimespanUnit(String text, TimeUnit tu, int size) {
this.text = text;
this.nanos = nanos;
this.nanos = tu.toNanos(1);
this.size = size;
this.timeUnit = tu;
}
public long toNanos(long value) {
return timeUnit.toNanos(value);
}
public static TimespanUnit fromText(String text) {

@ -51,26 +51,12 @@ public final class ValueParser {
}
public static long parseTimespan(String s) {
if (s.endsWith("ns")) {
return Long.parseLong(s.substring(0, s.length() - 2).trim());
}
if (s.endsWith("us")) {
return MICROSECONDS.toNanos(Long.parseLong(s.substring(0, s.length() - 2).trim()));
}
if (s.endsWith("ms")) {
return MILLISECONDS.toNanos(Long.parseLong(s.substring(0, s.length() - 2).trim()));
}
if (s.endsWith("s")) {
return SECONDS.toNanos(Long.parseLong(s.substring(0, s.length() - 1).trim()));
}
if (s.endsWith("m")) {
return MINUTES.toNanos(Long.parseLong(s.substring(0, s.length() - 1).trim()));
}
if (s.endsWith("h")) {
return HOURS.toNanos(Long.parseLong(s.substring(0, s.length() - 1).trim()));
}
if (s.endsWith("d")) {
return DAYS.toNanos(Long.parseLong(s.substring(0, s.length() - 1).trim()));
for (TimespanUnit unit : TimespanUnit.values()) {
String text = unit.text;
if (s.endsWith(text)) {
long value = Long.parseLong(s.substring(0, s.length() - text.length()).strip());
return unit.toNanos(value);
}
}
try {