1313package org .quickperf .jvm .jmcrule ;
1414
1515import org .openjdk .jmc .common .item .IItemCollection ;
16+ import org .openjdk .jmc .common .unit .IQuantity ;
1617import org .openjdk .jmc .common .util .IPreferenceValueProvider ;
17- import org .openjdk .jmc .flightrecorder .rules .IRule ;
18- import org .openjdk .jmc .flightrecorder .rules .Result ;
19- import org .openjdk .jmc .flightrecorder .rules .RuleRegistry ;
20- import org .openjdk .jmc .flightrecorder .rules .Severity ;
18+ import org .openjdk .jmc .flightrecorder .rules .*;
19+ import org .openjdk .jmc .flightrecorder .rules .jdk .cpu .HighJvmCpuRule ;
20+ import org .openjdk .jmc .flightrecorder .rules .jdk .memory .GarbageCollectionInfoRule ;
21+ import org .openjdk .jmc .flightrecorder .rules .jdk .memory .GcStallRule ;
22+ import org .openjdk .jmc .flightrecorder .rules .jdk .memory .HeapInspectionRule ;
23+ import org .openjdk .jmc .flightrecorder .rules .jdk .memory .SystemGcRule ;
2124import org .quickperf .ExtractablePerformanceMeasure ;
2225import org .quickperf .jvm .jfr .JfrRecording ;
2326import org .quickperf .unit .Count ;
2427
2528import java .io .PrintWriter ;
2629import java .io .StringWriter ;
27- import java .util .ArrayList ;
28- import java .util .List ;
30+ import java .util .*;
2931import java .util .concurrent .ExecutionException ;
3032import java .util .concurrent .RunnableFuture ;
33+ import java .util .stream .Collectors ;
3134
3235public class JmcRuleCountMeasureExtractor implements ExtractablePerformanceMeasure <JfrRecording , JmcRulesMeasure > {
3336
3437 public static final JmcRuleCountMeasureExtractor INSTANCE = new JmcRuleCountMeasureExtractor ();
35-
36- private JmcRuleCountMeasureExtractor () {}
38+ //We need to ignore some rules which appear to throw NullPointerException in org.openjdk.jmc:flightrecorder.rules.jdk:8.x.y
39+ private static final List <Class <? extends IRule >> IGNORED_RULES = Arrays .asList (
40+ HighJvmCpuRule .class ,
41+ GcStallRule .class ,
42+ GarbageCollectionInfoRule .class ,
43+ HeapInspectionRule .class ,
44+ SystemGcRule .class
45+ );
46+
47+ private JmcRuleCountMeasureExtractor () {
48+ }
3749
3850 @ Override
3951 public JmcRulesMeasure extractPerfMeasureFrom (JfrRecording jfrRecording ) {
4052
4153 IItemCollection jfrEvents = jfrRecording .getJfrEvents ();
42- List <Result > ruleEvaluations = evaluateJmcRules (jfrEvents );
54+ List <IResult > ruleEvaluations = evaluateJmcRules (jfrEvents );
4355
4456 List <Count > jmcRules = buildJmcRuleCountsFrom (ruleEvaluations );
4557
4658 return new JmcRulesMeasure (jmcRules );
4759
4860 }
4961
50- private List <Result > evaluateJmcRules (IItemCollection jfrEvents ) {
51- List <Result > ruleEvaluations = new ArrayList <>();
52- for (IRule rule : RuleRegistry .getRules ()) {
53- RunnableFuture <Result > future = rule .evaluate (jfrEvents , IPreferenceValueProvider .DEFAULT_VALUES );
62+ private List <IResult > evaluateJmcRules (IItemCollection jfrEvents ) {
63+ List <IResult > ruleEvaluations = new ArrayList <>();
64+ Collection <IRule > rules = RuleRegistry .getRules ().stream ()
65+ .filter (r -> !IGNORED_RULES .contains (r .getClass ()))
66+ .collect (Collectors .toList ());
67+ for (IRule rule : rules ) {
68+ RunnableFuture <IResult > future = rule .createEvaluation (jfrEvents ,
69+ IPreferenceValueProvider .DEFAULT_VALUES , new ResultProvider ());
5470 future .run ();
55- Result result ;
71+ IResult result ;
5672 try {
5773 result = future .get ();
5874 } catch (InterruptedException | ExecutionException e ) {
@@ -63,11 +79,11 @@ private List<Result> evaluateJmcRules(IItemCollection jfrEvents) {
6379 return ruleEvaluations ;
6480 }
6581
66- private List <Count > buildJmcRuleCountsFrom (List <Result > ruleEvaluations ) {
82+ private List <Count > buildJmcRuleCountsFrom (List <IResult > ruleEvaluations ) {
6783 List <Count > jmcRules = new ArrayList <>();
68- for (Result ruleEvaluation : ruleEvaluations ) {
84+ for (IResult ruleEvaluation : ruleEvaluations ) {
6985 Count ruleScore = buildJmcRuleCountFrom (ruleEvaluation );
70- if (!ruleToExclude (ruleScore )) {
86+ if (!ruleToExclude (ruleScore )) {
7187 jmcRules .add (ruleScore );
7288 }
7389 }
@@ -76,26 +92,30 @@ private List<Count> buildJmcRuleCountsFrom(List<Result> ruleEvaluations) {
7692
7793 private boolean ruleToExclude (Count ruleScore ) {
7894 String ruleDescription = ruleScore .getComment ();
79- return ruleDescription .contains ("Rule: TLAB Allocation Ratio" )
80- || ruleDescription .contains ("Rule: Competing Processes" )
81- || ruleDescription .contains ("Rule: Command Line Options Check" )
82- || ruleDescription .contains ("Rule: Metaspace Live Set Trend" );
95+ return ruleDescription .contains ("Rule: TLAB Allocation Ratio" )
96+ || ruleDescription .contains ("Rule: Competing Processes" )
97+ || ruleDescription .contains ("Rule: Command Line Options Check" )
98+ || ruleDescription .contains ("Rule: Metaspace Live Set Trend" );
8399 }
84100
85- private Count buildJmcRuleCountFrom (Result result ) {
101+ private Count buildJmcRuleCountFrom (IResult result ) {
86102 StringWriter stringWriter = new StringWriter ();
87103
88104 PrintWriter printWriter = new PrintWriter (stringWriter );
89105 printWriter .println ("Rule: " + result .getRule ().getName ());
90- printWriter .println ("Severity: " + Severity .get (result .getScore ()));
91- long score = (long ) result .getScore ();
106+ printWriter .println ("Severity: " + result .getSeverity ());
107+ long score = Optional .ofNullable (result .getResult (TypedResult .SCORE ))
108+ .map (IQuantity ::longValue )
109+ .orElse (-1L );
92110 printWriter .println ("Score: " + score );
93- String longDescriptionAsHtml = result .getLongDescription ();
111+ final String longDescriptionAsHtml = Optional .ofNullable (result .getSummary ()).orElse ("" )
112+ + Optional .ofNullable (result .getExplanation ()).orElse ("" )
113+ + Optional .ofNullable (result .getSolution ()).orElse ("" );
94114 String textDesc = HtmlToPlainTextTransformer .INSTANCE .convertHtmlToPlainText (longDescriptionAsHtml );
95115 printWriter .println ("Message: " + textDesc );
96116
97117 String description = stringWriter .toString ();
98118 return new Count (score , description );
99119 }
100120
101- }
121+ }
0 commit comments