|
16 | 16 | package io.netty.buffer; |
17 | 17 |
|
18 | 18 | import io.netty.util.NettyRuntime; |
19 | | -import io.netty.util.concurrent.FastThreadLocalThread; |
20 | | -import jdk.jfr.consumer.RecordedEvent; |
21 | | -import jdk.jfr.consumer.RecordingStream; |
| 19 | +import io.netty.util.test.DisabledForSlowLeakDetection; |
22 | 20 | import org.junit.jupiter.api.RepeatedTest; |
23 | 21 | import org.junit.jupiter.api.RepetitionInfo; |
24 | 22 | import org.junit.jupiter.api.Test; |
25 | | -import org.junit.jupiter.api.Timeout; |
26 | | -import org.junit.jupiter.api.condition.EnabledForJreRange; |
27 | | -import org.junit.jupiter.api.condition.JRE; |
28 | 23 | import org.junit.jupiter.params.ParameterizedTest; |
29 | 24 | import org.junit.jupiter.params.provider.ValueSource; |
30 | 25 |
|
31 | 26 | import java.lang.reflect.Array; |
32 | 27 | import java.util.ArrayDeque; |
33 | | -import java.util.ArrayList; |
34 | 28 | import java.util.Arrays; |
35 | 29 | import java.util.Deque; |
36 | | -import java.util.List; |
37 | 30 | import java.util.SplittableRandom; |
38 | | -import java.util.concurrent.Callable; |
39 | | -import java.util.concurrent.CompletableFuture; |
40 | 31 | import java.util.concurrent.CountDownLatch; |
41 | | -import java.util.concurrent.FutureTask; |
42 | 32 | import java.util.concurrent.ThreadLocalRandom; |
43 | | -import java.util.concurrent.atomic.AtomicInteger; |
44 | 33 | import java.util.concurrent.atomic.AtomicReference; |
45 | 34 |
|
46 | 35 | import static org.junit.jupiter.api.Assertions.assertEquals; |
@@ -223,175 +212,7 @@ public void run() { |
223 | 212 | } |
224 | 213 | } |
225 | 214 |
|
226 | | - @SuppressWarnings("Since15") |
227 | | - @Test |
228 | | - @EnabledForJreRange(min = JRE.JAVA_17) // RecordingStream |
229 | | - @Timeout(10) |
230 | | - public void jfrChunkAllocation() throws Exception { |
231 | | - try (RecordingStream stream = new RecordingStream()) { |
232 | | - CompletableFuture<RecordedEvent> allocateFuture = new CompletableFuture<>(); |
233 | | - |
234 | | - stream.enable(AllocateChunkEvent.class); |
235 | | - stream.onEvent(AllocateChunkEvent.NAME, allocateFuture::complete); |
236 | | - stream.startAsync(); |
237 | | - |
238 | | - AdaptiveByteBufAllocator alloc = new AdaptiveByteBufAllocator(true, false); |
239 | | - alloc.directBuffer(128).release(); |
240 | | - |
241 | | - RecordedEvent allocate = allocateFuture.get(); |
242 | | - assertEquals(AdaptivePoolingAllocator.MIN_CHUNK_SIZE, allocate.getInt("capacity")); |
243 | | - assertTrue(allocate.getBoolean("pooled")); |
244 | | - assertFalse(allocate.getBoolean("threadLocal")); |
245 | | - assertTrue(allocate.getBoolean("direct")); |
246 | | - } |
247 | | - } |
248 | | - |
249 | | - @SuppressWarnings("Since15") |
250 | | - @Test |
251 | | - @EnabledForJreRange(min = JRE.JAVA_17) // RecordingStream |
252 | | - @Timeout(10) |
253 | | - public void shouldCreateTwoChunks() throws Exception { |
254 | | - try (RecordingStream stream = new RecordingStream()) { |
255 | | - final CountDownLatch eventsFlushed = new CountDownLatch(2); |
256 | | - stream.enable(AllocateChunkEvent.class); |
257 | | - stream.onEvent(AllocateChunkEvent.NAME, |
258 | | - event -> { |
259 | | - eventsFlushed.countDown(); |
260 | | - }); |
261 | | - stream.startAsync(); |
262 | | - ByteBufAllocator allocator = newAllocator(false); |
263 | | - int bufSize = 16896; |
264 | | - int minSegmentsPerChunk = 32; // See AdaptivePoolingAllocator.SizeClassChunkController. |
265 | | - int bufsToAllocate = 1 + minSegmentsPerChunk; |
266 | | - List<ByteBuf> buffers = new ArrayList<>(bufsToAllocate); |
267 | | - for (int i = 0; i < bufsToAllocate; ++i) { |
268 | | - buffers.add(allocator.heapBuffer(bufSize, bufSize)); |
269 | | - } |
270 | | - // release all buffers |
271 | | - for (ByteBuf buffer : buffers) { |
272 | | - buffer.release(); |
273 | | - } |
274 | | - buffers.clear(); |
275 | | - eventsFlushed.await(); |
276 | | - assertEquals(0, eventsFlushed.getCount()); |
277 | | - } |
278 | | - } |
279 | | - |
280 | | - @SuppressWarnings("Since15") |
281 | | - @Test |
282 | | - @EnabledForJreRange(min = JRE.JAVA_17) // RecordingStream |
283 | | - @Timeout(10) |
284 | | - public void shouldReuseTheSameChunk() throws Exception { |
285 | | - try (RecordingStream stream = new RecordingStream()) { |
286 | | - final CountDownLatch eventsFlushed = new CountDownLatch(1); |
287 | | - final AtomicInteger chunksAllocations = new AtomicInteger(); |
288 | | - stream.enable(AllocateChunkEvent.class); |
289 | | - stream.onEvent(AllocateChunkEvent.NAME, |
290 | | - event -> { |
291 | | - chunksAllocations.incrementAndGet(); |
292 | | - eventsFlushed.countDown(); |
293 | | - }); |
294 | | - stream.startAsync(); |
295 | | - int bufSize = 16896; |
296 | | - ByteBufAllocator allocator = newAllocator(false); |
297 | | - List<ByteBuf> buffers = new ArrayList<>(32); |
298 | | - for (int i = 0; i < 30; ++i) { |
299 | | - buffers.add(allocator.heapBuffer(bufSize, bufSize)); |
300 | | - } |
301 | | - // we still have 2 available segments in the chunk, so we should not allocate a new one |
302 | | - for (int i = 0; i < 128; ++i) { |
303 | | - allocator.heapBuffer(bufSize, bufSize).release(); |
304 | | - } |
305 | | - // release all buffers |
306 | | - for (ByteBuf buffer : buffers) { |
307 | | - buffer.release(); |
308 | | - } |
309 | | - buffers.clear(); |
310 | | - eventsFlushed.await(); |
311 | | - assertEquals(1, chunksAllocations.get()); |
312 | | - } |
313 | | - } |
314 | | - |
315 | | - @SuppressWarnings("Since15") |
316 | | - @Test |
317 | | - @EnabledForJreRange(min = JRE.JAVA_17) // RecordingStream |
318 | | - @Timeout(10) |
319 | | - public void jfrBufferAllocation() throws Exception { |
320 | | - try (RecordingStream stream = new RecordingStream()) { |
321 | | - CompletableFuture<RecordedEvent> allocateFuture = new CompletableFuture<>(); |
322 | | - CompletableFuture<RecordedEvent> releaseFuture = new CompletableFuture<>(); |
323 | | - |
324 | | - stream.enable(AllocateBufferEvent.class); |
325 | | - stream.onEvent(AllocateBufferEvent.NAME, allocateFuture::complete); |
326 | | - stream.enable(FreeBufferEvent.class); |
327 | | - stream.onEvent(FreeBufferEvent.NAME, releaseFuture::complete); |
328 | | - stream.startAsync(); |
329 | | - |
330 | | - AdaptiveByteBufAllocator alloc = new AdaptiveByteBufAllocator(true, false); |
331 | | - alloc.directBuffer(128).release(); |
332 | | - |
333 | | - RecordedEvent allocate = allocateFuture.get(); |
334 | | - assertEquals(128, allocate.getInt("size")); |
335 | | - assertEquals(128, allocate.getInt("maxFastCapacity")); |
336 | | - assertEquals(Integer.MAX_VALUE, allocate.getInt("maxCapacity")); |
337 | | - assertTrue(allocate.getBoolean("chunkPooled")); |
338 | | - assertFalse(allocate.getBoolean("chunkThreadLocal")); |
339 | | - assertTrue(allocate.getBoolean("direct")); |
340 | | - |
341 | | - RecordedEvent release = releaseFuture.get(); |
342 | | - assertEquals(128, release.getInt("size")); |
343 | | - assertEquals(128, release.getInt("maxFastCapacity")); |
344 | | - assertEquals(Integer.MAX_VALUE, release.getInt("maxCapacity")); |
345 | | - assertTrue(release.getBoolean("direct")); |
346 | | - } |
347 | | - } |
348 | | - |
349 | | - @SuppressWarnings("Since15") |
350 | | - @Test |
351 | | - @EnabledForJreRange(min = JRE.JAVA_17) // RecordingStream |
352 | | - @Timeout(10) |
353 | | - public void jfrBufferAllocationThreadLocal() throws Exception { |
354 | | - ByteBufAllocator alloc = new AdaptiveByteBufAllocator(true, true); |
355 | | - |
356 | | - Callable<Void> allocateAndRelease = () -> { |
357 | | - try (RecordingStream stream = new RecordingStream()) { |
358 | | - CompletableFuture<RecordedEvent> allocateFuture = new CompletableFuture<>(); |
359 | | - CompletableFuture<RecordedEvent> releaseFuture = new CompletableFuture<>(); |
360 | | - |
361 | | - // Prime the cache. |
362 | | - alloc.directBuffer(128).release(); |
363 | | - |
364 | | - stream.enable(AllocateBufferEvent.class); |
365 | | - stream.onEvent(AllocateBufferEvent.NAME, allocateFuture::complete); |
366 | | - stream.enable(FreeBufferEvent.class); |
367 | | - stream.onEvent(FreeBufferEvent.NAME, releaseFuture::complete); |
368 | | - stream.startAsync(); |
369 | | - |
370 | | - // Allocate out of the cache. |
371 | | - alloc.directBuffer(128).release(); |
372 | | - |
373 | | - RecordedEvent allocate = allocateFuture.get(); |
374 | | - assertEquals(128, allocate.getInt("size")); |
375 | | - assertEquals(128, allocate.getInt("maxFastCapacity")); |
376 | | - assertEquals(Integer.MAX_VALUE, allocate.getInt("maxCapacity")); |
377 | | - assertTrue(allocate.getBoolean("chunkPooled")); |
378 | | - assertTrue(allocate.getBoolean("chunkThreadLocal")); |
379 | | - assertTrue(allocate.getBoolean("direct")); |
380 | | - |
381 | | - RecordedEvent release = releaseFuture.get(); |
382 | | - assertEquals(128, release.getInt("size")); |
383 | | - assertEquals(128, release.getInt("maxFastCapacity")); |
384 | | - assertEquals(Integer.MAX_VALUE, release.getInt("maxCapacity")); |
385 | | - assertTrue(release.getBoolean("direct")); |
386 | | - return null; |
387 | | - } |
388 | | - }; |
389 | | - FutureTask<Void> task = new FutureTask<>(allocateAndRelease); |
390 | | - FastThreadLocalThread thread = new FastThreadLocalThread(task); |
391 | | - thread.start(); |
392 | | - task.get(); |
393 | | - } |
394 | | - |
| 215 | + @DisabledForSlowLeakDetection |
395 | 216 | @RepeatedTest(100) |
396 | 217 | void buddyAllocationConsistency(RepetitionInfo info) { |
397 | 218 | SplittableRandom rng = new SplittableRandom(info.getCurrentRepetition()); |
|