Skip to content

Commit

Permalink
additional minor optimization in UnifiedG1GCParser
Browse files Browse the repository at this point in the history
  • Loading branch information
dsgrieve committed Jun 15, 2023
1 parent 07194a2 commit f111734
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 13 deletions.
3 changes: 2 additions & 1 deletion api/src/main/java/com/microsoft/gctoolkit/GCToolKit.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import static java.lang.Class.forName;
Expand Down Expand Up @@ -305,7 +304,9 @@ public JavaVirtualMachine analyze(DataSource<?> dataSource) throws IOException
JavaVirtualMachine javaVirtualMachine = loadJavaVirtualMachine(logFile);
try {
List<Aggregator<? extends Aggregation>> filteredAggregators = filterAggregations(events);
long start = System.currentTimeMillis();
javaVirtualMachine.analyze(filteredAggregators, jvmEventChannel, dataSourceChannel);
LOGGER.log(Level.FINE,() -> "Analysis completed in " + (System.currentTimeMillis() - start) + "ms");
} catch(Throwable t) {
LOGGER.log(Level.SEVERE, "Internal Error: Cannot invoke analyze method", t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@ public String getName() {
public String toString() {
return this.name + " -> " + pattern.toString();
}

public Pattern pattern() { return pattern; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
import java.util.function.BiConsumer;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.microsoft.gctoolkit.event.GarbageCollectionTypes.fromLabel;

Expand Down Expand Up @@ -172,14 +174,32 @@ protected void process(String line) {
parse(line);
}

private static final Pattern gcIdPattern = GCLogParser.GCID_COUNTER.pattern();

private void parse(String line) {

// Minor optimization. The parse rule only applies to what comes after the GC ID.
final int end;
final int gcid;
final Matcher gcIdMatcher = gcIdPattern.matcher(line);
if (gcIdMatcher.find()) {
gcid = Integer.parseInt(gcIdMatcher.group(1));
end = gcIdMatcher.end();
} else {
gcid = -1;
end = 0;
}

final String lineAfterGcId = line.substring(end);

parseRules.stream()
.map(Map.Entry::getKey)
.map(rule -> new AbstractMap.SimpleEntry<>(rule, rule.parse(line)))
.map(rule -> new AbstractMap.SimpleEntry<>(rule, rule.parse(lineAfterGcId)))
.filter(tuple -> tuple.getValue() != null)
.findAny()
.ifPresentOrElse(
tuple -> {
setForwardReference(gcid, line.substring(0, end));
applyRule(tuple.getKey(), tuple.getValue(), line);
},
() -> log(line)
Expand All @@ -189,23 +209,15 @@ private void parse(String line) {

private void applyRule(GCParseRule ruleToApply, GCLogTrace trace, String line) {
try {
setForwardReference(line);
parseRules.select(ruleToApply).accept(trace, line);
} catch (Throwable t) {
LOGGER.throwing(this.getName(), "process", t);
}
}

private void setForwardReference(String line) {
GCLogTrace trace = GCID_COUNTER.parse(line);
if (trace != null) {
int gcid = trace.getIntegerGroup(1);
forwardReference = collectionsUnderway.get(gcid);
if (forwardReference == null) {
forwardReference = new G1GCForwardReference(new Decorators(line), trace.getIntegerGroup(1));
collectionsUnderway.put(forwardReference.getGcID(), forwardReference);
} else if (gcid != forwardReference.getGcID())
forwardReference = collectionsUnderway.get(gcid);
private void setForwardReference(int gcid, String line) {
if (gcid != -1) {
forwardReference = collectionsUnderway.computeIfAbsent(gcid, k -> new G1GCForwardReference(new Decorators(line), gcid));
}
}

Expand Down

0 comments on commit f111734

Please sign in to comment.