< prev index next >

test/jdk/jfr/event/gc/heapsummary/HeapSummaryEventAllGcs.java

Print this page
rev 13657 : 8223692: Add JFR G1 Heap Summary Event Support


  24  */
  25 
  26 package jdk.jfr.event.gc.heapsummary;
  27 
  28 import java.time.Duration;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.jfr.EventNames;
  35 import jdk.test.lib.jfr.Events;
  36 import jdk.test.lib.jfr.GCHelper;
  37 
  38 public class HeapSummaryEventAllGcs {
  39 
  40     public static void test(String expectedYoungCollector, String expectedOldCollector) throws Exception {
  41         Recording recording = new Recording();
  42         recording.enable(EventNames.GCConfiguration);
  43         recording.enable(EventNames.GCHeapSummary);

  44         recording.enable(EventNames.PSHeapSummary);
  45         recording.enable(EventNames.MetaspaceSummary).withThreshold(Duration.ofMillis(0));
  46 
  47         recording.start();
  48         // To eliminate the risk of being in the middle of a GC when the recording starts/stops,
  49         // we run 5 System.gc() and ignores the first and last GC.
  50         GCHelper.callSystemGc(5, true);
  51         recording.stop();
  52 
  53         if (!checkCollectors(recording, expectedYoungCollector, expectedOldCollector)) {
  54             return;
  55         }
  56         List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(Events.fromRecording(recording));
  57         for (RecordedEvent event : events) {
  58             System.out.println("Event:" + event);
  59         }
  60 
  61         Asserts.assertFalse(events.isEmpty(), "Expected at least one event.");
  62         Asserts.assertEquals(events.size() % 2, 0, "Events should come in pairs");
  63 
  64         int lastHeapGcId = -1;

  65         int lastPSGcId = -1;
  66         int lastMetaspaceGcId = -1;
  67 
  68         for (RecordedEvent event : events) {
  69             final String eventName = event.getEventType().getName();
  70             switch (eventName) {
  71                 case EventNames.GCHeapSummary:
  72                     lastHeapGcId = checkGcId(event, lastHeapGcId);
  73                     checkHeapEventContent(event);
  74                     break;




  75                 case EventNames.PSHeapSummary:
  76                     lastPSGcId = checkGcId(event, lastPSGcId);
  77                     checkPSEventContent(event);
  78                     break;
  79                 case EventNames.MetaspaceSummary:
  80                     lastMetaspaceGcId = checkGcId(event, lastMetaspaceGcId);
  81                     checkMetaspaceEventContent(event);
  82                     break;
  83                 default:
  84                     System.out.println("Failed event: " + event);
  85                     Asserts.fail("Unknown event type: " + eventName);
  86             }
  87         }
  88 
  89         // Sanity check. Not complete.
  90         Asserts.assertEquals(lastHeapGcId, lastMetaspaceGcId, "Should have gotten perm gen events for all GCs");
  91     }
  92 
  93     private static void checkMetaspaceEventContent(RecordedEvent event) {
  94         long totalUsed = Events.assertField(event, "metaspace.used").atLeast(0L).getValue();


 108         Asserts.assertEquals(dataReserved + classReserved, totalReserved, "Wrong reserved memory");
 109     }
 110 
 111     private static int checkGcId(RecordedEvent event, int currGcId) {
 112         int gcId = Events.assertField(event, "gcId").getValue();
 113         String when = Events.assertField(event, "when").notEmpty().getValue();
 114         if ("Before GC".equals(when)) {
 115             Asserts.assertGreaterThan(gcId, currGcId, "gcId should be increasing");
 116         } else {
 117             Asserts.assertEquals(gcId, currGcId, "After should have same gcId as last Before event");
 118         }
 119         return gcId;
 120     }
 121 
 122     private static void checkHeapEventContent(RecordedEvent event) {
 123         checkVirtualSpace(event, "heapSpace");
 124         long heapUsed = Events.assertField(event, "heapUsed").atLeast(0L).getValue();
 125         long start = Events.assertField(event, "heapSpace.start").atLeast(0L).getValue();
 126         long committedEnd = Events.assertField(event, "heapSpace.committedEnd").above(start).getValue();
 127         Asserts.assertLessThanOrEqual(heapUsed, committedEnd- start, "used can not exceed size");








 128     }
 129 
 130     private static void checkPSEventContent(RecordedEvent event) {
 131         checkVirtualSpace(event, "oldSpace");
 132         checkVirtualSpace(event, "youngSpace");
 133         checkSpace(event, "oldObjectSpace");
 134         checkSpace(event, "edenSpace");
 135         checkSpace(event, "fromSpace");
 136         checkSpace(event, "toSpace");
 137 
 138         checkPSYoungSizes(event);
 139         checkPSYoungStartEnd(event);
 140     }
 141 
 142     private static void checkPSYoungSizes(RecordedEvent event) {
 143         long youngSize = (long)Events.assertField(event, "youngSpace.committedEnd").getValue() -
 144                         (long)Events.assertField(event, "youngSpace.start").getValue();
 145         long edenSize = (long)Events.assertField(event, "edenSpace.end").getValue() -
 146                         (long)Events.assertField(event, "edenSpace.start").getValue();
 147         long fromSize = (long)Events.assertField(event, "fromSpace.end").getValue() -




  24  */
  25 
  26 package jdk.jfr.event.gc.heapsummary;
  27 
  28 import java.time.Duration;
  29 import java.util.List;
  30 
  31 import jdk.jfr.Recording;
  32 import jdk.jfr.consumer.RecordedEvent;
  33 import jdk.test.lib.Asserts;
  34 import jdk.test.lib.jfr.EventNames;
  35 import jdk.test.lib.jfr.Events;
  36 import jdk.test.lib.jfr.GCHelper;
  37 
  38 public class HeapSummaryEventAllGcs {
  39 
  40     public static void test(String expectedYoungCollector, String expectedOldCollector) throws Exception {
  41         Recording recording = new Recording();
  42         recording.enable(EventNames.GCConfiguration);
  43         recording.enable(EventNames.GCHeapSummary);
  44         recording.enable(EventNames.G1HeapSummary);
  45         recording.enable(EventNames.PSHeapSummary);
  46         recording.enable(EventNames.MetaspaceSummary).withThreshold(Duration.ofMillis(0));
  47 
  48         recording.start();
  49         // To eliminate the risk of being in the middle of a GC when the recording starts/stops,
  50         // we run 5 System.gc() and ignores the first and last GC.
  51         GCHelper.callSystemGc(5, true);
  52         recording.stop();
  53 
  54         if (!checkCollectors(recording, expectedYoungCollector, expectedOldCollector)) {
  55             return;
  56         }
  57         List<RecordedEvent> events = GCHelper.removeFirstAndLastGC(Events.fromRecording(recording));
  58         for (RecordedEvent event : events) {
  59             System.out.println("Event:" + event);
  60         }
  61 
  62         Asserts.assertFalse(events.isEmpty(), "Expected at least one event.");
  63         Asserts.assertEquals(events.size() % 2, 0, "Events should come in pairs");
  64 
  65         int lastHeapGcId = -1;
  66         int lastG1GcId = -1;
  67         int lastPSGcId = -1;
  68         int lastMetaspaceGcId = -1;
  69 
  70         for (RecordedEvent event : events) {
  71             final String eventName = event.getEventType().getName();
  72             switch (eventName) {
  73                 case EventNames.GCHeapSummary:
  74                     lastHeapGcId = checkGcId(event, lastHeapGcId);
  75                     checkHeapEventContent(event);
  76                     break;
  77                 case EventNames.G1HeapSummary:
  78                     lastG1GcId = checkGcId(event, lastG1GcId);
  79                     checkG1EventContent(event);
  80                     break;
  81                 case EventNames.PSHeapSummary:
  82                     lastPSGcId = checkGcId(event, lastPSGcId);
  83                     checkPSEventContent(event);
  84                     break;
  85                 case EventNames.MetaspaceSummary:
  86                     lastMetaspaceGcId = checkGcId(event, lastMetaspaceGcId);
  87                     checkMetaspaceEventContent(event);
  88                     break;
  89                 default:
  90                     System.out.println("Failed event: " + event);
  91                     Asserts.fail("Unknown event type: " + eventName);
  92             }
  93         }
  94 
  95         // Sanity check. Not complete.
  96         Asserts.assertEquals(lastHeapGcId, lastMetaspaceGcId, "Should have gotten perm gen events for all GCs");
  97     }
  98 
  99     private static void checkMetaspaceEventContent(RecordedEvent event) {
 100         long totalUsed = Events.assertField(event, "metaspace.used").atLeast(0L).getValue();


 114         Asserts.assertEquals(dataReserved + classReserved, totalReserved, "Wrong reserved memory");
 115     }
 116 
 117     private static int checkGcId(RecordedEvent event, int currGcId) {
 118         int gcId = Events.assertField(event, "gcId").getValue();
 119         String when = Events.assertField(event, "when").notEmpty().getValue();
 120         if ("Before GC".equals(when)) {
 121             Asserts.assertGreaterThan(gcId, currGcId, "gcId should be increasing");
 122         } else {
 123             Asserts.assertEquals(gcId, currGcId, "After should have same gcId as last Before event");
 124         }
 125         return gcId;
 126     }
 127 
 128     private static void checkHeapEventContent(RecordedEvent event) {
 129         checkVirtualSpace(event, "heapSpace");
 130         long heapUsed = Events.assertField(event, "heapUsed").atLeast(0L).getValue();
 131         long start = Events.assertField(event, "heapSpace.start").atLeast(0L).getValue();
 132         long committedEnd = Events.assertField(event, "heapSpace.committedEnd").above(start).getValue();
 133         Asserts.assertLessThanOrEqual(heapUsed, committedEnd- start, "used can not exceed size");
 134     }
 135 
 136     private static void checkG1EventContent(RecordedEvent event) {
 137         long edenUsedSize = Events.assertField(event, "edenUsedSize").atLeast(0L).getValue();
 138         long edenTotalSize = Events.assertField(event, "edenTotalSize").atLeast(0L).getValue();
 139         Asserts.assertLessThanOrEqual(edenUsedSize, edenTotalSize, "used can not exceed size");
 140         Events.assertField(event, "survivorUsedSize").atLeast(0L);
 141         Events.assertField(event, "numberOfRegions").atLeast(0);
 142     }
 143 
 144     private static void checkPSEventContent(RecordedEvent event) {
 145         checkVirtualSpace(event, "oldSpace");
 146         checkVirtualSpace(event, "youngSpace");
 147         checkSpace(event, "oldObjectSpace");
 148         checkSpace(event, "edenSpace");
 149         checkSpace(event, "fromSpace");
 150         checkSpace(event, "toSpace");
 151 
 152         checkPSYoungSizes(event);
 153         checkPSYoungStartEnd(event);
 154     }
 155 
 156     private static void checkPSYoungSizes(RecordedEvent event) {
 157         long youngSize = (long)Events.assertField(event, "youngSpace.committedEnd").getValue() -
 158                         (long)Events.assertField(event, "youngSpace.start").getValue();
 159         long edenSize = (long)Events.assertField(event, "edenSpace.end").getValue() -
 160                         (long)Events.assertField(event, "edenSpace.start").getValue();
 161         long fromSize = (long)Events.assertField(event, "fromSpace.end").getValue() -


< prev index next >