Skip to content

Commit

Permalink
Filter zero pids from ps command.
Browse files Browse the repository at this point in the history
If one of processes will be not defined(0 be default) `ps` command fails.

PiperOrigin-RevId: 410805868
  • Loading branch information
wilwell authored and copybara-github committed Nov 18, 2021
1 parent 3ab612c commit 40c9b75
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -565,7 +565,9 @@ public Map<Long, WorkerMetric.WorkerStat> collectStats(OS os, List<Long> process
return pidResults;
}

String pids = Joiner.on(",").join(processIds);
List<Long> filteredProcessIds =
processIds.stream().filter(p -> p > 0).collect(Collectors.toList());
String pids = Joiner.on(",").join(filteredProcessIds);
BufferedReader psOutput;

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ private void assertRecordedResponsethrowsException(String recordedResponse, Stri
}

@Test
public void testCollectStats() throws Exception {
public void testCollectStats_ignoreSpaces() throws Exception {
WorkerSpawnRunner runner =
new WorkerSpawnRunner(
new SandboxHelpers(false),
Expand Down Expand Up @@ -425,6 +425,36 @@ public void testCollectStats() throws Exception {
assertThat(pidResults.get(2L).getUsedMemoryInKB()).isEqualTo(4);
}

@Test
public void testCollectStats_filterInvalidPids() throws Exception {
WorkerSpawnRunner runner =
new WorkerSpawnRunner(
new SandboxHelpers(false),
fs.getPath("/execRoot"),
createWorkerPool(),
reporter,
localEnvProvider,
/* binTools */ null,
resourceManager,
/* runfilestTreeUpdater */ null,
new WorkerOptions(),
eventBus,
runtime);

String psOutput = "PID RSS \n 1 3216";
InputStream psStream = new ByteArrayInputStream(psOutput.getBytes(UTF_8));
Process process = mock(Process.class);

when(runtime.exec(new String[] {"bash", "-c", "ps -o pid,rss -p 1"})).thenReturn(process);
when(process.getInputStream()).thenReturn(psStream);

List<Long> pids = Arrays.asList(1L, 0L);
Map<Long, WorkerMetric.WorkerStat> pidResults = runner.collectStats(OS.LINUX, pids);

assertThat(pidResults).hasSize(1);
assertThat(pidResults.get(1L).getUsedMemoryInKB()).isEqualTo(3);
}

@Test
public void testExecInWorker_showsLogFileInException() throws Exception {
assertRecordedResponsethrowsException("Some text", "unparseable WorkResponse!\n");
Expand Down

0 comments on commit 40c9b75

Please sign in to comment.