Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get children process memory #16

Merged
merged 1 commit into from
Feb 25, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 25 additions & 7 deletions src/main/scala/io/joern/benchmarks/runner/MemoryMonitor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,31 @@ object MemoryMonitor {
private val logger = LoggerFactory.getLogger(getClass)

private def getProcessMemoryUsage(pid: Long): Option[Long] = {
try {
val output = Seq("ps", "-o", "rss=", "-p", pid.toString).!!.trim
output.toLongOption.map(_ * 1024) // Convert from KB to bytes
} catch {
case x: Throwable =>
logger.error("Unable to measure memory at current epoch", x)
None
def getMemoryForPid(pid: Long): Option[Long] = {
try {
val output = Seq("ps", "-o", "rss=", "-p", pid.toString).!!.trim
output.toLongOption.map(_ * 1024) // Convert KB to bytes
} catch {
case x: Throwable =>
logger.error("Unable to measure memory at current epoch", x)
None
}
}

def getChildPids(parentPid: Long): Seq[Long] = {
try {
val output = Seq("pgrep", "-P", parentPid.toString).!!.trim
output.split("\\s+").flatMap(_.toLongOption)
} catch {
case x: Throwable =>
logger.error("Unable to measure memory for child processes at current epoch", x)
Seq.empty
}
}

getMemoryForPid(pid) match {
case None => None
case Some(pidMem) => Option(pidMem + getChildPids(pid).flatMap(getMemoryForPid).sum)
}
}

Expand Down