|
@@ -22,7 +22,6 @@ import java.io.BufferedWriter;
|
|
|
import java.io.File;
|
|
|
import java.io.FileWriter;
|
|
|
import java.io.IOException;
|
|
|
-import java.util.Arrays;
|
|
|
import java.util.Random;
|
|
|
import java.util.Vector;
|
|
|
import java.util.regex.Matcher;
|
|
@@ -183,12 +182,12 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
|
|
|
LOG.info("Process-tree dump follows: \n" + processTreeDump);
|
|
|
assertTrue("Process-tree dump doesn't start with a proper header",
|
|
|
- processTreeDump.startsWith("\t|- PID PPID PGRPID SESSID CMD_NAME " +
|
|
|
- "USER_MODE_TIME(MILLIS) SYSTEM_TIME(MILLIS) VMEM_USAGE(BYTES) " +
|
|
|
- "RSSMEM_USAGE(PAGES) FULL_CMD_LINE\n"));
|
|
|
+ processTreeDump.startsWith("\t|- PID PPID PGRPID SESSID CMD_NAME "
|
|
|
+ + "VMEM_USAGE(BYTES) FULL_CMD_LINE\n"));
|
|
|
for (int i = N; i >= 0; i--) {
|
|
|
- String cmdLineDump = "\\|- [0-9]+ [0-9]+ [0-9]+ [0-9]+ \\(sh\\)" +
|
|
|
- " [0-9]+ [0-9]+ [0-9]+ [0-9]+ sh " + shellScript + " " + i;
|
|
|
+ String cmdLineDump =
|
|
|
+ "\\|- [0-9]+ [0-9]+ [0-9]+ [0-9]+ \\(sh\\) [0-9]+ sh " + shellScript
|
|
|
+ + " " + i;
|
|
|
Pattern pat = Pattern.compile(cmdLineDump);
|
|
|
Matcher mat = pat.matcher(processTreeDump);
|
|
|
assertTrue("Process-tree dump doesn't contain the cmdLineDump of " + i
|
|
@@ -222,10 +221,7 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
String ppid;
|
|
|
String pgrpId;
|
|
|
String session;
|
|
|
- String vmem = "0";
|
|
|
- String rssmemPage = "0";
|
|
|
- String utime = "0";
|
|
|
- String stime = "0";
|
|
|
+ String vmem;
|
|
|
|
|
|
public ProcessStatInfo(String[] statEntries) {
|
|
|
pid = statEntries[0];
|
|
@@ -234,35 +230,27 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
pgrpId = statEntries[3];
|
|
|
session = statEntries[4];
|
|
|
vmem = statEntries[5];
|
|
|
- if (statEntries.length > 6) {
|
|
|
- rssmemPage = statEntries[6];
|
|
|
- }
|
|
|
- if (statEntries.length > 7) {
|
|
|
- utime = statEntries[7];
|
|
|
- stime = statEntries[8];
|
|
|
- }
|
|
|
}
|
|
|
|
|
|
// construct a line that mimics the procfs stat file.
|
|
|
// all unused numerical entries are set to 0.
|
|
|
public String getStatLine() {
|
|
|
return String.format("%s (%s) S %s %s %s 0 0 0" +
|
|
|
- " 0 0 0 0 %s %s 0 0 0 0 0 0 0 %s %s 0 0" +
|
|
|
+ " 0 0 0 0 0 0 0 0 0 0 0 0 0 %s 0 0 0" +
|
|
|
" 0 0 0 0 0 0 0 0" +
|
|
|
" 0 0 0 0 0",
|
|
|
- pid, name, ppid, pgrpId, session,
|
|
|
- utime, stime, vmem, rssmemPage);
|
|
|
+ pid, name, ppid, pgrpId, session, vmem);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* A basic test that creates a few process directories and writes
|
|
|
- * stat files. Verifies that the cpu time and memory is correctly
|
|
|
+ * stat files. Verifies that the virtual memory is correctly
|
|
|
* computed.
|
|
|
* @throws IOException if there was a problem setting up the
|
|
|
* fake procfs directories or files.
|
|
|
*/
|
|
|
- public void testCpuAndMemoryForProcessTree() throws IOException {
|
|
|
+ public void testVirtualMemoryForProcessTree() throws IOException {
|
|
|
|
|
|
// test processes
|
|
|
String[] pids = { "100", "200", "300", "400" };
|
|
@@ -277,13 +265,13 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
// assuming processes 100, 200, 300 are in tree and 400 is not.
|
|
|
ProcessStatInfo[] procInfos = new ProcessStatInfo[4];
|
|
|
procInfos[0] = new ProcessStatInfo(new String[]
|
|
|
- {"100", "proc1", "1", "100", "100", "100000", "100", "1000", "200"});
|
|
|
+ {"100", "proc1", "1", "100", "100", "100000"});
|
|
|
procInfos[1] = new ProcessStatInfo(new String[]
|
|
|
- {"200", "proc2", "100", "100", "100", "200000", "200", "2000", "400"});
|
|
|
+ {"200", "proc2", "100", "100", "100", "200000"});
|
|
|
procInfos[2] = new ProcessStatInfo(new String[]
|
|
|
- {"300", "proc3", "200", "100", "100", "300000", "300", "3000", "600"});
|
|
|
+ {"300", "proc3", "200", "100", "100", "300000"});
|
|
|
procInfos[3] = new ProcessStatInfo(new String[]
|
|
|
- {"400", "proc4", "1", "400", "400", "400000", "400", "4000", "800"});
|
|
|
+ {"400", "proc4", "1", "400", "400", "400000"});
|
|
|
|
|
|
writeStatFiles(procfsRootDir, pids, procInfos);
|
|
|
|
|
@@ -294,36 +282,8 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
processTree.getProcessTree();
|
|
|
|
|
|
// verify cumulative memory
|
|
|
- assertEquals("Cumulative virtual memory does not match", 600000L,
|
|
|
- processTree.getCumulativeVmem());
|
|
|
-
|
|
|
- // verify rss memory
|
|
|
- long cumuRssMem = ProcfsBasedProcessTree.PAGE_SIZE > 0 ?
|
|
|
- 600L * ProcfsBasedProcessTree.PAGE_SIZE : 0L;
|
|
|
- assertEquals("Cumulative rss memory does not match",
|
|
|
- cumuRssMem, processTree.getCumulativeRssmem());
|
|
|
-
|
|
|
- // verify cumulative cpu time
|
|
|
- long cumuCpuTime = ProcfsBasedProcessTree.JIFFY_LENGTH_IN_MILLIS > 0 ?
|
|
|
- 7200L * ProcfsBasedProcessTree.JIFFY_LENGTH_IN_MILLIS : 0L;
|
|
|
- assertEquals("Cumulative cpu time does not match",
|
|
|
- cumuCpuTime, processTree.getCumulativeCpuTime());
|
|
|
-
|
|
|
- // test the cpu time again to see if it cumulates
|
|
|
- procInfos[0] = new ProcessStatInfo(new String[]
|
|
|
- {"100", "proc1", "1", "100", "100", "100000", "100", "2000", "300"});
|
|
|
- procInfos[1] = new ProcessStatInfo(new String[]
|
|
|
- {"200", "proc2", "100", "100", "100", "200000", "200", "3000", "500"});
|
|
|
- writeStatFiles(procfsRootDir, pids, procInfos);
|
|
|
-
|
|
|
- // build the process tree.
|
|
|
- processTree.getProcessTree();
|
|
|
-
|
|
|
- // verify cumulative cpu time again
|
|
|
- cumuCpuTime = ProcfsBasedProcessTree.JIFFY_LENGTH_IN_MILLIS > 0 ?
|
|
|
- 9400L * ProcfsBasedProcessTree.JIFFY_LENGTH_IN_MILLIS : 0L;
|
|
|
- assertEquals("Cumulative cpu time does not match",
|
|
|
- cumuCpuTime, processTree.getCumulativeCpuTime());
|
|
|
+ assertEquals("Cumulative memory does not match",
|
|
|
+ Long.parseLong("600000"), processTree.getCumulativeVmem());
|
|
|
} finally {
|
|
|
FileUtil.fullyDelete(procfsRootDir);
|
|
|
}
|
|
@@ -335,7 +295,7 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
* @throws IOException if there was a problem setting up the
|
|
|
* fake procfs directories or files.
|
|
|
*/
|
|
|
- public void testMemForOlderProcesses() throws IOException {
|
|
|
+ public void testVMemForOlderProcesses() throws IOException {
|
|
|
// initial list of processes
|
|
|
String[] pids = { "100", "200", "300", "400" };
|
|
|
// create the fake procfs root directory.
|
|
@@ -349,13 +309,13 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
// assuming 100, 200 and 400 are in tree, 300 is not.
|
|
|
ProcessStatInfo[] procInfos = new ProcessStatInfo[4];
|
|
|
procInfos[0] = new ProcessStatInfo(new String[]
|
|
|
- {"100", "proc1", "1", "100", "100", "100000", "100"});
|
|
|
+ {"100", "proc1", "1", "100", "100", "100000"});
|
|
|
procInfos[1] = new ProcessStatInfo(new String[]
|
|
|
- {"200", "proc2", "100", "100", "100", "200000", "200"});
|
|
|
+ {"200", "proc2", "100", "100", "100", "200000"});
|
|
|
procInfos[2] = new ProcessStatInfo(new String[]
|
|
|
- {"300", "proc3", "1", "300", "300", "300000", "300"});
|
|
|
+ {"300", "proc3", "1", "300", "300", "300000"});
|
|
|
procInfos[3] = new ProcessStatInfo(new String[]
|
|
|
- {"400", "proc4", "100", "100", "100", "400000", "400"});
|
|
|
+ {"400", "proc4", "100", "100", "100", "400000"});
|
|
|
|
|
|
writeStatFiles(procfsRootDir, pids, procInfos);
|
|
|
|
|
@@ -366,69 +326,51 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
processTree.getProcessTree();
|
|
|
|
|
|
// verify cumulative memory
|
|
|
- assertEquals("Cumulative memory does not match",
|
|
|
- 700000L, processTree.getCumulativeVmem());
|
|
|
-
|
|
|
+ assertEquals("Cumulative memory does not match",
|
|
|
+ Long.parseLong("700000"), processTree.getCumulativeVmem());
|
|
|
+
|
|
|
// write one more process as child of 100.
|
|
|
String[] newPids = { "500" };
|
|
|
setupPidDirs(procfsRootDir, newPids);
|
|
|
|
|
|
ProcessStatInfo[] newProcInfos = new ProcessStatInfo[1];
|
|
|
newProcInfos[0] = new ProcessStatInfo(new String[]
|
|
|
- {"500", "proc5", "100", "100", "100", "500000", "500"});
|
|
|
+ {"500", "proc5", "100", "100", "100", "500000"});
|
|
|
writeStatFiles(procfsRootDir, newPids, newProcInfos);
|
|
|
|
|
|
- // check memory includes the new process.
|
|
|
+ // check vmem includes the new process.
|
|
|
processTree.getProcessTree();
|
|
|
- assertEquals("Cumulative vmem does not include new process",
|
|
|
- 1200000L, processTree.getCumulativeVmem());
|
|
|
- long cumuRssMem = ProcfsBasedProcessTree.PAGE_SIZE > 0 ?
|
|
|
- 1200L * ProcfsBasedProcessTree.PAGE_SIZE : 0L;
|
|
|
- assertEquals("Cumulative rssmem does not include new process",
|
|
|
- cumuRssMem, processTree.getCumulativeRssmem());
|
|
|
+ assertEquals("Cumulative memory does not include new process",
|
|
|
+ Long.parseLong("1200000"), processTree.getCumulativeVmem());
|
|
|
|
|
|
// however processes older than 1 iteration will retain the older value
|
|
|
- assertEquals("Cumulative vmem shouldn't have included new process",
|
|
|
- 700000L, processTree.getCumulativeVmem(1));
|
|
|
- cumuRssMem = ProcfsBasedProcessTree.PAGE_SIZE > 0 ?
|
|
|
- 700L * ProcfsBasedProcessTree.PAGE_SIZE : 0L;
|
|
|
- assertEquals("Cumulative rssmem shouldn't have included new process",
|
|
|
- cumuRssMem, processTree.getCumulativeRssmem(1));
|
|
|
-
|
|
|
+ assertEquals("Cumulative memory shouldn't have included new process",
|
|
|
+ Long.parseLong("700000"), processTree.getCumulativeVmem(1));
|
|
|
+
|
|
|
// one more process
|
|
|
newPids = new String[]{ "600" };
|
|
|
setupPidDirs(procfsRootDir, newPids);
|
|
|
|
|
|
newProcInfos = new ProcessStatInfo[1];
|
|
|
newProcInfos[0] = new ProcessStatInfo(new String[]
|
|
|
- {"600", "proc6", "100", "100", "100", "600000", "600"});
|
|
|
+ {"600", "proc6", "100", "100", "100", "600000"});
|
|
|
writeStatFiles(procfsRootDir, newPids, newProcInfos);
|
|
|
|
|
|
// refresh process tree
|
|
|
processTree.getProcessTree();
|
|
|
|
|
|
// processes older than 2 iterations should be same as before.
|
|
|
- assertEquals("Cumulative vmem shouldn't have included new processes",
|
|
|
- 700000L, processTree.getCumulativeVmem(2));
|
|
|
- cumuRssMem = ProcfsBasedProcessTree.PAGE_SIZE > 0 ?
|
|
|
- 700L * ProcfsBasedProcessTree.PAGE_SIZE : 0L;
|
|
|
- assertEquals("Cumulative rssmem shouldn't have included new processes",
|
|
|
- cumuRssMem, processTree.getCumulativeRssmem(2));
|
|
|
-
|
|
|
+ assertEquals("Cumulative memory shouldn't have included new processes",
|
|
|
+ Long.parseLong("700000"), processTree.getCumulativeVmem(2));
|
|
|
+
|
|
|
// processes older than 1 iteration should not include new process,
|
|
|
// but include process 500
|
|
|
- assertEquals("Cumulative vmem shouldn't have included new processes",
|
|
|
- 1200000L, processTree.getCumulativeVmem(1));
|
|
|
- cumuRssMem = ProcfsBasedProcessTree.PAGE_SIZE > 0 ?
|
|
|
- 1200L * ProcfsBasedProcessTree.PAGE_SIZE : 0L;
|
|
|
- assertEquals("Cumulative rssmem shouldn't have included new processes",
|
|
|
- cumuRssMem, processTree.getCumulativeRssmem(1));
|
|
|
-
|
|
|
+ assertEquals("Cumulative memory shouldn't have included new processes",
|
|
|
+ Long.parseLong("1200000"), processTree.getCumulativeVmem(1));
|
|
|
+
|
|
|
// no processes older than 3 iterations, this should be 0
|
|
|
assertEquals("Getting non-zero vmem for processes older than 3 iterations",
|
|
|
0L, processTree.getCumulativeVmem(3));
|
|
|
- assertEquals("Getting non-zero rssmem for processes older than 3 iterations",
|
|
|
- 0L, processTree.getCumulativeRssmem(3));
|
|
|
} finally {
|
|
|
FileUtil.fullyDelete(procfsRootDir);
|
|
|
}
|
|
@@ -453,18 +395,24 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
int numProcesses = pids.length;
|
|
|
// Processes 200, 300, 400 and 500 are descendants of 100. 600 is not.
|
|
|
ProcessStatInfo[] procInfos = new ProcessStatInfo[numProcesses];
|
|
|
- procInfos[0] = new ProcessStatInfo(new String[] {
|
|
|
- "100", "proc1", "1", "100", "100", "100000", "100", "1000", "200"});
|
|
|
- procInfos[1] = new ProcessStatInfo(new String[] {
|
|
|
- "200", "proc2", "100", "100", "100", "200000", "200", "2000", "400"});
|
|
|
- procInfos[2] = new ProcessStatInfo(new String[] {
|
|
|
- "300", "proc3", "200", "100", "100", "300000", "300", "3000", "600"});
|
|
|
- procInfos[3] = new ProcessStatInfo(new String[] {
|
|
|
- "400", "proc4", "200", "100", "100", "400000", "400", "4000", "800"});
|
|
|
- procInfos[4] = new ProcessStatInfo(new String[] {
|
|
|
- "500", "proc5", "400", "100", "100", "400000", "400", "4000", "800"});
|
|
|
- procInfos[5] = new ProcessStatInfo(new String[] {
|
|
|
- "600", "proc6", "1", "1", "1", "400000", "400", "4000", "800"});
|
|
|
+ procInfos[0] =
|
|
|
+ new ProcessStatInfo(new String[] { "100", "proc1", "1", "100",
|
|
|
+ "100", "100000" });
|
|
|
+ procInfos[1] =
|
|
|
+ new ProcessStatInfo(new String[] { "200", "proc2", "100", "100",
|
|
|
+ "100", "200000" });
|
|
|
+ procInfos[2] =
|
|
|
+ new ProcessStatInfo(new String[] { "300", "proc3", "200", "100",
|
|
|
+ "100", "300000" });
|
|
|
+ procInfos[3] =
|
|
|
+ new ProcessStatInfo(new String[] { "400", "proc4", "200", "100",
|
|
|
+ "100", "400000" });
|
|
|
+ procInfos[4] =
|
|
|
+ new ProcessStatInfo(new String[] { "500", "proc5", "400", "100",
|
|
|
+ "100", "400000" });
|
|
|
+ procInfos[5] =
|
|
|
+ new ProcessStatInfo(new String[] { "600", "proc6", "1", "1", "1",
|
|
|
+ "400000" });
|
|
|
|
|
|
String[] cmdLines = new String[numProcesses];
|
|
|
cmdLines[0] = "proc1 arg1 arg2";
|
|
@@ -487,17 +435,15 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
|
|
|
LOG.info("Process-tree dump follows: \n" + processTreeDump);
|
|
|
assertTrue("Process-tree dump doesn't start with a proper header",
|
|
|
- processTreeDump.startsWith("\t|- PID PPID PGRPID SESSID CMD_NAME " +
|
|
|
- "USER_MODE_TIME(MILLIS) SYSTEM_TIME(MILLIS) VMEM_USAGE(BYTES) " +
|
|
|
- "RSSMEM_USAGE(PAGES) FULL_CMD_LINE\n"));
|
|
|
+ processTreeDump.startsWith("\t|- PID PPID PGRPID SESSID CMD_NAME "
|
|
|
+ + "VMEM_USAGE(BYTES) FULL_CMD_LINE\n"));
|
|
|
for (int i = 0; i < 5; i++) {
|
|
|
ProcessStatInfo p = procInfos[i];
|
|
|
assertTrue(
|
|
|
"Process-tree dump doesn't contain the cmdLineDump of process "
|
|
|
+ p.pid, processTreeDump.contains("\t|- " + p.pid + " "
|
|
|
+ p.ppid + " " + p.pgrpId + " " + p.session + " (" + p.name
|
|
|
- + ") " + p.utime + " " + p.stime + " " + p.vmem + " "
|
|
|
- + p.rssmemPage + " " + cmdLines[i]));
|
|
|
+ + ") " + p.vmem + " " + cmdLines[i]));
|
|
|
}
|
|
|
|
|
|
// 600 should not be in the dump
|
|
@@ -506,7 +452,7 @@ public class TestProcfsBasedProcessTree extends TestCase {
|
|
|
"Process-tree dump shouldn't contain the cmdLineDump of process "
|
|
|
+ p.pid, processTreeDump.contains("\t|- " + p.pid + " " + p.ppid
|
|
|
+ " " + p.pgrpId + " " + p.session + " (" + p.name + ") "
|
|
|
- + p.utime + " " + p.stime + " " + p.vmem + " " + cmdLines[5]));
|
|
|
+ + p.vmem + " " + cmdLines[5]));
|
|
|
} finally {
|
|
|
FileUtil.fullyDelete(procfsRootDir);
|
|
|
}
|