1 /*
   2  * Copyright (c) 2018, Google 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 MyPackage;
  25 
  26 import java.util.List;
  27 
  28 /**
  29  * @test
  30  * @summary Verifies if cached and live are the same right after a GC.
  31  * @build Frame HeapMonitor
  32  * @compile HeapMonitorCachedTest.java
  33  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorCachedTest
  34  */
  35 
  36 public class HeapMonitorCachedTest {
  37 
  38   private static native boolean cachedAndLiveAreSame();
  39   private static native void getLiveTracesToForceGc();
  40   private static native long getCachedHashCode();
  41 
  42   public static void main(String[] args) {
  43     HeapMonitor.enableSampling();
  44     List<Frame> frameList = HeapMonitor.repeatAllocate(10);
  45     frameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorCachedTest.java", 44));
  46     Frame[] frames = frameList.toArray(new Frame[0]);
  47 
  48     // Check that the data is available while heap sampling is enabled.
  49     boolean status = HeapMonitor.framesExistSomewhere(frames);
  50     if (!status) {
  51       throw new RuntimeException("Failed to find the traces before the wipe out.");
  52     }
  53 
  54     // Check cached & live are the same after a GC.
  55     getLiveTracesToForceGc();
  56     status = cachedAndLiveAreSame();
  57     if (!status) {
  58       throw new RuntimeException("Cached frames and live frames are not the same.");
  59     }
  60 
  61     // Allocate some more and then free it all, the cache hash code should remain the same.
  62     long cacheHashCode = getCachedHashCode();
  63     HeapMonitor.repeatAllocate(10);
  64     // Free the memory entirely.
  65     HeapMonitor.freeStorage();
  66     long secondCacheHashCode = getCachedHashCode();
  67 
  68     if (cacheHashCode != secondCacheHashCode) {
  69       throw new RuntimeException("Cached hash code changed.");
  70     }
  71 
  72     // Check cached & live are not the same: cached will still have the old items and live, by
  73     // provoking a GC, will have nothing left.
  74     status = cachedAndLiveAreSame();
  75     if (status) {
  76       throw new RuntimeException("Cached frames and live frames are still the same.");
  77     }
  78 
  79     // Disabling the sampling should wipe everything out.
  80     HeapMonitor.disableSampling();
  81   }
  82 }