Skip to content

Commit

Permalink
fix a bug in identifying thread dump starting point
Browse files Browse the repository at this point in the history
fix compilation warnings
  • Loading branch information
Haim Yadid committed Dec 1, 2022
1 parent 65da701 commit d627ebb
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 73 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/performizeit/jmxsupport/JMXConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}

Expand All @@ -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;
}
Expand Down
48 changes: 22 additions & 26 deletions src/main/java/com/performizeit/mjprof/MJProf.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -82,7 +83,7 @@ public static void main(String[] args) throws IOException {

}

static ArrayList <Pipe> pipes = new ArrayList<Pipe>();
static ArrayList <Pipe> pipes = new ArrayList<>();
static ArrayList <Generator<ThreadInfo>> generators = new ArrayList<Generator<ThreadInfo>>();
private static void constructPlumbing(ArrayList<MJStep> steps) {
int i=0;
Expand Down Expand Up @@ -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<String> keys = new ArrayList<String>(StepsRepository.getRepository().keySet());
Collections.sort(keys,new Comparator<String>() {
@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<String> 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");

Expand Down Expand Up @@ -199,9 +197,7 @@ private static void getSynopsisContent(StringBuilder sb, List<String> 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");
Expand Down Expand Up @@ -236,7 +232,7 @@ static int findNextSeperator(String str) {

static ArrayList<String> splitCommandLine(String arg) {
String argPart = arg;
ArrayList<String> argParts = new ArrayList<String>();
ArrayList<String> argParts = new ArrayList<>();
for (int idx = findNextSeperator(argPart); idx != -1; idx = findNextSeperator(argPart)) {
argParts.add(argPart.substring(0, idx));
argPart = argPart.substring(idx + 1);
Expand All @@ -248,7 +244,7 @@ static ArrayList<String> splitCommandLine(String arg) {
static ArrayList<MJStep> parseCommandLine(String concatArgs) {

ArrayList<String> argParts = splitCommandLine(concatArgs);
ArrayList<String> finalArgsParts = new ArrayList<String>();
ArrayList<String> finalArgsParts = new ArrayList<>();
for (String argPart : argParts) {
String expand = Macros.getInstance().getProperty(argPart);
if (expand != null) {
Expand All @@ -267,7 +263,7 @@ static ArrayList<MJStep> parseCommandLine(String concatArgs) {
if (i<argParts.size()-1) expandedExpression .append(".");
}
expandedExpressionstr = expandedExpression.toString();
ArrayList<MJStep> mjsteps = new ArrayList<MJStep>();
ArrayList<MJStep> mjsteps = new ArrayList<>();
for (String s : argParts) {
if (s.equalsIgnoreCase("help")) {
printSynopsisAndExit();
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/performizeit/mjprof/api/Attr.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package com.performizeit.mjprof.api;

/**
* Created by life on 13/5/14.
*/
public class Attr {
public String attrName;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@

public class ThreadInfoAggregator {
private final List<String> props;
HashSet<String> propsMap = new HashSet<String>();
HashSet<String> propsMap = new HashSet<>();

HashMap<List<Object>, ThreadInfo> aggregator = new HashMap<List<Object>, ThreadInfo>();
HashMap<List<Object>, ThreadInfo> aggregator = new HashMap<>();

public ThreadInfoAggregator(List<String> props) {
this.props = props;
for (String prop : props) propsMap.add(prop);
propsMap.addAll(props);
}

public void accumulateThreadInfo(ThreadInfo threadInfo) {
Expand All @@ -32,7 +32,7 @@ public void accumulateThreadInfo(ThreadInfo threadInfo) {
}

public ArrayList<ThreadInfo> getAggrInfos() {
return new ArrayList<ThreadInfo>(aggregator.values());
return new ArrayList<>(aggregator.values());

}

Expand All @@ -54,17 +54,15 @@ 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);
}

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);
}
Expand All @@ -83,12 +81,13 @@ private void mergeProfiles(ThreadInfo target, ThreadInfo threadInfo) {


public void mergeNonKeyProps(ThreadInfo target, ThreadInfo threadInfo) {
ArrayList<String> keys = new ArrayList<String>(threadInfo.getProps());
ArrayList<String> 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);
Expand All @@ -111,7 +110,7 @@ public void mergeNonKeyProps(ThreadInfo target, ThreadInfo threadInfo) {
}

ArrayList<Object> generateKey(ThreadInfo threadInfo) {
ArrayList<Object> key = new ArrayList<Object>();
ArrayList<Object> key = new ArrayList<>();
for (String prop : props) {
key.add(threadInfo.getVal(prop));
}
Expand Down
30 changes: 7 additions & 23 deletions src/main/java/com/performizeit/mjprof/monads/Macros.java
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -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<String>() {
@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");
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/performizeit/mjprof/parser/HexaLong.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ public class HexaLong implements Comparable<HexaLong> {

public HexaLong(long val) {
value = val;

}

public HexaLong(String s) throws NumberFormatException {
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit d627ebb

Please sign in to comment.