1 /*
   2  * Copyright (c) 2017, 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 Checks the Recent garbage storage system.
  31  * @build Frame HeapMonitor
  32  * @compile HeapMonitorRecentTest.java
  33  * @run main/othervm/native -agentlib:HeapMonitor MyPackage.HeapMonitorRecentTest
  34  */
  35 
  36 public class HeapMonitorRecentTest {
  37 
  38   private native static boolean framesNotInLiveOrRecent(Frame[] frames);
  39   private native static boolean framesExistInRecent(Frame[] frames);
  40 
  41   private static List<Frame> runner(int max) {
  42     List<Frame> frameList = null;
  43     for (int j = 0; j < max; j++) {
  44       frameList = HeapMonitor.allocate();
  45     }
  46     frameList.add(new Frame("runner", "(I)Ljava/util/List;", "HeapMonitorRecentTest.java", 44));
  47     return frameList;
  48   }
  49 
  50   public static void main(String[] args) {
  51     HeapMonitor.enableSampling();
  52     // We are testing for the recent garbage sampler:
  53     // First run for 10 iterations to fill up the garbage sampler.
  54     List<Frame> firstFrameList = runner(10);
  55     firstFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorRecentTest.java", 54));
  56 
  57     // Now because we are in a different stack frame line here, we can just re-use the same runner.
  58     // Run for 10, we really should no longer see the original frames and only see these.
  59     // See the HeapMonitorFrequentTest to understand the Frequent variation.
  60     List<Frame> secondFrameList = runner(10);
  61     secondFrameList.add(new Frame("main", "([Ljava/lang/String;)V", "HeapMonitorRecentTest.java", 60));
  62 
  63     // We should no longer have the initial frames.
  64     boolean status = framesNotInLiveOrRecent(firstFrameList.toArray(new Frame[0]));
  65     if (!status) {
  66       throw new RuntimeException("Initial frames still internally there.");
  67     }
  68 
  69     // We should see those new frames.
  70     status = framesExistInRecent(secondFrameList.toArray(new Frame[0]));
  71     if (!status) {
  72       throw new RuntimeException("Second frame list not found in both live and recent.");
  73     }
  74   }
  75 }