transform commands: heapdump, perfcounter

pull/1306/head
gongdewei 5 years ago
parent 5b7603eebe
commit 25a8ba9f4f

@ -0,0 +1,42 @@
package com.taobao.arthas.core.command.model;
/**
* Model of `heapdump` command
* @author gongdewei 2020/4/24
*/
public class HeapDumpModel extends ResultModel {
private String dumpFile;
private boolean live;
public HeapDumpModel() {
}
public HeapDumpModel(String dumpFile, boolean live) {
this.dumpFile = dumpFile;
this.live = live;
}
public String getDumpFile() {
return dumpFile;
}
public void setDumpFile(String dumpFile) {
this.dumpFile = dumpFile;
}
public boolean isLive() {
return live;
}
public void setLive(boolean live) {
this.live = live;
}
@Override
public String getType() {
return "heapdump";
}
}

@ -0,0 +1,42 @@
package com.taobao.arthas.core.command.model;
import java.util.List;
/**
* Model of 'perfcounter'
*
* @author gongdewei 2020/4/27
*/
public class PerfCounterModel extends ResultModel {
private List<PerfCounterVO> perfCounters;
private boolean details;
public PerfCounterModel() {
}
public PerfCounterModel(List<PerfCounterVO> perfCounters, boolean details) {
this.perfCounters = perfCounters;
this.details = details;
}
@Override
public String getType() {
return "perfcounter";
}
public List<PerfCounterVO> getPerfCounters() {
return perfCounters;
}
public void setPerfCounters(List<PerfCounterVO> perfCounters) {
this.perfCounters = perfCounters;
}
public boolean isDetails() {
return details;
}
public void setDetails(boolean details) {
this.details = details;
}
}

@ -0,0 +1,62 @@
package com.taobao.arthas.core.command.model;
/**
* VO for PerfCounterCommand
*
* @author gongdewei 2020/4/27
*/
public class PerfCounterVO {
private String name;
private String units;
private String variability;
private Object value;
public PerfCounterVO() {
}
public PerfCounterVO(String name, Object value) {
this.name = name;
this.value = value;
}
public PerfCounterVO(String name, String units, String variability, Object value) {
this.name = name;
this.units = units;
this.variability = variability;
this.value = value;
}
public void setName(String name) {
this.name = name;
}
public void setUnits(String units) {
this.units = units;
}
public void setVariability(String variability) {
this.variability = variability;
}
public void setValue(Object value) {
this.value = value;
}
public String getName() {
return name;
}
public String getUnits() {
return units;
}
public String getVariability() {
return variability;
}
public Object getValue() {
return value;
}
}

