From d627ebb30b56f95a2840a9580919d655d302b7e9 Mon Sep 17 00:00:00 2001 From: Haim Yadid Date: Thu, 1 Dec 2022 11:24:25 +0200 Subject: [PATCH] fix a bug in identifying thread dump starting point fix compilation warnings --- .../jmxsupport/JMXConnection.java | 6 +-- .../java/com/performizeit/mjprof/MJProf.java | 48 +++++++++---------- .../com/performizeit/mjprof/api/Attr.java | 3 -- .../mjprof/model/ThreadInfoAggregator.java | 23 +++++---- .../performizeit/mjprof/monads/Macros.java | 30 +++--------- .../performizeit/mjprof/parser/HexaLong.java | 3 +- .../StreamDataSourcePluginBase.java | 5 +- .../mjprof/parser/HexaLongTest.java | 6 ++- 8 files changed, 51 insertions(+), 73 deletions(-) diff --git a/src/main/java/com/performizeit/jmxsupport/JMXConnection.java b/src/main/java/com/performizeit/jmxsupport/JMXConnection.java index 8467eec..f5d87fa 100755 --- a/src/main/java/com/performizeit/jmxsupport/JMXConnection.java +++ b/src/main/java/com/performizeit/jmxsupport/JMXConnection.java @@ -24,8 +24,6 @@ import java.net.URLClassLoader; import java.util.HashMap; import java.util.Map; -import java.util.logging.Level; -import java.util.logging.Logger; import javax.management.AttributeNotFoundException; import javax.management.InstanceNotFoundException; import javax.management.MBeanException; @@ -102,7 +100,7 @@ public MBeanServerConnection getServerConnection() throws IOException { GC = new ObjectName("java.lang:type=GarbageCollector,name=*"); THREADING = new ObjectName("java.lang:type=Threading"); } catch (MalformedObjectNameException | NullPointerException ex) { - Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex); + ex.printStackTrace(); } } @@ -113,7 +111,7 @@ public long getUptime() { l = (Long) getServerConnection().getAttribute(JMXConnection.RUNTIME, "Uptime"); } catch (MBeanException | AttributeNotFoundException | InstanceNotFoundException | ReflectionException | IOException ex) { - Logger.getLogger(JMXConnection.class.getName()).log(Level.SEVERE, null, ex); + ex.printStackTrace(); } return l; } diff --git a/src/main/java/com/performizeit/mjprof/MJProf.java b/src/main/java/com/performizeit/mjprof/MJProf.java index 60c97f8..c3d9b02 100644 --- a/src/main/java/com/performizeit/mjprof/MJProf.java +++ b/src/main/java/com/performizeit/mjprof/MJProf.java @@ -17,31 +17,32 @@ package com.performizeit.mjprof; -import java.io.IOException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - - import com.performizeit.mjprof.api.Attr; +import com.performizeit.mjprof.api.Param; import com.performizeit.mjprof.api.ThreadInfoComparator; import com.performizeit.mjprof.monads.MJStep; -import com.performizeit.mjprof.api.Param; import com.performizeit.mjprof.monads.Macros; import com.performizeit.mjprof.monads.StepInfo; import com.performizeit.mjprof.monads.StepsRepository; import com.performizeit.mjprof.parser.ThreadInfo; import com.performizeit.mjprof.plugin.PluginUtils; -import com.performizeit.mjprof.plugin.types.*; +import com.performizeit.mjprof.plugin.types.DataSource; +import com.performizeit.mjprof.plugin.types.DumpReducer; +import com.performizeit.mjprof.plugin.types.Filter; +import com.performizeit.mjprof.plugin.types.Outputer; +import com.performizeit.mjprof.plugin.types.SingleThreadMapper; +import com.performizeit.mjprof.plugin.types.Terminal; import com.performizeit.plumbing.Generator; import com.performizeit.plumbing.GeneratorHandler; import com.performizeit.plumbing.Pipe; import com.performizeit.plumbing.PipeHandler; +import java.util.ArrayList; +import java.util.List; + public class MJProf { - public static void main(String[] args) throws IOException { + public static void main(String[] args) { // System.setProperty("java.awt.headless","true"); // when using lanterna we suffer when there we do not disable GUI if (args.length < 1) { printSynopsisAndExit(); @@ -82,7 +83,7 @@ public static void main(String[] args) throws IOException { } - static ArrayList pipes = new ArrayList(); + static ArrayList pipes = new ArrayList<>(); static ArrayList > generators = new ArrayList>(); private static void constructPlumbing(ArrayList steps) { int i=0; @@ -138,16 +139,13 @@ public static String getSynopsisString() { StringBuilder sb = new StringBuilder(); sb.append("Synopsis\nA list of the following monads concatenated with . \n"); sb.append("\nExpanded Expression:\n"); - sb.append(expandedExpressionstr+"\n"); - List keys = new ArrayList(StepsRepository.getRepository().keySet()); - Collections.sort(keys,new Comparator() { - @Override - public int compare(String o1, String o2) { - if (o1.startsWith("-")) o1 = o1.substring(1)+"-"; - if (o2.startsWith("-")) o2 = o2.substring(1)+"-"; - return o1.compareTo(o2); + sb.append(expandedExpressionstr).append("\n"); + List keys = new ArrayList<>(StepsRepository.getRepository().keySet()); + keys.sort((o1, o2) -> { + if (o1.startsWith("-")) o1 = o1.substring(1) + "-"; + if (o2.startsWith("-")) o2 = o2.substring(1) + "-"; + return o1.compareTo(o2); - } }); sb.append("\nData sources:\n"); @@ -199,9 +197,7 @@ private static void getSynopsisContent(StringBuilder sb, List keys,Class } lineLength += appendAndCount(sb, "/"); } - for (int j = 0; j < (70 - lineLength); j++) { - sb.append(" "); - } + sb.append(" ".repeat(Math.max(0, (70 - lineLength)))); sb.append("- "); sb.append(stepInfo.getDescription()); sb.append("\n"); @@ -236,7 +232,7 @@ static int findNextSeperator(String str) { static ArrayList splitCommandLine(String arg) { String argPart = arg; - ArrayList argParts = new ArrayList(); + ArrayList argParts = new ArrayList<>(); for (int idx = findNextSeperator(argPart); idx != -1; idx = findNextSeperator(argPart)) { argParts.add(argPart.substring(0, idx)); argPart = argPart.substring(idx + 1); @@ -248,7 +244,7 @@ static ArrayList splitCommandLine(String arg) { static ArrayList parseCommandLine(String concatArgs) { ArrayList argParts = splitCommandLine(concatArgs); - ArrayList finalArgsParts = new ArrayList(); + ArrayList finalArgsParts = new ArrayList<>(); for (String argPart : argParts) { String expand = Macros.getInstance().getProperty(argPart); if (expand != null) { @@ -267,7 +263,7 @@ static ArrayList parseCommandLine(String concatArgs) { if (i mjsteps = new ArrayList(); + ArrayList mjsteps = new ArrayList<>(); for (String s : argParts) { if (s.equalsIgnoreCase("help")) { printSynopsisAndExit(); diff --git a/src/main/java/com/performizeit/mjprof/api/Attr.java b/src/main/java/com/performizeit/mjprof/api/Attr.java index 618790a..1fd5ac3 100644 --- a/src/main/java/com/performizeit/mjprof/api/Attr.java +++ b/src/main/java/com/performizeit/mjprof/api/Attr.java @@ -1,8 +1,5 @@ package com.performizeit.mjprof.api; -/** - * Created by life on 13/5/14. - */ public class Attr { public String attrName; diff --git a/src/main/java/com/performizeit/mjprof/model/ThreadInfoAggregator.java b/src/main/java/com/performizeit/mjprof/model/ThreadInfoAggregator.java index fb4e7e2..02adef1 100644 --- a/src/main/java/com/performizeit/mjprof/model/ThreadInfoAggregator.java +++ b/src/main/java/com/performizeit/mjprof/model/ThreadInfoAggregator.java @@ -9,13 +9,13 @@ public class ThreadInfoAggregator { private final List props; - HashSet propsMap = new HashSet(); + HashSet propsMap = new HashSet<>(); - HashMap, ThreadInfo> aggregator = new HashMap, ThreadInfo>(); + HashMap, ThreadInfo> aggregator = new HashMap<>(); public ThreadInfoAggregator(List props) { this.props = props; - for (String prop : props) propsMap.add(prop); + propsMap.addAll(props); } public void accumulateThreadInfo(ThreadInfo threadInfo) { @@ -32,7 +32,7 @@ public void accumulateThreadInfo(ThreadInfo threadInfo) { } public ArrayList getAggrInfos() { - return new ArrayList(aggregator.values()); + return new ArrayList<>(aggregator.values()); } @@ -54,8 +54,6 @@ private void mergeCPU(ThreadInfo target, ThreadInfo threadInfo) { Long cpuTarget = (Long) target.getVal(CPUNS); Long cpu = (Long) threadInfo.getVal(CPUNS); if (cpu != null && cpuTarget != null) { // no cpu enrichment - if (cpu == null) cpu = 0l; - if (cpuTarget == null) cpuTarget = 0l; cpuTarget += cpu; target.setVal(CPUNS, cpuTarget); } @@ -63,8 +61,8 @@ private void mergeCPU(ThreadInfo target, ThreadInfo threadInfo) { Long wallTarget = (Long) target.getVal(WALL); Long wall = (Long) threadInfo.getVal(WALL); if (wall != null || wallTarget != null) { - if (wall == null) wall = 0l; - if (wallTarget == null) wallTarget = 0l; + if (wall == null) wall = 0L; + if (wallTarget == null) wallTarget = 0L; if (wallTarget < wall) wallTarget = wall; target.setVal(WALL, wallTarget); } @@ -83,12 +81,13 @@ private void mergeProfiles(ThreadInfo target, ThreadInfo threadInfo) { public void mergeNonKeyProps(ThreadInfo target, ThreadInfo threadInfo) { - ArrayList keys = new ArrayList(threadInfo.getProps()); + ArrayList keys = new ArrayList<>(threadInfo.getProps()); for (String prop1 : keys) { + if (propsMap.contains(prop1)) continue; // key properties are not merged if (prop1.equals(STACK) || prop1.equals(COUNT) || prop1.equals(CPUNS) || - prop1.equals(WALL) || prop1.equals(CPU_PREC) || propsMap.contains(prop1)) + prop1.equals(WALL) || prop1.equals(CPU_PREC)) continue; // already merged to specially - if (propsMap.contains(prop1)) continue; // key properties are not merged + Object valTarget = target.getVal(prop1); Object val = threadInfo.getVal(prop1); @@ -111,7 +110,7 @@ public void mergeNonKeyProps(ThreadInfo target, ThreadInfo threadInfo) { } ArrayList generateKey(ThreadInfo threadInfo) { - ArrayList key = new ArrayList(); + ArrayList key = new ArrayList<>(); for (String prop : props) { key.add(threadInfo.getVal(prop)); } diff --git a/src/main/java/com/performizeit/mjprof/monads/Macros.java b/src/main/java/com/performizeit/mjprof/monads/Macros.java index 746d0cd..6b582c6 100644 --- a/src/main/java/com/performizeit/mjprof/monads/Macros.java +++ b/src/main/java/com/performizeit/mjprof/monads/Macros.java @@ -1,17 +1,14 @@ package com.performizeit.mjprof.monads; import java.io.FileInputStream; -import java.io.FileNotFoundException; import java.io.IOException; import java.util.Arrays; -import java.util.Collections; -import java.util.Comparator; import java.util.Properties; public class Macros { private Properties defaultProps = null; - private static Macros instance = new Macros(); + private static final Macros instance = new Macros(); private String reason = ""; private String macroFile; @@ -48,38 +45,25 @@ public String getProperty(String name) { return (String) defaultProps.get(name); } - public static void main(String[] args) { - Macros ins = getInstance(); - for (String name : ins.getProps().keySet().toArray(new String[ins.getProps().size()])) { - System.out.println(name + ":" + ins.getProperty(name)); - - } - } - public void getSynopsisContent(StringBuilder sb) { if (defaultProps == null) { sb.append(reason).append("\n"); return; } if (defaultProps.size() == 0) { - sb.append("No macros defined in file '" + macroFile + "' \n"); + sb.append("No macros defined in file '").append(macroFile).append("' \n"); } String[] names = getProps().keySet().toArray(new String[getProps().size()]); - Arrays.sort(names, new Comparator() { - @Override - public int compare(String o1, String o2) { - if (o1.startsWith("-")) o1 = o1.substring(1) + "-"; - if (o2.startsWith("-")) o2 = o2.substring(1) + "-"; - return o1.compareTo(o2); + Arrays.sort(names, (o1, o2) -> { + if (o1.startsWith("-")) o1 = o1.substring(1) + "-"; + if (o2.startsWith("-")) o2 = o2.substring(1) + "-"; + return o1.compareTo(o2); - } }); for (String name : names) { String ln = " " + name; sb.append(ln); - for (int j = 0; j < (70 - ln.length()); j++) { - sb.append(" "); - } + sb.append(" ".repeat(Math.max(0, (70 - ln.length())))); sb.append("- "); sb.append(getProperty(name)); sb.append("\n"); diff --git a/src/main/java/com/performizeit/mjprof/parser/HexaLong.java b/src/main/java/com/performizeit/mjprof/parser/HexaLong.java index ed314dd..4cff27d 100644 --- a/src/main/java/com/performizeit/mjprof/parser/HexaLong.java +++ b/src/main/java/com/performizeit/mjprof/parser/HexaLong.java @@ -23,7 +23,6 @@ public class HexaLong implements Comparable { public HexaLong(long val) { value = val; - } public HexaLong(String s) throws NumberFormatException { @@ -41,7 +40,7 @@ public String toString() { @Override public int compareTo(HexaLong o) { - return new Long(value).compareTo(o.value); + return Long.compare(value, o.value); } public long getValue() { diff --git a/src/main/java/com/performizeit/mjprof/plugins/dataSource/StreamDataSourcePluginBase.java b/src/main/java/com/performizeit/mjprof/plugins/dataSource/StreamDataSourcePluginBase.java index d634191..9705ed9 100644 --- a/src/main/java/com/performizeit/mjprof/plugins/dataSource/StreamDataSourcePluginBase.java +++ b/src/main/java/com/performizeit/mjprof/plugins/dataSource/StreamDataSourcePluginBase.java @@ -2,6 +2,7 @@ import java.io.BufferedReader; import java.io.IOException; +import java.util.regex.Pattern; import com.performizeit.mjprof.plugin.types.DataSource; import com.performizeit.mjprof.parser.ThreadDump; @@ -23,10 +24,10 @@ protected String getStackStringFromReader() { StringBuilder linesOfStack = new StringBuilder(); String line; try { - + Pattern startWithADate = Pattern.compile("^(\\d\\d\\d\\d-\\d\\d-\\d\\d .*)"); while ((line = reader.readLine()) != null) { // System.out.println(line); - if (line.length() > 0 && Character.isDigit(line.charAt(0))) { //starting a new stack dump + if (line.length() > 0 && startWithADate.matcher(line).matches()) { //starting a new stack dump if (linesOfStack.length() > 0) { return linesOfStack.toString(); } diff --git a/src/test/java/com/performizeit/mjprof/parser/HexaLongTest.java b/src/test/java/com/performizeit/mjprof/parser/HexaLongTest.java index 56ca620..cfcbf24 100644 --- a/src/test/java/com/performizeit/mjprof/parser/HexaLongTest.java +++ b/src/test/java/com/performizeit/mjprof/parser/HexaLongTest.java @@ -21,10 +21,14 @@ public class HexaLongTest { @Test - public void testConstructor() { + public void testStringConstructor() { assertEquals(10,(new HexaLong("a")).getValue()); assertEquals(20,(new HexaLong("14")).getValue()); assertEquals(32,(new HexaLong("20")).getValue()); assertEquals(32,(new HexaLong("0x20")).getValue()); } + @Test + public void testToString() { + assertEquals("20",(new HexaLong(32)).toString()); + } }