|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. |
| 8 | + * |
| 9 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 10 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 11 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 12 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 13 | + * accompanied this code). |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License version |
| 16 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 17 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | + * |
| 19 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 20 | + * or visit www.oracle.com if you need additional information or have any |
| 21 | + * questions. |
| 22 | + */ |
| 23 | +package jdk.jfr.event.runtime; |
| 24 | + |
| 25 | +import java.io.IOException; |
| 26 | +import java.nio.file.Files; |
| 27 | +import java.nio.file.Path; |
| 28 | +import java.time.Instant; |
| 29 | +import java.util.Collections; |
| 30 | +import java.util.LinkedHashSet; |
| 31 | +import java.util.Set; |
| 32 | + |
| 33 | +import jdk.jfr.Configuration; |
| 34 | +import jdk.jfr.Event; |
| 35 | +import jdk.jfr.Recording; |
| 36 | +import jdk.jfr.StackTrace; |
| 37 | +import jdk.jfr.consumer.RecordedClassLoader; |
| 38 | +import jdk.jfr.consumer.RecordingStream; |
| 39 | + |
| 40 | +/** |
| 41 | + * @test |
| 42 | + * @summary The test verifies that jdk.ClassLoaderStatistics and |
| 43 | + * jdk.ThreadThreadDump are not emitted at the beginning of a chunk |
| 44 | + * when the period is everyChunk, as is the case in default.jfc |
| 45 | + * @requires vm.flagless |
| 46 | + * @requires vm.hasJFR |
| 47 | + * @library /test/lib /test/jdk |
| 48 | + * @run main/othervm jdk.jfr.event.runtime.TestBackToBackSensitive |
| 49 | + */ |
| 50 | +public class TestBackToBackSensitive { |
| 51 | + @StackTrace(false) |
| 52 | + static class FillEvent extends Event { |
| 53 | + String message; |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String... arg) throws Exception { |
| 57 | + Set<Instant> threadDumps = Collections.synchronizedSet(new LinkedHashSet<>()); |
| 58 | + Set<Instant> classLoaderStatistics = Collections.synchronizedSet(new LinkedHashSet<>()); |
| 59 | + Set<Instant> physicalMemory = Collections.synchronizedSet(new LinkedHashSet<>()); |
| 60 | + |
| 61 | + Configuration configuration = Configuration.getConfiguration("default"); |
| 62 | + try (RecordingStream r1 = new RecordingStream(configuration)) { |
| 63 | + r1.setMaxSize(Long.MAX_VALUE); |
| 64 | + r1.onEvent("jdk.ThreadDump", e -> threadDumps.add(e.getStartTime())); |
| 65 | + r1.onEvent("jdk.ClassLoaderStatistics", e -> { |
| 66 | + RecordedClassLoader cl = e.getValue("classLoader"); |
| 67 | + if (cl != null) { |
| 68 | + if (cl.getType().getName().contains("PlatformClassLoader")) { |
| 69 | + classLoaderStatistics.add(e.getStartTime()); |
| 70 | + } |
| 71 | + } |
| 72 | + }); |
| 73 | + r1.onEvent("jdk.PhysicalMemory", e -> physicalMemory.add(e.getStartTime())); |
| 74 | + // Start chunk 1 |
| 75 | + r1.startAsync(); |
| 76 | + try (Recording r2 = new Recording()) { |
| 77 | + // Start chunk 2 |
| 78 | + r2.start(); |
| 79 | + // Starts chunk 3 |
| 80 | + r2.stop(); |
| 81 | + } |
| 82 | + // Start chunk 4 by filling up chunk 3 |
| 83 | + for (int i = 0; i < 1_500_000; i++) { |
| 84 | + FillEvent f = new FillEvent(); |
| 85 | + f.commit(); |
| 86 | + } |
| 87 | + r1.stop(); |
| 88 | + long chunkFiles = filesInRepository(); |
| 89 | + System.out.println("Number of chunk files: " + chunkFiles); |
| 90 | + // When jdk.ClassLoaderStatistics and jdk.ThreadThreadDump are expected to be |
| 91 | + // emitted: |
| 92 | + // Chunk 1: begin, end |
| 93 | + // Chunk 2: begin, end |
| 94 | + // Chunk 3: end |
| 95 | + // Chunk 4: end |
| 96 | + assertCount("jdk.ThreadDump", threadDumps, 2 + 2 + (chunkFiles - 2)); |
| 97 | + assertCount("jdk.ClassLoaderStatistics", classLoaderStatistics, 2 + 2 + (chunkFiles - 2)); |
| 98 | + // When jdk.PhysicalMemory is expected to be emitted: |
| 99 | + // Chunk 1: begin, end |
| 100 | + // Chunk 2: begin, end |
| 101 | + // Chunk 3: begin, end |
| 102 | + // Chunk 4: begin, end |
| 103 | + assertCount("jdk.PhysicalMemory", physicalMemory, 2 * chunkFiles); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private static long filesInRepository() throws IOException { |
| 108 | + Path repository = Path.of(System.getProperty("jdk.jfr.repository")); |
| 109 | + return Files.list(repository).filter(p -> p.toString().endsWith(".jfr")).count(); |
| 110 | + } |
| 111 | + |
| 112 | + private static void assertCount(String eventName, Set<Instant> timestamps, long expected) throws Exception { |
| 113 | + System.out.println("Timestamps for " + eventName + ":"); |
| 114 | + for (Instant timestamp : timestamps) { |
| 115 | + System.out.println(timestamp); |
| 116 | + } |
| 117 | + int count = timestamps.size(); |
| 118 | + if (count != expected) { |
| 119 | + throw new Exception("Expected " + expected + " timestamps for event " + eventName + ", but got " + count); |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments