1 /*
   2  * Copyright (c) 2013, 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.event.runtime;
  27 
  28 import static jdk.test.lib.Asserts.assertGreaterThan;
  29 import static jdk.test.lib.Asserts.assertTrue;
  30 
  31 import java.lang.management.ManagementFactory;
  32 import java.time.Duration;
  33 import java.util.Collections;
  34 import java.util.List;
  35 import java.util.Random;
  36 import java.util.concurrent.CountDownLatch;
  37 
  38 import com.sun.management.ThreadMXBean;
  39 
  40 import jdk.jfr.Recording;
  41 import jdk.jfr.consumer.RecordedEvent;
  42 import jdk.jfr.consumer.RecordedThread;
  43 import jdk.test.lib.jfr.EventNames;
  44 import jdk.test.lib.jfr.Events;
  45 
  46 /**
  47  * @test
  48  * @key jfr
  49  * @requires vm.hasJFR
  50  * @library /test/lib
  51  * @modules jdk.jfr
  52  *          jdk.management
  53  *
  54  * @run main/othervm -XX:-UseTLAB jdk.jfr.event.runtime.TestThreadAllocationEvent
  55  */
  56 
  57 /**
  58  * The test will create a few threads that will allocate memory for a short time.
  59  * During this time a number of thread_allocation events will be generated.
  60  * The test will verify:
  61  * 1. That number of allocated bytes is not decreasing for a thread.
  62  *  - This assumption is only true when not using TLABs. For this reason the
  63  *    test is run with -XX:-UseTLAB. When using TLABs, the code calculating the
  64  *    allocated bytes is using the current TLAB to do as good of an approximation
  65  *    as possible, but this introduces a race which might double count the current
  66  *    TLAB when it is full and in the middle of being switched out.
  67  * 2. That sum of allocated bytes approximately matches value in ThreadMXBean.
  68  */
  69 public class TestThreadAllocationEvent {
  70     private static final String EVENT_NAME = EventNames.ThreadAllocationStatistics;
  71     private static final String testThreadName = "testThread-";
  72     private static final long eventPeriodMillis = 50;
  73 
  74     // The value in both the JFR event and in the ThreadMXBean is documented as
  75     // an "approximation" of number of bytes allocated.
  76     // To not give any false errors, we allow an error margin of 5 mb.
  77     // The test will typically allocate over 600 mb, so 5 mb is an error of less than 1%.
  78     private static final long allowedTotalAllocatedDiff = 5000000;
  79 
  80     public static void main(String[] args) throws Throwable {
  81         Recording recording = new Recording();
  82         recording.enable(EVENT_NAME).withPeriod(Duration.ofMillis(eventPeriodMillis));
  83         recording.start();
  84 
  85         AllocatorThread[] threads = new AllocatorThread[4];
  86         CountDownLatch allocationsDoneLatch = new CountDownLatch(threads.length);
  87         for (int i = 0; i < threads.length; i++) {
  88             threads[i] = new AllocatorThread(allocationsDoneLatch, 1000 * (i + 1));
  89             threads[i].setName(testThreadName + i);
  90             threads[i].setDaemon(true);
  91             threads[i].start();
  92         }
  93 
  94         // Take regular measurements while the threads are allocating memory.
  95         // Stop measurement when all threads are ready.
  96         try {
  97             allocationsDoneLatch.await();
  98         } catch (InterruptedException e) {
  99             e.printStackTrace();
 100         }
 101         // Verify that number of allocated bytes is not decreasing.
 102         recording.stop();
 103         verifyAllocationsNotDecreasing(Events.fromRecording(recording), threads);
 104 
 105         // Now allocations are done and threads are waiting to die.
 106         // Make a new instant recording to get total number of allocated bytes.
 107         // The reason for this extra recording is to make sure we get a JFR event
 108         // after all allocations are done so we can compare the JFR value with
 109         // the value reported by ThreadMXBean.
 110         recording = new Recording();
 111         recording.enable(EVENT_NAME);
 112         recording.start();
 113         recording.stop();
 114         verifyTotalAllocated(Events.fromRecording(recording), threads);
 115     }
 116 
 117     /**
 118      * Verify that the allocated value never decreases.
 119      * We only compare our own allocator threads. The reason for that is that other threads
 120      * may start/stop at any time, and we do not know if other thread names are unique.
 121      */
 122      private static void verifyAllocationsNotDecreasing(List<RecordedEvent> events, AllocatorThread[] threads) {
 123          Collections.sort(events, (u,v) -> u.getEndTime().compareTo(v.getEndTime()));
 124          long[] prevAllocated = new long[threads.length];
 125          for (RecordedEvent event : events) {
 126              RecordedThread rt = Events.assertField(event, "thread").notNull().getValue(); // Check that we have a thread.
 127              String name = rt.getJavaName();
 128              for (int i = 0; i < threads.length; i++) {
 129                  if (name.equals(threads[i].getName())) {
 130                      long curr = Events.assertField(event, "allocated").atLeast(prevAllocated[i]).getValue();
 131                      prevAllocated[i] = curr;
 132                  }
 133              }
 134          }
 135 
 136          for (int i = 0; i < threads.length; i++) {
 137              assertGreaterThan(prevAllocated[i], 0L, "No allocations for thread " + threads[i].getName());
 138          }
 139      }
 140 
 141      /**
 142       * Verify that total allocated bytes in JFR event approximately matches the value in ThreadMXBean.
 143       */
 144     private static void verifyTotalAllocated(List<RecordedEvent> events, AllocatorThread[] threads) {
 145         boolean[] isEventFound = new boolean[threads.length];
 146         for (RecordedEvent event : events) {
 147             RecordedThread rt = Events.assertField(event, "thread").notNull().getValue();
 148             String name = rt.getJavaName();
 149             for (int i = 0; i < threads.length; ++i) {
 150                 if (name.equals(threads[i].getName())) {
 151                     System.out.println("Event:" + event);
 152                     long maxAllowed = threads[i].totalAllocated + allowedTotalAllocatedDiff;
 153                     long minAllowed = Math.max(0, threads[i].totalAllocated - allowedTotalAllocatedDiff);
 154                     Events.assertField(event, "allocated").atLeast(minAllowed).atMost(maxAllowed);
 155                     isEventFound[i] = true;
 156                 }
 157             }
 158         }
 159         for (int i = 0; i < threads.length; ++i) {
 160             assertTrue(isEventFound[i], "No event for thread id " + i);
 161         }
 162     }
 163 
 164     /**
 165      * Thread that does a number of allocations and records total number of
 166      * bytes allocated as reported by ThreadMXBean.
 167      */
 168     public static class AllocatorThread extends Thread {
 169         private volatile long totalAllocated = -1;
 170         private final int averageAllocationSize;
 171         public byte[] buffer;
 172         private final CountDownLatch allocationsDoneLatch;
 173 
 174         public AllocatorThread(CountDownLatch allocationsDoneLatch, int averageAllocationSize) {
 175             this.allocationsDoneLatch = allocationsDoneLatch;
 176             this.averageAllocationSize = averageAllocationSize;
 177         }
 178 
 179         @Override
 180         public void run() {
 181             Random rand = new Random();
 182             int allocationSizeBase = averageAllocationSize / 2;
 183             int allocationSizeRandom = averageAllocationSize;
 184             for (int batches=0; batches<100; batches++) {
 185                 for (int i=0; i<1500; i++) {
 186                     buffer = new byte[rand.nextInt(allocationSizeRandom) + allocationSizeBase];
 187                 }
 188                 try {
 189                     // No need to allocate too much data between JFR events, so do a short sleep.
 190                     Thread.sleep(eventPeriodMillis / 5);
 191                 } catch (InterruptedException e) {
 192                     e.printStackTrace();
 193                 }
 194             }
 195             totalAllocated = getThreadAllocatedBytes();
 196             allocationsDoneLatch.countDown();
 197 
 198             // Need to keep thread alive so we can get the final JFR event.
 199             // This is a daemon thread, so it will finish when the main thread finishes.
 200             while (true) {
 201                 Thread.yield();
 202             }
 203         }
 204 
 205         private long getThreadAllocatedBytes() {
 206             ThreadMXBean bean = (ThreadMXBean) ManagementFactory.getThreadMXBean();
 207             return bean.getThreadAllocatedBytes(Thread.currentThread().getId());
 208         }
 209     }
 210 
 211 }