1 /*
   2  * Copyright (c) 2015, 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.
   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 /*
  25  * Results of running the JstatGcTool ("jstat -gccause <pid>")
  26  *
  27  * Output example:
  28  * S0     S1     E      O      M     CCS    YGC     YGCT    FGC    FGCT    CGC    CGCT     GCT    LGCC                 GCC
  29  * 0.00   6.25  46.19   0.34  57.98  54.63  15305 1270.551     0    0.000     0    0.000 1270.551 Allocation Failure   No GC
  30 
  31  * Output description:
  32  * S0         Survivor space 0 utilization as a percentage of the space's current capacity.
  33  * S1         Survivor space 1 utilization as a percentage of the space's current capacity.
  34  * E           Eden space utilization as a percentage of the space's current capacity.
  35  * O           Old space utilization as a percentage of the space's current capacity.
  36  * M          Metaspace utilization as a percentage of the space's current capacity.
  37  * CCS     Compressed Class Space
  38  * YGC     Number of young generation GC events.
  39  * YGCT    Young generation garbage collection time.
  40  * FGC      Number of full GC events.
  41  * FGCT    Full garbage collection time.
  42  * CGC      Number of STW of Concurrent GC.
  43  * CGCT    STW of Concurrent GC time.
  44  * GCT      Total garbage collection time.
  45  * LGCC    Cause of last Garbage Collection.
  46  * GCC      Cause of current Garbage Collection.
  47  */
  48 package utils;
  49 
  50 import common.ToolResults;
  51 
  52 public class JstatGcCauseResults extends JstatResults {
  53 
  54     public JstatGcCauseResults(ToolResults rawResults) {
  55         super(rawResults);
  56     }
  57 
  58     /**
  59      * Checks the overall consistency of the results reported by the tool
  60      */
  61     public void assertConsistency() {
  62 
  63         assertThat(getExitCode() == 0, "Unexpected exit code: " + getExitCode());
  64 
  65         int YGC = getIntValue("YGC");
  66         float YGCT = getFloatValue("YGCT");
  67         assertThat(YGCT >= 0, "Incorrect time value for YGCT");
  68         if (YGC > 0) {
  69             assertThat(YGCT > 0, "Number of young generation GC Events is " + YGC + ", but YGCT is 0");
  70         }
  71 
  72         float GCT = getFloatValue("GCT");
  73         assertThat(GCT >= 0, "Incorrect time value for GCT");
  74         assertThat(GCT >= YGCT, "GCT < YGCT (total garbage collection time < young generation garbage collection time)");
  75 
  76         int FGC = getIntValue("FGC");
  77         float FGCT = getFloatValue("FGCT");
  78         if (FGC > 0) {
  79             assertThat(FGCT > 0, "Number of full GC events is " + FGC + ", but FGCT is 0");
  80         }
  81 
  82         int CGC = getIntValue("CGC");
  83         float CGCT = getFloatValue("CGCT");
  84         if (CGC > 0) {
  85             assertThat(CGCT > 0, "Number of full GC events is " + CGC + ", but CGCT is 0");
  86         }
  87 
  88         assertThat(GCT >= (FGCT + CGCT), "GCT < YGCT (total garbage collection time < full generation garbage collection time)");
  89 
  90         assertThat(checkFloatIsSum(GCT, YGCT, FGCT, CGCT), "GCT != (YGCT + FGCT + CGCT) " + "(GCT = " + GCT + ", YGCT = " + YGCT
  91                 + ", FGCT = " + FGCT + ", CGCT = " + CGCT + ", (YCGT + FGCT + CGCT) = " + (YGCT + FGCT + CGCT) + ")");
  92     }
  93 
  94     private static final float FLOAT_COMPARISON_TOLERANCE = 0.0011f;
  95 
  96     private static boolean checkFloatIsSum(float sum, float... floats) {
  97         for (float f : floats) {
  98             sum -= f;
  99         }
 100 
 101         return Math.abs(sum) <= FLOAT_COMPARISON_TOLERANCE;
 102     }
 103 
 104     private void assertThat(boolean b, String message) {
 105         if (!b) {
 106             throw new RuntimeException(message);
 107         }
 108     }
 109 
 110 }