processes sort method added same as the oshi's demo

This commit is contained in:
rememberber 2021-12-20 16:15:32 +08:00
parent 6a6243be95
commit caae68cab6
2 changed files with 168 additions and 7 deletions

View File

@ -1,16 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.luoboduner.moo.info.ui.form.ProcessesForm"> <form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="com.luoboduner.moo.info.ui.form.ProcessesForm">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> <grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="20" left="20" bottom="20" right="20"/> <margin top="20" left="20" bottom="20" right="20"/>
<constraints> <constraints>
<xy x="20" y="20" width="500" height="400"/> <xy x="20" y="20" width="760" height="400"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
<children> <children>
<scrollpane id="306d1"> <scrollpane id="306d1">
<constraints> <constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> <grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints> </constraints>
<properties/> <properties/>
<border type="none"/> <border type="none"/>
@ -21,6 +21,77 @@
</component> </component>
</children> </children>
</scrollpane> </scrollpane>
<grid id="b5aed" layout-manager="GridLayoutManager" row-count="1" column-count="8" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="10" right="0"/>
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
</constraints>
<properties/>
<border type="none"/>
<children>
<component id="1fce3" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Sort by: "/>
</properties>
</component>
<hspacer id="496f9">
<constraints>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<component id="d0b3e" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="5" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CPU %: "/>
</properties>
</component>
<component id="23441" class="javax.swing.JRadioButton" binding="cpuButton">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="CPU %"/>
</properties>
</component>
<component id="a05b5" class="javax.swing.JRadioButton" binding="cumulativeCpuButton">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Cumulative CPU"/>
</properties>
</component>
<component id="5b233" class="javax.swing.JRadioButton" binding="memButton">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Memory %"/>
</properties>
</component>
<component id="a334" class="javax.swing.JRadioButton" binding="perProc">
<constraints>
<grid row="0" column="6" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="of one Processor"/>
</properties>
</component>
<component id="e33d0" class="javax.swing.JRadioButton" binding="perSystem">
<constraints>
<grid row="0" column="7" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="of System"/>
</properties>
</component>
</children>
</grid>
</children> </children>
</grid> </grid>
</form> </form>

View File

