50113
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
|
|
26 |
package jdk.jfr.api.event;
|
|
27 |
|
|
28 |
import java.time.Duration;
|
|
29 |
|
|
30 |
import jdk.jfr.Event;
|
|
31 |
import jdk.jfr.Recording;
|
|
32 |
import jdk.test.lib.Asserts;
|
|
33 |
|
|
34 |
/*
|
|
35 |
* @test
|
|
36 |
* @summary Test enable/disable event and verify recording has expected events.
|
|
37 |
* @key jfr
|
|
38 |
* @library /test/lib
|
|
39 |
* @run main/othervm -Xlog:jfr+event+setting=trace jdk.jfr.api.event.TestShouldCommit
|
|
40 |
*/
|
|
41 |
|
|
42 |
public class TestShouldCommit {
|
|
43 |
|
|
44 |
public static void main(String[] args) throws Exception {
|
|
45 |
Recording rA = new Recording();
|
|
46 |
|
|
47 |
verifyShouldCommitFalse(); // No active recordings
|
|
48 |
|
|
49 |
rA.start();
|
|
50 |
rA.enable(MyEvent.class).withoutThreshold(); // recA=all
|
|
51 |
verifyShouldCommitTrue();
|
|
52 |
|
|
53 |
setThreshold(rA, 100); // recA=100
|
|
54 |
verifyThreshold(100);
|
|
55 |
|
|
56 |
setThreshold(rA, 200); // recA=200
|
|
57 |
verifyThreshold(200);
|
|
58 |
|
|
59 |
Recording rB = new Recording();
|
|
60 |
verifyThreshold(200); // recA=200, recB=not started
|
|
61 |
|
|
62 |
rB.start();
|
|
63 |
verifyThreshold(200); // recA=200, recB=not specified, settings from recA is used.
|
|
64 |
|
|
65 |
setThreshold(rB, 100); // recA=200, recB=100
|
|
66 |
verifyThreshold(100);
|
|
67 |
|
|
68 |
setThreshold(rB, 300); // recA=200, recB=300
|
|
69 |
verifyThreshold(200);
|
|
70 |
|
|
71 |
rA.disable(MyEvent.class); // recA=disabled, recB=300
|
|
72 |
|
|
73 |
verifyThreshold(300);
|
|
74 |
|
|
75 |
rB.disable(MyEvent.class); // recA=disabled, recB=disabled
|
|
76 |
verifyShouldCommitFalse();
|
|
77 |
|
|
78 |
setThreshold(rA, 200); // recA=200, recB=disabled
|
|
79 |
verifyThreshold(200);
|
|
80 |
|
|
81 |
rB.enable(MyEvent.class).withoutThreshold(); // recA=200, recB=all
|
|
82 |
verifyShouldCommitTrue();
|
|
83 |
|
|
84 |
setThreshold(rB, 100); // recA=200, recB=100
|
|
85 |
verifyThreshold(100);
|
|
86 |
|
|
87 |
rB.stop(); // recA=200, recB=stopped
|
|
88 |
verifyThreshold(200);
|
|
89 |
|
|
90 |
rA.stop(); // recA=stopped, recB=stopped
|
|
91 |
verifyShouldCommitFalse();
|
|
92 |
|
|
93 |
rA.close();
|
|
94 |
rB.close();
|
|
95 |
|
|
96 |
verifyShouldCommitFalse();
|
|
97 |
}
|
|
98 |
|
|
99 |
private static void setThreshold(Recording r, long millis) {
|
|
100 |
r.enable(MyEvent.class).withThreshold(Duration.ofMillis(millis));
|
|
101 |
}
|
|
102 |
|
|
103 |
private static void verifyThreshold(long threshold) throws Exception {
|
|
104 |
// Create 2 events, with different sleep time between begin() and end()
|
|
105 |
// First event ends just before threshold, the other just after.
|
|
106 |
verifyThreshold(threshold-5, threshold);
|
|
107 |
verifyThreshold(threshold+5, threshold);
|
|
108 |
}
|
|
109 |
|
|
110 |
private static void verifyThreshold(long sleepMs, long thresholdMs) throws Exception {
|
|
111 |
MyEvent event = new MyEvent();
|
|
112 |
|
|
113 |
long beforeStartNanos = System.nanoTime();
|
|
114 |
event.begin();
|
|
115 |
long afterStartNanos = System.nanoTime();
|
|
116 |
|
|
117 |
Thread.sleep(sleepMs);
|
|
118 |
|
|
119 |
long beforeStopNanos = System.nanoTime();
|
|
120 |
event.end();
|
|
121 |
long afterStopNanos = System.nanoTime();
|
|
122 |
|
|
123 |
boolean actualShouldCommit = event.shouldCommit();
|
|
124 |
|
|
125 |
final long safetyMarginNanos = 2000000; // Allow an error of 2 ms. May have to be tuned.
|
|
126 |
|
|
127 |
//Duration of event has been at least minDurationMicros
|
|
128 |
long minDurationMicros = (beforeStopNanos - afterStartNanos - safetyMarginNanos) / 1000;
|
|
129 |
//Duration of event has been at most maxDurationMicros
|
|
130 |
long maxDurationMicros = (afterStopNanos - beforeStartNanos + safetyMarginNanos) / 1000;
|
|
131 |
Asserts.assertLessThanOrEqual(minDurationMicros, maxDurationMicros, "Wrong min/max duration. Test error.");
|
|
132 |
|
|
133 |
long thresholdMicros = thresholdMs * 1000;
|
|
134 |
Boolean shouldCommit = null;
|
|
135 |
if (minDurationMicros > thresholdMicros) {
|
|
136 |
shouldCommit = new Boolean(true); // shouldCommit() must be true
|
|
137 |
} else if (maxDurationMicros < thresholdMicros) {
|
|
138 |
shouldCommit = new Boolean(false); // shouldCommit() must be false
|
|
139 |
} else {
|
|
140 |
// Too close to call. No checks are done since we are not sure of expected shouldCommit().
|
|
141 |
}
|
|
142 |
|
|
143 |
System.out.printf(
|
|
144 |
"threshold=%d, duration=[%d-%d], shouldCommit()=%b, expected=%s%n",
|
|
145 |
thresholdMicros, minDurationMicros, maxDurationMicros, actualShouldCommit,
|
|
146 |
(shouldCommit!=null ? shouldCommit : "too close to call"));
|
|
147 |
|
|
148 |
try {
|
|
149 |
if (shouldCommit != null) {
|
|
150 |
Asserts.assertEquals(shouldCommit.booleanValue(), actualShouldCommit, "Wrong shouldCommit()");
|
|
151 |
}
|
|
152 |
} catch (Exception e) {
|
|
153 |
System.out.println("Unexpected value of shouldCommit(). Searching for active threshold...");
|
|
154 |
searchThreshold(thresholdMs, 2000+thresholdMs);
|
|
155 |
throw e;
|
|
156 |
}
|
|
157 |
}
|
|
158 |
|
|
159 |
// Sleeps until shouldCommit() is true, or give up. Used for logging.
|
|
160 |
private static void searchThreshold(long expectedMs, long maxMs) throws Exception {
|
|
161 |
long start = System.nanoTime();
|
|
162 |
long stop = start + maxMs * 1000000;
|
|
163 |
|
|
164 |
MyEvent event = new MyEvent();
|
|
165 |
event.begin();
|
|
166 |
event.end();
|
|
167 |
|
|
168 |
while (!event.shouldCommit() && System.nanoTime() < stop) {
|
|
169 |
Thread.sleep(1);
|
|
170 |
event.end();
|
|
171 |
}
|
|
172 |
long durationMicros = (System.nanoTime() - start) / 1000;
|
|
173 |
long expectedMicros = expectedMs * 1000;
|
|
174 |
System.out.printf("shouldCommit()=%b after %,d ms, expected %,d%n", event.shouldCommit(), durationMicros, expectedMicros);
|
|
175 |
}
|
|
176 |
|
|
177 |
private static void verifyShouldCommitFalse() {
|
|
178 |
MyEvent event = new MyEvent();
|
|
179 |
event.begin();
|
|
180 |
event.end();
|
|
181 |
Asserts.assertFalse(event.shouldCommit(), "shouldCommit() expected false");
|
|
182 |
}
|
|
183 |
|
|
184 |
private static void verifyShouldCommitTrue() {
|
|
185 |
MyEvent event = new MyEvent();
|
|
186 |
event.begin();
|
|
187 |
event.end();
|
|
188 |
Asserts.assertTrue(event.shouldCommit(), "shouldCommit() expected true");
|
|
189 |
}
|
|
190 |
|
|
191 |
private static class MyEvent extends Event {
|
|
192 |
}
|
|
193 |
|
|
194 |
}
|