1 /*
 2  * Copyright (c) 2019, 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 
24 package jdk.jfr.event.oldobject;
25 
26 import java.time.Duration;
27 import java.util.ArrayList;
28 import java.util.List;
29 
30 import jdk.jfr.Recording;
31 import jdk.jfr.consumer.RecordedEvent;
32 import jdk.jfr.internal.test.WhiteBox;
33 import jdk.test.lib.jfr.EventNames;
34 import jdk.test.lib.jfr.Events;
35 
36 /**
37  * @test
38  * @requires vm.flagless
39  * @requires vm.hasJFR
40  * @requires !(vm.opt.final.UseCompactObjectHeaders == true | vm.opt.final.UseShenandoahGC == true)
41  * @library /test/lib /test/jdk
42  * @modules jdk.jfr/jdk.jfr.internal.test
43  * @run main/othervm -XX:TLABSize=2k jdk.jfr.event.oldobject.TestObjectAge
44  */
45 public class TestObjectAge {
46 
47     public final static List<Object[]> leak = new ArrayList<>(OldObjects.MIN_SIZE);
48 
49     public static void main(String[] args) throws Exception {
50         WhiteBox.setWriteAllObjectSamples(true);
51 
52         try (Recording recording = new Recording()) {
53             recording.enable(EventNames.OldObjectSample).withStackTrace().with("cutoff", "1 h");
54             recording.start();
55 
56             // Allocate array to trigger sampling code path for interpreter / c1
57             for (int i = 0; i < OldObjects.MIN_SIZE; i++) {
58                 leak.add(new Object[0]);
59             }
60             recording.stop();
61 
62             List<RecordedEvent> events = Events.fromRecording(recording);
63             // OK if we sometimes don't get an event
64             for (RecordedEvent event : events) {
65                 Duration objectAge = event.getDuration("objectAge");
66                 // Sanity check
67                 if (objectAge.isNegative() || objectAge.toMinutes() > 60) {
68                     throw new Exception("Object age " + objectAge + " is not reasonable");
69                 }
70             }
71         }
72     }
73 }