@ -4,6 +4,7 @@ import cn.hutool.log.Log;
import cn.hutool.log.LogFactory; import cn.hutool.log.LogFactory;
import com.intellij.uiDesigner.core.GridConstraints; import com.intellij.uiDesigner.core.GridConstraints;
import com.intellij.uiDesigner.core.GridLayoutManager; import com.intellij.uiDesigner.core.GridLayoutManager;
import com.intellij.uiDesigner.core.Spacer;
import com.luoboduner.moo.info.App; import com.luoboduner.moo.info.App;
import com.luoboduner.moo.info.ui.UiConsts; import com.luoboduner.moo.info.ui.UiConsts;
import lombok.Getter; import lombok.Getter;
@ -40,6 +41,11 @@ public class ProcessesForm {
private static ProcessesForm processesForm; private static ProcessesForm processesForm;
private JPanel mainPanel; private JPanel mainPanel;
private JTable processTable; private JTable processTable;
private JRadioButton cpuButton;
private JRadioButton cumulativeCpuButton;
private JRadioButton memButton;
private JRadioButton perProc;
private JRadioButton perSystem;
public static ProcessesForm getInstance() { public static ProcessesForm getInstance() {
if (processesForm == null) { if (processesForm == null) {
@ -56,6 +62,38 @@ public class ProcessesForm {
} }
private static void initUi() { private static void initUi() {
resetCpuPercentButtonGroup();
resetSortByButtonGroup();
processesForm = getInstance();
if (SystemInfo.getCurrentPlatform().equals(PlatformEnum.WINDOWS)) {
processesForm.perSystem.setSelected(true);
} else {
processesForm.perProc.setSelected(true);
}
processesForm.cpuButton.setSelected(true);
processesForm.perProc.addActionListener(e -> {
resetCpuPercentButtonGroup();
processesForm.perProc.setSelected(true);
});
processesForm.perSystem.addActionListener(e -> {
resetCpuPercentButtonGroup();
processesForm.perSystem.setSelected(true);
});
processesForm.cpuButton.addActionListener(e -> {
resetSortByButtonGroup();
processesForm.cpuButton.setSelected(true);
});
processesForm.cumulativeCpuButton.addActionListener(e -> {
resetSortByButtonGroup();
processesForm.cumulativeCpuButton.setSelected(true);
});
processesForm.memButton.addActionListener(e -> {
resetSortByButtonGroup();
processesForm.memButton.setSelected(true);
});
} }
/** /**
@ -105,6 +143,7 @@ public class ProcessesForm {
* @return * @return
*/ */
private static Object[][] parseProcesses(List<OSProcess> list, SystemInfo si) { private static Object[][] parseProcesses(List<OSProcess> list, SystemInfo si) {
processesForm = getInstance();
long totalMem = si.getHardware().getMemory().getTotal(); long totalMem = si.getHardware().getMemory().getTotal();
int cpuCount = si.getHardware().getProcessor().getLogicalProcessorCount(); int cpuCount = si.getHardware().getProcessor().getLogicalProcessorCount();
// Build a map with a value for each process to control the sort // Build a map with a value for each process to control the sort
@ -114,7 +153,13 @@ public class ProcessesForm {
// Ignore the Idle process on Windows // Ignore the Idle process on Windows
if (pid > 0 || !SystemInfo.getCurrentPlatform().equals(PlatformEnum.WINDOWS)) { if (pid > 0 || !SystemInfo.getCurrentPlatform().equals(PlatformEnum.WINDOWS)) {
// Set up for appropriate sort // Set up for appropriate sort
processSortValueMap.put(p, (double) p.getResidentSetSize()); if (processesForm.cpuButton.isSelected()) {
processSortValueMap.put(p, p.getProcessCpuLoadBetweenTicks(priorSnapshotMap.get(pid)));
} else if (processesForm.cumulativeCpuButton.isSelected()) {
processSortValueMap.put(p, p.getProcessCpuLoadCumulative());
} else {
processSortValueMap.put(p, (double) p.getResidentSetSize());
}
} }
} }
// Now sort the list by the values // Now sort the list by the values
@ -132,7 +177,11 @@ public class ProcessesForm {
procArr[i][0] = pid; procArr[i][0] = pid;
procArr[i][1] = p.getParentProcessID(); procArr[i][1] = p.getParentProcessID();
procArr[i][2] = p.getThreadCount(); procArr[i][2] = p.getThreadCount();
{ if (processesForm.perProc.isSelected()) {
procArr[i][3] = String.format("%.1f",
100d * p.getProcessCpuLoadBetweenTicks(priorSnapshotMap.get(pid)) / cpuCount);
procArr[i][4] = String.format("%.1f", 100d * p.getProcessCpuLoadCumulative() / cpuCount);
} else {
procArr[i][3] = String.format("%.1f", procArr[i][3] = String.format("%.1f",
100d * p.getProcessCpuLoadBetweenTicks(priorSnapshotMap.get(pid))); 100d * p.getProcessCpuLoadBetweenTicks(priorSnapshotMap.get(pid)));
procArr[i][4] = String.format("%.1f", 100d * p.getProcessCpuLoadCumulative()); procArr[i][4] = String.format("%.1f", 100d * p.getProcessCpuLoadCumulative());
@ -166,6 +215,21 @@ public class ProcessesForm {
} }
} }
private static void resetSortByButtonGroup() {
processesForm = getInstance();
processesForm.cpuButton.setSelected(false);
processesForm.cumulativeCpuButton.setSelected(false);
processesForm.memButton.setSelected(false);
}
private static void resetCpuPercentButtonGroup() {
processesForm = getInstance();
processesForm.perProc.setSelected(false);
processesForm.perSystem.setSelected(false);
}
{ {
// GUI initializer generated by IntelliJ IDEA GUI Designer // GUI initializer generated by IntelliJ IDEA GUI Designer
// >>> IMPORTANT!! <<< // >>> IMPORTANT!! <<<
@ -182,11 +246,37 @@ public class ProcessesForm {
*/ */
private void $$$setupUI$$$() { private void $$$setupUI$$$() {
mainPanel = new JPanel(); mainPanel = new JPanel();
mainPanel.setLayout(new GridLayoutManager(1, 1, new Insets(20, 20, 20, 20), -1, -1)); mainPanel.setLayout(new GridLayoutManager(2, 1, new Insets(20, 20, 20, 20), -1, -1));
final JScrollPane scrollPane1 = new JScrollPane(); final JScrollPane scrollPane1 = new JScrollPane();
mainPanel.add(scrollPane1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); mainPanel.add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false));
processTable = new JTable(); processTable = new JTable();
scrollPane1.setViewportView(processTable); scrollPane1.setViewportView(processTable);
final JPanel panel1 = new JPanel();
panel1.setLayout(new GridLayoutManager(1, 8, new Insets(0, 0, 10, 0), -1, -1));
mainPanel.add(panel1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false));
final JLabel label1 = new JLabel();
label1.setText("Sort by: ");
panel1.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
final Spacer spacer1 = new Spacer();
panel1.add(spacer1, new GridConstraints(0, 4, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_WANT_GROW, 1, null, null, null, 0, false));
final JLabel label2 = new JLabel();
label2.setText("CPU %: ");
panel1.add(label2, new GridConstraints(0, 5, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_FIXED, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
cpuButton = new JRadioButton();
cpuButton.setText("CPU %");
panel1.add(cpuButton, new GridConstraints(0, 1, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
cumulativeCpuButton = new JRadioButton();
cumulativeCpuButton.setText("Cumulative CPU");
panel1.add(cumulativeCpuButton, new GridConstraints(0, 2, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
memButton = new JRadioButton();
memButton.setText("Memory %");
panel1.add(memButton, new GridConstraints(0, 3, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
perProc = new JRadioButton();
perProc.setText("of one Processor");
panel1.add(perProc, new GridConstraints(0, 6, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
perSystem = new JRadioButton();
perSystem.setText("of System");
panel1.add(perSystem, new GridConstraints(0, 7, 1, 1, GridConstraints.ANCHOR_WEST, GridConstraints.FILL_NONE, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_FIXED, null, null, null, 0, false));
} }
/** /**