Skip to content

Commit

Permalink
8334765: JFR: Log chunk waste
Browse files Browse the repository at this point in the history
Reviewed-by: mgronlun
  • Loading branch information
egahlin committed Jun 24, 2024
1 parent b2930c5 commit 55c7969
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
Expand Down Expand Up @@ -30,6 +30,8 @@
import java.nio.file.Path;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Map;
import java.util.HashMap;
import java.util.function.Predicate;

import jdk.jfr.consumer.RecordedEvent;
Expand All @@ -56,6 +58,7 @@ public final class ChunkWriter implements Closeable {
private final RecordingInput input;
private final RecordingOutput output;
private final Predicate<RecordedEvent> filter;
private final Map<String, Long> waste = new HashMap<>();

private long chunkStartPosition;
private boolean chunkComplete;
Expand Down Expand Up @@ -178,6 +181,16 @@ public void endChunk(ChunkHeader header) throws IOException {
pools = new LongMap<>();
chunkComplete = true;
lastCheckpoint = 0;
if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.DEBUG)) {
// Log largest waste first
waste.entrySet().stream()
.sorted((a, b) -> b.getValue().compareTo(a.getValue()))
.forEach(entry -> {
String msg = "Total chunk waste by " + entry.getKey() + ": " + entry.getValue() + " bytes.";
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.DEBUG, msg);
});
}
waste.clear();
}

private void writeMetadataEvent(ChunkHeader header) throws IOException {
Expand Down Expand Up @@ -216,6 +229,20 @@ private void write(CheckpointEvent event, long delta) throws IOException {
}
}
}
if (Logger.shouldLog(LogTag.JFR_SYSTEM_PARSER, LogLevel.DEBUG)) {
for (CheckpointPool pool : event.getPools()) {
for (PoolEntry pe : pool.getEntries()) {
if (!pe.isTouched()) {
String name = pe.getType().getName();
long amount = pe.getEndPosition() - pe.getStartPosition();
waste.merge(pe.getType().getName(), amount, Long::sum);
String msg = "Unreferenced constant ID " + pe.getId() +
" of type "+ name + " using " + amount + " bytes.";
Logger.log(LogTag.JFR_SYSTEM_PARSER, LogLevel.TRACE, msg);
}
}
}
}
long endPosition = output.position();
long size = endPosition - startPosition;
output.position(startPosition);
Expand Down
4 changes: 2 additions & 2 deletions test/jdk/jdk/jfr/jvm/TestWaste.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2022, 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
Expand Down Expand Up @@ -47,7 +47,7 @@
* @requires vm.hasJFR
* @library /test/lib /test/jdk
* @modules jdk.jfr/jdk.jfr.internal.test
* @run main/othervm -XX:TLABSize=2k jdk.jfr.jvm.TestWaste
* @run main/othervm -Xlog:jfr+system+parser=debug -XX:TLABSize=2k jdk.jfr.jvm.TestWaste
*/
public class TestWaste {
static List<Object> list = new LinkedList<>();
Expand Down

0 comments on commit 55c7969

Please sign in to comment.