@ -10,6 +10,8 @@ import com.alibaba.arthas.deps.org.slf4j.Logger;
import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.sun.management.HotSpotDiagnosticMXBean;
import com.taobao.arthas.core.command.Constants;
import com.taobao.arthas.core.command.model.HeapDumpModel;
import com.taobao.arthas.core.command.model.MessageModel;
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.middleware.cli.annotations.Argument;
@ -48,7 +50,6 @@ public class HeapDumpCommand extends AnnotatedCommand {
@Override
public void process(CommandProcess process) {
int status = 0;
try {
String dumpFile = file;
if (dumpFile == null || dumpFile.isEmpty()) {
@ -58,18 +59,17 @@ public class HeapDumpCommand extends AnnotatedCommand {
file.delete();
}
process.write("Dumping heap to " + dumpFile + "...\n");
process.appendResult(new MessageModel("Dumping heap to " + dumpFile + " ..."));
run(process, dumpFile, live);
process.write("Heap dump file created\n");
process.appendResult(new MessageModel("Heap dump file created"));
process.appendResult(new HeapDumpModel(dumpFile, live));
process.end();
} catch (Throwable t) {
logger.error("heap dump error", t);
process.write("Heap dump error: " + t.getMessage() + '\n');
status = 1;
} finally {
process.end(status);
String errorMsg = "heap dump error: " + t.getMessage();
logger.error(errorMsg, t);
process.end(-1, errorMsg);
}
}

@ -1,9 +1,9 @@
package com.taobao.arthas.core.command.monitor200;
import static com.taobao.text.ui.Element.label;
import java.lang.reflect.Method;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -12,15 +12,14 @@ import com.alibaba.arthas.deps.org.slf4j.LoggerFactory;
import com.taobao.arthas.common.JavaVersionUtils;
import com.taobao.arthas.common.PidUtils;
import com.taobao.arthas.core.command.Constants;
import com.taobao.arthas.core.command.model.PerfCounterModel;
import com.taobao.arthas.core.command.model.PerfCounterVO;
import com.taobao.arthas.core.shell.command.AnnotatedCommand;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.middleware.cli.annotations.Description;
import com.taobao.middleware.cli.annotations.Name;
import com.taobao.middleware.cli.annotations.Option;
import com.taobao.middleware.cli.annotations.Summary;
import com.taobao.text.Decoration;
import com.taobao.text.ui.TableElement;
import com.taobao.text.util.RenderUtil;
import sun.management.counter.Counter;
import sun.management.counter.perf.PerfInstrumentation;
@ -51,36 +50,26 @@ public class PerfCounterCommand extends AnnotatedCommand {
@Override
public void process(CommandProcess process) {
try {
TableElement table = new TableElement().leftCellPadding(1).rightCellPadding(1);
if (this.details) {
table = new TableElement(3, 1, 1, 10).leftCellPadding(1).rightCellPadding(1);
table.row(true, label("Name").style(Decoration.bold.bold()),
label("Variability").style(Decoration.bold.bold()),
label("Units").style(Decoration.bold.bold()), label("Value").style(Decoration.bold.bold()));
}
List<Counter> perfCounters = getPerfCounters();
if (perfCounters.isEmpty()) {
process.end(1,
"please check arthas log. if java version >=9 , try to add jvm options when start your process: "
+ "--add-opens java.base/jdk.internal.perf=ALL-UNNAMED "
+ "--add-exports java.base/jdk.internal.perf=ALL-UNNAMED");
return;
}
List<Counter> perfCounters = getPerfCounters();
if (perfCounters.isEmpty()) {
process.write(
"please check arthas log. if java version >=9 , try to add jvm options when start your process: "
+ "--add-opens java.base/jdk.internal.perf=ALL-UNNAMED "
+ "--add-exports java.base/jdk.internal.perf=ALL-UNNAMED\n");
} else {
for (Counter counter : perfCounters) {
if (details) {
table.row(counter.getName(), String.valueOf(counter.getVariability()),
String.valueOf(counter.getUnits()), String.valueOf(counter.getValue()));
} else {
table.row(counter.getName(), String.valueOf(counter.getValue()));
}
}
List<PerfCounterVO> perfCounterVOs = new ArrayList<PerfCounterVO>();
for (Counter counter : perfCounters) {
PerfCounterVO perfCounterVO = new PerfCounterVO(counter.getName(), counter.getValue());
if (details) {
perfCounterVO.setUnits(counter.getUnits().toString());
perfCounterVO.setVariability(counter.getVariability().toString());
}
process.write(RenderUtil.render(table, process.width()));
} finally {
process.end();
perfCounterVOs.add(perfCounterVO);
}
process.appendResult(new PerfCounterModel(perfCounterVOs, details));
process.end();
}
private static List<Counter> getPerfCounters() {

@ -0,0 +1,46 @@
package com.taobao.arthas.core.command.view;
import com.taobao.arthas.core.command.model.PerfCounterModel;
import com.taobao.arthas.core.command.model.PerfCounterVO;
import com.taobao.arthas.core.shell.command.CommandProcess;
import com.taobao.text.Decoration;
import com.taobao.text.ui.TableElement;
import com.taobao.text.util.RenderUtil;
import java.util.List;
import static com.taobao.text.ui.Element.label;
/**
* View of 'perfcounter' command
*
* @author gongdewei 2020/4/27
*/
public class PerfCounterView extends ResultView<PerfCounterModel> {
@Override
public void draw(CommandProcess process, PerfCounterModel result) {
List<PerfCounterVO> perfCounters = result.getPerfCounters();
boolean details = result.isDetails();
TableElement table;
if (details) {
table = new TableElement(3, 1, 1, 10).leftCellPadding(1).rightCellPadding(1);
table.row(true, label("Name").style(Decoration.bold.bold()),
label("Variability").style(Decoration.bold.bold()),
label("Units").style(Decoration.bold.bold()), label("Value").style(Decoration.bold.bold()));
} else {
table = new TableElement(4, 6).leftCellPadding(1).rightCellPadding(1);
table.row(true, label("Name").style(Decoration.bold.bold()),
label("Value").style(Decoration.bold.bold()));
}
for (PerfCounterVO counter : perfCounters) {
if (details) {
table.row(counter.getName(), counter.getVariability(),
counter.getUnits(), String.valueOf(counter.getValue()));
} else {
table.row(counter.getName(), String.valueOf(counter.getValue()));
}
}
process.write(RenderUtil.render(table, process.width()));
}
}
Loading…
Cancel
Save